1 16 package net.sf.dozer.util.mapping.util; 17 18 19 import java.util.Collection ; 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 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 createFieldMappingErrorMsg(Object sourceObj, FieldMap fieldMapping, 38 Object sourceFieldValue, Object destObj, Throwable t) { 39 String sourceClassName = null; 40 if (sourceObj != null) { 41 sourceClassName = sourceObj.getClass().getName(); 42 } 43 44 String sourceValueType = null; 45 if (sourceFieldValue != null) { 46 sourceValueType = sourceFieldValue.getClass().toString(); 47 } else { 48 sourceValueType = fieldMapping.getSourceField().getType(); 49 } 50 51 String destClassName = null; 52 if (destObj != null) { 53 destClassName = destObj.getClass().getName(); 54 } 55 56 String destFieldTypeName = null; 57 try { 58 destFieldTypeName = fieldMapping.getDestFieldType(destObj.getClass()).getName(); 59 } catch (Exception 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 createFieldMappingSuccessMsg(Class sourceClass, Class destClass, String sourceFieldName, String destFieldName, 72 Object sourceFieldValue, Object destFieldValue) { 73 String sourceClassStr = mappingUtils.getClassNameWithoutPackage(sourceClass); 74 String destClassStr = mappingUtils.getClassNameWithoutPackage(destClass); 75 76 return "MAPPED: " + sourceClassStr + "." + sourceFieldName + " --> " + destClassStr + "." + destFieldName 77 + " VALUES: " + getLogOutput(sourceFieldValue) + " --> " + getLogOutput(destFieldValue); 78 } 79 80 private static String getLogOutput(Object object) { 81 String output = "NULL"; 82 if (object == null) { 83 return output; 84 } 85 try { 86 if (object.getClass().isArray() || Collection .class.isAssignableFrom(object.getClass())) { 87 output = ReflectionToStringBuilder.toString(object, ToStringStyle.MULTI_LINE_STYLE); 88 } else { 89 output = object.toString(); 90 } 91 } catch (RuntimeException e) { 92 output = object.toString(); 93 } 94 return output; 95 } 96 } | Popular Tags |