コード例 #1
0
  @Test
  public void transactionsOnVariusTypesOfEntities() throws EntityNotFoundException {

    Entity person = new Entity("Person", "tom");
    datastore.put(person);

    // Transaction on root entities
    Transaction transaction = datastore.beginTransaction();

    Entity tom = datastore.get(person.getKey());
    tom.setProperty("age", 40);
    datastore.put(tom);
    transaction.commit();

    // Transaction on child entities
    transaction = datastore.beginTransaction();
    tom = datastore.get(person.getKey());

    // Create a Photo entity that is a child of Person entity named "tom"
    Entity photo = new Entity("Photo", tom.getKey());
    photo.setProperty("photoUrl", "images/photo");
    datastore.put(photo);
    transaction.commit();

    // Transaction on entities in different entity groups
    transaction = datastore.beginTransaction();

    Entity photoNotChild = new Entity("Photo");
    photoNotChild.setProperty("photoUrl", "images/photo");
    datastore.put(photoNotChild);
    transaction.commit();
  }
コード例 #2
0
  @Test
  public void updateFieldInEntity() throws EntityNotFoundException {

    Entity person = new Entity("Person");
    person.setProperty("firstName", "Ivan");
    person.setProperty("age", 22l);

    Key key = datastore.put(person);

    // Start transaction
    Transaction transaction = datastore.beginTransaction();
    try {

      Entity result = datastore.get(key);
      result.setProperty("age", 30l);

      datastore.put(result);

      transaction.commit();

    } finally {
      if (transaction.isActive()) {
        transaction.rollback();
      }
    }

    Entity result = datastore.get(key);
    assertThat("Ivan", is(equalTo(result.getProperty("firstName"))));
    assertThat(30l, is(equalTo(result.getProperty("age"))));
  }
コード例 #3
0
ファイル: UserStorage.java プロジェクト: kvj/Lima1
 public static String verifyToken(String token) {
   DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
   Transaction txn = datastore.beginTransaction();
   try {
     Query searchToken = new Query("Token").addFilter("token", FilterOperator.EQUAL, token);
     Entity tokenEntity = datastore.prepare(searchToken).asSingleEntity();
     if (null == tokenEntity) {
       log.warn("Token {} not found - error", token);
       return null;
     }
     log.info("Updating token");
     tokenEntity.setProperty("accessed", new Date().getTime());
     datastore.put(txn, tokenEntity);
     txn.commit();
     log.info("Token OK");
     Entity userEntity = datastore.get((Key) tokenEntity.getProperty("user"));
     return (String) userEntity.getProperty("username");
   } catch (Exception e) {
     log.error("Token error", e);
     return null;
   } finally {
     if (txn.isActive()) {
       txn.rollback();
     }
   }
 }
コード例 #4
0
 @PUT
 @Consumes(MediaType.APPLICATION_XML)
 public Response putTaskData(String value) {
   Response res = null;
   // add your code here
   // first check if the Entity exists in the datastore
   DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
   MemcacheService syncCache = MemcacheServiceFactory.getMemcacheService();
   syncCache.setErrorHandler(ErrorHandlers.getConsistentLogAndContinue(Level.INFO));
   Key entKey = KeyFactory.createKey("TaskData", keyname);
   Date date = new Date();
   try {
     // if it is, signal that we updated the entity
     Entity ts = datastore.get(entKey);
     ts.setProperty("value", value);
     ts.setProperty("date", date);
     datastore.put(ts);
     res = Response.noContent().build();
   } catch (EntityNotFoundException e) {
     // if it is not, create it and
     // signal that we created the entity in the datastore
     Entity taskdata = new Entity("TaskData", keyname);
     taskdata.setProperty("value", value);
     taskdata.setProperty("date", date);
     datastore.put(taskdata);
     res = Response.created(uriInfo.getAbsolutePath()).build();
   }
   TaskData td = new TaskData(keyname, value, date);
   syncCache.put(keyname, td);
   return res;
 }
