001package org.andromda.cartridges.ejb3.metafacades;
002
003import java.text.MessageFormat;
004import java.util.Collection;
005import java.util.List;
006import org.andromda.cartridges.ejb3.EJB3Globals;
007import org.andromda.metafacades.uml.EntityAttribute;
008import org.andromda.metafacades.uml.ModelElementFacade;
009import org.apache.commons.collections.CollectionUtils;
010import org.apache.commons.collections.Predicate;
011import org.apache.commons.lang.ObjectUtils;
012import org.apache.commons.lang.StringUtils;
013
014/**
015 * MetafacadeLogic implementation for org.andromda.cartridges.ejb3.metafacades.EJB3ManageableEntityFacade.
016 *
017 * @see EJB3ManageableEntityFacade
018 */
019public class EJB3ManageableEntityFacadeLogicImpl
020    extends EJB3ManageableEntityFacadeLogic
021{
022    private static final long serialVersionUID = 34L;
023    /**
024     * The property which stores the pattern defining the manageable create exception name.
025     */
026    public static final String CREATE_EXCEPTION_NAME_PATTERN = "manageableCreateExceptionNamePattern";
027
028    /**
029     * The property which stores the pattern defining the manageable read exception name.
030     */
031    public static final String READ_EXCEPTION_NAME_PATTERN = "manageableReadExceptionNamePattern";
032
033    /**
034     * The property which stores the pattern defining the manageable update exception name.
035     */
036    public static final String UPDATE_EXCEPTION_NAME_PATTERN = "manageableUpdateExceptionNamePattern";
037
038    /**
039     * The property which stores the pattern defining the manageable delete exception name.
040     */
041    public static final String DELETE_EXCEPTION_NAME_PATTERN = "manageableDeleteExceptionNamePattern";
042
043    /**
044     * The property that stores the JNDI name prefix.
045     */
046    public static final String SERVICE_JNDI_NAME_PREFIX = "jndiNamePrefix";
047
048    /**
049     * The property that stores the manageable service base name pattern
050     */
051    public static final String MANAGEABLE_SERVICE_BASE_NAME_PATTERN = "manageableServiceBaseNamePattern";
052
053    /**
054     * Constructor
055     *
056     * @param metaObject
057     * @param context
058     */
059    public EJB3ManageableEntityFacadeLogicImpl(final Object metaObject, final String context)
060    {
061        super (metaObject, context);
062    }
063
064    /**
065     * @see EJB3ManageableEntityFacadeLogic#handleGetManageableServiceCreateExceptionName()
066     */
067    @Override
068    protected String handleGetManageableServiceCreateExceptionName()
069    {
070        String exceptionNamePattern = (String)this.getConfiguredProperty(CREATE_EXCEPTION_NAME_PATTERN);
071
072        return MessageFormat.format(
073                exceptionNamePattern,
074                StringUtils.trimToEmpty(this.getName()));
075    }
076
077    /**
078     * @see EJB3ManageableEntityFacadeLogic#handleGetFullyQualifiedManageableServiceCreateExceptionName()
079     */
080    @Override
081    protected String handleGetFullyQualifiedManageableServiceCreateExceptionName()
082    {
083        return EJB3MetafacadeUtils.getFullyQualifiedName(
084                this.getManageablePackageName(),
085                this.getManageableServiceCreateExceptionName(),
086                null);
087    }
088
089    /**
090     * @see EJB3ManageableEntityFacadeLogic#handleGetManageableServiceReadExceptionName()
091     */
092    @Override
093    protected String handleGetManageableServiceReadExceptionName()
094    {
095        String exceptionNamePattern = (String)this.getConfiguredProperty(READ_EXCEPTION_NAME_PATTERN);
096
097        return MessageFormat.format(
098                exceptionNamePattern,
099                StringUtils.trimToEmpty(this.getName()));
100    }
101
102    /**
103     * @see EJB3ManageableEntityFacadeLogic#handleGetFullyQualifiedManageableServiceReadExceptionName()
104     */
105    @Override
106    protected String handleGetFullyQualifiedManageableServiceReadExceptionName()
107    {
108        return EJB3MetafacadeUtils.getFullyQualifiedName(
109                this.getManageablePackageName(),
110                this.getManageableServiceReadExceptionName(),
111                null);
112    }
113
114    /**
115     * @see EJB3ManageableEntityFacadeLogic#handleGetManageableServiceUpdateExceptionName()
116     */
117    @Override
118    protected String handleGetManageableServiceUpdateExceptionName()
119    {
120        String exceptionNamePattern = (String)this.getConfiguredProperty(UPDATE_EXCEPTION_NAME_PATTERN);
121
122        return MessageFormat.format(
123                exceptionNamePattern,
124                StringUtils.trimToEmpty(this.getName()));
125    }
126
127    /**
128     * @see EJB3ManageableEntityFacadeLogic#handleGetFullyQualifiedManageableServiceUpdateExceptionName()
129     */
130    @Override
131    protected String handleGetFullyQualifiedManageableServiceUpdateExceptionName()
132    {
133        return EJB3MetafacadeUtils.getFullyQualifiedName(
134                this.getManageablePackageName(),
135                this.getManageableServiceUpdateExceptionName(),
136                null);
137    }
138
139    /**
140     * @see EJB3ManageableEntityFacadeLogic#handleGetManageableServiceDeleteExceptionName()
141     */
142    @Override
143    protected String handleGetManageableServiceDeleteExceptionName()
144    {
145        String exceptionNamePattern = (String)this.getConfiguredProperty(DELETE_EXCEPTION_NAME_PATTERN);
146
147        return MessageFormat.format(
148                exceptionNamePattern,
149                StringUtils.trimToEmpty(this.getName()));
150    }
151
152    /**
153     * @see EJB3ManageableEntityFacadeLogic#handleGetFullyQualifiedManageableServiceDeleteExceptionName()
154     */
155    @Override
156    protected String handleGetFullyQualifiedManageableServiceDeleteExceptionName()
157    {
158        return EJB3MetafacadeUtils.getFullyQualifiedName(
159                this.getManageablePackageName(),
160                this.getManageableServiceDeleteExceptionName(),
161                null);
162    }
163
164    /**
165     * @return getConfiguredProperty(EJB3Globals.PERSISTENCE_CONTEXT_UNIT_NAME)
166     * @see EJB3EntityFacadeLogic#handleGetDefaultPersistenceContextUnitName()
167     */
168    protected String handleGetDefaultPersistenceContextUnitName()
169    {
170        return StringUtils.trimToEmpty(
171                ObjectUtils.toString(this.getConfiguredProperty(EJB3Globals.PERSISTENCE_CONTEXT_UNIT_NAME)));
172    }
173
174    /**
175     * @see EJB3ManageableEntityFacadeLogic#handleGetJndiNamePrefix()
176     */
177    @Override
178    protected String handleGetJndiNamePrefix()
179    {
180        return this.isConfiguredProperty(SERVICE_JNDI_NAME_PREFIX) ?
181                ObjectUtils.toString(this.getConfiguredProperty(SERVICE_JNDI_NAME_PREFIX)) : null;
182    }
183
184    /**
185     * @see EJB3ManageableEntityFacadeLogic#handleGetManageableServiceBaseName()
186     */
187    @Override
188    protected String handleGetManageableServiceBaseName()
189    {
190        String exceptionNamePattern = (String)this.getConfiguredProperty(MANAGEABLE_SERVICE_BASE_NAME_PATTERN);
191
192        return MessageFormat.format(
193            exceptionNamePattern,
194                StringUtils.trimToEmpty(this.getManageableServiceName()));
195    }
196
197    /**
198     * @see EJB3ManageableEntityFacadeLogic#handleGetManageableServiceBaseFullPath()
199     */
200    @Override
201    protected String handleGetManageableServiceBaseFullPath()
202    {
203        return StringUtils.replace(this.getFullyQualifiedManageableServiceBaseName(), ".", "/");
204    }
205
206    /**
207     * @see EJB3ManageableEntityFacadeLogic#handleGetFullyQualifiedManageableServiceBaseName()
208     */
209    @Override
210    protected String handleGetFullyQualifiedManageableServiceBaseName()
211    {
212        return EJB3MetafacadeUtils.getFullyQualifiedName(
213                this.getManageablePackageName(),
214                this.getManageableServiceBaseName(),
215                null);
216    }
217
218    /**
219     * @see EJB3ManageableEntityFacadeLogic#handleGetManageableRolesAllowed()
220     */
221    @Override
222    protected String handleGetManageableRolesAllowed()
223    {
224        StringBuilder rolesAllowed = null;
225        String[] roles = StringUtils.split(
226                StringUtils.trimToEmpty(
227                        ObjectUtils.toString(this.getConfiguredProperty(EJB3Globals.MANAGEABLE_ROLES_ALLOWED))),
228                ',');
229        String separator = "";
230
231        for (int i = 0; i < roles.length; i++)
232        {
233            if (rolesAllowed == null)
234            {
235                rolesAllowed = new StringBuilder();
236            }
237            rolesAllowed.append(separator);
238            rolesAllowed.append('"');
239            rolesAllowed.append(roles[i]);
240            rolesAllowed.append('"');
241            separator = ", ";
242        }
243        return rolesAllowed != null ? rolesAllowed.toString() : null;
244    }
245
246    /**
247     * @see EJB3ManageableEntityFacadeLogic#getIdentifier()
248     *
249     * Override the implementation in EJB3EntityFacade as UML2 models cannot retrieve the identifier via the
250     * super EJB3EntityFacade.
251     */
252    @Override
253    public ModelElementFacade getIdentifier()
254    {
255        return super.getIdentifiers().iterator().next();
256    }
257
258    /**
259     * @see EJB3ManageableEntityFacadeLogic#getAllInstanceAttributes()
260     *
261     * Override the implementation in EJB3EntityFacade as UML2 models will not get an
262     * EJB3ManageableEntityAttributeFacade when retrieving the attributes.
263     */
264    @Override public List getAllInstanceAttributes()
265    {
266        return EJB3MetafacadeUtils.getAllInstanceAttributes(this);
267    }
268
269    /**
270     * @see EJB3ManageableEntityFacadeLogic#getInheritedInstanceAttributes()
271     *
272     * Override the implementation in EJB3EntityFacade as UML2 models will not get an
273     * EJB3ManageableEntityAttributeFacade when retrieving the attributes.
274     */
275    @Override
276    public List getInheritedInstanceAttributes()
277    {
278        return EJB3MetafacadeUtils.getInheritedInstanceAttributes(this);
279    }
280
281    /**
282     * @see EJB3ManageableEntityFacadeLogic#getInstanceAttributes(boolean, boolean)
283     *
284     * Override the implementation in EJB3EntityFacade as UML2 models will not get an
285     * EJB3ManageableEntityAttributeFacade when retrieving the attributes.
286     */
287    @Override
288    public Collection getInstanceAttributes(final boolean follow, final boolean withIdentifiers)
289    {
290        final Collection attributes = this.getAttributes(follow, withIdentifiers);
291        CollectionUtils.filter(
292            attributes,
293            new Predicate()
294            {
295                public boolean evaluate(final Object object)
296                {
297                    boolean valid = true;
298                    if (object instanceof EntityAttribute)
299                    {
300                        valid = !((EntityAttribute)object).isStatic();
301                    }
302                    return valid;
303                }
304            });
305        return attributes;
306    }
307}