001: package org.andromda.cartridges.spring.metafacades;
002:
003: import org.andromda.cartridges.spring.SpringProfile;
004: import org.andromda.core.common.ExceptionUtils;
005: import org.andromda.metafacades.uml.ClassifierFacade;
006: import org.andromda.metafacades.uml.ModelElementFacade;
007: import org.andromda.metafacades.uml.OperationFacade;
008: import org.andromda.metafacades.uml.UMLProfile;
009: import org.apache.commons.collections.CollectionUtils;
010: import org.apache.commons.collections.Predicate;
011: import org.apache.commons.lang.StringUtils;
012:
013: /**
014: * Contains utilities specific to dealing with the Spring cartridge metafacades.
015: *
016: * @author Chad Brandon
017: * @author Peter Friese
018: */
019: class SpringMetafacadeUtils {
020: /**
021: * Creates a fully qualified name from the given <code>packageName</code>, <code>name</code>, and
022: * <code>suffix</code>.
023: *
024: * @param packageName the name of the model element package.
025: * @param name the name of the model element.
026: * @param suffix the suffix to append.
027: * @return the new fully qualified name.
028: */
029: static String getFullyQualifiedName(String packageName,
030: String name, String suffix) {
031: StringBuffer fullyQualifiedName = new StringBuffer(StringUtils
032: .trimToEmpty(packageName));
033: if (StringUtils.isNotBlank(packageName)) {
034: fullyQualifiedName.append(".");
035: }
036: fullyQualifiedName.append(StringUtils.trimToEmpty(name));
037: if (StringUtils.isNotEmpty(suffix)) {
038: fullyQualifiedName.append(StringUtils.trimToEmpty(suffix));
039: }
040: return fullyQualifiedName.toString();
041: }
042:
043: /**
044: * Creates a fully qualified name from the given <code>packageName</code>, <code>name</code>, and
045: * <code>suffix</code>.
046: *
047: * @param packageName the name of the model element package.
048: * @param name the name of the model element.
049: * @return the new fully qualified name.
050: */
051: static String getFullyQualifiedName(String packageName, String name) {
052: return getFullyQualifiedName(packageName, name, null);
053: }
054:
055: /**
056: * Gets the remoting type for the passed in <code>classifier</code>. If the remoting type can be retrieved from the
057: * <code>classifier</code>, then that is used, otherwise the <code>defaultRemotingType</code> is returned.
058: *
059: * @return String the remoting type name.
060: */
061: static String getServiceRemotingType(ClassifierFacade classifier,
062: String defaultServiceRemotingType) {
063: ExceptionUtils.checkNull("classifer", classifier);
064: String remotingType = null;
065: if (classifier.hasStereotype(UMLProfile.STEREOTYPE_SERVICE)) {
066: String remotingTypeValue = (String) classifier
067: .findTaggedValue(SpringProfile.TAGGEDVALUE_SPRING_SERVICE_REMOTING_TYPE);
068: // if the remoting type wasn't found, search all super classes
069: if (StringUtils.isEmpty(remotingTypeValue)) {
070: remotingType = (String) CollectionUtils.find(classifier
071: .getAllGeneralizations(), new Predicate() {
072: public boolean evaluate(Object object) {
073: return ((ModelElementFacade) object)
074: .findTaggedValue(SpringProfile.TAGGEDVALUE_SPRING_SERVICE_REMOTING_TYPE) != null;
075: }
076: });
077: }
078: if (StringUtils.isNotEmpty(remotingTypeValue)) {
079: remotingType = remotingTypeValue;
080: }
081: }
082: if (StringUtils.isEmpty(remotingType)) {
083: remotingType = defaultServiceRemotingType;
084: }
085: return remotingType.toLowerCase().trim();
086: }
087:
088: /**
089: * Get the interceptors for the passed in <code>classifier</code>. If the interceptors can be retrieved from the
090: * <code>classifier</code>, then these will be used, otherwise the <code>defaultInterceptors</code> are
091: * returned.
092: *
093: * @param classifier the classifier whose interceptors we are looking for.
094: * @param defaultInterceptors a list of interceptors to use if the classifier itself has no explicit interceptors.
095: *
096: * @return String[] the interceptors.
097: */
098: static String[] getServiceInterceptors(ClassifierFacade classifier,
099: String[] defaultInterceptors) {
100: ExceptionUtils.checkNull("classifier", classifier);
101: String[] interceptors = null;
102: if (classifier.hasStereotype(UMLProfile.STEREOTYPE_SERVICE)) {
103: String interceptorsValue = (String) classifier
104: .findTaggedValue(SpringProfile.TAGGEDVALUE_SPRING_SERVICE_INTERCEPTORS);
105: // if the interceptors weren't found, search all super classes
106: if (StringUtils.isEmpty(interceptorsValue)) {
107: interceptorsValue = (String) CollectionUtils.find(
108: classifier.getAllGeneralizations(),
109: new Predicate() {
110: public boolean evaluate(Object object) {
111: return ((ModelElementFacade) object)
112: .findTaggedValue(SpringProfile.TAGGEDVALUE_SPRING_SERVICE_INTERCEPTORS) != null;
113: }
114: });
115: }
116: // interceptors are a comma-separated list of strings, go and split the list
117: if (StringUtils.isNotEmpty(interceptorsValue)) {
118: interceptors = interceptorsValue.split(",");
119: }
120: }
121: if (interceptors == null || interceptors.length == 0) {
122: interceptors = defaultInterceptors;
123: }
124: return interceptors;
125: }
126:
127: /**
128: * Gets the remote service port for the passed in <code>classifier</code>. If the remote service
129: * port can be retrieved from the <code>classifier</code>, then that is used, otherwise the
130: * <code>defaultRemoteServicePort</code> is returned.
131: *
132: * @return String the remote service port.
133: */
134: static String getServiceRemotePort(ClassifierFacade classifier,
135: String defaultRemoteServicePort) {
136: ExceptionUtils.checkNull("classifer", classifier);
137: String remoteServicePort = null;
138: if (classifier.hasStereotype(UMLProfile.STEREOTYPE_SERVICE)) {
139: String remoteServicePortValue = (String) classifier
140: .findTaggedValue(SpringProfile.TAGGEDVALUE_SPRING_SERVICE_REMOTE_PORT);
141: // if the remote service port wasn't found, search all super classes
142: if (StringUtils.isEmpty(remoteServicePortValue)) {
143: remoteServicePort = (String) CollectionUtils.find(
144: classifier.getAllGeneralizations(),
145: new Predicate() {
146: public boolean evaluate(Object object) {
147: return ((ModelElementFacade) object)
148: .findTaggedValue(SpringProfile.TAGGEDVALUE_SPRING_SERVICE_REMOTE_PORT) != null;
149: }
150: });
151: }
152: if (StringUtils.isNotEmpty(remoteServicePortValue)) {
153: remoteServicePort = remoteServicePortValue;
154: }
155: }
156: if (StringUtils.isEmpty(remoteServicePort)) {
157: remoteServicePort = defaultRemoteServicePort;
158: }
159: return remoteServicePort.toLowerCase().trim();
160: }
161:
162: /**
163: * Checks whether the passed in operation is a query and should be using named parameters.
164: *
165: * @param operation the operation.
166: * @param defaultUseNamedParameters the default value.
167: * @return whether named parameters should be used.
168: */
169: static boolean getUseNamedParameters(OperationFacade operation,
170: boolean defaultUseNamedParameters) {
171: ExceptionUtils.checkNull("operation", operation);
172: boolean useNamedParameters = defaultUseNamedParameters;
173: if (operation.isQuery()) {
174: String useNamedParametersValue = StringUtils
175: .trimToEmpty((String) operation
176: .findTaggedValue(SpringProfile.TAGGEDVALUE_HIBERNATE_USE_NAMED_PARAMETERS));
177: if (StringUtils.isNotEmpty(useNamedParametersValue)) {
178: useNamedParameters = Boolean.valueOf(
179: useNamedParametersValue).booleanValue();
180: }
181: }
182: return useNamedParameters;
183: }
184: }
|