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.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;
31 import com.sun.jersey.api.view.Viewable;
34 * This class is mirrored at
35 * {@link org.eclipse.winery.repository.resources.servicetemplates.boundarydefinitions.reqscaps.CapabilitiesResource}
37 public class RequirementsResource extends EntityWithoutIdCollectionResource<RequirementResource, TRequirementRef> {
39 public RequirementsResource(IPersistable res, List<TRequirementRef> refs) {
40 super(RequirementResource.class, TRequirementRef.class, refs, res);
44 public Viewable getHTML() {
45 throw new IllegalStateException("Not yet required: boundarydefinitions.jsp renders all tab content.");
49 * Adds an element using form-encoding
51 * This is necessary as TRequirementRef contains an IDREF and the XML
52 * snippet itself does not contain the target id
54 * @param name the optional name of the requirement
55 * @param reference the reference to a requirement in the topology
58 @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
59 public Response addNewElement(@FormParam("name") String name, @FormParam("ref") String reference) {
60 // Implementation adapted from super addNewElement
62 if (reference == null) {
63 return Response.status(Status.BAD_REQUEST).entity("A reference has to be provided").build();
66 TRequirementRef ref = new TRequirementRef();
67 ref.setName(name); // may also be null
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();
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
82 return CollectionsHelper.persist(this.res, this, ref);