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 {ErrorCodes} from '../../openaireLibrary/utils/properties/errorCodes';
7
import {ErrorMessagesComponent}    from '../../openaireLibrary/utils/errorMessages.component';
8
import {SearchFields, FieldDetails} from '../../openaireLibrary/utils/properties/searchFields';
9
import {SearchPageTableViewComponent } from '../../openaireLibrary/searchPages/searchUtils/searchPageTableView.component';
10
import {SearchCustomFilter, 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="Search Projects"
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 by project title, grant id, funder... "
30
                          [piwikSiteId]="piwikSiteId" [hasPrefix]="false" [customFilter]="customFilter" [enableEntitySelection]="true"
31
                          [includeOnlyResultsAndFilter]="false" [showBreadcrumb]="true">
32
    </search-page-table>
33
    `
34

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

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

    
52
  private communityId: string = '';
53

    
54
  @ViewChild (SearchPageTableViewComponent) searchPage : SearchPageTableViewComponent ;
55
  piwikSiteId = null;
56
  customFilter: SearchCustomFilter = null;
57
  initialLoad = true;
58
  constructor (private route: ActivatedRoute, private _searchProjectsService: SearchCommunityProjectsService) {
59
    this.errorCodes = new ErrorCodes();
60
    this.errorMessages = new ErrorMessagesComponent();
61
    this.searchUtils.status = this.errorCodes.LOADING;
62
  }
63

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

    
69
    //this.searchPage.refineFields = this.refineFields;
70
    this.sub =  this.route.queryParams.subscribe(params => {
71
      //if (!this.communityId && typeof document !== 'undefined') {
72
      this.communityId  = ConnectHelper.getCommunityFromDomain(this.properties.domain);
73
      if(!this.communityId) {
74
        this.communityId = params['communityId'];
75
      }
76
      this.customFilter = new SearchCustomFilter("Community", "communityId", this.communityId, "");
77
      this.piwikSiteId = PiwikHelper.getSiteId(this.communityId,this.properties.environment);
78
      this.searchUtils.keyword =  (params['fv0']?params['fv0']:(params['keyword']?params['keyword']:''));
79
      //this.filters = this.createFilters();
80
      //this.searchPage.getParametersFromUrl(params);
81

    
82
      if(this.initialLoad) {
83
        this.initialLoad = false;
84
        this._getResults(params);
85
      }else{
86
        this.searchPage.goTo(1);
87
      }
88
    });
89
    });
90
  }
91

    
92
  public ngOnDestroy() {
93
    if(this.sub){
94
      this.sub.unsubscribe();
95
    }
96
    if(this.subResults){
97
      this.subResults.unsubscribe();
98
    }
99
  }
100
    private _getResults(params){
101
      this.searchUtils.status = this.errorCodes.LOADING;
102
      this.disableForms = true;
103
      this.enableSearchView = false;
104
      this.results = [];
105
      this.searchUtils.totalResults = 0;
106

    
107
      let size: number = 0;
108

    
109
      this.subResults = this._searchProjectsService.searchProjects(this.properties, this.communityId).subscribe(
110
          data => {
111
            // data=data.slice(0,150)
112
            this.filters = this.createFilters(data, params);
113

    
114
            this.searchUtils.totalResults = data.length;
115
            //console.info("search Projects [total results:"+this.searchUtils.totalResults+"]");
116
            this.results = data;
117

    
118
            this.searchPage.checkSelectedFilters(this.filters);
119

    
120
            //var errorCodes:ErrorCodes = new ErrorCodes();
121
            this.searchUtils.status = this.errorCodes.DONE;
122
            if(this.searchUtils.totalResults == 0 ){
123
              this.searchUtils.status = this.errorCodes.NONE;
124
            } else {
125
              this.searchPage.triggerInitialLoad();
126
              this.searchPage.transform(this.results);
127
              this.disableForms = false;
128
            }
129
            this.enableSearchView = true;
130
          },
131
          err => {
132
            /*
133
            console.log(err);
134
             //TODO check erros (service not available, bad request)
135

    
136
            if(err.status == '404') {
137
              this.searchUtils.status = this.errorCodes.NOT_FOUND;
138
            } else if(err.status == '500') {
139
              this.searchUtils.status = this.errorCodes.ERROR;
140
            } else {
141
              this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
142
            }
143
            */
144
            this.handleError("Error getting projects for community with id: "+this.communityId, err);
145
            this.searchUtils.status = this.errorMessages.getErrorCode(err.status);
146

    
147
            this.enableSearchView = true;
148
          }
149
      );
150
    }
151
  private setFilters(){
152
    //TODO set filters from
153
  }
154

    
155
  private createFilters(data, params):Filter[] {
156
    let length = Array.isArray(data) ? data.length : 1;
157

    
158
    var filter_names=["Funder"];
159
    var filter_ids=["relfunder"];
160
    var searchFields = new SearchFields();
161
    var filter_original_ids = ["relfunder"];//searchFields.JOURNAL_FIELDS;
162

    
163
    this.refineFields = ["relfunder"];
164
    this.searchPage.refineFields = this.refineFields;
165
    this.searchPage.getParametersFromUrl(params);
166

    
167
    var value_names=[/*["Journal", "Journal Aggregator\/Publisher"]*/];
168
    var value_original_ids=[/*["pubsrepository::journal", "aggregator::pubsrepository::journals"]*/];
169

    
170
    var funders = new Set<String>();
171
    var value_name = [];
172
    var value_original_id = [];
173
    let i;
174
    for(i=0; i<length; i++) {
175
      let resData = Array.isArray(data) ? data[i] : data;
176
      if(resData.funder && !funders.has(resData.funder)) {
177
        funders.add(resData.funder);
178
        value_name.push(resData.funder);
179
        value_original_id.push(resData.funder.trim());
180
      }
181
    }
182
    value_names.push(value_name);
183
    value_original_ids.push(value_original_id);
184

    
185
    var filters: Filter[] =[];
186
    for(i =0 ; i < filter_names.length;i++){
187
      if(value_names[i].length > 0) {
188
        var values: Value[] = [];
189
        for (var j = 0; j < value_names[i].length; j++) {
190
          var value: Value = {name: value_names[i][j], id: value_original_ids[i][j], number: j, selected: false}
191
          values.push(value);
192
        }
193
        var filter: Filter = {
194
          title: filter_names[i],
195
          filterId: filter_ids[i],
196
          originalFilterId: filter_original_ids[i],
197
          values: values,
198
          countSelectedValues: 0,
199
          "filterOperator": 'or',
200
          valueIsExact: true,
201
          filterType: "checkbox"
202
        };
203
        filters.push(filter);
204
      }
205
    }
206
    return filters;
207
  }
208

    
209
  private handleError(message: string, error) {
210
    console.error("Projects simple Search Page: "+message, error);
211
  }
212
}
(14-14/24)