Package com.google.appengine.api.datastore

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


    q_user.addFilter(ACCOUNT_ID_PROPERTY, FilterOperator.EQUAL, user);
    PreparedQuery pq = datastore.prepare(q_user);
    Entity e_user = pq.asSingleEntity();
    if(e_user == null)
      return wiol;
    Key k = e_user.getKey();
    Query q = new Query(WORKLIST_ITEM_KIND);
    q.setAncestor(k);
    PreparedQuery pq_wl = datastore.prepare(q);
    List<Entity> el = pq_wl.asList(FetchOptions.Builder.withDefaults());
    for(Iterator<Entity> i = el.iterator(); i.hasNext(); ) {
View Full Code Here


    // TODO remove this

    if (be == null)
      return null;

    Key k = be.getBlogKey();
    String key = "title" + String.valueOf(k.getId());

    Object title = CacheService.get(key);

    if (title == null) {
      Blog b = this.currentManager.getObjectById(Blog.class, k);
View Full Code Here

    List<Blog> blogs = new ArrayList<Blog>();

    if (talkfeedUser != null) {
      // key from user
      Key key = talkfeedUser.getKey();

      // list subscription
      Query qSub = pm.newQuery(Subscription.class);
      qSub.setFilter("userKey == bk");
      qSub.declareParameters("com.google.appengine.api.datastore.Key bk");
View Full Code Here

  private List<Entity> toEntities(Object object, Object longOrStringId, Object parent)
  {
    final List<Entity> result = Lists.newArrayList();
    final ObjectEncoder encoder = store.getEncoder(object.getClass());

    final Key existingKey = cache.getKey(object);
    final Key key;
    if (existingKey == null)
    {
      if (longOrStringId == null)
      {
        longOrStringId = encoder.getId(object);
        if (longOrStringId == null)
          longOrStringId = encoder.getNextId(service);
      }

      if (parent == null)
        parent = encoder.getParent(object);

      key = createKey(cache.getKey(parent), typeToKind(object.getClass()), longOrStringId);
    }
    else
    {
      key = existingKey;
      if (!cache.isActivated(object))
        throw new IllegalArgumentException("Attempt to store unactivated instance!");
    }
    final Entity e = new Entity(key);

    register(object, key, true);

    encoder.encode(new InstanceStore()
    {
      @Override
      public Key toKey(Object instance, Object parent)
      {
        if (instance == null)
          return null;

        Key key = cache.getKey(instance);
        if (key == null)
        {
          result.addAll(toEntities(instance, null, parent));
          key = cache.getKey(instance);
        }
View Full Code Here

  }

  private <T> T toObject(Class<T> clazz, final Entity entity, boolean refresh, int retries, ActivationFilter activationFilter)
  {
    final List<String> activationPath = Lists.newArrayList();
    Key key = entity.getKey();
    T object = cache.<T> getObject(key);
    if (object == null || refresh || !cache.isActivated(object))
    {
      if (object == null)
        object = createShell(clazz, key);
View Full Code Here

          @Override
          protected Iterable<Key> keys()
          {
            List<Key> keys = Lists.newArrayList();
            Key parentKey = cache.getKey(parent);
            String kind = typeToKind(type);
            for (Object id : ids)
              keys.add(createKey(parentKey, kind, id));
            return keys;
          }
View Full Code Here

        fieldEncoder.encode(new InstanceStore()
        {
          @Override
          public Key toKey(Object instance, Object parent)
          {
            Key key = cache.getKey(instance);
            if (key == null)
              throw new IllegalArgumentException("Must filter on instances from same ObjectStoreSession");
            return key;
          }
        }, ConvertUtils.convert(v, f.getType()), new PropertyStore()
        {
          @Override
          public void setProperty(String key, Object value, boolean index)
          {
            if(isMultiple)
            {
              List<Object> list = ObjectUtils.cast(properties.get(key));
              if(list == null)
              {
                list = Lists.newArrayList();
                properties.put(key, list);
              }
              list.add(value);
            }
            else
            {
              properties.put(key, value);
            }
          }
        });

        if (properties.size() > 1 && gop != FilterOperator.EQUAL)
          throw new IllegalArgumentException("Cannot filter embedded types with operator: " + gop);

        for (Entry<String, Object> entry : properties.entrySet())
          query.addFilter(entry.getKey(), gop, entry.getValue());

        return this;
      }

      @Override
      public Find<T> filterBetween(String field, Object lower, Object upper)
      {
        return filterBetween(field, lower, upper, true, false);
      }

      @Override
      public Find<T> filterBetween(String field, Object lower, Object upper, boolean includeBottom, boolean includeTop)
      {
        Find.Op lowerOp, upperOp;
        if (includeBottom)
          lowerOp = Find.Op.GreaterThanOrEqualTo;
        else
          lowerOp = Find.Op.GreaterThan;

        if (includeTop)
          upperOp = Find.Op.LessThanOrEqualTo;
        else
          upperOp = Find.Op.LessThan;

        filter(field, lowerOp, lower);
        filter(field, upperOp, upper);
        return this;
      }

      @Override
      public Find<T> filterBeginsWith(String field, String value)
      {
        filterBetween(field, value, value + "\uFFFD");
        return this;
      }

      @Override
      public Find<T> ancestor(Object ancestor)
      {
        query.setAncestor(cache.getKey(ancestor));
        return this;
      }

      int start = 0;

      @Override
      public Find<T> start(int offset)
      {
        this.start = offset;
        return this;
      }

      int limit = Integer.MAX_VALUE;

      @Override
      public Find<T> limit(int count)
      {
        limit = count;
        return this;
      }

      private boolean activated = true;

      @Override
      public Find<T> unactivated()
      {
        activated = false;
        query.setKeysOnly();
        return this;
      }

      private boolean refresh = false;

      @Override
      public Find<T> refresh()
      {
        refresh = true;
        return this;
      }

      private int retries = 5;

      @Override
      public Find<T> retries(int count)
      {
        retries = count;
        return this;
      }

      private List<String> activatePatterns = Lists.newArrayList();

      @Override
      public Find<T> activate(String... patterns)
      {
        activatePatterns.addAll(Arrays.asList(patterns));
        return this;
      }

      @Override
      public Find<T> activate(int levels)
      {
        return activate(ActivationFilter.getLevelActivationFilter(levels));
      }

      @Override
      public Iterable<T> now()
      {
        return FutureUtils.safeGet(later());
      }

      @Override
      public Future<Iterable<T>> later()
      {
        final Iterable<T> result = queryNow(); // get the query going now
        return new ImmediateFuture<Iterable<T>>()
        {
          @Override
          public Iterable<T> get()
          {
            return result;
          }
        };
      }

      private Iterable<T> queryNow()
      {
        return toEntityIterable(service.prepare(query).asIterable(withOffset(start).limit(limit)));
      }

      private Iterable<T> toEntityIterable(final Iterable<Entity> iterable)
      {
        return new Iterable<T>()
        {
          @Override
          public Iterator<T> iterator()
          {
            return new Iterator<T>()
            {
              Iterator<Entity> nested = iterable.iterator();
              ActivationFilter filter = ActivationFilter.compile(activatePatterns);

              @Override
              public boolean hasNext()
              {
                return nested.hasNext();
              }

              @SuppressWarnings("unchecked")
              @Override
              public T next()
              {
                Entity entity = nested.next();
                if (activated)
                {
                  return toObject(type, entity, refresh, retries, filter);
                }
                else
                {
                  Key key = entity.getKey();
                  T object = (T) cache.getKey(key);
                  if (object == null)
                    object = createShell(type, key);
                  return object;
                }
View Full Code Here

  public Key getKey(Object object)
  {
    if (object == null)
      return null;

    Key key = objectToKey.get(object);
    if (key == null)
    {
      key = upstream.getKey(object);
      if (key != null)
        localSet(object, key);
View Full Code Here

  }

  @Override
  public Object decode(InstanceSource instanceSource, PropertySource source)
  {
    Key key = source.getProperty(property);
    if (key == null)
      return null;
    else
      return instanceSource.fromKey(fieldName, key);
  }
View Full Code Here

      DataManager dm = DataManagerFactory.getInstance();
      PersistenceManager pm = dm.newPersistenceManager();
     
      User u = dm.getUserFromId(pm,jid);
     
      Key subToRemove = null;
     
      //no number : remove last subscription
      if (number == null) subToRemove = u.getLastSubscriptionKey();
      else {
        int nb = 0;
        try {
          nb = Integer.valueOf(number);
        } catch (Exception ex){
          //bad number
          return;
        }
       
        //find sub
        Query q = pm.newQuery(Subscription.class);
        q.setFilter("userKey == k");
        q.declareParameters("com.google.appengine.api.datastore.Key k");
       
        @SuppressWarnings("unchecked")
        List<Subscription> subs = (List<Subscription>) q.execute(u.getKey());
        int i = 1;
        for(Subscription sub : subs){
          if (nb == i) {
            subToRemove = sub.getKey();
            break;
          }
          i++;
        }
      }
     
      if (subToRemove != null){
        UserManager serv = new UserManager();
        serv.removeUserSubscription(subToRemove.getId());
       
        TalkService.sendMessage(jid, "subscription is removed");
        //update cache
        SubscriptionCache.removeUserFromCache(jid);
      }
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.