| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.dozer.util; |
| 17 | |
|
| 18 | |
import org.apache.commons.lang3.builder.ReflectionToStringBuilder; |
| 19 | |
import org.apache.commons.lang3.builder.ToStringStyle; |
| 20 | |
import org.dozer.fieldmap.FieldMap; |
| 21 | |
import org.slf4j.Logger; |
| 22 | |
import org.slf4j.LoggerFactory; |
| 23 | |
|
| 24 | |
import java.util.Collection; |
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | 132059 | public final class LogMsgFactory { |
| 33 | |
|
| 34 | 132059 | private final Logger log = LoggerFactory.getLogger(LogMsgFactory.class); |
| 35 | |
|
| 36 | |
public String createFieldMappingErrorMsg(Object srcObj, FieldMap fieldMapping, Object srcFieldValue, Object destObj) { |
| 37 | 16 | String srcClassName = null; |
| 38 | 16 | if (srcObj != null) { |
| 39 | 16 | srcClassName = srcObj.getClass().getName(); |
| 40 | |
} |
| 41 | |
|
| 42 | |
String srcValueType; |
| 43 | 16 | String srcFieldValueString = null; |
| 44 | 16 | if (srcFieldValue != null) { |
| 45 | 16 | srcValueType = srcFieldValue.getClass().toString(); |
| 46 | |
|
| 47 | |
try { |
| 48 | 16 | srcFieldValueString = srcFieldValue.toString(); |
| 49 | 0 | } catch (Exception e) { |
| 50 | 0 | log.error("An exception occurred invoking toString() on the source field value: " + srcFieldValue.getClass().getName(), e); |
| 51 | 0 | srcFieldValueString = "Unable to determine source field value"; |
| 52 | 16 | } |
| 53 | |
} else { |
| 54 | 0 | srcValueType = fieldMapping.getSrcFieldType(); |
| 55 | |
} |
| 56 | |
|
| 57 | 16 | String destClassName = null; |
| 58 | 16 | if (destObj != null) { |
| 59 | 16 | destClassName = destObj.getClass().getName(); |
| 60 | |
} |
| 61 | |
|
| 62 | 16 | String destFieldTypeName = null; |
| 63 | |
try { |
| 64 | 16 | if (destObj != null) { |
| 65 | 16 | destFieldTypeName = fieldMapping.getDestFieldType(destObj.getClass()).getName(); |
| 66 | |
} |
| 67 | 0 | } catch (Exception e) { |
| 68 | 0 | log.warn("Unable to determine dest field type when build log.error message"); |
| 69 | 16 | } |
| 70 | |
|
| 71 | 16 | return "Field mapping error -->" + "\n MapId: " + fieldMapping.getMapId() + "\n Type: " + fieldMapping.getType() |
| 72 | |
+ "\n Source parent class: " + srcClassName + "\n Source field name: " + fieldMapping.getSrcFieldName() |
| 73 | |
+ "\n Source field type: " + srcValueType + "\n Source field value: " + srcFieldValueString + "\n Dest parent class: " |
| 74 | |
+ destClassName + "\n Dest field name: " + fieldMapping.getDestFieldName() + "\n Dest field type: " + destFieldTypeName; |
| 75 | |
} |
| 76 | |
|
| 77 | |
public String createFieldMappingSuccessMsg(Class<?> srcClass, Class<?> destClass, String srcFieldName, String destFieldName, |
| 78 | |
Object srcFieldValue, Object destFieldValue, String classMapId) { |
| 79 | 0 | String srcClassStr = MappingUtils.getClassNameWithoutPackage(srcClass); |
| 80 | 0 | String destClassStr = MappingUtils.getClassNameWithoutPackage(destClass); |
| 81 | |
|
| 82 | 0 | return "MAPPED: " + srcClassStr + "." + srcFieldName + " --> " + destClassStr + "." + destFieldName + " VALUES: " |
| 83 | |
+ getLogOutput(srcFieldValue) + " --> " + getLogOutput(destFieldValue) + " MAPID: " |
| 84 | |
+ (classMapId != null ? classMapId : ""); |
| 85 | |
} |
| 86 | |
|
| 87 | |
private String getLogOutput(Object object) { |
| 88 | 0 | String output = "NULL"; |
| 89 | 0 | if (object == null) { |
| 90 | 0 | return output; |
| 91 | |
} |
| 92 | |
try { |
| 93 | 0 | if (object.getClass().isArray() || Collection.class.isAssignableFrom(object.getClass())) { |
| 94 | 0 | output = ReflectionToStringBuilder.toString(object, ToStringStyle.MULTI_LINE_STYLE); |
| 95 | |
} else { |
| 96 | 0 | output = object.toString(); |
| 97 | |
} |
| 98 | 0 | } catch (RuntimeException e) { |
| 99 | 0 | output = object.toString(); |
| 100 | 0 | } |
| 101 | 0 | return output; |
| 102 | |
} |
| 103 | |
|
| 104 | |
} |