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
10 * Oliver Kopp - initial API and implementation
11 *******************************************************************************/
12 package org.eclipse.winery.repository.resources.entitytypes.relationshiptypes;
14 import java.util.Collection;
15 import java.util.SortedSet;
17 import javax.ws.rs.Consumes;
18 import javax.ws.rs.GET;
19 import javax.ws.rs.PUT;
20 import javax.ws.rs.Path;
21 import javax.ws.rs.Produces;
22 import javax.ws.rs.core.MediaType;
23 import javax.ws.rs.core.Response;
24 import javax.xml.namespace.QName;
26 import org.eclipse.winery.model.tosca.TExtensibleElements;
27 import org.eclipse.winery.model.tosca.TRelationshipType;
28 import org.eclipse.winery.model.tosca.TRelationshipType.SourceInterfaces;
29 import org.eclipse.winery.model.tosca.TRelationshipType.TargetInterfaces;
30 import org.eclipse.winery.model.tosca.TRelationshipType.ValidSource;
31 import org.eclipse.winery.model.tosca.TRelationshipType.ValidTarget;
32 import org.eclipse.winery.model.tosca.TTopologyElementInstanceStates;
33 import org.eclipse.winery.common.ids.definitions.NodeTypeId;
34 import org.eclipse.winery.common.ids.definitions.RelationshipTypeId;
35 import org.eclipse.winery.repository.backend.BackendUtils;
36 import org.eclipse.winery.repository.backend.Repository;
37 import org.eclipse.winery.repository.resources.entitytypes.InstanceStatesResource;
38 import org.eclipse.winery.repository.resources.entitytypes.TopologyGraphElementEntityTypeResource;
39 import org.eclipse.winery.repository.resources.interfaces.InterfacesResource;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
43 import com.sun.jersey.api.view.Viewable;
45 public class RelationshipTypeResource extends TopologyGraphElementEntityTypeResource {
47 private static final Logger logger = LoggerFactory.getLogger(RelationshipTypeResource.class);
50 public RelationshipTypeResource(RelationshipTypeId id) {
54 @Path("implementations/")
55 public ImplementationsOfOneRelationshipTypeResource getImplementations() {
56 return new ImplementationsOfOneRelationshipTypeResource((RelationshipTypeId) this.id);
59 @Path("visualappearance/")
60 public VisualAppearanceResource getVisualAppearanceResource() {
61 return new VisualAppearanceResource(this, this.getElement().getOtherAttributes(), (RelationshipTypeId) this.id);
64 @Path("instancestates/")
65 public InstanceStatesResource getInstanceStatesResource() {
66 TTopologyElementInstanceStates instanceStates = this.getRelationshipType().getInstanceStates();
67 if (instanceStates == null) {
68 // if an explicit (empty) list does not exist, create it
69 instanceStates = new TTopologyElementInstanceStates();
70 this.getRelationshipType().setInstanceStates(instanceStates);
72 return new InstanceStatesResource(this.getRelationshipType().getInstanceStates(), this);
75 @Path("sourceinterfaces/")
76 public InterfacesResource getSourceInterfaces() {
77 SourceInterfaces interfaces = this.getRelationshipType().getSourceInterfaces();
78 if (interfaces == null) {
79 interfaces = new SourceInterfaces();
80 this.getRelationshipType().setSourceInterfaces(interfaces);
82 return new InterfacesResource("source", interfaces.getInterface(), this);
85 @Path("targetinterfaces/")
86 public InterfacesResource getTargetInterfaces() {
87 TargetInterfaces interfaces = this.getRelationshipType().getTargetInterfaces();
88 if (interfaces == null) {
89 interfaces = new TargetInterfaces();
90 this.getRelationshipType().setTargetInterfaces(interfaces);
92 return new InterfacesResource("target", interfaces.getInterface(), this);
95 @Path("validendings/")
97 @Produces(MediaType.TEXT_HTML)
98 public Response getHTML() {
99 Viewable viewable = new Viewable("/jsp/entitytypes/relationshiptypes/validendings.jsp", this);
100 return Response.ok().entity(viewable).build();
105 public String getValidSource() {
106 ValidSource validSource;
107 if (((validSource = this.getRelationshipType().getValidSource()) == null) || (validSource.getTypeRef() == null)) {
110 return this.getRelationshipType().getValidSource().getTypeRef().toString();
115 @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
116 public Response setValidSource(String typeRef) {
117 ValidSource vs = new ValidSource();
118 QName qname = QName.valueOf(typeRef);
119 vs.setTypeRef(qname);
120 this.getRelationshipType().setValidSource(vs);
121 return BackendUtils.persist(this);
126 public String getValidTarget() {
127 ValidTarget validTarget;
128 if (((validTarget = this.getRelationshipType().getValidTarget()) == null) || (validTarget.getTypeRef() == null)) {
131 return this.getRelationshipType().getValidTarget().getTypeRef().toString();
136 @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
137 public Response setValidTarget(String typeRef) {
138 ValidTarget vt = new ValidTarget();
139 QName qname = QName.valueOf(typeRef);
140 vt.setTypeRef(qname);
141 this.getRelationshipType().setValidTarget(vt);
142 return BackendUtils.persist(this);
146 * Required for validendings.jsp
148 public Collection<NodeTypeId> getPossibleValidEndings() {
149 SortedSet<NodeTypeId> allNodeTypeIds = Repository.INSTANCE.getAllTOSCAComponentIds(NodeTypeId.class);
150 return allNodeTypeIds;
154 * Convenience method to avoid casting at the caller's side.
156 public TRelationshipType getRelationshipType() {
157 return (TRelationshipType) this.getElement();
161 protected TExtensibleElements createNewElement() {
162 return new TRelationshipType();