0fd3bd3d3083eda1f628ed8eeaa183e373091b14
[vfc/nfvo/wfengine.git] / winery / org.eclipse.winery.repository / src / main / java / org / eclipse / winery / repository / resources / entitytypes / nodetypes / reqandcapdefs / CapabilityDefinitionResource.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.entitytypes.nodetypes.reqandcapdefs;
13
14 import java.util.List;
15
16 import javax.ws.rs.FormParam;
17 import javax.ws.rs.PUT;
18 import javax.ws.rs.Path;
19 import javax.ws.rs.core.Response;
20 import javax.xml.namespace.QName;
21
22 import org.eclipse.winery.model.tosca.TCapabilityDefinition;
23 import org.eclipse.winery.model.tosca.TCapabilityDefinition.Constraints;
24 import org.eclipse.winery.model.tosca.TConstraint;
25 import org.eclipse.winery.repository.backend.BackendUtils;
26 import org.eclipse.winery.repository.resources.AbstractComponentInstanceResource;
27 import org.eclipse.winery.repository.resources._support.IPersistable;
28 import org.eclipse.winery.repository.resources._support.collections.IIdDetermination;
29 import org.eclipse.winery.repository.resources.entitytypes.nodetypes.NodeTypeResource;
30
31 /**
32  * Implementation similar to RequirementDefinitionResource, but with
33  * TCapabilityDefinition instead of TRequirementDefinition
34  */
35 public final class CapabilityDefinitionResource extends AbstractReqOrCapDefResource<TCapabilityDefinition> {
36         
37         private TCapabilityDefinition capDef;
38         
39         
40         /**
41          * Constructor has to follow the pattern of EnetityTResource as the
42          * constructor is invoked by reflection in EntityWithIdcollectionResource
43          * 
44          * @param res the resource this req def is nested in. Has to be of Type
45          *            "NodeTypeResource". Due to the implementation of
46          *            org.eclipse.winery .repository.resources._support.collections.
47          *            withid.EntityWithIdCollectionResource
48          *            .getEntityResourceInstance(EntityT, int), we have to use
49          *            "AbstractComponentInstanceResource" as type
50          */
51         public CapabilityDefinitionResource(IIdDetermination<TCapabilityDefinition> idDetermination, TCapabilityDefinition capDef, int idx, List<TCapabilityDefinition> list, AbstractComponentInstanceResource res) {
52                 super(idDetermination, capDef, idx, list, (NodeTypeResource) res, CapabilityDefinitionResource.getConstraints(capDef));
53                 this.capDef = capDef;
54         }
55         
56         /**
57          * Quick hack to avoid internal server error
58          */
59         public CapabilityDefinitionResource(IIdDetermination<TCapabilityDefinition> idDetermination, TCapabilityDefinition capDef, int idx, List<TCapabilityDefinition> list, IPersistable res) {
60                 this(idDetermination, capDef, idx, list, (AbstractComponentInstanceResource) res);
61         }
62         
63         /**
64          * Fetch the list of constraints from the given definition. If the list does
65          * not exist, the list is created an stored in the given capDef
66          */
67         public static List<TConstraint> getConstraints(TCapabilityDefinition capDef) {
68                 Constraints constraints = capDef.getConstraints();
69                 if (constraints == null) {
70                         constraints = new Constraints();
71                         capDef.setConstraints(constraints);
72                 }
73                 return constraints.getConstraint();
74         }
75         
76         public QName getType() {
77                 return this.capDef.getCapabilityType();
78         }
79         
80         @PUT
81         @Path("type")
82         public Response setType(@FormParam(value = "type") String value) {
83                 QName qname = QName.valueOf(value);
84                 this.capDef.setCapabilityType(qname);
85                 return BackendUtils.persist(this.parent);
86         }
87         
88         @Override
89         public String getId(TCapabilityDefinition e) {
90                 return e.getName();
91         }
92 }