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 {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" [(connectCommunityId)]=connectCommunityId [piwikSiteId]=piwikSiteId
28
                 searchFormClass="datasetsSearchForm"
29
                 [(sort)]=sort >
30
    </advanced-search-page>
31

    
32
    `
33
 })
34

    
35
export class AdvancedSearchDatasetsComponent {
36
  private errorCodes: ErrorCodes;
37
  private errorMessages: ErrorMessagesComponent;
38
  properties:EnvProperties;
39
@Input() piwikSiteId = null;
40
@Input() connectCommunityId: string;
41
  public results =[];
42
  public filters =[];
43

    
44
  public searchUtils:SearchUtilsClass = new SearchUtilsClass();
45
  public searchFields:SearchFields = new SearchFields();
46

    
47
  public fieldIds:  string[] = this.searchFields.RESULT_ADVANCED_FIELDS;
48
  public fieldIdsMap= this.searchFields.RESULT_FIELDS;
49
  public selectedFields:AdvancedField[] =  [];
50

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

    
66

    
67

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

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

    
86
      let page = (params['page']=== undefined)?1:+params['page'];
87
      this.searchUtils.page = ( page <= 0 ) ? 1 : page;
88

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

    
98
      this.searchPage.fieldIds = this.fieldIds;
99
      this.selectedFields =[];
100
      this.searchPage.selectedFields = this.selectedFields;
101
      this.searchPage.fieldIdsMap = this.fieldIdsMap;
102
      this.searchPage.connectCommunityId = this.connectCommunityId;
103
      this.searchPage.getSelectedFiltersFromUrl(params);
104
      this.getResults(this.searchPage.createQueryParameters(),  this.searchUtils.page, this.searchUtils.size, this.searchUtils.sortBy);
105

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

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

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

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

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

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

    
185
  public queryChanged($event) {
186
    this.loadPaging = true;
187

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

    
192
  }
193

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