aboutsummaryrefslogtreecommitdiffstats
path: root/winery/org.eclipse.winery.repository/src/main/java/org/eclipse/winery/repository/resources/entitytypes/relationshiptypes/RelationshipTypeResource.java
blob: c874bd50188ca6551024f714f1c16d41096beecf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*******************************************************************************
 * Copyright (c) 2012-2013 University of Stuttgart.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and the Apache License 2.0 which both accompany this distribution,
 * and are available at http://www.eclipse.org/legal/epl-v10.html
 * and http://www.apache.org/licenses/LICENSE-2.0
 *
 * Contributors:
 *     Oliver Kopp - initial API and implementation
 *******************************************************************************/
package org.eclipse.winery.repository.resources.entitytypes.relationshiptypes;

import java.util.Collection;
import java.util.SortedSet;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.xml.namespace.QName;

import org.eclipse.winery.model.tosca.TExtensibleElements;
import org.eclipse.winery.model.tosca.TRelationshipType;
import org.eclipse.winery.model.tosca.TRelationshipType.SourceInterfaces;
import org.eclipse.winery.model.tosca.TRelationshipType.TargetInterfaces;
import org.eclipse.winery.model.tosca.TRelationshipType.ValidSource;
import org.eclipse.winery.model.tosca.TRelationshipType.ValidTarget;
import org.eclipse.winery.model.tosca.TTopologyElementInstanceStates;
import org.eclipse.winery.common.ids.definitions.NodeTypeId;
import org.eclipse.winery.common.ids.definitions.RelationshipTypeId;
import org.eclipse.winery.repository.backend.BackendUtils;
import org.eclipse.winery.repository.backend.Repository;
import org.eclipse.winery.repository.resources.entitytypes.InstanceStatesResource;
import org.eclipse.winery.repository.resources.entitytypes.TopologyGraphElementEntityTypeResource;
import org.eclipse.winery.repository.resources.interfaces.InterfacesResource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sun.jersey.api.view.Viewable;

public class RelationshipTypeResource extends TopologyGraphElementEntityTypeResource {
	
	private static final Logger logger = LoggerFactory.getLogger(RelationshipTypeResource.class);
	
	
	public RelationshipTypeResource(RelationshipTypeId id) {
		super(id);
	}
	
	@Path("implementations/")
	public ImplementationsOfOneRelationshipTypeResource getImplementations() {
		return new ImplementationsOfOneRelationshipTypeResource((RelationshipTypeId) this.id);
	}
	
	@Path("visualappearance/")
	public VisualAppearanceResource getVisualAppearanceResource() {
		return new VisualAppearanceResource(this, this.getElement().getOtherAttributes(), (RelationshipTypeId) this.id);
	}
	
	@Path("instancestates/")
	public InstanceStatesResource getInstanceStatesResource() {
		TTopologyElementInstanceStates instanceStates = this.getRelationshipType().getInstanceStates();
		if (instanceStates == null) {
			// if an explicit (empty) list does not exist, create it
			instanceStates = new TTopologyElementInstanceStates();
			this.getRelationshipType().setInstanceStates(instanceStates);
		}
		return new InstanceStatesResource(this.getRelationshipType().getInstanceStates(), this);
	}
	
	@Path("sourceinterfaces/")
	public InterfacesResource getSourceInterfaces() {
		SourceInterfaces interfaces = this.getRelationshipType().getSourceInterfaces();
		if (interfaces == null) {
			interfaces = new SourceInterfaces();
			this.getRelationshipType().setSourceInterfaces(interfaces);
		}
		return new InterfacesResource("source", interfaces.getInterface(), this);
	}
	
	@Path("targetinterfaces/")
	public InterfacesResource getTargetInterfaces() {
		TargetInterfaces interfaces = this.getRelationshipType().getTargetInterfaces();
		if (interfaces == null) {
			interfaces = new TargetInterfaces();
			this.getRelationshipType().setTargetInterfaces(interfaces);
		}
		return new InterfacesResource("target", interfaces.getInterface(), this);
	}
	
	@Path("validendings/")
	@GET
	@Produces(MediaType.TEXT_HTML)
	public Response getHTML() {
		Viewable viewable = new Viewable("/jsp/entitytypes/relationshiptypes/validendings.jsp", this);
		return Response.ok().entity(viewable).build();
	}
	
	@Path("validsource")
	@GET
	public String getValidSource() {
		ValidSource validSource;
		if (((validSource = this.getRelationshipType().getValidSource()) == null) || (validSource.getTypeRef() == null)) {
			return null;
		}
		return this.getRelationshipType().getValidSource().getTypeRef().toString();
	}
	
	@Path("validsource")
	@PUT
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	public Response setValidSource(String typeRef) {
		ValidSource vs = new ValidSource();
		QName qname = QName.valueOf(typeRef);
		vs.setTypeRef(qname);
		this.getRelationshipType().setValidSource(vs);
		return BackendUtils.persist(this);
	}
	
	@Path("validtarget")
	@GET
	public String getValidTarget() {
		ValidTarget validTarget;
		if (((validTarget = this.getRelationshipType().getValidTarget()) == null) || (validTarget.getTypeRef() == null)) {
			return null;
		}
		return this.getRelationshipType().getValidTarget().getTypeRef().toString();
	}
	
	@Path("validtarget")
	@PUT
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	public Response setValidTarget(String typeRef) {
		ValidTarget vt = new ValidTarget();
		QName qname = QName.valueOf(typeRef);
		vt.setTypeRef(qname);
		this.getRelationshipType().setValidTarget(vt);
		return BackendUtils.persist(this);
	}
	
	/**
	 * Required for validendings.jsp
	 */
	public Collection<NodeTypeId> getPossibleValidEndings() {
		SortedSet<NodeTypeId> allNodeTypeIds = Repository.INSTANCE.getAllTOSCAComponentIds(NodeTypeId.class);
		return allNodeTypeIds;
	}
	
	/**
	 * Convenience method to avoid casting at the caller's side.
	 */
	public TRelationshipType getRelationshipType() {
		return (TRelationshipType) this.getElement();
	}
	
	@Override
	protected TExtensibleElements createNewElement() {
		return new TRelationshipType();
	}
	
}