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]="type!='deposit'?'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=='all' || type == 'deposit')" [showLastIndex]="type!='deposit'"
39
      [tableViewLink]="tableViewLink">
40
      
41
    </new-search-page>
42

    
43
    `
44
 })
45

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

    
57
  public fieldIds:  string[] = this.searchFields.DATASOURCE_ADVANCED_FIELDS;
58
  public fieldIdsMap= this.searchFields.DATASOURCE_FIELDS;
59
  public selectedFields:AdvancedField[] =  [];
60

    
61
  public resourcesQuery = "(oaftype exact datasource)";
62
  public csvParams: string;
63
  public disableForms: boolean = false;
64
  public loadPaging: boolean = true;
65
  public oldTotalResults: number = 0;
66
  public pagingLimit: number = 0;
67
  public isPiwikEnabled;
68
  properties:EnvProperties;
69
  @Input() type: "all" | "registries" | "journals" | "compatible" | "deposit" = "all";
70
  public refineFields: string[];
71
  pageTitle;
72
  @ViewChild(NewSearchPageComponent) searchPage: NewSearchPageComponent;
73
  @Input() simpleView: boolean = true;
74
  @Input() simpleSearchLink: string = "";
75
  advancedSearchLink: string = "";
76
  @Input() hasPrefix: boolean = true;
77
  @Input() openaireLink: string;
78
  @Input() includeOnlyResultsAndFilter: boolean = false;
79
  @Output() searchPageUpdates = new EventEmitter();
80
  constructor (private route: ActivatedRoute, private _searchDataProvidersService: SearchDataprovidersService ) {
81
    this.results =[];
82
    this.errorCodes = new ErrorCodes();
83
    this.errorMessages = new ErrorMessagesComponent();
84
    this.searchUtils.status = this.errorCodes.LOADING;
85
  }
86
  ngOnInit() {
87
    this.refineFields = DatasourcesHelperClass.getrefineFields(this.type);
88
    this.pageTitle = DatasourcesHelperClass.getTitle(this.type);
89
    this.route.data
90
      .subscribe((data: { envSpecific: EnvProperties }) => {
91
        this.properties= data.envSpecific;
92
        if (!this.simpleSearchLink) {
93
          this.simpleSearchLink = this.properties.searchLinkToDataProviders;
94
        }        this.advancedSearchLink = this.properties.searchLinkToAdvancedDataProviders;
95
        this.searchUtils.baseUrl = (this.simpleView)?this.simpleSearchLink:this.advancedSearchLink;
96
         this.pagingLimit = data.envSpecific.pagingLimit;
97
         this.isPiwikEnabled = data.envSpecific.enablePiwikTrack;
98

    
99
      });
100

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

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

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

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

    
149
      this.csvParams = (parameters ? ("&fq=("+parameters) : "") + (parameters ? ")" : "");
150
      this.csvParams += (refineFieldsFilterQuery ? refineFieldsFilterQuery : "");
151

    
152
      //var errorCodes:ErrorCodes = new ErrorCodes();
153
      this.searchUtils.status = this.errorCodes.LOADING;
154
      //this.searchPage.openLoading();
155
      this.disableForms = true;
156
      this.searchPageUpdates.emit({disableForms: this.disableForms,searchUtils: this.searchUtils});
157

    
158
      this.results = [];
159
      this.searchUtils.totalResults = 0;
160

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

    
183
            if(this.searchUtils.status == this.errorCodes.DONE) {
184
              // Page out of limit
185
              let totalPages:any = this.searchUtils.totalResults/(this.searchUtils.size);
186
              if(!(Number.isInteger(totalPages))) {
187
                  totalPages = (parseInt(totalPages, 10) + 1);
188
              }
189
              if(totalPages < page) {
190
                this.searchUtils.totalResults = 0;
191
                this.searchUtils.status = this.errorCodes.OUT_OF_BOUND;
192
              }
193
            }
194
          },
195
          err => {
196
              //console.log(err);
197
              this.handleError("Error getting content providers", err);
198
              this.searchUtils.status = this.errorMessages.getErrorCode(err.status);
199

    
200
              //TODO check erros (service not available, bad request)
201
              // if( ){
202
              //   this.searchUtils.status = errorCodes.ERROR;
203
              // }
204
              //var errorCodes:ErrorCodes = new ErrorCodes();
205
              //this.searchUtils.status = errorCodes.NOT_AVAILABLE;
206
              /*if(err.status == '404') {
207
                this.searchUtils.status = this.errorCodes.NOT_FOUND;
208
              } else if(err.status == '500') {
209
                this.searchUtils.status = this.errorCodes.ERROR;
210
              } else {
211
                this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
212
              }*/
213
              //this.searchPage.closeLoading();
214
              this.disableForms = false;
215
            this.searchPageUpdates.emit({disableForms: this.disableForms,searchUtils: this.searchUtils});
216

    
217
          }
218
        );
219
      }
220
  }
221

    
222
  private handleError(message: string, error) {
223
    console.error("Content Providers advanced Search Page: "+message, error);
224
  }
225
}
(1-1/8)