2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 * Created by obarda on 3/7/2016.
24 import { ComponentType } from 'app/utils';
25 import { Component, ComponentInstance, ResourceInstance, ServiceInstance, ServiceProxyInstance } from '../models';
26 import { LeftPaletteComponent } from '../models/components/displayComponent';
28 export class ComponentInstanceFactory {
30 static createComponentInstance(componentInstance: ComponentInstance): ComponentInstance {
31 let newComponentInstance: ComponentInstance;
32 switch (componentInstance.originType) {
33 case ComponentType.SERVICE:
34 newComponentInstance = new ServiceInstance(componentInstance);
36 case ComponentType.SERVICE_PROXY:
37 newComponentInstance = new ServiceProxyInstance(componentInstance);
40 newComponentInstance = new ResourceInstance(componentInstance);
43 return newComponentInstance;
46 static createEmptyComponentInstance = (componentInstanceType?: string): ComponentInstance => {
47 let newComponentInstance: ComponentInstance;
48 switch (componentInstanceType) {
49 case ComponentType.SERVICE:
50 newComponentInstance = new ServiceInstance();
52 case ComponentType.SERVICE_PROXY:
53 newComponentInstance = new ServiceProxyInstance();
56 newComponentInstance = new ResourceInstance();
59 return newComponentInstance;
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;
76 function getOriginType(component: LeftPaletteComponent): string {
77 if (component.componentSubType) {
78 return component.componentSubType;
80 if (component.componentType === ComponentType.SERVICE) {
81 return ComponentType.SERVICE_PROXY;
83 return component.resourceType;