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 {SearchOrpsService} from '../../services/searchOrps.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-orps',
17
    template: `
18
    <advanced-search-page pageTitle="Advanced Search for Other Research Products" entityType="other"
19
                 type = "other research products"
20
                 [(results)] = "results"
21
                 [(searchUtils)] = "searchUtils"
22
                 [(fieldIds)]="fieldIds" [(fieldIdsMap)]="fieldIdsMap"   [(selectedFields)]="selectedFields"
23
                 (queryChange)="queryChanged($event)"
24
                 [csvParams]="csvParams" csvPath="other" simpleSearchLink="/search/find/other"
25
                 [disableForms]="disableForms"
26
                 [loadPaging]="loadPaging"
27
                 [oldTotalResults]="oldTotalResults" [(connectCommunityId)]=connectCommunityId [piwikSiteId]=piwikSiteId
28
                 searchFormClass="orpsSearchForm"
29
                 [(sort)]=sort >
30
    </advanced-search-page>
31

    
32
    `
33
 })
34

    
35
export class AdvancedSearchOrpsComponent {
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 other)  )";
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 _searchOrpsService: SearchOrpsService ) {
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.searchLinkToAdvancedOrps;
74
         this.pagingLimit = data.envSpecific.pagingLimit;
75
         this.isPiwikEnabled = data.envSpecific.enablePiwikTrack;
76

    
77
      });
78

    
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
      this.searchUtils.status = this.errorCodes.LOADING;
124
      this.disableForms = true;
125
      this.results = [];
126
      this.searchUtils.totalResults = 0;
127

    
128
      //console.info("Advanced Search for Other Research Products: Execute search query "+parameters);
129
      this._searchOrpsService.advancedSearchOrps(parameters, page, size, sortBy, this.properties).subscribe(
130
        data => {
131
                this.searchUtils.totalResults = data[0];
132
                this.results = data[1];
133
                this.searchPage.updateBaseUrlWithParameters();
134

    
135
                this.searchUtils.status = this.errorCodes.DONE;
136
                if(this.searchUtils.totalResults == 0 ){
137
                  this.searchUtils.status = this.errorCodes.NONE;
138
                }
139
                this.disableForms = false;
140

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

    
158
              //TODO check erros (service not available, bad request)
159
              /*if(err.status == '404') {
160
                this.searchUtils.status = this.errorCodes.NOT_FOUND;
161
              } else if(err.status == '500') {
162
                this.searchUtils.status = this.errorCodes.ERROR;
163
              } else {
164
                this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
165
              }*/
166

    
167
              this.disableForms = false;
168
        }
169
      );
170
    }
171
  }
172
  private setFilters(){
173
    //TODO set filters from
174
  }
175

    
176
  public queryChanged($event) {
177
    this.loadPaging = true;
178

    
179
    var parameters = $event.value;
180
    //this.getResults(parameters, this.searchUtils.page,this.searchUtils.size, this.searchUtils.sortBy);
181
    //console.info("queryChanged: Execute search query "+parameters);
182

    
183
  }
184

    
185
  private handleError(message: string, error) {
186
    console.error("Other Research Products advanced Search Page: "+message, error);
187
  }
188
}
(7-7/14)