001package org.andromda.metafacades.uml14;
002
003import java.util.ArrayList;
004import java.util.Collection;
005import java.util.Iterator;
006import java.util.LinkedHashSet;
007import org.andromda.metafacades.uml.AttributeFacade;
008import org.andromda.metafacades.uml.ClassifierFacade;
009import org.andromda.metafacades.uml.EventFacade;
010import org.andromda.metafacades.uml.FrontEndAction;
011import org.andromda.metafacades.uml.FrontEndControllerOperation;
012import org.andromda.metafacades.uml.FrontEndEvent;
013import org.andromda.metafacades.uml.FrontEndForward;
014import org.andromda.metafacades.uml.ModelElementFacade;
015import org.andromda.metafacades.uml.OperationFacade;
016import org.andromda.metafacades.uml.TransitionFacade;
017import org.andromda.metafacades.uml.UMLProfile;
018import org.apache.commons.collections.CollectionUtils;
019import org.apache.commons.collections.Predicate;
020import org.apache.commons.collections.Transformer;
021import org.apache.commons.lang.ObjectUtils;
022import org.apache.commons.lang.StringUtils;
023
024/**
025 * MetafacadeLogic implementation for org.andromda.metafacades.uml.FrontEndParameter.
026 *
027 * @see org.andromda.metafacades.uml.FrontEndParameter
028 * @author Bob Fields
029 */
030public class FrontEndParameterLogicImpl
031    extends FrontEndParameterLogic
032{
033    private static final long serialVersionUID = 2219144832310011722L;
034
035    /**
036     * @param metaObject
037     * @param context
038     */
039    public FrontEndParameterLogicImpl(
040        Object metaObject,
041        String context)
042    {
043        super(metaObject, context);
044    }
045
046    /**
047     * @see org.andromda.metafacades.uml.FrontEndParameter#isControllerOperationArgument()
048     */
049    @Override
050    protected boolean handleIsControllerOperationArgument()
051    {
052        return this.getControllerOperation() != null;
053    }
054
055    /**
056     * @see org.andromda.metafacades.uml.FrontEndParameter#getControllerOperation()
057     */
058    @Override
059    protected OperationFacade handleGetControllerOperation()
060    {
061        return this.getOperation();
062    }
063
064    /**
065     * @see org.andromda.metafacades.uml.FrontEndParameter#isContainedInFrontEndUseCase()
066     */
067    @Override
068    protected boolean handleIsContainedInFrontEndUseCase()
069    {
070        return this.getEvent() instanceof FrontEndEvent || this.getOperation() instanceof FrontEndControllerOperation;
071    }
072
073    /**
074     * @see org.andromda.metafacades.uml.FrontEndParameter#getView()
075     */
076    @Override
077    protected Object handleGetView()
078    {
079        Object view = null;
080        final EventFacade event = this.getEvent();
081        if (event != null)
082        {
083            final TransitionFacade transition = event.getTransition();
084            if (transition instanceof FrontEndAction)
085            {
086                final FrontEndAction action = (FrontEndAction)transition;
087                view = action.getInput();
088            }
089            else if (transition instanceof FrontEndForward)
090            {
091                final FrontEndForward forward = (FrontEndForward)transition;
092                if (forward.isEnteringView())
093                {
094                    view = forward.getTarget();
095                }
096            }
097        }
098        return view;
099    }
100
101    /**
102     * @see org.andromda.metafacades.uml.FrontEndParameter#isActionParameter()
103     */
104    @Override
105    protected boolean handleIsActionParameter()
106    {
107        final FrontEndAction action = this.getAction();
108        return action != null && action.getParameters().contains(this.THIS());
109    }
110
111    /**
112     * @see org.andromda.metafacades.uml.FrontEndParameter#getAction()
113     */
114    @Override
115    protected FrontEndAction handleGetAction()
116    {
117        Object actionObject = null;
118        final EventFacade event = this.getEvent();
119        if (event != null)
120        {
121            final TransitionFacade transition = event.getTransition();
122            if (transition instanceof FrontEndAction)
123            {
124                actionObject = transition;
125            }
126        }
127        return (FrontEndAction)actionObject;
128    }
129
130    /**
131     * @see org.andromda.metafacades.uml.FrontEndParameter#isTable()
132     */
133    @Override
134    protected boolean handleIsTable()
135    {
136        boolean isTable = false;
137        final ClassifierFacade type = this.getType();
138        if (type != null)
139        {
140            isTable = type.isCollectionType() || type.isArrayType();
141            if (isTable)
142            {
143                final String tableTaggedValue = ObjectUtils.toString(this.findTaggedValue(UMLProfile.TAGGEDVALUE_PRESENTATION_IS_TABLE));
144                isTable =
145                    StringUtils.isNotBlank(tableTaggedValue) ? Boolean.valueOf(tableTaggedValue.trim()) : true;
146                if (!isTable)
147                {
148                    isTable = !this.getTableColumnNames().isEmpty();
149                }
150            }
151        }
152        return isTable && this.getOperation() == null;
153    }
154
155    /**
156     * @see org.andromda.metafacades.uml.FrontEndParameter#getTableColumnNames()
157     */
158    @Override
159    protected Collection<String> handleGetTableColumnNames()
160    {
161        final Collection tableColumnNames = new LinkedHashSet();
162        final Collection taggedValues = this.findTaggedValues(UMLProfile.TAGGEDVALUE_PRESENTATION_TABLE_COLUMNS);
163        if (!taggedValues.isEmpty())
164        {
165            for (final Iterator iterator = taggedValues.iterator(); iterator.hasNext();)
166            {
167                final String taggedValue = StringUtils.trimToNull(String.valueOf(iterator.next()));
168                if (taggedValue != null)
169                {
170                    final String[] properties = taggedValue.split("[,\\s]+");
171                    for (int ctr = 0; ctr < properties.length; ctr++)
172                    {
173                        final String property = properties[ctr];
174                        tableColumnNames.add(property);
175                    }
176                }
177            }
178        }
179        // - if we have no table column names explicitly defined, use the table attribute names.
180        if (tableColumnNames.isEmpty())
181        {
182            tableColumnNames.addAll(this.getTableAttributeNames());
183        }
184        return tableColumnNames;
185    }
186
187    /**
188     * @see org.andromda.metafacades.uml.FrontEndParameter#getTableColumns()
189     */
190    @Override
191    protected Collection<String> handleGetTableColumns()
192    {
193        final Collection tableColumns = new ArrayList(this.getNonArrayAttributes());
194        final Collection tableColumnNames = this.getTableColumnNames();
195        CollectionUtils.filter(tableColumns,
196            new Predicate()
197            {
198                public boolean evaluate(final Object object)
199                {
200                    final ModelElementFacade attribute = (ModelElementFacade)object;
201                    final String attributeName = attribute.getName();
202                    return attributeName != null && tableColumnNames.contains(attributeName);
203                }
204            });
205        return tableColumns;
206    }
207
208    /**
209     * Gets all attributes for an array type that has a corresponding non-array
210     * type.
211     * @return the collection of attributes.
212     */
213    private Collection getNonArrayAttributes()
214    {
215        final Collection nonArrayAttributes = new ArrayList();
216        final ClassifierFacade type = this.getType();
217        if (type != null && type.isArrayType())
218        {
219            final ClassifierFacade nonArrayType = type.getNonArray();
220            if (nonArrayType != null)
221            {
222                nonArrayAttributes.addAll(nonArrayType.getAttributes(true));
223            }
224        }
225        return nonArrayAttributes;
226    }
227
228    /**
229     * @see org.andromda.metafacades.uml.FrontEndParameter#getTableAttributeNames()
230     */
231    @Override
232    protected Collection<String> handleGetTableAttributeNames()
233    {
234        final Collection tableAttributeNames = new ArrayList(this.getNonArrayAttributes());
235        CollectionUtils.transform(tableAttributeNames,
236            new Transformer()
237            {
238                public Object transform(final Object object)
239                {
240                    return ((AttributeFacade)object).getName();
241                }
242            });
243        return tableAttributeNames;
244    }
245}