Project

General

Profile

1 50169 argiro.kok
import {Component, Input, ViewChild} from '@angular/core';
2 58030 argiro.kok
import {ActivatedRoute} from '@angular/router';
3
import {AdvancedField} from '../searchUtils/searchHelperClasses.class';
4 50169 argiro.kok
import {SearchProjectsService} from '../../services/searchProjects.service';
5 50761 argiro.kok
import {ErrorCodes} from '../../utils/properties/errorCodes';
6 58030 argiro.kok
import {ErrorMessagesComponent} from '../../utils/errorMessages.component';
7 50169 argiro.kok
import {AdvancedSearchPageComponent} from '../searchUtils/advancedSearchPage.component';
8 58030 argiro.kok
import {SearchFields} from '../../utils/properties/searchFields';
9 56092 argiro.kok
import {SearchCustomFilter, SearchUtilsClass} from '../searchUtils/searchUtils.class';
10 58030 argiro.kok
import {EnvProperties} from '../../utils/properties/env-properties';
11 50169 argiro.kok
12
@Component({
13
    selector: 'advanced-search-projects',
14
    template: `
15
    <advanced-search-page pageTitle="Advanced Search for Projects" entityType="project"
16
                 type = "projects"
17
                 [(results)] = "results"
18
                 [(searchUtils)] = "searchUtils"
19
                 [(fieldIds)]="fieldIds" [(fieldIdsMap)]="fieldIdsMap"   [(selectedFields)]="selectedFields"
20 50586 argiro.kok
                 [csvParams]="csvParams" csvPath="projects" simpleSearchLink="/search/find/projects"
21 50169 argiro.kok
                 [disableForms]="disableForms"
22
                 [loadPaging]="loadPaging"
23 53353 argiro.kok
                 [oldTotalResults]="oldTotalResults"  [piwikSiteId]=piwikSiteId
24
                 searchFormClass="projectsSearchForm">
25 50169 argiro.kok
    </advanced-search-page>
26
27
    `
28
 })
