service role and service function metadata not imported
[sdc.git] / catalog-ui / src / app / view-models / workspace / tabs / general / general-view-model.ts
index 8023cee..86bcab0 100644 (file)
@@ -85,6 +85,10 @@ export interface IGeneralScope extends IWorkspaceViewModelScope {
     instantiationTypes:Array<instantiationType>;
     isHiddenCategorySelected: boolean;
     isModelRequired: boolean;
+    othersFlag: boolean;
+    functionOption: string;
+    othersRoleFlag: boolean;
+    roleOption: string;
 
     save():Promise<any>;
     revert():void;
@@ -108,6 +112,8 @@ export interface IGeneralScope extends IWorkspaceViewModelScope {
     possibleToUpdateIcon():boolean;
     initModel():void;
     isVspImport(): boolean;
+    setServiceFunction(option:string):void;
+    setServiceRole(option:string):void;
 }
 
 // tslint:disable-next-line:max-classes-per-file
@@ -253,6 +259,8 @@ export class GeneralViewModel {
         this.$scope.progressService = this.progressService;
         this.$scope.componentCategories = new componentCategories();
         this.$scope.componentCategories.selectedCategory = this.$scope.component.selectedCategory;
+        this.$scope.othersFlag = false;
+        this.$scope.othersRoleFlag = false;
 
         // Init UIModel
         this.$scope.component.tags = _.without(this.$scope.component.tags, this.$scope.component.name);
@@ -299,19 +307,23 @@ export class GeneralViewModel {
                         });
                         (<Service>this.$scope.component).derivedFromGenericType = serviceCsar.substitutionNodeType;
                         this.$scope.onBaseTypeChange();
+                        this.setFunctionRole(service);
                     },
                     (error) => {
                         const errorMsg = this.$filter('translate')('IMPORT_FAILURE_MESSAGE_TEXT');
                         console.error(errorMsg, error);
                         const errorDetails = {
-                            'Error': error.reason,
-                            'Details': error.message
+                            'Error': this.capitalize(error.reason),
+                            'Details': this.capitalize(error.message)
                         };
                         this.modalServiceSdcUI.openErrorDetailModal('Error', this.$filter('translate')('IMPORT_FAILURE_MESSAGE_TEXT'),
                             'error-modal', errorDetails);
                         this.$state.go('dashboard');
                     });
             }
+
+            this.setFunctionRole(service);
+
             if (this.$scope.isEditMode() && service.serviceType == 'Service' && !service.csarUUID) {
                 this.$scope.isShowFileBrowse = true;
             }
@@ -339,7 +351,6 @@ export class GeneralViewModel {
             this.$scope.isShowOnboardingSelectionBrowse = false;
         }
 
