dc6ad94f1c2f18f8dc7e0c61f8fc7b71d948bda9
[vfc/nfvo/wfengine.git] /
1 /*******************************************************************************
2  * Copyright (c) 2014 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.servicetemplates.boundarydefinitions.reqscaps;
13
14 import java.util.List;
15
16 import javax.ws.rs.Consumes;
17 import javax.ws.rs.FormParam;
18 import javax.ws.rs.POST;
19 import javax.ws.rs.core.MediaType;
20 import javax.ws.rs.core.Response;
21 import javax.ws.rs.core.Response.Status;
22
23 import org.eclipse.winery.common.ModelUtilities;
24 import org.eclipse.winery.model.tosca.TRequirement;
25 import org.eclipse.winery.model.tosca.TRequirementRef;
26 import org.eclipse.winery.repository.resources._support.IPersistable;
27 import org.eclipse.winery.repository.resources._support.collections.CollectionsHelper;
28 import org.eclipse.winery.repository.resources._support.collections.withoutid.EntityWithoutIdCollectionResource;
29 import org.eclipse.winery.repository.resources.servicetemplates.ServiceTemplateResource;
30
31 import com.sun.jersey.api.view.Viewable;
32
33 /**
34  * This class is mirrored at
35  * {@link org.eclipse.winery.repository.resources.servicetemplates.boundarydefinitions.reqscaps.CapabilitiesResource}
36  */
37 public class RequirementsResource extends EntityWithoutIdCollectionResource<RequirementResource, TRequirementRef> {
38         
39         public RequirementsResource(IPersistable res, List<TRequirementRef> refs) {
40                 super(RequirementResource.class, TRequirementRef.class, refs, res);
41         }
42         
43         @Override
44         public Viewable getHTML() {
45                 throw new IllegalStateException("Not yet required: boundarydefinitions.jsp renders all tab content.");
46         }
47         
48         /**
49          * Adds an element using form-encoding
50          * 
51          * This is necessary as TRequirementRef contains an IDREF and the XML
52          * snippet itself does not contain the target id
53          * 
54          * @param name the optional name of the requirement
55          * @param reference the reference to a requirement in the topology
56          */
57         @POST
58         @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
59         public Response addNewElement(@FormParam("name") String name, @FormParam("ref") String reference) {
60                 // Implementation adapted from super addNewElement
61                 
62                 if (reference == null) {
63                         return Response.status(Status.BAD_REQUEST).entity("A reference has to be provided").build();
64                 }
65                 
66                 TRequirementRef ref = new TRequirementRef();
67                 ref.setName(name); // may also be null
68                 
69                 // The XML model forces us to put a reference to the object and not just the string
70                 ServiceTemplateResource rs = (ServiceTemplateResource) this.res;
71                 TRequirement resolved = ModelUtilities.resolveRequirement(rs.getServiceTemplate(), reference);
72                 // In case nothing was found: report back to the user
73                 if (resolved == null) {
74                         return Response.status(Status.BAD_REQUEST).entity("Reference could not be resolved").build();
75                 }
76                 
77                 ref.setRef(resolved);
78                 
79                 // "this.alreadyContains(ref)" cannot be called as this leads to a mappable exception: The data does not contain an id where the given ref attribute may point to
80                 
81                 this.list.add(ref);
82                 return CollectionsHelper.persist(this.res, this, ref);
83         }
84         
85 }