Project

General

Profile

1
import {Component, ElementRef, EventEmitter, Input, Output} from '@angular/core';
2
import {SearchProjectsService} from '../../services/searchProjects.service';
3
import {ProjectService} from '../../landingPages/project/project.service';
4
import {ClaimEntity, ClaimProject} from './claimHelper.class';
5
import {EnvProperties} from '../../utils/properties/env-properties';
6
import {ErrorCodes} from '../../utils/properties/errorCodes';
7
import {StringUtils} from "../../utils/string-utils.class";
8
import {Filter} from "../../searchPages/searchUtils/searchHelperClasses.class";
9

    
10
declare var UIkit:any;
11

    
12
@Component({
13
    selector: 'claim-projects-search-form',
14

    
15
    templateUrl:'claimProjectSearchForm.component.html',
16

    
17
})
18
export class ClaimProjectsSearchFormComponent {
19
   ngOnInit() {
20
     // this.getFunders();
21
     // this.search(this.page, this.size);
22
   }
23
  //  @ViewChild (ModalLoading) loading : ModalLoading ;
24

    
25
  // @Input() public inline: boolean = false ; // for claimed started from landing pages
26
  public query = '';
27
  @Input() public selectedProjects:ClaimEntity[]=[] ;
28
  public elementRef;
29

    
30
  // public funders:string[];
31
  // public selectedFunderId:string ="0";
32
  // selectedFunderName:string ="Select funder:";
33
  @Output() projectSelected = new EventEmitter();
34
  @Input() public properties:EnvProperties;
35
  @Input() public inlineClaim:boolean=false;
36
  @Input()  localStoragePrefix:string = "";
37

    
38
  public errorCodes:ErrorCodes = new ErrorCodes();
39
  public projects:string[];
40
  public warningMessage = "";
41
  openaireResults:ClaimEntity[] = [];
42
  openaireResultsNum: number ;
43
  openaireResultsPage : number = 1;
44
  openaireResultsStatus:number = this.errorCodes.NONE;
45
  page : number = 1;
46
  size:number = 5;
47
  keyword:string = '';
48
  showResults = false;
49

    
50
  // public openaireRefineFields: string[] = ["relfunder",  "relproject", "resultbestaccessright", "instancetypename", "resultlanguagename", "community"];
51
  // public openaireRefineFieldsQuery: string = "&refine=true&fields=funder&fields=projectstartyear&fields=projectendyear&fields=projectecsc39";
52
  public refineFields: string[] = ["funder"];
53
  public refineFieldsQuery: string = "&refine=true&fields=funder";
54
  filters = [];
55
  prevFilters = [];
56
  startYear = "";
57
  endYear = "";
58
  constructor(private _service: ProjectService, private _projectService: SearchProjectsService, myElement: ElementRef) {
59
      this.elementRef = myElement;
60

    
61
  }
62

    
63

    
64
  search(page,size) {
65
    if(this.keyword.length == 0){
66
      this.showResults =false;
67
      return;
68
    }
69
    this.showResults =true;
70
    this.openaireResults = [];
71
    this.openaireResultsStatus = this.errorCodes.LOADING;
72
    this.prevFilters = this.filters;
73

    
74
    //searchProjects (params: string, refineParams:string, page: number, size: number, refineFields:string[] , properties:EnvProperties ):any {
75
    this._projectService.searchProjects(this.createOpenaireQueryParams(),(page==1)? this.refineFieldsQuery:null, page, size, (page==1)?this.refineFields:[], this.properties).subscribe(
76
      data => {
77
          if(data != null) {
78
              this.openaireResultsPage=page;
79
              this.openaireResultsNum = data[0];
80
              this.openaireResults =ClaimProjectsSearchFormComponent.openaire2ClaimEntity(data[1]);
81
              if(data[2] && data[2].length > 0){
82
                this.filters =  this.checkSelectedFilters( data[2], this.prevFilters);
83
              }
84

    
85
              this.openaireResultsStatus = this.errorCodes.DONE;
86
              if(this.openaireResultsNum == 0){
87
                this.openaireResultsStatus = this.errorCodes.NONE;
88
              }
89
          }else {
90
            this.openaireResultsStatus = this.errorCodes.ERROR;
91
          }
92
       },
93
      err => {
94
        this.openaireResultsStatus = this.errorCodes.ERROR;
95
        //console.log(err.status);
96
        ClaimProjectsSearchFormComponent.handleError("Error getting projects by keyword: "+this.keyword, err);
97
      }
98
    );
99
  }
100
// select(entity){
101
//   if(this.selectedProjects.length > 50){
102
//     UIkit.notification({
103
//         message : 'Your basket exceeds the number of allowed projects (50)',
104
//         status  : 'warning',
105
//         timeout : 1500,
106
//         pos     : 'top-center'
107
//     });
108
//     return;
109
//   }
110
//     this.query = "";
111
//     // this.searchTermStream.next(this.query); //clear
112
//     entity = entity.value;
113
//     // var project: ClaimProject = new ClaimProject();
114
//     // project.funderId = entity.funderId;
115
//     // project.funderName = entity.funderName;
116
//     // project.id = entity.id;
117
//     // project.projectName = entity.projectName;
118
//     // project.projectAcronym = entity.projectAcronym;
119
//     // project.startDate = entity.startDate;
120
//     // project.endDate = entity.endDate;
121
//     // project.code = entity.code;
122
//     // project.jurisdiction = entity.jurisdiction;
123
//     // project.fundingLevel0 = entity.fundingLevel0;
124
//
125
//
126
//     var index:number =this.selectedProjects.indexOf(entity);
127
//     var found:boolean = false;
128
//     this.warningMessage = "";
129
//
130
//     for (var _i = 0; _i < this.selectedProjects.length; _i++) {
131
//         let project = this.selectedProjects[_i];
132
//         if(entity.id == project.id){
133
//               found=true;
134
//               this.warningMessage = "Project already in your basket";
135
//         }
136
//      }
137
//
138
//     if (!found) {
139
//       this.selectedProjects.push(entity);
140
//       if(this.selectedProjects != null){
141
//         localStorage.setItem(this.localStoragePrefix + "projects", JSON.stringify(this.selectedProjects));
142
//       }
143
//       this.projectSelected.emit({
144
//         value: true
145
//       });
146
//
147
//     }
148
// }
149
/*  static showItem(item):string{
150
    return ((item.field[1]['@value'])?item.field[1]['@value']+" - ":"" ) + item.field[3]['@value'];
151
  }*/
152
  remove(item){
153
    let index:number =this.selectedProjects.indexOf(item);
154
     if (index > -1) {
155
        this.selectedProjects.splice(index, 1);
156
    }
157

    
158
  }
159
/*  handleClick(event){
160
   var clickedComponent = event.target;
161
   var inside = false;
162
   do {
163
       if (clickedComponent === this.elementRef.nativeElement) {
164
           inside = true;
165
       }
166
      clickedComponent = clickedComponent.parentNode;
167
   } while (clickedComponent);
168

    
169
  }*/
170
  static openaire2ClaimEntity(items){
171
    const projects: ClaimEntity[] = [];
172
    for(const item of items){
173
      const entity: ClaimEntity = new ClaimEntity();
174
      entity.project = new ClaimProject();
175
      entity.project.funderId = item.funderId;
176
      entity.project.funderName = item.funderShortname;
177
      entity.id = item.id;
178
      entity.title = item.title.name;
179
      entity.project.acronym = item.acronym;
180
      entity.project.startDate = item.startYear;
181
      entity.project.endDate = item.endYear;
182
      entity.project.code = item.code;
183
      entity.project.jurisdiction = item.jurisdiction;
184
      entity.project.fundingLevel0 = item.fundingLevel0;
185
      entity.type="project";
186
      projects.push(entity);
187
    }
188
    return projects;
189
  }
190
  private openaireResultsPageChange($event) {
191
     this.openaireResultsPage=$event.value;
192
     this.openaireResults = [];
193
     this.search(this.openaireResultsPage,this.size);
194
  }
195
  private static handleError(message: string, error) {
196
      console.error("Claim project search form (component): "+message, error);
197
  }
198

    
199

    
200
  createOpenaireQueryParams():string {
201
    let query = "";
202
    if(this.keyword.length > 0){
203
      query += "q=" + StringUtils.quote(StringUtils.URIEncode(this.keyword));
204
    }
205

    
206
    if(this.startYear.length > 0 ){
207
      query+='&fq=projectstartyear exact \"'+this.startYear+'\"'
208
    }
209
    if(this.endYear.length > 0 ){
210
      query+='&fq=projectendyear exact \"'+this.endYear+'\"'
211
    }
212
    let allFqs = "";
213
    for (let filter of this.filters){
214
      if(filter.countSelectedValues > 0){
215
        let count_selected=0;
216
        let fq = "";
217
        for (let value of filter.values){
218
          if(value.selected == true){
219
            count_selected++;
220
            fq+=(fq.length > 0 ? " " + filter.filterOperator + " ":"" ) + filter.filterId + " exact " + (StringUtils.quote(value.id));
221
          }
222
        }
223
        if(count_selected > 0){
224
          fq="&fq="+StringUtils.URIEncode(fq);
225
          allFqs += fq;
226
        }
227
      }
228
    }
229
    return query+allFqs;
230

    
231
  }
232
  public yearChanged() {
233
    this.search(this.page, this.size);
234

    
235
  }
236

    
237
  filterChanged($event) {
238
    this.search(this.page, this.size);
239

    
240
  }
241
  public checkSelectedFilters(filters:Filter[], prevFilters:Filter[]){
242
    for(let i=0; i< filters.length ; i++){
243
      let filter:Filter = filters[i];
244
      filter.countSelectedValues = 0;
245
      let prevFilterSelectedValues:string[] = [];
246
      for(let j=0; j< prevFilters.length ; j++){
247
        if(filters[i].filterId == prevFilters[j].filterId){
248
          if(prevFilters[j].countSelectedValues >0){
249
            for(let filterValue of prevFilters[j].values) {
250
              if(filterValue.selected){
251
                prevFilterSelectedValues.push(filterValue.id);
252
              }
253
            }
254

    
255
          }
256
          break;
257
        }
258
      }
259
      for(let filterValue of filter.values) {
260
        if(prevFilterSelectedValues.indexOf(filterValue.id) > -1) {
261
          filterValue.selected = true;
262
          filter.countSelectedValues++;
263
        }
264

    
265
      }
266

    
267
    }
268
    return filters;
269
  }
270
  totalPages(totalResults: number): number {
271
    let totalPages:any = totalResults/(this.size);
272
    if(!(Number.isInteger(totalPages))) {
273
      totalPages = (parseInt(totalPages, 10) + 1);
274
    }
275
    return totalPages;
276
  }
277
}
(7-7/15)