Add winery source code
[vfc/nfvo/wfengine.git] / winery / org.eclipse.winery.repository / src / main / java / org / eclipse / winery / repository / resources / entitytypeimplementations / nodetypeimplementations / NodeTypeImplementationResource.java
1 /*******************************************************************************
2  * Copyright (c) 2012-2013 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.entitytypeimplementations.nodetypeimplementations;
13
14 import javax.ws.rs.Path;
15 import javax.ws.rs.core.Response;
16 import javax.xml.namespace.QName;
17
18 import org.eclipse.winery.common.ids.definitions.NodeTypeImplementationId;
19 import org.eclipse.winery.model.tosca.TDeploymentArtifacts;
20 import org.eclipse.winery.model.tosca.TExtensibleElements;
21 import org.eclipse.winery.model.tosca.TImplementationArtifacts;
22 import org.eclipse.winery.model.tosca.TNodeTypeImplementation;
23 import org.eclipse.winery.repository.backend.BackendUtils;
24 import org.eclipse.winery.repository.resources.INodeTemplateResourceOrNodeTypeImplementationResource;
25 import org.eclipse.winery.repository.resources.INodeTypeImplementationResourceOrRelationshipTypeImplementationResource;
26 import org.eclipse.winery.repository.resources.artifacts.DeploymentArtifactsResource;
27 import org.eclipse.winery.repository.resources.artifacts.ImplementationArtifactsResource;
28 import org.eclipse.winery.repository.resources.entitytypeimplementations.EntityTypeImplementationResource;
29
30 public class NodeTypeImplementationResource extends EntityTypeImplementationResource implements INodeTemplateResourceOrNodeTypeImplementationResource, INodeTypeImplementationResourceOrRelationshipTypeImplementationResource {
31         
32         public NodeTypeImplementationResource(NodeTypeImplementationId id) {
33                 super(id);
34         }
35         
36         /**
37          * public because of exporter
38          */
39         public TNodeTypeImplementation getNTI() {
40                 return (TNodeTypeImplementation) this.getElement();
41         }
42         
43         /**
44          * Even if both node type implementations and relationship type
45          * implementations have implementation artifacts, there is no common
46          * supertype. To avoid endless casts, we just implement the method here
47          * 
48          * @return
49          */
50         @Path("implementationartifacts/")
51         public ImplementationArtifactsResource getImplementationArtifacts() {
52                 TImplementationArtifacts implementationArtifacts;
53                 implementationArtifacts = this.getNTI().getImplementationArtifacts();
54                 if (implementationArtifacts == null) {
55                         implementationArtifacts = new TImplementationArtifacts();
56                         this.getNTI().setImplementationArtifacts(implementationArtifacts);
57                 }
58                 return new ImplementationArtifactsResource(implementationArtifacts.getImplementationArtifact(), this);
59         }
60         
61         /**
62          * Only NodeTypes have deployment artifacts, not RelationshipType.
63          * Therefore, this method is declared in
64          * {@link NodeTypeImplementationResource} and not in
65          * {@link EntityTypeImplementationResource}
66          */
67         @Path("deploymentartifacts/")
68         public DeploymentArtifactsResource getDeploymentArtifacts() {
69                 TDeploymentArtifacts deploymentArtifacts;
70                 deploymentArtifacts = this.getNTI().getDeploymentArtifacts();
71                 if (deploymentArtifacts == null) {
72                         deploymentArtifacts = new TDeploymentArtifacts();
73                         this.getNTI().setDeploymentArtifacts(deploymentArtifacts);
74                 }
75                 return new DeploymentArtifactsResource(deploymentArtifacts.getDeploymentArtifact(), this);
76         }
77         
78         @Override
79         protected TExtensibleElements createNewElement() {
80                 return new TNodeTypeImplementation();
81         }
82         
83         @Override
84         protected void copyIdToFields() {
85                 this.getNTI().setTargetNamespace(this.getId().getNamespace().getDecoded());
86                 this.getNTI().setName(this.getId().getXmlId().getDecoded());
87         }
88         
89         @Override
90         public QName getType() {
91                 return this.getNTI().getNodeType();
92         }
93         
94         @Override
95         public Response setType(QName type) {
96                 this.getNTI().setNodeType(type);
97                 return BackendUtils.persist(this);
98         }
99         
100         @Override
101         public Response setType(String typeStr) {
102                 QName type = QName.valueOf(typeStr);
103                 return this.setType(type);
104         }
105 }