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 {ErrorMessagesComponent}    from '../../openaireLibrary/utils/errorMessages.component';
9
import {SearchFields, FieldDetails} from '../../openaireLibrary/utils/properties/searchFields';
10
import {SearchPageTableViewComponent } from '../../openaireLibrary/searchPages/searchUtils/searchPageTableView.component';
11
import {SearchUtilsClass } from '../../openaireLibrary/searchPages/searchUtils/searchUtils.class';
12
import {EnvProperties} from '../../openaireLibrary/utils/properties/env-properties';
13
import {SearchCommunityProjectsService} from '../../openaireLibrary/connect/projects/searchProjects.service';
14
import {PiwikHelper} from '../../utils/piwikHelper';
15
import {ConnectHelper} from '../../openaireLibrary/connect/connectHelper';
16

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

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

    
34
})
35
export class OpenaireSearchProjectsComponent {
36
  private errorCodes: ErrorCodes;
37
  private errorMessages: ErrorMessagesComponent;
38
  public columnNames = ['Project', 'GrantId', 'Funder'];
39
  public results =[];
40
  public filters =[];
41
  public searchUtils:SearchUtilsClass = new SearchUtilsClass();
42
  public sub: any; public subResults: any;
43
  public _location:Location;
44
  public searchFields:SearchFields = new SearchFields();
45
  public refineFields: string[] = [];// =  this.searchFields.JOURNAL_FIELDS;
46
  properties:EnvProperties;
47

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

    
51
  private communityId: string = '';
52

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

    
56
  constructor (private route: ActivatedRoute, private _searchProjectsService: SearchCommunityProjectsService) {
57
    this.errorCodes = new ErrorCodes();
58
    this.errorMessages = new ErrorMessagesComponent();
59
    this.searchUtils.status = this.errorCodes.LOADING;
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
      //if (!this.communityId && typeof document !== 'undefined') {
70
      this.communityId  = ConnectHelper.getCommunityFromDomain(this.properties.domain);
71
      if(!this.communityId) {
72
        this.communityId = params['communityId'];
73
      }
74

    
75
      this.piwikSiteId = PiwikHelper.siteIDs[this.communityId];
76
      this.searchUtils.keyword = (params['keyword']?params['keyword']:'');
77
      //this.filters = this.createFilters();
78
      //this.searchPage.getParametersFromUrl(params);
79

    
80
      this._getResults(params);
81
    });
82
    });
83
  }
84

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

    
100
      let size: number = 0;
101

    
102
      this.subResults = this._searchProjectsService.searchProjects(this.properties, this.communityId).subscribe(
103
          data => {
104
            this.filters = this.createFilters(data, params);
105

    
106
            this.searchUtils.totalResults = data.length;
107
            //console.info("search Projects [total results:"+this.searchUtils.totalResults+"]");
108
            this.results = data;
109

    
110
            this.searchPage.checkSelectedFilters(this.filters);
111

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

    
128
            if(err.status == '404') {
129
              this.searchUtils.status = this.errorCodes.NOT_FOUND;
130
            } else if(err.status == '500') {
131
              this.searchUtils.status = this.errorCodes.ERROR;
132
            } else {
133
              this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
134
            }
135
            */
136
            this.handleError("Error getting projects for community with id: "+this.communityId, err);
137
            this.searchUtils.status = this.errorMessages.getErrorCode(err.status);
138

    
139
            this.enableSearchView = true;
140
          }
141
      );
142
    }
143
  private setFilters(){
144
    //TODO set filters from
145
  }
146

    
147
  private createFilters(data, params):Filter[] {
148
    let length = Array.isArray(data) ? data.length : 1;
149

    
150
    var filter_names=["Funder"];
151
    var filter_ids=["relfunder"];
152
    var searchFields = new SearchFields();
153
    var filter_original_ids = ["relfunder"];//searchFields.JOURNAL_FIELDS;
154

    
155
    this.refineFields = ["relfunder"];
156
    this.searchPage.refineFields = this.refineFields;
157
    this.searchPage.getParametersFromUrl(params);
158

    
159
    var value_names=[/*["Journal", "Journal Aggregator\/Publisher"]*/];
160
    var value_original_ids=[/*["pubsrepository::journal", "aggregator::pubsrepository::journals"]*/];
161

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

    
177
    var filters: Filter[] =[];
178
    for(i =0 ; i < filter_names.length;i++){
179
      var values:Value[] = [];
180
      for(var j =0 ; j < value_names[i].length;j++){
181
        var value:Value = {name: value_names[i][j], id: value_original_ids[i][j], number:j, selected:false}
182
        values.push(value);
183
      }
184
       var filter:Filter = {title: filter_names[i], filterId: filter_ids[i], originalFilterId:  filter_original_ids[i], values : values, countSelectedValues:0, "filterOperator": 'or', valueIsExact: true, valueIsUnique: false };
185
       filters.push(filter);
186
    }
187
    return filters;
188
  }
189

    
190
  private handleError(message: string, error) {
191
    console.error("Projects simple Search Page: "+message, error);
192
  }
193
}
(14-14/21)