コード例 #5
0
  private ServiceResponse updateStatus(String _name, Long _id, String _status, User user)
      throws UnauthorizedException {
    if (user == null) {
      throw new UnauthorizedException("UnauthorizedException # User is Null.");
    }
    Key key = KeyFactory.createKey(_name, _id);
    Date currentDate = new Date();
    String userEmail = user.getEmail();

    DatastoreService dsService = DatastoreServiceFactory.getDatastoreService();
    ServiceResponse res = new ServiceResponse();
    try {
      Entity entity = dsService.get(key);

      entity.setProperty(_updatedAt, currentDate);
      entity.setProperty(_updatedBy, userEmail);

      entity.setProperty(_status, _status);

      dsService.put(entity);

      res.set_id(entity.getKey().getId());

    } catch (com.google.appengine.api.datastore.EntityNotFoundException e) {
      throw new EntityNotFoundException("Object does not exist");
    }
    return res;
  }
コード例 #6
0
  /**
   * This method is used for updating an existing entity. If the entity does not exist in the
   * datastore, an exception is thrown. It uses HTTP PUT method.
   *
   * @param store the entity to be updated.
   * @return The updated entity.
   * @throws UnauthorizedException
   */
  @ApiMethod(
      name = "modify",
      scopes = {Config.EMAIL_SCOPE},
      clientIds = {Config.CHROME_CLIENT_ID, Config.WEB_CLIENT_ID, Config.API_EXPLORER_CLIENT_ID})
  public ServiceResponse modify(
      @Named("_name") String _name, @Named("_id") Long _id, ServiceRequest req, User user)
      throws UnauthorizedException {
    if (user == null) {
      throw new UnauthorizedException("UnauthorizedException # User is Null.");
    }
    Key key = KeyFactory.createKey(_name, _id);
    Date currentDate = new Date();
    String userEmail = user.getEmail();

    DatastoreService dsService = DatastoreServiceFactory.getDatastoreService();
    ServiceResponse res = new ServiceResponse();
    try {
      Entity entity = dsService.get(key);

      entity.setProperty(_updatedAt, currentDate);
      entity.setProperty(_updatedBy, userEmail);

      entity.setUnindexedProperty(data, new Text(req.getData()));

      dsService.put(entity);

      res.set_id(entity.getKey().getId());
    } catch (com.google.appengine.api.datastore.EntityNotFoundException e) {
      throw new EntityNotFoundException("Object does not exist.");
    }

    return res;
  }
コード例 #7
0
  @ApiMethod(name = "group.save", httpMethod = ApiMethod.HttpMethod.POST)
  public void setSaveGroup(@Named("groupId") Long groupId, Group data) throws IOException {

    // Get the key
    Key rootKey = KeyFactory.createKey("Root", 1);
    Key groupKey = KeyFactory.createKey(rootKey, "Group", groupId);

    // Get or create the group
    Entity group;
    try {
      group = dataStore.get(groupKey);

      //noinspection unchecked
      List<Key> keys = (List<Key>) group.getProperty("forms");
      keys = firstNonNull(keys, Collections.<Key>emptyList());
      // Erase all the previous data
      dataStore.delete(keys);
    } catch (EntityNotFoundException e) {
      logger.fine("Creating new group");
    }

    // Saves the new data
    JsonNode json = NewData.mapper().convertValue(data, JsonNode.class);
    NewData newData = new NewData(dataStore, json);
    newData.run(true);
  }
コード例 #8
0
 @SuppressWarnings("unchecked")
 @Override
 protected Response findPizzaComponents(String token) {
   if (token == null) {
     return RestResponse.FORBIDDEN();
   }
   DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
   Key key = KeyFactory.createKey("PizzaFactory", token);
   List<PizzaCrust> components = null;
   try {
     Entity pizzaFactory = datastore.get(key);
     List<EmbeddedEntity> list = (List<EmbeddedEntity>) pizzaFactory.getProperty(type);
     components = new ArrayList<PizzaCrust>();
     if (list != null) {
       for (EmbeddedEntity e : list) {
         PizzaCrust component = PizzaCrustResource.entityToObject(e);
         components.add(component);
       }
     }
     GenericEntity<List<PizzaCrust>> lists = new GenericEntity<List<PizzaCrust>>(components) {};
     response = RestResponse.OK(lists);
   } catch (EntityNotFoundException e) {
     response = RestResponse.NOT_FOUND();
   }
   return response;
 }
