Project

General

Profile

1
import {Component, EventEmitter, Input, Output, ViewChild} from '@angular/core';
2
import {ActivatedRoute} from '@angular/router';
3
import {AdvancedField} from './searchUtils/searchHelperClasses.class';
4
import {SearchDataprovidersService} from '../services/searchDataproviders.service';
5
import {ErrorCodes} from '../utils/properties/errorCodes';
6
import {ErrorMessagesComponent} from '../utils/errorMessages.component';
7
import {SearchFields} from '../utils/properties/searchFields';
8
import {SearchCustomFilter, SearchUtilsClass} from './searchUtils/searchUtils.class';
9

    
10
import {EnvProperties} from '../utils/properties/env-properties';
11
import {NewSearchPageComponent} from "./searchUtils/newSearchPage.component";
12
import {DatasourcesHelperClass} from "./searchUtils/datasourcesHelper.class";
13

    
14

    
15
@Component({
16
    selector: 'search-dataproviders',
17
    template: `
18
 
19
    <new-search-page
20
      pageTitle="{{(simpleView?'':'Advanced ')}} Search for {{ pageTitle }}"
21
      entityType="dataprovider"
22
      type="content providers"
23
      [results]="results"
24
      [searchUtils]="searchUtils"
25
      [fieldIds]="fieldIds" [fieldIdsMap]="fieldIdsMap" [selectedFields]="selectedFields"
26
      [csvParams]="csvParams" csvPath="datasources"
27
      [simpleSearchLink]="simpleSearchLink" [advancedSearchLink]="advancedSearchLink"
28
      [disableForms]="disableForms"
29
      [loadPaging]="loadPaging"
30
      [oldTotalResults]="oldTotalResults"
31
      [openaireLink]=openaireLink
32
      [includeOnlyResultsAndFilter]="includeOnlyResultsAndFilter"
33
      [piwikSiteId]=piwikSiteId [hasPrefix]="hasPrefix"
34
      searchFormClass="datasourcesSearchForm"
35
      [entitiesSelection]="type=='all'"  [showAdvancedSearchLink]="type=='all'" 
36
      [filters]="filters"
37
      [simpleView]="simpleView" formPlaceholderText="Search by title, country, organization, subject, type..." 
38
      [showResultCount]="type!='registries'">
39
    </new-search-page>
40

    
41
    `
42
 })
