Project

General

Profile

1
import {Component, Input, ViewChild} from '@angular/core';
2
import {ActivatedRoute} from '@angular/router';
3
import {Location} from '@angular/common';
4

    
5
import {SearchPublicationsService} from '../../services/searchPublications.service';
6
import {ErrorCodes} from '../../utils/properties/errorCodes';
7
import {ErrorMessagesComponent} from '../../utils/errorMessages.component';
8
import {SearchFields} from '../../utils/properties/searchFields';
9
import {SearchPageComponent} from '../searchUtils/searchPage.component';
10
import {SearchCustomFilter, SearchUtilsClass} from '../searchUtils/searchUtils.class';
11
import {DOI} from '../../utils/string-utils.class';
12
import {EnvProperties} from '../../utils/properties/env-properties';
13

    
14
@Component({
15
    selector: 'search-publications',
16
    template: `
17
<!--      [searchFormClass]="customFilter.queryFieldName == 'communityId' ? 'communityPanelBackground' : 'publicationsSearchForm'"-->
18

    
19
    <search-page  pageTitle="Search Publications"
20
                  formPlaceholderText = "Search for Publications"
21
                 type="publications" entityType="publication"
22
                 [(filters)] = "filters" [(results)] = "results"
23
                 [(searchUtils)] = "searchUtils"   [(baseUrl)] = baseUrl
24
                 (queryChange)="queryChanged($event)"
25
                 [csvParams]="csvParams" csvPath="publications"
26
                 advancedSearchLink="/search/advanced/publications"
27
                 [disableForms]="disableForms"
28
                 [loadPaging]="loadPaging"
29
                 [oldTotalResults]="oldTotalResults" 
30
                 [searchFormClass]="customFilter && customFilter.queryFieldName == 'communityId' ? 
31
                    'communityPanelBackground' : 'publicationsSearchForm'"
32
                 [(openaireLink)]=openaireLink
33
                 [(advancedSearchParameters)]=advancedSearchParameters
34
                  [piwikSiteId]=piwikSiteId
35
                 [(sort)]=sort >
36
    </search-page>
37

    
38
    `
39

    
40
})
41
export class SearchPublicationsComponent {
42
  private errorCodes: ErrorCodes;
43
  private errorMessages: ErrorMessagesComponent;
44
@Input() piwikSiteId = null;
45
  public results =[];
46
  public filters =[];
47
  public searchUtils:SearchUtilsClass = new SearchUtilsClass();
48
  public baseUrl:string = "";
49
  public sub: any;
50
  public subResults: any;
51
  public searchFields:SearchFields = new SearchFields();
52
  public refineFields: string[] =  this.searchFields.RESULT_REFINE_FIELDS;
53
  public fieldIdsMap=this.searchFields.RESULT_FIELDS;
54
    //:  { [key:string] :{ name:string, operator:string, type:string, indexField:string, equalityOperator:string  }} = this.searchFields.PUBLICATION_FIELDS_MAP;
55
  public urlParams : Map<string, string>;
56
  @ViewChild (SearchPageComponent) searchPage : SearchPageComponent ;
57
  public _location:Location;
58

    
59
  public CSV: any = {  "columnNames":  ["Title", "Authors", "Publication Year", "DOI",
60
                                       /*"Download From", "Publication type", "Journal",*/
61
                                       "Funder", "Project Name (GA Number)", "Access"],
62
                      "export":[]
63
                  };
64
  public CSVDownloaded = false;
65
  public csvParams: string;
66
  public disableForms: boolean = false;
67
  public loadPaging: boolean = true;
68
  public oldTotalResults: number = 0;
69
  @Input() openaireLink: string;
70
  @Input() customFilter:SearchCustomFilter= null;
71
  @Input() advancedSearchParameters ;
72
  pagingLimit = 0;
73
  public sort: boolean = true;
74
properties: EnvProperties;
75
  constructor (private route: ActivatedRoute, private _searchPublicationsService: SearchPublicationsService ) {
76
    this.errorCodes = new ErrorCodes();
77
    this.errorMessages = new ErrorMessagesComponent();
78
    this.searchUtils.status = this.errorCodes.LOADING;
79
    this.searchUtils.page =1;
80

    
81
  }
82

    
83
  public ngOnInit() {
84
    this.route.data
85
      .subscribe((data: { envSpecific: EnvProperties }) => {
86
        this.properties = data.envSpecific;
87
        this.baseUrl = data.envSpecific.searchLinkToPublications;
88
        this.pagingLimit = data.envSpecific.pagingLimit;
89

    
90
      });
91
    this.searchPage.refineFields = this.refineFields;
92
    this.searchPage.fieldIdsMap = this.fieldIdsMap;
93
    this.searchPage.type = "publications";
94
    var firstLoad =true;
95
    this.sub =  this.route.queryParams.subscribe(params => {
96
        if(params['page'] && this.searchUtils.page != params['page']) {
97
          this.loadPaging = false;
98
          this.oldTotalResults = this.searchUtils.totalResults;
99
        }
100

    
101
        this.searchUtils.keyword = (params['keyword']?params['keyword']:'');
102
        var refine = true;
103
        if(this.searchUtils.page != ((params['page']=== undefined)?1:+params['page']) && this.filters && !firstLoad){
104
          refine = false;
105

    
106
        }
107
        firstLoad = false;
108
        this.searchUtils.page = (params['page']=== undefined)?1:+params['page'];
109
        this.searchUtils.size = (params['size']=== undefined)?10:+params['size'];
110
        if(this.searchUtils.size != 5 && this.searchUtils.size != 10 && this.searchUtils.size != 20 && this.searchUtils.size != 50) {
111
          this.searchUtils.size = 10;
112
        }
113
        this.searchUtils.sortBy = (params['sortBy'])?params['sortBy']:'';
114
        if(this.searchUtils.sortBy && this.searchUtils.sortBy != "resultdateofacceptance,descending" && this.searchUtils.sortBy != "resultdateofacceptance,ascending") {
115
          this.searchUtils.sortBy = "";
116
        }
117

    
118
        this.searchPage.customFilter = this.customFilter;
119
        var queryParameters = this.searchPage.getQueryParametersFromUrl(params);
120
        this._getResults(queryParameters, refine, this.searchUtils.page, this.searchUtils.size, this.searchUtils.sortBy);
121
    });
122
  }
123

    
124
  public ngOnDestroy() {
125
    if(this.sub){
126
      this.sub.unsubscribe();
127
    }
128
    if(this.subResults){
129
      this.subResults.unsubscribe();
130
    }
131
  }
132

    
133
public getResultsForEntity(entity:string, id:string, page: number, size: number){
134
  var parameters = "";
135

    
136
  if(entity == "project") {
137
    parameters = "projects/"+id;
138
  }
139

    
140
  if(parameters != "") {
141

    
142
      this._searchPublicationsService.searchPublicationsForEntity(parameters, page, size, this.properties).subscribe(
143
          data => {
144
              this.searchUtils.totalResults = data[0];
145
              //console.info("search Publications for "+entity+": [Parameters:"+parameters+" ]  [total results:"+this.searchUtils.totalResults+"]");
146
              this.results = data[1];
147

    
148
              //var errorCodes:ErrorCodes = new ErrorCodes();
149
              this.searchUtils.status = this.errorCodes.DONE;
150
              if(this.searchUtils.totalResults == 0 ){
151
                this.searchUtils.status = this.errorCodes.NONE;
152
              }
153
          },
154
          err => {
155
              //console.log(err);
156
              this.handleError("Error getting publications for "+entity+" with id: "+id, err);
157
              this.searchUtils.status = this.errorMessages.getErrorCode(err.status);
158

    
159
               //TODO check erros (service not available, bad request)
160
              // if( ){
161
              //   this.searchUtils.status = ErrorCodes.ERROR;
162
              // }
163
              //var errorCodes:ErrorCodes = new ErrorCodes();
164
              //this.searchUtils.status = errorCodes.ERROR;
165
              /*if(err.status == '404') {
166
                this.searchUtils.status = this.errorCodes.NOT_FOUND;
167
              } else if(err.status == '500') {
168
                this.searchUtils.status = this.errorCodes.ERROR;
169
              } else {
170
                this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
171
              }*/
172
          }
173
      );
174
  }
175
}
176

    
177
public getResultsForDataproviders(id:string, resultsFrom:string, page: number, size: number){
178
  var parameters;
179
  if(resultsFrom == "collectedFrom") {
180
      parameters = "publications?fq=collectedfromdatasourceid exact "+'"'+id+'"';
181
  } else if(resultsFrom == "hostedBy") {
182
      parameters = "publications?fq=resulthostingdatasourceid exact "+'"'+id+'"';
183
  }
184

    
185
  if(parameters != "") {
186

    
187
      this._searchPublicationsService.searchPublicationsForDataproviders(parameters, page, size, this.properties).subscribe(
188
          data => {
189
              this.searchUtils.totalResults = data[0];
190
              //console.info("search Publications for Dataproviders: [Parameters:"+parameters+" ]  [total results:"+this.searchUtils.totalResults+"]");
191
              this.results = data[1];
192

    
193
              //var errorCodes:ErrorCodes = new ErrorCodes();
194
              this.searchUtils.status = this.errorCodes.DONE;
195
              if(this.searchUtils.totalResults == 0 ){
196
                this.searchUtils.status = this.errorCodes.NONE;
197
              }
198
          },
199
          err => {
200
              //console.log(err);
201
              this.handleError("Error getting publications for content provider ("+resultsFrom+") with id: "+id, err);
202
              this.searchUtils.status = this.errorMessages.getErrorCode(err.status);
203

    
204
               //TODO check erros (service not available, bad request)
205
              // if( ){
206
              //   this.searchUtils.status = ErrorCodes.ERROR;
207
              // }
208
              //var errorCodes:ErrorCodes = new ErrorCodes();
209
              //this.searchUtils.status = errorCodes.ERROR;
210
              /*if(err.status == '404') {
211
                this.searchUtils.status = this.errorCodes.NOT_FOUND;
212
              } else if(err.status == '500') {
213
                this.searchUtils.status = this.errorCodes.ERROR;
214
              } else {
215
                this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
216
              }*/
217
          }
218
      );
219
  }
220
}
221

    
222
public getResults(keyword:string,refine:boolean, page: number, size: number, sortBy: string){
223
  var parameters = "";
224
  if(keyword.length > 0){
225
    var DOIs:string[] = DOI.getDOIsFromString(keyword);
226
    var doisParams = "";
227

    
228
    for(var i =0 ;i < DOIs.length; i++){
229
      doisParams+=(doisParams.length > 0?"&":"")+'doi="'+ DOIs[i]+'"';
230
    }
231
    if(doisParams.length > 0){
232
        parameters +=  "&"+doisParams;
233
    }else{
234
      parameters = "q=" +  keyword;
235
    }
236
  }
237
  this._getResults(parameters,refine,page,size,sortBy);
238
}
239

    
240
private _getResults(parameters:string,refine:boolean, page: number, size: number, sortBy: string){
241
  if(page > this.pagingLimit) {
242
    size=0;
243
  }
244
  if(page <= this.pagingLimit || this.searchUtils.status == this.errorCodes.LOADING) {
245
    this.csvParams = parameters;
246

    
247
    this.searchUtils.status = this.errorCodes.LOADING;
248
    this.disableForms = true;
249
    this.results = [];
250
    this.searchUtils.totalResults = 0;
251

    
252
    this.subResults = this._searchPublicationsService.searchPublications(parameters,(refine)?this.searchPage.getRefineFieldsQuery():null, page, size, sortBy, this.searchPage.getFields(), this.properties).subscribe(
253
        data => {
254
            this.searchUtils.totalResults = data[0];
255
            //console.info("search Publications: [Parameters:"+parameters+" ]  [total results:"+this.searchUtils.totalResults+"]");
256
            this.results = data[1];
257
            if(refine){
258
              this.filters = data[2];
259
            }
260
            this.searchPage.checkSelectedFilters(this.filters);
261
            this.searchPage.updateBaseUrlWithParameters(this.filters);
262

    
263
            //var errorCodes:ErrorCodes = new ErrorCodes();
264

    
265
            this.searchUtils.status = this.errorCodes.DONE;
266
            if(this.searchUtils.totalResults == 0 ){
267
              this.searchUtils.status = this.errorCodes.NONE;
268
            }
269
            //this.searchPage.closeLoading();
270
            this.disableForms = false;
271

    
272
            if(this.searchUtils.status == this.errorCodes.DONE) {
273
              // Page out of limit!!!
274
              let totalPages:any = this.searchUtils.totalResults/(this.searchUtils.size);
275
              if(!(Number.isInteger(totalPages))) {
276
                  totalPages = (parseInt(totalPages, 10) + 1);
277
              }
278
              if(totalPages < page) {
279
                this.searchUtils.totalResults = 0;
280
                this.searchUtils.status = this.errorCodes.OUT_OF_BOUND;
281
              }
282
            }
283
        },
284
        err => {
285
            //console.log(err);
286
            this.handleError("Error getting publications", err);
287
            this.searchUtils.status = this.errorMessages.getErrorCode(err.status);
288

    
289
             //TODO check erros (service not available, bad request)
290
            // if( ){
291
            //   this.searchUtils.status = ErrorCodes.ERROR;
292
            // }
293
            //var errorCodes:ErrorCodes = new ErrorCodes();
294
            //this.searchUtils.status = errorCodes.ERROR;
295
            /*if(err.status == '404') {
296
              this.searchUtils.status = this.errorCodes.NOT_FOUND;
297
            } else if(err.status == '500') {
298
              this.searchUtils.status = this.errorCodes.ERROR;
299
            } else {
300
              this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
301
            }*/
302

    
303
            //this.searchPage.closeLoading();
304
            this.disableForms = false;
305

    
306
        }
307
    );
308
  }
309
}
310
/*
311
public getAggregatorResults(id:string, page: number, size: number){
312
  this.subResults = this._searchPublicationsService.searchAggregators('&fq=collectedfromdatasourceid exact "'+id+'"',"&refine=true&fields=resulthostingdatasource" , page, size).subscribe(
313
      data => {
314
          this.results = data;
315

    
316
          var errorCodes:ErrorCodes = new ErrorCodes();
317
          this.searchUtils.status = errorCodes.DONE;
318
          if(this.searchUtils.totalResults == 0 ){
319
            this.searchUtils.status = errorCodes.NONE;
320
          }
321
      },
322
      err => {
323
          console.log(err);
324
           //TODO check erros (service not available, bad request)
325
          // if( ){
326
          //   this.searchUtils.status = ErrorCodes.ERROR;
327
          // }
328
          var errorCodes:ErrorCodes = new ErrorCodes();
329
          this.searchUtils.status = errorCodes.ERROR;
330
      }
331
  );
332
}*/
333

    
334
  public queryChanged($event) {
335
    this.loadPaging = true;
336

    
337
    var parameters = $event.value;
338
    //console.info("queryChanged: Execute search query "+parameters);
339
    //this._getResults(parameters, true, this.searchUtils.page, this.searchUtils.size, this.searchUtils.sortBy);
340

    
341
  }
342

    
343
  private handleError(message: string, error) {
344
    console.error("Publications simple Search Page: "+message, error);
345
  }
346
}
(11-11/14)