Project

General

Profile

1
import {Component, ViewChild} from '@angular/core';
2
import { ActivatedRoute} from '@angular/router';
3

    
4
import {ErrorCodes} from '../../openaireLibrary/utils/properties/errorCodes';
5
import {ErrorMessagesComponent}    from '../../openaireLibrary/utils/errorMessages.component';
6
import {SearchFields} from '../../openaireLibrary/utils/properties/searchFields';
7
import {SearchCustomFilter, SearchUtilsClass} from '../../openaireLibrary/searchPages/searchUtils/searchUtils.class';
8
import {EnvProperties} from '../../openaireLibrary/utils/properties/env-properties';
9
import {SearchCommunityDataprovidersService} from '../../openaireLibrary/connect/contentProviders/searchDataproviders.service';
10
import {PiwikHelper} from '../../utils/piwikHelper';
11
import {properties} from "../../../environments/environment";
12
import {CommunitiesService} from "../../openaireLibrary/connect/communities/communities.service";
13
import {CommunityService} from "../../openaireLibrary/connect/community/community.service";
14
import {Subscriber} from "rxjs";
15
import {AdvancedField} from "../../openaireLibrary/searchPages/searchUtils/searchHelperClasses.class";
16
import {NewSearchPageComponent} from "../../openaireLibrary/searchPages/searchUtils/newSearchPage.component";
17
import {SearchResult} from "../../openaireLibrary/utils/entities/searchResult";
18

    
19
@Component({
20
    selector: 'openaire-search-dataproviders',
21
    template: `
22
      
23
    <new-search-page  
24
      pageTitle="Search Content Providers"
25
      entityType="dataprovider"
26
      type=" Content provider"
27
      [results]="results"
28
      [searchUtils]="searchUtils"
29
      [sortedByChanged]="searchUtils.sortBy" [resultsPerPageChanged]="searchUtils.size"
30
      [fieldIds]="fieldIds" [fieldIdsMap]="fieldIdsMap" [selectedFields]="selectedFields"
31
      
32
      [simpleSearchLink]="properties.searchLinkToDataProviders"  
33
      [disableForms]="disableForms"
34
      [disableRefineForms]="disableRefineForms"
35
      [loadPaging]="loadPaging"
36
      [oldTotalResults]="oldTotalResults"
37
      [openaireLink]="'https://' + (properties.environment == 'production'?'':'beta.') + 'explore.openaire.eu/search/find/dataproviders'"
38
      [includeOnlyResultsAndFilter]="false"
39
      [piwikSiteId]=piwikSiteId [hasPrefix]="false"
40
      searchFormClass="datasourcesTableSearchForm"      [entitiesSelection]="true"  [showSwitchSearchLink]="false"
41
      [filters]="filters"
42
      [simpleView]="true" formPlaceholderText="Search by name..."
43
      [showResultCount]="false" [showIndexInfo]="false" [showDownload]="false"
44
      [sort]="false" [showBreadcrumb]="true">
45
      [customFilter]=customFilter
46
    </new-search-page>
47

    
48
    `
49

    
50
})
51
export class OpenaireSearchDataprovidersComponent {
52
  private errorCodes: ErrorCodes;
53
  private errorMessages: ErrorMessagesComponent;
54
  public columnNames = ['Name', 'Official Name'];
55
  public results =[];
56
  public filters =[];
57
  public searchUtils:SearchUtilsClass = new SearchUtilsClass();
58
  public searchFields:SearchFields = new SearchFields();
59
  public refineFields: string[] = [];// =  this.searchFields.JOURNAL_FIELDS;
60
  properties:EnvProperties= properties;
61

    
62
  public disableForms: boolean = false;
63
  public enableSearchView: boolean = true;
64

    
65
  private communityId: string = '';
66
  subscriptions = [];
67
  piwikSiteId = null;
68
  customFilter: SearchCustomFilter = null;
69
  initialLoad = true;
70
  public allResults =[];
71
  @ViewChild(NewSearchPageComponent, { static: true }) searchPage: NewSearchPageComponent;
72
  public fieldIds:  string[] = this.searchFields.DATASOURCE_ADVANCED_FIELDS;
73
  public fieldIdsMap= this.searchFields.DATASOURCE_FIELDS;
74
  public selectedFields:AdvancedField[] =  [];
75
  public disableRefineForms: boolean = false;
76
  public loadPaging: boolean = true;
77
  public oldTotalResults: number = 0;
78
  keyword;
79
  constructor (private route: ActivatedRoute, private _searchDataprovidersService: SearchCommunityDataprovidersService, private _communitiesService: CommunitiesService, private _communityService: CommunityService) {
80
    this.errorCodes = new ErrorCodes();
81
    this.errorMessages = new ErrorMessagesComponent();
82
    this.searchUtils.status = this.errorCodes.LOADING;
83

    
84
  }
85

    
86
  public ngOnInit() {
87
    this.searchUtils.baseUrl = this.properties.searchLinkToDataProviders;
88
      this.subscriptions.push(this._communityService.getCommunityAsObservable().subscribe(community =>{
89
        if(community != null) {
90
          this.communityId = community.communityId;
91
          this.customFilter = new SearchCustomFilter("Community", "communityId", this.communityId, community.shortTitle);
92
          this.piwikSiteId = PiwikHelper.getSiteId(this.communityId, this.properties.environment);
93
          this.subscriptions.push(this.route.queryParams.subscribe(params => {
94
            let page = (params['page'] === undefined) ? 1 : +params['page'];
95
            this.searchUtils.page = (page <= 0) ? 1 : page;
96
            this.searchUtils.size = (params['size']=== undefined)?10:+params['size'];
97
            if(this.searchUtils.size != 5 && this.searchUtils.size != 10 && this.searchUtils.size != 20 && this.searchUtils.size != 50) {
98
              this.searchUtils.size = 10;
99
            }
100
            this.keyword = decodeURIComponent(params['fv0']?params['fv0']:(params['keyword']?params['keyword']:''));
101
            this.selectedFields = [];
102
            this.searchPage.prepareSearchPage(this.fieldIds, this.selectedFields, [], [], this.fieldIdsMap, this.customFilter, params, "dataprovider");
103
            if (this.initialLoad) {
104
              this.initialLoad = false;
105
              this._getResults();
106
            } else {
107
              this.filterResults();
108
            }
109
          }));
110
        }
111
    }));
112

    
113
  }
114

    
115
  public ngOnDestroy() {
116
    this.subscriptions.forEach(subscription => {
117
      if (subscription instanceof Subscriber) {
118
        subscription.unsubscribe();
119
      }
120
    });
121
  }
122
    private _getResults(){
123
      this.searchUtils.status = this.errorCodes.LOADING;
124
      this.disableForms = true;
125
      this.enableSearchView = false;
126
      this.results = [];
127
      this.searchUtils.totalResults = 0;
128
      this.subscriptions.push(this._searchDataprovidersService.searchDataproviders(this.properties, this.communityId).subscribe(
129
          data => {
130
            this.allResults = this.parseResults(data);
131
            this.searchUtils.status = this.errorCodes.DONE;
132
            this.filterResults();
133
          },
134
          err => {
135
            /*
136
            console.log(err);
137
             //TODO check erros (service not available, bad request)
138

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

    
150
            this.enableSearchView = true;
151
          }
152
      ));
153
    }
154
  parseResults(data){
155
    let results:SearchResult[]=[];
156
    for(let result of data){
157
      let sResult:SearchResult = new SearchResult();
158
      sResult.id = result.openaireId;
159
      sResult.title = {name:"", accessMode: null, sc39: null};
160
      sResult.title.name = result.officialname?result.officialname:result.name;
161
      results.push(sResult);
162
    }
163
    return results;
164
  }
165
  filterResults(){
166
    let results = this.allResults.filter(value => {return value.title.name && value.title.name.toLowerCase().indexOf(this.keyword.toLowerCase()) !=-1 });
167
    // this.oldTotalResults = results.length;
168
    this.searchUtils.totalResults = results.length;
169
    this.results = results.slice((this.searchUtils.page - 1) * this.searchUtils.size, this.searchUtils.page *this.searchUtils.size );
170
    this.searchUtils.status = this.results.length == 0 ? this.errorCodes.NONE:  this.errorCodes.DONE;
171
    this.searchPage.buildPageURLParameters(this.filters, [], false);
172
    this.disableForms = false;
173
    this.enableSearchView = true;
174
  }
175
  private handleError(message: string, error) {
176
    console.error("Content Providers simple Search Page: "+message, error);
177
  }
178
}
(3-3/12)