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

me.prettyprint.hom.cache.TableParserValidator Maven / Gradle / Ivy

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

import java.lang.annotation.Annotation;

import javax.persistence.Inheritance;
import javax.persistence.Table;

import me.prettyprint.hom.CFMappingDef;
import me.prettyprint.hom.ClassCacheMgr;

/**
 * Parse, validate, and set defaults if needed for Inheritance functionality.
 * 
 * @author bburruss
 */
public class TableParserValidator implements ParserValidator {

  @Override
  public  void parse(ClassCacheMgr cacheMgr, Annotation anno, CFMappingDef cfMapDef) {
    if (anno instanceof Table) {
      parseTableAnnotation(cacheMgr, (Table) anno, cfMapDef);
    } else {
      throw new HectorObjectMapperException("This class cannot parse annotation "
          + anno.getClass().getSimpleName());
    }
  }

  private  void parseTableAnnotation(ClassCacheMgr cacheMgr, Table anno,
      CFMappingDef cfMapDef) {
    CFMappingDef tmpDef;

    // column family can only be mapped to one class (base class)
    if (null != (tmpDef = cacheMgr.getCfMapDef(anno.name(), false))) {
      throw new HectorObjectMapperException(
          "classes, "
              + cfMapDef.getEffectiveClass().getName()
              + " and "
              + tmpDef.getEffectiveClass().getName()
              + ", are both mapped to ColumnFamily, "
              + tmpDef.getEffectiveColFamName()
              + ".  Can only have one Class/ColumnFamily mapping - if multiple classes can be derived from a single ColumnFamily, use @"
              + Inheritance.class.getSimpleName());
    }
    cfMapDef.setColFamName(anno.name());
  }

  @Override
  public  void validateAndSetDefaults(ClassCacheMgr cacheMgr, CFMappingDef cfMapDef) {
    if (cfMapDef.isStandaloneClass()) {
      validateStandaloneClass(cacheMgr, cfMapDef);
    } else if (cfMapDef.isBaseInheritanceClass()) {
      validateBaseClass(cacheMgr, cfMapDef);
    } else if (cfMapDef.isDerivedClassInheritance()) {
      validateDerivedClass(cacheMgr, cfMapDef);
    }
  }

  private  void validateStandaloneClass(ClassCacheMgr cacheMgr, CFMappingDef cfMapDef) {
    CFMappingDef cfSuperDef;

    if (null == cfMapDef.getEffectiveColFamName()) {
      throw new HectorObjectMapperException("Class, " + cfMapDef.getRealClass().getName()
          + ", is missing @" + Table.class.getSimpleName());
    } else if (null != (cfSuperDef = cacheMgr.findBaseClassViaMappings(cfMapDef))) {
      throw new HectorObjectMapperException("@" + Table.class.getSimpleName()
          + " can only be used once per hierarchy and has been specified in class "
          + cfSuperDef.getRealClass().getName() + " and " + cfMapDef.getRealClass().getName()
          + " - quitting");
    }
  }

  private  void validateBaseClass(ClassCacheMgr cacheMgr, CFMappingDef cfMapDef) {
    if (null == cfMapDef.getColFamName()) {
      throw new HectorObjectMapperException(cfMapDef.getRealClass()
          + " is recognized as a base class, but doesn't specify @" + Table.class.getSimpleName()
          + " - quitting");
    }
  }

  private  void validateDerivedClass(ClassCacheMgr cacheMgr, CFMappingDef cfMapDef) {
    findAndSetBaseClassViaMappings(cacheMgr, cfMapDef);
    if (null == cfMapDef.getCfBaseMapDef()) {
      throw new HectorObjectMapperException("@" + Table.class.getSimpleName() + " used by class, "
          + cfMapDef.getRealClass().getName() + ", "
          + " has already been specified by base class, "
          + cfMapDef.getCfBaseMapDef().getEffectiveClass().getName());
    }

    // save this class in the base class for reference during loading
    cfMapDef.getCfBaseMapDef().addDerivedClassMap(cfMapDef);
  }

  private  void findAndSetBaseClassViaMappings(ClassCacheMgr cacheMgr, CFMappingDef cfMapDef) {
    CFMappingDef cfBaseMapDef = cacheMgr.findBaseClassViaMappings(cfMapDef);

    if (null == cfBaseMapDef) {
      throw new HectorObjectMapperException(cfMapDef.getRealClass()
          + " is a derived class entity but @" + Table.class.getSimpleName()
          + " is not specified in its super classes - quitting");
    }

    cfMapDef.setCfBaseMapDef(cfBaseMapDef);

    // Class derClazz = cfMapDef.getEffectiveClass();
    // Class superClazz = (Class) derClazz.getSuperclass();
    // if (null == superClazz) {
    // return;
    // }
    //
    // CFMappingDef superCfMapDef = getCfMapDef(superClazz, false);
    // if (null == superCfMapDef) {
    // return;
    // }
    //
    // while (null != superCfMapDef && null ==
    // superCfMapDef.getInheritanceType()) {
    // superCfMapDef = (CFMappingDef) superCfMapDef.getCfBaseMapDef();
    // }
    //
    // if (null == superCfMapDef) {
    // throw new HectorObjectMapperException(derClazz.getName() +
    // " has mapped super class, but "
    // + superClazz.getName() + " isn't marked with @" +
    // Inheritance.class.getSimpleName()
    // + " - cannot continue");
    // }

  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy