Coverage Report - org.dozer.util.LogMsgFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
LogMsgFactory
48%
18/37
25%
4/16
5
 
 1  
 /**
 2  
  * Copyright 2005-2013 Dozer Project
 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 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  
  * Internal class used to build various types of log messages. Only intended for internal use.
 28  
  * 
 29  
  * @author tierney.matt
 30  
  * @author garsombke.franz
 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  
       //1797808 - If an error occurs calling src value toString(), eat the error and still log the dozer error msg
 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  
 }