コード例 #9
0
  /**
   * This method gets the entity having primary key id. It uses HTTP GET method.
   *
   * @param id the primary key of the java bean.
   * @return The entity with primary key id.
   * @throws UnauthorizedException
   */
  @ApiMethod(
      name = "findById",
      scopes = {Config.EMAIL_SCOPE},
      clientIds = {Config.CHROME_CLIENT_ID, Config.WEB_CLIENT_ID, Config.API_EXPLORER_CLIENT_ID})
  public ServiceResponse findById(@Named("_name") String _name, @Named("_id") Long _id, User user)
      throws UnauthorizedException {
    if (user == null) {
      throw new UnauthorizedException("UnauthorizedException # User is Null.");
    }
    Key key = KeyFactory.createKey(_name, _id);

    DatastoreService dsService = DatastoreServiceFactory.getDatastoreService();
    ServiceResponse res = new ServiceResponse();
    try {
      Entity entity = dsService.get(key);
      res.set_id(entity.getKey().getId());

      res.set_createdAt((Date) entity.getProperty(_createdAt));
      res.set_createdBy((String) entity.getProperty(_createdBy));

      res.set_upatedAt((Date) entity.getProperty(_updatedAt));
      res.set_updatedBy((String) entity.getProperty(_updatedBy));

      res.set_status((String) entity.getProperty(_status));

      Text dataText = (Text) entity.getProperty(data);
      res.setData(dataText.getValue());

    } catch (com.google.appengine.api.datastore.EntityNotFoundException e) {
      throw new EntityNotFoundException("Object does not exist.");
    }

    return res;
  }
コード例 #10
0
ファイル: MemoStorable.java プロジェクト: almende/memo-nodes
  // Factory methods:
  public static MemoStorable load(Entity ent) {
    //		long start = System.currentTimeMillis();
    byte[] result;
    Key key = ent.getKey();
    int size = 0;
    long spread = 0;
    //		int count=0;
    try {
      Blob blob = (Blob) ent.getProperty("payload");
      size = (int) ((Long) ent.getProperty("size") % Integer.MAX_VALUE);
      spread = ((Long) ent.getProperty("spread"));

      byte[] data = blob.getBytes();
      result = Arrays.copyOf(data, data.length);
      //			count++;
      while (ent.hasProperty("next")) {
        // Expensive, should not be used too much!
        Key next = (Key) ent.getProperty("next");
        ent = datastore.get(next);
        blob = (Blob) ent.getProperty("payload");
        data = blob.getBytes();
        result = concat(data, result); // Add to front of result, due to
        //				count++;						// the storing order
      }

    } catch (EntityNotFoundException e) {
      e.printStackTrace();
      return null;
    }
    MemoStorable res = _unserialize(result, size);
    res.spread = spread;
    res.storedSize = result.length;
    if (res != null) res.myKey = key;
    return res;
  }
コード例 #11
0
 private Entity getEntity(BlobKey blobKey) throws FileNotFoundException {
   DatastoreService datastore = getDatastoreService();
   try {
     return datastore.get(getMetadataKeyForBlobKey(blobKey));
   } catch (EntityNotFoundException ex) {
     throw new FileNotFoundException();
   }
 }
コード例 #12
0
ファイル: Util.java プロジェクト: nimezhu/RNABrowser
 /**
  * Search and return the entity from datastore.
  *
  * @param key : key to find the entity
  * @return entity
  */
 public static Entity findEntity(Key key) {
   logger.log(Level.INFO, "Search the entity");
   try {
     return datastore.get(key);
   } catch (EntityNotFoundException e) {
     return null;
   }
 }
