All Downloads are FREE. Search and download functionalities are using the official Maven repository.

me.prettyprint.hom.CFMappingDef Maven / Gradle / Ivy

There is a newer version: 3.0-04
Show newest version
package me.prettyprint.hom;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import javax.persistence.DiscriminatorType;
import javax.persistence.Entity;
import javax.persistence.InheritanceType;

import me.prettyprint.hom.cache.HectorObjectMapperException;

import com.google.common.collect.Sets;

/**
 * Holder for the mapping between a Class annotated with {@link Entity} and the
 * Cassandra column family name.
 * 
 * @author Todd Burruss
 * 
 * @param 
 */
public class CFMappingDef {
  private Class realClass;
  private Class effectiveClass;
  private CFMappingDef cfBaseMapDef;
  private CFMappingDef cfSuperMapDef;
  private String colFamName;
  private InheritanceType inheritanceType;
  private String discColumn;
  private DiscriminatorType discType;
  private Object discValue; // this can be a variety of types

  private String[] sliceColumnNameArr;
  private Method anonymousPropertyAddHandler;
  private KeyDefinition keyDef;

  private Map> derivedClassMap = new HashMap>();
  private Collection allMappedProps;

  // provide caching by class object for the class' mapping definition
  private Map propertyCacheByPropName = new HashMap();

  // provide caching by class object for the class' mapping definition
  private Map propertyCacheByColName = new HashMap();

  public CFMappingDef(Class clazz) {
    setDefaults(clazz);
  }

  /**
   * Setup mapping with defaults for the given class. Does not parse all
   * annotations.
   * 
   * @param realClass
   */
  @SuppressWarnings("unchecked")
  public void setDefaults(Class realClass) {
    this.realClass = realClass;
    this.keyDef = new KeyDefinition();

    // find the "effective" class - skipping up the hierarchy over inner classes
    effectiveClass = realClass;
    boolean entityFound;
    while (!(entityFound = (null != effectiveClass.getAnnotation(Entity.class)))) {
      // TODO:BTB this might should be isSynthetic
      if (!effectiveClass.isAnonymousClass()) {
        break;
      } else {
        effectiveClass = (Class) effectiveClass.getSuperclass();
      }
    }

    // if class is missing @Entity, then proceed no further
    if (!entityFound) {
      throw new HomMissingEntityAnnotationException("class, " + realClass.getName()
          + ", not annotated with @" + Entity.class.getSimpleName());
    }
  }

  public boolean isColumnSliceRequired() {
    return null == getAnonymousPropertyAddHandler()
        && !isAnyCollections()
        && !isAbstract()
        && !isDerivedEntity();
  }

  public boolean isAnyCollections() {
    if ( null == getAllProperties() ) {
      return false;
    }
    
    for (PropertyMappingDefinition md : getAllProperties()) {
      if (md.isCollectionType()) {
        return true;
      }
    }
    return false;
  }

  public void addDerivedClassMap(CFMappingDef cfDerivedMapDef) {
    derivedClassMap.put(cfDerivedMapDef.getDiscValue(), cfDerivedMapDef);
  }

  public PropertyMappingDefinition getPropMapByPropName(String propName) {
    return propertyCacheByPropName.get(propName);
  }

  public PropertyMappingDefinition getPropMapByColumnName(String colName) {
    PropertyMappingDefinition md = propertyCacheByColName.get(colName);
    if (null != md) {
      return md;
    } else if (null != cfSuperMapDef) {
      return cfSuperMapDef.getPropMapByColumnName(colName);
    } else {
      return null;
    }
  }

  public void addPropertyDefinition(PropertyMappingDefinition propDef) {
    propertyCacheByColName.put(propDef.getColName(), propDef);
    propertyCacheByPropName.put(propDef.getPropDesc().getName(), propDef);
  }

  public String getColFamName() {
    return colFamName;
  }

  public String getEffectiveColFamName() {
    if (null != colFamName) {
      return colFamName;
    } else if (null != cfBaseMapDef) {
      return cfBaseMapDef.getColFamName();
    } else {
      throw new HectorObjectMapperException(
          "trying to get ColumnFamily name, but is missing for mapping, " + this.toString());
    }
  }

  public Class getEffectiveClass() {
    return effectiveClass;
  }

