Project

General

Profile

1 58115 argiro.kok
import {Component, EventEmitter, Input, Output, ViewChild} from '@angular/core';
2 58030 argiro.kok
import {ActivatedRoute} from '@angular/router';
3 58073 argiro.kok
import {AdvancedField} from './searchUtils/searchHelperClasses.class';
4
import {SearchOrganizationsService} from '../services/searchOrganizations.service';
5
import {ErrorCodes} from '../utils/properties/errorCodes';
6
import {ErrorMessagesComponent} from '../utils/errorMessages.component';
7
import {SearchFields} from '../utils/properties/searchFields';
8
import {SearchCustomFilter, SearchUtilsClass} from './searchUtils/searchUtils.class';
9
import {EnvProperties} from '../utils/properties/env-properties';
10
import {NewSearchPageComponent} from "./searchUtils/newSearchPage.component";
11 50169 argiro.kok
12
13
@Component({
14 58073 argiro.kok
    selector: 'search-organizations',
15 50169 argiro.kok
    template: `
16 58073 argiro.kok
      <new-search-page
17
        pageTitle="{{(simpleView?'':'Advanced ')}} Search for {{ 'organizations' | titlecase }}"
18
        entityType="organization"
19
        type="organizations"
20 58115 argiro.kok
        [results]="results"
21
        [searchUtils]="searchUtils"
22
        [fieldIds]="fieldIds" [fieldIdsMap]="fieldIdsMap" [selectedFields]="selectedFields"
23 58073 argiro.kok
        [csvParams]="csvParams" csvPath="organizations"
24
        [simpleSearchLink]="simpleSearchLink" [advancedSearchLink]="advancedSearchLink"
25
        [disableForms]="disableForms"
26
        [loadPaging]="loadPaging"
27
        [oldTotalResults]="oldTotalResults"
28 58115 argiro.kok
        [openaireLink]=openaireLink
29 58073 argiro.kok
        [piwikSiteId]=piwikSiteId [hasPrefix]="hasPrefix"
30 58327 konstantin
        searchFormClass="organizationsSearchForm"
31 58115 argiro.kok
        [includeOnlyResultsAndFilter]="includeOnlyResultsAndFilter"
32
33
        [filters]="filters"
34 58331 argiro.kok
        [simpleView]="simpleView" formPlaceholderText="Search by organization name..."
35 58073 argiro.kok
      >
36
      </new-search-page>
37 50169 argiro.kok
    `
38
 })
