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 {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 {RouterHelper} from "../utils/routerHelper.class";
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="explorePanelBackground 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]="depositLearnHowPage"
24
                   [queryParams]="properties.environment!='development'?{}:routerHelper.createQueryParam('communityId',communityId)">
25
              Deposit
26
            </a></li>
27
            <li><span class="active uk-text-bold">Browse content providers</span></li>
28
          </ul>
29
        </div>
30
      </div>
31
    </div>
32
    <search-page pageTitle="Deposit Browse & Search repositories" [hasPrefix]=false
33
                 formPlaceholderText = "Search by title, country, organization, subject, type..."
34
                 type="content providers" entityType="dataprovider" [(filters)] = "filters"
35
                 [(results)] = "results"   [(searchUtils)] = "searchUtils" [baseUrl] = "baseUrl"
36
                 (queryChange)="queryChanged($event)"
37
                 [csvParams]="csvParams" csvPath="datasources"
38
                 [disableForms]="disableForms"
39
                 [loadPaging]="loadPaging"
40
                 [oldTotalResults]="oldTotalResults"
41
                 [piwikSiteId]=piwikSiteId
42
                 [searchFormClass]="''"
43
                 [usedBy]="'deposit'" 
44
                 [showMoreFilterValuesInline]=true
45
                 [filterValuesNum]=4
46
                 [lastIndex]=false 
47
                 [zenodoInformation]="zenodoInformation">
48
    </search-page>
49
    `
50
})
51
export class SearchDataprovidersToDepositComponent {
52
  private errorCodes: ErrorCodes;
53
  private errorMessages: ErrorMessagesComponent;
54
  @Input() piwikSiteId = null;
55
  public results =[];
56
  public filters =[];
57
  public totalResults:number  = 0 ;
58
  public baseUrl:string;
59
  public searchUtils:SearchUtilsClass = new SearchUtilsClass();
60
  public sub: any; public subResults: any;
61
  public _location:Location;
62
  public searchFields:SearchFields = new SearchFields();
63
  public refineFields: string[] =  this.searchFields.DEPOSIT_DATASOURCE_REFINE_FIELDS;
64
  public fieldIdsMap= this.searchFields.DEPOSIT_DATASOURCE_FIELDS;
65
  public keywordFields = this.searchFields.DEPOSIT_DATASOURCE_KEYWORD_FIELDS;
66
  public csvParams: string;
67

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

    
73
  properties:EnvProperties;
74

    
75
  @ViewChild (SearchPageComponent) searchPage : SearchPageComponent ;
76

    
77
  @Input() public communityId: string = null;
78
  @Input() public zenodoInformation: ZenodoInformationClass = new ZenodoInformationClass();
79

    
80
  depositLearnHowPage: string = null;
81
  public routerHelper:RouterHelper = new RouterHelper();
82

    
83
  constructor (private route: ActivatedRoute, private _searchDataprovidersService: SearchDataprovidersService) {
84
    this.errorCodes = new ErrorCodes();
85
    this.errorMessages = new ErrorMessagesComponent();
86
    this.searchUtils.status = this.errorCodes.LOADING;
87
    this.searchUtils.page =1;
88
  }
89

    
90
  public ngOnInit() {
91
    this.route.data
92
      .subscribe((data: { envSpecific: EnvProperties }) => {
93
        this.properties = data.envSpecific;
94
        this.depositLearnHowPage = this.properties.depositLearnHowPage;
95
        this.baseUrl = this.properties.depositSearchPage;
96
        this.pagingLimit = this.properties.pagingLimit;
97
      });
98
    this.searchPage.refineFields = this.refineFields;
99
    this.searchPage.fieldIdsMap = this.fieldIdsMap;
100
    this.searchPage.keywordFields = this.keywordFields;
101
    var firstLoad =true;
102

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

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

    
114
      }
115
      firstLoad = false;
116
      this.searchUtils.page = (params['page']=== undefined)?1:+params['page'];
117
      this.searchUtils.size = (params['size']=== undefined)?5:+params['size'];
118
      if(this.searchUtils.size != 5 && this.searchUtils.size != 10 && this.searchUtils.size != 20 && this.searchUtils.size != 50) {
119
        this.searchUtils.size = 5;
120
      }
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

    
136
  public getResults(keyword:string,refine:boolean, page: number, size: number){
137
    var parameters = "";
138
    if(keyword.length > 0){
139
      //parameters = "q="+ keyword;
140

    
141
      if(this.keywordFields.length > 0) {
142
        parameters = "&fq=";
143
      }
144

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

    
162
      this.searchUtils.status = this.errorCodes.LOADING;
163

    
164
      this.disableForms = true;
165
      this.results = [];
166
      this.searchUtils.totalResults = 0;
167

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

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

    
199
          this.disableForms = false;
200
        }
201
      );
202
    }
203
  }
204

    
205
  public queryChanged($event) {
206
    this.loadPaging = true;
207
  }
208

    
209
  private handleError(message: string, error) {
210
    console.error("Content Providers simple Search Page: "+message, error);
211
  }
212
}
(7-7/11)