Package siena

Examples of siena.Id


        if (siena.ClassInfo.isModel(clazz)) {
            String keyName = SienaModelUtils.keyName(clazz);
            String idKey = name + "." + keyName;
            if (params.containsKey(idKey) && params.get(idKey).length > 0 && params.get(idKey)[0] != null && params.get(idKey)[0].trim().length() > 0) {
              Field idField = SienaModelUtils.keyField(clazz);
              Id idAnn = idField.getAnnotation(Id.class);
          if(idAnn != null && idAnn.value() == Generator.AUTO_INCREMENT) {
            // ONLY long ID can be auto_incremented
                  String id = params.get(idKey)[0];
                  try {
                      siena.Query<?> query = pm().createQuery(clazz).filter(keyName,
                          play.data.binding.Binder.directBind(name, annotations, id + "", SienaModelUtils.keyType(clazz)));
View Full Code Here


        if (value != null && ClassInfo.isModel(value.getClass())) {
          Key key = GaeMappingUtils.getKey(value);
          q.addFilter(propertyName, op, key);
        } else {
          if (ClassInfo.isId(f)) {
            Id id = f.getAnnotation(Id.class);
            switch(id.value()) {
            case NONE:
              if(value != null){
                if(!Collection.class.isAssignableFrom(value.getClass())){
                  // long or string goes toString
                  Key key;
                  if(parentKey == null){
                    key = KeyFactory.createKey(
                      q.getKind(),
                      value.toString());
                  }else {
                    key = KeyFactory.createKey(
                        parentKey,
                        q.getKind(),
                        value.toString());
                  }
                  q.addFilter(Entity.KEY_RESERVED_PROPERTY, op, key);
                }else {
                  List<Key> keys = new ArrayList<Key>();
                  for(Object val: (Collection<?>)value) {
                    if(parentKey == null){
                      keys.add(KeyFactory.createKey(q.getKind(), val.toString()));
                    }else {
                      keys.add(KeyFactory.createKey(parentKey, q.getKind(), val.toString()));
                    }
                  }
                  q.addFilter(Entity.KEY_RESERVED_PROPERTY, op, keys);
                }
              }
              break;
            case AUTO_INCREMENT:
              if(value != null){
                if(!Collection.class.isAssignableFrom(value.getClass())){
                  Key key;
                  Class<?> type = f.getType();
 
                  if(Long.TYPE == type || Long.class.isAssignableFrom(type)){
                    if(parentKey == null){
                      key = KeyFactory.createKey(
                          q.getKind(),
                          (Long)value);
                    }else {
                      key = KeyFactory.createKey(
                          parentKey,
                          q.getKind(),
                          (Long)value);
                    }
                  } else {
                    if(parentKey == null){
                      key = KeyFactory.createKey(
                        q.getKind(),
                        value.toString());
                    }else {
                      key = KeyFactory.createKey(
                          parentKey,
                          q.getKind(),
                          value.toString());
                    }
                  }
                 
                  q.addFilter(Entity.KEY_RESERVED_PROPERTY, op, key);
                }else {
                  List<Key> keys = new ArrayList<Key>();
                  for(Object val: (Collection<?>)value) {
                    if (value instanceof String)
                      val = Long.parseLong((String) val);
                    if(parentKey == null){
                      keys.add(KeyFactory.createKey(q.getKind(), (Long)val));
                    }else {
                      keys.add(KeyFactory.createKey(parentKey, q.getKind(), (Long)val));
                    }
                  }
                  q.addFilter(Entity.KEY_RESERVED_PROPERTY, op, keys);
                }
              }
              break;
            case UUID:
              if(value != null) {
                if(!Collection.class.isAssignableFrom(value.getClass())){
                  // long or string goes toString
                  Key key;
                  if(parentKey == null){
                    key = KeyFactory.createKey(
                        q.getKind(),
                        value.toString());
                  }else {
                    key = KeyFactory.createKey(
                        parentKey,
                        q.getKind(),
                        value.toString());
                  }
                  q.addFilter(Entity.KEY_RESERVED_PROPERTY, op, key);
                }else {
                  List<Key> keys = new ArrayList<Key>();
                  for(Object val: (Collection<?>)value) {
                    keys.add(KeyFactory.createKey(q.getKind(), val.toString()));
                  }
                  q.addFilter(Entity.KEY_RESERVED_PROPERTY, op, keys);
                }
              }
              break;
            default:
              throw new SienaException("Id Generator "+id.value()+ " not supported");
            }
   
          } else if (Enum.class.isAssignableFrom(f.getType())) {
            value = value.toString();
            q.addFilter(propertyName, op, value);
View Full Code Here

public class GaeMappingUtils {
 
  public static Entity createEntityInstance(Field idField, ClassInfo info, Object obj){
    Entity entity = null;
    Id id = idField.getAnnotation(Id.class);
    Class<?> type = idField.getType();

    if(id != null){
      switch(id.value()) {
      case NONE:
        Object idVal = null;
        idVal = Util.readField(obj, idField);
        if(idVal == null)
          throw new SienaException("Id Field " + idField.getName() + " value null");
        String keyVal = Util.toString(idField, idVal);       
        entity = new Entity(info.tableName, keyVal);
        break;
      case AUTO_INCREMENT:
        // manages String ID as not long!!!
        if(Long.TYPE == type || Long.class.isAssignableFrom(type)){
          entity = new Entity(info.tableName);
        }else {
          Object idStringVal = null;
          idStringVal = Util.readField(obj, idField);
          if(idStringVal == null)
            throw new SienaException("Id Field " + idField.getName() + " value null");
          String keyStringVal = Util.toString(idField, idStringVal);       
          entity = new Entity(info.tableName, keyStringVal);
        }
        break;
      case UUID:
        entity = new Entity(info.tableName, UUID.randomUUID().toString());
        break;
      default:
        throw new SienaRestrictedApiException("DB", "createEntityInstance", "Id Generator "+id.value()+ " not supported");
      }
    }
    else throw new SienaException("Field " + idField.getName() + " is not an @Id field");
   
    return entity;
View Full Code Here

 
  public static Entity createEntityInstanceFromParent(
      Field idField, ClassInfo info, Object obj,
      Key parentKey, ClassInfo parentInfo, Field parentField){
    Entity entity = null;
    Id id = idField.getAnnotation(Id.class);
    Class<?> type = idField.getType();

    if(id != null){
      switch(id.value()) {
      case NONE:
        Object idVal = null;
        idVal = Util.readField(obj, idField);
        if(idVal == null)
          throw new SienaException("Id Field " + idField.getName() + " value null");
        String keyVal = Util.toString(idField, idVal);       
        entity = new Entity(getKindWithAncestorField(info, parentInfo, parentField), keyVal, parentKey);
        break;
      case AUTO_INCREMENT:
        // manages String ID as not long!!!
        if(Long.TYPE == type || Long.class.isAssignableFrom(type)){
          entity = new Entity(getKindWithAncestorField(info, parentInfo, parentField), parentKey);
        }else {
          Object idStringVal = null;
          idStringVal = Util.readField(obj, idField);
          if(idStringVal == null)
            throw new SienaException("Id Field " + idField.getName() + " value null");
          String keyStringVal = Util.toString(idField, idStringVal);       
          entity = new Entity(getKindWithAncestorField(info, parentInfo, parentField), keyStringVal, parentKey);
        }
        break;
      case UUID:
        entity = new Entity(getKindWithAncestorField(info, parentInfo, parentField), UUID.randomUUID().toString(), parentKey);
        break;
      default:
        throw new SienaRestrictedApiException("DB", "createEntityInstance", "Id Generator "+id.value()+ " not supported");
      }
    }
    else throw new SienaException("Field " + idField.getName() + " is not an @Id field");
   
    return entity;
View Full Code Here

   
    return entity;
  }
 
  public static void setIdFromKey(Field idField, Object obj, Key key) {
    Id id = idField.getAnnotation(Id.class);
    Class<?> type = idField.getType();
    if(id != null){
      switch(id.value()) {
      case NONE:
        //idField.setAccessible(true);
        Object val = null;
        if (Long.TYPE==type || Long.class.isAssignableFrom(type)){
          val = Long.parseLong((String) key.getName());
        }
        else if (String.class.isAssignableFrom(type)){
          val = key.getName();
        }
        else{
          throw new SienaRestrictedApiException("DB", "setKey", "Id Type "+idField.getType()+ " not supported");
        }
         
        Util.setField(obj, idField, val);
        break;
      case AUTO_INCREMENT:
        // Long value means key.getId()
        if (Long.TYPE==type || Long.class.isAssignableFrom(idField.getType())){
          Util.setField(obj, idField, key.getId());
        }else {
          idField.setAccessible(true);
          Object val2 = null;
          if (Long.TYPE==type || Long.class.isAssignableFrom(idField.getType())){
            val = Long.parseLong((String) key.getName());
          }
          else if (String.class.isAssignableFrom(idField.getType())){
            val = key.getName();
          }
          else{
            throw new SienaRestrictedApiException("DB", "setKey", "Id Type "+idField.getType()+ " not supported");
          }
           
          Util.setField(obj, idField, val2);
        }
        break;
      case UUID:
        Util.setField(obj, idField, key.getName());
        break;
      default:
        throw new SienaException("Id Generator "+id.value()+ " not supported");
      }
    }
    else throw new SienaException("Field " + idField.getName() + " is not an @Id field");
  }
View Full Code Here

      if(value == null) return null;
     
      Class<?> type = idField.getType();
     
      if(idField.isAnnotationPresent(Id.class)){
        Id id = idField.getAnnotation(Id.class);
        switch(id.value()) {
        case NONE:
          // long or string goes toString
          return KeyFactory.createKey(
            ClassInfo.getClassInfo(clazz).tableName,
            value.toString());
        case AUTO_INCREMENT:
          // as a string with auto_increment can't exist, it is not cast into long
          if (Long.TYPE == type || Long.class.isAssignableFrom(type)){
            return KeyFactory.createKey(
              ClassInfo.getClassInfo(clazz).tableName,
              (Long)value);
          }
          return KeyFactory.createKey(
            ClassInfo.getClassInfo(clazz).tableName,
            value.toString());
         
        case UUID:
          return KeyFactory.createKey(
            ClassInfo.getClassInfo(clazz).tableName,
            value.toString());
        default:
          throw new SienaException("Id Generator "+id.value()+ " not supported");
        }
      }
      else throw new SienaException("Field " + idField.getName() + " is not an @Id field");
    } catch (Exception e) {
      throw new SienaException(e);
View Full Code Here

      if(value == null) return null;
     
      Class<?> type = idField.getType();
     
      if(idField.isAnnotationPresent(Id.class)){
        Id id = idField.getAnnotation(Id.class);
        switch(id.value()) {
        case NONE:
          // long or string goes toString
          return KeyFactory.createKey(
              parentKey,
              getKindWithAncestorField(info, parentInfo, parentField),
              value.toString());
        case AUTO_INCREMENT:
          // as a string with auto_increment can't exist, it is not cast into long
          if (Long.TYPE == type || Long.class.isAssignableFrom(type)){
            return KeyFactory.createKey(
                parentKey,
                getKindWithAncestorField(info, parentInfo, parentField),
                (Long)value);
          }
          return KeyFactory.createKey(
              parentKey,
              getKindWithAncestorField(info, parentInfo, parentField),
              value.toString());
         
        case UUID:
          return KeyFactory.createKey(
              parentKey,
              getKindWithAncestorField(info, parentInfo, parentField),
              value.toString());
        default:
          throw new SienaException("Id Generator "+id.value()+ " not supported");
        }
      }
      else throw new SienaException("Field " + idField.getName() + " is not an @Id field");
    } catch (Exception e) {
      throw new SienaException(e);
View Full Code Here

   
    try {
      Field idField = info.getIdField();
     
      if(idField.isAnnotationPresent(Id.class)){
        Id id = idField.getAnnotation(Id.class);
        switch(id.value()) {
        case NONE:
          // long or string goes toString
          return KeyFactory.createKey(
              ClassInfo.getClassInfo(clazz).tableName,
              idVal.toString());
        case AUTO_INCREMENT:
          Class<?> type = idField.getType();
          // as a string with auto_increment can't exist, it is not cast into long
          if (Long.TYPE==type || Long.class.isAssignableFrom(type)){
            return KeyFactory.createKey(
              ClassInfo.getClassInfo(clazz).tableName,
              (Long)idVal);
          }
          return KeyFactory.createKey(
            ClassInfo.getClassInfo(clazz).tableName,
            idVal.toString());
        case UUID:
          return KeyFactory.createKey(
            ClassInfo.getClassInfo(clazz).tableName,
            idVal.toString());
        default:
          throw new SienaException("Id Generator "+id.value()+ " not supported");
        }
      }
      else throw new SienaException("Field " + idField.getName() + " is not an @Id field");
    } catch (Exception e) {
      throw new SienaException(e);
View Full Code Here

      Object idVal = Util.readField(object, idField);
      if(idVal == null)
        throw new SienaException("Id Field " + idField.getName() + " value null");
     
      if(idField.isAnnotationPresent(Id.class)){
        Id id = idField.getAnnotation(Id.class);
        switch(id.value()) {
        case NONE:
          // long or string goes toString
          return KeyFactory.createKey(
              parentKey,
              getKindWithAncestorField(info, parentInfo, parentField),
              idVal.toString());
        case AUTO_INCREMENT:
          Class<?> type = idField.getType();
          // as a string with auto_increment can't exist, it is not cast into long
          if (Long.TYPE==type || Long.class.isAssignableFrom(type)){
            return KeyFactory.createKey(
              parentKey,
              getKindWithAncestorField(info, parentInfo, parentField),
              (Long)idVal);
          }
          return KeyFactory.createKey(
              parentKey,
              getKindWithAncestorField(info, parentInfo, parentField),
              idVal.toString());
        case UUID:
          return KeyFactory.createKey(
              parentKey,
              getKindWithAncestorField(info, parentInfo, parentField),
              idVal.toString());
        default:
          throw new SienaException("Id Generator "+id.value()+ " not supported");
        }
      }
      else throw new SienaException("Field " + idField.getName() + " is not an @Id field");
    } catch (Exception e) {
      throw new SienaException(e);
View Full Code Here

  }
   
 
  public static String getItemName(Class<?> clazz, Object obj){
    Field idField = ClassInfo.getIdField(clazz);
    Id id = idField.getAnnotation(Id.class);

    String keyVal = null;
    if(id != null){
      switch(id.value()) {
      case NONE:
      {
        Object idVal = Util.readField(obj, idField);
        if(idVal == null)
          throw new SienaException("Id Field " + idField.getName() + " value null");
        keyVal = toString(idField, idVal);       
        break;
      }
      case AUTO_INCREMENT:
        // manages String ID as not long!!!
        throw new SienaRestrictedApiException("DB", "getItemName", "@Id AUTO_INCREMENT not supported by SDB");
      case UUID:
      {
        Object idVal = Util.readField(obj, idField);
        if(idVal == null){
          UUID uuid = UUID.randomUUID();
          keyVal = uuid.toString();
         
          if(idField.getType() == UUID.class){
            Util.setField(obj, idField, uuid);
          }
          else if(idField.getType() == String.class){
            Util.setField(obj, idField, uuid.toString());
          }
          else {
            throw new SienaRestrictedApiException("DB", "getItemName", "@Id UUID must be of type String or UUID");
          }
        }else {
          keyVal = toString(idField, idVal);
        }
        break;
      }
      default:
        throw new SienaRestrictedApiException("DB", "createEntityInstance", "Id Generator "+id.value()+ " not supported");
      }
    }
    else throw new SienaException("Field " + idField.getName() + " is not an @Id field");

    return keyVal;
View Full Code Here

TOP

Related Classes of siena.Id

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.