@Override public <X> X find(final Class<X> entityClass, final Object primaryKey) { return find(entityClass, primaryKey, Collections.<String, Object>emptyMap()); }
@Override public <X> X find(final Class<X> entityClass, final Object primaryKey, final Map<String, Object> properties) { final Key<X, ?> key = Key.get(this, entityClass, primaryKey); return find(key, properties); }
protected <Y> void parseSingularJsonReference( X targetEntity, ErraiSingularAttribute<? super X, Y> attr, JSONValue attrJsonValue, ErraiEntityManager eem) { if (attrJsonValue == null || attrJsonValue.isNull() != null) return; Key<Y, ?> key = (Key<Y, ?>) Key.fromJsonObject(eem, attrJsonValue.isObject(), true); logger.trace(" looking for " + key); Y value = eem.find(key, Collections.<String,Object>emptyMap()); attr.set(targetEntity, value); }
@Override public void visit(String key, String value) { Key<?, ?> k = parseNamespacedKey(em, key, false); if (k == null) return; logger.trace("getAll(): considering " + value); if (type.isSuperclassOf(k.getEntityType())) { logger.trace(" --> correct type"); JSONObject candidate = JSONParser.parseStrict(value).isObject(); Assert.notNull(candidate); if (matcher.matches(candidate)) { @SuppressWarnings("unchecked") Key<X, ?> typedKey = (Key<X, ?>) k; // Unfortunately, this throws away a lot of work we've already done (getting the entity type, // creating the key, doing a backend.get(), parsing the JSON value, ...) // it would be nice to avoid this, but we have to go back to the entity manager in case the // thing we want is in the persistence context. entities.add((X) em.find(k.getEntityType().getJavaType(), typedKey.getId())); } else { logger.trace(" --> but not a match"); } } else { logger.trace(" --> wrong type"); } } });
private static <X, Y> void copySingularAssociation( ErraiEntityManager em, ErraiAttribute<X, Y> attr, X targetEntity, X sourceEntity) { ErraiIdentifiableType<Y> relatedEntityType = em.getMetamodel().entity(attr.getJavaType()); Y oldRelatedEntity = attr.get(sourceEntity); Y resolvedEntity; if (oldRelatedEntity == null) { resolvedEntity = null; } else { Key<Y, ?> key = em.keyFor(oldRelatedEntity); resolvedEntity = em.find(key, Collections.<String,Object>emptyMap()); if (resolvedEntity == null) { resolvedEntity = relatedEntityType.newInstance(); } } attr.set(targetEntity, resolvedEntity); }
protected <C, E> void parsePluralJsonReference( X targetEntity, ErraiPluralAttribute<? super X, C, E> attr, JSONArray attrJsonValues, ErraiEntityManager eem) { if (attrJsonValues == null || attrJsonValues.isNull() != null) return; Class<E> attributeElementType = attr.getElementType().getJavaType(); ErraiIdentifiableType<E> attrEntityType = eem.getMetamodel().entity(attributeElementType); // FIXME this is broken for Map attributes // TODO when we support Map attributes, we should get the attribute with getCollection()/getMap() to fix this warning Collection<E> collection = (Collection<E>) attr.createEmptyCollection(); for (int i = 0; i < attrJsonValues.size(); i++) { Key<E, ?> key = (Key<E, ?>) Key.fromJsonObject(eem, attrJsonValues.get(i).isObject(), true); logger.trace(" looking for " + key); E value = eem.getPartiallyConstructedEntity(key); if (value == null) { value = eem.find(key, Collections.<String,Object>emptyMap()); } collection.add(value); } attr.set(targetEntity, (C) collection); }
private static <X, C, E> void copyPluralAssociation( ErraiEntityManager em, ErraiPluralAttribute<X, C, E> attr, X targetEntity, X sourceEntity) { C oldCollection = attr.get(sourceEntity); C newCollection; if (oldCollection == null) { newCollection = null; } else { newCollection = attr.createEmptyCollection(); ErraiIdentifiableType<E> elemType = em.getMetamodel().entity(attr.getElementType().getJavaType()); // TODO support map-valued plural attributes for (Object oldEntry : (Collection<?>) oldCollection) { Key<Object, ?> key = em.keyFor(oldEntry); Object resolvedEntry = em.find(key, Collections.<String,Object>emptyMap()); if (resolvedEntry == null) { resolvedEntry = elemType.newInstance(); } ((Collection) newCollection).add(resolvedEntry); } } attr.set(targetEntity, newCollection); }
/** * Changes the ID of an existing entity in desiredStateEm. * * @param entityType The metamodel type for the entity whose ID is to be changed * @param oldId The ID that the entity currently has. * @param newId The ID that the entity will have when this method returns. */ private <E> void changeId(ErraiIdentifiableType<E> entityType, Object oldId, Object newId) { // XXX this routine is probably better handled internally by the ErraiEntityManager // TODO what about related entities that refer to this one? (needs tests) E entity = desiredStateEm.find(entityType.getJavaType(), oldId); desiredStateEm.remove(entity); desiredStateEm.flush(); desiredStateEm.detach(entity); entityType.getId(Object.class).set(entity, newId); desiredStateEm.persist(entity); }
E resolved = expectedStateEm.find(expectedStateEm.keyFor(requestedNew), Collections.<String,Object>emptyMap()); expectedStateEm.remove(resolved); resolved = desiredStateEm.find(desiredStateEm.keyFor(requestedNew), Collections.<String,Object>emptyMap()); desiredStateEm.remove(resolved); DeleteResponse<E> dr = (DeleteResponse<E>) response; System.out.println(" -> Delete " + dr.getEntity()); E resolved = expectedStateEm.find(expectedStateEm.keyFor(dr.getEntity()), Collections.<String,Object>emptyMap()); expectedStateEm.remove(resolved); expectedStateEm.detach(resolved); resolved = desiredStateEm.find(desiredStateEm.keyFor(dr.getEntity()), Collections.<String,Object>emptyMap()); if (resolved != null) { desiredStateEm.remove(resolved); E inTheWay = desiredStateEm.find(conflictingKey, Collections.<String,Object>emptyMap()); if (inTheWay != null) { ErraiIdentifiableType<E> entityType = desiredStateEm.getMetamodel().entity(entityClass);
case DETACHED: boolean sendUpdateEvent = true; // if false, send persist event X mergeTarget = find(key, Collections.<String,Object>emptyMap()); if (mergeTarget == null) { sendUpdateEvent = false;