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