Project

General

Profile

1
import {Component, Input, ViewChild} from '@angular/core';
2
import { ActivatedRoute} from '@angular/router';
3
import { Filter, Value} from '../searchUtils/searchHelperClasses.class';
4
import {SearchDataprovidersService} from '../../services/searchDataproviders.service';
5
import {SearchResult}     from '../../utils/entities/searchResult';
6
import {ErrorCodes} from '../../utils/properties/errorCodes';
7
import {ErrorMessagesComponent}    from '../../utils/errorMessages.component';
8
import {SearchFields, FieldDetails} from '../../utils/properties/searchFields';
9
import {SearchPageTableViewComponent } from '../searchUtils/searchPageTableView.component';
10
import {SearchUtilsClass } from '../searchUtils/searchUtils.class';
11
import{EnvProperties} from '../../utils/properties/env-properties';
12

    
13
@Component({
14
    selector: 'search-entity-registries-table',
15
    template: `
16

    
17
    <search-page-table pageTitle="Registries/ Databases"
18
                       type="content providers" entityType="dataprovider" [(filters)] = "filters"
19
                       [(results)] = "results" [(searchUtils)] = "searchUtils"
20
                       [columnNames]="columnNames"
21
                       [showResultCount]=false
22
                       [disableForms]="disableForms"
23
                       [enableSearchView]="enableSearchView"
24
                       [searchViewLink]="'/search/entity-registries'"
25
                       searchFormClass="entityRegistriesTableSearchForm"
26
                       formPlaceholderText="Search for Registries/ Databases"
27
                       [mapUrl]="mapUrl" [mapTooltipType]="'registries'">
28
    </search-page-table>
29

    
30
    `
31

    
32
})
33
export class SearchEntityRegistriesTableComponent {
34
  private errorCodes: ErrorCodes;
35
  private errorMessages: ErrorMessagesComponent;
36
  public results =[];
37
  public filters =[];
38
  public columnNames = ["Name", "Type", "Country", "Institution", "Compatibility"];
39
  public baseUrl:string;
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.ENTITY_REGISTRIES_FIELDS;
45
  public disableForms: boolean = false;
46
  public enableSearchView: boolean = true;
47
  public mapUrl: string;
48
properties:EnvProperties;
49
  @ViewChild (SearchPageTableViewComponent) searchPage : SearchPageTableViewComponent ;
50

    
51
  constructor (private route: ActivatedRoute, private _searchDataprovidersService: SearchDataprovidersService ) {
52
    this.errorCodes = new ErrorCodes();
53
    this.errorMessages = new ErrorMessagesComponent();
54
    this.searchUtils.status = this.errorCodes.LOADING;
55
  }
56

    
57
  public ngOnInit() {
58
    this.route.data
59
      .subscribe((data: { envSpecific: EnvProperties }) => {
60
        this.properties = data.envSpecific;
61
        this.baseUrl = data.envSpecific.searchLinkToEntityRegistriesDataProviders;
62
        this.mapUrl = this.properties.statisticsFrameAPIURL+"markers-test.html?show=registries";
63
      });
64
    this.refineFields.push("datasourcecountryname");
65
    this.searchPage.refineFields = this.refineFields;
66
    this.sub =  this.route.queryParams.subscribe(params => {
67
      this.searchUtils.keyword = (params['keyword']?params['keyword']:'');
68
      //this.searchUtils.page = (params['page']=== undefined)?1:+params['page'];
69
      //this.filters = this.createFilters();
70

    
71
      //this.searchPage.getParametersFromUrl(params);
72
      this._getResults(params, false, this.searchUtils.page);
73
    });
74
  }
75

    
76
  public ngOnDestroy() {
77
    if(this.sub){
78
      this.sub.unsubscribe();
79
    }
80
    if(this.subResults){
81
      this.subResults.unsubscribe();
82
    }  }
83
    private _getResults(params,refine:boolean, page: number){
84
      //var errorCodes:ErrorCodes = new ErrorCodes();
85
      this.searchUtils.status = this.errorCodes.LOADING;
86
      this.disableForms = true;
87
      this.enableSearchView = false;
88
      this.results = [];
89
      this.searchUtils.totalResults = 0;
90

    
91
      let size: number = 0;
92
      this.subResults = this._searchDataprovidersService.searchEntityRegistriesTable(this.properties).subscribe(
93
          data => {
94
              size = data;
95
              if(size > 0) {
96
                this.subResults = this._searchDataprovidersService.searchEntityRegistries("",null, page, size, [],this.properties).subscribe(
97
                    data => {
98
                        this.filters = this.createFilters(data[1], params);
99

    
100
                        this.searchUtils.totalResults = data[0];
101
                        this.results = data[1];
102
                        this.searchPage.checkSelectedFilters(this.filters);
103

    
104
                        //var errorCodes:ErrorCodes = new ErrorCodes();
105
                        this.searchUtils.status = this.errorCodes.DONE;
106
                        if(this.searchUtils.totalResults == 0 ){
107
                          this.searchUtils.status = this.errorCodes.NONE;
108
                        } else {
109
                          this.searchPage.triggerInitialLoad();
110
                          this.searchPage.transform(this.results);
111
                          this.disableForms = false;
112
                        }
113
                        this.enableSearchView = true;
114
                    },
115
                    err => {
116
                        //console.log(err);
117
                        this.handleError("Error getting entity registries", err);
118
                        this.searchUtils.status = this.errorMessages.getErrorCode(err.status);
119

    
120
                         //TODO check erros (service not available, bad request)
121
                        // if( ){
122
                        //   this.searchUtils.status = ErrorCodes.ERROR;
123
                        // }
124
                        //var errorCodes:ErrorCodes = new ErrorCodes();
125
                        //this.searchUtils.status = errorCodes.ERROR;
126
                        /*if(err.status == '404') {
127
                          this.searchUtils.status = this.errorCodes.NOT_FOUND;
128
                        } else if(err.status == '500') {
129
                          this.searchUtils.status = this.errorCodes.ERROR;
130
                        } else {
131
                          this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
132
                        }*/
133
                        this.enableSearchView = true;
134
                    }
135
                );
136
              } else {
137
                this.searchPage.checkSelectedFilters(this.filters);
138

    
139
                //var errorCodes:ErrorCodes = new ErrorCodes();
140
                this.searchUtils.status = this.errorCodes.NONE;
141
                this.enableSearchView = true;
142
              }
143
          },
144
          err => {
145
              //console.log(err);
146
              this.handleError("Error getting entity registries (size=0)", err);
147
              this.searchUtils.status = this.errorMessages.getErrorCode(err.status);
148

    
149
               //TODO check erros (service not available, bad request)
150
              // if( ){
151
              //   this.searchUtils.status = ErrorCodes.ERROR;
152
              // }
153
              //var errorCodes:ErrorCodes = new ErrorCodes();
154
              //this.searchUtils.status = errorCodes.ERROR;
155
              /*if(err.status == '404') {
156
                this.searchUtils.status = this.errorCodes.NOT_FOUND;
157
              } else if(err.status == '500') {
158
                this.searchUtils.status = this.errorCodes.ERROR;
159
              } else {
160
                this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
161
              }*/
162
              this.enableSearchView = true;
163
          }
164
      );
165
/*
166
       this.subResults = this._searchDataprovidersService.searchEntityRegistries(parameters,(refine)?this.searchPage.getRefineFieldsQuery():null, page, size, []).subscribe(
167
          data => {
168
              this.searchUtils.totalResults = data[0];
169
              console.info("search Entity Registries: [Parameters:"+parameters+" ]  [total results:"+this.searchUtils.totalResults+"]");
170
              this.results = data[1];
171
              this.searchPage.checkSelectedFilters(this.filters);
172
              this.searchPage.updateBaseUrlWithParameters(this.filters);
173
              var errorCodes:ErrorCodes = new ErrorCodes();
174
              this.searchUtils.status = errorCodes.DONE;
175
              if(this.searchUtils.totalResults == 0 ){
176
                this.searchUtils.status = errorCodes.NONE;
177
              }
178
              //this.searchPage.closeLoading();
179
              this.disableForms = false;
180

    
181
          },
182
          err => {
183
              console.log(err);
184
               //TODO check erros (service not available, bad request)
185
              // if( ){
186
              //   this.searchUtils.status = ErrorCodes.ERROR;
187
              // }
188
              var errorCodes:ErrorCodes = new ErrorCodes();
189
              this.searchUtils.status = errorCodes.ERROR;
190
              //this.searchPage.closeLoading();
191
              this.disableForms = false;
192

    
193
          }
194
      );
195
*/
196
    }
197
  private setFilters(){
198
    //TODO set filters from
199
  }
200

    
201

    
202
  private createFilters(data, params):Filter[] {
203
    let length = Array.isArray(data) ? data.length : 1;
204

    
205
    var filter_names=["Type","Compatibility","Country"];
206
    var filter_ids=["datasourcetypename","datasourcecompatibilityname","datasourcecountryname"];
207
    var searchFields = new SearchFields();
208
    var filter_original_ids = searchFields.ENTITY_REGISTRIES_FIELDS;
209
    filter_original_ids.push("datasourcecountryname");
210
    var filter_valueIsExact = [true, true, false];
211

    
212
    this.searchPage.getParametersFromUrl(params);
213

    
214
    var value_names=[
215
      ["Funder database","Registry of repositories","Scholarly Comm. Infrastructure","Registry","Information Space","Web Source"],
216

    
217
      ["OpenAIRE Basic (DRIVER OA)","OpenAIRE 2.0 (EC funding)", "OpenAIRE 2.0+ (DRIVER OA, EC funding)", "OpenAIRE 3.0 (OA, funding)","OpenAIRE Data (funded, referenced datasets)",
218
       "collected from a compatible aggregator", "proprietary", "under validation"]];
219

    
220
    var value_original_ids=[
221
      ["Funder database","Registry of repositories","Scholarly Comm. Infrastructure","Registry","Information Space","Web Source"],
222
      //["entityregistry::projects","entityregistry::repositories","scholarcomminfra","entityregistry","infospace","websource"],
223
      //["driver","openaire2.0", "driver-openaire2.0", "openaire3.0","openaire2.0_data"]
224
    ["OpenAIRE Basic (DRIVER OA)","OpenAIRE 2.0 (EC funding)", "OpenAIRE 2.0+ (DRIVER OA, EC funding)", "OpenAIRE 3.0 (OA, funding)","OpenAIRE Data (funded, referenced datasets)",
225
     "collected from a compatible aggregator", "proprietary", "under validation"]];
226

    
227
     var countries = new Set<String>();
228
     var value_name = [];
229
     var value_original_id = [];
230
     let i, j;
231

    
232
     for(i=0; i<length; i++) {
233
       let resData = Array.isArray(data) ? data[i] : data;
234
       if(resData.countries) {
235
         let length2 = Array.isArray(resData.countries) ? resData.countries.length : 1;
236

    
237
         for(j=0; j<length2; j++) {
238
          let datasourceCountry = Array.isArray(resData.countries) ? resData.countries[j] : resData.countries;
239
          if(!countries.has(datasourceCountry)) {
240
           countries.add(datasourceCountry);
241
           value_name.push(datasourceCountry);
242
           value_original_id.push(datasourceCountry.trim());
243
          }
244
         }
245
       }
246
     }
247

    
248
     value_names.push(value_name);
249
     value_original_ids.push(value_original_id);
250

    
251
    var filters: Filter[] =[];
252
    for(i =0 ; i < filter_names.length;i++){
253
      var values:Value[] = [];
254
      for(j =0 ; j < value_names[i].length;j++){
255
        var value:Value = {name: value_names[i][j], id: value_original_ids[i][j], number:j, selected:false}
256
        values.push(value);
257
      }
258
       var filter:Filter = {title: filter_names[i], filterId: filter_ids[i], originalFilterId:  filter_original_ids[i], values : values, countSelectedValues:0, "filterOperator": 'or', valueIsExact: filter_valueIsExact[i] };
259
       filters.push(filter);
260
    }
261
    return filters;
262
    }
263

    
264
    private handleError(message: string, error) {
265
      console.error("Entity Registries Search Page - Table view: "+message, error);
266
    }
267
}
(7-7/12)