Package com.googlecode.mjorm

Examples of com.googlecode.mjorm.MjormException


    Class<?> targetClass = targetType.asClass();

    // get the descriptors
    LinkedList<ObjectDescriptor> descriptors = registry.getDescriptorsForType(targetClass);
    if (descriptors.isEmpty()) {
      throw new MjormException("Unable to find ObjectDescriptor for "+targetClass);
    }

    // get descriptor
    ObjectDescriptor descriptor = descriptors.get(descriptors.size()-1);

    // get the discriminator name
    String discriminatorName = descriptor.getDiscriminatorName();
    Object discriminator = null;
    if (discriminatorName!=null && discriminatorName.trim().length()>0) {
      discriminator = DBObjectUtil.getNestedProperty(source, discriminatorName);
    }

    // if we have a discriminiator - figure out which
    // subclass to use
    if (discriminator!=null) {
      ObjectDescriptor subClass = descriptor.getSubClassObjectDescriptor(discriminator);
      if (subClass==null && Modifier.isAbstract(descriptor.getType().getModifiers())) {
        throw new MjormException(
          "Sublcass for discriminiator value "+discriminator
          +" was not found on abstract ObjectDescriptor for "
          + descriptor.getType().getName());
      } else if (subClass!=null) {
        descriptor = subClass;
        descriptors.addFirst(subClass);
      }
    }

    // create the return object
    Object ret;
    try {
      ret = ReflectionUtil.instantiate(descriptor.getType());
    } catch (Exception e) {
      throw new MjormException(
        "Error creating class: "+targetClass, e);
    }

    // loop through each descriptor
    for (ObjectDescriptor desc : descriptors) {
 
      // loop through each property
      for (PropertyDescriptor prop : desc.getProperties()) {
 
        try {

          // the field name
          String fieldName = prop.isIdentifier() ? "_id" : prop.getFieldName();

          // get the value
          Object value = source.get(fieldName);

          // convert
          if (value!=null) {
            // setup hints
            TypeConversionHints nextHints = new TypeConversionHints();
            if (prop.getConversionHints()!=null && !prop.getConversionHints().isEmpty()) {
              for (Entry<String, Object> entry : prop.getConversionHints().entrySet()) {
                nextHints.set(entry.getKey(), entry.getValue());
              }
            }

            // add generic type parameter hints
            Type[] genericParameterTypes = prop.getGenericParameterTypes();
            if (genericParameterTypes!=null && genericParameterTypes.length>0) {
              nextHints.set(TypeConversionHints.HINT_GENERIC_TYPE_PARAMETERS, genericParameterTypes);
            }
           
            // convert
            value = context.convert(value, prop.getType(), nextHints);
          }

          // set
          prop.set(ret, value);
 
        } catch (Exception e) {
          throw new MjormException(
            "Error mapping property "+prop.getName()
            +" of class "+descriptor.getType(), e);
        }
 
      }
View Full Code Here


    Class<?> sourceClass = source.getClass();

    // get the descriptors
    List<ObjectDescriptor> descriptors = registry.getDescriptorsForType(sourceClass);
    if (descriptors.isEmpty()) {
      throw new MjormException("Unable to find ObjectDescriptor for "+sourceClass.getClass());
    }

    // create the return object
    BasicDBObject ret = new BasicDBObject();

    // loop through each descriptor
    for (ObjectDescriptor descriptor : descriptors) {
 
      // loop through each property
      for (PropertyDescriptor prop : descriptor.getProperties()) {
 
        try {
          // get it
          Object value = prop.get(source);
 
          // auto generate value
          if (prop.isAutoGenerated() && value==null) {
            value = prop.getValueGenerator()!=null
              ? prop.getValueGenerator().generate()
              : ObjectIdValueGenerator.INSTANCE.generate();
            prop.set(source, context.convert(value, prop.getType()));
          }

          // the field name
          String fieldName = prop.isIdentifier() ? "_id" : prop.getFieldName();

          // convert it
          if (value!=null) {

            // get storage type
            JavaType storageType = prop.getStorageType();
            if (storageType==null && value!=null) {
              storageType = context.getStorageType(value.getClass());
            }

            // convert
            value = context.convert(value, storageType);
          }

          // set on DBObject
          ret.put(fieldName, value);

        } catch (Exception e) {
          throw new MjormException(
            "Error mapping property "+prop.getName()
            +" of class "+descriptor.getType(), e);
        }
 
      }
View Full Code Here

  private ObjectDescriptor parseClass(Class<?> clazz, List<ObjectDescriptor> descriptors) {

    // get the entity annotation
    Entity entity = clazz.getAnnotation(Entity.class);
    if (entity==null) {
      throw new MjormException(
        clazz.getName()+" does not have an "+Entity.class.getName()+" annotation");
    }

    // parse entity date
    String discriminatorName = entity.discriminatorName();
    DiscriminatorType discriminatorType = entity.discriminatorType();
    SubClass[] subClasses = entity.subClasses();

    // create an object descriptor
    ObjectDescriptor desc = new ObjectDescriptor();
    desc.setType(clazz);
    desc.setDiscriminatorName(discriminatorName);
    desc.setDiscriminatorType(discriminatorType.toString());

    // get BeanInfo
    BeanInfo info = ReflectionUtil.getBeanInfo(clazz);
    Map<Method, java.beans.PropertyDescriptor> methodMap
      = new HashMap<Method, java.beans.PropertyDescriptor>();
    for (java.beans.PropertyDescriptor pd : info.getPropertyDescriptors()) {
      methodMap.put(pd.getReadMethod(), pd);
      methodMap.put(pd.getWriteMethod(), pd);
    }

    // get all of the methods
    for (Method method : clazz.getMethods()) {

      // look for the annotations
      Property property = method.getAnnotation(Property.class);
      Id id = method.getAnnotation(Id.class);
      if (property==null) {
        continue;
      }

      // find pd
      java.beans.PropertyDescriptor pd = methodMap.get(method);
      if (pd==null) {
        throw new MjormException(
          method.getName()+" is not not a valid bean method.");
      }

      // "parse" data
      String propField = !property.field().equals("")
View Full Code Here

TOP

Related Classes of com.googlecode.mjorm.MjormException

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.