001: /*
002: * Copyright 2005-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package net.sf.dozer.util.mapping.classmap;
017:
018: import java.util.ArrayList;
019: import java.util.List;
020:
021: import net.sf.dozer.util.mapping.converters.CustomConverterContainer;
022: import net.sf.dozer.util.mapping.fieldmap.FieldMap;
023: import net.sf.dozer.util.mapping.util.MapperConstants;
024: import net.sf.dozer.util.mapping.util.MappingUtils;
025:
026: import org.apache.commons.lang.builder.ToStringBuilder;
027: import org.apache.commons.lang.builder.ToStringStyle;
028:
029: /**
030: * Internal class that represents a class mapping definition. Holds all of the information about a single class mapping.
031: * Only intended for internal use.
032: *
033: * @author garsombke.franz
034: * @author sullins.ben
035: * @author tierney.matt
036: *
037: */
038: public class ClassMap {
039:
040: private Configuration globalConfiguration;
041: private DozerClass srcClass;
042: private DozerClass destClass;
043: private List fieldMaps = new ArrayList();
044: private List allowedExceptions = new ArrayList();
045: private String type;
046: private String dateFormat;
047: private String beanFactory;
048: private boolean mapNull = MapperConstants.DEFAULT_MAP_NULL_POLICY;
049: private boolean mapEmptyString = MapperConstants.DEFAULT_MAP_EMPTY_STRING_POLICY;
050: private Boolean wildcard;
051: private Boolean stopOnErrors;
052: private Boolean trimStrings;
053: private CustomConverterContainer customConverters;
054: private String mapId;
055: private String relationshipType;
056:
057: public ClassMap(Configuration globalConfiguration) {
058: this .globalConfiguration = globalConfiguration;
059: }
060:
061: public List getFieldMaps() {
062: return fieldMaps;
063: }
064:
065: public boolean isStopOnErrors() {
066: return stopOnErrors != null ? stopOnErrors.booleanValue()
067: : globalConfiguration.getStopOnErrors().booleanValue();
068: }
069:
070: public void setStopOnErrors(Boolean stopOnErrors) {
071: this .stopOnErrors = stopOnErrors;
072: }
073:
074: public boolean isTrimStrings() {
075: return trimStrings != null ? trimStrings.booleanValue()
076: : globalConfiguration.getTrimStrings().booleanValue();
077: }
078:
079: public void setTrimStrings(Boolean trimStrings) {
080: this .trimStrings = trimStrings;
081: }
082:
083: public List getAllowedExceptions() {
084: if (!allowedExceptions.isEmpty()) {
085: return allowedExceptions;
086: } else {
087: return globalConfiguration.getAllowedExceptions() != null ? globalConfiguration
088: .getAllowedExceptions().getExceptions()
089: : allowedExceptions;
090: }
091: }
092:
093: public void setAllowedExceptions(List allowedExceptions) {
094: this .allowedExceptions = allowedExceptions;
095: }
096:
097: public FieldMap getFieldMapUsingDest(String destFieldName) {
098: FieldMap result = null;
099:
100: if (fieldMaps != null) {
101: int size = fieldMaps.size();
102: for (int i = 0; i < size; i++) {
103: FieldMap fieldMap = (FieldMap) fieldMaps.get(i);
104: String fieldName = fieldMap.getDestFieldName();
105: String alternateFieldName = fieldName.substring(0, 1)
106: .toLowerCase()
107: + fieldName.substring(1);
108:
109: // Check for exact match on field name. Also, check against alternate field name. The alternate field
110: // name is used just in case the attribute was specified in the dozer xml file starting in a Capital letter.
111: // This prevents the field from getting double mapped in the case that the class attr is named "field1" but in
112: // the dozer xml is it specified as "Field1". This should never happen, but check just in case since the use case
113: // doesnt actually error out. It just double maps which is a problem when the data type is a Collections.
114: if (fieldName.equals(destFieldName)
115: || alternateFieldName.equals(destFieldName)) {
116: result = fieldMap;
117: break;
118: }
119: }
120: }
121: return result;
122: }
123:
124: public FieldMap getFieldMapUsingSrc(String srcFieldName) {
125: FieldMap result = null;
126:
127: if (fieldMaps != null) {
128: for (int i = 0; i < fieldMaps.size(); i++) {
129: FieldMap fieldMap = (FieldMap) fieldMaps.get(i);
130: String fieldName = fieldMap.getSrcFieldName();
131:
132: if ((fieldName != null)
133: && fieldName.equals(srcFieldName)) {
134: result = fieldMap;
135: break;
136: }
137: }
138: }
139: return result;
140: }
141:
142: public void setFieldMaps(List fieldMaps) {
143: this .fieldMaps = fieldMaps;
144: }
145:
146: public void addFieldMapping(FieldMap fieldMap) {
147: fieldMaps.add(fieldMap);
148: }
149:
150: public void removeFieldMapping(FieldMap fieldMap) {
151: fieldMaps.remove(fieldMap);
152: }
153:
154: public boolean isWildcard() {
155: return wildcard != null ? wildcard.booleanValue()
156: : globalConfiguration.getWildcard().booleanValue();
157: }
158:
159: public void setWildcard(Boolean wildcardPolicy) {
160: this .wildcard = wildcardPolicy;
161: }
162:
163: public String getType() {
164: return type;
165: }
166:
167: public void setType(String type) {
168: this .type = type;
169: }
170:
171: public String getDateFormat() {
172: return !MappingUtils.isBlankOrNull(dateFormat) ? dateFormat
173: : globalConfiguration.getDateFormat();
174: }
175:
176: public void setDateFormat(String dateFormat) {
177: this .dateFormat = dateFormat;
178: }
179:
180: public CustomConverterContainer getCustomConverters() {
181: return customConverters != null ? customConverters
182: : globalConfiguration.getCustomConverters();
183: }
184:
185: public void setCustomConverters(
186: CustomConverterContainer customConverters) {
187: this .customConverters = customConverters;
188: }
189:
190: public Class getSrcClassToMap() {
191: return srcClass.getClassToMap();
192: }
193:
194: public Class getDestClassToMap() {
195: return destClass.getClassToMap();
196: }
197:
198: public boolean isDestMapNull() {
199: return destClass.getMapNull() != null ? destClass.getMapNull()
200: .booleanValue() : mapNull;
201: }
202:
203: public boolean isSrcMapNull() {
204: return srcClass.getMapNull() != null ? srcClass.getMapNull()
205: .booleanValue() : mapNull;
206: }
207:
208: public boolean isDestMapEmptyString() {
209: return destClass.getMapEmptyString() != null ? destClass
210: .getMapEmptyString().booleanValue() : mapEmptyString;
211: }
212:
213: public boolean isSrcMapEmptyString() {
214: return srcClass.getMapEmptyString() != null ? srcClass
215: .getMapEmptyString().booleanValue() : mapEmptyString;
216: }
217:
218: public String getDestClassBeanFactory() {
219: return !MappingUtils.isBlankOrNull(destClass.getBeanFactory()) ? destClass
220: .getBeanFactory()
221: : getBeanFactory();
222: }
223:
224: public String getSrcClassBeanFactory() {
225: return !MappingUtils.isBlankOrNull(srcClass.getBeanFactory()) ? srcClass
226: .getBeanFactory()
227: : getBeanFactory();
228: }
229:
230: public String getDestClassBeanFactoryId() {
231: return destClass.getFactoryBeanId();
232: }
233:
234: public String getSrcClassBeanFactoryId() {
235: return srcClass.getFactoryBeanId();
236: }
237:
238: public String getSrcClassMapGetMethod() {
239: return srcClass.getMapGetMethod();
240: }
241:
242: public String getSrcClassMapSetMethod() {
243: return srcClass.getMapSetMethod();
244: }
245:
246: public String getDestClassMapGetMethod() {
247: return destClass.getMapGetMethod();
248: }
249:
250: public String getDestClassMapSetMethod() {
251: return destClass.getMapSetMethod();
252: }
253:
254: public String getDestClassCreateMethod() {
255: return destClass.getCreateMethod();
256: }
257:
258: public String getSrcClassCreateMethod() {
259: return srcClass.getCreateMethod();
260: }
261:
262: public void setSrcClassCreateMethod(String createMethod) {
263: srcClass.setCreateMethod(createMethod);
264: }
265:
266: public void setDestClassCreateMethod(String createMethod) {
267: destClass.setCreateMethod(createMethod);
268: }
269:
270: public boolean isDestClassMapTypeCustomGetterSetter() {
271: return destClass.isMapTypeCustomGetterSetterClass();
272: }
273:
274: public boolean isSrcClassMapTypeCustomGetterSetter() {
275: return srcClass.isMapTypeCustomGetterSetterClass();
276: }
277:
278: public void setSrcClass(DozerClass srcClass) {
279: this .srcClass = srcClass;
280: }
281:
282: public void setDestClass(DozerClass destClass) {
283: this .destClass = destClass;
284: }
285:
286: public String getDestClassName() {
287: return destClass.getName();
288: }
289:
290: public String getSrcClassName() {
291: return srcClass.getName();
292: }
293:
294: public String getBeanFactory() {
295: return !MappingUtils.isBlankOrNull(beanFactory) ? beanFactory
296: : globalConfiguration.getBeanFactory();
297: }
298:
299: public void setBeanFactory(String beanFactory) {
300: this .beanFactory = beanFactory;
301: }
302:
303: public String getMapId() {
304: return mapId;
305: }
306:
307: public void setMapId(String mapId) {
308: this .mapId = mapId;
309: }
310:
311: public void setMapNull(boolean mapNull) {
312: this .mapNull = mapNull;
313: }
314:
315: public void setMapEmptyString(boolean mapEmptyString) {
316: this .mapEmptyString = mapEmptyString;
317: }
318:
319: public Configuration getGlobalConfiguration() {
320: return globalConfiguration;
321: }
322:
323: public String getRelationshipType() {
324: return relationshipType != null ? relationshipType
325: : globalConfiguration.getRelationshipType();
326: }
327:
328: public void setRelationshipType(String relationshipType) {
329: this .relationshipType = relationshipType;
330: }
331:
332: public String toString() {
333: return new ToStringBuilder(this , ToStringStyle.MULTI_LINE_STYLE)
334: .append("source class", getSrcClassName()).append(
335: "destination class", getDestClassName())
336: .append("map-id", mapId).toString();
337: }
338: }
|