1 package org.andromda.cartridges.ejb;
2
3 import org.andromda.metafacades.uml.AttributeFacade;
4 import org.andromda.metafacades.uml.ModelElementFacade;
5
6 import java.util.ArrayList;
7 import java.util.Collection;
8 import java.util.Iterator;
9
10 /**
11 * Transform class for the EJB cartridge.
12 *
13 * @author Richard Kunze
14 * @author Chad Brandon
15 */
16 public class EJBScriptHelper
17 {
18
19 /**
20 * Create a comma seperated list of attributes.
21 * <p>
22 * This method can be used to generated e.g. argument lists for constructors, method calls etc.
23 *
24 * @param attributes a collection of {@link Attribute} objects
25 * @param includeTypes if <code>true</code>, the type names of the attributes are included.
26 * @param includeNames if <code>true</code>, the names of the attributes are included
27 */
28 public String getAttributesAsList(Collection attributes, boolean includeTypes, boolean includeNames)
29 {
30 if (!includeNames && !includeTypes || attributes == null)
31 {
32 return "";
33 }
34
35 StringBuffer sb = new StringBuffer();
36 String separator = "";
37
38 for (final Iterator it = attributes.iterator(); it.hasNext();)
39 {
40 AttributeFacade attr = (AttributeFacade)it.next();
41 sb.append(separator);
42 separator = ", ";
43 if (includeTypes)
44 {
45 sb.append(attr.getType().getFullyQualifiedName());
46 sb.append(" ");
47 }
48 if (includeNames)
49 {
50 sb.append(attr.getName());
51 }
52 }
53 return sb.toString();
54 }
55
56 /**
57 * Filter a list of model elements by visibility.
58 *
59 * @param list the original list
60 * @param visibility the visibility - "public" "protected", "private" or the empty string (for package visibility)
61 * @return a list with all elements from the original list that have a matching visibility.
62 */
63 public Collection filterByVisibility(Collection list, String visibility)
64 {
65 Collection retval = new ArrayList(list.size());
66 for (final Iterator iter = list.iterator(); iter.hasNext();)
67 {
68 ModelElementFacade elem = (ModelElementFacade)iter.next();
69 if (visibility.equals(elem.getVisibility().toString()))
70 {
71 retval.add(elem);
72 }
73 }
74 return retval;
75 }
76
77 }