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 / interfaces / InterfacesResource.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.interfaces;
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.Produces;
20 import javax.ws.rs.core.MediaType;
21 import javax.ws.rs.core.Response;
22 import javax.ws.rs.core.Response.Status;
23
24 import org.apache.commons.lang3.StringUtils;
25 import org.eclipse.winery.model.tosca.TInterface;
26 import org.eclipse.winery.repository.backend.BackendUtils;
27 import org.eclipse.winery.repository.resources._support.IPersistable;
28 import org.eclipse.winery.repository.resources._support.collections.withid.EntityWithIdCollectionResource;
29 import org.eclipse.winery.repository.resources.entitytypes.TopologyGraphElementEntityTypeResource;
30 import org.eclipse.winery.repository.resources.entitytypes.relationshiptypes.RelationshipTypeResource;
31 import org.restdoc.annotations.RestDoc;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 import com.sun.jersey.api.view.Viewable;
36
37 public class InterfacesResource extends EntityWithIdCollectionResource<InterfaceResource, TInterface> {
38         
39         private static final Logger logger = LoggerFactory.getLogger(InterfacesResource.class);
40         
41         private TopologyGraphElementEntityTypeResource typeResource;
42         
43         private String urlPrefix;
44         
45         
46         public InterfacesResource(IPersistable res, List<TInterface> list) {
47                 super(InterfaceResource.class, TInterface.class, list, res);
48         }
49         
50         /**
51          * @param urlPrefix prefix to be prepended to the URL.
52          *            "source"|"target"|null. E.g., "source" for "sourceinterfaces"
53          */
54         public InterfacesResource(String urlPrefix, List<TInterface> list, IPersistable typeResource) {
55                 super(InterfaceResource.class, TInterface.class, list, typeResource);
56                 this.urlPrefix = urlPrefix;
57                 this.typeResource = (TopologyGraphElementEntityTypeResource) typeResource;
58         }
59         
60         @Override
61         public Viewable getHTML() {
62                 return new Viewable("/jsp/interfaces/interfaces.jsp", this);
63         }
64         
65         /**
66          * Implementation base: <br />
67          * {@link org.eclipse.winery.repository.resources.AbstractComponentResource.
68          * onPost(String)}
69          * 
70          * @return entity: id of the stored interface
71          */
72         @POST
73         @RestDoc(methodDescription = "Creates a new interface. Returns conflict if interface already exists")
74         @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
75         @Produces(MediaType.TEXT_PLAIN)
76         public Response onPost(@FormParam("interfaceName") String interfaceName) {
77                 if (StringUtils.isEmpty(interfaceName)) {
78                         return Response.status(Status.BAD_REQUEST).entity("null interfaceName").build();
79                 }
80                 
81                 TInterface iface = new TInterface();
82                 iface.setName(interfaceName);
83                 
84                 // check for duplicates
85                 // return "conflict" if interface already exists
86                 if (this.alreadyContains(iface)) {
87                         return Response.status(Status.CONFLICT).build();
88                 }
89                 
90                 this.list.add(iface);
91                 return BackendUtils.persist(this.res);
92         }
93         
94         /**
95          * Required by interfaces.jsp
96          */
97         public String getUrlPrefix() {
98                 return this.urlPrefix;
99         }
100         
101         @Override
102         public String getId(TInterface entity) {
103                 return entity.getName();
104         }
105         
106         /**
107          * @return the namespace of the node/relationship type
108          */
109         public String getNamespace() {
110                 return this.typeResource.getId().getNamespace().getDecoded();
111         }
112         
113         /**
114          * @return the name of the node/relationship type
115          */
116         public String getName() {
117                 return this.typeResource.getName();
118         }
119         
120         public String getRelationshipTypeOrNodeTypeURLFragment() {
121                 if (this.typeResource instanceof RelationshipTypeResource) {
122                         return "relationshiptype";
123                 } else {
124                         return "nodetype";
125                 }
126         }
127         
128         public String getRelationshipTypeOrNodeType() {
129                 if (this.typeResource instanceof RelationshipTypeResource) {
130                         return "Relationship Type";
131                 } else {
132                         return "Node Type";
133                 }
134         }
135         
136         public String getTypeQName() {
137                 String res = this.typeResource.getId().getQName().toString();
138                 return res;
139         }
140 }