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

    
32
    `
33
 })
34

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

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

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

    
50
  @ViewChild (AdvancedSearchPageComponent) searchPage : AdvancedSearchPageComponent ;
51
  public resourcesQuery = "( (oaftype exact result) and (resulttypeid exact software)  )";
52
  public csvParams: string;
53
  public disableForms: boolean = false;
54
  public loadPaging: boolean = true;
55
  public oldTotalResults: number = 0;
56
  public pagingLimit: number = 0;
57
  public isPiwikEnabled;
58
  public sort: boolean = true;
59
  properties:EnvProperties;
60

    
61
  constructor (private route: ActivatedRoute, private _searchSoftwareService: SearchSoftwareService ) {
62
    this.results =[];
63
    this.errorCodes = new ErrorCodes();
64
    this.errorMessages = new ErrorMessagesComponent();
65
    this.searchUtils.status = this.errorCodes.LOADING;
66

    
67

    
68

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

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

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

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

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

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

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

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

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

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

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

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

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

    
193
  }
194

    
195
  private handleError(message: string, error) {
196
    console.error("Software advanced Search Page: "+message, error);
197
  }
198
}
(13-13/14)