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

    
30
    `
31
 })
32

    
33
export class AdvancedSearchOrganizationsComponent {
34
  private errorCodes: ErrorCodes;
35
  properties:EnvProperties;
36
@Input() piwikSiteId = null;
37
  public results =[];
38
  public filters =[];
39
  public searchUtils:SearchUtilsClass = new SearchUtilsClass();
40
  public searchFields:SearchFields = new SearchFields();
41

    
42
  public fieldIds:  string[] = this.searchFields.ORGANIZATION_ADVANCED_FIELDS;
43
  public fieldIdsMap  = this.searchFields.ORGANIZATION_FIELDS;
44
  public selectedFields:AdvancedField[] =  [];
45
  public csvParams: string;
46
  public disableForms: boolean = false;
47
  public loadPaging: boolean = true;
48
  public oldTotalResults: number = 0;
49
  public pagingLimit: number = 0;
50
  public isPiwikEnabled;
51

    
52
  @ViewChild (AdvancedSearchPageComponent) searchPage : AdvancedSearchPageComponent ;
53

    
54
public resourcesQuery = "(oaftype exact organization)";
55
  constructor (private route: ActivatedRoute, private _searchOrganizationsService: SearchOrganizationsService ) {
56
    this.results =[];
57
    this.errorCodes = new ErrorCodes();
58
    this.searchUtils.status = this.errorCodes.LOADING;
59

    
60

    
61

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

    
71
      });
72
    //var errorCodes:ErrorCodes = new ErrorCodes();
73
    this.searchUtils.status = this.errorCodes.LOADING;
74

    
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.getSelectedFiltersFromUrl(params);
88
      this.getResults(this.searchPage.createQueryParameters(),  this.searchUtils.page, this.searchUtils.size);
89

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

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

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

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

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

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

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

    
175
  }
176

    
177

    
178
}
(5-5/14)