Project

General

Profile

1 61381 k.triantaf
import {Injectable, OnDestroy} from '@angular/core';
2
import {HttpClient} from "@angular/common/http";
3
import {BehaviorSubject, from, Observable, of, Subscription} from 'rxjs';
4
import {map} from 'rxjs/operators';
5
import {EnvProperties} from "../properties/env-properties";
6
import {Portal} from "../entities/adminTool/portal";
7
import {Page} from "../entities/adminTool/page";
8
9
@Injectable({  providedIn: 'root' })
10
export class ConfigurationService{
11
  private communityInformation: BehaviorSubject<Portal> = new BehaviorSubject(null);
12
  private sub: Subscription = null;
13
  private source: Observable<Portal> = null;
14
  private promise: Promise<boolean> = null;
15
16
   constructor(private http: HttpClient ) {}
17
18
   ngOnDestroy() {
19
      this.clearSubscriptions();
20
   }
21
22
  clearSubscriptions() {
23
     if(this.sub) {
24
       this.sub.unsubscribe();
25
     }
26
   }
27
28
  /**
29
   * @deprecated
30
   */
31
   getCommunityInformation(properties:EnvProperties, community:string){
32
     let url = properties.adminToolsAPIURL +"/"+properties.adminToolsPortalType+"/" + community + '/full';
33
     return this.http.get((properties.useLongCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url);
34
               //.map(res => res.json());
35
   }
36
37
  public initStaticCommunityInformation(communityInformation: Portal) {
38
    this.promise = new Promise<any>((resolve => {
39
            this.communityInformation.next(communityInformation);
40
            resolve();
41
    }));
42
  }
43
  public initCommunityInformation(properties:EnvProperties, community:string) {
44
    if(community == null) return;
45
46
    let url = properties.adminToolsAPIURL +"/" + properties.adminToolsPortalType + "/" + community + "/full";
47
    this.promise = new Promise<any>((resolve => {
48
      this.source = this.http.get<Portal>((properties.useLongCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url);
49
50
        this.sub = this.source
51
          .subscribe(
52
          (communityInformation: Portal) => {
53
            this.communityInformation.next(communityInformation);
54
            resolve();
55
          },
56
          error => {
57
            this.communityInformation.error(error);
58
            resolve();
59
          });
60
    }));
61
62
  }
63
64
  public get communityInformationState(): Observable<Portal> {
65
    return this.communityInformation.asObservable();
66
  }
67
68
  /**
69
   * @deprecated
70
   */
71
   isEntityEnabled(APIUrl:string, community:string,entity: string){
72
     //console.log("isEntityEnabled:  "+entity);
73
     let url = "isEntityEnabled-"+entity;
74
75
    //  if(entity == "publication" || entity == "dataset" || entity == "datasource"){
76
    //     return Observable.of(new Object()).mapTo(false);
77
    //  }
78
    //  return Observable.of(new Object()).mapTo(true);
79
     return this.http.get(APIUrl + "/page")
80
               .pipe(map(res => true));
81
   }
82
83
  /**
84
   * @deprecated
85
   */
86
   isPageEnabled(properties:EnvProperties, community:string,router: string){
87
     let page_route: string = router.split('?')[0].substring(1);
88
89
     let url = properties.adminToolsAPIURL + "/"+properties.adminToolsPortalType+"/" + community+"/pages?page_route="+page_route;
90
      return this.http.get((properties.useLongCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url)
91
              //.map(res => res.json())
92
              .pipe(map(res => {
93
                let result = false;
94
                if(res['length'] >0 && res[0].route == page_route){
95
                  result =  res[0].isEnabled;
96
                }
97
98
                return result;
99
              }));//.do(res => {console.log("Route "+page_route +" is "+res)});
100
   }
101
102
  async isPageEnabledByStateAsync(properties: EnvProperties, community:string, page_route: string) {
103
     if(!this.promise) {
104
       this.initCommunityInformation(properties, community);
105
     }
106
107
     await this.promise;
108
     if(this.sub){
109
       this.sub.unsubscribe();
110
     }
111
     return this.filtering(page_route);
112
  }
113
114
  isPageEnabledByState(properties: EnvProperties, community:string, router: string): Observable<boolean> {
115
     let page_route: string = router.split('?')[0].substring(1);
116
     return from(this.isPageEnabledByStateAsync(properties, community, page_route));
117
  }
118
119
  filtering(page_route: string) {
120
      let community: Portal = this.communityInformation.getValue();
121
      let pages: Page[] = <Page[]>community.pages;
122
      if (pages) {
123
        let page = pages.find((page: Page) => page.route == page_route);
124
        return page && page.isEnabled;
125
      } else {
126
        return false;
127
      }
128
  }
129
130
  /**
131
   * @deprecated
132
   */
133
  getMainPageContent(APIUrl:string, community:string,){
134
     return this.http.get(APIUrl + "/page")
135
               .pipe(map(res => true));
136
   }
137
138
  /**
139
   * @deprecated
140
   */
141
  getSpecialAnouncementContent(APIUrl:string, community:string,){
142
     return this.http.get(APIUrl + "/page")
143
               .pipe(map(res => ""));
144
   }
145
146
  /**
147
   * @deprecated
148
   */
149
  getHelpPageContent(APIUrl:string, community:string, router:string){
150
     return  this.http.get(APIUrl + "/page")
151
               .pipe(map(res => true));
152
   }
153
    // private handleError (error: Response) {
154
    // // in a real world app, we may send the error to some remote logging infrastructure
155
    // // instead of just logging it to the console
156
    //     console.log(error);
157
    //     return this.http.get(this.APIUrl + "/page")
158
    //               .map(res => true);
159
    // }
160
161
162
}