Project

General

Profile

1 59816 argiro.kok
import {Injectable, OnDestroy} from '@angular/core';
2 55964 argiro.kok
import {HttpClient} from "@angular/common/http";
3 59966 argiro.kok
import {BehaviorSubject, from, Observable, of, Subscription} from 'rxjs';
4 59816 argiro.kok
import {map} from 'rxjs/operators';
5 55995 argiro.kok
import {EnvProperties} from "../properties/env-properties";
6 59465 konstantin
import {Portal} from "../entities/adminTool/portal";
7 59074 konstantin
import {Page} from "../entities/adminTool/page";
8 50169 argiro.kok
9 59074 konstantin
@Injectable({  providedIn: 'root' })
10 59816 argiro.kok
export class ConfigurationService{
11 59465 konstantin
  private communityInformation: BehaviorSubject<Portal> = new BehaviorSubject(null);
12 59074 konstantin
  private sub: Subscription = null;
13 59465 konstantin
  private source: Observable<Portal> = null;
14 59074 konstantin
  private promise: Promise<boolean> = null;
15 50169 argiro.kok
16 59074 konstantin
   constructor(private http: HttpClient ) {}
17 54775 konstantin
18 59074 konstantin
   ngOnDestroy() {
19 59816 argiro.kok
      this.clearSubscriptions();
20
   }
21
22
  clearSubscriptions() {
23 59074 konstantin
     if(this.sub) {
24
       this.sub.unsubscribe();
25
     }
26
   }
27
28
  /**
29
   * @deprecated
30
   */
31 55995 argiro.kok
   getCommunityInformation(properties:EnvProperties, community:string){
32 59465 konstantin
     let url = properties.adminToolsAPIURL +"/"+properties.adminToolsPortalType+"/" + community + '/full';
33 59074 konstantin
     return this.http.get((properties.useLongCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url);
34 55964 argiro.kok
               //.map(res => res.json());
35 50586 argiro.kok
   }
36
37 59966 argiro.kok
  public initStaticCommunityInformation(communityInformation: Portal) {
38
    this.promise = new Promise<any>((resolve => {
39
            this.communityInformation.next(communityInformation);
40
            resolve();
41
    }));
42
  }
43 59074 konstantin
  public initCommunityInformation(properties:EnvProperties, community:string) {
44 59137 konstantin
    if(community == null) return;
45
46 59465 konstantin
    let url = properties.adminToolsAPIURL +"/" + properties.adminToolsPortalType + "/" + community + "/full";
47 59074 konstantin
    this.promise = new Promise<any>((resolve => {
48 59465 konstantin
      this.source = this.http.get<Portal>((properties.useLongCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url);
49 59074 konstantin
50
        this.sub = this.source
51
          .subscribe(
52 59465 konstantin
          (communityInformation: Portal) => {
53 59074 konstantin
            this.communityInformation.next(communityInformation);
54
            resolve();
55 59085 konstantin
          },
56
          error => {
57 59245 k.triantaf
            this.communityInformation.error(error);
58 59085 konstantin
            resolve();
59 59074 konstantin
          });
60
    }));
61
62
  }
63
64 59465 konstantin
  public get communityInformationState(): Observable<Portal> {
65 59074 konstantin
    return this.communityInformation.asObservable();
66
  }
67
68 58417 konstantin
  /**
69
   * @deprecated
70
   */
71 50586 argiro.kok
   isEntityEnabled(APIUrl:string, community:string,entity: string){
72 54775 konstantin
     //console.log("isEntityEnabled:  "+entity);
73 50169 argiro.kok
     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 50586 argiro.kok
     return this.http.get(APIUrl + "/page")
80 55964 argiro.kok
               .pipe(map(res => true));
81 50169 argiro.kok
   }
82
83 59074 konstantin
  /**
84
   * @deprecated
85
   */
86 55995 argiro.kok
   isPageEnabled(properties:EnvProperties, community:string,router: string){
87 58417 konstantin
     let page_route: string = router.split('?')[0].substring(1);
88 59074 konstantin
89 59465 konstantin
     let url = properties.adminToolsAPIURL + "/"+properties.adminToolsPortalType+"/" + community+"/pages?page_route="+page_route;
90 59074 konstantin
      return this.http.get((properties.useLongCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url)
91 55964 argiro.kok
              //.map(res => res.json())
92
              .pipe(map(res => {
93 54906 k.triantaf
                let result = false;
94 58417 konstantin
                if(res['length'] >0 && res[0].route == page_route){
95 50586 argiro.kok
                  result =  res[0].isEnabled;
96
                }
97
98
                return result;
99 58417 konstantin
              }));//.do(res => {console.log("Route "+page_route +" is "+res)});
100 50169 argiro.kok
   }
101 50586 argiro.kok
102 59074 konstantin
  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 59816 argiro.kok
     if(this.sub){
109
       this.sub.unsubscribe();
110
     }
111 59074 konstantin
     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 59465 konstantin
      let community: Portal = this.communityInformation.getValue();
121 59074 konstantin
      let pages: Page[] = <Page[]>community.pages;
122
      if (pages) {
123 60968 k.triantaf
        let page = pages.find((page: Page) => page.route == page_route);
124
        return page && page.isEnabled;
125
      } else {
126
        return false;
127 59074 konstantin
      }
128
  }
129
130 58417 konstantin
  /**
131
   * @deprecated
132
   */
133
  getMainPageContent(APIUrl:string, community:string,){
134 50586 argiro.kok
     return this.http.get(APIUrl + "/page")
135 55964 argiro.kok
               .pipe(map(res => true));
136 50169 argiro.kok
   }
137 58417 konstantin
138
  /**
139
   * @deprecated
140
   */
141
  getSpecialAnouncementContent(APIUrl:string, community:string,){
142 50586 argiro.kok
     return this.http.get(APIUrl + "/page")
143 55964 argiro.kok
               .pipe(map(res => ""));
144 50169 argiro.kok
   }
145 58417 konstantin
146
  /**
147
   * @deprecated
148
   */
149
  getHelpPageContent(APIUrl:string, community:string, router:string){
150 50586 argiro.kok
     return  this.http.get(APIUrl + "/page")
151 55964 argiro.kok
               .pipe(map(res => true));
152 50169 argiro.kok
   }
153 50586 argiro.kok
    // 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 50169 argiro.kok
161
162
}