  public InheritanceType getInheritanceType() {
    return inheritanceType;
  }

  public void setInheritanceType(InheritanceType inheritanceType) {
    this.inheritanceType = inheritanceType;
  }

  public String getDiscColumn() {
    if (null == cfBaseMapDef) {
      return discColumn;
    } else {
      return cfBaseMapDef.getDiscColumn();
    }
  }

  public void setDiscColumn(String discColumn) {
    this.discColumn = discColumn;
  }

  public DiscriminatorType getDiscType() {
    if (null == cfBaseMapDef) {
      return discType;
    } else {
      return cfBaseMapDef.getDiscType();
    }
  }

  public void setDiscType(DiscriminatorType discType) {
    this.discType = discType;
  }

  public Object getDiscValue() {
    return discValue;
  }

  public void setDiscValue(Object discValue) {
    this.discValue = discValue;
  }

  public KeyDefinition getKeyDef() {
    if (null == cfBaseMapDef) {
      return keyDef;
    } else {
      return cfBaseMapDef.getKeyDef();
    }
  }

  @SuppressWarnings({ "unchecked", "rawtypes" })
  public Collection getAllProperties() {
    if (null == allMappedProps) {
      Set propSet = new HashSet();
      for (PropertyMappingDefinition propMapDef : propertyCacheByColName.values()) {
        propSet.add(propMapDef);
      }

      if (null == cfSuperMapDef) {
        allMappedProps = propSet;
      } else {
        allMappedProps = Sets.union(propSet, (Set) cfSuperMapDef.getAllProperties());
      }
    }

    return allMappedProps;
  }

  public CFMappingDef getCfBaseMapDef() {
    return cfBaseMapDef;
  }

  public void setCfBaseMapDef(CFMappingDef cfBaseMapDef) {
    this.cfBaseMapDef = cfBaseMapDef;
  }

  public void setColFamName(String colFamName) {
    this.colFamName = colFamName;
  }

  public Map> getDerivedClassMap() {
    return derivedClassMap;
  }

  public CFMappingDef getCfSuperMapDef() {
    return cfSuperMapDef;
  }

  public void setCfSuperMapDef(CFMappingDef cfSuperMapDef) {
    this.cfSuperMapDef = cfSuperMapDef;
  }

  public Class getRealClass() {
    return realClass;
  }

  @Override
  public String toString() {
    return "CFMappingDef [colFamName=" + colFamName + ", realClass=" + realClass
        + ", effectiveClass=" + effectiveClass + "]";
  }

  public boolean isAbstract() {
    return Modifier.isAbstract(effectiveClass.getModifiers());
  }
  
  public boolean isBaseEntity() {
    return null != inheritanceType;
  }

  public boolean isPersistableEntity() {
    return !isAbstract();
  }

  public boolean isPersistableDerivedEntity() {
    return !isBaseEntity() && null != getDiscValue() && !isAbstract();
  }

  public boolean isNonPersistableDerivedEntity() {
    return !isBaseEntity() && !isPersistableDerivedEntity() && isAbstract();
  }

  public boolean isDerivedEntity() {
    return isPersistableDerivedEntity() || isNonPersistableDerivedEntity();
  }
  
  public String[] getSliceColumnNameArr() {
    return sliceColumnNameArr;
  }

  public void setSliceColumnNameArr(String[] sliceColumnNameArr) {
    this.sliceColumnNameArr = sliceColumnNameArr;
  }

  public Method getAnonymousPropertyAddHandler() {
    return anonymousPropertyAddHandler;
  }

  public void setAnonymousPropertyAddHandler(Method anonymousPropertyAddHandler) {
    this.anonymousPropertyAddHandler = anonymousPropertyAddHandler;
  }

  public boolean isAnonymousHandlerAvailable() {
    return null != getAnonymousPropertyAddHandler();
  }
//
//  public boolean isSliceColumnArrayRequired() {
//    return null != sliceColumnNameArr && 0 < sliceColumnNameArr.length;
//  }

  public Collection getCollectionProperties() {
    Set collSet = new HashSet();
    for ( PropertyMappingDefinition md : getAllProperties() ) {
      if ( md.isCollectionType() ) {
        collSet.add(md);
      }
    }
    
    return collSet;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy