Package com.google.appengine.api.datastore

Examples of com.google.appengine.api.datastore.Key


                    // our object is not linked to another object...so it doesn't have any key
                    if(objVal == null) {
                        continue;
                    }

                    Key key = GaeMappingUtils.getKey(objVal);
          List<Key> keys = fieldMap.get(field);
          if(!keys.contains(key))
            keys.add(key);
        }
      }
     
      Map<Field, Map<Key, Entity>> entityMap =
        new HashMap<Field, Map<Key, Entity>>();

      try {
        // retrieves all joined entities per field
        for(Field field: fieldMap.keySet()){
          Map<Key, Entity> entities = ds.get(fieldMap.get(field));
          // gets the future here because we need it so we wait for it
          entityMap.put(field, entities);
        }
      }catch(Exception ex){
        throw new SienaException(ex);
      }
      // associates linked models to their models
      // linkedModels is just a map to contain entities already mapped
      Map<Key, Object> linkedModels = new HashMap<Key, Object>();
      Object linkedObj;
      Entity entity;
     
      for (final T model : models) {
        for(Field field: fieldMap.keySet()){
          Object objVal = Util.readField(model, field);
                    // our object is not linked to another object...so it doesn't have any key
                    if(objVal == null) {
                        continue;
                    }

          Key key = GaeMappingUtils.getKey(objVal);
          linkedObj = linkedModels.get(key);
          if(linkedObj==null){
            entity = entityMap.get(field).get(key);
            linkedObj = objVal;
            GaeMappingUtils.fillModel(linkedObj, entity);
View Full Code Here


   
    return entity;
  }
 
  public static Entity createEntityInstanceForUpdate(ClassInfo info, Object obj){
    Key key = makeKey(info, obj);
    Entity entity = new Entity(key);
   
    return entity;
  }
View Full Code Here

   
    return entity;
  }
 
  public static Entity createEntityInstanceForUpdateFromParent(ClassInfo info, Object obj, Key parentKey, ClassInfo parentInfo, Field parentField){
    Key key = makeKeyFromParent(info, obj, parentKey, parentInfo, parentField);
    Entity entity = new Entity(key);
   
    return entity;
  }
View Full Code Here

          /*&& !ClassInfo.isAggregated(field)
          && !ClassInfo.isOwned(field)*/) {
        if (value == null) {
          entity.setProperty(property, null);
        } else {
          Key key = getKey(value);
          entity.setProperty(property, key);
        }
      } else {
        if (value != null) {
          if (fieldClass == Json.class) {
View Full Code Here

      String property = ClassInfo.getColumnNames(field)[0];
      try {
        Class<?> fieldClass = field.getType();
        if (ClassInfo.isModel(fieldClass) && !ClassInfo.isEmbedded(field)) {
          /*if(!ClassInfo.isAggregated(field)){*/
            Key key = (Key) entity.getProperty(property);
            if (key != null) {
              Object value = Util.createObjectInstance(fieldClass);
              Field id = ClassInfo.getIdField(fieldClass);
              setIdFromKey(id, value, key);
              Util.setField(obj, field, value);
View Full Code Here

    Class<?> clazz = obj.getClass();
    ClassInfo info = ClassInfo.getClassInfo(clazz);
   
    Field id = info.getIdField();
    Class<?> fieldClass = id.getType();
    Key key = entity.getKey();
    if (key != null) {
      setIdFromKey(id, obj, key);
    }

    for (Field field : info.updateFields) {
View Full Code Here

          gaeCtx.useCursor = false;
          query.option(QueryOptionOffset.ID).activate();         
        }
       
        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);
          } else {
            q.addFilter(propertyName, op, value);
          }
        }
      }else if(QueryFilterSearch.class.isAssignableFrom(filter.getClass())){
        Class<T> clazz = query.getQueriedClass();
        QueryFilterSearch qf = (QueryFilterSearch)filter;
        if(qf.fields.length>1)
          throw new SienaException("Search not possible for several fields in GAE: only one field");
        try {
          Field field = Util.getField(clazz, qf.fields[0]);
          if(field.isAnnotationPresent(Unindexed.class)){
            throw new SienaException("Cannot search the @Unindexed field "+field.getName());
          }
         
          // cuts match into words
          String[] words = qf.match.split("\\s");
         
          // if several words, then only OR operator represented by IN GAE
          Pattern pNormal = Pattern.compile("[^\\*](\\w+)[^\\*]");
          if(words.length>1){
            for(String word:words){
              if(!pNormal.matcher(word).matches()){
                throw new SienaException("Cannot do a multiwords search with the * operator");
              }
            }
            List<String> wordList = new ArrayList<String>();
            Collections.addAll(wordList, words);
            addSearchFilterIn(q, field, wordList);
          }else {
            // searches for pattern such as "alpha*" or "*alpha" or "alpha"
            Pattern pStart = Pattern.compile("(\\w+)\\*");
   
            String word = words[0];
            Matcher matcher = pStart.matcher(word);
            if(matcher.matches()){
              String realWord = matcher.group(1);
              addSearchFilterBeginsWith(q, field, realWord);
              continue;
            }
           
            matcher = pNormal.matcher(word);
            if(matcher.matches()){
              addSearchFilterEquals(q, field, word);
              continue;
            }
           
            Pattern pEnd = Pattern.compile("\\*(\\w+)");
            matcher = pEnd.matcher(word);
            if(matcher.matches()){
              throw new SienaException("Cannot do a \"*word\" search in GAE");
            }            
          }         
        }catch(Exception e){
          throw new SienaException(e);
        }
        break;
      }else if(QueryFilterEmbedded.class.isAssignableFrom(filter.getClass())){
        QueryFilterEmbedded qf = (QueryFilterEmbedded)filter;
       
        String propName = "";
        int sz = qf.fields.size();
        for(int i=0; i<sz; i++){
          propName += ClassInfo.getSingleColumnName(qf.fields.get(i));
          if(i < sz-1){
            propName += qf.fieldSeparator;
          }
        }
       
        Object value = qf.value;
        FilterOperator op = operators.get(qf.operator);
       
        // IN and NOT_EQUAL doesn't allow to use cursors
        if(op == FilterOperator.IN || op == FilterOperator.NOT_EQUAL){
          QueryOptionGaeContext gaeCtx = (QueryOptionGaeContext)query.option(QueryOptionGaeContext.ID);
          if(gaeCtx==null){
            gaeCtx = new QueryOptionGaeContext();
            query.options().put(gaeCtx.type, gaeCtx);
          }
          gaeCtx.useCursor = false;
          query.option(QueryOptionOffset.ID).activate();         
        }
       
        q.addFilter(propName, op, value);
      }
    }
   
    // adds filter on owners
    List<QueryOwned> ownees = query.getOwnees();
    for (QueryOwned ownee : ownees) {
      String propertyName = ClassInfo.getSimplestColumnName(ownee.field);
      FilterOperator op = operators.get("=");
      Key key = GaeMappingUtils.getKey(ownee.owner);
      q.addFilter(propertyName, op, key);
    }
   
    List<QueryOrder> orders = query.getOrders();
    for (QueryOrder order : orders) {
View Full Code Here

    txn.commit();
  }

  public void traceSchema() {
    Query q = new Query();
    Key k = KeyFactory.createKey(DATASTORE_ENTITY_SET_NAME,
        DATASTORE_ENTITY_KEY_NAME);
    q.setAncestor(k);
    q.setKeysOnly();
    List<Entity> l = datastore.prepare(q).asList(
        FetchOptions.Builder.withDefaults());
View Full Code Here

    }
  }

  public void dropSchema() {
    Query q = new Query();
    Key k = KeyFactory.createKey(DATASTORE_ENTITY_SET_NAME,
        DATASTORE_ENTITY_KEY_NAME);
    q.setAncestor(k);
    q.setKeysOnly();
    List<Entity> l = datastore.prepare(q).asList(
        FetchOptions.Builder.withDefaults());
View Full Code Here

                                  // department
                                  // information
                                  // object
                                  // dao
    if (!checkDepartmentExists(dio)) { // Checks if department exists first
      Key k = KeyFactory.createKey(DATASTORE_ENTITY_SET_NAME,
          DATASTORE_ENTITY_KEY_NAME); // Get the key of the parent
                        // node you wish to attach
                        // entity to (in this case our
                        // top most node)
      Transaction txn = datastore.beginTransaction(); // begin a
View Full Code Here

TOP

Related Classes of com.google.appengine.api.datastore.Key

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.