NG-UI Added support for the quotos

 * Add the quotos limition to be defined from the UI.
 * Added the checkbox to activate the quote limitation.

Change-Id: I3795d00a3615018743449538e232b3bb31591d82
Signed-off-by: Barath Kumar R <barath.r@tataelxsi.co.in>
diff --git a/src/app/projects/ProjectsComponent.ts b/src/app/projects/ProjectsComponent.ts
index 7524994..fe55271 100644
--- a/src/app/projects/ProjectsComponent.ts
+++ b/src/app/projects/ProjectsComponent.ts
@@ -32,6 +32,7 @@
 import { RestService } from 'RestService';
 import { Subscription } from 'rxjs';
 import { SharedService } from 'SharedService';
+import { isNullOrUndefined } from 'util';
 
 /**
  * Creating component
@@ -169,10 +170,12 @@
     public generateProjectData(projectData: ProjectDetails): ProjectData {
         return {
             projectName: projectData.name,
-            modificationDate: this.sharedService.convertEpochTime(projectData._admin.modified),
-            creationDate: this.sharedService.convertEpochTime(projectData._admin.created),
+            modificationDate: this.sharedService.convertEpochTime(!isNullOrUndefined(projectData._admin)
+                ? projectData._admin.modified : null),
+            creationDate: this.sharedService.convertEpochTime(!isNullOrUndefined(projectData._admin) ? projectData._admin.created : null),
             id: projectData._id,
-            project: projectData._id
+            project: projectData._id,
+            quotas: !isNullOrUndefined(projectData.quotas) ? projectData.quotas : null
         };
     }
 
diff --git a/src/app/projects/project-create-update/ProjectCreateUpdateComponent.html b/src/app/projects/project-create-update/ProjectCreateUpdateComponent.html
index c327119..8afb727 100644
--- a/src/app/projects/project-create-update/ProjectCreateUpdateComponent.html
+++ b/src/app/projects/project-create-update/ProjectCreateUpdateComponent.html
@@ -17,30 +17,51 @@
 -->
 <form [formGroup]="projectForm" (ngSubmit)="projectAction(getProjectType)">
   <div class="modal-header">
-    <h4 class="modal-title" id="modal-basic-title">{{ (getProjectType == 'Add' ? 'NEW' : 'EDIT') | translate}} {{'PROJECT' | translate}}</h4>
+    <h4 class="modal-title" id="modal-basic-title">{{ (getProjectType == 'Add' ? 'NEW' : 'EDIT') | translate}}
+      {{'PROJECT' | translate}}</h4>
     <button class="button-xs" type="button" class="close" aria-label="Close" (click)="activeModal.close()">
       <i class="fas fa-times-circle text-danger"></i>
     </button>
   </div>
-  <div class="modal-body project-create-update">
+  <div class="modal-body modal-body-custom-height">
+    <div class="row" *ngIf="getProjectType === 'Add'">
+      <label class="col-xs-10 col-sm-10 col-md-10 col-lg-10 col-xl-10">{{'RECENTLY' | translate}}
+        {{'CREATED' | translate}} {{'PROJECT' | translate}}:
+        <b>{{(recentProject)?recentProject.name:''}}</b>
+      </label>
+    </div>
     <div class="form-group row">
-      <label class="col-sm-12 col-form-label mandatory-label" [ngClass]="{'text-danger': projectForm.invalid === true && submitted === true}">{{'MANDATORYCHECK' | translate}}</label>
+      <label class="col-sm-12 col-form-label mandatory-label"
+        [ngClass]="{'text-danger': projectForm.invalid === true && submitted === true}">{{'MANDATORYCHECK' | translate}}</label>
       <label class="col-sm-4 col-form-label">{{'PROJECT' | translate}} {{'NAME' | translate}}*</label>
       <div class="col-sm-8">
-        <input placeholder="{{'PROJECT' | translate}} {{'NAME' | translate}}" type="text" class="form-control" formControlName="project_name" [(ngModel)]="projectName" [ngClass]="{ 'is-invalid': submitted && f.project_name.errors }" required>
+        <input placeholder="{{'PROJECT' | translate}} {{'NAME' | translate}}" type="text" class="form-control"
+          formControlName="project_name" [ngClass]="{ 'is-invalid': submitted && f.project_name.errors }" required>
       </div>
     </div>
     <div class="form-group row" *ngIf="getProjectType === 'Add'">
       <label class="col-sm-4 col-form-label">{{'DOMAIN' | translate}} {{'NAME' | translate}}</label>
       <div class="col-sm-8">
-        <ng-select [clearable]="false" placeholder="{{'SELECT' | translate}}"
-        [items]="domains" bindLabel="text" bindValue="id" formControlName="domain_name" id="domain_name"
-        [ngClass]="{ 'is-invalid': submitted && f.domain_name.errors }"></ng-select>      </div>
+        <ng-select [clearable]="false" placeholder="{{'SELECT' | translate}}" [items]="domains" bindLabel="text"
+          bindValue="id" formControlName="domain_name" id="domain_name"
+          [ngClass]="{ 'is-invalid': submitted && f.domain_name.errors }"></ng-select>
+      </div>
     </div>
-    <div class="form-group row" *ngIf="getProjectType === 'Add'">
-      <label class="col-xs-10 col-sm-10 col-md-10 col-lg-10 col-xl-10">{{'RECENTLY' | translate}} {{'CREATED' | translate}} {{'PROJECT' | translate}}:
-        <b>{{(recentProject)?recentProject.name:''}}</b>
-      </label>
+    <div class="form-check form-check-inline ml-2">
+      <input class="form-check-input" type="checkbox" formControlName="enable_quota" (change)="checkQuota()"
+        id="quotaCheck" *ngIf="getProjectType === 'Add' || quotaRefs === null">
+      <label class="form-check-label" for="quotaCheck">{{'PAGE.PROJECT.QUOTA' | translate}}</label>
+    </div>
+    <div class="row mt-1" [ngbCollapse]="!f.enable_quota.value">
+      <div class="form-group col-sm-6" *ngFor="let quota of quotaItems;">
+        <div class="row">
+          <label class="col-sm-7 col-form-label">{{quota.title | translate}}*</label>
+          <div class="col-sm-5">
+            <input placeholder="{{'COUNT' | translate}}" type="number" min="{{quota.minValue}}"
+              class="form-control" [formControlName]="quota.value" [ngClass]="{ 'is-invalid': submitted && f[quota.value].errors }" required>
+          </div>
+        </div>
+      </div>
     </div>
   </div>
   <div class="modal-footer">
@@ -48,4 +69,4 @@
     <button type="submit" class="btn btn-primary">{{(getProjectType == 'Add' ? 'CREATE' : 'APPLY') | translate}}</button>
   </div>
 </form>
-<app-loader [waitingMessage]="message" *ngIf="isLoadingResults"></app-loader>
+<app-loader [waitingMessage]="message" *ngIf="isLoadingResults"></app-loader>
\ No newline at end of file
diff --git a/src/app/projects/project-create-update/ProjectCreateUpdateComponent.ts b/src/app/projects/project-create-update/ProjectCreateUpdateComponent.ts
index ea0bb8a..ba176e0 100644
--- a/src/app/projects/project-create-update/ProjectCreateUpdateComponent.ts
+++ b/src/app/projects/project-create-update/ProjectCreateUpdateComponent.ts
@@ -19,14 +19,14 @@
  * @file Project Add Modal
  */
 import { Component, Injector, Input, OnInit } from '@angular/core';
