Project

General

Profile

1
import {Injectable, OnDestroy} from '@angular/core';
2
import {HttpClient} from "@angular/common/http";
3
import {BehaviorSubject, from, Observable, 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 initCommunityInformation(properties:EnvProperties, community:string) {
38
    if(community == null) return;
39

    
40
    let url = properties.adminToolsAPIURL +"/" + properties.adminToolsPortalType + "/" + community + "/full";
41
    this.promise = new Promise<any>((resolve => {
42
      this.source = this.http.get<Portal>((properties.useLongCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url);
43

    
44
        this.sub = this.source
45
          .subscribe(
46
          (communityInformation: Portal) => {
47
            this.communityInformation.next(communityInformation);
48
            resolve();
49
          },
50
          error => {
51
            this.communityInformation.error(error);
52
            resolve();
53
          });
54
    }));
55

    
56
  }
57

    
58
  public get communityInformationState(): Observable<Portal> {
59
    return this.communityInformation.asObservable();
60
  }
61

    
62
  /**
63
   * @deprecated
64
   */
65
   isEntityEnabled(APIUrl:string, community:string,entity: string){
66
     //console.log("isEntityEnabled:  "+entity);
67
     let url = "isEntityEnabled-"+entity;
68

    
69
    //  if(entity == "publication" || entity == "dataset" || entity == "datasource"){
70
    //     return Observable.of(new Object()).mapTo(false);
71
    //  }
72
    //  return Observable.of(new Object()).mapTo(true);
73
     return this.http.get(APIUrl + "/page")
74
               .pipe(map(res => true));
75
   }
76

    
77
  /**
78
   * @deprecated
79
   */
80
   isPageEnabled(properties:EnvProperties, community:string,router: string){
81
     let page_route: string = router.split('?')[0].substring(1);
82

    
83
     let url = properties.adminToolsAPIURL + "/"+properties.adminToolsPortalType+"/" + community+"/pages?page_route="+page_route;
84
      return this.http.get((properties.useLongCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url)
85
              //.map(res => res.json())
86
              .pipe(map(res => {
87
                let result = false;
88
                if(res['length'] >0 && res[0].route == page_route){
89
                  result =  res[0].isEnabled;
90
                }
91

    
92
                return result;
93
              }));//.do(res => {console.log("Route "+page_route +" is "+res)});
94
   }
95

    
96
  async isPageEnabledByStateAsync(properties: EnvProperties, community:string, page_route: string) {
97
     if(!this.promise) {
98
       this.initCommunityInformation(properties, community);
99
     }
100

    
101
     await this.promise;
102
     if(this.sub){
103
       this.sub.unsubscribe();
104
     }
105
     return this.filtering(page_route);
106
  }
107

    
108
  isPageEnabledByState(properties: EnvProperties, community:string, router: string): Observable<boolean> {
109
     let page_route: string = router.split('?')[0].substring(1);
110
     return from(this.isPageEnabledByStateAsync(properties, community, page_route));
111
  }
112

    
113
  filtering(page_route: string) {
114
      let community: Portal = this.communityInformation.getValue();
115

    
116
      let pages: Page[] = <Page[]>community.pages;
117
      pages = pages.filter(function (page: Page) {
118
        return page.route == page_route;
119
      });
120

    
121
      if (pages) {
122
        let result = false;
123
        if (pages['length'] > 0 && pages[0].route == page_route) {
124
          result = pages[0].isEnabled;
125
        }
126
        return result;
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
}
(1-1/2)