From c7a2bc846504f5ff1b4cc78369dfcb0c9e3c9f8c Mon Sep 17 00:00:00 2001
From: mme <mechtel@iqb.hu-berlin.de>
Date: Thu, 14 Mar 2019 12:56:11 +0100
Subject: [PATCH] added: sysConfig

---
 src/app/app.component.ts           | 69 ++++++++++++++++--------------
 src/app/app.interfaces.ts          | 14 ++++++
 src/app/backend.service.ts         | 12 +++++-
 src/app/maindata.service.ts        | 27 +++++++++++-
 src/app/start/start.component.html |  7 +--
 src/app/start/start.component.ts   | 20 ++++-----
 6 files changed, 99 insertions(+), 50 deletions(-)

diff --git a/src/app/app.component.ts b/src/app/app.component.ts
index 204bcaa6..38945859 100644
--- a/src/app/app.component.ts
+++ b/src/app/app.component.ts
@@ -29,47 +29,50 @@ export class AppComponent implements OnInit {
       }
     });
 
-    // restore login status if stored in localStorage
-    const loginToken = localStorage.getItem('lt');
-    if (loginToken !== null) {
-      if (loginToken.length > 0) {
-        let personToken = localStorage.getItem('pt');
-        let bookletDbId = 0;
-        if (personToken !== null) {
-          if (personToken.length > 0) {
-            const bookletDbIdStr = localStorage.getItem('bi');
-            if (bookletDbIdStr !== null) {
-              bookletDbId = Number(bookletDbIdStr);
+    this.bs.getSysConfig().subscribe(sc => {
+      this.mds.setSysConfig(sc);
+      // restore login status if stored in localStorage
+      const loginToken = localStorage.getItem('lt');
+      if (loginToken !== null) {
+        if (loginToken.length > 0) {
+          let personToken = localStorage.getItem('pt');
+          let bookletDbId = 0;
+          if (personToken !== null) {
+            if (personToken.length > 0) {
+              const bookletDbIdStr = localStorage.getItem('bi');
+              if (bookletDbIdStr !== null) {
+                bookletDbId = Number(bookletDbIdStr);
+              }
             }
+          } else {
+            personToken = '';
+          }
+          let code = localStorage.getItem('c');
+          if (code === null) {
+            code = '';
           }
-        } else {
-          personToken = '';
-        }
-        let code = localStorage.getItem('c');
-        if (code === null) {
-          code = '';
-        }
 
           // bookletDbId is not yet checked by getLoginData, only passed-through
           this.bs.getLoginData(loginToken, personToken, bookletDbId).subscribe(ld => {
-          if (ld instanceof ServerError) {
-            this.mds.setNewLoginData();
-          } else {
-            const loginData = ld as LoginData;
-            loginData.logintoken = loginToken;
-            loginData.persontoken = personToken;
-            if (personToken.length === 0) {
-              loginData.code = code;
-              loginData.booklet = 0;
+            if (ld instanceof ServerError) {
+              this.mds.setNewLoginData();
+            } else {
+              const loginData = ld as LoginData;
+              loginData.logintoken = loginToken;
+              loginData.persontoken = personToken;
+              if (personToken.length === 0) {
+                loginData.code = code;
+                loginData.booklet = 0;
+              }
+              this.mds.setNewLoginData(loginData);
             }
-            this.mds.setNewLoginData(loginData);
-          }
-        });
+          });
+        } else {
+          this.mds.setNewLoginData();
+        }
       } else {
         this.mds.setNewLoginData();
       }
-    } else {
-      this.mds.setNewLoginData();
-    }
+    });
   }
 }
diff --git a/src/app/app.interfaces.ts b/src/app/app.interfaces.ts
index 55dc01c9..4f37cf45 100644
--- a/src/app/app.interfaces.ts
+++ b/src/app/app.interfaces.ts
@@ -37,3 +37,17 @@ export interface PersonTokenAndBookletDbId {
   persontoken: string;
   bookletDbId: number;
 }
+
+export interface KeyValuePair {
+  [K: string]: string;
+}
+
+export enum SysConfigKey {
+  testEndButtonText = 'testEndButtonText',
+  bookletSelectPrompt = 'bookletSelectPrompt',
+  bookletSelectTitle = 'bookletSelectTitle',
+  bookletSelectPromptOne = 'bookletSelectPromptOne',
+  bookletSelectPromptMany = 'bookletSelectPromptMany',
+  codeInputTitle = 'codeInputTitle',
+  codeInputPrompt = 'codeInputPrompt'
+}
diff --git a/src/app/backend.service.ts b/src/app/backend.service.ts
index 38b42502..b24154e1 100644
--- a/src/app/backend.service.ts
+++ b/src/app/backend.service.ts
@@ -1,8 +1,10 @@
+
 import { Injectable, Inject } from '@angular/core';
 import { HttpClient, HttpHeaders, HttpEvent, HttpErrorResponse } from '@angular/common/http';
 import { Observable, of } from 'rxjs';
 import { catchError } from 'rxjs/operators';
