Project

General

Profile

1
import {Component, Input, ViewChild} from '@angular/core';
2
import {ActivatedRoute} from '@angular/router';
3
import {Location} from '@angular/common';
4
import {SearchDataprovidersService} from '../services/searchDataproviders.service';
5
import {ErrorCodes} from '../utils/properties/errorCodes';
6
import {ErrorMessagesComponent}    from '../utils/errorMessages.component';
7
import {SearchFields} from '../utils/properties/searchFields';
8
import {SearchPageComponent } from '../searchPages/searchUtils/searchPage.component';
9
import {SearchCustomFilter, SearchUtilsClass} from '../searchPages/searchUtils/searchUtils.class';
10
import {EnvProperties} from '../utils/properties/env-properties';
11
import {StringUtils} from "../utils/string-utils.class";
12
import {ZenodoInformationClass} from "./utils/zenodoInformation.class";
13
import {PiwikService} from "../utils/piwik/piwik.service";
14

    
15
@Component({
16
  selector: 'search-dataproviders',
17
  template: `
18

    
19
    <div class="uk-section uk-padding-remove-bottom uk-padding-remove-top">
20
      <div class="communityPanelBackground uk-margin-top uk-padding-small">
21
        <div class="uk-container uk-container-large uk-margin-small-top uk-margin-small-bottom">
22
          <ul class="uk-breadcrumb">
23
            <li><a class="breadcrumb" routerLinkActive="router-link-active" routerLink="/deposit">Deposit</a></li>
24
            <li><span class="active">Browse repositories</span></li>
25
          </ul>
26
        </div>
27
      </div>
28
    </div>
29
    <search-page pageTitle="Search Content Providers"
30
                 formPlaceholderText = "Search for Content Providers"
31
                 type="content providers" entityType="dataprovider" [(filters)] = "filters"
32
                 [(results)] = "results"   [(searchUtils)] = "searchUtils" [baseUrl] = "baseUrl"
33
                 (queryChange)="queryChanged($event)"
34
                 [csvParams]="csvParams" csvPath="datasources"
35
                 [disableForms]="disableForms"
36
                 [loadPaging]="loadPaging"
37
                 [oldTotalResults]="oldTotalResults"
38
                 [piwikSiteId]=piwikSiteId
39
                 [searchFormClass]="''"
40
                 [usedBy]="'deposit'" 
41
                 [showMoreFilterValuesInline]=true
42
                 [filterValuesNum]=4
43
                 [lastIndex]=false 
44
                 [zenodoInformation]="zenodoInformation">
45
    </search-page>
46
    `
47
})
48
export class SearchDataprovidersToDepositComponent {
49
  private errorCodes: ErrorCodes;
50
  private errorMessages: ErrorMessagesComponent;
51
  @Input() piwikSiteId = null;
52
  piwiksub:any;
53
  @Input() customFilter:SearchCustomFilter= null;
54
  public results =[];
55
  public filters =[];
56
  public totalResults:number  = 0 ;
57
  public baseUrl:string;
58
  public searchUtils:SearchUtilsClass = new SearchUtilsClass();
59
  public sub: any; public subResults: any;
60
  public _location:Location;
61
  public searchFields:SearchFields = new SearchFields();
62
  public refineFields: string[] =  this.searchFields.DEPOSIT_DATASOURCE_REFINE_FIELDS;
63
  public fieldIdsMap= this.searchFields.DEPOSIT_DATASOURCE_FIELDS;
64
  public keywordFields = this.searchFields.DEPOSIT_DATASOURCE_KEYWORD_FIELDS;
65
  public csvParams: string;
66

    
67
  public disableForms: boolean = false;
68
  public loadPaging: boolean = true;
69
  public oldTotalResults: number = 0;
70
  pagingLimit = 0;
71

    
72
  properties:EnvProperties;
73

    
74
  @ViewChild (SearchPageComponent) searchPage : SearchPageComponent ;
75

    
76
  @Input() public zenodoInformation: ZenodoInformationClass = new ZenodoInformationClass();
77

    
78
  constructor (private route: ActivatedRoute, private _searchDataprovidersService: SearchDataprovidersService,
79
               private _piwikService:PiwikService) {
80
    this.errorCodes = new ErrorCodes();
81
    this.errorMessages = new ErrorMessagesComponent();
82
    this.searchUtils.status = this.errorCodes.LOADING;
83
    this.searchUtils.page =1;
84
  }
85

    
86
  public ngOnInit() {
87
    this.route.data
88
      .subscribe((data: { envSpecific: EnvProperties }) => {
89
        this.properties = data.envSpecific;
90
        this.baseUrl = "/search-deposit";//data.envSpecific.searchLinkToDataProviders;
91
        this.pagingLimit = data.envSpecific.pagingLimit;
92

    
93
        if(this.properties.enablePiwikTrack && (typeof document !== 'undefined')){
94
          this.piwiksub = this._piwikService.trackView(this.properties, "Deposit search page)", this.piwikSiteId).subscribe();
95
        }
96
      });
97
    this.searchPage.refineFields = this.refineFields;
98
    this.searchPage.fieldIdsMap = this.fieldIdsMap;
99
    this.searchPage.keywordFields = this.keywordFields;
100
    var firstLoad =true;
101

    
102
    this.sub =  this.route.queryParams.subscribe(params => {
103
      if(params['page'] && this.searchUtils.page != params['page']) {
104
        this.loadPaging = false;
105
        this.oldTotalResults = this.searchUtils.totalResults;
106
      }
107

    
108
      this.searchUtils.keyword = (params['keyword']?params['keyword']:'');
109
      var refine = true;
110
      if(this.searchUtils.page != ((params['page']=== undefined)?1:+params['page']) && this.filters && !firstLoad){
111
        refine = false;
112

    
113
      }
114
      firstLoad = false;
115
      this.searchUtils.page = (params['page']=== undefined)?1:+params['page'];
116
      this.searchUtils.size = (params['size']=== undefined)?5:+params['size'];
117
      if(this.searchUtils.size != 5 && this.searchUtils.size != 10 && this.searchUtils.size != 20 && this.searchUtils.size != 50) {
118
        this.searchUtils.size = 5;
119
      }
120
      this.searchPage.customFilter = this.customFilter;
121
      this.searchPage.usedBy = "deposit";
122
      var queryParameters = this.searchPage.getQueryParametersFromUrl(params);
123
      this._getResults(queryParameters, refine, this.searchUtils.page, this.searchUtils.size);
124
    });
125
  }
126

    
127
  public ngOnDestroy() {
128
    if(this.sub){
129
      this.sub.unsubscribe();
130
    }
131
    if(this.subResults){
132
      this.subResults.unsubscribe();
133
    }
134

    
135
    if(this.piwiksub){
136
      this.piwiksub.unsubscribe();
137
    }
138
  }
139

    
140
  public getResults(keyword:string,refine:boolean, page: number, size: number){
141
    var parameters = "";
142
    if(keyword.length > 0){
143
      //parameters = "q="+ keyword;
144

    
145
      if(this.keywordFields.length > 0) {
146
        parameters = "&fq=";
147
      }
148

    
149
      for(let i=0; i< this.keywordFields.length ; i++) {
150
        if(i > 0) {
151
          parameters += " or ";
152
        }
153
        let field = this.keywordFields[i];
154
        parameters += field.name+field.equalityOperator+StringUtils.URIEncode(this.searchUtils.keyword);
155
      }
156
    }
157
    this._getResults(parameters,refine,page, size);
158
  }
159
  private _getResults(parameters:string,refine:boolean, page: number, size: number){
160
    if(page > this.pagingLimit) {
161
      size=0;
162
    }
163
    if(page <= this.pagingLimit || this.searchUtils.status == this.errorCodes.LOADING) {
164
      this.csvParams = parameters;
165

    
166
      this.searchUtils.status = this.errorCodes.LOADING;
167

    
168
      this.disableForms = true;
169
      this.results = [];
170
      this.searchUtils.totalResults = 0;
171

    
172
      this.subResults = this._searchDataprovidersService.searchDataprovidersForDepositSearch(parameters,(refine)?this.searchPage.getRefineFieldsQuery():null, page, size, this.searchPage.getFields(),this.properties, "deposit").subscribe(
173
        data => {
174
          this.searchUtils.totalResults = data[0];
175
          this.results = data[1];
176
          if(refine){
177
            this.filters = data[2];
178
          }
179
          this.searchPage.checkSelectedFilters(this.filters);
180
          this.searchPage.updateBaseUrlWithParameters(this.filters);
181
          this.searchUtils.status = this.errorCodes.DONE;
182
          if(this.searchUtils.totalResults == 0 ){
183
            this.searchUtils.status = this.errorCodes.NONE;
184
          }
185
          this.disableForms = false;
186

    
187
          if(this.searchUtils.status == this.errorCodes.DONE) {
188
            // Page out of limit!!!
189
            let totalPages:any = this.searchUtils.totalResults/(this.searchUtils.size);
190
            if(!(Number.isInteger(totalPages))) {
191
              totalPages = (parseInt(totalPages, 10) + 1);
192
            }
193
            if(totalPages < page) {
194
              this.searchUtils.totalResults = 0;
195
              this.searchUtils.status = this.errorCodes.OUT_OF_BOUND;
196
            }
197
          }
198
        },
199
        err => {
200
          this.handleError("Error getting content providers", err);
201
          this.searchUtils.status = this.errorMessages.getErrorCode(err.status);
202

    
203
          this.disableForms = false;
204
        }
205
      );
206
    }
207
  }
208

    
209
  public queryChanged($event) {
210
    this.loadPaging = true;
211
  }
212

    
213
  private handleError(message: string, error) {
214
    console.error("Content Providers simple Search Page: "+message, error);
215
  }
216
}
(7-7/11)