-
         //init file extensions based on the file that was imported.
         if (this.$scope.component.isResource() && (<Resource>this.$scope.component).importedFile) {
             let fileName:string = (<Resource>this.$scope.component).importedFile.filename;
@@ -357,8 +368,6 @@ export class GeneralViewModel {
             //(<Resource>this.$scope.component).importedFile.filetype="csar";
         }
 
-
-
         this.$scope.setValidState(true);
 
         this.$scope.calculateUnique = (mainCategory:string, subCategory:string):string => {
@@ -375,7 +384,6 @@ export class GeneralViewModel {
             this.$scope.originComponent.contactId = this.$scope.component.contactId;
         }
 
-
         this.$scope.$on('$destroy', () => {
             this.EventListenerService.unRegisterObserver(EVENTS.ON_LIFECYCLE_CHANGE_WITH_SAVE);
             this.EventListenerService.unRegisterObserver(EVENTS.ON_LIFECYCLE_CHANGE);
@@ -383,6 +391,38 @@ export class GeneralViewModel {
 
     };
 
+    private capitalize(s) {
+        return s && s[0].toUpperCase() + s.slice(1);
+    }
+
+    private setFunctionRole = (service : Service) : void => {
+        if (!service.serviceFunction && service.componentMetadata) {
+            service.serviceFunction = service.componentMetadata.serviceFunction;
+        }
+        if (!service.serviceRole && service.componentMetadata) {
+            service.serviceRole = service.componentMetadata.serviceRole;
+        }
+        if (service.serviceFunction) {
+            const functionList : string[] = this.$scope.getMetadataKeyValidValues('Service Function');
+            if (functionList.find(value => value == service.serviceFunction) != undefined) {
+                this.$scope.functionOption = service.serviceFunction;
+            } else {
+                this.$scope.functionOption = 'Others';
+                this.$scope.othersFlag = true;
+            }
+        }
+
+        if (service.serviceRole) {
+            const roleList : string[] = this.$scope.getMetadataKeyValidValues('Service Role');
+            if (roleList.find(value => value == service.serviceRole) != undefined) {
+                this.$scope.roleOption = service.serviceRole;
+            } else {
+                this.$scope.roleOption = 'Others';
+                this.$scope.othersRoleFlag = true;
+            }
+        }
+    }
+
     // Convert category string MainCategory_#_SubCategory to Array with one item (like the server except)
     private convertCategoryStringToOneArray = ():IMainCategory[] => {
         let tmp = this.$scope.component.selectedCategory.split("_#_");
@@ -760,8 +800,21 @@ export class GeneralViewModel {
         });
 
         this.$scope.onCategoryChange = (): void => {
+            if (!this.$scope.component.selectedCategory) {
+                this.$scope.editForm['category'].$setDirty();
+            }
+            if (!this.$scope.component.description) {
+                this.$scope.editForm['description'].$setDirty();
+            }
             this.$scope.component.selectedCategory = this.$scope.componentCategories.selectedCategory;
             if (this.$scope.component.selectedCategory) {
+                this.$scope.roleOption = null;
+                (<Service>this.$scope.component).serviceRole = null;
+                this.$scope.othersFlag = false;
+                this.$scope.functionOption = null;
+                (<Service>this.$scope.component).serviceFunction = null;
+                this.$scope.othersRoleFlag = false;
+
                 this.$scope.component.categories = this.convertCategoryStringToOneArray();
                 this.$scope.component.icon = DEFAULT_ICON;
                 if (this.$scope.component.categories[0].metadataKeys) {
@@ -769,6 +822,18 @@ export class GeneralViewModel {
                         if (!this.$scope.component.categorySpecificMetadata[metadataKey.name]) {
                             this.$scope.component.categorySpecificMetadata[metadataKey.name] = metadataKey.defaultValue ? metadataKey.defaultValue : "";
                         }
+                        if (metadataKey.name === 'Service Role') {
+                            if ((<Service>this.$scope.component).componentMetadata && (<Service>this.$scope.component).componentMetadata.serviceRole) {
+                                this.$scope.roleOption = (<Service>this.$scope.component).componentMetadata.serviceRole;
+                            }
+                            else {
+                                this.$scope.roleOption = this.$scope.component.categorySpecificMetadata[metadataKey.name];
+                            }
+                            (<Service>this.$scope.component).serviceRole = this.$scope.roleOption;
+                        }
+                        if (metadataKey.name === 'Service Function') {
+                            this.$scope.functionOption = this.$scope.component.categorySpecificMetadata[metadataKey.name];
+                        }
                     }
                 }
                 if (this.$scope.component.categories[0].subcategories && this.$scope.component.categories[0].subcategories[0].metadataKeys) {
@@ -846,6 +911,28 @@ export class GeneralViewModel {
             }
         };
 
+        this.$scope.setServiceFunction = (option:string): void => {
+            if (option === 'Others') {
+                this.$scope.othersFlag = true;
+                (<Service>this.$scope.component).serviceFunction = '';
+            } else {
+                this.$scope.othersFlag = false;
+                (<Service>this.$scope.component).serviceFunction = option;
+            }
+
+        }
+
+        this.$scope.setServiceRole = (option:string): void => {
+            if (option === 'Others') {
+                this.$scope.othersRoleFlag = true;
+                (<Service>this.$scope.component).serviceRole = '';
+            } else {
+                this.$scope.othersRoleFlag = false;
+                (<Service>this.$scope.component).serviceRole = option;
+            }
+
+        }
+
         this.EventListenerService.registerObserverCallback(EVENTS.ON_LIFECYCLE_CHANGE, this.$scope.reload);
 
         this.$scope.isMetadataKeyMandatory = (key: string): boolean => {
@@ -856,7 +943,11 @@ export class GeneralViewModel {
         this.$scope.getMetadataKeyValidValues = (key: string): string[] => {
             let metadataKey = this.getMetadataKey(key);
             if (metadataKey) {
-                return metadataKey.validValues;
+                if (key == 'Service Function' || key == 'Service Role') {
+                    return metadataKey.validValues.concat("Others");
+                } else {
+                    return metadataKey.validValues;
+                }
             }
             return [];
         }
@@ -879,8 +970,10 @@ export class GeneralViewModel {
 
         this.$scope.isMetadataKeyForComponentCategoryService = (key: string, attribute: string): boolean => {
             let metadatakey = this.getMetadataKey(key);
-            if (metadatakey && (!this.$scope.component[attribute] || !metadatakey.validValues.find(v => v === this.$scope.component[attribute]))) {
-                this.$scope.component[attribute] = metadatakey.defaultValue;
+            if (attribute != 'serviceFunction' && attribute != 'serviceRole') {
+                if (metadatakey && (!this.$scope.component[attribute] || !metadatakey.validValues.find(v => v === this.$scope.component[attribute]))) {
+                    this.$scope.component[attribute] = metadatakey.defaultValue;
+                }
             }
             return metadatakey != null;
         }
@@ -923,6 +1016,13 @@ export class GeneralViewModel {
                     }
                 });
             }
+            if((<Service>this.$scope.component).derivedFromGenericType) {
+                baseTypeResponseList.baseTypes.forEach(baseTypeObj => {
+                    if(baseTypeObj.toscaResourceName == (<Service>this.$scope.component).derivedFromGenericType) {
+                        baseType = baseTypeObj;
+                    }
+                });
+            }
             baseType.versions.reverse().forEach(version => this.$scope.baseTypeVersions.push(version));
             this.$scope.component.derivedFromGenericType = baseType.toscaResourceName;
             this.$scope.component.derivedFromGenericVersion = this.$scope.baseTypeVersions[0];