39
40 58073 argiro.kok
export class SearchOrganizationsComponent {
41 50169 argiro.kok
  private errorCodes: ErrorCodes;
42 54825 konstantin
  private errorMessages: ErrorMessagesComponent;
43 50586 argiro.kok
  properties:EnvProperties;
44 51746 argiro.kok
@Input() piwikSiteId = null;
45 50169 argiro.kok
  public results =[];
46
  public filters =[];
47
  public searchUtils:SearchUtilsClass = new SearchUtilsClass();
48
  public searchFields:SearchFields = new SearchFields();
49
50
  public fieldIds:  string[] = this.searchFields.ORGANIZATION_ADVANCED_FIELDS;
51
  public fieldIdsMap  = this.searchFields.ORGANIZATION_FIELDS;
52
  public selectedFields:AdvancedField[] =  [];
53
  public csvParams: string;
54
  public disableForms: boolean = false;
55
  public loadPaging: boolean = true;
56
  public oldTotalResults: number = 0;
57 50586 argiro.kok
  public pagingLimit: number = 0;
58
  public isPiwikEnabled;
59 50169 argiro.kok
60 56059 argiro.kok
  @Input() customFilter:SearchCustomFilter= null;
61 58073 argiro.kok
  public refineFields: string[] = this.searchFields.ORGANIZATION_REFINE_FIELDS;
62
  @ViewChild(NewSearchPageComponent) searchPage: NewSearchPageComponent;
63
  @Input() simpleView: boolean = true;
64 58115 argiro.kok
  @Input() simpleSearchLink: string = "";
65 58073 argiro.kok
  advancedSearchLink: string = "";
66
  @Input() hasPrefix: boolean = true;
67
  @Input() openaireLink: string;
68 58115 argiro.kok
  @Input() includeOnlyResultsAndFilter: boolean = false;
69
  @Output() searchPageUpdates = new EventEmitter();
70 50169 argiro.kok
public resourcesQuery = "(oaftype exact organization)";
71
  constructor (private route: ActivatedRoute, private _searchOrganizationsService: SearchOrganizationsService ) {
72
    this.results =[];
73
    this.errorCodes = new ErrorCodes();
74 54825 konstantin
    this.errorMessages = new ErrorMessagesComponent();
75 50169 argiro.kok
    this.searchUtils.status = this.errorCodes.LOADING;
76
77
78
79
  }
80
  ngOnInit() {
81 50586 argiro.kok
    this.route.data
82
      .subscribe((data: { envSpecific: EnvProperties }) => {
83
        this.properties= data.envSpecific;
84 58115 argiro.kok
        if (!this.simpleSearchLink) {
85 58149 konstantin
          this.simpleSearchLink = this.properties.searchLinkToOrganizations;
86
        }        this.advancedSearchLink = this.properties.searchLinkToAdvancedOrganizations;
87 58073 argiro.kok
        this.searchUtils.baseUrl = (this.simpleView)?this.simpleSearchLink:this.advancedSearchLink;
88
        this.pagingLimit = data.envSpecific.pagingLimit;
89
        this.isPiwikEnabled = data.envSpecific.enablePiwikTrack;
90 50586 argiro.kok
91
      });
92 50169 argiro.kok
    //var errorCodes:ErrorCodes = new ErrorCodes();
93
    this.searchUtils.status = this.errorCodes.LOADING;
94 58073 argiro.kok
    var firstLoad = true;
95 50169 argiro.kok
96
    this.sub =  this.route.queryParams.subscribe(params => {
97 58030 argiro.kok
      this.loadPaging = true;
98 50169 argiro.kok
      if(params['page'] && this.searchUtils.page != params['page']) {
99
        this.loadPaging = false;
100
        this.oldTotalResults = this.searchUtils.totalResults;
101
      }
102 58073 argiro.kok
      var refine = true;
103
      if (this.searchUtils.page != ((params['page'] === undefined) ? 1 : +params['page']) && this.filters && !firstLoad) {
104
        refine = false;
105 50169 argiro.kok
106 58073 argiro.kok
      }
107 50169 argiro.kok
      let page = (params['page']=== undefined)?1:+params['page'];
108
      this.searchUtils.page = ( page <= 0 ) ? 1 : page;
109 53919 konstantin
110
      this.searchUtils.size = (params['size']=== undefined)?10:+params['size'];
111
      if(this.searchUtils.size != 5 && this.searchUtils.size != 10 && this.searchUtils.size != 20 && this.searchUtils.size != 50) {
112
        this.searchUtils.size = 10;
113
      }
114
115 50169 argiro.kok
      this.searchPage.fieldIds = this.fieldIds;
116
      this.selectedFields =[];
117 58073 argiro.kok
      // this.searchPage.selectedFields = this.selectedFields;
118
      // this.searchPage.fieldIdsMap = this.fieldIdsMap;
119
      // this.searchPage.customFilter = this.customFilter;
120
      // this.searchPage.getSelectedFiltersFromUrl(params);
121 58105 konstantin
      this.searchPage.prepareSearchPage(this.fieldIds, this.selectedFields, this.refineFields, [], this.fieldIdsMap,this.customFilter,params, "organization");
122 50169 argiro.kok
123 58073 argiro.kok
      this.getResults(this.searchPage.getSearchAPIQueryForAdvancedSearhFields(),  this.searchUtils.page, this.searchUtils.size, refine, this.searchPage.getSearchAPIQueryForRefineFields(params, firstLoad));
124
125 50169 argiro.kok
    });
126
  }
127
  ngOnDestroy() {
128
    this.sub.unsubscribe();
129
  }
130
  sub: any;
131 58073 argiro.kok
  public getResults(parameters:string, page: number, size: number, refine: boolean, refineFieldsFilterQuery = null){
132 50586 argiro.kok
    if(page > this.pagingLimit) {
133
      size=0;
134
    }
135
    if(page <= this.pagingLimit || this.searchUtils.status == this.errorCodes.LOADING) {
136 58132 konstantin
      // if(parameters!= null && parameters != ''  ) {
137
      //   this.csvParams ="&fq=( "+this.resourcesQuery + "and (" + parameters + "))";
138
      // }else{
139
      //   this.csvParams ="&fq="+this.resourcesQuery;
140
      // }
141 50169 argiro.kok
142 58132 konstantin
      this.csvParams = (parameters ? ("&fq=("+parameters) : "") + (parameters ? ")" : "");
143
      this.csvParams += (refineFieldsFilterQuery ? refineFieldsFilterQuery : "");
144
145 50586 argiro.kok
      //var errorCodes:ErrorCodes = new ErrorCodes();
146
      this.searchUtils.status = this.errorCodes.LOADING;
147
      //this.searchPage.openLoading();
148
      this.disableForms = true;
149 58115 argiro.kok
      this.searchPageUpdates.emit({disableForms: this.disableForms,searchUtils: this.searchUtils});
150
151 50586 argiro.kok
      this.results = [];
152
      this.searchUtils.totalResults = 0;
153 50169 argiro.kok
154 54775 konstantin
      //console.info("Advanced Search for Organizations: Execute search query "+parameters);
155 58073 argiro.kok
       this._searchOrganizationsService.advancedSearchOrganizations(parameters, page, size, this.properties,(refine) ? this.searchPage.getRefineFieldsQuery() : null, this.searchPage.getFields(), refineFieldsFilterQuery).subscribe(
156 50169 argiro.kok
        data => {
157
            this.searchUtils.totalResults = data[0];
158
            this.results = data[1];
159 58073 argiro.kok
          if (refine) {
160 58354 argiro.kok
            this.filters = this.searchPage.prepareFiltersToShow(data[2], this.searchUtils.totalResults);
161 58073 argiro.kok
          }else{
162 58105 konstantin
            this.searchPage.buildPageURLParameters(this.filters, [],false);
163 58073 argiro.kok
          }
164
            // this.searchPage.updateBaseUrlWithParameters();
165 50169 argiro.kok
            //var errorCodes:ErrorCodes = new ErrorCodes();
166
            this.searchUtils.status = this.errorCodes.DONE;
167
            if(this.searchUtils.totalResults == 0 ){
168
              this.searchUtils.status = this.errorCodes.NONE;
169
            }
170
            //this.searchPage.closeLoading();
171
            this.disableForms = false;
172 58115 argiro.kok
          this.searchPageUpdates.emit({disableForms: this.disableForms,searchUtils: this.searchUtils});
173 50169 argiro.kok
174 58115 argiro.kok
          if(this.searchUtils.status == this.errorCodes.DONE) {
175 50169 argiro.kok
              // Page out of limit!!!
176
              let totalPages:any = this.searchUtils.totalResults/(this.searchUtils.size);
177
              if(!(Number.isInteger(totalPages))) {
178
                  totalPages = (parseInt(totalPages, 10) + 1);
179
              }
180
              if(totalPages < page) {
181
                this.searchUtils.totalResults = 0;
182
                this.searchUtils.status = this.errorCodes.OUT_OF_BOUND;
183
              }
184
            }
185
        },
186
        err => {
187 54825 konstantin
            //console.log(err);
188
            this.handleError("Error getting organizations", err);
189
            this.searchUtils.status = this.errorMessages.getErrorCode(err.status);
190
191 50169 argiro.kok
            //TODO check erros (service not available, bad request)
192
            // if( ){
193
            //   this.searchUtils.status = errorCodes.ERROR;
194
            // }
195
            //var errorCodes:ErrorCodes = new ErrorCodes();
196
            //this.searchUtils.status = errorCodes.NOT_AVAILABLE;
197 54825 konstantin
            /*if(err.status == '404') {
198 50169 argiro.kok
              this.searchUtils.status = this.errorCodes.NOT_FOUND;
199
            } else if(err.status == '500') {
200
              this.searchUtils.status = this.errorCodes.ERROR;
201
            } else {
202
              this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
203 54825 konstantin
            }*/
204 50169 argiro.kok
205
            //this.searchPage.closeLoading();
206
            this.disableForms = false;
207 58115 argiro.kok
          this.searchPageUpdates.emit({disableForms: this.disableForms,searchUtils: this.searchUtils});
208
209 50169 argiro.kok
        }
210 50586 argiro.kok
      );
211
    }
212 50169 argiro.kok
  }
213
214 54825 konstantin
  private handleError(message: string, error) {
215
    console.error("Organizations advanced Search Page: "+message, error);
216
  }
217 50169 argiro.kok
}