From aa3784b6e68583c087d1700ebfe95be10ab6eb20 Mon Sep 17 00:00:00 2001
From: "florian.azizian" <florian.azizian@maarch.org>
Date: Thu, 14 Jun 2018 10:00:59 +0000
Subject: [PATCH] FEAT #7737 front admin templates lock

---
 .../template-administration.component.html    |  4 +--
 .../template-administration.component.ts      | 27 ++++++++++++++-----
 2 files changed, 23 insertions(+), 8 deletions(-)

diff --git a/apps/maarch_entreprise/Views/template-administration.component.html b/apps/maarch_entreprise/Views/template-administration.component.html
index 652253bf8a7..0bb0c11decd 100755
--- a/apps/maarch_entreprise/Views/template-administration.component.html
+++ b/apps/maarch_entreprise/Views/template-administration.component.html
@@ -149,8 +149,8 @@
                     </div>
                     <div class="form-group">
                         <div class="col-sm-12" style="text-align:center;">
-                            <button mat-raised-button color="primary" type="submit" [disabled]="!templatesFormUp.form.valid">{{lang.save}}</button>
-                            <button *ngIf="!creationMode" mat-raised-button color="primary" type="button" (click)="duplicateTemplate()">{{lang.duplicate}}</button>
+                            <button mat-raised-button color="primary" type="submit" [disabled]="!templatesFormUp.form.valid || lockFound">{{lang.save}}</button>
+                            <button *ngIf="!creationMode" mat-raised-button color="primary" type="button" (click)="duplicateTemplate()" [disabled]="lockFound">{{lang.duplicate}}</button>
                         </div>
                     </div>
                 </form>
diff --git a/apps/maarch_entreprise/js/angular/app/administration/template-administration.component.ts b/apps/maarch_entreprise/js/angular/app/administration/template-administration.component.ts
index d4328958c75..ca58262a7c6 100755
--- a/apps/maarch_entreprise/js/angular/app/administration/template-administration.component.ts
+++ b/apps/maarch_entreprise/js/angular/app/administration/template-administration.component.ts
@@ -30,6 +30,8 @@ export class TemplateAdministrationComponent implements OnInit {
     datasourcesList: any;
     jnlpValue: any = {};
     buttonFileName: any = this.lang.importFile;
+    lockFound: boolean = false;
+    intervalLockFile: any;
 
     loading: boolean = false;
 
@@ -38,8 +40,8 @@ export class TemplateAdministrationComponent implements OnInit {
         this.mobileQuery = media.matchMedia('(max-width: 768px)');
         this._mobileQueryListener = () => changeDetectorRef.detectChanges();
         this.mobileQuery.addListener(this._mobileQueryListener);
-        window['angularProfileComponent'] = {
-            componentAfterUpload: (base64Content: any) => this.processAfterUpload(base64Content),
+        window['angularTemplateComponent'] = {
+            componentAfterUpload: (base64Content: any) => this.processAfterUpload(base64Content)
         };
     }
 
@@ -161,7 +163,7 @@ export class TemplateAdministrationComponent implements OnInit {
             reader.readAsDataURL(fileInput.target.files[0]);
             
             reader.onload = function (value: any) {
-                window['angularProfileComponent'].componentAfterUpload(value.target.result);
+                window['angularTemplateComponent'].componentAfterUpload(value.target.result);
             };
         }
     }
@@ -178,14 +180,14 @@ export class TemplateAdministrationComponent implements OnInit {
 
     startJnlp() {
         if (this.creationMode) {
-            this.jnlpValue.objectType = 'templateStyle';
+            this.jnlpValue.objectType = 'templateCreation';
             for(let element of this.defaultTemplatesList){
                 if(this.template.template_style == element.fileExt + ': ' + element.fileName){
                     this.jnlpValue.objectId = element.filePath;
                 }
             }
         } else {
-            this.jnlpValue.objectType = 'template';
+            this.jnlpValue.objectType = 'templateModification';
             this.jnlpValue.objectId   = this.template.template_id;
         }
         this.jnlpValue.table    = 'templates';
@@ -196,12 +198,25 @@ export class TemplateAdministrationComponent implements OnInit {
         .subscribe((data: any) => {
             this.template.userUniqueId = data.userUniqueId;
             this.fileToImport();
-            window.location.href       = this.coreUrl + 'rest/jnlp?fileName=' + data.generatedJnlp;
+            window.location.href = this.coreUrl + 'rest/jnlp?fileName=' + data.generatedJnlp;
+            this.checkLockFile();
         }, (err) => {
             this.notify.error(err.error.errors);
         });        
     }
 
+    checkLockFile(){
+        this.intervalLockFile = setInterval(() => {
+            this.http.get(this.coreUrl + 'rest/jnlp/lock/' + this.template.userUniqueId)
+            .subscribe((data: any) => {
+                this.lockFound = data.lockFileFound;
+                if(!this.lockFound){
+                    clearInterval(this.intervalLockFile);
+                }
+            });
+        }, 1000)
+    }
+
     duplicateTemplate()
     {
         let r = confirm(this.lang.confirmDuplicate);
-- 
GitLab