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

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

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

    
91
  public getResultsForOrganizations(organizationId:string, filterquery:string, page: number, size: number, refineFields:string[]){
92
    this._searchProjectsService.getProjectsForOrganizations(organizationId,filterquery, page, size,refineFields).subscribe(
93
        data => {
94
            this.searchUtils.totalResults = data[0]; // the results can be filtered so this number can be no total results
95
            console.info("search Projects for Organization: [Id:"+organizationId+" ]  [total results:"+this.searchUtils.totalResults+"]");
96
            this.results = data[1];
97
            if(refineFields && refineFields.length > 0){
98
              this.filters = data[2];
99
              filterquery = decodeURIComponent(filterquery);
100
              for(var i = 0; i < this.filters.length; i++){
101
                if(filterquery.indexOf(this.filters[i].filterId) !== -1){
102
                  console.log("this.filters[i].filterId:"+this.filters[i].filterId);
103
                  for(var j = 0; j < this.filters[i].values.length; j++){
104
                    console.log("this.filters[i].values[j].id:"+this.filters[i].values[j].id);
105
                      if(filterquery.indexOf(this.filters[i].values[j].id) !== -1){
106
                        this.filters[i].values[j].selected = true;
107
                      }
108
                  }
109
                }
110
              }
111
            }
112

    
113
            if(!this.totalResults && filterquery == ""){
114
              this.totalResults = this.searchUtils.totalResults;
115
              this.funders = [];
116
              for(var i = 0; i < this.filters.length; i++){
117
                console.log("this.filters[i].filterId:"+this.filters[i].filterId);
118
                if(this.filters[i].filterId == "funderid"){
119
                   this.funders = (this.filters[i].values);
120

    
121
                }
122
              }
123
              console.log("  this.funders:"+   this.funders);
124

    
125
            }
126
            var errorCodes:ErrorCodes = new ErrorCodes();
127
            this.searchUtils.status = errorCodes.DONE;
128
            if(this.searchUtils.totalResults == 0 ){
129
              this.searchUtils.status = errorCodes.NONE;
130
            }
131
        },
132
        err => {
133
            console.log(err);
134
             //TODO check erros (service not available, bad request)
135
            // if( ){
136
            //   this.searchUtils.status = ErrorCodes.ERROR;
137
            // }
138
            var errorCodes:ErrorCodes = new ErrorCodes();
139
            this.searchUtils.status = errorCodes.ERROR;
140
        }
141
    );
142
  }
143

    
144

    
145
}
(5-5/6)