View Javadoc
1   package org.andromda.cartridges.hibernate.metafacades;
2   
3   import java.util.Collection;
4   import org.andromda.cartridges.hibernate.HibernateProfile;
5   import org.andromda.core.common.ExceptionUtils;
6   import org.andromda.metafacades.uml.ClassifierFacade;
7   import org.andromda.metafacades.uml.FilteredCollection;
8   import org.andromda.metafacades.uml.ModelElementFacade;
9   import org.andromda.metafacades.uml.OperationFacade;
10  import org.apache.commons.collections.CollectionUtils;
11  import org.apache.commons.collections.Predicate;
12  import org.apache.commons.lang.StringUtils;
13  
14  /**
15   * Contains utilities for use with Hibernate metafacades.
16   *
17   * @author Chad Brandon
18   */
19  class HibernateMetafacadeUtils
20  {
21      /**
22       * Gets the view type for the passed in <code>classifier</code>. If the
23       * view type can be retrieved from the <code>classifier</code>, then that
24       * is used, otherwise the <code>defaultViewType</code> is returned.
25       * @param classifier
26       * @param defaultViewType
27       * @return String the view type name.
28       */
29      static String getViewType(
30          ClassifierFacade classifier,
31          String defaultViewType)
32      {
33          ExceptionUtils.checkNull("classifer", classifier);
34          String viewType = null;
35          if (classifier.hasStereotype(HibernateProfile.STEREOTYPE_SERVICE))
36          {
37              String viewTypeValue = (String)classifier.findTaggedValue(HibernateProfile.TAGGEDVALUE_EJB_VIEWTYPE);
38  
39              // if the view type wasn't found, search all super classes
40              if (StringUtils.isEmpty(viewTypeValue))
41              {
42                  viewType =
43                      (String)CollectionUtils.find(
44                          classifier.getAllGeneralizations(),
45                          new Predicate()
46                          {
47                              public boolean evaluate(Object object)
48                              {
49                                  return ((ModelElementFacade)object).findTaggedValue(
50                                      HibernateProfile.TAGGEDVALUE_EJB_VIEWTYPE) != null;
51                              }
52                          });
53              }
54              if (StringUtils.isNotBlank(viewTypeValue))
55              {
56                  viewType = viewTypeValue;
57              }
58          }
59          if (StringUtils.isEmpty(viewType) || viewType==null)
60          {
61              viewType = defaultViewType;
62          }
63          return viewType.toLowerCase();
64      }
65  
66      /**
67       * Creates a fully qualified Hibernate name from the given <code>packageName</code>,
68       * <code>name</code>, and <code>suffix</code>. Not to be used for attributes, types, or parameters
69       * which could possibly be java.lang. types.
70       *
71       * @param packageName the name of the model element package.
72       * @param name the name of the model element.
73       * @param suffix the suffix to append.
74       * @return the new fully qualified name.
75       */
76      static String getFullyQualifiedName(
77          String packageName,
78          String name,
79          String suffix)
80      {
81          StringBuilder fullyQualifiedName = new StringBuilder(StringUtils.trimToEmpty(packageName));
82          if (StringUtils.isNotBlank(packageName))
83          {
84              fullyQualifiedName.append('.');
85          }
86          fullyQualifiedName.append(StringUtils.trimToEmpty(name));
87          fullyQualifiedName.append(StringUtils.trimToEmpty(suffix));
88          return fullyQualifiedName.toString();
89      }
90  
91      /**
92       * filters all static operations
93       * @param operations
94       * @return businessOperations
95       */
96      static Collection<OperationFacade> filterBusinessOperations(Collection<OperationFacade> operations)
97      {
98          Collection<OperationFacade> businessOperations =
99              new FilteredCollection(operations)
100             {
101                 private static final long serialVersionUID = 34L;
102                 public boolean evaluate(Object object)
103                 {
104                     return !((OperationFacade)object).isStatic();
105                 }
106             };
107         return businessOperations;
108     }
109 
110     /**
111      * Checks whether the passed in operation is a query and should be using named parameters.
112      *
113      * @param operation the operation.
114      * @param defaultUseNamedParameters the default value.
115      * @return whether named parameters should be used.
116      */
117     static boolean getUseNamedParameters(OperationFacade operation,
118         boolean defaultUseNamedParameters)
119     {
120         ExceptionUtils.checkNull("operation", operation);
121         boolean useNamedParameters = defaultUseNamedParameters;
122         if (operation.isQuery())
123         {
124             String useNamedParametersValue = StringUtils.trimToEmpty((String)operation
125                     .findTaggedValue(HibernateProfile.TAGGEDVALUE_HIBERNATE_USE_NAMED_PARAMETERS));
126             if (StringUtils.isNotBlank(useNamedParametersValue))
127             {
128                 useNamedParameters = Boolean.valueOf(useNamedParametersValue).booleanValue();
129             }
130         }
131         return useNamedParameters;
132     }
133 }