Project

General

Profile

1
import {Component, Input, ViewChild} from '@angular/core';
2
import {Observable}       from 'rxjs/Observable';
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 {AdvancedSearchPageComponent} from '../searchUtils/advancedSearchPage.component';
9
import {SearchFields, FieldDetails} from '../../utils/properties/searchFields';
10
import {SearchUtilsClass } from '../searchUtils/searchUtils.class';
11
import{EnvProperties} from '../../utils/properties/env-properties';
12

    
13

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

    
30
    `
31
 })
32

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

    
41
  public searchUtils:SearchUtilsClass = new SearchUtilsClass();
42
  public searchFields:SearchFields = new SearchFields();
43

    
44
  public fieldIds:  string[] = this.searchFields.RESULT_ADVANCED_FIELDS;
45
  public fieldIdsMap= this.searchFields.RESULT_FIELDS;
46
  public selectedFields:AdvancedField[] =  [];
47

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

    
61

    
62

    
63
  }
64
  ngOnInit() {
65
    this.route.data
66
      .subscribe((data: { envSpecific: EnvProperties }) => {
67
        this.properties= data.envSpecific;
68
        this.searchUtils.baseUrl = data.envSpecific.searchLinkToAdvancedDatasets;
69
         this.pagingLimit = data.envSpecific.pagingLimit;
70
         this.isPiwikEnabled = data.envSpecific.enablePiwikTrack;
71

    
72
      });
73
    //var errorCodes:ErrorCodes = new ErrorCodes();
74
    this.searchUtils.status = this.errorCodes.LOADING;
75
    this.sub =  this.route.queryParams.subscribe(params => {
76
      if(params['page'] && this.searchUtils.page != params['page']) {
77
        this.loadPaging = false;
78
        this.oldTotalResults = this.searchUtils.totalResults;
79
      }
80

    
81
      let page = (params['page']=== undefined)?1:+params['page'];
82
      this.searchUtils.page = ( page <= 0 ) ? 1 : page;
83
      this.searchPage.fieldIds = this.fieldIds;
84
      this.selectedFields =[];
85
      this.searchPage.selectedFields = this.selectedFields;
86
      this.searchPage.fieldIdsMap = this.fieldIdsMap;
87
      this.searchPage.connectCommunityId = this.connectCommunityId;
88
      this.searchPage.getSelectedFiltersFromUrl(params);
89
      this.getResults(this.searchPage.createQueryParameters(),  this.searchUtils.page, this.searchUtils.size);
90

    
91
    });
92
  }
93
  ngOnDestroy() {
94
    this.sub.unsubscribe();
95
  }
96
  sub: any;
97
  public getResults(parameters:string, page: number, size: number){
98
    if(page > this.pagingLimit) {
99
      size=0;
100
    }
101
    if(page <= this.pagingLimit || this.searchUtils.status == this.errorCodes.LOADING) {
102
      if(parameters!= null && parameters != ''  ) {
103
        this.csvParams ="&fq=( "+this.resourcesQuery + "and (" + parameters + "))";
104
      }else{
105
        this.csvParams ="&fq="+this.resourcesQuery;
106
      }
107

    
108
      //var errorCodes:ErrorCodes = new ErrorCodes();
109
      this.searchUtils.status = this.errorCodes.LOADING;
110
      //this.searchPage.openLoading();
111
      this.disableForms = true;
112
      this.results = [];
113
      this.searchUtils.totalResults = 0;
114

    
115
      console.info("Advanced Search for Research Data: Execute search query "+parameters);
116
      this._searchDatasetsService.advancedSearchDatasets(parameters, page, size, this.properties).subscribe(
117
        data => {
118
                this.searchUtils.totalResults = data[0];
119
                console.info("search Research Data total="+this.searchUtils.totalResults);
120
                this.results = data[1];
121
                this.searchPage.updateBaseUrlWithParameters();
122
                //var errorCodes:ErrorCodes = new ErrorCodes();
123
                this.searchUtils.status = this.errorCodes.DONE;
124
                if(this.searchUtils.totalResults == 0 ){
125
                  this.searchUtils.status = this.errorCodes.NONE;
126
                }
127
                //this.searchPage.closeLoading();
128
                this.disableForms = false;
129

    
130
                if(this.searchUtils.status == this.errorCodes.DONE) {
131
                  // Page out of limit!!!
132
                  let totalPages:any = this.searchUtils.totalResults/(this.searchUtils.size);
133
                  if(!(Number.isInteger(totalPages))) {
134
                      totalPages = (parseInt(totalPages, 10) + 1);
135
                  }
136
                  if(totalPages < page) {
137
                    this.searchUtils.totalResults = 0;
138
                    this.searchUtils.status = this.errorCodes.OUT_OF_BOUND;
139
                  }
140
                }
141
        },
142
        err => {
143
              console.log(err);
144
              console.info("error");
145
              //TODO check erros (service not available, bad request)
146
              // if( ){
147
              //   this.searchUtils.status = errorCodes.ERROR;
148
              // }
149
              //var errorCodes:ErrorCodes = new ErrorCodes();
150
              //this.searchUtils.status = errorCodes.NOT_AVAILABLE;
151
              if(err.status == '404') {
152
                this.searchUtils.status = this.errorCodes.NOT_FOUND;
153
              } else if(err.status == '500') {
154
                this.searchUtils.status = this.errorCodes.ERROR;
155
              } else {
156
                this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
157
              }
158

    
159
              //this.searchPage.closeLoading();
160
              this.disableForms = false;
161
        }
162
      );
163
    }
164
  }
165
  private setFilters(){
166
    //TODO set filters from
167
  }
168

    
169
  public queryChanged($event) {
170
    this.loadPaging = true;
171

    
172
    var parameters = $event.value;
173
    this.getResults(parameters, this.searchUtils.page,this.searchUtils.size);
174
    console.info("queryChanged: Execute search query "+parameters);
175

    
176
  }
177

    
178

    
179
}
(3-3/14)