コード例 #13
0
 // [START entity_group_1]
 private static long getEntityGroupVersion(DatastoreService ds, Transaction tx, Key entityKey) {
   try {
     return Entities.getVersionProperty(ds.get(tx, Entities.createEntityGroupKey(entityKey)));
   } catch (EntityNotFoundException e) {
     // No entity group information, return a value strictly smaller than any
     // possible version
     return 0;
   }
 }
コード例 #14
0
 @Override
 public Entity get(String key) {
   Preconditions.checkNotNull(key);
   try {
     return service.get(KeyFactory.createKey(kind, escape(key)));
   } catch (EntityNotFoundException e) {
     return null;
   }
 }
コード例 #15
0
ファイル: Settings.java プロジェクト: jebriggs/AppEngine-cms
 public Config load() {
   Config cfg = new Config();
   try {
     cfg.entity = service.get(key());
   } catch (EntityNotFoundException e) {
     cfg.entity = new Entity(key());
     cfg.reset();
   }
   return cfg;
 }
コード例 #16
0
ファイル: MemoStorable.java プロジェクト: almende/memo-nodes
 public void delete(Key key) {
   if (deletedCache.getIfPresent(key) != null) return;
   if (myKey.equals(key)) {
     deletedCache.put(key, this);
   }
   try {
     Entity ent = datastore.get(key);
     if (ent.hasProperty("next")) {
       delete((Key) ent.getProperty("next")); // recurse
     }
     datastore.delete(key);
     try {
       datastore.get(myKey);
     } catch (EntityNotFoundException e) {
     }
   } catch (Exception e) {
     // e.printStackTrace();
   }
 }
コード例 #17
0
ファイル: MemoStorable.java プロジェクト: almende/memo-nodes
 public static MemoStorable load(Key key) {
   MemoStorable res = deletedCache.getIfPresent(key);
   if (res != null) return res;
   try {
     Entity ent = datastore.get(key);
     return load(ent);
   } catch (EntityNotFoundException e) {
     return null;
   }
 }
コード例 #18
0
ファイル: ModelDAO.java プロジェクト: plovesm/swim-meet_GAE
  public static Entity getEntity(Key key) {
    try {
      // First retrieve the entity
      Entity ent = ds.get(key);

      return ent;

    } catch (EntityNotFoundException e) {
      return null;
    }
  }
コード例 #19
0
 @RequestMapping(value = "/mobileform/{id}", method = RequestMethod.GET)
 public Entity getMobileFormById(@PathVariable String id) throws EntityNotFoundException {
   UserService userService = UserServiceFactory.getUserService();
   User user = userService.getCurrentUser();
   if (user != null) {
     DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
     Key key = KeyFactory.createKey("MobileForm", id);
     Entity result = datastore.get(key);
     return result;
   } else return null;
 }
コード例 #20
0
  public static File getByPathAndUser(String path, Owner owner) throws EntityNotFoundException {
    DatastoreService ds = DatastoreServiceFactory.getDatastoreService();

    Entity e = ds.get(PathHelper.getStorageKey("File", path, owner));

    if (e == null) return null;

    File f = new File();
    ReflectionHelper.setPropertiesFromEntity(File.class, f, e);

    return f;
  }
コード例 #21
0
  @ApiMethod(name = "form.get")
  public Form getForm(@Named("groupId") Long groupId, @Named("formId") Long formId)
      throws EntityNotFoundException {

    // Get the key
    Key rootKey = KeyFactory.createKey("Root", 1);
    Key groupKey = KeyFactory.createKey(rootKey, "Group", groupId);
    Key key = KeyFactory.createKey(groupKey, "Form", formId);

    Entity entity = dataStore.get(key);

    return Form.fromEntity(dataStore, entity);
  }
