001: package org.andromda.cartridges.hibernate.metafacades;
002:
003: import java.util.Collection;
004:
005: import org.andromda.cartridges.hibernate.HibernateProfile;
006: import org.andromda.core.common.ExceptionUtils;
007: import org.andromda.metafacades.uml.ClassifierFacade;
008: import org.andromda.metafacades.uml.FilteredCollection;
009: import org.andromda.metafacades.uml.ModelElementFacade;
010: import org.andromda.metafacades.uml.OperationFacade;
011: import org.apache.commons.collections.CollectionUtils;
012: import org.apache.commons.collections.Predicate;
013: import org.apache.commons.lang.StringUtils;
014:
015: /**
016: * Contains utilities for use with Hibernate metafacades.
017: *
018: * @author Chad Brandon
019: */
020: class HibernateMetafacadeUtils {
021: /**
022: * Gets the view type for the passed in <code>classifier</code>. If the
023: * view type can be retrieved from the <code>classifier</code>, then that
024: * is used, otherwise the <code>defaultViewType</code> is returned.
025: *
026: * @return String the view type name.
027: */
028: static String getViewType(ClassifierFacade classifier,
029: String defaultViewType) {
030: ExceptionUtils.checkNull("classifer", classifier);
031: String viewType = null;
032: if (classifier
033: .hasStereotype(HibernateProfile.STEREOTYPE_SERVICE)) {
034: String viewTypeValue = (String) classifier
035: .findTaggedValue(HibernateProfile.TAGGEDVALUE_EJB_VIEWTYPE);
036:
037: // if the view type wasn't found, search all super classes
038: if (StringUtils.isEmpty(viewTypeValue)) {
039: viewType = (String) CollectionUtils.find(classifier
040: .getAllGeneralizations(), new Predicate() {
041: public boolean evaluate(Object object) {
042: return ((ModelElementFacade) object)
043: .findTaggedValue(HibernateProfile.TAGGEDVALUE_EJB_VIEWTYPE) != null;
044: }
045: });
046: }
047: if (StringUtils.isNotEmpty(viewTypeValue)) {
048: viewType = viewTypeValue;
049: }
050: }
051: if (StringUtils.isEmpty(viewType)) {
052: viewType = defaultViewType;
053: }
054: return viewType.toLowerCase();
055: }
056:
057: /**
058: * Creates a fully qualified name from the given <code>packageName</code>,
059: * <code>name</code>, and <code>suffix</code>.
060: *
061: * @param packageName the name of the model element package.
062: * @param name the name of the model element.
063: * @param suffix the suffix to append.
064: * @return the new fully qualified name.
065: */
066: static String getFullyQualifiedName(String packageName,
067: String name, String suffix) {
068: StringBuffer fullyQualifiedName = new StringBuffer(StringUtils
069: .trimToEmpty(packageName));
070: if (StringUtils.isNotBlank(packageName)) {
071: fullyQualifiedName.append('.');
072: }
073: fullyQualifiedName.append(StringUtils.trimToEmpty(name));
074: fullyQualifiedName.append(StringUtils.trimToEmpty(suffix));
075: return fullyQualifiedName.toString();
076: }
077:
078: /**
079: * filters all static operations
080: */
081: static java.util.Collection filterBusinessOperations(
082: Collection operations) {
083: Collection businessOperations = new FilteredCollection(
084: operations) {
085: public boolean evaluate(Object object) {
086: return !((OperationFacade) object).isStatic();
087: }
088: };
089: return businessOperations;
090: }
091:
092: /**
093: * Checks whether the passed in operation is a query and should be using named parameters.
094: *
095: * @param operation the operation.
096: * @param defaultUseNamedParameters the default value.
097: * @return whether named parameters should be used.
098: */
099: static boolean getUseNamedParameters(OperationFacade operation,
100: boolean defaultUseNamedParameters) {
101: ExceptionUtils.checkNull("operation", operation);
102: boolean useNamedParameters = defaultUseNamedParameters;
103: if (operation.isQuery()) {
104: String useNamedParametersValue = StringUtils
105: .trimToEmpty((String) operation
106: .findTaggedValue(HibernateProfile.TAGGEDVALUE_HIBERNATE_USE_NAMED_PARAMETERS));
107: if (StringUtils.isNotEmpty(useNamedParametersValue)) {
108: useNamedParameters = Boolean.valueOf(
109: useNamedParametersValue).booleanValue();
110: }
111: }
112: return useNamedParameters;
113: }
114:
115: }
|