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

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

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

    
64
  public disableForms: boolean = false;
65
  public loadPaging: boolean = true;
66
  public oldTotalResults: number = 0;
67
  pagingLimit = 0;
68

    
69
  properties:EnvProperties;
70

    
71
  @ViewChild (SearchPageComponent) searchPage : SearchPageComponent ;
72

    
73
  @Input() public zenodoInformation: ZenodoInformationClass = new ZenodoInformationClass();
74

    
75
  constructor (private route: ActivatedRoute, private _searchDataprovidersService: SearchDataprovidersService) {
76
    this.errorCodes = new ErrorCodes();
77
    this.errorMessages = new ErrorMessagesComponent();
78
    this.searchUtils.status = this.errorCodes.LOADING;
79
    this.searchUtils.page =1;
80
  }
81

    
82
  public ngOnInit() {
83
    this.route.data
84
      .subscribe((data: { envSpecific: EnvProperties }) => {
85
        this.properties = data.envSpecific;
86
        this.baseUrl = "/search-deposit";//data.envSpecific.searchLinkToDataProviders;
87
        this.pagingLimit = data.envSpecific.pagingLimit;
88
      });
89
    this.searchPage.refineFields = this.refineFields;
90
    this.searchPage.fieldIdsMap = this.fieldIdsMap;
91
    this.searchPage.keywordFields = this.keywordFields;
92
    var firstLoad =true;
93

    
94
    this.sub =  this.route.queryParams.subscribe(params => {
95
      if(params['page'] && this.searchUtils.page != params['page']) {
96
        this.loadPaging = false;
97
        this.oldTotalResults = this.searchUtils.totalResults;
98
      }
99

    
100
      this.searchUtils.keyword = (params['keyword']?params['keyword']:'');
101
      var refine = true;
102
      if(this.searchUtils.page != ((params['page']=== undefined)?1:+params['page']) && this.filters && !firstLoad){
103
        refine = false;
104

    
105
      }
106
      firstLoad = false;
107
      this.searchUtils.page = (params['page']=== undefined)?1:+params['page'];
108
      this.searchUtils.size = (params['size']=== undefined)?5:+params['size'];
109
      if(this.searchUtils.size != 5 && this.searchUtils.size != 10 && this.searchUtils.size != 20 && this.searchUtils.size != 50) {
110
        this.searchUtils.size = 5;
111
      }
112
      this.searchPage.customFilter = this.customFilter;
113
      this.searchPage.usedBy = "deposit";
114
      var queryParameters = this.searchPage.getQueryParametersFromUrl(params);
115
      this._getResults(queryParameters, refine, this.searchUtils.page, this.searchUtils.size);
116
    });
117
  }
118

    
119
  public ngOnDestroy() {
120
    if(this.sub){
121
      this.sub.unsubscribe();
122
    }
123
    if(this.subResults){
124
      this.subResults.unsubscribe();
125
    }
126
  }
127

    
128
  public getResults(keyword:string,refine:boolean, page: number, size: number){
129
    var parameters = "";
130
    if(keyword.length > 0){
131
      //parameters = "q="+ keyword;
132

    
133
      if(this.keywordFields.length > 0) {
134
        parameters = "&fq=";
135
      }
136

    
137
      for(let i=0; i< this.keywordFields.length ; i++) {
138
        if(i > 0) {
139
          parameters += " or ";
140
        }
141
        let field = this.keywordFields[i];
142
        parameters += field.name+field.equalityOperator+StringUtils.URIEncode(this.searchUtils.keyword);
143
      }
144
    }
145
    this._getResults(parameters,refine,page, size);
146
  }
147
  private _getResults(parameters:string,refine:boolean, page: number, size: number){
148
    if(page > this.pagingLimit) {
149
      size=0;
150
    }
151
    if(page <= this.pagingLimit || this.searchUtils.status == this.errorCodes.LOADING) {
152
      this.csvParams = parameters;
153

    
154
      this.searchUtils.status = this.errorCodes.LOADING;
155

    
156
      this.disableForms = true;
157
      this.results = [];
158
      this.searchUtils.totalResults = 0;
159

    
160
      this.subResults = this._searchDataprovidersService.searchDataprovidersForDepositSearch(parameters,(refine)?this.searchPage.getRefineFieldsQuery():null, page, size, this.searchPage.getFields(),this.properties, "deposit").subscribe(
161
        data => {
162
          this.searchUtils.totalResults = data[0];
163
          this.results = data[1];
164
          if(refine){
165
            this.filters = data[2];
166
          }
167
          this.searchPage.checkSelectedFilters(this.filters);
168
          this.searchPage.updateBaseUrlWithParameters(this.filters);
169
          this.searchUtils.status = this.errorCodes.DONE;
170
          if(this.searchUtils.totalResults == 0 ){
171
            this.searchUtils.status = this.errorCodes.NONE;
172
          }
173
          this.disableForms = false;
174

    
175
          if(this.searchUtils.status == this.errorCodes.DONE) {
176
            // Page out of limit!!!
177
            let totalPages:any = this.searchUtils.totalResults/(this.searchUtils.size);
178
            if(!(Number.isInteger(totalPages))) {
179
              totalPages = (parseInt(totalPages, 10) + 1);
180
            }
181
            if(totalPages < page) {
182
              this.searchUtils.totalResults = 0;
183
              this.searchUtils.status = this.errorCodes.OUT_OF_BOUND;
184
            }
185
          }
186
        },
187
        err => {
188
          this.handleError("Error getting content providers", err);
189
          this.searchUtils.status = this.errorMessages.getErrorCode(err.status);
190

    
191
          this.disableForms = false;
192
        }
193
      );
194
    }
195
  }
196

    
197
  public queryChanged($event) {
198
    this.loadPaging = true;
199
  }
200

    
201
  private handleError(message: string, error) {
202
    console.error("Content Providers simple Search Page: "+message, error);
203
  }
204
}
(7-7/11)