29
30
export class AdvancedSearchProjectsComponent {
31
  private errorCodes: ErrorCodes;
32 54825 konstantin
  private errorMessages: ErrorMessagesComponent;
33 51746 argiro.kok
@Input() piwikSiteId = null;
34 56092 argiro.kok
@Input() customFilter:SearchCustomFilter= null;
35 50169 argiro.kok
  public results =[];
36
  public filters =[];
37
38
  public searchUtils:SearchUtilsClass = new SearchUtilsClass();
39
  public searchFields:SearchFields = new SearchFields();
40
41
  public fieldIds:  string[] = this.searchFields.PROJECT_ADVANCED_FIELDS;
42
  public fieldIdsMap = this.searchFields.PROJECT_FIELDS;
43
  public selectedFields:AdvancedField[] =  [];
44 50586 argiro.kok
  properties:EnvProperties;
45 50169 argiro.kok
46
  @ViewChild (AdvancedSearchPageComponent) searchPage : AdvancedSearchPageComponent ;
47
  public resourcesQuery = "(oaftype exact project)";
48
  public csvParams: string;
49
  public disableForms: boolean = false;
50
  public loadPaging: boolean = true;
51
  public oldTotalResults: number = 0;
52 50586 argiro.kok
  public pagingLimit: number = 0;
53
  public isPiwikEnabled;
54 50169 argiro.kok
55
  constructor (private route: ActivatedRoute, private _searchProjectsService: SearchProjectsService ) {
56
57
    this.results =[];
58
    this.errorCodes = new ErrorCodes();
59 54825 konstantin
    this.errorMessages = new ErrorMessagesComponent();
60 50169 argiro.kok
    this.searchUtils.status = this.errorCodes.LOADING;
61
62
63
64 50586 argiro.kok
65 50169 argiro.kok
  }
66
  ngOnInit() {
67 50586 argiro.kok
    this.route.data
68
      .subscribe((data: { envSpecific: EnvProperties }) => {
69
        this.properties= data.envSpecific;
70 50761 argiro.kok
        this.searchUtils.baseUrl = data.envSpecific.searchLinkToAdvancedProjects;
71 50586 argiro.kok
         this.pagingLimit = data.envSpecific.pagingLimit;
72
         this.isPiwikEnabled = data.envSpecific.enablePiwikTrack;
73
74
      });
75 50169 argiro.kok
    //var errorCodes:ErrorCodes = new ErrorCodes();
76
    this.searchUtils.status = this.errorCodes.LOADING;
77
    this.sub =  this.route.queryParams.subscribe(params => {
78 58030 argiro.kok
      this.loadPaging = true;
79 50169 argiro.kok
      if(params['page'] && this.searchUtils.page != params['page']) {
80
        this.loadPaging = false;
81
        this.oldTotalResults = this.searchUtils.totalResults;
82
      }
83
84
      let page = (params['page']=== undefined)?1:+params['page'];
85
      this.searchUtils.page = ( page <= 0 ) ? 1 : page;
86 53919 konstantin
87
      this.searchUtils.size = (params['size']=== undefined)?10:+params['size'];
88
      if(this.searchUtils.size != 5 && this.searchUtils.size != 10 && this.searchUtils.size != 20 && this.searchUtils.size != 50) {
89
        this.searchUtils.size = 10;
90
      }
91 54775 konstantin
92 50169 argiro.kok
      this.searchPage.fieldIds = this.fieldIds;
93
      this.selectedFields =[];
94
      this.searchPage.selectedFields = this.selectedFields;
95
      this.searchPage.fieldIdsMap = this.fieldIdsMap;
96 56092 argiro.kok
      this.searchPage.customFilter = this.customFilter;
97 50169 argiro.kok
      this.searchPage.getSelectedFiltersFromUrl(params);
98
      this.getResults(this.searchPage.createQueryParameters(),  this.searchUtils.page, this.searchUtils.size);
99
100
    });
101
  }
102
  ngOnDestroy() {
103
    this.sub.unsubscribe();
104
  }
105
  sub: any;
106
  public getResults(parameters:string, page: number, size: number){
107 50586 argiro.kok
    if(page > this.pagingLimit) {
108
      size=0;
109
    }
110
    if(page <= this.pagingLimit || this.searchUtils.status == this.errorCodes.LOADING) {
111 50169 argiro.kok
      if(parameters!= null && parameters != ''  ) {
112 50586 argiro.kok
        this.csvParams ="&fq=( "+this.resourcesQuery + "and (" + parameters + "))";
113 50169 argiro.kok
      }else{
114 50586 argiro.kok
        this.csvParams ="&fq="+this.resourcesQuery;
115 50169 argiro.kok
      }
116
117 50586 argiro.kok
      //var errorCodes:ErrorCodes = new ErrorCodes();
118
      this.searchUtils.status = this.errorCodes.LOADING;
119
      //this.searchPage.openLoading();
120
      this.disableForms = true;
121
      this.results = [];
122
      this.searchUtils.totalResults = 0;
123 50169 argiro.kok
124 54775 konstantin
      //console.info("Advanced Search for Publications: Execute search query "+parameters);
125 50586 argiro.kok
       this._searchProjectsService.advancedSearchProjects(parameters,  page, size, this.properties).subscribe(
126
          data => {
127
              this.searchUtils.totalResults = data[0];
128
              this.results = data[1];
129
              this.searchPage.updateBaseUrlWithParameters();
130
              //var errorCodes:ErrorCodes = new ErrorCodes();
131
              this.searchUtils.status = this.errorCodes.DONE;
132
              if(this.searchUtils.totalResults == 0 ){
133
                this.searchUtils.status = this.errorCodes.NONE;
134
              }
135
              //this.searchPage.closeLoading();
136
              this.disableForms = false;
137 50169 argiro.kok
138 50586 argiro.kok
              if(this.searchUtils.status == this.errorCodes.DONE) {
139
                // Page out of limit!!!
140
                let totalPages:any = this.searchUtils.totalResults/(this.searchUtils.size);
141
                if(!(Number.isInteger(totalPages))) {
142
                    totalPages = (parseInt(totalPages, 10) + 1);
143
                }
144
                if(totalPages < page) {
145
                  this.searchUtils.totalResults = 0;
146
                  this.searchUtils.status = this.errorCodes.OUT_OF_BOUND;
147
                }
148 50169 argiro.kok
              }
149 50586 argiro.kok
          },
150
          err => {
151 54825 konstantin
              //console.log(err);
152
              this.handleError("Error getting projects", err);
153
              this.searchUtils.status = this.errorMessages.getErrorCode(err.status);
154
155 50586 argiro.kok
              //TODO check erros (service not available, bad request)
156
              // if( ){
157
              //   this.searchUtils.status = errorCodes.ERROR;
158
              // }
159
              //var errorCodes:ErrorCodes = new ErrorCodes();
160
              //this.searchUtils.status = errorCodes.NOT_AVAILABLE;
161 54825 konstantin
              /*if(err.status == '404') {
162 50586 argiro.kok
                this.searchUtils.status = this.errorCodes.NOT_FOUND;
163
              } else if(err.status == '500') {
164
                this.searchUtils.status = this.errorCodes.ERROR;
165
              } else {
166
                this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
167 54825 konstantin
              }*/
168 50169 argiro.kok
169 50586 argiro.kok
              //this.searchPage.closeLoading();
170
              this.disableForms = false;
171 50169 argiro.kok
172 50586 argiro.kok
          }
173
      );
174
    }
175 50169 argiro.kok
  }
176
177 54825 konstantin
  private handleError(message: string, error) {
178
    console.error("Projects advanced Search Page: "+message, error);
179
  }
180 50169 argiro.kok
}