43

    
44
export class SearchDataProvidersComponent {
45
  private errorCodes: ErrorCodes;
46
  private errorMessages: ErrorMessagesComponent;
47
@Input() piwikSiteId = null;
48
@Input() customFilter:SearchCustomFilter= null;
49
  public results =[];
50
  public filters =[];
51
  public searchUtils:SearchUtilsClass = new SearchUtilsClass();
52
  public searchFields:SearchFields = new SearchFields();
53

    
54
  public fieldIds:  string[] = this.searchFields.DATASOURCE_ADVANCED_FIELDS;
55
  public fieldIdsMap= this.searchFields.DATASOURCE_FIELDS;
56
  public selectedFields:AdvancedField[] =  [];
57

    
58
  public resourcesQuery = "(oaftype exact datasource)";
59
  public csvParams: string;
60
  public disableForms: boolean = false;
61
  public loadPaging: boolean = true;
62
  public oldTotalResults: number = 0;
63
  public pagingLimit: number = 0;
64
  public isPiwikEnabled;
65
  properties:EnvProperties;
66
  @Input() type: "all" | "registries" | "journals" | "compatible" ="all";
67
  public refineFields: string[] = DatasourcesHelperClass.getrefineFields(this.type);
68
  pageTitle = DatasourcesHelperClass.getTitle(this.type);
69
  @ViewChild(NewSearchPageComponent) searchPage: NewSearchPageComponent;
70
  @Input() simpleView: boolean = true;
71
  @Input() simpleSearchLink: string = "";
72
  advancedSearchLink: string = "";
73
  @Input() hasPrefix: boolean = true;
74
  @Input() openaireLink: string;
75
  @Input() includeOnlyResultsAndFilter: boolean = false;
76
  @Output() searchPageUpdates = new EventEmitter();
77
  constructor (private route: ActivatedRoute, private _searchDataProvidersService: SearchDataprovidersService ) {
78
    this.results =[];
79
    this.errorCodes = new ErrorCodes();
80
    this.errorMessages = new ErrorMessagesComponent();
81
    this.searchUtils.status = this.errorCodes.LOADING;
82

    
83
    //console.info("Con -base url:"+this.searchUtils.baseUrl );
84

    
85
  }
86
  ngOnInit() {
87
    this.route.data
88
      .subscribe((data: { envSpecific: EnvProperties }) => {
89
        this.properties= data.envSpecific;
90
        if (!this.simpleSearchLink) {
91
          this.simpleSearchLink = this.properties.searchLinkToDataProviders;
92
        }        this.advancedSearchLink = this.properties.searchLinkToAdvancedDataProviders;
93
        this.searchUtils.baseUrl = (this.simpleView)?this.simpleSearchLink:this.advancedSearchLink;
94
         this.pagingLimit = data.envSpecific.pagingLimit;
95
         this.isPiwikEnabled = data.envSpecific.enablePiwikTrack;
96

    
97
      });
98

    
99
    let firstLoad = true;
100
    this.filters = DatasourcesHelperClass.createFilters(this.type);
101
    this.sub =  this.route.queryParams.subscribe(params => {
102
     this.loadPaging = true;
103
     if(params['page'] && this.searchUtils.page != params['page']) {
104
       this.loadPaging = false;
105
       this.oldTotalResults = this.searchUtils.totalResults;
106
     }
107
     var refine = true;
108
     if (this.searchUtils.page != ((params['page'] === undefined) ? 1 : +params['page']) && this.filters && !firstLoad) {
109
       refine = false;
110

    
111
     }
112
    let page = (params['page']=== undefined)?0:+params['page'];
113
    this.searchUtils.page = ( page < 1 ) ? 1 : page;
114

    
115
    this.searchUtils.size = (params['size']=== undefined)?10:+params['size'];
116
    if(this.searchUtils.size != 5 && this.searchUtils.size != 10 && this.searchUtils.size != 20 && this.searchUtils.size != 50) {
117
      this.searchUtils.size = 10;
118
    }
119

    
120
    this.searchPage.fieldIds = this.fieldIds;
121
    this.selectedFields =[];
122
    this.searchPage.prepareSearchPage(this.fieldIds, this.selectedFields, this.refineFields, [], this.fieldIdsMap,this.customFilter,params, "dataprovider");
123
    this.getResults(this.searchPage.getSearchAPIQueryForAdvancedSearhFields(),  this.searchUtils.page, this.searchUtils.size, refine, this.searchPage.getSearchAPIQueryForRefineFields(params, firstLoad));
124
    });
125
  }
126
  ngOnDestroy() {
127
    this.sub.unsubscribe();
128
  }
129
  sub: any;
130
  public getResults(parameters:string, page: number, size: number, refine: boolean, refineFieldsFilterQuery = null){
131
    if(page > this.pagingLimit ) {
132
      size=0;
133
    }
134
    if(page <= this.pagingLimit  || this.searchUtils.status == this.errorCodes.LOADING) {
135
      // if(parameters!= null && parameters != ''  ) {
136
      //   this.csvParams ="&fq=( "+this.resourcesQuery + "and (" + parameters + "))";
137
      // }else{
138
      //   this.csvParams ="&fq="+this.resourcesQuery;
139
      // }
140
      // this.csvParams += (refineFieldsFilterQuery?refineFieldsFilterQuery:'');
141

    
142
      this.csvParams = (parameters ? ("&fq=("+parameters) : "") + (parameters ? ")" : "");
143
      this.csvParams += (refineFieldsFilterQuery ? refineFieldsFilterQuery : "");
144

    
145
      //var errorCodes:ErrorCodes = new ErrorCodes();
146
      this.searchUtils.status = this.errorCodes.LOADING;
147
      //this.searchPage.openLoading();
148
      this.disableForms = true;
149
      this.searchPageUpdates.emit({disableForms: this.disableForms,searchUtils: this.searchUtils});
150

    
151
      this.results = [];
152
      this.searchUtils.totalResults = 0;
153

    
154
      //console.info("Advanced Search for Content Providers: Execute search query "+parameters);
155
      let datasourceQueryPrefix = DatasourcesHelperClass.getQueryPrefix(this.type);
156
      this._searchDataProvidersService.advancedSearchDataproviders( datasourceQueryPrefix +(datasourceQueryPrefix.length > 0 && parameters.length > 0 ?' and ':'') + parameters,  page, size, this.properties, (refine && this.type=="all") ? this.searchPage.getRefineFieldsQuery() : null, this.searchPage.getFields(), refineFieldsFilterQuery).subscribe(
157
         data => {
158
            this.searchUtils.totalResults = data[0];
159
            this.results = data[1];
160
           if (refine) {
161
             this.filters = this.searchPage.prepareFiltersToShow(this.type=="all"?data[2]:this.filters);
162
           }else{
163
             this.searchPage.buildPageURLParameters(this.filters, [], false);
164
           }
165
            // this.searchPage.updateBaseUrlWithParameters();
166
            //var errorCodes:ErrorCodes = new ErrorCodes();
167
            this.searchUtils.status = this.errorCodes.DONE;
168
            if(this.searchUtils.totalResults == 0 ){
169
              this.searchUtils.status = this.errorCodes.NONE;
170
            }
171
            //this.searchPage.closeLoading();
172
            this.disableForms = false;
173
           this.searchPageUpdates.emit({disableForms: this.disableForms,searchUtils: this.searchUtils});
174

    
175
            if(this.searchUtils.status == this.errorCodes.DONE) {
176
              // Page out of limit
177
              let totalPages:any = this.searchUtils.totalResults/(this.searchUtils.size);
178
              if(!(Number.isInteger(totalPages))) {
179
                  totalPages = (parseInt(totalPages, 10) + 1);
180
              }
181
              if(totalPages < page) {
182
                this.searchUtils.totalResults = 0;
183
                this.searchUtils.status = this.errorCodes.OUT_OF_BOUND;
184
              }
185
            }
186
          },
187
          err => {
188
              //console.log(err);
189
              this.handleError("Error getting content providers", err);
190
              this.searchUtils.status = this.errorMessages.getErrorCode(err.status);
191

    
192
              //TODO check erros (service not available, bad request)
193
              // if( ){
194
              //   this.searchUtils.status = errorCodes.ERROR;
195
              // }
196
              //var errorCodes:ErrorCodes = new ErrorCodes();
197
              //this.searchUtils.status = errorCodes.NOT_AVAILABLE;
198
              /*if(err.status == '404') {
199
                this.searchUtils.status = this.errorCodes.NOT_FOUND;
200
              } else if(err.status == '500') {
201
                this.searchUtils.status = this.errorCodes.ERROR;
202
              } else {
203
                this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
204
              }*/
205
              //this.searchPage.closeLoading();
206
              this.disableForms = false;
207
            this.searchPageUpdates.emit({disableForms: this.disableForms,searchUtils: this.searchUtils});
208

    
209
          }
210
        );
211
      }
212
  }
213

    
214
  private handleError(message: string, error) {
215
    console.error("Content Providers advanced Search Page: "+message, error);
216
  }
217
}
(1-1/8)