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 { Filter, Value} from '../searchUtils/searchHelperClasses.class';
6

    
7
import {SearchPublicationsService} from '../../services/searchPublications.service';
8
import {SearchResult}     from '../../utils/entities/searchResult';
9
import {OpenaireProperties, ErrorCodes} from '../../utils/properties/openaireProperties';
10
import {SearchFields, FieldDetails} from '../../utils/properties/searchFields';
11
import {SearchPageComponent } from '../searchUtils/searchPage.component';
12
import {SearchUtilsClass} from '../searchUtils/searchUtils.class';
13
import {DOI} from '../../utils/string-utils.class';
14

    
15
@Component({
16
    selector: 'search-publications',
17
    template: `
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="publicationsSearchForm">
31
    </search-page>
32

    
33
    `
34

    
35
})
36
export class SearchPublicationsComponent {
37
  private errorCodes: ErrorCodes;
38

    
39
  public results =[];
40
  public filters =[];
41
  public searchUtils:SearchUtilsClass = new SearchUtilsClass();
42
  public baseUrl:string = "";
43
  public sub: any;
44
  public subResults: any;
45
  public searchFields:SearchFields = new SearchFields();
46
  public refineFields: string[] =  this.searchFields.RESULT_REFINE_FIELDS;
47
  public fieldIdsMap=this.searchFields.RESULT_FIELDS;
48
    //:  { [key:string] :{ name:string, operator:string, type:string, indexField:string, equalityOperator:string  }} = this.searchFields.PUBLICATION_FIELDS_MAP;
49
  public urlParams : Map<string, string>;
50
  @ViewChild (SearchPageComponent) searchPage : SearchPageComponent ;
51
  public _location:Location;
52

    
53
  public CSV: any = {  "columnNames":  ["Title", "Authors", "Publication Year", "DOI",
54
                                       /*"Download From", "Publication type", "Journal",*/
55
                                       "Funder", "Project Name (GA Number)", "Access"],
56
                      "export":[]
57
                  };
58
  public CSVDownloaded = false;
59
  public csvParams: string;
60
  public disableForms: boolean = false;
61
  public loadPaging: boolean = true;
62
  public oldTotalResults: number = 0;
63

    
64
  constructor (private route: ActivatedRoute, private _searchPublicationsService: SearchPublicationsService ) {
65
    this.errorCodes = new ErrorCodes();
66
    this.searchUtils.status = this.errorCodes.LOADING;
67
    this.searchUtils.page =1;
68
    this.baseUrl = OpenaireProperties.getLinkToSearchPublications();
69

    
70
  }
71

    
72
  public ngOnInit() {
73
    this.searchPage.refineFields = this.refineFields;
74
    this.searchPage.fieldIdsMap = this.fieldIdsMap;
75
    this.searchPage.type = "publications";
76
    var firstLoad =true;
77
    this.sub =  this.route.queryParams.subscribe(params => {
78
        if(params['page'] && this.searchUtils.page != params['page']) {
79
          this.loadPaging = false;
80
          this.oldTotalResults = this.searchUtils.totalResults;
81
        }
82

    
83
        this.searchUtils.keyword = (params['keyword']?params['keyword']:'');
84
        var refine = true;
85
        if(this.searchUtils.page != ((params['page']=== undefined)?1:+params['page']) && this.filters && !firstLoad){
86
          refine = false;
87

    
88
        }
89
        firstLoad = false;
90
        this.searchUtils.page = (params['page']=== undefined)?1:+params['page'];
91
        var queryParameters = this.searchPage.getQueryParametersFromUrl(params);
92
        this._getResults(queryParameters, refine, this.searchUtils.page, this.searchUtils.size);
93
    });
94
  }
95

    
96
  public ngOnDestroy() {
97
    if(this.sub){
98
      this.sub.unsubscribe();
99
    }
100
    if(this.subResults){
101
      this.subResults.unsubscribe();
102
    }
103
  }
104

    
105
public getResultsForEntity(entity:string, id:string, page: number, size: number){
106
  var parameters = "";
107

    
108
  if(entity == "project") {
109
    parameters = "projects/"+id;
110
  }
111

    
112
  if(parameters != "") {
113

    
114
      this._searchPublicationsService.searchPublicationsForEntity(parameters, page, size).subscribe(
115
          data => {
116
              this.searchUtils.totalResults = data[0];
117
              console.info("search Publications for "+entity+": [Parameters:"+parameters+" ]  [total results:"+this.searchUtils.totalResults+"]");
118
              this.results = data[1];
119

    
120
              //var errorCodes:ErrorCodes = new ErrorCodes();
121
              this.searchUtils.status = this.errorCodes.DONE;
122
              if(this.searchUtils.totalResults == 0 ){
123
                this.searchUtils.status = this.errorCodes.NONE;
124
              }
125
          },
126
          err => {
127
              console.log(err);
128
               //TODO check erros (service not available, bad request)
129
              // if( ){
130
              //   this.searchUtils.status = ErrorCodes.ERROR;
131
              // }
132
              //var errorCodes:ErrorCodes = new ErrorCodes();
133
              //this.searchUtils.status = errorCodes.ERROR;
134
              if(err.status == '404') {
135
                this.searchUtils.status = this.errorCodes.NOT_FOUND;
136
              } else if(err.status == '500') {
137
                this.searchUtils.status = this.errorCodes.ERROR;
138
              } else {
139
                this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
140
              }
141
          }
142
      );
143
  }
144
}
145

    
146
public getResultsForDataproviders(id:string, resultsFrom:string, page: number, size: number){
147
  var parameters;
148
  if(resultsFrom == "collectedFrom") {
149
      parameters = "publications?fq=collectedfromdatasourceid exact "+'"'+id+'"';
150
  } else if(resultsFrom == "hostedBy") {
151
      parameters = "publications?fq=resulthostingdatasourceid exact "+'"'+id+'"';
152
  }
153

    
154
  if(parameters != "") {
155

    
156
      this._searchPublicationsService.searchPublicationsForDataproviders(parameters, page, size).subscribe(
157
          data => {
158
              this.searchUtils.totalResults = data[0];
159
              console.info("search Publications for Dataproviders: [Parameters:"+parameters+" ]  [total results:"+this.searchUtils.totalResults+"]");
160
              this.results = data[1];
161

    
162
              //var errorCodes:ErrorCodes = new ErrorCodes();
163
              this.searchUtils.status = this.errorCodes.DONE;
164
              if(this.searchUtils.totalResults == 0 ){
165
                this.searchUtils.status = this.errorCodes.NONE;
166
              }
167
          },
168
          err => {
169
              console.log(err);
170
               //TODO check erros (service not available, bad request)
171
              // if( ){
172
              //   this.searchUtils.status = ErrorCodes.ERROR;
173
              // }
174
              //var errorCodes:ErrorCodes = new ErrorCodes();
175
              //this.searchUtils.status = errorCodes.ERROR;
176
              if(err.status == '404') {
177
                this.searchUtils.status = this.errorCodes.NOT_FOUND;
178
              } else if(err.status == '500') {
179
                this.searchUtils.status = this.errorCodes.ERROR;
180
              } else {
181
                this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
182
              }
183
          }
184
      );
185
  }
186
}
187

    
188
public getResults(keyword:string,refine:boolean, page: number, size: number){
189
  var parameters = "";
190
  if(keyword.length > 0){
191
    var DOIs:string[] = DOI.getDOIsFromString(keyword);
192
    var doisParams = "";
193

    
194
    for(var i =0 ;i < DOIs.length; i++){
195
      doisParams+=(doisParams.length > 0?"&":"")+'doi="'+ DOIs[i]+'"';
196
    }
197
    if(doisParams.length > 0){
198
        parameters +=  "&"+doisParams;
199
    }else{
200
      parameters = "q=" +  keyword;
201
    }
202
  }
203
  this._getResults(parameters,refine,page,size);
204
}
205

    
206
private _getResults(parameters:string,refine:boolean, page: number, size: number){
207
    this.csvParams = parameters;
208

    
209
  // if(!refine && !this.searchPage){
210
  //     this.searchPage = new SearchPageComponent(this._location);
211
  // }
212

    
213
  //var errorCodes:ErrorCodes = new ErrorCodes();
214
  this.searchUtils.status = this.errorCodes.LOADING;
215
  //this.searchPage.openLoading();
216
  this.disableForms = true;
217
  this.results = [];
218
  this.searchUtils.totalResults = 0;
219

    
220
  this.subResults = this._searchPublicationsService.searchPublications(parameters,(refine)?this.searchPage.getRefineFieldsQuery():null, page, size, this.searchPage.getFields()).subscribe(
221
      data => {
222
          this.searchUtils.totalResults = data[0];
223
          console.info("search Publications: [Parameters:"+parameters+" ]  [total results:"+this.searchUtils.totalResults+"]");
224
          this.results = data[1];
225
          if(refine){
226
            this.filters = data[2];
227
          }
228
          this.searchPage.checkSelectedFilters(this.filters);
229
          this.searchPage.updateBaseUrlWithParameters(this.filters);
230

    
231
          //var errorCodes:ErrorCodes = new ErrorCodes();
232

    
233
          this.searchUtils.status = this.errorCodes.DONE;
234
          if(this.searchUtils.totalResults == 0 ){
235
            this.searchUtils.status = this.errorCodes.NONE;
236
          }
237
          //this.searchPage.closeLoading();
238
          this.disableForms = false;
239

    
240
          if(this.searchUtils.status == this.errorCodes.DONE) {
241
            // Page out of limit!!!
242
            let totalPages:any = this.searchUtils.totalResults/(this.searchUtils.size);
243
            if(!(Number.isInteger(totalPages))) {
244
                totalPages = (parseInt(totalPages, 10) + 1);
245
            }
246
            if(totalPages < page) {
247
              this.searchUtils.totalResults = 0;
248
              this.searchUtils.status = this.errorCodes.OUT_OF_BOUND;
249
            }
250
          }
251
      },
252
      err => {
253
          console.log(err);
254
           //TODO check erros (service not available, bad request)
255
          // if( ){
256
          //   this.searchUtils.status = ErrorCodes.ERROR;
257
          // }
258
          //var errorCodes:ErrorCodes = new ErrorCodes();
259
          //this.searchUtils.status = errorCodes.ERROR;
260
          if(err.status == '404') {
261
            this.searchUtils.status = this.errorCodes.NOT_FOUND;
262
          } else if(err.status == '500') {
263
            this.searchUtils.status = this.errorCodes.ERROR;
264
          } else {
265
            this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
266
          }
267

    
268
          //this.searchPage.closeLoading();
269
          this.disableForms = false;
270

    
271
      }
272
  );
273
}
274
/*
275
public getAggregatorResults(id:string, page: number, size: number){
276
  this.subResults = this._searchPublicationsService.searchAggregators('&fq=collectedfromdatasourceid exact "'+id+'"',"&refine=true&fields=resulthostingdatasource" , page, size).subscribe(
277
      data => {
278
          this.results = data;
279

    
280
          var errorCodes:ErrorCodes = new ErrorCodes();
281
          this.searchUtils.status = errorCodes.DONE;
282
          if(this.searchUtils.totalResults == 0 ){
283
            this.searchUtils.status = errorCodes.NONE;
284
          }
285
      },
286
      err => {
287
          console.log(err);
288
           //TODO check erros (service not available, bad request)
289
          // if( ){
290
          //   this.searchUtils.status = ErrorCodes.ERROR;
291
          // }
292
          var errorCodes:ErrorCodes = new ErrorCodes();
293
          this.searchUtils.status = errorCodes.ERROR;
294
      }
295
  );
296
}*/
297

    
298
  public queryChanged($event) {
299
    this.loadPaging = true;
300

    
301
    var parameters = $event.value;
302
    console.info("queryChanged: Execute search query "+parameters);
303
    console.info("Search Pubs::page "+this.searchUtils.page);
304
    this._getResults(parameters, true, this.searchUtils.page, this.searchUtils.size);
305
  }
306

    
307

    
308
}
(9-9/12)