001: package org.andromda.metafacades.uml14;
002:
003: import java.util.ArrayList;
004: import java.util.Collection;
005: import java.util.Collections;
006: import java.util.Iterator;
007:
008: import org.andromda.core.common.ExceptionUtils;
009: import org.andromda.core.configuration.Filters;
010: import org.andromda.core.metafacade.MetafacadeBase;
011: import org.andromda.core.metafacade.MetafacadeConstants;
012: import org.andromda.core.metafacade.MetafacadeFactory;
013: import org.andromda.core.metafacade.ModelAccessFacade;
014: import org.andromda.metafacades.uml.ModelElementFacade;
015: import org.andromda.metafacades.uml.PackageFacade;
016: import org.apache.commons.collections.CollectionUtils;
017: import org.apache.commons.collections.Predicate;
018: import org.apache.commons.lang.StringUtils;
019: import org.apache.log4j.Logger;
020: import org.omg.uml.UmlPackage;
021: import org.omg.uml.foundation.core.ModelElement;
022:
023: /**
024: * Contains a UML model, follows the {@link ModelAccessFacade} interface and can therefore be processed by AndroMDA.
025: *
026: * @author <a href="http://www.mbohlen.de">Matthias Bohlen </a>
027: * @author Chad Brandon
028: */
029: public class UMLModelAccessFacade implements ModelAccessFacade {
030: private static final Logger logger = Logger
031: .getLogger(UMLModelAccessFacade.class);
032: private UmlPackage model;
033:
034: /**
035: * @see org.andromda.core.metafacade.ModelAccessFacade#setModel(java.lang.Object)
036: */
037: public void setModel(final Object model) {
038: ExceptionUtils.checkNull("model", model);
039: ExceptionUtils.checkAssignable(UmlPackage.class,
040: "modelElement", model.getClass());
041: this .model = (UmlPackage) model;
042: }
043:
044: /**
045: * @see org.andromda.core.metafacade.ModelAccessFacade#getModel()
046: */
047: public Object getModel() {
048: return model;
049: }
050:
051: /**
052: * @see org.andromda.core.metafacade.ModelAccessFacade#getName(java.lang.Object)
053: */
054: public String getName(final Object modelElement) {
055: ExceptionUtils.checkNull("modelElement", modelElement);
056: ExceptionUtils.checkAssignable(ModelElementFacade.class,
057: "modelElement", modelElement.getClass());
058: return ((ModelElementFacade) modelElement).getName();
059: }
060:
061: /**
062: * @see org.andromda.core.metafacade.ModelAccessFacade#getPackageName(java.lang.Object)
063: */
064: public String getPackageName(final Object modelElement) {
065: ExceptionUtils.checkNull("modelElement", modelElement);
066: ExceptionUtils.checkAssignable(ModelElementFacade.class,
067: "modelElement", modelElement.getClass());
068: final ModelElementFacade modelElementFacade = (ModelElementFacade) modelElement;
069: final StringBuffer packageName = new StringBuffer(
070: modelElementFacade.getPackageName(true));
071:
072: // - if the model element is a package then the package name will be the name
073: // of the package with its package name
074: if (modelElement instanceof PackageFacade) {
075: final String name = modelElementFacade.getName();
076: if (StringUtils.isNotBlank(name)) {
077: packageName
078: .append(MetafacadeConstants.NAMESPACE_SCOPE_OPERATOR);
079: packageName.append(name);
080: }
081: }
082: return packageName.toString();
083: }
084:
085: /**
086: * @see org.andromda.core.metafacade.ModelAccessFacade#getStereotypeNames(java.lang.Object)
087: */
088: public Collection getStereotypeNames(final Object modelElement) {
089: Collection stereotypeNames = new ArrayList();
090: if (modelElement instanceof ModelElement) {
091: ModelElement element = (ModelElement) modelElement;
092: Collection stereotypes = element.getStereotype();
093: for (final Iterator iterator = stereotypes.iterator(); iterator
094: .hasNext();) {
095: ModelElement stereotype = (ModelElement) iterator
096: .next();
097: stereotypeNames.add(stereotype.getName());
098: }
099: } else if (modelElement instanceof ModelElementFacade) {
100: stereotypeNames = ((ModelElementFacade) modelElement)
101: .getStereotypeNames();
102: }
103: return stereotypeNames;
104: }
105:
106: /**
107: * @see org.andromda.core.metafacade.ModelAccessFacade#findByStereotype(java.lang.String)
108: */
109: public Collection findByStereotype(String stereotype) {
110: final String methodName = "UMLModelAccessFacade.findByStereotype";
111: final Collection metafacades = new ArrayList();
112: stereotype = StringUtils.trimToEmpty(stereotype);
113: if (StringUtils.isNotEmpty(stereotype)) {
114: if (this .model != null) {
115: final Collection underlyingElements = model.getCore()
116: .getModelElement().refAllOfType();
117: if (underlyingElements != null
118: && !underlyingElements.isEmpty()) {
119: for (final Iterator iterator = underlyingElements
120: .iterator(); iterator.hasNext();) {
121: ModelElement element = (ModelElement) iterator
122: .next();
123: Collection stereotypeNames = this
124: .getStereotypeNames(element);
125: if (stereotypeNames != null
126: && stereotypeNames.contains(stereotype)) {
127: metafacades.add(MetafacadeFactory
128: .getInstance().createMetafacade(
129: element));
130: }
131: }
132: }
133: if (logger.isDebugEnabled()) {
134: logger.debug("completed " + methodName + " with "
135: + metafacades.size() + " modelElements");
136: }
137: }
138: this .filterMetafacades(metafacades);
139: }
140: return metafacades;
141: }
142:
143: /**
144: * @see org.andromda.core.metafacade.ModelAccessFacade#getModelElements()
145: */
146: public Collection getModelElements() {
147: Collection metafacades = Collections.EMPTY_LIST;
148: if (this .model != null) {
149: metafacades = MetafacadeFactory.getInstance()
150: .createMetafacades(
151: this .model.getCore().getModelElement()
152: .refAllOfType());
153: this .filterMetafacades(metafacades);
154: }
155: return metafacades;
156: }
157:
158: /**
159: * Stores the package filter information. Protected
160: * visibility for better inner class access performance.
161: */
162: protected Filters modelPackages;
163:
164: /**
165: * @see org.andromda.core.metafacade.ModelAccessFacade#setPackageFilter(org.andromda.core.configuration.Filters)
166: */
167: public void setPackageFilter(final Filters modelPackages) {
168: this .modelPackages = modelPackages;
169: }
170:
171: /**
172: * Filters out those metafacades which <strong>should </strong> be processed.
173: *
174: * @param metafacades the Collection of modelElements.
175: */
176: private void filterMetafacades(final Collection metafacades) {
177: if (this .modelPackages != null && !this .modelPackages.isEmpty()) {
178: CollectionUtils.filter(metafacades, new Predicate() {
179: public boolean evaluate(final Object metafacade) {
180: boolean valid = false;
181: if (metafacade instanceof MetafacadeBase) {
182: final ModelElementFacade modelElementFacade = (ModelElementFacade) metafacade;
183: final StringBuffer packageName = new StringBuffer(
184: modelElementFacade.getPackageName(true));
185:
186: // - if the model element is a package then the package name will be the name
187: // of the package with its package name
188: if (metafacade instanceof PackageFacade) {
189: final String name = modelElementFacade
190: .getName();
191: if (StringUtils.isNotBlank(name)) {
192: packageName
193: .append(MetafacadeConstants.NAMESPACE_SCOPE_OPERATOR);
194: packageName.append(name);
195: }
196: }
197: valid = modelPackages.isApply(packageName
198: .toString());
199: }
200: return valid;
201: }
202: });
203: }
204: }
205: }
|