Project

General

Profile

1
import {Component, Input, ViewChild} from '@angular/core';
2
import { ActivatedRoute} from '@angular/router';
3
import {Location} from '@angular/common';
4
import { Filter, Value} from '../searchUtils/searchHelperClasses.class';
5
import {SearchProjectsService} from '../../services/searchProjects.service';
6
import {SearchResult}     from '../../utils/entities/searchResult';
7
import {ErrorCodes} from '../../utils/properties/errorCodes';
8
import {ErrorMessagesComponent}    from '../../utils/errorMessages.component';
9
import {SearchFields, FieldDetails} from '../../utils/properties/searchFields';
10
import {SearchPageComponent } from '../searchUtils/searchPage.component';
11
import {SearchCustomFilter, SearchUtilsClass} from '../searchUtils/searchUtils.class';
12
import{EnvProperties} from '../../utils/properties/env-properties';
13

    
14
@Component({
15
    selector: 'search-projects',
16
    template: `
17

    
18
    <search-page pageTitle="Search Projects"
19
                formPlaceholderText = "Search for Projects"
20
                 type="projects" entityType="project" [(filters)] = "filters"
21
                 [(results)] = "results" [(searchUtils)] = "searchUtils"
22
                 [baseUrl] = "baseUrl"  
23
                 [csvParams]="csvParams" csvPath="projects" advancedSearchLink="/search/advanced/projects"
24
                 [disableForms]="disableForms"
25
                 [loadPaging]="loadPaging"
26
                 [oldTotalResults]="oldTotalResults"
27
                 [piwikSiteId]=piwikSiteId
28
                 [searchFormClass]="customFilter && customFilter.queryFieldName == 'communityId' ? 
29
                      'communityPanelBackground' : 'projectsSearchForm'">
30
    </search-page>
31
     `
32

    
33
})
34
export class SearchProjectsComponent {
35
  private errorCodes: ErrorCodes;
36
  private errorMessages: ErrorMessagesComponent;
37
@Input() piwikSiteId = null;
38
@Input() customFilter:SearchCustomFilter= null;
39
  public results =[];
40
  public filters: Filter[] =[];
41
  public baseUrl:string;
42
  public searchUtils:SearchUtilsClass = new SearchUtilsClass();
43
  public sub: any;
44
  public subResults: any;
45
  public searchFields:SearchFields = new SearchFields();
46
  public refineFields: string[] =  this.searchFields.PROJECT_REFINE_FIELDS;
47
  public fieldIdsMap = this.searchFields.PROJECT_FIELDS;
48
  public _location:Location;
49
  public csvParams: string;
50
  public disableForms: boolean = false;
51
  public loadPaging: boolean = true;
52
  public oldTotalResults: number = 0;
53
  pagingLimit = 0;
54
properties: EnvProperties;
55

    
56
  @ViewChild (SearchPageComponent) searchPage : SearchPageComponent ;
57

    
58
  constructor (private route: ActivatedRoute, private _searchProjectsService: SearchProjectsService) {
59

    
60
    this.errorCodes = new ErrorCodes();
61
    this.errorMessages = new ErrorMessagesComponent();
62
    this.searchUtils.status = this.errorCodes.LOADING;
63
    this.searchUtils.page =1;
64

    
65
  }
66

    
67
  public ngOnInit() {
68
    this.route.data
69
      .subscribe((data: { envSpecific: EnvProperties }) => {
70
        this.properties = data.envSpecific;
71
        this.baseUrl = data.envSpecific.searchLinkToProjects;
72
        this.pagingLimit = data.envSpecific.pagingLimit;
73

    
74
      });
75
    this.searchPage.refineFields = this.refineFields;
76
    this.searchPage.fieldIdsMap = this.fieldIdsMap;
77
    //get refine field filters  from url parameters
78
    var firstLoad = true;
79
    this.sub =  this.route.queryParams.subscribe(params => {
80
      this.loadPaging = true;
81
      if(params['page'] && this.searchUtils.page != params['page']) {
82
        this.loadPaging = false;
83
        this.oldTotalResults = this.searchUtils.totalResults;
84
      }
85

    
86
      //get keyword from url parameters
87
      this.searchUtils.keyword = (params['keyword']?params['keyword']:'');
88
      var refine = true;
89
      if(this.searchUtils.page != ((params['page']=== undefined)?1:+params['page']) && this.filters && !firstLoad){
90
        refine = false;
91

    
92
      }
93
      firstLoad = false;
94
      //get page from url parameters
95
      this.searchUtils.page = (params['page']=== undefined)?1:+params['page'];
96
      this.searchUtils.size = (params['size']=== undefined)?10:+params['size'];
97
      if(this.searchUtils.size != 5 && this.searchUtils.size != 10 && this.searchUtils.size != 20 && this.searchUtils.size != 50) {
98
        this.searchUtils.size = 10;
99
      }
100
      this.searchPage.customFilter = this.customFilter;
101
      var queryParameters = this.searchPage.getQueryParametersFromUrl(params);
102
      this._getResults(queryParameters, refine, this.searchUtils.page, this.searchUtils.size);
103
    });
104
  }
105

    
106
  public ngOnDestroy() {
107
    if(this.sub){
108
      this.sub.unsubscribe();
109
    }
110
    if(this.subResults){
111
      this.subResults.unsubscribe();
112
    }
113
  }
114

    
115
  public getResults(keyword:string,refine:boolean, page: number, size: number){
116
    var parameters = "";
117
    if(keyword.length > 0){
118
      parameters = "q="+keyword;
119
    }
120
    this._getResults(parameters,refine,page,this.searchUtils.size);
121
  }
122
  private _getResults(parameters:string,refine:boolean, page: number, size: number){
123
    if(page > this.pagingLimit) {
124
      size=0;
125
    }
126
    if(page <= this.pagingLimit || this.searchUtils.status == this.errorCodes.LOADING) {
127
      this.csvParams = parameters;
128

    
129
      this.searchUtils.status = this.errorCodes.LOADING;
130
      //this.searchPage.openLoading();
131
      this.disableForms = true;
132
      this.results = [];
133
      this.searchUtils.totalResults = 0;
134

    
135
      this.subResults = this._searchProjectsService.searchProjects(parameters,(refine)?this.searchPage.getRefineFieldsQuery():null, page, size, this.searchPage.getFields(), this.properties).subscribe(
136
        data => {
137
            this.searchUtils.totalResults = data[0];
138
            //console.info("search Projects: [Parameters:"+parameters+" ]  [total results:"+this.searchUtils.totalResults+"]");
139
            this.results = data[1];
140
            if(refine){
141
              this.filters = data[2];
142
            }
143
            this.searchPage.checkSelectedFilters(this.filters);
144
            // this.filters = this.searchPage.checkSelectedFilters(data[2]);
145
            this.searchPage.updateBaseUrlWithParameters(this.filters);
146
            //var errorCodes:ErrorCodes = new ErrorCodes();
147
            this.searchUtils.status = this.errorCodes.DONE;
148
            if(this.searchUtils.totalResults == 0 ){
149
              this.searchUtils.status = this.errorCodes.NONE;
150
            }
151
            //this.searchPage.closeLoading();
152
            this.disableForms = false;
153

    
154
            if(this.searchUtils.status == this.errorCodes.DONE) {
155
              // Page out of limit!!!
156
              let totalPages:any = this.searchUtils.totalResults/(this.searchUtils.size);
157
              if(!(Number.isInteger(totalPages))) {
158
                  totalPages = (parseInt(totalPages, 10) + 1);
159
              }
160
              if(totalPages < page) {
161
                this.searchUtils.totalResults = 0;
162
                this.searchUtils.status = this.errorCodes.OUT_OF_BOUND;
163
              }
164
            }
165
        },
166
        err => {
167
            //console.log(err);
168
            this.handleError("Error getting projects", err);
169
            this.searchUtils.status = this.errorMessages.getErrorCode(err.status);
170

    
171
             //TODO check erros (service not available, bad request)
172
            // if( ){
173
            //   this.searchUtils.status = ErrorCodes.ERROR;
174
            // }
175
            //var errorCodes:ErrorCodes = new ErrorCodes();
176
            //this.searchUtils.status = errorCodes.ERROR;
177
            /*if(err.status == '404') {
178
              this.searchUtils.status = this.errorCodes.NOT_FOUND;
179
            } else if(err.status == '500') {
180
              this.searchUtils.status = this.errorCodes.ERROR;
181
            } else {
182
              this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
183
            }*/
184

    
185
            //this.searchPage.closeLoading();
186
            this.disableForms = false;
187
        }
188
      );
189
    }
190
  }
191

    
192
  public getResultsForDataproviders(id:string, page: number, size: number){
193

    
194
    this._searchProjectsService.getProjectsforDataProvider(id, page, size, this.properties).subscribe(
195
        data => {
196
            this.searchUtils.totalResults = data[0];
197
            //console.info("search Projects for Dataproviders: [Id:"+id+" ]  [total results:"+this.searchUtils.totalResults+"]");
198
            this.results = data[1];
199

    
200
            //var errorCodes:ErrorCodes = new ErrorCodes();
201
            this.searchUtils.status = this.errorCodes.DONE;
202
            if(this.searchUtils.totalResults == 0 ){
203
              this.searchUtils.status = this.errorCodes.NONE;
204
            }
205
        },
206
        err => {
207
            //console.log(err);
208
            this.handleError("Error getting projects for content provider with id: "+id, err);
209
            this.searchUtils.status = this.errorMessages.getErrorCode(err.status);
210

    
211
             //TODO check erros (service not available, bad request)
212
            // if( ){
213
            //   this.searchUtils.status = ErrorCodes.ERROR;
214
            // }
215
            //var errorCodes:ErrorCodes = new ErrorCodes();
216
            //this.searchUtils.status = errorCodes.ERROR;
217
            /*if(err.status == '404') {
218
              this.searchUtils.status = this.errorCodes.NOT_FOUND;
219
            } else if(err.status == '500') {
220
              this.searchUtils.status = this.errorCodes.ERROR;
221
            } else {
222
              this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
223
            }*/
224
        }
225
    );
226
  }
227

    
228

    
229
  // public queryChanged($event) {
230
  //   this.loadPaging = true;
231
  //
232
  //   this.urlParams = undefined;
233
  //   var parameters = $event.value;
234
  //   //this._getResults(parameters, true, this.searchUtils.page, this.searchUtils.size);
235
  // }
236

    
237
  private handleError(message: string, error) {
238
    console.error("Projects simple Search Page: "+message, error);
239
  }
240
}
(5-5/8)