a2a8533deea2d2d66c2674bfa4b580e54dcd1de5
[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;
13
14 import java.util.Collection;
15 import java.util.Collections;
16 import java.util.SortedSet;
17
18 import org.eclipse.winery.common.ids.definitions.ArtifactTypeId;
19 import org.eclipse.winery.common.ids.definitions.NodeTypeId;
20 import org.eclipse.winery.common.ids.definitions.PolicyTypeId;
21 import org.eclipse.winery.common.ids.definitions.RelationshipTypeId;
22 import org.eclipse.winery.common.ids.definitions.TOSCAComponentId;
23 import org.eclipse.winery.repository.Utils;
24 import org.eclipse.winery.repository.backend.Repository;
25 import org.eclipse.winery.repository.resources.entitytemplates.artifacttemplates.ArtifactTemplatesResource;
26 import org.eclipse.winery.repository.resources.entitytemplates.policytemplates.PolicyTemplatesResource;
27 import org.eclipse.winery.repository.resources.entitytypeimplementations.nodetypeimplementations.NodeTypeImplementationsResource;
28 import org.eclipse.winery.repository.resources.entitytypeimplementations.relationshiptypeimplementations.RelationshipTypeImplementationsResource;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 public final class GenericComponentPageData {
33         
34         private static final Logger logger = LoggerFactory.getLogger(GenericComponentPageData.class);
35         
36         private final SortedSet<? extends TOSCAComponentId> componentInstanceIds;
37         
38         private final Class<? extends AbstractComponentsResource> resourceClass;
39         
40         
41         public GenericComponentPageData(Class<? extends AbstractComponentsResource> resourceClass) {
42                 this.resourceClass = resourceClass;
43                 Class<? extends TOSCAComponentId> cIdClass = Utils.getComponentIdClassForComponentContainer(resourceClass);
44                 this.componentInstanceIds = Repository.INSTANCE.getAllTOSCAComponentIds(cIdClass);
45         }
46         
47         /**
48          * Outputs the data for GenericComponentPage (Name / Id / Namespace) needed
49          * for the genericcomponentpage.jsp
50          */
51         public SortedSet<? extends TOSCAComponentId> getComponentInstanceIds() {
52                 return this.componentInstanceIds;
53         }
54         
55         public String getType() {
56                 return Utils.getTypeForComponentContainer(this.resourceClass);
57         }
58         
59         public String getCSSclass() {
60                 // The resources do NOT know their CSS class
61                 // Layout is far away from a resource
62                 // Instead of a huge if/else-cascade, we derive the CSS name from the
63                 // class name
64                 String type = this.getType();
65                 // convention: first letter in small letters
66                 String res = type.substring(0, 1).toLowerCase() + type.substring(1);
67                 // this generated "xSDImport" as CSS class for XSDImport
68                 return res;
69         }
70         
71         public String getLabel() {
72                 String type = this.getType();
73                 // E.g., convert ArtifactTemplate to Artifact Template
74                 String res = type.replaceAll("(\\p{Lower})(\\p{Upper})", "$1 $2");
75                 return res;
76         }
77         
78         /**
79          * Required for genericcomponentpage.jsp -> addComponentInstance.jsp
80          * 
81          * May only be used if the component supports the type (e.g., artifact
82          * templates)
83          * 
84          * @return the list of all known <em>types</em>
85          */
86         public Collection<? extends TOSCAComponentId> getTypeSelectorData() {
87                 Class<? extends TOSCAComponentId> typeIdClass;
88                 if (this.resourceClass.equals(ArtifactTemplatesResource.class)) {
89                         typeIdClass = ArtifactTypeId.class;
90                 } else if (this.resourceClass.equals(NodeTypeImplementationsResource.class)) {
91                         typeIdClass = NodeTypeId.class;
92                 } else if (this.resourceClass.equals(RelationshipTypeImplementationsResource.class)) {
93                         typeIdClass = RelationshipTypeId.class;
94                 } else if (this.resourceClass.equals(PolicyTemplatesResource.class)) {
95                         typeIdClass = PolicyTypeId.class;
96                 } else {
97                         return Collections.emptyList();
98                 }
99                 SortedSet<? extends TOSCAComponentId> allTOSCAcomponentIds = Repository.INSTANCE.getAllTOSCAComponentIds(typeIdClass);
100                 return allTOSCAcomponentIds;
101         }
102         
103 }