[SDC-29] rebase continue work to align source
[sdc.git] / catalog-ui / src / app / utils / component-factory.ts
1 'use strict';
2 import {DEFAULT_ICON, ResourceType, ComponentType} from "./constants";
3 import {ServiceService, CacheService, ResourceService, ProductService} from "app/services";
4 import {IMainCategory, ISubCategory, ICsarComponent, Component, Resource, Service, Product} from "app/models";
5 import {ComponentMetadata} from "../models/component-metadata";
6 import {ComponentServiceNg2} from "../ng2/services/component-services/component.service";
7 import {ComponentGenericResponse} from "../ng2/services/responses/component-generic-response";
8
9
10 export class ComponentFactory {
11
12     static '$inject' = [
13         'Sdc.Services.Components.ResourceService',
14         'Sdc.Services.Components.ServiceService',
15         'Sdc.Services.Components.ProductService',
16         'Sdc.Services.CacheService',
17         '$q',
18         'ComponentServiceNg2'
19     ];
20
21     constructor(private ResourceService:ResourceService,
22                 private ServiceService:ServiceService,
23                 private ProductService:ProductService,
24                 private cacheService:CacheService,
25                 private $q:ng.IQService,
26                 private ComponentServiceNg2: ComponentServiceNg2) {
27     }
28
29     public createComponent = (component:Component):Component => {
30         let newComponent:Component;
31         switch (component.componentType) {
32
33             case 'SERVICE':
34                 newComponent = new Service(this.ServiceService, this.$q, <Service> component);
35                 break;
36
37             case 'RESOURCE':
38                 newComponent = new Resource(this.ResourceService, this.$q, <Resource> component);
39                 break;
40
41             case 'PRODUCT':
42                 newComponent = new Product(this.ProductService, this.$q, <Product> component);
43                 break;
44         }
45         return newComponent;
46     };
47
48     public createProduct = (product:Product):Product => {
49         let newProduct:Product = new Product(this.ProductService, this.$q, <Product> product);
50         return newProduct;
51     };
52
53     public createService = (service:Service):Service => {
54         let newService:Service = new Service(this.ServiceService, this.$q, <Service> service);
55         return newService;
56     };
57
58     public createResource = (resource:Resource):Resource => {
59         let newResource:Resource = new Resource(this.ResourceService, this.$q, <Resource> resource);
60         return newResource;
61     };
62
63     public createFromCsarComponent = (csar:ICsarComponent):Component => {
64         let newResource:Resource = <Resource>this.createEmptyComponent(ComponentType.RESOURCE);
65         newResource.name = csar.vspName;
66
67         /**
68          * Onboarding CSAR contains category and sub category that are uniqueId.
69          * Need to find the category and sub category and extract the name from them.
70          * First concat all sub categories to one array.
71          * Then find the selected sub category and category.
72          * @type {any}
73          */
74         let availableCategories = angular.copy(this.cacheService.get('resourceCategories'));
75         let allSubs = [];
76         _.each(availableCategories, (main:IMainCategory)=> {
77             if (main.subcategories) {
78                 allSubs = allSubs.concat(main.subcategories);
79             }
80         });
81
82         let selectedCategory:IMainCategory = _.find(availableCategories, function (main:IMainCategory) {
83             return main.uniqueId === csar.category;
84         });
85
86         let selectedSubCategory:ISubCategory = _.find(allSubs, (sub:ISubCategory)=> {
87             return sub.uniqueId === csar.subCategory;
88         });
89
90         // Build the categories and sub categories array (same format as component category)
91         let categories:Array<IMainCategory> = new Array();
92         let subcategories:Array<ISubCategory> = new Array();
93         if (selectedCategory && selectedSubCategory) {
94             subcategories.push(selectedSubCategory);
95             selectedCategory.subcategories = subcategories;
96             categories.push(selectedCategory);
97         }
98
99         // Fill the component with details from CSAR
100         newResource.selectedCategory = selectedCategory && selectedSubCategory ? selectedCategory.name + "_#_" + selectedSubCategory.name : '';
101         newResource.categories = categories;
102         newResource.vendorName = csar.vendorName;
103         newResource.vendorRelease = csar.vendorRelease;
104         newResource.csarUUID = csar.packageId;
105         newResource.csarPackageType = csar.packageType;
106         newResource.csarVersion = csar.version;
107         newResource.packageId = csar.packageId;
108         newResource.description = csar.description;
109         newResource.filterTerm = newResource.name +  ' '  + newResource.description + ' ' + newResource.vendorName + ' ' + newResource.csarVersion
110         return newResource;
111     };
112
113     public createEmptyComponent = (componentType:string):Component => {
114         let newComponent:Component;
115
116         switch (componentType) {
117
118             case ComponentType.SERVICE:
119                 newComponent = new Service(this.ServiceService, this.$q);
120                 break;
121
122             case ComponentType.RESOURCE:
123             case ResourceType.VF:
124             case ResourceType.VL:
125             case ResourceType.VFC:
126             case ResourceType.CP:
127                 newComponent = new Resource(this.ResourceService, this.$q);
128                 break;
129
130             case ComponentType.PRODUCT:
131                 newComponent = new Product(this.ProductService, this.$q);
132                 break;
133         }
134         newComponent.componentType = componentType;
135         newComponent.tags = [];
136         newComponent.icon = DEFAULT_ICON;
137         return newComponent;
138     };
139
140     public getComponentFromServer = (componentType:string, componentId:string):ng.IPromise<Component> => {
141         let newComponent:Component = this.createEmptyComponent(componentType);
142         newComponent.setUniqueId(componentId);
143         return newComponent.getComponent();
144     };
145
146     public createComponentOnServer = (componentObject:Component):ng.IPromise<Component> => {
147         let component:Component = this.createComponent(componentObject);
148         return component.createComponentOnServer();
149
150     };
151
152     public getComponentWithMetadataFromServer = (componentType:string, componentId:string):ng.IPromise<Component> => {
153         let deferred = this.$q.defer();
154         let component = this.createEmptyComponent(componentType);
155         component.setUniqueId(componentId);
156         this.ComponentServiceNg2.getComponentMetadata(component).subscribe((response:ComponentGenericResponse) => {
157             component.setComponentMetadata(response.metadata);
158             deferred.resolve(component);
159         });
160         return deferred.promise;
161     }
162 }