private static String extractType(Entity entity) { return entity.getKind(); }
private Entity makeCopy(Entity entity) { // We don't check key when we look for changes so it's // ok that the copy doesn't have its key set. Entity copy = new Entity(entity.getKind()); EntityUtils.copyProperties(entity, copy); return copy; }
/** * Ensures that the given value is not null. Throws * {@link NullPointerException} with a helpful error message if it is. */ private Object checkAssignmentToNotNullField(Object val, int fieldNumber) { if (val != null) { // not null so no problem return val; } // Put together a really helpful error message AbstractMemberMetaData mmd = getMetaData(fieldNumber); String propertyName = getPropertyNameForMember(mmd); final String msg = String.format(ILLEGAL_NULL_ASSIGNMENT_ERROR_FORMAT, datastoreEntity.getKind(), datastoreEntity.getKey(), propertyName, mmd.getFullFieldName()); throw new NullPointerException(msg); }
void storePKIdField(int fieldNumber, Object value) { AbstractMemberMetaData mmd = getMetaData(fieldNumber); if (!mmd.getType().equals(Long.class)) { throw new NucleusFatalUserException( "Field with \"" + DatastoreManager.PK_ID + "\" extension must be of type Long"); } Key key = null; if (value != null) { key = KeyFactory.createKey(datastoreEntity.getKind(), (Long) value); } storeKeyPK(key); }
/** * Constructor. * @param op ObjectProvider for the object being handled. * @param datastoreEntity Entity to represent this object * @param fieldNumbers Field numbers that will be processed (optional, null means all fields). */ protected DatastoreFieldManager(ObjectProvider op, Entity datastoreEntity, int[] fieldNumbers) { this.ec = op.getExecutionContext(); this.datastoreEntity = datastoreEntity; this.fieldManagerStateStack.addFirst(new FieldManagerState(op)); DatastoreManager storeManager = (DatastoreManager) ec.getStoreManager(); this.typeConversionUtils = storeManager.getTypeConversionUtils(); // Sanity check String expectedKind = EntityUtils.determineKind(op.getClassMetaData(), ec); if (!expectedKind.equals(datastoreEntity.getKind())) { throw new NucleusException( "ObjectProvider is for <" + expectedKind + "> but key is for <" + datastoreEntity.getKind() + ">. One way this can happen is if you attempt to fetch an object of one type using" + " a Key of a different type.").setFatal(); } }
/** * Convenience method to return an Entity with the same properties as the input Entity but with the * specified parent. * @param parentKey Key for the parent * @param originalEntity The original Entity * @return The new Entity */ public static Entity recreateEntityWithParent(Key parentKey, Entity originalEntity) { Entity entity = null; if (originalEntity.getKey().getName() != null) { entity = new Entity(originalEntity.getKind(), originalEntity.getKey().getName(), parentKey); } else { entity = new Entity(originalEntity.getKind(), parentKey); } EntityUtils.copyProperties(originalEntity, entity); return entity; }
@Override public void deleteAll(String namespace) { NamespaceManager.set(namespace); DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); Query query = new Query(); PreparedQuery pq = datastore.prepare(query); for (Entity entity : pq.asIterable()) { String kind = entity.getKind(); if (kind.startsWith("__") && !kind.contains("yawp")) { continue; } datastore.delete(entity.getKey()); } }
private void storePKNameField(int fieldNumber, String value) { // TODO(maxr) make sure the pk is an encoded string AbstractMemberMetaData mmd = getMetaData(fieldNumber); if (!mmd.getType().equals(String.class)) { throw new NucleusFatalUserException( "Field with \"" + DatastoreManager.PK_ID + "\" extension must be of type String"); } Key key = null; if (value != null) { key = KeyFactory.createKey(datastoreEntity.getParent(), datastoreEntity.getKind(), value); } storeKeyPK(key); }
public static JsonNode entityToJson(Entity entity) throws LeanException { ObjectNode json = getObjectMapper().createObjectNode(); json.put("_id", entity.getKey().getId()); json.putPOJO("_kind", entity.getKind()); json.putPOJO("_account", entity.getProperty("_account")); Map<String, Object> props = entity.getProperties(); for (Map.Entry<String, Object> prop : props.entrySet()) { addTypedNode(json, prop.getKey(), prop.getValue()); } return json; }
Iterator<Entity> iterator = entities.iterator(); Entity entity = iterator.next(); String kind = entity.getKind();
ColumnMetadata cm = statistics.getProperty(entity.getKind(), property); if (cm != null)
private void storePrimaryKey(int fieldNumber, Object value) { AbstractMemberMetaData mmd = getMetaData(fieldNumber); if (mmd.getType().equals(long.class)) { Key key = null; if (mmd.getValueStrategy() == IdentityStrategy.IDENTITY) { // Key being generated by datastore so put null key in key = null; } else { key = KeyFactory.createKey(datastoreEntity.getKind(), (Long) value); } storeKeyPK(key); } else if (mmd.getType().equals(Long.class)) { Key key = null; if (value != null) { // TODO This is actually against the JDO/JPA specs; having IDENTITY strategy means the DB will (always) choose key = KeyFactory.createKey(datastoreEntity.getKind(), (Long) value); } storeKeyPK(key); } else if (mmd.getType().equals(Key.class)) { Key key = (Key) value; if (key != null && key.getParent() != null && parentAlreadySet) { throw new NucleusFatalUserException(PARENT_ALREADY_SET); } storeKeyPK((Key) value); } else { throw exceptionForUnexpectedKeyType("Primary key", fieldNumber); } }
str.append(", "); NucleusLogger.DATASTORE_NATIVE.debug("Putting entity of kind " + entity.getKind() + " with key " + entity.getKey() + " as {" + str.toString() + "}");
if (entity.getKind().equals(Translation.LOCK_KIND)) { throw new ConcurrentModificationException( "Entity corresponding to [" + k + "] is processed under a transaction");