private Key<X, ?> keyFromJson(JSONValue json) { JSONValue keyJson = json.isObject().get(id.getName()); Object idValue = JsonUtil.basicValueFromJson(keyJson, id.getJavaType()); return new Key<X, Object>(this, idValue); }
/** * Returns a JSONObject representation of this key which can be turned back * into a Key instance via * {@link Key#fromJsonObject(ErraiEntityManager, JSONObject, boolean)}. * * @see #toJson() * @return a JSON object that represents this key. Never null. */ public JSONObject toJsonObject() { JSONObject keyJson = new JSONObject(); keyJson.put("entityType", new JSONString(entityType.getJavaType().getName())); keyJson.put("id", JsonUtil.basicValueToJson(id)); return keyJson; }
@Override public <X> boolean isModified(Key<X, ?> key, X value) { ErraiManagedType<X> entityType = key.getEntityType(); String keyJson = namespace + key.toJson(); JSONValue newValueJson = entityType.toJson(em, value); JSONValue oldValueJson = JSONParser.parseStrict(LocalStorage.get(keyJson)); boolean modified = !JsonUtil.equals(newValueJson, oldValueJson); if (modified) { logger.trace("Detected modified entity " + key); logger.trace(" Old: " + oldValueJson); logger.trace(" New: " + newValueJson); } return modified; }
protected <Y> void parseInlineJson(X targetEntity, ErraiAttribute<? super X, Y> attr, JSONValue attrJsonValue, ErraiEntityManager eem) { Class<Y> attributeType = attr.getJavaType(); Y value; // FIXME this should search all managed types, or maybe all embeddables. not just entities. if (eem.getMetamodel().getEntities().contains(attributeType)) { ErraiIdentifiableType<Y> attrEntityType = eem.getMetamodel().entity(attributeType); value = attrEntityType.fromJson(eem, attrJsonValue); } else { value = JsonUtil.basicValueFromJson(attrJsonValue, attributeType); } attr.set(targetEntity, value); }
/** * Returns an inline JSON representation of the value of the given attribute * of the given entity instance. * * @param targetEntity * The instance of the entity to retrieve the attribute value from. * Not null. * @param attr * The attribute to read from {@code targetEntity}. Not null. * @param eem * The ErraiEntityManager that owns the entity. Not null. * @return a JSONValue that represents the requested attribute value of the * given entity. Never null, although it could be JSONNull. */ private <Y> JSONValue makeInlineJson(X targetEntity, ErraiAttribute<? super X, Y> attr, ErraiEntityManager eem) { Class<Y> attributeType = attr.getJavaType(); Y attrValue = attr.get(Assert.notNull(targetEntity)); // FIXME this should search all managed types, or maybe all embeddables. not just entities. // TODO it would be better to code-generate an Attribute.asJson() method than to do this at runtime if (eem.getMetamodel().getEntities().contains(attributeType)) { ErraiIdentifiableType<Y> attrEntityType = eem.getMetamodel().entity(attributeType); return attrEntityType.toJson(eem, attrValue); } return JsonUtil.basicValueToJson(attrValue); }
/** * Returns a Key instance based on the given JSON object. * * @param em * The entity manager that can be used to look up the entity type * corresponding with the key. * @param key * The properties of the key to create. * @param failIfNotFound * If true, and the entity type given in {@code key} is not known to * {@code em}, an IllegalArgumentException will be thrown. * @return An instance of Key that corresponds with the entity type and ID of * the given JSON object. */ public static Key<?, ?> fromJsonObject(ErraiEntityManager em, JSONObject key, boolean failIfNotFound) { String entityClassName = key.get("entityType").isString().stringValue(); ErraiIdentifiableType<Object> et = em.getMetamodel().entity(entityClassName, failIfNotFound); if (et == null) { return null; } ErraiSingularAttribute<?, Object> idAttr = et.getId(Object.class); Object id = JsonUtil.basicValueFromJson(key.get("id"), idAttr.getJavaType()); return new Key<Object, Object>(et, id); } }