001package org.andromda.cartridges.ejb;
002
003import java.util.ArrayList;
004import java.util.Collection;
005import java.util.Iterator;
006import org.andromda.metafacades.uml.AttributeFacade;
007import org.andromda.metafacades.uml.ModelElementFacade;
008
009/**
010 * Transform class for the EJB cartridge.
011 *
012 * @author Richard Kunze
013 * @author Chad Brandon
014 */
015public class EJBScriptHelper
016{
017    /**
018     * Create a comma separated list of attributes.
019     * <p>
020     * This method can be used to generated e.g. argument lists for constructors, method calls etc.
021     *
022     * @param attributes   a collection of {@link AttributeFacade} objects
023     * @param includeTypes if <code>true</code>, the type names of the attributes are included.
024     * @param includeNames if <code>true</code>, the names of the attributes are included
025     * @return attributes
026     */
027    public String getAttributesAsList(Collection attributes, boolean includeTypes, boolean includeNames)
028    {
029        if (!includeNames && !includeTypes || attributes == null)
030        {
031            return "";
032        }
033
034        StringBuilder sb = new StringBuilder();
035        String separator = "";
036
037        for (final Iterator it = attributes.iterator(); it.hasNext();)
038        {
039            AttributeFacade attr = (AttributeFacade)it.next();
040            sb.append(separator);
041            separator = ", ";
042            if (includeTypes)
043            {
044                sb.append(attr.getType().getFullyQualifiedName());
045                sb.append(' ');
046            }
047            if (includeNames)
048            {
049                sb.append(attr.getName());
050            }
051        }
052        return sb.toString();
053    }
054
055    /**
056     * Filter a list of model elements by visibility.
057     *
058     * @param list       the original list
059     * @param visibility the visibility - "public" "protected", "private" or the empty string (for package visibility)
060     * @return a list with all elements from the original list that have a matching visibility.
061     */
062    public Collection filterByVisibility(Collection list, String visibility)
063    {
064        Collection<ModelElementFacade> result = new ArrayList<ModelElementFacade>(list.size());
065        for (ModelElementFacade elem : (Iterable<ModelElementFacade>) list)
066        {
067            if (visibility.equals(elem.getVisibility()))
068            {
069                result.add(elem);
070            }
071        }
072        return result;
073    }
074
075}