Add winery source code
[vfc/nfvo/wfengine.git] / winery / org.eclipse.winery.repository / src / main / java / org / eclipse / winery / repository / resources / artifacts / ImplementationArtifactsResource.java
1 /*******************************************************************************
2  * Copyright (c) 2012-2013,2015 University of Stuttgart.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * and the Apache License 2.0 which both accompany this distribution,
6  * and are available at http://www.eclipse.org/legal/epl-v10.html
7  * and http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Contributors:
10  *     Oliver Kopp - initial API and implementation
11  *******************************************************************************/
12 package org.eclipse.winery.repository.resources.artifacts;
13
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.List;
17
18 import javax.xml.namespace.QName;
19
20 import org.eclipse.winery.model.tosca.TImplementationArtifacts.ImplementationArtifact;
21 import org.eclipse.winery.repository.resources.INodeTypeImplementationResourceOrRelationshipTypeImplementationResource;
22 import org.eclipse.winery.repository.resources.entitytypeimplementations.nodetypeimplementations.NodeTypeImplementationResource;
23 import org.eclipse.winery.repository.resources.entitytypeimplementations.relationshiptypeimplementations.RelationshipTypeImplementationResource;
24 import org.eclipse.winery.repository.resources.entitytypes.nodetypes.NodeTypeResource;
25 import org.eclipse.winery.repository.resources.entitytypes.nodetypes.NodeTypesResource;
26 import org.eclipse.winery.repository.resources.entitytypes.relationshiptypes.RelationshipTypeResource;
27 import org.eclipse.winery.repository.resources.entitytypes.relationshiptypes.RelationshipTypesResource;
28 import org.eclipse.winery.repository.resources.interfaces.InterfaceResource;
29
30 /**
31  * ImplementationArtifact instead of TImplementationArtifact has to be used
32  * because of difference in the XSD at tImplementationArtifacts vs.
33  * tDeploymentArtifacts
34  */
35 public class ImplementationArtifactsResource extends GenericArtifactsResource<ImplementationArtifactResource, ImplementationArtifact> {
36         
37         private List<ImplementationArtifact> implementationArtifacts;
38         
39         
40         public ImplementationArtifactsResource(List<ImplementationArtifact> implementationArtifact, INodeTypeImplementationResourceOrRelationshipTypeImplementationResource res) {
41                 super(ImplementationArtifactResource.class, ImplementationArtifact.class, implementationArtifact, res);
42                 this.implementationArtifacts = implementationArtifact;
43         }
44         
45         /**
46          * @return a cast to TNodeTypeImplementationResource of the parent of this
47          *         resource.
48          */
49         protected NodeTypeImplementationResource getNTI() {
50                 return (NodeTypeImplementationResource) this.res;
51         }
52         
53         /**
54          * @return a cast to TNodeTypeImplementationResource of the parent of this
55          *         resource.
56          */
57         protected RelationshipTypeImplementationResource getRTI() {
58                 return (RelationshipTypeImplementationResource) this.res;
59         }
60         
61         @Override
62         public Collection<ImplementationArtifactResource> getAllArtifactResources() {
63                 Collection<ImplementationArtifactResource> res = new ArrayList<ImplementationArtifactResource>(this.implementationArtifacts.size());
64                 for (ImplementationArtifact da : this.implementationArtifacts) {
65                         ImplementationArtifactResource r = new ImplementationArtifactResource(da, this.implementationArtifacts, this.res);
66                         res.add(r);
67                 }
68                 return res;
69         }
70         
71         /** required by artifacts.jsp **/
72         public List<InterfaceResource> getInterfacesOfAssociatedType() {
73                 boolean isNodeTypeImplementation = this.res instanceof NodeTypeImplementationResource;
74                 QName type;
75                 List<InterfaceResource> interfaces = new ArrayList<InterfaceResource>();
76                 if (isNodeTypeImplementation) {
77                         type = this.getNTI().getType();
78                         NodeTypeResource typeResource = (NodeTypeResource) new NodeTypesResource().getComponentInstaceResource(type);
79                         interfaces.addAll(typeResource.getInterfaces().getAllEntityResources());
80                 } else {
81                         type = this.getRTI().getType();
82                         RelationshipTypeResource typeResource = (RelationshipTypeResource) new RelationshipTypesResource().getComponentInstaceResource(type);
83                         interfaces.addAll(typeResource.getSourceInterfaces().getAllEntityResources());
84                         interfaces.addAll(typeResource.getTargetInterfaces().getAllEntityResources());
85                 }
86                 return interfaces;
87         }
88         
89         @Override
90         public String getId(ImplementationArtifact entity) {
91                 return entity.getName();
92         }
93 }