-import { FormBuilder, FormGroup, Validators } from '@angular/forms';
+import { AbstractControl, FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms';
 import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
 import { TranslateService } from '@ngx-translate/core';
 import { NotifierService } from 'angular-notifier';
 import { APIURLHEADER, ERRORDATA, MODALCLOSERESPONSEDATA } from 'CommonModel';
 import { DataService } from 'DataService';
 import { environment } from 'environment';
-import { ProjectData, ProjectDetails } from 'ProjectModel';
+import { ProjectData, ProjectDetails, QUOTA_ITEMS, QUOTAITEM} from 'ProjectModel';
 import { ProjectService } from 'ProjectService';
 import { RestService } from 'RestService';
 import { SharedService } from 'SharedService';
@@ -55,9 +55,6 @@
   /** Contains the recently created project details @public */
   public recentProject: ProjectDetails;
 
-  /** Contains project name @public */
-  public projectName: string;
-
   /** Contains project create or edit @public */
   public getProjectType: string;
 
@@ -79,6 +76,12 @@
   /** Holds list of domains @public */
   public domains: {}[] = [];
 
+  /** Holds list of quota items @public */
+  public quotaItems: QUOTAITEM[] = QUOTA_ITEMS;
+
+  /** Holds project reference from response  @public */
+  public quotaRefs: {} = null;
+
   /** FormBuilder instance added to the formBuilder @private */
   private formBuilder: FormBuilder;
 
@@ -116,7 +119,8 @@
     /** Initializing Form Action */
     this.projectForm = this.formBuilder.group({
       project_name: ['', Validators.required],
-      domain_name: [null]
+      domain_name: [null],
+      enable_quota: [false, Validators.required]
     });
   }
 
