Catalog alignment
[sdc.git] / catalog-ui / src / app / utils / component-instance-factory.ts
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 /**
21  * Created by obarda on 3/7/2016.
22  */
23 'use strict';
24 import { ComponentType } from 'app/utils';
25 import { Component, ComponentInstance, ResourceInstance, ServiceInstance, ServiceProxyInstance } from '../models';
26 import { LeftPaletteComponent } from '../models/components/displayComponent';
27
28 export class ComponentInstanceFactory {
29
30     static createComponentInstance(componentInstance: ComponentInstance): ComponentInstance {
31         let newComponentInstance: ComponentInstance;
32         switch (componentInstance.originType) {
33             case ComponentType.SERVICE:
34                 newComponentInstance = new ServiceInstance(componentInstance);
35                 break;
36            case ComponentType.SERVICE_PROXY:
37                 newComponentInstance = new ServiceProxyInstance(componentInstance);
38                 break;
39             default :
40                 newComponentInstance = new ResourceInstance(componentInstance);
41                 break;
42         }
43         return newComponentInstance;
44     }
45
46     static createEmptyComponentInstance = (componentInstanceType?: string): ComponentInstance => {
47         let newComponentInstance: ComponentInstance;
48         switch (componentInstanceType) {
49             case ComponentType.SERVICE:
50                 newComponentInstance = new ServiceInstance();
51                 break;
52             case ComponentType.SERVICE_PROXY:
53                 newComponentInstance = new ServiceProxyInstance();
54                 break;
55             default :
56                 newComponentInstance = new ResourceInstance();
57                 break;
58         }
59         return newComponentInstance;
60     }
61
62     static createComponentInstanceFromComponent = (component: LeftPaletteComponent): ComponentInstance => {
63         const newComponentInstance: ComponentInstance = ComponentInstanceFactory.createEmptyComponentInstance(component.componentType);
64         newComponentInstance.uniqueId = component.uniqueId + (new Date()).getTime();
65         newComponentInstance.posX = 0;
66         newComponentInstance.posY = 0;
67         newComponentInstance.name = component.name;
68         newComponentInstance.componentVersion = component.version;
69         newComponentInstance.originType = getOriginType(component);
70         newComponentInstance.requirements = component.requirements;
71         newComponentInstance.capabilities = component.capabilities;
72         newComponentInstance.icon = component.icon;
73         newComponentInstance.componentUid = component.uniqueId;
74         return newComponentInstance;
75
76         function getOriginType(component: LeftPaletteComponent): string  {
77             if (component.componentSubType) {
78                 return component.componentSubType;
79             } else {
80                 if (component.componentType === ComponentType.SERVICE) {
81                     return ComponentType.SERVICE_PROXY;
82                 } else {
83                     return component.resourceType;
84                 }
85             }
86         }
87     }
88 }