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 {SearchCommunityDataprovidersService} from '../../openaireLibrary/connect/contentProviders/searchDataproviders.service';
14
import {ConnectHelper} from '../../openaireLibrary/connect/connectHelper';
15
import {PiwikHelper} from '../../utils/piwikHelper';
16

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

    
21
    <search-page-table    pageTitle="Search Content Providers"
22
                          type="content providers" entityType="dataprovider"
23
                          [(results)] = "results"  [(searchUtils)] = "searchUtils"
24
                          [columnNames]="columnNames"
25
                          [showResultCount]=false
26
                          openaireLink="https://beta.explore.openaire.eu/search/find/dataproviders"
27
                          [disableForms]="disableForms"
28
                          [enableSearchView]="enableSearchView"
29
                          searchFormClass="datasourcesTableSearchForm"
30
                          formPlaceholderText="Search for Content Providers"
31
                          [piwikSiteId]="piwikSiteId" [hasPrefix]="false">
32
    </search-page-table>
33
    `
34

    
35
})
36
export class OpenaireSearchDataprovidersComponent {
37
  private errorCodes: ErrorCodes;
38
  private errorMessages: ErrorMessagesComponent;
39
  public columnNames = ['Name', 'Official Name'];
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 _searchDataprovidersService: SearchCommunityDataprovidersService) {
58
    this.errorCodes = new ErrorCodes();
59
    this.errorMessages = new ErrorMessagesComponent();
60
    this.searchUtils.status = this.errorCodes.LOADING;
61

    
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
      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._searchDataprovidersService.searchDataproviders(this.properties, this.communityId).subscribe(
104
          data => {
105
            //this.filters = this.createFilters(data, params);
106
            this.searchUtils.totalResults = data.length;
107
            //console.info("search Content Providers [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 content providers 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=[];
151
    var filter_ids=[];
152
    var searchFields = new SearchFields();
153
    var filter_original_ids = [];//searchFields.JOURNAL_FIELDS;
154

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

    
159
    var value_names=[];
160
    var value_original_ids=[];
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' };
185
       filters.push(filter);
186
    }
187
    console.info(filters);
188

    
189
    return filters;
190
  }
191
  */
192

    
193
  private handleError(message: string, error) {
194
    console.error("Content Providers simple Search Page: "+message, error);
195
  }
196
}
(3-3/21)