@@ -129,11 +133,14 @@
     if (this.getProjectType === 'Edit') {
       this.dataService.currentMessage.subscribe((data: ProjectData) => {
         if (data.projectName !== undefined || data.projectName !== '' || data.projectName !== null) {
-          this.projectName = data.projectName;
+          this.projectForm.patchValue({ project_name: data.projectName });
           this.projectRef = data.id;
+          this.quotaRefs = data.quotas;
+          this.patchQuotaInfo(this.quotaRefs);
         }
       });
     } else {
+      this.patchQuotaInfo();
       this.getProjects();
     }
   }
@@ -172,10 +179,11 @@
     const apiURLHeader: APIURLHEADER = {
       url: environment.PROJECTS_URL
     };
-    const projectPayload: {} = {
+    const projectPayload: ProjectDetails = {
       name: this.projectForm.value.project_name,
       domain_name: !isNullOrUndefined(this.projectForm.value.domain_name) ? this.projectForm.value.domain_name : undefined
     };
+    this.addQuotaLimit(projectPayload);
     this.restService.postResource(apiURLHeader, projectPayload).subscribe(() => {
       this.activeModal.close(this.modalData);
       this.isLoadingResults = false;
@@ -185,13 +193,29 @@
       this.isLoadingResults = false;
     });
   }
+  /** Handle enable quota limit checkbox event @public */
+  public checkQuota(): void {
+    if (this.getFormControl('enable_quota').value) {
+      this.quotaItems.forEach((quotaItem: QUOTAITEM): void => {
+        this.projectForm.addControl(quotaItem.value, new FormControl(quotaItem.minValue, Validators.required));
+      });
+    } else {
+      this.quotaItems.forEach((quotaItem: QUOTAITEM): void => {
+        this.getFormControl(quotaItem.value).setValue(quotaItem.minValue);
+      });
+    }
+  }
   /** Edit project @public */
   public editProject(): void {
     this.isLoadingResults = true;
     const apiURLHeader: APIURLHEADER = {
       url: environment.PROJECTS_URL + '/' + this.projectRef
     };
-    this.restService.patchResource(apiURLHeader, { name: this.projectForm.value.project_name }).subscribe(() => {
+    const projectPayload: ProjectDetails = {
+      name: this.projectForm.value.project_name
+    };
+    this.addQuotaLimit(projectPayload);
+    this.restService.patchResource(apiURLHeader, projectPayload).subscribe(() => {
       this.activeModal.close(this.modalData);
       this.isLoadingResults = false;
       this.projectService.setHeaderProjects();
@@ -230,4 +254,40 @@
       });
     }
   }
+
+  /** Used to get the AbstractControl of controlName passed @private */
+  private getFormControl(controlName: string): AbstractControl {
+    return this.projectForm.controls[controlName];
+  }
+
+  /** Add quota information to payload @private */
+  private addQuotaLimit(payload: ProjectDetails): void {
+    if (this.getFormControl('enable_quota').value) {
+      payload.quotas = {};
+      this.quotaItems.forEach((quotaItem: QUOTAITEM): void => {
+        payload.quotas[quotaItem.value] = this.getFormControl(quotaItem.value).value;
+      });
+    }
+  }
+
+  /** Set quota information in project form model @private */
+  private patchQuotaInfo(quotaRef?: {}): void {
+    if (quotaRef !== null && this.getProjectType === 'Edit') {
+      this.getFormControl('enable_quota').setValue(true);
+      this.quotaItems.forEach((quotaItem: QUOTAITEM): void => {
+        if (!isNullOrUndefined(quotaRef[quotaItem.value])) {
+          this.projectForm.addControl(quotaItem.value, new FormControl(quotaRef[quotaItem.value],
+            [Validators.required, Validators.min(quotaItem.minValue), Validators.max(quotaItem.maxValue)]));
+        } else {
+          this.projectForm.addControl(quotaItem.value, new FormControl(quotaItem.minValue, [Validators.required,
+          Validators.min(quotaItem.minValue), Validators.max(quotaItem.maxValue)]));
+        }
+      });
+    } else {
+      this.quotaItems.forEach((quotaItem: QUOTAITEM): void => {
+        this.projectForm.addControl(quotaItem.value, new FormControl(quotaItem.minValue, [Validators.required,
+        Validators.min(quotaItem.minValue), Validators.max(quotaItem.maxValue)]));
+      });
+    }
+  }
 }