Project

General

Profile

1
import {SearchProjectsService} from '../../services/searchProjects.service';
2
import {ErrorCodes} from '../../utils/properties/openaireProperties';
3
import {SearchUtilsClass } from '../../searchPages/searchUtils/searchUtils.class';
4

    
5
export class FetchProjects{
6
  public results =[];
7

    
8
  public filters; // for getResultsForOrganizations
9
  public totalResults; // for getResultsForOrganizations // this is total results with the initial query  - before filtering
10
  public funders:any = [];  // for getResultsForOrganizations // this is filled with the initial query - before filtering
11

    
12
  public sub: any;
13
  public subResults: any;
14
  public searchUtils:SearchUtilsClass = new SearchUtilsClass();
15

    
16

    
17
  constructor (private _searchProjectsService: SearchProjectsService) {
18
    var errorCodes:ErrorCodes = new ErrorCodes();
19
    this.searchUtils.status =errorCodes.LOADING;
20

    
21
  }
22

    
23
  public ngOnDestroy() {
24
    if(this.sub){
25
      this.sub.unsubscribe();
26
    }
27
    if(this.subResults){
28
      this.subResults.unsubscribe();
29
    }
30
  }
31

    
32
  public getResultsByKeyword(keyword:string,  page: number, size: number){
33
    var parameters = "";
34
    if(keyword.length > 0){
35
      parameters = "q=" + keyword;
36
    }
37

    
38
    var errorCodes:ErrorCodes = new ErrorCodes();
39
    this.searchUtils.status = errorCodes.LOADING;
40

    
41
  this.subResults = this._searchProjectsService.searchProjects(parameters, null, page, size, []).subscribe(
42
        data => {
43
            this.searchUtils.totalResults = data[0];
44
            console.info("search Projects: [Parameters:"+parameters+" ]  [total results:"+this.searchUtils.totalResults+"]");
45
            this.results = data[1];
46

    
47
             var errorCodes:ErrorCodes = new ErrorCodes();
48
            this.searchUtils.status = errorCodes.DONE;
49
            if(this.searchUtils.totalResults == 0 ){
50
              this.searchUtils.status = errorCodes.NONE;
51
            }
52
         },
53
        err => {
54
            console.log(err);
55
             //TODO check erros (service not available, bad request)
56
            // if( ){
57
            //   this.searchUtils.status = ErrorCodes.ERROR;
58
            // }
59
            var errorCodes:ErrorCodes = new ErrorCodes();
60
            this.searchUtils.status = errorCodes.ERROR;
61
         }
62
    );
63
  }
64

    
65
  public getResultsForDataproviders(id:string, page: number, size: number){
66
      var errorCodes:ErrorCodes = new ErrorCodes();
67
      this.searchUtils.status = errorCodes.LOADING;
68

    
69
    this._searchProjectsService.getProjectsforDataProvider(id, page, size).subscribe(
70
        data => {
71
            this.searchUtils.totalResults = data[0];
72
            console.info("search Projects for Dataproviders: [Id:"+id+" ]  [total results:"+this.searchUtils.totalResults+"]");
73
            this.results = data[1];
74

    
75
            var errorCodes:ErrorCodes = new ErrorCodes();
76
            this.searchUtils.status = errorCodes.DONE;
77
            if(this.searchUtils.totalResults == 0 ){
78
              this.searchUtils.status = errorCodes.NONE;
79
            }
80
        },
81
        err => {
82
            console.log(err);
83
             //TODO check erros (service not available, bad request)
84
            // if( ){
85
            //   this.searchUtils.status = ErrorCodes.ERROR;
86
            // }
87
            var errorCodes:ErrorCodes = new ErrorCodes();
88
            this.searchUtils.status = errorCodes.ERROR;
89
        }
90
    );
91
  }
92

    
93
  public getNumForEntity(entity: string, id:string) {
94
      var parameters="";
95

    
96
      if(entity == "organization") {
97
        parameters = "organizations/"+id+"/projects/count";
98
      }
99

    
100
      var errorCodes:ErrorCodes = new ErrorCodes();
101
      this.searchUtils.status = errorCodes.LOADING;
102

    
103
      if(parameters != "") {
104

    
105
          this._searchProjectsService.numOfProjects(parameters).subscribe(
106
              data => {
107
                  this.searchUtils.totalResults = data;
108

    
109
                  var errorCodes:ErrorCodes = new ErrorCodes();
110
                  this.searchUtils.status = errorCodes.DONE;
111
                  if(this.searchUtils.totalResults == 0 ){
112
                    this.searchUtils.status = errorCodes.NONE;
113
                  }
114
              },
115
              err => {
116
                  console.log(err);
117
                   //TODO check erros (service not available, bad request)
118
                  // if( ){
119
                  //   this.searchUtils.status = ErrorCodes.ERROR;
120
                  // }
121
                  var errorCodes:ErrorCodes = new ErrorCodes();
122
                  this.searchUtils.status = errorCodes.ERROR;
123
              }
124
          );
125
      }
126
  }
127

    
128
  public getResultsForOrganizations(organizationId:string, filterquery:string, page: number, size: number, refineFields:string[]){
129
      var errorCodes:ErrorCodes = new ErrorCodes();
130
      this.searchUtils.status = errorCodes.LOADING;
131

    
132
    this._searchProjectsService.getProjectsForOrganizations(organizationId,filterquery, page, size,refineFields).subscribe(
133
        data => {
134
            this.searchUtils.totalResults = data[0]; // the results can be filtered so this number can be no total results
135
            console.info("search Projects for Organization: [Id:"+organizationId+" ]  [total results:"+this.searchUtils.totalResults+"]");
136
            this.results = data[1];
137
            if(refineFields && refineFields.length > 0){
138
              this.filters = data[2];
139
              filterquery = decodeURIComponent(filterquery);
140
              for(var i = 0; i < this.filters.length; i++){
141
                if(filterquery.indexOf(this.filters[i].filterId) !== -1){
142
                  console.log("this.filters[i].filterId:"+this.filters[i].filterId);
143
                  for(var j = 0; j < this.filters[i].values.length; j++){
144
                    console.log("this.filters[i].values[j].id:"+this.filters[i].values[j].id);
145
                      if(filterquery.indexOf(this.filters[i].values[j].id) !== -1){
146
                        this.filters[i].values[j].selected = true;
147
                      }
148
                  }
149
                }
150
              }
151
            }
152

    
153
            if(!this.totalResults && filterquery == ""){
154
              this.totalResults = this.searchUtils.totalResults;
155
              this.funders = [];
156
              for(var i = 0; i < this.filters.length; i++){
157
                console.log("this.filters[i].filterId:"+this.filters[i].filterId);
158
                if(this.filters[i].filterId == "funderid"){
159
                   this.funders = (this.filters[i].values);
160

    
161
                }
162
              }
163
              console.log("  this.funders:"+   this.funders);
164

    
165
            }
166
            var errorCodes:ErrorCodes = new ErrorCodes();
167
            this.searchUtils.status = errorCodes.DONE;
168
            if(this.searchUtils.totalResults == 0 ){
169
              this.searchUtils.status = errorCodes.NONE;
170
            }
171
        },
172
        err => {
173
            console.log(err);
174
             //TODO check erros (service not available, bad request)
175
            // if( ){
176
            //   this.searchUtils.status = ErrorCodes.ERROR;
177
            // }
178
            var errorCodes:ErrorCodes = new ErrorCodes();
179
            this.searchUtils.status = errorCodes.ERROR;
180
        }
181
    );
182
  }
183

    
184

    
185
}
(5-5/6)