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, ServiceSubstitutionInstance, 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);
39 case ComponentType.SERVICE_SUBSTITUTION:
40 newComponentInstance = new ServiceSubstitutionInstance(componentInstance);
43 newComponentInstance = new ResourceInstance(componentInstance);
46 return newComponentInstance;
49 static createEmptyComponentInstance = (componentInstanceType?: string): ComponentInstance => {
50 let newComponentInstance: ComponentInstance;
51 switch (componentInstanceType) {
52 case ComponentType.SERVICE:
53 newComponentInstance = new ServiceInstance();
55 case ComponentType.SERVICE_PROXY:
56 newComponentInstance = new ServiceProxyInstance();
58 case ComponentType.SERVICE_SUBSTITUTION:
59 newComponentInstance = new ServiceSubstitutionInstance();
62 newComponentInstance = new ResourceInstance();
65 return newComponentInstance;
68 static createComponentInstanceFromComponent = (component: LeftPaletteComponent, useServiceSubstitutionForNestedServices?: boolean): ComponentInstance => {
69 const newComponentInstance: ComponentInstance = ComponentInstanceFactory.createEmptyComponentInstance(component.componentType);
70 newComponentInstance.uniqueId = component.uniqueId + (new Date()).getTime();
71 newComponentInstance.posX = 0;
72 newComponentInstance.posY = 0;
73 newComponentInstance.name = component.name;
74 newComponentInstance.componentVersion = component.version;
75 newComponentInstance.originType = getOriginType(component);
76 newComponentInstance.requirements = component.requirements;
77 newComponentInstance.capabilities = component.capabilities;
78 newComponentInstance.icon = component.icon;
79 newComponentInstance.componentUid = component.uniqueId;
80 return newComponentInstance;
82 function getOriginType(component: LeftPaletteComponent): string {
83 if (component.componentSubType) {
84 return component.componentSubType;
86 if (component.componentType === ComponentType.SERVICE) {
87 if (useServiceSubstitutionForNestedServices){
88 return ComponentType.SERVICE_SUBSTITUTION;
90 return ComponentType.SERVICE_PROXY;
93 return component.resourceType;