コード例 #22
0
  @ApiMethod(name = "form.list")
  public List<Form> getForms(@Named("groupId") Long groupId) throws EntityNotFoundException {

    // Get the key
    Key rootKey = KeyFactory.createKey("Root", 1);
    Key groupKey = KeyFactory.createKey(rootKey, "Group", groupId);

    // Get or create the group
    Entity group;
    group = dataStore.get(groupKey);

    //noinspection unchecked
    List<Key> keys = (List<Key>) group.getProperty("forms");
    keys = firstNonNull(keys, Collections.<Key>emptyList());

    List<Form> forms = new ArrayList<>();
    for (Key key : keys) {
      Form f = Form.fromEntity(dataStore, dataStore.get(key));
      forms.add(f);
    }

    return forms;
  }
コード例 #23
0
ファイル: MemoStorable.java プロジェクト: almende/memo-nodes
  public Key store(Key orig, String type, long storeDate) {
    //		long start = System.currentTimeMillis();
    final int MAXSIZE = 1000000;
    Entity ent;
    Key next = null;
    this.storeTime = storeDate;
    this.nanoTime = System.nanoTime();

    byte[] data = this.serialize();
    Integer length = data.length;
    int pointer = 0;
    //		int counter = 0;
    while (length - pointer >= MAXSIZE) {
      // Expensive, should not be used too much!
      ent = new Entity(type + "_fragment");
      ent.setUnindexedProperty(
          "payload", new Blob(Arrays.copyOfRange(data, pointer, pointer + MAXSIZE)));
      if (next != null) ent.setUnindexedProperty("next", next);
      datastore.put(ent);
      //			counter++;
      next = ent.getKey();
      pointer += MAXSIZE;
    }

    if (orig != null) {
      System.err.println(
          "Warning, storing storable twice! Strange, should not happen with our immutable structures.");
      ent = new Entity(orig);
    } else {
      ent = new Entity(type);
    }
    ent.setUnindexedProperty("payload", new Blob(Arrays.copyOfRange(data, pointer, length)));
    if (next != null) ent.setUnindexedProperty("next", next);
    ent.setProperty("timestamp", this.storeTime);
    ent.setProperty("size", length);
    ent.setProperty("spread", this.spread);
    datastore.put(ent);
    //		counter++;
    myKey = ent.getKey();
    // Try to force index writing
    try {
      datastore.get(myKey);
    } catch (EntityNotFoundException e) {
    }
    this.storedSize = length;
    // System.out.println("Just stored shard of "+length+ " bytes in "+counter+" fragments  in
    // "+(System.currentTimeMillis()-start)+" ms");
    return myKey;
  }
コード例 #24
0
  @ApiMethod(name = "group.list")
  public List<Group> getGroups() throws EntityNotFoundException, IOException {
    Key rootKey = KeyFactory.createKey("Root", 1);
    // Get or create the root
    Entity root;
    try {
      root = dataStore.get(rootKey);
    } catch (EntityNotFoundException e) {
      //			root = new Startup(dataStore).run(false);
      return Collections.emptyList();
    }

    //noinspection unchecked
    List<Key> keys = (List<Key>) root.getProperty("groups");
    keys = firstNonNull(keys, Collections.<Key>emptyList());

    List<Group> groups = new ArrayList<>();
    for (Key key : keys) {
      Group g = Group.fromEntity(dataStore, dataStore.get(key));
      groups.add(g);
    }

    return groups;
  }
コード例 #25
0
 public AudioClip getAudioClipById(String id) throws BadRequestException {
   AudioClip audioClip;
   try {
     Key audio_key = KeyFactory.stringToKey(id);
     Entity result = datastore.get(audio_key);
     audioClip =
         new AudioClip(
             id,
             (String) result.getProperty(TuneInConstants.AUDIO_CLIP_TITLE),
             (String) result.getProperty(TuneInConstants.AUDIO_CLIP_OWNER_ID),
             (String) result.getProperty(TuneInConstants.AUDIO_CLIP_AUDIO_ID),
             (String) result.getProperty(TuneInConstants.AUDIO_CLIP_IMAGE_ID),
             (Date) result.getProperty(TuneInConstants.AUDIO_CLIP_DATE));
     return audioClip;
   } catch (Exception e) {
     throw new BadRequestException();
   }
 }
