Project

General

Profile

1
import {Component, Input, ViewChild} from '@angular/core';
2
import { ActivatedRoute} from '@angular/router';
3

    
4
import { Filter, Value} from '../../openaireLibrary/searchPages/searchUtils/searchHelperClasses.class';
5

    
6
import {SearchResult}     from '../../openaireLibrary/utils/entities/searchResult';
7
import {ErrorCodes} from '../../openaireLibrary/utils/properties/errorCodes';
8
import {SearchFields, FieldDetails} from '../../openaireLibrary/utils/properties/searchFields';
9
import {SearchPageTableViewComponent } from '../../openaireLibrary/searchPages/searchUtils/searchPageTableView.component';
10
import {SearchUtilsClass } from '../../openaireLibrary/searchPages/searchUtils/searchUtils.class';
11
import {EnvProperties} from '../../openaireLibrary/utils/properties/env-properties';
12
import {SearchCommunityProjectsService} from '../../openaireLibrary/connect/projects/searchProjects.service';
13
import {PiwikHelper} from '../../utils/piwikHelper';
14
import {ConnectHelper} from '../../openaireLibrary/connect/connectHelper';
15

    
16
@Component({
17
    selector: 'openaire-search-projects',
18
    template: `
19

    
20
    <search-page-table    pageTitle="OpenAIRE Projects Table"
21
                          type="projects" entityType="project" [(filters)] = "filters"
22
                          [(results)] = "results"  [(searchUtils)] = "searchUtils"
23
                          [columnNames]="columnNames"
24
                          [showResultCount]=false
25
                          openaireLink="https://beta.explore.openaire.eu/search/find/projects"
26
                          [disableForms]="disableForms"
27
                          [enableSearchView]="enableSearchView"
28
                          searchFormClass="projectsTableSearchForm"
29
                          formPlaceholderText="Search for Projects">
30
    </search-page-table>
31
    `
32

    
33
})
34
export class OpenaireSearchProjectsComponent {
35
  private errorCodes: ErrorCodes;
36

    
37
  public columnNames = ['Project', 'GrantId', 'Funder'];
38
  public results =[];
39
  public filters =[];
40
  public searchUtils:SearchUtilsClass = new SearchUtilsClass();
41
  public sub: any; public subResults: any;
42
  public _location:Location;
43
  public searchFields:SearchFields = new SearchFields();
44
  public refineFields: string[] = [];// =  this.searchFields.JOURNAL_FIELDS;
45
  properties:EnvProperties;
46

    
47
  public disableForms: boolean = false;
48
  public enableSearchView: boolean = true;
49

    
50
  private communityId: string = '';
51

    
52
  @ViewChild (SearchPageTableViewComponent) searchPage : SearchPageTableViewComponent ;
53
  piwikSiteId = null;
54

    
55
  constructor (private route: ActivatedRoute, private _searchProjectsService: SearchCommunityProjectsService) {
56
    this.errorCodes = new ErrorCodes();
57
    this.searchUtils.status = this.errorCodes.LOADING;
58
    var communityId = ConnectHelper.getCommunityFromDomain(document.location.hostname);
59
    this.piwikSiteId = PiwikHelper.siteIDs[communityId];
60
  }
61

    
62
  public ngOnInit() {
63
    this.route.data
64
      .subscribe((data: { envSpecific: EnvProperties }) => {
65
        this.properties = data.envSpecific;
66

    
67
    //this.searchPage.refineFields = this.refineFields;
68
    this.sub =  this.route.queryParams.subscribe(params => {
69
      this.communityId = params['communityId'];
70
      if(!this.communityId){
71
        this.communityId  = ConnectHelper.getCommunityFromDomain(document.location.hostname);
72
      }
73
      this.searchUtils.keyword = (params['keyword']?params['keyword']:'');
74
      //this.filters = this.createFilters();
75
      //this.searchPage.getParametersFromUrl(params);
76

    
77
      this._getResults(params);
78
    });
79
    });
80
  }
81

    
82
  public ngOnDestroy() {
83
    if(this.sub){
84
      this.sub.unsubscribe();
85
    }
86
    if(this.subResults){
87
      this.subResults.unsubscribe();
88
    }
89
  }
90
    private _getResults(params){
91
      this.searchUtils.status = this.errorCodes.LOADING;
92
      this.disableForms = true;
93
      this.enableSearchView = false;
94
      this.results = [];
95
      this.searchUtils.totalResults = 0;
96

    
97
      let size: number = 0;
98

    
99
      this.subResults = this._searchProjectsService.searchProjects(this.properties, this.communityId).subscribe(
100
          data => {
101
            this.filters = this.createFilters(data, params);
102

    
103
            console.info(data);
104
            this.searchUtils.totalResults = data.length;
105
            console.info("search Projects [total results:"+this.searchUtils.totalResults+"]");
106
            this.results = data;
107

    
108
            this.searchPage.checkSelectedFilters(this.filters);
109

    
110
            //var errorCodes:ErrorCodes = new ErrorCodes();
111
            this.searchUtils.status = this.errorCodes.DONE;
112
            if(this.searchUtils.totalResults == 0 ){
113
              this.searchUtils.status = this.errorCodes.NONE;
114
            } else {
115
              this.searchPage.triggerInitialLoad();
116
              this.searchPage.transform(this.results);
117
              this.disableForms = false;
118
            }
119
            this.enableSearchView = true;
120
          },
121
          err => {
122
            console.log(err);
123
             //TODO check erros (service not available, bad request)
124

    
125
            if(err.status == '404') {
126
              this.searchUtils.status = this.errorCodes.NOT_FOUND;
127
            } else if(err.status == '500') {
128
              this.searchUtils.status = this.errorCodes.ERROR;
129
            } else {
130
              this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
131
            }
132

    
133
            this.enableSearchView = true;
134
          }
135
      );
136
    }
137
  private setFilters(){
138
    //TODO set filters from
139
  }
140

    
141
  private createFilters(data, params):Filter[] {
142
    let length = Array.isArray(data) ? data.length : 1;
143

    
144
    var filter_names=["Funder"];
145
    var filter_ids=["relfunder"];
146
    var searchFields = new SearchFields();
147
    var filter_original_ids = ["relfunder"];//searchFields.JOURNAL_FIELDS;
148

    
149
    this.refineFields = ["relfunder"];
150
    this.searchPage.refineFields = this.refineFields;
151
    this.searchPage.getParametersFromUrl(params);
152

    
153
    var value_names=[/*["Journal", "Journal Aggregator\/Publisher"]*/];
154
    var value_original_ids=[/*["pubsrepository::journal", "aggregator::pubsrepository::journals"]*/];
155

    
156
    var funders = new Set<String>();
157
    var value_name = [];
158
    var value_original_id = [];
159
    let i;
160
    for(i=0; i<length; i++) {
161
      let resData = Array.isArray(data) ? data[i] : data;
162
      if(resData.funder && !funders.has(resData.funder)) {
163
        funders.add(resData.funder);
164
        value_name.push(resData.funder);
165
        value_original_id.push(resData.funder.trim());
166
      }
167
    }
168
    value_names.push(value_name);
169
    value_original_ids.push(value_original_id);
170

    
171
    var filters: Filter[] =[];
172
    for(i =0 ; i < filter_names.length;i++){
173
      var values:Value[] = [];
174
      for(var j =0 ; j < value_names[i].length;j++){
175
        var value:Value = {name: value_names[i][j], id: value_original_ids[i][j], number:j, selected:false}
176
        values.push(value);
177
      }
178
       var filter:Filter = {title: filter_names[i], filterId: filter_ids[i], originalFilterId:  filter_original_ids[i], values : values, countSelectedValues:0, "filterOperator": 'or' };
179
       filters.push(filter);
180
       console.info(filter);
181
    }
182
    return filters;
183
  }
184
}
(11-11/18)