-import { LoginData, BookletStatus, PersonTokenAndBookletDbId, BookletData, BookletDataListByCode } from './app.interfaces';
+import { LoginData, BookletStatus, PersonTokenAndBookletDbId, BookletData, BookletDataListByCode,
+  KeyValuePair } from './app.interfaces';
 
 // ============================================================================
 // class instead of interface to be able to use instanceof to check type
@@ -69,6 +71,14 @@ export class BackendService {
         );
   }
 
+  // BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
+  getSysConfig(): Observable<KeyValuePair> {
+    return this.http.get<KeyValuePair>(this.serverSlimUrl + 'sysconfig')
+        .pipe(
+          catchError(e => of(null))
+        );
+  }
+
   // BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
   getBookletStatus(bookletid: string, code = ''): Observable<BookletStatus | ServerError> {
     let urlString = '?b=' + bookletid;
diff --git a/src/app/maindata.service.ts b/src/app/maindata.service.ts
index 1c6e847b..0fed3eab 100644
--- a/src/app/maindata.service.ts
+++ b/src/app/maindata.service.ts
@@ -1,7 +1,8 @@
+import { KeyValuePair } from './test-controller/test-controller.interfaces';
 import { ServerError, BackendService } from './backend.service';
 import { BehaviorSubject, Subject, forkJoin } from 'rxjs';
 import { Injectable } from '@angular/core';
-import { LoginData } from './app.interfaces';
+import { LoginData, SysConfigKey } from './app.interfaces';
 
 @Injectable({
   providedIn: 'root'
@@ -22,6 +23,7 @@ export class MainDataService {
 
   public loginData$ = new BehaviorSubject<LoginData>(this.standardLoginData);
   public globalErrorMsg$ = new BehaviorSubject<ServerError>(null);
+  private _sysConfig: KeyValuePair = null;
 
   // set by app.component.ts
   public postMessage$ = new Subject<MessageEvent>();
@@ -133,4 +135,27 @@ export class MainDataService {
     const myLoginData = this.loginData$.getValue();
     return myLoginData.persontoken;
   }
+
+  // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+  setSysConfig(sc: KeyValuePair) {
+    this._sysConfig = sc;
+  }
+
+  getSysConfigValue(key: SysConfigKey): string {
+    if (this._sysConfig !== null) {
+      if (this._sysConfig.hasOwnProperty(key)) {
+        return this._sysConfig[key];
+      }
+    }
+    switch (key) {
+      case SysConfigKey.testEndButtonText: return 'Test beenden';
+      case SysConfigKey.bookletSelectPrompt: return 'Bitte wählen';
+      case SysConfigKey.bookletSelectTitle: return 'Bitte wählen';
+      case SysConfigKey.bookletSelectPromptOne: return 'Bitte klick auf die Schaltfläche links, um den Test zu starten!';
+      case SysConfigKey.bookletSelectPromptMany: return 'Bitte klick auf eine der Schaltflächen links, um einen Test zu starten!';
+      case SysConfigKey.codeInputPrompt: return 'Bitte den Personencode eingeben!';
+      case SysConfigKey.codeInputTitle: return 'Personencode eingeben';
+    }
+    return '?';
+  }
 }
diff --git a/src/app/start/start.component.html b/src/app/start/start.component.html
index f74a19d5..dd49b9d7 100644
--- a/src/app/start/start.component.html
+++ b/src/app/start/start.component.html
@@ -27,13 +27,14 @@
           <button mat-raised-button type="submit" [disabled]="testtakerloginform.invalid" color="primary">Weiter</button>
         </mat-card-actions>
       </form>
-      <p class="error-msg">{{ errormsg?.labelNice }}</p>
+      <p class="error-msg">{{ (mds.globalErrorMsg$ | async)?.labelNice }}</p>
+
     </mat-card>
 
     <!-- - - - - - - - - - - - - - - - - -->
     <mat-card fxFlex="0 0 400px" *ngIf="showCodeForm">
       <form [formGroup]="codeinputform" (ngSubmit)="codeinput()">
-        <mat-card-title>Anmelden</mat-card-title>
+        <mat-card-title>{{ codeInputTitle }}</mat-card-title>
         <mat-card-content>
           <mat-form-field>
             <input matInput formControlName="code" placeholder="Personen-Code"(keyup.enter)="codeinput()">
@@ -122,7 +123,7 @@
             <p>Es wird gerade ein Test ausgeführt. Bitte wählen Sie durch Klicken auf eine der beiden Schaltflächen
               links, ob der Test fortgesetzt oder beendet werden soll!</p>
         </div>
-        <div class="error-msg">{{ (mds.globalErrorMsg | async)?.labelNice }}</div>
+        <div class="error-msg">{{ (mds.globalErrorMsg$ | async)?.labelNice }}</div>
 
       </mat-card-content>
       <mat-card-actions>
diff --git a/src/app/start/start.component.ts b/src/app/start/start.component.ts
index dc282ddf..e6176aed 100644
--- a/src/app/start/start.component.ts
+++ b/src/app/start/start.component.ts
@@ -1,3 +1,4 @@
+import { SysConfigKey } from './../app.interfaces';
 import { MainDataService } from './../maindata.service';
 import { Subscription, BehaviorSubject, forkJoin } from 'rxjs';
 import { MessageDialogComponent, MessageDialogData, MessageType } from './../iqb-common';
@@ -15,7 +16,6 @@ import { StartButtonData } from './start-button-data.class';
 })
 export class StartComponent implements OnInit, OnDestroy {
   private loginDataSubscription: Subscription = null;
-  private globalErrorMsgSubscription: Subscription = null;
   private dataLoading = false;
 
   // for template
@@ -24,7 +24,6 @@ export class StartComponent implements OnInit, OnDestroy {
   private showBookletButtons = false;
   private bookletlist: StartButtonData[] = [];
   private showTestRunningButtons = false;
-  private errormsg: ServerError = null;
   private validCodes = [];
   private loginStatusText = ['nicht angemeldet'];
 
@@ -34,9 +33,8 @@ export class StartComponent implements OnInit, OnDestroy {
   private testEndButtonText = 'Test beenden';
   private bookletSelectPrompt = 'Bitte wählen';
   private bookletSelectTitle = 'Bitte wählen';
-  private bookletSelectPromptOne = 'Bitte klick auf die Schaltfläche links, um den Test zu starten!';
-  private bookletSelectPromptMany = 'Bitte klicken Sie auf eine der Schaltflächen, um einen Test zu starten!';
-  private codeInputPrompt = 'Bitte Log-in eingeben, der auf dem Zettel steht!';
+  private codeInputPrompt = '';
+  private codeInputTitle = '';
 
   // ??
   // private sessiondata: PersonBooklets;
@@ -54,9 +52,6 @@ export class StartComponent implements OnInit, OnDestroy {
   }
 
   ngOnInit() {
-    this.globalErrorMsgSubscription = this.mds.globalErrorMsg$.subscribe(m => {
-      this.errormsg = m;
-    });
     this.loginDataSubscription = this.mds.loginData$.subscribe(logindata => {
       this.bookletlist = [];
       if (logindata.logintoken.length > 0) {
@@ -115,12 +110,13 @@ export class StartComponent implements OnInit, OnDestroy {
             } else {
               // code not yet given
               // code prompt
-
+              this.codeInputTitle = this.mds.getSysConfigValue(SysConfigKey.codeInputTitle);
+              this.codeInputPrompt = this.mds.getSysConfigValue(SysConfigKey.codeInputPrompt);
               this.showCodeForm = true;
               this.showBookletButtons = false;
             }
           } else {
-            // no code but there is only one
+            // no code but there is only one code
             // buttons to select booklet
 
             this.showCodeForm = false;
@@ -152,10 +148,10 @@ export class StartComponent implements OnInit, OnDestroy {
                 this.bookletSelectPrompt = (allOk.length > 1) ? 'Testhefte beendet' : 'Testheft beendet';
                 this.bookletSelectTitle = 'Beendet';
               } else if (numberOfOpenBooklets === 1) {
-                this.bookletSelectPrompt = this.bookletSelectPromptOne;
+                this.bookletSelectPrompt = this.mds.getSysConfigValue(SysConfigKey.bookletSelectPromptOne);
                 this.bookletSelectTitle = 'Bitte starten';
               } else {
-                this.bookletSelectPrompt = this.bookletSelectPromptMany;
+                this.bookletSelectPrompt = this.mds.getSysConfigValue(SysConfigKey.bookletSelectPromptMany);
                 this.bookletSelectTitle = 'Bitte wählen';
               }
             });
-- 
GitLab