コード例 #26
0
ファイル: TaskDao.java プロジェクト: azizdeves/knightyusuf
  public List<Statistic> getStatisitc(String name, String momin, Date start, int nbrWeek) {

    TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
    List<Key> keys = new ArrayList<Key>();
    String st = null;
    Calendar cal = Calendar.getInstance();
    cal.setFirstDayOfWeek(Calendar.SUNDAY);
    cal.setTime(start);
    for (int i = 0; i < nbrWeek; i++) {
      st = momin + "$" + name + "$" + cal.get(Calendar.YEAR) + "$" + cal.get(Calendar.WEEK_OF_YEAR);
      keys.add(KeyFactory.createKey(Statistic.class.getSimpleName(), st));
      cal.add(Calendar.DAY_OF_MONTH, 7);
    }

    DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
    Map<Key, Entity> map = ds.get(keys);
    return fromEntity(map.values());
  }
コード例 #27
0
ファイル: Datastore.java プロジェクト: samuelgoto/kumbaya
 /**
  * Gets a persistent record with the devices to be notified using a multicast message.
  *
  * @param encodedKey encoded key for the persistent record.
  */
 public static List<String> getMulticast(String encodedKey) {
   Key key = KeyFactory.stringToKey(encodedKey);
   Entity entity;
   Transaction txn = datastore.beginTransaction();
   try {
     entity = datastore.get(key);
     @SuppressWarnings("unchecked")
     List<String> devices = (List<String>) entity.getProperty(MULTICAST_REG_IDS_PROPERTY);
     txn.commit();
     return devices;
   } catch (EntityNotFoundException e) {
     logger.severe("No entity for key " + key);
     return Collections.emptyList();
   } finally {
     if (txn.isActive()) {
       txn.rollback();
     }
   }
 }
コード例 #28
0
  @DELETE
  public void deleteIt() {

    // delete an entity from the datastore
    // just print a message upon exception (don't throw)
    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    MemcacheService syncCache = MemcacheServiceFactory.getMemcacheService();
    if (syncCache.get(keyname) != null) {
      syncCache.delete(keyname);
    }
    Key entKey = KeyFactory.createKey("TaskData", keyname);
    try {
      Entity ent = datastore.get(entKey);
      datastore.delete(entKey);
      System.err.println("TaskData deleted in Datastore");
    } catch (EntityNotFoundException e) {
      System.err.println("TaskData not found in Datastore");
    }
  }
コード例 #29
0
 // for the application
 @GET
 @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
 public TaskData getTaskData() {
   // same code as above method
   DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
   MemcacheService syncCache = MemcacheServiceFactory.getMemcacheService();
   if (syncCache.get(keyname) != null) {
     TaskData ts = (TaskData) syncCache.get(keyname);
     return ts;
   }
   Key entKey = KeyFactory.createKey("TaskData", keyname);
   try {
     Entity ent = datastore.get(entKey);
     TaskData ts =
         new TaskData(keyname, (String) ent.getProperty("value"), (Date) ent.getProperty("date"));
     return ts;
   } catch (EntityNotFoundException e) {
     throw new RuntimeException("Get: TaskData with " + keyname + " not found");
   }
 }
コード例 #30
0
ファイル: aToken.java プロジェクト: probe-shawn/alive-estate
  public static Entity get(
      HttpServletRequest request, HttpServletResponse response, String cookie_name) {
    String key = null;
    Cookie[] cookies = request.getCookies();
    for (int i = 0; cookies != null && i < cookies.length; ++i)
      if (cookies[i].getName().equals(cookie_name)) {
        key = cookies[i].getValue();
      }

    DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
    Entity entity = null;
    if (key != null) {
      try {
        entity = ds.get(KeyFactory.stringToKey(key));
      } catch (EntityNotFoundException e) {
        entity = null;
      }
    }
    return entity;
  }