Source option- avoiding duplicates

Implemented code to prevent adding duplicate resources in source list

Issue-ID: CCSDK-707
Change-Id: I56caa3a248a2e2a611dceec0982820ea13c438a6
Signed-off-by: Arundathi Patil <arundpil@in.ibm.com>
diff --git a/cds-ui/client/src/app/feature-modules/resource-definition/resource-edit/sources-template/sources-template.component.html b/cds-ui/client/src/app/feature-modules/resource-definition/resource-edit/sources-template/sources-template.component.html
index b179f01..a5f367d 100644
--- a/cds-ui/client/src/app/feature-modules/resource-definition/resource-edit/sources-template/sources-template.component.html
+++ b/cds-ui/client/src/app/feature-modules/resource-definition/resource-edit/sources-template/sources-template.component.html
@@ -27,7 +27,7 @@
       class="sources-list"
       (cdkDropListDropped)="drop($event)">
       	<div class="sources-box" *ngFor="let item of sourcesOptions;let i = index" cdkDrag>
-          <mat-expansion-panel class="expansion-panel" (opened)="selected(item)">
+          <mat-expansion-panel class="expansion-panel">
       			<mat-expansion-panel-header>
         			<mat-panel-title>
                   {{item.name}}
diff --git a/cds-ui/client/src/app/feature-modules/resource-definition/resource-edit/sources-template/sources-template.component.ts b/cds-ui/client/src/app/feature-modules/resource-definition/resource-edit/sources-template/sources-template.component.ts
index 42f990a..3c76b85 100644
--- a/cds-ui/client/src/app/feature-modules/resource-definition/resource-edit/sources-template/sources-template.component.ts
+++ b/cds-ui/client/src/app/feature-modules/resource-definition/resource-edit/sources-template/sources-template.component.ts
@@ -48,7 +48,8 @@
     sources:ISourcesData; 
     sourcesOptions = [];
     sourcesData = {};
-    @Output() resourcesData = new EventEmitter();  
+    @Output() resourcesData = new EventEmitter();
+    tempOption = [];
  
     constructor(private store: Store<IAppState>, private apiService: ResourceEditService) {
     this.rdState = this.store.select('resources');
@@ -62,12 +63,10 @@
       resourcesdata => {
         var resourcesState: IResourcesState = { resources: resourcesdata.resources, isLoadSuccess: resourcesdata.isLoadSuccess, isSaveSuccess: resourcesdata.isSaveSuccess, isUpdateSuccess: resourcesdata.isUpdateSuccess };
         this.resources=resourcesState.resources;
-      //   this.sources = resourcesState.resources.sources;
          if(resourcesState.resources.definition && resourcesState.resources.definition.sources) {
          this.sources = resourcesState.resources.definition.sources;
          }
         for (let key in this.sources) {
-            // this.sourcesOptions.push(key);
             let source = {
                name : key,
                data: this.sources[key]
@@ -136,14 +135,45 @@
   }
     
  drop(event: CdkDragDrop<string[]>) {
-    if (event.previousContainer === event.container) {
-      moveItemInArray(event.container.data, event.previousIndex, event.currentIndex);
-    } else {
-      transferArrayItem(event.previousContainer.data,
-                        event.container.data,
-                        event.previousIndex,
-                        event.currentIndex);
-    }
+   if (!this.checkIfSourceExists(event.item.element.nativeElement.innerText)) {
+      if (event.previousContainer === event.container) {
+         moveItemInArray(event.container.data, event.previousIndex, event.currentIndex);
+      } else {
+         transferArrayItem(event.previousContainer.data,
+            event.container.data,
+            event.previousIndex,
+            event.currentIndex);
+      }
+      this.tempOption.forEach((item) => {
+         if (item.name == event.item.element.nativeElement.innerText) {
+            this.apiService.getModelType(item.value)
+               .subscribe(data => {
+                  console.log(data);
+                  data.forEach(dataitem => {
+                     if (typeof (dataitem) == "object") {
+                        for (let key1 in dataitem) {
+                           if (key1 == 'properties') {
+                              let newPropOnj = {}
+                              for (let key2 in dataitem[key1]) {
+                                 console.log(dataitem[key1][key2]);
+                                 let varType = dataitem[key1][key2].type
+                                 // let property :  varType = 
+                                 newPropOnj[key2] = dataitem[key1][key2];
+                              }
+                           }
+                        }
+                     }
+                  });
+                  this.sourcesData = data;
+                  this.sourcesOptions.forEach(sourcesOptionsitem => {
+                     if (sourcesOptionsitem.name == item.name) {
+                        sourcesOptionsitem.data = data;
+                     }
+                  })
+               })
+         }
+      });
+   }
   }
 
   getResources() {
@@ -152,11 +182,21 @@
       console.log(data);
       for (let key in data[0]) {
          let sourceObj = { name: key, value: data[0][key] }
-         this.option.push(sourceObj);  
+         this.option.push(sourceObj);
+         this.tempOption.push(sourceObj); 
      }
-      // this.sourcesOptions = data;
    }, error=>{
       console.log(error);
    })
   }
+
+  checkIfSourceExists(sourceName) {
+   let sourceExists: boolean = false;
+   this.sourcesOptions.forEach(item => {
+      if (item.name == sourceName) {
+         sourceExists = true;
+      }
+   });
+   return sourceExists;
+}
 }