Project

General

Profile

1 50428 argiro.kok
import {Component, Input, ViewChild} from '@angular/core';
2 51012 konstantin
import { ActivatedRoute} from '@angular/router';
3 50428 argiro.kok
4 51012 konstantin
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 54863 konstantin
import {ErrorMessagesComponent}    from '../../openaireLibrary/utils/errorMessages.component';
9 51012 konstantin
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 51658 konstantin
import {SearchCommunityDataprovidersService} from '../../openaireLibrary/connect/contentProviders/searchDataproviders.service';
14 51566 argiro.kok
import {ConnectHelper} from '../../openaireLibrary/connect/connectHelper';
15 51749 argiro.kok
import {PiwikHelper} from '../../utils/piwikHelper';
16 51012 konstantin
17 50428 argiro.kok
@Component({
18
    selector: 'openaire-search-dataproviders',
19
    template: `
20
21 56841 konstantin
    <search-page-table    pageTitle="Search Content Providers"
22 51012 konstantin
                          type="content providers" entityType="dataprovider"
23
                          [(results)] = "results"  [(searchUtils)] = "searchUtils"
24
                          [columnNames]="columnNames"
25
                          [showResultCount]=false
26 52171 konstantin
                          openaireLink="https://beta.explore.openaire.eu/search/find/dataproviders"
27 51012 konstantin
                          [disableForms]="disableForms"
28 52167 konstantin
                          [enableSearchView]="enableSearchView"
29 51012 konstantin
                          searchFormClass="datasourcesTableSearchForm"
30 56841 konstantin
                          formPlaceholderText="Search for Content Providers"
31
                          [piwikSiteId]="piwikSiteId" [hasPrefix]="false">
32 51012 konstantin
    </search-page-table>
33
    `
34 50428 argiro.kok
35
})
36
export class OpenaireSearchDataprovidersComponent {
37 51012 konstantin
  private errorCodes: ErrorCodes;
38 54863 konstantin
  private errorMessages: ErrorMessagesComponent;
39 51012 konstantin
  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 52167 konstantin
  public enableSearchView: boolean = true;
51 51012 konstantin
52 51014 konstantin
  private communityId: string = '';
53
54 51012 konstantin
  @ViewChild (SearchPageTableViewComponent) searchPage : SearchPageTableViewComponent ;
55 51749 argiro.kok
  piwikSiteId = null;
56 52167 konstantin
57 51012 konstantin
  constructor (private route: ActivatedRoute, private _searchDataprovidersService: SearchCommunityDataprovidersService) {
58
    this.errorCodes = new ErrorCodes();
59 54863 konstantin
    this.errorMessages = new ErrorMessagesComponent();
60 51012 konstantin
    this.searchUtils.status = this.errorCodes.LOADING;
61 54949 argiro.kok
62 51012 konstantin
  }
63
64
  public ngOnInit() {
65
    this.route.data
66
      .subscribe((data: { envSpecific: EnvProperties }) => {
67
        this.properties = data.envSpecific;
68 51407 argiro.kok
69 51012 konstantin
    //this.searchPage.refineFields = this.refineFields;
70
    this.sub =  this.route.queryParams.subscribe(params => {
71 56331 konstantin
      this.communityId  = ConnectHelper.getCommunityFromDomain(this.properties.domain);
72
      if(!this.communityId) {
73
        this.communityId = params['communityId'];
74 51407 argiro.kok
      }
75 56331 konstantin
76 56841 konstantin
      this.piwikSiteId = PiwikHelper.getSiteId(this.communityId,this.properties.environment);
77 51012 konstantin
      this.searchUtils.keyword = (params['keyword']?params['keyword']:'');
78
      //this.filters = this.createFilters();
79
      //this.searchPage.getParametersFromUrl(params);
80
81
      this._getResults(params);
82
    });
83 51407 argiro.kok
      });
84 51012 konstantin
  }
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 52167 konstantin
      this.enableSearchView = false;
98 51012 konstantin
      this.results = [];
99
      this.searchUtils.totalResults = 0;
100
101
      let size: number = 0;
102
103 51014 konstantin
      this.subResults = this._searchDataprovidersService.searchDataproviders(this.properties, this.communityId).subscribe(
104 51012 konstantin
          data => {
105
            //this.filters = this.createFilters(data, params);
106
            this.searchUtils.totalResults = data.length;
107 54781 konstantin
            //console.info("search Content Providers [total results:"+this.searchUtils.totalResults+"]");
108 51012 konstantin
            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 52167 konstantin
            } else {
117
              this.searchPage.triggerInitialLoad();
118
              this.searchPage.transform(this.results);
119
              this.disableForms = false;
120 51012 konstantin
            }
121 52167 konstantin
            this.enableSearchView = true;
122 51012 konstantin
          },
123
          err => {
124 54863 konstantin
            /*
125 51012 konstantin
            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 54863 konstantin
            */
136
            this.handleError("Error getting content providers for community with id: "+this.communityId, err);
137
            this.searchUtils.status = this.errorMessages.getErrorCode(err.status);
138 51012 konstantin
139 52167 konstantin
            this.enableSearchView = true;
140 51012 konstantin
          }
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 54863 konstantin
193
  private handleError(message: string, error) {
194
    console.error("Content Providers simple Search Page: "+message, error);
195
  }
196 50428 argiro.kok
}