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 {SearchFields, FieldDetails} from '../../utils/properties/searchFields';
9
import {SearchPageComponent } from '../searchUtils/searchPage.component';
10
import {SearchUtilsClass } from '../searchUtils/searchUtils.class';
11
import{EnvProperties} from '../../utils/properties/env-properties';
12

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

    
17
    <search-page pageTitle="Search Projects"
18
                formPlaceholderText = "Search for Projects"
19
                 type="projects" entityType="project" [(filters)] = "filters"
20
                 [(results)] = "results" [(searchUtils)] = "searchUtils"
21
                 [baseUrl] = "baseUrl" (queryChange)="queryChanged($event)"
22
                 [csvParams]="csvParams" csvPath="projects" advancedSearchLink="/search/advanced/projects"
23
                 [disableForms]="disableForms"
24
                 [loadPaging]="loadPaging"
25
                 [oldTotalResults]="oldTotalResults"
26
                 [piwikSiteId]=piwikSiteId
27
                 searchFormClass="projectsSearchForm">
28
    </search-page>
29
     `
30

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

    
53
  @ViewChild (SearchPageComponent) searchPage : SearchPageComponent ;
54

    
55
  constructor (private route: ActivatedRoute, private _searchProjectsService: SearchProjectsService) {
56
    console.info(" constructor SearchProjectsComponent "+this.refineFields.length);
57

    
58
    this.errorCodes = new ErrorCodes();
59
    this.searchUtils.status = this.errorCodes.LOADING;
60
    this.searchUtils.page =1;
61

    
62
  }
63

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

    
71
      });
72
    this.searchPage.refineFields = this.refineFields;
73
    this.searchPage.fieldIdsMap = this.fieldIdsMap;
74
    console.info(" ngOnInit SearchProjectsComponent "+this.refineFields.length);
75
    //get refine field filters  from url parameters
76
    var firstLoad = true;
77
    this.sub =  this.route.queryParams.subscribe(params => {
78
      if(params['page'] && this.searchUtils.page != params['page']) {
79
        this.loadPaging = false;
80
        this.oldTotalResults = this.searchUtils.totalResults;
81
      }
82

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

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

    
103
  public ngOnDestroy() {
104
    if(this.sub){
105
      this.sub.unsubscribe();
106
    }
107
    if(this.subResults){
108
      this.subResults.unsubscribe();
109
    }
110
  }
111

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

    
126
      this.searchUtils.status = this.errorCodes.LOADING;
127
      //this.searchPage.openLoading();
128
      this.disableForms = true;
129
      this.results = [];
130
      this.searchUtils.totalResults = 0;
131

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

    
151
            if(this.searchUtils.status == this.errorCodes.DONE) {
152
              // Page out of limit!!!
153
              let totalPages:any = this.searchUtils.totalResults/(this.searchUtils.size);
154
              if(!(Number.isInteger(totalPages))) {
155
                  totalPages = (parseInt(totalPages, 10) + 1);
156
              }
157
              if(totalPages < page) {
158
                this.searchUtils.totalResults = 0;
159
                this.searchUtils.status = this.errorCodes.OUT_OF_BOUND;
160
              }
161
            }
162
        },
163
        err => {
164
            console.log(err);
165
             //TODO check erros (service not available, bad request)
166
            // if( ){
167
            //   this.searchUtils.status = ErrorCodes.ERROR;
168
            // }
169
            //var errorCodes:ErrorCodes = new ErrorCodes();
170
            //this.searchUtils.status = errorCodes.ERROR;
171
            if(err.status == '404') {
172
              this.searchUtils.status = this.errorCodes.NOT_FOUND;
173
            } else if(err.status == '500') {
174
              this.searchUtils.status = this.errorCodes.ERROR;
175
            } else {
176
              this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
177
            }
178

    
179
            //this.searchPage.closeLoading();
180
            this.disableForms = false;
181
        }
182
      );
183
    }
184
  }
185

    
186
  public getResultsForDataproviders(id:string, page: number, size: number){
187

    
188
    this._searchProjectsService.getProjectsforDataProvider(id, page, size, this.properties).subscribe(
189
        data => {
190
            this.searchUtils.totalResults = data[0];
191
            console.info("search Projects for Dataproviders: [Id:"+id+" ]  [total results:"+this.searchUtils.totalResults+"]");
192
            this.results = data[1];
193

    
194
            //var errorCodes:ErrorCodes = new ErrorCodes();
195
            this.searchUtils.status = this.errorCodes.DONE;
196
            if(this.searchUtils.totalResults == 0 ){
197
              this.searchUtils.status = this.errorCodes.NONE;
198
            }
199
        },
200
        err => {
201
            console.log(err);
202
             //TODO check erros (service not available, bad request)
203
            // if( ){
204
            //   this.searchUtils.status = ErrorCodes.ERROR;
205
            // }
206
            //var errorCodes:ErrorCodes = new ErrorCodes();
207
            //this.searchUtils.status = errorCodes.ERROR;
208
            if(err.status == '404') {
209
              this.searchUtils.status = this.errorCodes.NOT_FOUND;
210
            } else if(err.status == '500') {
211
              this.searchUtils.status = this.errorCodes.ERROR;
212
            } else {
213
              this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
214
            }
215
        }
216
    );
217
  }
218

    
219

    
220
  public queryChanged($event) {
221
    this.loadPaging = true;
222

    
223
    this.urlParams = undefined;
224
    var parameters = $event.value;
225
    this._getResults(parameters, true, this.searchUtils.page, this.searchUtils.size);
226
  }
227

    
228
}
(9-9/14)