1 package org.andromda.metafacades.uml14;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.Collections;
6 import java.util.Iterator;
7 import org.andromda.core.common.ExceptionUtils;
8 import org.andromda.core.configuration.Filters;
9 import org.andromda.core.metafacade.MetafacadeBase;
10 import org.andromda.core.metafacade.MetafacadeConstants;
11 import org.andromda.core.metafacade.MetafacadeFactory;
12 import org.andromda.core.metafacade.ModelAccessFacade;
13 import org.andromda.metafacades.uml.EntityMetafacadeUtils;
14 import org.andromda.metafacades.uml.ModelElementFacade;
15 import org.andromda.metafacades.uml.PackageFacade;
16 import org.apache.commons.collections.CollectionUtils;
17 import org.apache.commons.collections.Predicate;
18 import org.apache.commons.lang.StringUtils;
19 import org.apache.log4j.Logger;
20 import org.omg.uml.UmlPackage;
21 import org.omg.uml.foundation.core.ModelElement;
22
23
24
25
26
27
28
29
30 public class UMLModelAccessFacade
31 implements ModelAccessFacade
32 {
33 private static final Logger logger = Logger.getLogger(UMLModelAccessFacade.class);
34 private UmlPackage model;
35
36
37
38
39 public void setModel(final Object model)
40 {
41 ExceptionUtils.checkNull("model", model);
42 ExceptionUtils.checkAssignable(
43 UmlPackage.class,
44 "modelElement",
45 model.getClass());
46 this.model = (UmlPackage)model;
47
48
49 EntityMetafacadeUtils.clearForeignKeyConstraintNameCache();
50 }
51
52
53
54
55 public UmlPackage getModel()
56 {
57 return model;
58 }
59
60
61
62
63 public String getName(final Object modelElement)
64 {
65 ExceptionUtils.checkNull("modelElement", modelElement);
66 ExceptionUtils.checkAssignable(
67 ModelElementFacade.class,
68 "modelElement",
69 modelElement.getClass());
70 return ((ModelElementFacade)modelElement).getName();
71 }
72
73
74
75
76 public String getPackageName(final Object modelElement)
77 {
78 ExceptionUtils.checkNull("modelElement", modelElement);
79 ExceptionUtils.checkAssignable(
80 ModelElementFacade.class,
81 "modelElement",
82 modelElement.getClass());
83 final ModelElementFacade modelElementFacade = (ModelElementFacade)modelElement;
84 final StringBuilder packageName = new StringBuilder(modelElementFacade.getPackageName(true));
85
86
87
88 if (modelElement instanceof PackageFacade)
89 {
90 final String name = modelElementFacade.getName();
91 if (StringUtils.isNotBlank(name))
92 {
93 packageName.append(MetafacadeConstants.NAMESPACE_SCOPE_OPERATOR);
94 packageName.append(name);
95 }
96 }
97 return packageName.toString();
98 }
99
100
101
102
103 public Collection<String> getStereotypeNames(final Object modelElement)
104 {
105 Collection<String> stereotypeNames = new ArrayList();
106 if (modelElement instanceof ModelElement)
107 {
108 ModelElement element = (ModelElement)modelElement;
109 Collection<ModelElement> stereotypes = element.getStereotype();
110 for (final Iterator<ModelElement> iterator = stereotypes.iterator(); iterator.hasNext();)
111 {
112 ModelElement stereotype = iterator.next();
113 stereotypeNames.add(stereotype.getName());
114 }
115 }
116 else if (modelElement instanceof ModelElementFacade)
117 {
118 stereotypeNames = ((ModelElementFacade)modelElement).getStereotypeNames();
119 }
120 return stereotypeNames;
121 }
122
123
124
125
126 public Collection<MetafacadeBase> findByStereotype(String stereotype)
127 {
128 final String methodName = "UMLModelAccessFacade.findByStereotype";
129 final Collection<MetafacadeBase> metafacades = new ArrayList<MetafacadeBase>();
130 stereotype = StringUtils.trimToEmpty(stereotype);
131 if (StringUtils.isNotBlank(stereotype))
132 {
133 if (this.model != null)
134 {
135 final Collection<ModelElement> underlyingElements = model.getCore().getModelElement().refAllOfType();
136 if (underlyingElements != null && !underlyingElements.isEmpty())
137 {
138 for (final ModelElement element : underlyingElements)
139 {
140 Collection<String> stereotypeNames = this.getStereotypeNames(element);
141 if (stereotypeNames != null && stereotypeNames.contains(stereotype))
142 {
143 metafacades.add(MetafacadeFactory.getInstance().createMetafacade(element));
144 }
145 }
146 }
147 if (logger.isDebugEnabled())
148 {
149 logger.debug("completed " + methodName + " with " + metafacades.size() + " modelElements");
150 }
151 }
152 this.filterMetafacades(metafacades);
153 }
154 return metafacades;
155 }
156
157
158
159
160 public Collection<MetafacadeBase> getModelElements()
161 {
162 Collection<MetafacadeBase> metafacades = Collections.emptyList();
163 if (this.model != null)
164 {
165 metafacades =
166 MetafacadeFactory.getInstance().createMetafacades(
167 this.model.getCore().getModelElement().refAllOfType());
168 this.filterMetafacades(metafacades);
169 }
170 return metafacades;
171 }
172
173
174
175
176
177 protected Filters modelPackages;
178
179
180
181
182 public void setPackageFilter(final Filters modelPackages)
183 {
184 this.modelPackages = modelPackages;
185 }
186
187
188
189
190
191
192 private void filterMetafacades(final Collection metafacades)
193 {
194 if (this.modelPackages != null && !this.modelPackages.isEmpty())
195 {
196 CollectionUtils.filter(
197 metafacades,
198 new Predicate()
199 {
200 public boolean evaluate(final Object metafacade)
201 {
202 boolean valid = false;
203 if (metafacade instanceof MetafacadeBase)
204 {
205 final ModelElementFacade modelElementFacade = (ModelElementFacade)metafacade;
206 final StringBuilder packageName = new StringBuilder(modelElementFacade.getPackageName(true));
207
208
209
210 if (metafacade instanceof PackageFacade)
211 {
212 final String name = modelElementFacade.getName();
213 if (StringUtils.isNotBlank(name))
214 {
215 packageName.append(MetafacadeConstants.NAMESPACE_SCOPE_OPERATOR);
216 packageName.append(name);
217 }
218 }
219 valid = modelPackages.isApply(packageName.toString());
220 }
221 return valid;
222 }
223 });
224 }
225 }
226 }