Project

General

Profile

1
import {Component, Input, ViewChild} from '@angular/core';
2
import {Observable}       from 'rxjs/Observable';
3
import { Router, ActivatedRoute} from '@angular/router';
4
import {Filter, Value,AdvancedField} from '../searchUtils/searchHelperClasses.class';
5
import {SearchDataprovidersService} from '../../services/searchDataproviders.service';
6
import {SearchResult}     from '../../utils/entities/searchResult';
7
import {OpenaireProperties, ErrorCodes} from '../../utils/properties/openaireProperties';
8
import {AdvancedSearchPageComponent} from '../searchUtils/advancedSearchPage.component';
9
import {SearchFields, FieldDetails} from '../../utils/properties/searchFields';
10
import {SearchUtilsClass } from '../searchUtils/searchUtils.class';
11

    
12

    
13

    
14
@Component({
15
    selector: 'advanced-search-dataprovider',
16
    template: `
17
    <advanced-search-page pageTitle="Advanced Search for Content Providers" entityType="dataprovider"
18
                 type = "content providers"
19
                 [(results)] = "results"
20
                 [(fieldIds)]="fieldIds" [(fieldIdsMap)]="fieldIdsMap"   [(selectedFields)]="selectedFields"
21
                  [(searchUtils)] = "searchUtils"
22
                 (queryChange)="queryChanged($event)"
23
                 [csvParams]="csvParams" csvPath="resources" simpleSearchLink="/search/find/dataproviders"
24
                 [disableForms]="disableForms"
25
                 [loadPaging]="loadPaging"
26
                 [oldTotalResults]="oldTotalResults">
27
    </advanced-search-page>
28

    
29
    `
30
 })
31

    
32
export class AdvancedSearchDataProvidersComponent {
33
  private errorCodes: ErrorCodes;
34

    
35
  public results =[];
36
  public filters =[];
37
  public searchUtils:SearchUtilsClass = new SearchUtilsClass();
38
  public searchFields:SearchFields = new SearchFields();
39

    
40
  public fieldIds:  string[] = this.searchFields.DATASOURCE_ADVANCED_FIELDS;
41
  public fieldIdsMap= this.searchFields.DATASOURCE_FIELDS;
42
  public selectedFields:AdvancedField[] =  [];
43

    
44
  @ViewChild (AdvancedSearchPageComponent) searchPage : AdvancedSearchPageComponent ;
45
  public resourcesQuery = "(oaftype exact datasource)";
46
  public csvParams: string;
47
  public disableForms: boolean = false;
48
  public loadPaging: boolean = true;
49
  public oldTotalResults: number = 0;
50

    
51
  constructor (private route: ActivatedRoute, private _searchDataProvidersService: SearchDataprovidersService ) {
52
    this.results =[];
53
    this.errorCodes = new ErrorCodes();
54
    this.searchUtils.status = this.errorCodes.LOADING;
55
    this.searchUtils.baseUrl = OpenaireProperties.searchLinkToAdvancedDataProviders;
56
    console.info("Con -base url:"+this.searchUtils.baseUrl );
57

    
58
  }
59
  ngOnInit() {
60
   this.sub =  this.route.queryParams.subscribe(params => {
61
     if(params['page'] && this.searchUtils.page != params['page']) {
62
       this.loadPaging = false;
63
       this.oldTotalResults = this.searchUtils.totalResults;
64
     }
65

    
66
    let page = (params['page']=== undefined)?0:+params['page'];
67
    this.searchUtils.page = ( page < 1 ) ? 1 : page;
68
    this.searchPage.fieldIds = this.fieldIds;
69
    this.selectedFields =[];
70
    this.searchPage.selectedFields = this.selectedFields;
71
    this.searchPage.fieldIdsMap = this.fieldIdsMap;
72
    this.searchPage.getSelectedFiltersFromUrl(params);
73
    this.getResults(this.searchPage.createQueryParameters(),  this.searchUtils.page, this.searchUtils.size);
74
    });
75
  }
76
  ngOnDestroy() {
77
    this.sub.unsubscribe();
78
  }
79
  sub: any;
80
  public getResults(parameters:string, page: number, size: number){
81
      if(parameters!= null && parameters != ''  ) {
82
        this.csvParams ="&type=datasources&query=( "+this.resourcesQuery + "and (" + parameters + "))";
83
      }else{
84
        this.csvParams ="&type=datasources&query="+this.resourcesQuery;
85
      }
86

    
87
    //var errorCodes:ErrorCodes = new ErrorCodes();
88
    this.searchUtils.status = this.errorCodes.LOADING;
89
    //this.searchPage.openLoading();
90
    this.disableForms = true;
91
    this.results = [];
92
    this.searchUtils.totalResults = 0;
93

    
94
    console.info("Advanced Search for Content Providers: Execute search query "+parameters);
95
     this._searchDataProvidersService.advancedSearchDataproviders(parameters,  page, size).subscribe(
96
        data => {
97
            this.searchUtils.totalResults = data[0];
98
            console.info("Adv Search Content Providers total="+this.searchUtils.totalResults);
99
            this.results = data[1];
100
            this.searchPage.updateBaseUrlWithParameters();
101
            //var errorCodes:ErrorCodes = new ErrorCodes();
102
            this.searchUtils.status = this.errorCodes.DONE;
103
            if(this.searchUtils.totalResults == 0 ){
104
              this.searchUtils.status = this.errorCodes.NONE;
105
            }
106
            //this.searchPage.closeLoading();
107
            this.disableForms = false;
108

    
109
            if(this.searchUtils.status == this.errorCodes.DONE) {
110
              // Page out of limit
111
              let totalPages:any = this.searchUtils.totalResults/(this.searchUtils.size);
112
              if(!(Number.isInteger(totalPages))) {
113
                  totalPages = (parseInt(totalPages, 10) + 1);
114
              }
115
              if(totalPages < page) {
116
                this.searchUtils.totalResults = 0;
117
                this.searchUtils.status = this.errorCodes.OUT_OF_BOUND;
118
              }
119
            }
120
        },
121
        err => {
122
            console.log(err);
123
            console.info("error");
124
            //TODO check erros (service not available, bad request)
125
            // if( ){
126
            //   this.searchUtils.status = errorCodes.ERROR;
127
            // }
128
            //var errorCodes:ErrorCodes = new ErrorCodes();
129
            //this.searchUtils.status = errorCodes.NOT_AVAILABLE;
130
            if(err.status == '404') {
131
              this.searchUtils.status = this.errorCodes.NOT_FOUND;
132
            } else if(err.status == '500') {
133
              this.searchUtils.status = this.errorCodes.ERROR;
134
            } else {
135
              this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
136
            }
137
            //this.searchPage.closeLoading();
138
            this.disableForms = false;
139
        }
140
    );
141
  }
142

    
143
  public queryChanged($event) {
144
    this.loadPaging = true;
145
    
146
    var parameters = $event.value;
147
    this.getResults(parameters, this.searchUtils.page,this.searchUtils.size);
148
    console.info("queryChanged: Execute search query "+parameters);
149

    
150
  }
151

    
152

    
153
}
(1-1/12)