Project

General

Profile

1
import {Component, Input, ViewChild} from '@angular/core';
2
import {Observable}       from 'rxjs';
3
import { Router, ActivatedRoute} from '@angular/router';
4
import {Filter, Value,AdvancedField} from '../searchUtils/searchHelperClasses.class';
5
import {SearchDatasetsService} from '../../services/searchDatasets.service';
6
import {SearchResult}     from '../../utils/entities/searchResult';
7
import {ErrorCodes} from '../../utils/properties/errorCodes';
8
import {ErrorMessagesComponent}    from '../../utils/errorMessages.component';
9
import {AdvancedSearchPageComponent} from '../searchUtils/advancedSearchPage.component';
10
import {SearchFields, FieldDetails} from '../../utils/properties/searchFields';
11
import {SearchCustomFilter, SearchUtilsClass} from '../searchUtils/searchUtils.class';
12
import{EnvProperties} from '../../utils/properties/env-properties';
13

    
14

    
15
@Component({
16
    selector: 'advanced-search-datasets',
17
    template: `
18
    <advanced-search-page pageTitle="Advanced Search for Research Data" entityType="dataset"
19
                 type = "research data"
20
                 [(results)] = "results"
21
                 [(searchUtils)] = "searchUtils"
22
                 [(fieldIds)]="fieldIds" [(fieldIdsMap)]="fieldIdsMap"   [(selectedFields)]="selectedFields"
23
                 (queryChange)="queryChanged($event)"
24
                 [csvParams]="csvParams" csvPath="datasets" simpleSearchLink="/search/find/datasets"
25
                 [disableForms]="disableForms"
26
                 [loadPaging]="loadPaging"
27
                 [oldTotalResults]="oldTotalResults" 
28
                 [piwikSiteId]=piwikSiteId [hasPrefix]="hasPrefix"
29
                 searchFormClass="datasetsSearchForm"
30
                 [(sort)]=sort >
31
    </advanced-search-page>
32

    
33
    `
34
 })
35

    
36
export class AdvancedSearchDatasetsComponent {
37
  private errorCodes: ErrorCodes;
38
  private errorMessages: ErrorMessagesComponent;
39
  properties:EnvProperties;
40
  @Input() piwikSiteId = null;
41
  @Input() hasPrefix: boolean = true;
42
  @Input() customFilter:SearchCustomFilter= null;
43

    
44
  public results =[];
45
  public filters =[];
46

    
47
  public searchUtils:SearchUtilsClass = new SearchUtilsClass();
48
  public searchFields:SearchFields = new SearchFields();
49

    
50
  public fieldIds:  string[] = this.searchFields.RESULT_ADVANCED_FIELDS;
51
  public fieldIdsMap= this.searchFields.RESULT_FIELDS;
52
  public selectedFields:AdvancedField[] =  [];
53

    
54
  @ViewChild (AdvancedSearchPageComponent) searchPage : AdvancedSearchPageComponent ;
55
  public resourcesQuery = "( (oaftype exact result) and (resulttypeid exact dataset)  )";
56
  public csvParams: string;
57
  public disableForms: boolean = false;
58
  public loadPaging: boolean = true;
59
  public oldTotalResults: number = 0;
60
  public pagingLimit: number = 0;
61
  public sort: boolean = true;
62
  public isPiwikEnabled;
63
  constructor (private route: ActivatedRoute, private _searchDatasetsService: SearchDatasetsService ) {
64
    this.results =[];
65
    this.errorCodes = new ErrorCodes();
66
    this.errorMessages = new ErrorMessagesComponent();
67
    this.searchUtils.status = this.errorCodes.LOADING;
68

    
69

    
70

    
71
  }
72
  ngOnInit() {
73
    this.route.data
74
      .subscribe((data: { envSpecific: EnvProperties }) => {
75
        this.properties= data.envSpecific;
76
        this.searchUtils.baseUrl = data.envSpecific.searchLinkToAdvancedDatasets;
77
         this.pagingLimit = data.envSpecific.pagingLimit;
78
         this.isPiwikEnabled = data.envSpecific.enablePiwikTrack;
79

    
80
      });
81
    //var errorCodes:ErrorCodes = new ErrorCodes();
82
    this.searchUtils.status = this.errorCodes.LOADING;
83
    this.sub =  this.route.queryParams.subscribe(params => {
84
      if(params['page'] && this.searchUtils.page != params['page']) {
85
        this.loadPaging = false;
86
        this.oldTotalResults = this.searchUtils.totalResults;
87
      }
88

    
89
      let page = (params['page']=== undefined)?1:+params['page'];
90
      this.searchUtils.page = ( page <= 0 ) ? 1 : page;
91

    
92
      this.searchUtils.size = (params['size']=== undefined)?10:+params['size'];
93
      if(this.searchUtils.size != 5 && this.searchUtils.size != 10 && this.searchUtils.size != 20 && this.searchUtils.size != 50) {
94
        this.searchUtils.size = 10;
95
      }
96
      this.searchUtils.sortBy = (params['sortBy'])?params['sortBy']:'';
97
      if(this.searchUtils.sortBy && this.searchUtils.sortBy != "resultdateofacceptance,descending" && this.searchUtils.sortBy != "resultdateofacceptance,ascending") {
98
        this.searchUtils.sortBy = "";
99
      }
100

    
101
      this.searchPage.fieldIds = this.fieldIds;
102
      this.selectedFields =[];
103
      this.searchPage.selectedFields = this.selectedFields;
104
      this.searchPage.fieldIdsMap = this.fieldIdsMap;
105
      this.searchPage.customFilter = this.customFilter;
106
      this.searchPage.getSelectedFiltersFromUrl(params);
107
      this.getResults(this.searchPage.createQueryParameters(),  this.searchUtils.page, this.searchUtils.size, this.searchUtils.sortBy);
108

    
109
    });
110
  }
111
  ngOnDestroy() {
112
    this.sub.unsubscribe();
113
  }
114
  sub: any;
115
  public getResults(parameters:string, page: number, size: number, sortBy: string){
116
    if(page > this.pagingLimit) {
117
      size=0;
118
    }
119
    if(page <= this.pagingLimit || this.searchUtils.status == this.errorCodes.LOADING) {
120
      if(parameters!= null && parameters != ''  ) {
121
        this.csvParams ="&fq=( "+this.resourcesQuery + "and (" + parameters + "))";
122
      }else{
123
        this.csvParams ="&fq="+this.resourcesQuery;
124
      }
125

    
126
      //var errorCodes:ErrorCodes = new ErrorCodes();
127
      this.searchUtils.status = this.errorCodes.LOADING;
128
      //this.searchPage.openLoading();
129
      this.disableForms = true;
130
      this.results = [];
131
      this.searchUtils.totalResults = 0;
132

    
133
      //console.info("Advanced Search for Research Data: Execute search query "+parameters);
134
      this._searchDatasetsService.advancedSearchDatasets(parameters, page, size, sortBy, this.properties).subscribe(
135
        data => {
136
                this.searchUtils.totalResults = data[0];
137
                this.results = data[1];
138
                this.searchPage.updateBaseUrlWithParameters();
139
                //var errorCodes:ErrorCodes = new ErrorCodes();
140
                this.searchUtils.status = this.errorCodes.DONE;
141
                if(this.searchUtils.totalResults == 0 ){
142
                  this.searchUtils.status = this.errorCodes.NONE;
143
                }
144
                //this.searchPage.closeLoading();
145
                this.disableForms = false;
146

    
147
                if(this.searchUtils.status == this.errorCodes.DONE) {
148
                  // Page out of limit!!!
149
                  let totalPages:any = this.searchUtils.totalResults/(this.searchUtils.size);
150
                  if(!(Number.isInteger(totalPages))) {
151
                      totalPages = (parseInt(totalPages, 10) + 1);
152
                  }
153
                  if(totalPages < page) {
154
                    this.searchUtils.totalResults = 0;
155
                    this.searchUtils.status = this.errorCodes.OUT_OF_BOUND;
156
                  }
157
                }
158
        },
159
        err => {
160
              //console.log(err);
161
              this.handleError("Error getting research data", err);
162
              this.searchUtils.status = this.errorMessages.getErrorCode(err.status);
163

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

    
178
              //this.searchPage.closeLoading();
179
              this.disableForms = false;
180
        }
181
      );
182
    }
183
  }
184
  private setFilters(){
185
    //TODO set filters from
186
  }
187

    
188
  public queryChanged($event) {
189
    this.loadPaging = true;
190

    
191
    var parameters = $event.value;
192
    //this.getResults(parameters, this.searchUtils.page,this.searchUtils.size, this.searchUtils.sortBy);
193
    //console.info("queryChanged: Execute search query "+parameters);
194

    
195
  }
196

    
197
  private handleError(message: string, error) {
198
    console.error("Research Data advanced Search Page: "+message, error);
199
  }
200
}
(3-3/14)