Merge "Fix build errors in autorelease full clean build"
[vfc/nfvo/wfengine.git] / winery / org.eclipse.winery.repository / src / main / java / org / eclipse / winery / repository / resources / servicetemplates / boundarydefinitions / reqscaps / CapabilitiesResource.java
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.TCapability;
25 import org.eclipse.winery.model.tosca.TCapabilityRef;
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 an adaption from
35  * {@link org.eclipse.winery.repository.resources.servicetemplates.boundarydefinitions.reqscaps.RequirementsResource}
36  */
37 public class CapabilitiesResource extends EntityWithoutIdCollectionResource<CapabilityResource, TCapabilityRef> {
38         
39         public CapabilitiesResource(IPersistable res, List<TCapabilityRef> refs) {
40                 super(CapabilityResource.class, TCapabilityRef.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         @POST
49         @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
50         public Response addNewElement(@FormParam("name") String name, @FormParam("ref") String reference) {
51                 // Implementation adapted from super addNewElement
52                 
53                 if (reference == null) {
54                         return Response.status(Status.BAD_REQUEST).entity("A reference has to be provided").build();
55                 }
56                 
57                 TCapabilityRef ref = new TCapabilityRef();
58                 ref.setName(name); // may also be null
59                 
60                 // The XML model fordces us to put a reference to the object and not just the string
61                 ServiceTemplateResource rs = (ServiceTemplateResource) this.res;
62                 TCapability resolved = ModelUtilities.resolveCapability(rs.getServiceTemplate(), reference);
63                 // In case nothing was found: report back to the user
64                 if (resolved == null) {
65                         return Response.status(Status.BAD_REQUEST).entity("Reference could not be resolved").build();
66                 }
67                 
68                 ref.setRef(resolved);
69                 
70                 // "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
71                 
72                 this.list.add(ref);
73                 return CollectionsHelper.persist(this.res, this, ref);
74         }
75         
76 }