Project

General

Profile

1
import {Component, ViewChild} from "@angular/core";
2
import {SearchUtilsClass} from "../../openaireLibrary/searchPages/searchUtils/searchUtils.class";
3
import {ErrorMessagesComponent} from "../../openaireLibrary/utils/errorMessages.component";
4
import {ErrorCodes} from "../../openaireLibrary/utils/properties/errorCodes";
5
import {EnvProperties} from "../../openaireLibrary/utils/properties/env-properties";
6
import {ActivatedRoute} from "@angular/router";
7
import {AdvancedField, Filter, Value} from "../../openaireLibrary/searchPages/searchUtils/searchHelperClasses.class";
8
import {SearchFields} from "../../openaireLibrary/utils/properties/searchFields";
9
import {CommunitiesService} from "../../openaireLibrary/connect/communities/communities.service";
10
import {Session, User} from "../../openaireLibrary/login/utils/helper.class";
11
import {CommunityInfo} from "../../openaireLibrary/connect/community/communityInfo";
12
import {StringUtils} from "../../openaireLibrary/utils/string-utils.class";
13
import {HelperFunctions} from "../../openaireLibrary/utils/HelperFunctions.class";
14
import {UserManagementService} from "../../openaireLibrary/services/user-management.service";
15
import {Breadcrumb} from "../../openaireLibrary/utils/breadcrumbs/breadcrumbs.component";
16
import {NewSearchPageComponent} from "../../openaireLibrary/searchPages/searchUtils/newSearchPage.component";
17
import {properties} from "../../../environments/environment";
18
import {Subscriber} from "rxjs";
19

    
20
@Component({
21
  selector: 'search-communities',
22
  template: `
23
    <new-search-page pageTitle="OpenAIRE-Connect | Search Communities"
24
                     [hasPrefix]=false [piwikSiteId]="piwikSiteId"
25
                     searchFormClass="communitiesSearchForm"
26
                     [formPlaceholderText]="'Search for Research Gateways…'"
27
                     type="communities" entityType="community"
28
                     [results]="results" [searchUtils]="searchUtils"
29
                     [showResultCount]=true
30
                     [disableForms]="disableForms"
31
                     [showIndexInfo]=false
32
                     [simpleView]="true"
33
                     [fieldIds]="fieldIds" [fieldIdsMap]="fieldIdsMap" [selectedFields]="selectedFields"
34
                     [simpleSearchLink]="searchLink" [entitiesSelection]="false" [showBreadcrumb]="true">
35
    </new-search-page>
36
  `
37
})
38
export class SearchCommunitiesComponent {
39
  public piwikSiteId = null;
40
  private errorCodes: ErrorCodes;
41
  private errorMessages: ErrorMessagesComponent;
42
  public results: CommunityInfo[] = [];
43
  public totalResults: CommunityInfo[] = [];
44
  public subscriptions = [];
45
  public filters = [];
46
  public searchFields: SearchFields = new SearchFields();
47
  public searchUtils: SearchUtilsClass = new SearchUtilsClass();
48
  public selectedFields: AdvancedField[] = [];
49
  public disableForms: boolean = false;
50
  public baseUrl: string = null;
51
  public fieldIds: string[] = ["q"];
52
  public refineFields: string[] = this.searchFields.COMMUNITIES_SEARCH_FIELDS;
53
  public fieldIdsMap = {
54
    ["q"]: {
55
      name: "All fields",
56
      type: "keyword",
57
      param: "q",
58
      operator: "op",
59
      equalityOperator: "=",
60
      filterType: null
61
    }
62
  };
63
  public keyword = "";
64
  public searchLink;
65
  public showType = false;
66
  public breadcrumbs: Breadcrumb[] = [{name: 'home', route: '/'}, {name: 'communities'}];
67
  properties: EnvProperties;
68
  @ViewChild(NewSearchPageComponent) searchPage: NewSearchPageComponent;
69
  private user: User;
70
  private userFilterLoaded: boolean = false;
71
  
72
  constructor(private route: ActivatedRoute,
73
              private _communitiesService: CommunitiesService,
74
              private userManagementService: UserManagementService) {
75
    this.errorCodes = new ErrorCodes();
76
    this.errorMessages = new ErrorMessagesComponent();
77
    this.searchUtils.status = this.errorCodes.LOADING;
78
  }
79
  
80
  public ngOnInit() {
81
    var firstLoad = true;
82
    this.properties = properties;
83
    this.piwikSiteId = this.properties.piwikSiteId;
84
    this.baseUrl = this.properties.searchLinkToCommunities;
85
    
86
    this.subscriptions.push(this.route.queryParams.subscribe(params => {
87
      this.searchPage.resultsPerPage = 10;
88
      this.keyword = (params['fv0'] ? params['fv0'] : '');
89
      this.keyword = StringUtils.URIDecode(this.keyword);
90
      this.searchUtils.page = (params['page'] === undefined) ? 1 : +params['page'];
91
      this.searchUtils.sortBy = (params['sortBy'] === undefined) ? '' : params['sortBy'];
92
      this.searchUtils.size = (params['size'] === undefined) ? this.searchPage.resultsPerPage : +params['size'];
93
      this.searchUtils.baseUrl = this.baseUrl;
94
      this.searchPage.searchUtils = this.searchUtils;
95
      if (this.searchUtils.size != 5 && this.searchUtils.size != 10 && this.searchUtils.size != 20 && this.searchUtils.size != 50) {
96
        this.searchUtils.size = this.searchPage.resultsPerPage;
97
      }
98
      if (this.searchUtils.sortBy && this.searchUtils.sortBy != "creationdate,descending" && this.searchUtils.sortBy != "creationdate,ascending") {
99
        this.searchUtils.sortBy = "";
100
      }
101
      this.searchPage.refineFields = this.refineFields;
102
      this.searchLink = this.properties.searchLinkToCommunities;
103
      this.selectedFields = [];
104
      this.searchPage.prepareSearchPage(this.fieldIds, this.selectedFields, this.refineFields, [], this.fieldIdsMap, null, params, "community", null);
105
      
106
      let queryParams = params;//this.searchPage.getQueryParamsFromUrl(params);
107
      if (typeof document !== 'undefined') {
108
        this.subscriptions.push(this.userManagementService.getUserInfo().subscribe(user => {
109
          this.user = user;
110
          this.initCommunities(queryParams);
111
        }));
112
      } else {
113
        this.initCommunities(queryParams);
114
      }
115
    }));
116
  }
117
  
118
  ngOnDestroy() {
119
    this.subscriptions.forEach(subscription => {
120
      if (subscription instanceof Subscriber) {
121
        subscription.unsubscribe();
122
      }
123
    });
124
  }
125
  
126
  /**
127
   * Initialize communities from Communities APIs
128
   *
129
   * @param params
130
   */
131
  private initCommunities(params) {
132
    this.subscriptions.push(this._communitiesService.getCommunitiesState().subscribe(
133
      data => {
134
        if (!data) {
135
          return;
136
        }
137
        for (let i = 0; i < data.length; i++) {
138
          this.totalResults[i] = data[i];
139
          this.totalResults[i].isManager = false;
140
          this.totalResults[i].isSubscribed = false;
141
          if (this.user) {
142
            this.totalResults[i].isManager = this.isCommunityManager(this.totalResults[i]);
143
            this.totalResults[i].isSubscribed = Session.isSubscribedTo('community', this.totalResults[i].communityId, this.user);
144
          }
145
        }
146
        if (this.user) {
147
          this._getResults(params);
148
        } else {
149
          this._getResults(params);
150
        }
151
      },
152
      err => {
153
        this.handleError('Error getting communities', err);
154
        this.searchUtils.status = this.errorMessages.getErrorCode(err.status);
155
        this.disableForms = false;
156
      }
157
    ));
158
  }
159
  
160
  
161
  /**
162
   * Get all communities from Communities API and apply permission access validator,
163
   * keyword searching, filter, paging and sorting.
164
   *
165
   * @param params, status
166
   * @private
167
   */
168
  private _getResults(params) {
169
    this.searchUtils.status = this.errorCodes.LOADING;
170
    this.disableForms = true;
171
    this.results = this.totalResults;
172
    this.filters = this.createFilters();
173
    this.searchUtils.totalResults = 0;
174
    this.applyParams(params);
175
  }
176
  
177
  /**
178
   * Return the communities in which user has permission to view or manage.
179
   */
180
  private showCommunities() {
181
    let ret: CommunityInfo[] = [];
182
    for (let result of this.results) {
183
      if (result.status == 'hidden') {
184
        continue;
185
      } else if (result.status == "manager") {
186
        let mail = (this.user) ? this.user.email : null;
187
        if (mail == null) { // no user
188
          continue;
189
        } else if (Session.isCommunityCurator(this.user) || Session.isPortalAdministrator(this.user)) {
190
          ret.push(result);
191
        } else if (result.managers.indexOf(mail) != -1) {
192
          ret.push(result);
193
        }
194
        continue;
195
      }
196
      ret.push(result);
197
    }
198
    this.results = ret;
199
  }
200
  
201
  /**
202
   * Apply permission access validator,
203
   * keyword searching, filter, paging and sorting.
204
   *
205
   * @param params
206
   * @param status
207
   */
208
  public applyParams(params: Map<string, string>) {
209
    this.showCommunities();
210
    if (this.keyword && this.keyword != '') {
211
      this.searchForKeywords();
212
    }
213
    this.checkFilters(params);
214
    this.sort();
215
    this.searchUtils.totalResults = this.results.length;
216
    this.filters = this.searchPage.prepareFiltersToShow(this.filters, this.searchUtils.totalResults);
217
    this.results = this.results.slice((this.searchUtils.page - 1) * this.searchUtils.size, (this.searchUtils.page * this.searchUtils.size));
218
    this.searchUtils.status = this.errorCodes.DONE;
219
    if (this.searchUtils.totalResults == 0) {
220
      this.searchUtils.status = this.errorCodes.NONE;
221
    }
222
    this.disableForms = false;
223
    if (this.searchUtils.status == this.errorCodes.DONE) {
224
      // Page out of limit!!!
225
      let totalPages: any = this.searchUtils.totalResults / (this.searchUtils.size);
226
      if (!(Number.isInteger(totalPages))) {
227
        totalPages = (parseInt(totalPages, 10) + 1);
228
      }
229
      if (totalPages < this.searchUtils.page) {
230
        this.searchUtils.totalResults = 0;
231
        this.searchUtils.status = this.errorCodes.OUT_OF_BOUND;
232
      }
233
    }
234
  }
235
  
236
  
237
  /**
238
   * Parse the given keywords into array and check if any of the requirements field of a community includes
239
   * one of the given words.
240
   */
241
  private searchForKeywords() {
242
    let ret: CommunityInfo[] = [];
243
    let keywords: string[] = this.keyword.split(/,(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)/, -1);
244
    for (let i = 0; i < this.results.length; i++) {
245
      for (let keyword of keywords) {
246
        keyword = keyword.toLowerCase();
247
        if (keyword != '' && (StringUtils.containsWord(this.results[i].title, keyword) || StringUtils.containsWord(this.results[i].shortTitle, keyword) ||
248
          StringUtils.containsWord(this.results[i].communityId, keyword) || StringUtils.containsWord(this.results[i].description, keyword))) {
249
          ret.push(this.results[i]);
250
          break;
251
        }
252
      }
253
    }
254
    this.results = ret;
255
  }
256
  
257
  /**
258
   * Check the current results if they satisfy the values of each filter category and
259
   * update the number of possible results in each value.
260
   *
261
   * @param params
262
   */
263
  private checkFilters(params) {
264
    let typeResults: CommunityInfo[] = this.applyFilter('type', params);
265
    let statusResults: CommunityInfo[] = this.results;
266
    let roleResults: CommunityInfo[] = this.results;
267
    if (this.user) {
268
      statusResults = this.applyFilter('status', params);
269
      roleResults = this.applyFilter('role', params);
270
      this.resetFilterNumbers('status');
271
      this.updateFilterNumbers(typeResults.filter(value => {
272
        return roleResults.includes(value);
273
      }), 'status');
274
      this.resetFilterNumbers('role');
275
      this.updateFilterNumbers(statusResults.filter(value => {
276
        return typeResults.includes(value);
277
      }), 'role');
278
    }
279
    this.resetFilterNumbers('type');
280
    this.updateFilterNumbers(statusResults.filter(value => {
281
      return roleResults.includes(value);
282
    }), 'type');
283
    this.results = statusResults.filter(value => {
284
      return typeResults.includes(value);
285
    })
286
    this.results = this.results.filter(value => {
287
      return roleResults.includes(value);
288
    });
289
  }
290
  
291
  /**
292
   * Apply filter with filterId and return the results
293
   *
294
   * @param filterId
295
   * @param params
296
   */
297
  private applyFilter(filterId: string, params): CommunityInfo[] {
298
    let results: CommunityInfo[] = [];
299
    let values: string[] = [];
300
    if (params[filterId]) {
301
      values = (StringUtils.URIDecode(params[filterId])).split(/,(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)/, -1);
302
    }
303
    if (filterId == 'type') {
304
      for (let i = 0; i < this.results.length; i++) {
305
        if (values.length == 0) {
306
          results.push(this.results[i]);
307
        } else {
308
          for (let value of values) {
309
            if (this.results[i].type == value.replace(/["']/g, "")) {
310
              results.push(this.results[i]);
311
              break;
312
            }
313
          }
314
        }
315
      }
316
    } else if (filterId == 'status') {
317
      for (let i = 0; i < this.results.length; i++) {
318
        if (values.length == 0) {
319
          results.push(this.results[i]);
320
        } else {
321
          for (let value of values) {
322
            if (value.replace(/["']/g, "") == 'subscribed') {
323
              if (this.results[i].isSubscribed) {
324
                results.push(this.results[i]);
325
                break;
326
              }
327
            } else if (value.replace(/["']/g, "") == 'nonsubscribed') {
328
              if (!this.results[i].isSubscribed) {
329
                results.push(this.results[i]);
330
                break;
331
              }
332
            }
333
          }
334
        }
335
      }
336
    } else if (filterId == 'role') {
337
      for (let i = 0; i < this.results.length; i++) {
338
        if (values.length == 0) {
339
          results.push(this.results[i]);
340
        } else {
341
          for (let value of values) {
342
            if (value.replace(/["']/g, "") == 'manager') {
343
              if (this.results[i].isManager) {
344
                results.push(this.results[i]);
345
                break;
346
              }
347
            }
348
          }
349
        }
350
      }
351
    }
352
    return results;
353
  }
354
  
355
  /**
356
   * Reset the values of filter with id filterId with zero.
357
   *
358
   * @param filterId
359
   */
360
  private resetFilterNumbers(filterId: string) {
361
    for (let i = 0; i < this.filters.length; i++) {
362
      if (this.filters[i].filterId == filterId) {
363
        for (let j = 0; j < this.filters[i].values.length; j++) {
364
          this.filters[i].values[j].number = 0;
365
        }
366
        break;
367
      }
368
    }
369
  }
370
  
371
  /**
372
   * Update the values of filter with id filterId based on
373
   * results.
374
   *
375
   * @param results
376
   * @param filterId
377
   */
378
  private updateFilterNumbers(results: CommunityInfo[], filterId: string) {
379
    for (let k = 0; k < results.length; k++) {
380
      for (let i = 0; i < this.filters.length; i++) {
381
        if (this.filters[i].filterId == filterId) {
382
          if (this.filters[i].filterId == 'type') {
383
            for (let j = 0; j < this.filters[i].values.length; j++) {
384
              if (results[k].type == this.filters[i].values[j].id) {
385
                this.filters[i].values[j].number++;
386
              }
387
            }
388
          } else if (this.filters[i].filterId == 'status') {
389
            if (results[k].isSubscribed) {
390
              this.filters[i].values[0].number++;
391
            } else {
392
              this.filters[i].values[1].number++;
393
            }
394
          } else if (this.filters[i].filterId == 'role') {
395
            if (results[k].isManager) {
396
              this.filters[i].values[0].number++;
397
            }
398
          }
399
          break;
400
        }
401
      }
402
    }
403
  }
404
  
405
  /**
406
   * Sorting results based on sortBy.
407
   */
408
  private sort() {
409
    if (this.searchUtils.sortBy == '') {
410
      this.results.sort((left, right): number => {
411
        if (left.title > right.title) {
412
          return 1;
413
        } else if (left.title < right.title) {
414
          return -1;
415
        } else {
416
          return 0;
417
        }
418
      })
419
    } else if (this.searchUtils.sortBy == 'creationdate,descending') {
420
      this.results.sort((left, right): number => {
421
        if (!right.date || left.date > right.date) {
422
          return -1;
423
        } else if (!left.date || left.date < right.date) {
424
          return 1;
425
        } else {
426
          return 0;
427
        }
428
      })
429
    } else if (this.searchUtils.sortBy == 'creationdate,ascending') {
430
      this.results.sort((left, right): number => {
431
        if (!right.date || left.date > right.date) {
432
          return 1;
433
        } else if (!left.date || left.date < right.date) {
434
          return -1;
435
        } else {
436
          return 0;
437
        }
438
      })
439
    }
440
  }
441
  
442
  
443
  private isCommunityManager(community: CommunityInfo): boolean {
444
    return Session.isPortalAdministrator(this.user) || Session.isCommunityCurator(this.user) || Session.isManager('community', community.communityId, this.user);
445
  }
446
  
447
  /**
448
   * Create Search Communities filters.
449
   *
450
   */
451
  private createFilters(): Filter[] {
452
    let filter_names = [];
453
    let filter_ids = [];
454
    let searchFields = new SearchFields();
455
    let filter_original_ids = searchFields.COMMUNITIES_SEARCH_FIELDS;
456
    let value_names = [];
457
    let value_original_ids = [];
458
    this.showType = this.results.filter(community => community.type === 'ri').length > 0;
459
    if (this.showType) {
460
      filter_names.push("Type");
461
      filter_ids.push("type");
462
      value_names.push(["Research Communities", "Research Initiatives"]);
463
      value_original_ids.push(["community", "ri"]);
464
    } else {
465
      filter_original_ids = searchFields.COMMUNITIES_SEARCH_FIELDS.splice(0, 1);
466
    }
467
    if (this.user) {
468
      filter_names.push("Status");
469
      filter_ids.push("status");
470
      value_names.push(["Subscribed", "Non-subscribed"]);
471
      value_original_ids.push(["subscribed", "nonsubscribed"]);
472
      filter_names.push("Role");
473
      filter_ids.push("role");
474
      value_names.push(["Manager"]);
475
      value_original_ids.push(["manager"]);
476
      this.userFilterLoaded = true;
477
    }
478
    let filters: Filter[] = [];
479
    for (let i = 0; i < filter_names.length; i++) {
480
      let values: Value[] = [];
481
      for (let j = 0; j < value_names[i].length; j++) {
482
        let value: Value = {name: value_names[i][j], id: value_original_ids[i][j], number: 0, selected: false};
483
        values.push(value);
484
      }
485
      let filter: Filter = {
486
        title: filter_names[i],
487
        filterId: filter_ids[i],
488
        originalFilterId: filter_original_ids[i],
489
        values: values,
490
        countSelectedValues: 0,
491
        "filterOperator": 'or',
492
        valueIsExact: true,
493
        filterType: "checkbox"
494
      };
495
      filters.push(filter);
496
    }
497
    return filters;
498
  }
499
  
500
  private handleError(message: string, error) {
501
    console.error('Communities Search Page: ' + message, error);
502
  }
503
  
504
}
(2-2/3)