Project

General

Profile

1
import {SearchProjectsService} from '../../services/searchProjects.service';
2
import {ErrorCodes} from '../../utils/properties/errorCodes';
3
import {ErrorMessagesComponent}    from '../../utils/errorMessages.component';
4
import {SearchCustomFilter, SearchUtilsClass} from '../../searchPages/searchUtils/searchUtils.class';
5
import{EnvProperties} from '../../utils/properties/env-properties';
6
import {StringUtils} from '../../utils/string-utils.class';
7
import {SearchResult} from "../entities/searchResult";
8
export class FetchProjects{
9
  private errorCodes: ErrorCodes;
10
  private errorMessages: ErrorMessagesComponent;
11

    
12
  public results: SearchResult[] =[];
13

    
14
  public filters; // for getResultsForOrganizations
15
  public funders:any = [];  // for getResultsForOrganizations // this is filled with the initial query - before filtering
16

    
17
  public sub: any;
18
  public subResults: any;
19
  public searchUtils:SearchUtilsClass = new SearchUtilsClass();
20

    
21

    
22
  constructor (private _searchProjectsService: SearchProjectsService) {
23
    this.errorCodes = new ErrorCodes();
24
    this.errorMessages = new ErrorMessagesComponent();
25
    this.searchUtils.status = this.errorCodes.LOADING;
26

    
27
  }
28

    
29
  public ngOnDestroy() {
30
    if(this.sub){
31
      this.sub.unsubscribe();
32
    }
33
    if(this.subResults){
34
      this.subResults.unsubscribe();
35
    }
36
  }
37

    
38
  public getResultsByKeyword(keyword:string,  page: number, size: number, properties:EnvProperties,  customFilter:SearchCustomFilter=null){
39
    var parameters = "";
40
    if(keyword.length > 0){
41
      parameters = "q=" + StringUtils.URIEncode(keyword);
42
    }
43

    
44
    //var errorCodes:ErrorCodes = new ErrorCodes();
45
    this.searchUtils.status = this.errorCodes.LOADING;
46
    var refineParams = null;
47
    if(customFilter){
48
      refineParams = (refineParams?(refineParams+'&'):'')+"&fq="+StringUtils.URIEncode(customFilter.queryFieldName + " exact " + StringUtils.quote((customFilter.valueId )));
49
    }
50
  this.subResults = this._searchProjectsService.searchProjects(parameters, refineParams, page, size, [], properties).subscribe(
51
        data => {
52
            this.searchUtils.totalResults = data[0];
53
            //console.info("search Projects: [Parameters:"+parameters+" ]  [total results:"+this.searchUtils.totalResults+"]");
54
            this.results = data[1];
55

    
56
             //var errorCodes:ErrorCodes = new ErrorCodes();
57
            this.searchUtils.status = this.errorCodes.DONE;
58
            if(this.searchUtils.totalResults == 0 ){
59
              this.searchUtils.status = this.errorCodes.NONE;
60
            }
61
         },
62
        err => {
63
            /*console.log(err);
64
             //TODO check erros (service not available, bad request)
65
            // if( ){
66
            //   this.searchUtils.status = ErrorCodes.ERROR;
67
            // }
68
            //var errorCodes:ErrorCodes = new ErrorCodes();
69
            //this.searchUtils.status = errorCodes.ERROR;
70
            if(err.status == '404') {
71
              this.searchUtils.status = this.errorCodes.NOT_FOUND;
72
            } else if(err.status == '500') {
73
              this.searchUtils.status = this.errorCodes.ERROR;
74
            } else {
75
              this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
76
            }*/
77
            this.handleError("Error getting projects for keyword: "+keyword, err);
78
            this.searchUtils.status = this.errorMessages.getErrorCode(err.status);
79
         }
80
    );
81
  }
82

    
83
  public getResultsForDataproviders(id:string, page: number, size: number, properties:EnvProperties){
84
      //var errorCodes:ErrorCodes = new ErrorCodes();
85
      this.searchUtils.status = this.errorCodes.LOADING;
86

    
87
    this._searchProjectsService.getProjectsforDataProvider(id, page, size, properties).subscribe(
88
        data => {
89
            this.searchUtils.totalResults = data[0];
90
            //console.info("search Projects for Dataproviders: [Id:"+id+" ]  [total results:"+this.searchUtils.totalResults+"]");
91
            this.results = data[1];
92

    
93
            //var errorCodes:ErrorCodes = new ErrorCodes();
94
            this.searchUtils.status = this.errorCodes.DONE;
95
            if(this.searchUtils.totalResults == 0 ){
96
              this.searchUtils.status = this.errorCodes.NONE;
97
            }
98
        },
99
        err => {
100
            /*console.log(err);
101
             //TODO check erros (service not available, bad request)
102
            // if( ){
103
            //   this.searchUtils.status = ErrorCodes.ERROR;
104
            // }
105
            //var errorCodes:ErrorCodes = new ErrorCodes();
106
            //this.searchUtils.status = errorCodes.ERROR;
107
            if(err.status == '404') {
108
              this.searchUtils.status = this.errorCodes.NOT_FOUND;
109
            } else if(err.status == '500') {
110
              this.searchUtils.status = this.errorCodes.ERROR;
111
            } else {
112
              this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
113
            }*/
114
            this.handleError("Error getting projects for content provider with id: "+id, err);
115
            this.searchUtils.status = this.errorMessages.getErrorCode(err.status);
116
        }
117
    );
118
  }
119

    
120
  public getNumForEntity(entity: string, id:string, properties:EnvProperties) {
121
      //var errorCodes:ErrorCodes = new ErrorCodes();
122
      this.searchUtils.status = this.errorCodes.LOADING;
123

    
124
      if(id != "" && entity != "") {
125

    
126
          this._searchProjectsService.numOfEntityProjects(id, entity, properties ).subscribe(
127
              data => {
128
                  this.searchUtils.totalResults = data;
129

    
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
              },
136
              err => {
137
                  /*console.log(err);
138
                   //TODO check erros (service not available, bad request)
139
                  // if( ){
140
                  //   this.searchUtils.status = ErrorCodes.ERROR;
141
                  // }
142
                  //var errorCodes:ErrorCodes = new ErrorCodes();
143
                  //this.searchUtils.status = errorCodes.ERROR;
144

    
145
                  if(err.status == '404') {
146
                    this.searchUtils.status = this.errorCodes.NOT_FOUND;
147
                  } else if(err.status == '500') {
148
                    this.searchUtils.status = this.errorCodes.ERROR;
149
                  } else {
150
                    this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
151
                  }*/
152
                  this.handleError("Error getting number of projects for "+entity+" with id: "+id, err);
153
                  this.searchUtils.status = this.errorMessages.getErrorCode(err.status);
154
              }
155
          );
156
      }
157
  }
158

    
159
  public getResultsForOrganizations(organizationId:string, filterquery:string, page: number, size: number, refineFields:string[], properties:EnvProperties){
160
      //var errorCodes:ErrorCodes = new ErrorCodes();
161
      this.searchUtils.status = this.errorCodes.LOADING;
162

    
163
    this._searchProjectsService.getProjectsForOrganizations(organizationId,filterquery, page, size,refineFields, properties).subscribe(
164
        data => {
165
            this.searchUtils.totalResults = data[0]; // the results can be filtered so this number can be no total results
166
            //console.info("search Projects for Organization: [Id:"+organizationId+" ]  [total results:"+this.searchUtils.totalResults+"]");
167
            this.results = data[1];
168
            if(refineFields && refineFields.length > 0){
169
              this.filters = data[2];
170
              filterquery = decodeURIComponent(filterquery);
171
              for(var i = 0; i < this.filters.length; i++){
172
                if(filterquery.indexOf(this.filters[i].filterId) !== -1){
173
                  //console.log("this.filters[i].filterId:"+this.filters[i].filterId);
174
                  for(var j = 0; j < this.filters[i].values.length; j++){
175
                    //console.log("this.filters[i].values[j].id:"+this.filters[i].values[j].id);
176
                      if(filterquery.indexOf(this.filters[i].values[j].id) !== -1){
177
                        this.filters[i].values[j].selected = true;
178
                      }
179
                  }
180
                }
181
              }
182
            }
183
            if(filterquery == ""){
184
              this.searchUtils.totalResultsNoFilters = this.searchUtils.totalResults;
185
              this.funders = [];
186
              for(var i = 0; i < this.filters.length; i++){
187
                //console.log("this.filters[i].filterId:"+this.filters[i].filterId);
188
                if(this.filters[i].filterId == "funder"){
189
                   this.funders = (this.filters[i].values);
190
                }
191
              }
192
              //console.log("  this.funders:"+   this.funders);
193

    
194
            }
195
            //var errorCodes:ErrorCodes = new ErrorCodes();
196
            this.searchUtils.status = this.errorCodes.DONE;
197
            if(this.searchUtils.totalResults == 0 ){
198
              this.searchUtils.status = this.errorCodes.NONE;
199
            }
200
        },
201
        err => {
202
            /*console.log(err);
203
             //TODO check erros (service not available, bad request)
204
            // if( ){
205
            //   this.searchUtils.status = ErrorCodes.ERROR;
206
            // }
207
            //var errorCodes:ErrorCodes = new ErrorCodes();
208
            //this.searchUtils.status = errorCodes.ERROR;
209
            if(err.status == '404') {
210
              this.searchUtils.status = this.errorCodes.NOT_FOUND;
211
            } else if(err.status == '500') {
212
              this.searchUtils.status = this.errorCodes.ERROR;
213
            } else {
214
              this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
215
            }*/
216
            this.handleError("Error getting projects for organization with id: "+organizationId, err);
217
            this.searchUtils.status = this.errorMessages.getErrorCode(err.status);
218
        }
219
    );
220
  }
221

    
222
  private handleError(message: string, error) {
223
      console.error("Fetch Projects (class): "+message, error);
224
  }
225
}
(3-3/4)