KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > dozer > util > mapping > util > LogMsgFactory


1 /*
2  * Copyright 2005-2007 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package net.sf.dozer.util.mapping.util;
17
18
19 import java.util.Collection JavaDoc;
20
21 import net.sf.dozer.util.mapping.fieldmap.FieldMap;
22
23 import org.apache.commons.lang.builder.ReflectionToStringBuilder;
24 import org.apache.commons.lang.builder.ToStringStyle;
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27
28 /**
29  * @author tierney.matt
30  * @author garsombke.franz
31  */

32 public class LogMsgFactory {
33   private static final Log log = LogFactory.getLog(LogMsgFactory.class);
34   
35   private final MappingUtils mappingUtils = new MappingUtils();
36   
37   public String JavaDoc createFieldMappingErrorMsg(Object JavaDoc sourceObj, FieldMap fieldMapping,
38       Object JavaDoc sourceFieldValue, Object JavaDoc destObj, Throwable JavaDoc t) {
39     String JavaDoc sourceClassName = null;
40     if (sourceObj != null) {
41       sourceClassName = sourceObj.getClass().getName();
42     }
43
44     String JavaDoc sourceValueType = null;
45     if (sourceFieldValue != null) {
46       sourceValueType = sourceFieldValue.getClass().toString();
47     } else {
48       sourceValueType = fieldMapping.getSourceField().getType();
49     }
50
51     String JavaDoc destClassName = null;
52     if (destObj != null) {
53       destClassName = destObj.getClass().getName();
54     }
55
56     String JavaDoc destFieldTypeName = null;
57     try {
58       destFieldTypeName = fieldMapping.getDestFieldType(destObj.getClass()).getName();
59     } catch (Exception JavaDoc e) {
60       log.warn("unable to determine dest field type when build log.error message");
61     }
62
63     return "Field mapping error -->" + "\n MapId: " + fieldMapping.getMapId() + "\n Type: " + fieldMapping.getType()
64         + "\n Source parent class: " + sourceClassName + "\n Source field name: "
65         + fieldMapping.getSourceField().getName() + "\n Source field type: " + sourceValueType
66         + "\n Source field value: " + sourceFieldValue + "\n Dest parent class: " + destClassName
67         + "\n Dest field name: " + fieldMapping.getDestField().getName() + "\n Dest field type: "
68         + destFieldTypeName;
69   }
70   
71   public String JavaDoc createFieldMappingSuccessMsg(Class JavaDoc sourceClass, Class JavaDoc destClass, String JavaDoc sourceFieldName, String JavaDoc destFieldName,
72     Object JavaDoc sourceFieldValue, Object JavaDoc destFieldValue) {
73     String JavaDoc sourceClassStr = mappingUtils.getClassNameWithoutPackage(sourceClass);
74     String JavaDoc destClassStr = mappingUtils.getClassNameWithoutPackage(destClass);
75       
76     return "MAPPED: " + sourceClassStr + "." + sourceFieldName + " --> " + destClassStr + "." + destFieldName
77         + " VALUES: " + getLogOutput(sourceFieldValue) + " --> " + getLogOutput(destFieldValue);
78   }
79   
80   private static String JavaDoc getLogOutput(Object JavaDoc object) {
81     String JavaDoc output = "NULL";
82     if (object == null) {
83       return output;
84     }
85     try {
86       if (object.getClass().isArray() || Collection JavaDoc.class.isAssignableFrom(object.getClass())) {
87         output = ReflectionToStringBuilder.toString(object, ToStringStyle.MULTI_LINE_STYLE);
88       } else {
89         output = object.toString();
90       }
91     } catch (RuntimeException JavaDoc e) {
92       output = object.toString();
93     }
94     return output;
95   }
96 }
Popular Tags