Coverage Report - org.dozer.classmap.ClassMap
 
Classes in this File Line Coverage Branch Coverage Complexity
ClassMap
94%
102/108
85%
41/48
1.433
 
 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.classmap;
 17  
 
 18  
 import org.apache.commons.lang3.builder.ToStringBuilder;
 19  
 import org.apache.commons.lang3.builder.ToStringStyle;
 20  
 import org.dozer.converters.CustomConverterContainer;
 21  
 import org.dozer.fieldmap.FieldMap;
 22  
 import org.dozer.util.DozerConstants;
 23  
 import org.dozer.util.MappingUtils;
 24  
 
 25  
 import java.util.ArrayList;
 26  
 import java.util.List;
 27  
 
 28  
 /**
 29  
  * Internal class that represents a class mapping definition. Holds all of the information about a single class mapping.
 30  
  * Only intended for internal use.
 31  
  * 
 32  
  * @author garsombke.franz
 33  
  * @author sullins.ben
 34  
  * @author tierney.matt
 35  
  * 
 36  
  */
 37  
 public class ClassMap {
 38  
 
 39  
   private Configuration globalConfiguration;
 40  
   private DozerClass srcClass;
 41  
   private DozerClass destClass;
 42  11349
   private List<FieldMap> fieldMaps = new ArrayList<FieldMap>();
 43  11349
   private List<Class<RuntimeException>> allowedExceptions = new ArrayList<Class<RuntimeException>>();
 44  
   private MappingDirection type;
 45  
   private String dateFormat;
 46  
   private String beanFactory;
 47  11349
   private boolean mapNull = DozerConstants.DEFAULT_MAP_NULL_POLICY;
 48  11349
   private boolean mapEmptyString = DozerConstants.DEFAULT_MAP_EMPTY_STRING_POLICY;
 49  
   private Boolean wildcard;
 50  
   private Boolean stopOnErrors;
 51  
   private Boolean trimStrings;
 52  
   private CustomConverterContainer customConverters;
 53  
   private String mapId;
 54  
   private RelationshipType relationshipType;
 55  
 
 56  11349
   public ClassMap(Configuration globalConfiguration) {
 57  11349
     this.globalConfiguration = globalConfiguration;
 58  11349
   }
 59  
 
 60  
   public List<FieldMap> getFieldMaps() {
 61  446602
     return fieldMaps;
 62  
   }
 63  
 
 64  
   public boolean isStopOnErrors() {
 65  5414
     return stopOnErrors != null ? stopOnErrors.booleanValue() : globalConfiguration.getStopOnErrors().booleanValue();
 66  
   }
 67  
 
 68  
   public void setStopOnErrors(Boolean stopOnErrors) {
 69  5842
     this.stopOnErrors = stopOnErrors;
 70  5842
   }
 71  
 
 72  
   public boolean isTrimStrings() {
 73  96412
     return trimStrings != null ? trimStrings.booleanValue() : globalConfiguration.getTrimStrings().booleanValue();
 74  
   }
 75  
 
 76  
   public void setTrimStrings(Boolean trimStrings) {
 77  5405
     this.trimStrings = trimStrings;
 78  5405
   }
 79  
 
 80  
   public List<Class<RuntimeException>> getAllowedExceptions() {
 81  5406
     if (!allowedExceptions.isEmpty()) {
 82  0
       return allowedExceptions;
 83  
     } else {
 84  5406
       return globalConfiguration.getAllowedExceptions().getExceptions();
 85  
     }
 86  
   }
 87  
 
 88  
   public void setAllowedExceptions(List<Class<RuntimeException>> allowedExceptions) {
 89  5398
     this.allowedExceptions = allowedExceptions;
 90  5398
   }
 91  
 
 92  
   public FieldMap getFieldMapUsingDest(String destFieldName) {
 93  16128
     return getFieldMapUsingDest(destFieldName, false);
 94  
   }
 95  
 
 96  
   public FieldMap getFieldMapUsingDest(String destFieldName, boolean isMap) {
 97  21653
     if (fieldMaps == null) {
 98  0
       return null;
 99  
     }
 100  
 
 101  21653
     FieldMap result = null;
 102  
 
 103  21653
     for (FieldMap fieldMap : fieldMaps) {
 104  201289
       String fieldName = fieldMap.getDestFieldName();
 105  
 
 106  201289
       if (isMap && MappingUtils.isDeepMapping(fieldName)) {
 107  24
         fieldName = fieldName.split("\\.")[0];
 108  
       }
 109  
 
 110  201289
       String alternateFieldName = provideAlternateName(fieldName);
 111  
 
 112  
       // Check for exact match on field name. Also, check against alternate field name. The alternate field
 113  
       // name is used just in case the attribute was specified in the dozer xml file starting in a Capital letter.
 114  
       // This prevents the field from getting double mapped in the case that the class attr is named "field1" but in
 115  
       // the dozer xml is it specified as "Field1". This should never happen, but check just in case since the use case
 116  
       // doesnt actually error out. It just double maps which is a problem when the data type is a Collections.
 117  201289
       if (fieldName.equals(destFieldName) || alternateFieldName.equals(destFieldName)) {
 118  3360
         result = fieldMap;
 119  3360
         break;
 120  
       }
 121  197929
     }
 122  
 
 123  21653
     return result;
 124  
   }
 125  
 
 126  
   String provideAlternateName(String fieldName) {
 127  201290
     return fieldName.substring(0, 1).toLowerCase() + fieldName.substring(1);
 128  
   }
 129  
 
 130  
   public FieldMap getFieldMapUsingSrc(String srcFieldName) {
 131  19182
     FieldMap result = null;
 132  
 
 133  19182
     if (fieldMaps != null) {
 134  19182
       for (FieldMap fieldMap : fieldMaps) {
 135  187092
         String fieldName = fieldMap.getSrcFieldName();
 136  
 
 137  187092
         if ((fieldName != null) && fieldName.equals(srcFieldName)) {
 138  1245
           result = fieldMap;
 139  1245
           break;
 140  
         }
 141  185847
       }
 142  
     }
 143  19182
     return result;
 144  
   }
 145  
 
 146  
   public void setFieldMaps(List<FieldMap> fieldMaps) {
 147  1
     this.fieldMaps = fieldMaps;
 148  1
   }
 149  
 
 150  
   public void addFieldMapping(FieldMap fieldMap) {
 151  51787
     fieldMaps.add(fieldMap);
 152  51787
   }
 153  
 
 154  
   public void removeFieldMapping(FieldMap fieldMap) {
 155  1277
     fieldMaps.remove(fieldMap);
 156  1277
   }
 157  
 
 158  
   public boolean isWildcard() {
 159  16431
     return wildcard != null ? wildcard.booleanValue() : globalConfiguration.getWildcard().booleanValue();
 160  
   }
 161  
 
 162  
   public void setWildcard(Boolean wildcardPolicy) {
 163  6269
     this.wildcard = wildcardPolicy;
 164  6269
   }
 165  
 
 166  
   public MappingDirection getType() {
 167  16190
     return type;
 168  
   }
 169  
 
 170  
   public void setType(MappingDirection type) {
 171  5679
     this.type = type;
 172  5679
   }
 173  
 
 174  
   public String getDateFormat() {
 175  51635
     return !MappingUtils.isBlankOrNull(dateFormat) ? dateFormat : globalConfiguration.getDateFormat();
 176  
   }
 177  
 
 178  
   public void setDateFormat(String dateFormat) {
 179  5508
     this.dateFormat = dateFormat;
 180  5508
   }
 181  
 
 182  
   public CustomConverterContainer getCustomConverters() {
 183  681624
     return customConverters != null ? customConverters : globalConfiguration.getCustomConverters();
 184  
   }
 185  
 
 186  
   public void setCustomConverters(CustomConverterContainer customConverters) {
 187  0
     this.customConverters = customConverters;
 188  0
   }
 189  
 
 190  
   public Class<?> getSrcClassToMap() {
 191  549952
     return srcClass.getClassToMap();
 192  
   }
 193  
 
 194  
   public Class<?> getDestClassToMap() {
 195  588467
     return destClass.getClassToMap();
 196  
   }
 197  
 
 198  
   public boolean isDestMapNull() {
 199  141218
     return destClass.getMapNull() != null ? destClass.getMapNull().booleanValue() : mapNull;
 200  
   }
 201  
 
 202  
   public boolean isSrcMapNull() {
 203  5398
     return srcClass.getMapNull() != null ? srcClass.getMapNull().booleanValue() : mapNull;
 204  
   }
 205  
 
 206  
   public boolean isDestMapEmptyString() {
 207  50155
     return destClass.getMapEmptyString() != null ? destClass.getMapEmptyString().booleanValue() : mapEmptyString;
 208  
   }
 209  
 
 210  
   public boolean isSrcMapEmptyString() {
 211  5398
     return srcClass.getMapEmptyString() != null ? srcClass.getMapEmptyString().booleanValue() : mapEmptyString;
 212  
   }
 213  
 
 214  
   public String getDestClassBeanFactory() {
 215  219200
     return !MappingUtils.isBlankOrNull(destClass.getBeanFactory()) ? destClass.getBeanFactory() : getBeanFactory();
 216  
   }
 217  
 
 218  
   public String getSrcClassBeanFactory() {
 219  23311
     return !MappingUtils.isBlankOrNull(srcClass.getBeanFactory()) ? srcClass.getBeanFactory() : getBeanFactory();
 220  
   }
 221  
 
 222  
   public String getDestClassBeanFactoryId() {
 223  178238
     return destClass.getFactoryBeanId();
 224  
   }
 225  
 
 226  
   public String getSrcClassBeanFactoryId() {
 227  5398
     return srcClass.getFactoryBeanId();
 228  
   }
 229  
 
 230  
   public String getSrcClassMapGetMethod() {
 231  44930
     return srcClass.getMapGetMethod();
 232  
   }
 233  
 
 234  
   public String getSrcClassMapSetMethod() {
 235  32319
     return srcClass.getMapSetMethod();
 236  
   }
 237  
 
 238  
   public String getDestClassMapGetMethod() {
 239  66432
     return destClass.getMapGetMethod();
 240  
   }
 241  
 
 242  
   public String getDestClassMapSetMethod() {
 243  54925
     return destClass.getMapSetMethod();
 244  
   }
 245  
 
 246  
   public String getDestClassCreateMethod() {
 247  178236
     return destClass.getCreateMethod();
 248  
   }
 249  
 
 250  
   public String getSrcClassCreateMethod() {
 251  5398
     return srcClass.getCreateMethod();
 252  
   }
 253  
 
 254  
   public void setSrcClassCreateMethod(String createMethod) {
 255  5398
     srcClass.setCreateMethod(createMethod);
 256  5398
   }
 257  
 
 258  
   public void setDestClassCreateMethod(String createMethod) {
 259  5398
     destClass.setCreateMethod(createMethod);
 260  5398
   }
 261  
 
 262  
   public boolean isDestClassMapTypeCustomGetterSetter() {
 263  12861
     return destClass.isMapTypeCustomGetterSetterClass();
 264  
   }
 265  
 
 266  
   public boolean isSrcClassMapTypeCustomGetterSetter() {
 267  12991
     return srcClass.isMapTypeCustomGetterSetterClass();
 268  
   }
 269  
 
 270  
   public void setSrcClass(DozerClass srcClass) {
 271  11340
     this.srcClass = srcClass;
 272  11340
   }
 273  
 
 274  
   public void setDestClass(DozerClass destClass) {
 275  11340
     this.destClass = destClass;
 276  11340
   }
 277  
 
 278  
   public String getDestClassName() {
 279  5468
     return destClass.getName();
 280  
   }
 281  
 
 282  
   public String getSrcClassName() {
 283  5520
     return srcClass.getName();
 284  
   }
 285  
 
 286  
   public String getBeanFactory() {
 287  242121
     return !MappingUtils.isBlankOrNull(beanFactory) ? beanFactory : globalConfiguration.getBeanFactory();
 288  
   }
 289  
 
 290  
   public void setBeanFactory(String beanFactory) {
 291  1
     this.beanFactory = beanFactory;
 292  1
   }
 293  
 
 294  
   public String getMapId() {
 295  301313
     return mapId;
 296  
   }
 297  
 
 298  
   public void setMapId(String mapId) {
 299  1343
     this.mapId = mapId;
 300  1343
   }
 301  
 
 302  
   public void setMapNull(boolean mapNull) {
 303  67
     this.mapNull = mapNull;
 304  67
   }
 305  
 
 306  
   public void setMapEmptyString(boolean mapEmptyString) {
 307  15
     this.mapEmptyString = mapEmptyString;
 308  15
   }
 309  
 
 310  
   public Configuration getGlobalConfiguration() {
 311  0
     return globalConfiguration;
 312  
   }
 313  
 
 314  
   public void setGlobalConfiguration(Configuration globalConfiguration) {
 315  5401
     this.globalConfiguration = globalConfiguration;
 316  5401
   }
 317  
 
 318  
   public RelationshipType getRelationshipType() {
 319  62675
     return relationshipType != null ? relationshipType : globalConfiguration.getRelationshipType();
 320  
   }
 321  
 
 322  
   public void setRelationshipType(RelationshipType relationshipType) {
 323  5401
     this.relationshipType = relationshipType;
 324  5401
   }
 325  
 
 326  
   public DozerClass getSrcClass() {
 327  29088
     return srcClass;
 328  
   }
 329  
 
 330  
   public DozerClass getDestClass() {
 331  52310
     return destClass;
 332  
   }
 333  
 
 334  
   @Override
 335  
   public String toString() {
 336  0
     return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE).append("source class", getSrcClassName()).append(
 337  
         "destination class", getDestClassName()).append("map-id", mapId).toString();
 338  
   }
 339  
 }