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";
10 export class ComponentFactory {
13 'Sdc.Services.Components.ResourceService',
14 'Sdc.Services.Components.ServiceService',
15 'Sdc.Services.Components.ProductService',
16 'Sdc.Services.CacheService',
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) {
29 public createComponent = (component:Component):Component => {
30 let newComponent:Component;
31 switch (component.componentType) {
34 newComponent = new Service(this.ServiceService, this.$q, <Service> component);
38 newComponent = new Resource(this.ResourceService, this.$q, <Resource> component);
42 newComponent = new Product(this.ProductService, this.$q, <Product> component);
48 public createProduct = (product:Product):Product => {
49 let newProduct:Product = new Product(this.ProductService, this.$q, <Product> product);
53 public createService = (service:Service):Service => {
54 let newService:Service = new Service(this.ServiceService, this.$q, <Service> service);
58 public createResource = (resource:Resource):Resource => {
59 let newResource:Resource = new Resource(this.ResourceService, this.$q, <Resource> resource);
63 public createFromCsarComponent = (csar:ICsarComponent):Component => {
64 let newResource:Resource = <Resource>this.createEmptyComponent(ComponentType.RESOURCE);
65 newResource.name = csar.vspName;
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.
74 let availableCategories = angular.copy(this.cacheService.get('resourceCategories'));
76 _.each(availableCategories, (main:IMainCategory)=> {
77 if (main.subcategories) {
78 allSubs = allSubs.concat(main.subcategories);
82 let selectedCategory:IMainCategory = _.find(availableCategories, function (main:IMainCategory) {
83 return main.uniqueId === csar.category;
86 let selectedSubCategory:ISubCategory = _.find(allSubs, (sub:ISubCategory)=> {
87 return sub.uniqueId === csar.subCategory;
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);
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
113 public createEmptyComponent = (componentType:string):Component => {
114 let newComponent:Component;
116 switch (componentType) {
118 case ComponentType.SERVICE:
119 newComponent = new Service(this.ServiceService, this.$q);
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);
130 case ComponentType.PRODUCT:
131 newComponent = new Product(this.ProductService, this.$q);
134 newComponent.componentType = componentType;
135 newComponent.tags = [];
136 newComponent.icon = DEFAULT_ICON;
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();
146 public createComponentOnServer = (componentObject:Component):ng.IPromise<Component> => {
147 let component:Component = this.createComponent(componentObject);
148 return component.createComponentOnServer();
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);
160 return deferred.promise;