61af3bdb8512b627c358ddc57640704ddf8a51eb
[vfc/nfvo/wfengine.git] /
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.imports.xsdimports;
13
14 import java.util.Collection;
15 import java.util.HashMap;
16 import java.util.HashSet;
17 import java.util.Map;
18 import java.util.Set;
19 import java.util.TreeSet;
20
21 import javax.ws.rs.GET;
22 import javax.ws.rs.Path;
23 import javax.ws.rs.PathParam;
24 import javax.ws.rs.Produces;
25 import javax.ws.rs.QueryParam;
26 import javax.ws.rs.core.MediaType;
27
28 import org.eclipse.winery.common.RepositoryFileReference;
29 import org.eclipse.winery.common.ids.Namespace;
30 import org.eclipse.winery.common.ids.definitions.imports.XSDImportId;
31 import org.eclipse.winery.repository.Utils;
32 import org.eclipse.winery.repository.backend.Repository;
33 import org.eclipse.winery.repository.resources.AbstractComponentsResource;
34 import org.restdoc.annotations.RestDoc;
35
36 import com.fasterxml.jackson.core.JsonProcessingException;
37
38 /**
39  * Manages all imports of type XML Schema Definition <br />
40  * The actual implementation is done in the AbstractComponentsResource
41  * 
42  * FIXME: This class should be generalized to handle ImportId
43  */
44 public class XSDImportsResource extends AbstractComponentsResource<XSDImportResource> {
45         
46         @Path("{namespace}/")
47         @GET
48         @RestDoc(methodDescription = "Returns all available local names of defined elements in this namespace")
49         @Produces(MediaType.APPLICATION_JSON)
50         public String getAllElementLocalNames(@PathParam("namespace") String nsString, @QueryParam(value = "elements") String returnElements, @QueryParam(value = "types") String returnTypes) {
51                 // returnElements is not read as either types or elements may be read
52                 Set<String> allNCNames = this.getAllElementLocalNamesAsSet(nsString, returnTypes != null);
53                 try {
54                         return Utils.mapper.writeValueAsString(allNCNames);
55                 } catch (JsonProcessingException e) {
56                         throw new IllegalStateException(e);
57                 }
58         }
59         
60         /**
61          * @param nsString the namesapce as String
62          * @param returnTypes true: return ElementTypes, false: return Elements
63          */
64         private Set<String> getAllElementLocalNamesAsSet(final String nsString, final boolean getTypes) {
65                 Set<XSDImportId> importsOfNS = this.getImportsOfNS(nsString);
66                 
67                 // TreeSet enables ordering
68                 Set<String> allNCNames = new TreeSet<String>();
69                 
70                 for (XSDImportId imp : importsOfNS) {
71                         XSDImportResource res = new XSDImportResource(imp);
72                         Collection<String> col;
73                         if (getTypes) {
74                                 col = res.getAllDefinedTypesLocalNames();
75                         } else {
76                                 col = res.getAllDefinedElementsLocalNames();
77                         }
78                         allNCNames.addAll(col);
79                 }
80                 return allNCNames;
81         }
82         
83         /**
84          * Finds out all imports belonging to the given namespace
85          * 
86          * @param nsString the namespace to query
87          */
88         private Set<XSDImportId> getImportsOfNS(final String nsString) {
89                 // FIXME: Currently not supported by the repository, therefore, we filter by hand
90                 Set<XSDImportId> allImports = Repository.INSTANCE.getAllTOSCAComponentIds(XSDImportId.class);
91                 Namespace ns = new Namespace(nsString, true);
92                 Set<XSDImportId> importsOfNs = new HashSet<XSDImportId>();
93                 for (XSDImportId imp : allImports) {
94                         if (imp.getNamespace().equals(ns)) {
95                                 importsOfNs.add(imp);
96                         }
97                 }
98                 return importsOfNs;
99         }
100         
101         /**
102          * Returns a mapping from localnames to XSD files, containing the defined
103          * local names for the given namespace
104          */
105         public Map<String, RepositoryFileReference> getMapFromLocalNameToXSD(final String nsString, final boolean getTypes) {
106                 Set<XSDImportId> importsOfNS = this.getImportsOfNS(nsString);
107                 Map<String, RepositoryFileReference> result = new HashMap<>();
108                 for (XSDImportId imp : importsOfNS) {
109                         XSDImportResource res = new XSDImportResource(imp);
110                         Collection<String> col;
111                         if (getTypes) {
112                                 col = res.getAllDefinedTypesLocalNames();
113                         } else {
114                                 col = res.getAllDefinedElementsLocalNames();
115                         }
116                         RepositoryFileReference ref = res.getXSDFileReference();
117                         for (String localName : col) {
118                                 result.put(localName, ref);
119                         }
120                 }
121                 return result;
122         }
123         
124 }