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;
14 import java.util.Collection;
15 import java.util.SortedSet;
17 import javax.ws.rs.GET;
18 import javax.ws.rs.Path;
19 import javax.ws.rs.Produces;
20 import javax.ws.rs.WebApplicationException;
21 import javax.ws.rs.core.MediaType;
22 import javax.ws.rs.core.Response;
23 import javax.ws.rs.core.Response.Status;
25 import org.eclipse.winery.common.ids.definitions.TOSCAComponentId;
26 import org.eclipse.winery.model.tosca.TEntityType;
27 import org.eclipse.winery.repository.backend.BackendUtils;
28 import org.eclipse.winery.repository.datatypes.select2.Select2DataWithOptGroups;
29 import org.eclipse.winery.repository.datatypes.select2.Select2OptGroup;
30 import org.eclipse.winery.repository.resources.entitytypes.properties.PropertiesDefinitionResource;
32 public abstract class EntityTypeResource extends AbstractComponentInstanceResourceWithNameDerivedFromAbstractFinal {
34 protected EntityTypeResource(TOSCAComponentId id) {
39 protected void copyIdToFields() {
40 TEntityType entityType = this.getEntityType();
41 entityType.setTargetNamespace(this.getId().getNamespace().getDecoded());
42 entityType.setName(this.getId().getXmlId().getDecoded());
46 * Convenience method to avoid casting. Required by
47 * PropertiesDefinitionResource's jsp
49 public TEntityType getEntityType() {
50 return (TEntityType) this.element;
54 * Models PropertiesDefinition
56 @Path("propertiesdefinition/")
57 public PropertiesDefinitionResource getPropertiesDefinitionResource() {
58 return new PropertiesDefinitionResource(this);
62 * Used by children to implement getListOfAllInstances()
64 protected SortedSet<Select2OptGroup> getListOfAllInstances(Class<? extends TOSCAComponentId> clazz) {
65 Select2DataWithOptGroups data = new Select2DataWithOptGroups();
67 Collection<? extends TOSCAComponentId> instanceIds = BackendUtils.getAllElementsRelatedWithATypeAttribute(clazz, this.id.getQName());
69 for (TOSCAComponentId instanceId : instanceIds) {
70 String groupText = instanceId.getNamespace().getDecoded();
71 String text = BackendUtils.getName(instanceId);
72 data.add(groupText, instanceId.getQName().toString(), text);
75 return data.asSortedSet();
79 * Returns an array suitable for processing in a {@code select2} field See
80 * {@link http://ivaynberg.github.io/select2}
82 * Each element: {id: "{ns}localname", text: "name/id"}
86 @Produces(MediaType.APPLICATION_JSON)
87 public SortedSet<Select2OptGroup> getListOfAllInstances() {
88 Response res = Response.status(Status.INTERNAL_SERVER_ERROR).entity("not yet implemented").build();
89 throw new WebApplicationException(res);