private Entity createEntityCopy(Entity entity) { Key key = entity.getKey(); NamespaceManager.set(key.getNamespace()); Key newKey; if (key.getId() == 0) { newKey = KeyFactory.createKey(key.getKind(), key.getName()); } else { newKey = KeyFactory.createKey(key.getKind(), key.getId()); } Entity newEntity = new Entity(newKey); newEntity.setPropertiesFrom(entity); return newEntity; }
public Long getId() { if (id != null) { return id.getId(); } else { return 0L; } }
@Test public void testMarshall_Embedded() { RootEntity rootObject = new RootEntity(); // one entity ChildEntity childObject = new ChildEntity("Test City"); ChildEntity embeddedObject = new ChildEntity("Old Test City"); embeddedObject.setId(1L); embeddedObject.setType("EmbeddedType"); rootObject.setId("TestUser"); rootObject.setCount(25); childObject.setParent(rootObject); rootObject.setNewChildEntity(childObject); // one entity rootObject.setEmbeddedEntity(embeddedObject); // not included, @Embedded IdentityHashMap<Object, Entity> stack = testMarshaller.marshall(null, rootObject); Entity rootObjectEntity = stack.get(rootObject); Entity childObjectEntity = stack.get(childObject); EmbeddedEntity ee = (EmbeddedEntity) rootObjectEntity.getProperty("embeddedEntity"); Key childKey = (Key) rootObjectEntity.getProperty("newChildEntity"); Key parentKey = childKey.getParent(); assertEquals(2, stack.size()); assertNotNull(parentKey); assertEquals(EmbeddedEntity.class, ee.getClass()); assertTrue(childKey instanceof Key); assertEquals(rootObjectEntity.getKey().getId(), parentKey.getId()); assertEquals(rootObjectEntity.getKey().getName(), parentKey.getName()); assertEquals(1L, ee.getProperties().get("id")); assertEquals("EmbeddedType", ee.getProperties().get("type")); }
/** @throws Exception */ @Test public void assignKeyIfNecessaryWhenParentExists() throws Exception { DatastoreUtil.allocateId(ds, "Child"); Key dummyKey2 = DatastoreUtil.allocateId(ds, "Child"); Key parentKey = DatastoreUtil.allocateId(ds, "Parent"); Entity entity = new Entity("Child", parentKey); DatastoreUtil.assignKeyIfNecessary(ds, entity); assertThat(entity.getKey().getId(), is(not(dummyKey2.getId() + 1))); }
@Override public ApiResponse generateRequestResponse(ApiRequest request) throws Exception { ApiResponse response = new ApiResponse(); // prevent spam UserEntry user = request.getUser(); long businessesCreated = user.getStats().getBusinessesCreated(); if (businessesCreated >= MAXIMUM_BUSINESS_CREATIONS) { Log.warn("Prevented business creation from user: "******"Spam prevention: You may not create any more businesses. Please contact us if you want to increase your quota.")); return response; } String name = request.getParameter("name"); String mail = request.getParameter("mail"); boolean addQueue = request.getParameterAsBoolean("addQueue", false); // create the business BusinessEntry businessEntry = new BusinessEntry(); businessEntry.setName(name); businessEntry.setMail(mail); Key businessKey = BusinessHelper.saveEntry(businessEntry); businessEntry.setKey(businessKey); // update user stats user.getStats().setBusinessesCreated(businessesCreated + 1); UserHelper.saveEntry(user); // add permission for business PermissionHelper.grantPermission(user, businessEntry, PermissionEntry.PERMISSION_OWN); if (addQueue) { // create a default queue QueueEntry queueEntry = new QueueEntry(businessKey.getId()); queueEntry.parseFromRequest(request); Key queueKey = QueueHelper.saveEntry(queueEntry); queueEntry.setKey(queueKey); // add permission for queue PermissionHelper.grantPermission( request.getUser(), queueEntry, PermissionEntry.PERMISSION_OWN); // add the new queue to the business in order to return both as one response ArrayList<QueueEntry> queues = new ArrayList<QueueEntry>(); queues.add(queueEntry); businessEntry.setQueues(queues); } response.setContent(businessEntry); return response; }
/** * Adds an Event todo in the Datastore * * @param et the event todo added * @return whether transaction is successful */ public boolean addEventTodo(EventTodoModel et) { boolean ok = false; if (getEventTodoModelByTodoId(et.getEventID(), et.getTodoId()) == null) { Key key = Datastore.allocateId(parentKey, "EventTodoModel"); Transaction trans = Datastore.beginTransaction(); et.setKey(key); et.setId(key.getId()); Datastore.put(et); trans.commit(); ok = true; } return ok; }
public String toString() { StringBuilder b = new StringBuilder(); Key k = KeyFactory.stringToKey(key); b.append("Question (").append(k.getId()).append(") : ").append(" Text=").append(questionText); b.append(" Date Posted=").append(datePosted); b.append(" ParentInfo = ").append(k.getParent().toString()); b.append("\n"); for (Comment c : comments) { b.append("\t\t").append(c.toString()).append("\n"); } return b.toString(); }
public void testFetch() { Entity entity = new Entity(HasTableAndColumnsInMappingJDO.TABLE_NAME); entity.setProperty(HasTableAndColumnsInMappingJDO.FOO_COLUMN_NAME, "foo val"); Key key = ds.put(entity); String keyStr = KeyFactory.keyToString(key); beginTxn(); HasTableAndColumnsInMappingJDO htacim = pm.getObjectById(HasTableAndColumnsInMappingJDO.class, keyStr); assertNotNull(htacim); assertEquals(Long.valueOf(key.getId()), htacim.getId()); assertEquals("foo val", htacim.getFoo()); commitTxn(); }
public static Note createNewNote( Key tactKey, NoteValue noteValue, Long orderNumber, Accidental accid, NotePitch pitch) { Entity note = new Entity(Note.KIND); if (tactKey != null) { note.setProperty(Note.TACT_ID, tactKey.getId()); } note.setProperty(NUMBER, orderNumber); note.setUnindexedProperty(Note.VALUE, noteValue.toString()); note.setUnindexedProperty(Note.PITCH, pitch.toString()); note.setUnindexedProperty(Note.DYNAMIC, Dynamic.f.toString()); note.setUnindexedProperty(Note.ACCIDENTAL, accid.toString()); note.setUnindexedProperty(Note.TRIOLA, false); note.setUnindexedProperty(Note.WITH_DOT, false); note.setUnindexedProperty(Note.AKCENT, false); note.setUnindexedProperty(TO_REMOVE, false); DataStoreWrapper.save(note); return new Note(note); }
@Override public void deleteConference(long conferenceId) { try { Conference c = em.find(Conference.class, conferenceId); em.getTransaction().begin(); // first remove related relation ... for (Key Skey : c.getStakeholders()) { Stakeholder s = em.find(Stakeholder.class, Skey.getId()); s.getConferences().remove(c.getId()); em.persist(s); } // then remove the entity em.remove(c); em.getTransaction().commit(); } catch (Exception e) { e.printStackTrace(); } finally { em.close(); } }
@Test public void testMarshall_Child() { RootEntity rootObject = new RootEntity(); // one entity ChildEntity childObject = new ChildEntity("Test City"); rootObject.setId("TestUser"); rootObject.setCount(25); childObject.setParent(rootObject); rootObject.setNewChildEntity(childObject); // one entity IdentityHashMap<Object, Entity> stack = testMarshaller.marshall(null, rootObject); Entity rootObjectEntity = stack.get(rootObject); Entity childObjectEntity = stack.get(childObject); Key childKey = (Key) rootObjectEntity.getProperty("newChildEntity"); Key parentKey = childKey.getParent(); assertEquals(2, stack.size()); assertNotNull(parentKey); assertTrue(childKey instanceof Key); assertEquals(rootObjectEntity.getKey().getId(), parentKey.getId()); assertEquals(rootObjectEntity.getKey().getName(), parentKey.getName()); }
public T findByKey(final Key key) { return findOne(key.getId()); }
private String getJobWideUniqueData(Key jobKey) { return getJobWideUniqueData(jobKey.getParent().getId(), jobKey.getId()); }
public String getId() { return Long.toString(key.getId()); }
/** * Test that an entity in the datastore with absent fields loads correctly, when the fields in the * entity class have default values */ @Test public void testNewVersionOfEntity() throws Exception { fact.register(EntityWithDefault.class); Objectify ofy = fact.begin(); // e1 has absent properties Entity e1 = new Entity("EntityWithDefault"); e1.setProperty("a", "1"); com.google.appengine.api.datastore.Key k1 = ofy.getDatastore().put(e1); EntityWithDefault o = ofy.get(new Key<EntityWithDefault>(EntityWithDefault.class, k1.getId())); assert o.a != null; assert "1".equals(o.a); assert o.b != null; assert "foo".equals(o.b); assert o.c != null; assert "bar".equals(o.c); assert o.s != null; assert "default2".equals(o.s.s); }