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
10 * Oliver Kopp - initial API and implementation
11 *******************************************************************************/
12 package org.eclipse.winery.repository.resources.servicetemplates.boundarydefinitions.reqscaps;
14 import java.util.List;
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;
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;
31 import com.sun.jersey.api.view.Viewable;
34 * This class is an adaption from
35 * {@link org.eclipse.winery.repository.resources.servicetemplates.boundarydefinitions.reqscaps.RequirementsResource}
37 public class CapabilitiesResource extends EntityWithoutIdCollectionResource<CapabilityResource, TCapabilityRef> {
39 public CapabilitiesResource(IPersistable res, List<TCapabilityRef> refs) {
40 super(CapabilityResource.class, TCapabilityRef.class, refs, res);
44 public Viewable getHTML() {
45 throw new IllegalStateException("Not yet required: boundarydefinitions.jsp renders all tab content.");
49 @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
50 public Response addNewElement(@FormParam("name") String name, @FormParam("ref") String reference) {
51 // Implementation adapted from super addNewElement
53 if (reference == null) {
54 return Response.status(Status.BAD_REQUEST).entity("A reference has to be provided").build();
57 TCapabilityRef ref = new TCapabilityRef();
58 ref.setName(name); // may also be null
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();
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
73 return CollectionsHelper.persist(this.res, this, ref);