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

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

package me.prettyprint.hom;

import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.util.LinkedHashMap;
import java.util.Map;

import me.prettyprint.hom.cache.HectorObjectMapperException;

/**
 * Defines properties used for representing and constructing a cassandra key and
 * mapping to/from POJO property(ies).
 * 
 * @author B. Todd Burruss
 */
public class KeyDefinition {
  private Map idPropertyMap = new LinkedHashMap();
  private Class pkClazz;
  private Map propertyDescriptorMap;

  public void addIdPropertyMap(PropertyMappingDefinition idProperty) {
    idPropertyMap.put(idProperty.getPropDesc().getName(), idProperty);
  }

  public void setPkClass(Class pkClazz) {
    this.pkClazz = pkClazz;
    if (null == this.pkClazz) {
      throw new IllegalArgumentException("Primary Key Class cannot be null");
    }

    PropertyDescriptor[] pdArr;
    try {
      pdArr = Introspector.getBeanInfo(pkClazz, Object.class).getPropertyDescriptors();
    } catch (IntrospectionException e) {
      throw new HectorObjectMapperException(e);
    }

    propertyDescriptorMap = new LinkedHashMap();
    for ( PropertyDescriptor pd : pdArr) {
      propertyDescriptorMap.put(pd.getName(), pd);
    }
  }

  public Class getPkClazz() {
    return pkClazz;
  }

  public Map getIdPropertyMap() {
    return idPropertyMap;
  }

  public Map getPropertyDescriptorMap() {
    return propertyDescriptorMap;
  }

  /**
   * Determines if the key is complex (IdClass, Embedded, etc) or a simple one field type.
   *
   * @return true if complex, false otherwise
   */
  public boolean isComplexKey() {
    return null != getPkClazz();
  }

  /**
   * Determines if a simple ID is present.
   *
   * @return true if the key is not a complex ID and at least one field was annotated with @Id.
   */
  public boolean isSimpleIdPresent() {
    return !isComplexKey() && !idPropertyMap.isEmpty();
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy