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="Search Projects"
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
                          [piwikSiteId]="piwikSiteId" [hasPrefix]="false">
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

    
57
  constructor (private route: ActivatedRoute, private _searchProjectsService: SearchCommunityProjectsService) {
58
    this.errorCodes = new ErrorCodes();
59
    this.errorMessages = new ErrorMessagesComponent();
60
    this.searchUtils.status = this.errorCodes.LOADING;
61
  }
62

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

    
68
    //this.searchPage.refineFields = this.refineFields;
69
    this.sub =  this.route.queryParams.subscribe(params => {
70
      //if (!this.communityId && typeof document !== 'undefined') {
71
      this.communityId  = ConnectHelper.getCommunityFromDomain(this.properties.domain);
72
      if(!this.communityId) {
73
        this.communityId = params['communityId'];
74
      }
75

    
76
      this.piwikSiteId = PiwikHelper.getSiteId(this.communityId,this.properties.environment);
77
      this.searchUtils.keyword = (params['keyword']?params['keyword']:'');
78
      //this.filters = this.createFilters();
79
      //this.searchPage.getParametersFromUrl(params);
80

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

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

    
101
      let size: number = 0;
102

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

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

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

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

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

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

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

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

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

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

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

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

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