View Javadoc
1   package org.andromda.cartridges.ejb3.metafacades;
2   
3   import java.text.MessageFormat;
4   import java.util.Collection;
5   import java.util.List;
6   import org.andromda.cartridges.ejb3.EJB3Globals;
7   import org.andromda.metafacades.uml.EntityAttribute;
8   import org.andromda.metafacades.uml.ModelElementFacade;
9   import org.apache.commons.collections.CollectionUtils;
10  import org.apache.commons.collections.Predicate;
11  import org.apache.commons.lang.ObjectUtils;
12  import org.apache.commons.lang.StringUtils;
13  
14  /**
15   * MetafacadeLogic implementation for org.andromda.cartridges.ejb3.metafacades.EJB3ManageableEntityFacade.
16   *
17   * @see EJB3ManageableEntityFacade
18   */
19  public class EJB3ManageableEntityFacadeLogicImpl
20      extends EJB3ManageableEntityFacadeLogic
21  {
22      private static final long serialVersionUID = 34L;
23      /**
24       * The property which stores the pattern defining the manageable create exception name.
25       */
26      public static final String CREATE_EXCEPTION_NAME_PATTERN = "manageableCreateExceptionNamePattern";
27  
28      /**
29       * The property which stores the pattern defining the manageable read exception name.
30       */
31      public static final String READ_EXCEPTION_NAME_PATTERN = "manageableReadExceptionNamePattern";
32  
33      /**
34       * The property which stores the pattern defining the manageable update exception name.
35       */
36      public static final String UPDATE_EXCEPTION_NAME_PATTERN = "manageableUpdateExceptionNamePattern";
37  
38      /**
39       * The property which stores the pattern defining the manageable delete exception name.
40       */
41      public static final String DELETE_EXCEPTION_NAME_PATTERN = "manageableDeleteExceptionNamePattern";
42  
43      /**
44       * The property that stores the JNDI name prefix.
45       */
46      public static final String SERVICE_JNDI_NAME_PREFIX = "jndiNamePrefix";
47  
48      /**
49       * The property that stores the manageable service base name pattern
50       */
51      public static final String MANAGEABLE_SERVICE_BASE_NAME_PATTERN = "manageableServiceBaseNamePattern";
52  
53      /**
54       * Constructor
55       *
56       * @param metaObject
57       * @param context
58       */
59      public EJB3ManageableEntityFacadeLogicImpl(final Object metaObject, final String context)
60      {
61          super (metaObject, context);
62      }
63  
64      /**
65       * @see EJB3ManageableEntityFacadeLogic#handleGetManageableServiceCreateExceptionName()
66       */
67      @Override
68      protected String handleGetManageableServiceCreateExceptionName()
69      {
70          String exceptionNamePattern = (String)this.getConfiguredProperty(CREATE_EXCEPTION_NAME_PATTERN);
71  
72          return MessageFormat.format(
73                  exceptionNamePattern,
74                  StringUtils.trimToEmpty(this.getName()));
75      }
76  
77      /**
78       * @see EJB3ManageableEntityFacadeLogic#handleGetFullyQualifiedManageableServiceCreateExceptionName()
79       */
80      @Override
81      protected String handleGetFullyQualifiedManageableServiceCreateExceptionName()
82      {
83          return EJB3MetafacadeUtils.getFullyQualifiedName(
84                  this.getManageablePackageName(),
85                  this.getManageableServiceCreateExceptionName(),
86                  null);
87      }
88  
89      /**
90       * @see EJB3ManageableEntityFacadeLogic#handleGetManageableServiceReadExceptionName()
91       */
92      @Override
93      protected String handleGetManageableServiceReadExceptionName()
94      {
95          String exceptionNamePattern = (String)this.getConfiguredProperty(READ_EXCEPTION_NAME_PATTERN);
96  
97          return MessageFormat.format(
98                  exceptionNamePattern,
99                  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 }