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 / EntityTypeResource.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;
13
14 import java.util.Collection;
15 import java.util.SortedSet;
16
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;
24
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;
31
32 public abstract class EntityTypeResource extends AbstractComponentInstanceResourceWithNameDerivedFromAbstractFinal {
33         
34         protected EntityTypeResource(TOSCAComponentId id) {
35                 super(id);
36         }
37         
38         @Override
39         protected void copyIdToFields() {
40                 TEntityType entityType = this.getEntityType();
41                 entityType.setTargetNamespace(this.getId().getNamespace().getDecoded());
42                 entityType.setName(this.getId().getXmlId().getDecoded());
43         }
44         
45         /**
46          * Convenience method to avoid casting. Required by
47          * PropertiesDefinitionResource's jsp
48          */
49         public TEntityType getEntityType() {
50                 return (TEntityType) this.element;
51         }
52         
53         /**
54          * Models PropertiesDefinition
55          */
56         @Path("propertiesdefinition/")
57         public PropertiesDefinitionResource getPropertiesDefinitionResource() {
58                 return new PropertiesDefinitionResource(this);
59         }
60         
61         /**
62          * Used by children to implement getListOfAllInstances()
63          */
64         protected SortedSet<Select2OptGroup> getListOfAllInstances(Class<? extends TOSCAComponentId> clazz) {
65                 Select2DataWithOptGroups data = new Select2DataWithOptGroups();
66                 
67                 Collection<? extends TOSCAComponentId> instanceIds = BackendUtils.getAllElementsRelatedWithATypeAttribute(clazz, this.id.getQName());
68                 
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);
73                 }
74                 
75                 return data.asSortedSet();
76         }
77         
78         /**
79          * Returns an array suitable for processing in a {@code select2} field See
80          * {@link http://ivaynberg.github.io/select2}
81          * 
82          * Each element: {id: "{ns}localname", text: "name/id"}
83          */
84         @Path("instances/")
85         @GET
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);
90         }
91         
92 }