@Override protected DBRef asReferenceKey(Class<?> entity, Object id) { String collection = morphia.getMapper().getCollectionName(entity); Key<?> key = new Key<Object>(entity, collection, id); return morphia.getMapper().keyToDBRef(key); } }
/** * Creates a Key for a type and an ID value * * @param type the Class of the entity * @param id the ID value * @param <T> the type of the entity * @return the Key */ public <T> Key<T> manualRefToKey(final Class<T> type, final Object id) { return id == null ? null : new Key<T>(type, getCollectionName(type), id); }
<T> Key<T> manualRefToKey(final String collection, final Object id) { return id == null ? null : new Key<T>((Class<? extends T>) getClassFromCollection(collection), collection, id); }
<T> Key<T> createKey(final Class<T> clazz, final Serializable id) { return new Key<T>(clazz, getCollectionName(clazz), id); }
public Project findOne(ObjectId id, QueryParams requestParams) { return datastore.getByKey(Project.class, new Key<>(Project.class, id)); }
public Task findOne(ObjectId id, QueryParams requestParams) { return datastore.getByKey(Task.class, new Key<>(Task.class, id)); }
private Key extractKey(final Mapper mapper, final MappedField mf, final DBObject dbObject) { if (mapper == null || mf == null || dbObject == null || (dbObject instanceof BasicDBList)) return null; final ObjectId objectId = (ObjectId) dbObject.get(mapper.ID_KEY); //HACKY GET RID OF SOON final Object obj = mapper.getOptions().getObjectFactory().createInstance(mapper, mf, dbObject); if (objectId == null || obj == null) return null; return new Key(obj.getClass(), objectId); }
/** * Gets the Key for an entity and a specific collection * * @param entity the entity to process * @param collection the collection to use in the Key rather than the mapped collection as defined on the entity's class * @param <T> the type of the entity * @return the Key */ public <T> Key<T> getKey(final T entity, final String collection) { T unwrapped = entity; if (unwrapped instanceof ProxiedEntityReference) { final ProxiedEntityReference proxy = (ProxiedEntityReference) unwrapped; return (Key<T>) proxy.__getKey(); } unwrapped = ProxyHelper.unwrap(unwrapped); if (unwrapped instanceof Key) { return (Key<T>) unwrapped; } final Object id = getId(unwrapped); final Class<T> aClass = (Class<T>) unwrapped.getClass(); return id == null ? null : new Key<T>(aClass, collection, id); }
@SuppressWarnings("unchecked") private <T> List<Key<T>> postSaveOperations(final Iterable<T> entities, final Map<Object, DBObject> involvedObjects, final DBCollection collection, final boolean fetchKeys) { List<Key<T>> keys = new ArrayList<Key<T>>(); for (final T entity : entities) { final DBObject dbObj = involvedObjects.remove(entity); if (fetchKeys) { if (dbObj.get(Mapper.ID_KEY) == null) { throw new MappingException(format("Missing _id after save on %s", entity.getClass().getName())); } mapper.updateKeyAndVersionInfo(this, dbObj, createCache(), entity); keys.add(new Key<T>((Class<? extends T>) entity.getClass(), collection.getName(), mapper.getId(entity))); } mapper.getMappedClass(entity).callLifecycleMethods(PostPersist.class, entity, dbObj, mapper); } for (Entry<Object, DBObject> entry : involvedObjects.entrySet()) { final Object key = entry.getKey(); mapper.getMappedClass(key).callLifecycleMethods(PostPersist.class, key, entry.getValue(), mapper); } return keys; }
/** * Gets the Key for an entity * * @param entity the entity to process * @param <T> the type of the entity * @return the Key */ public <T> Key<T> getKey(final T entity) { T unwrapped = entity; if (unwrapped instanceof ProxiedEntityReference) { final ProxiedEntityReference proxy = (ProxiedEntityReference) unwrapped; return (Key<T>) proxy.__getKey(); } unwrapped = ProxyHelper.unwrap(unwrapped); if (unwrapped instanceof Key) { return (Key<T>) unwrapped; } final Object id = getId(unwrapped); final Class<T> aClass = (Class<T>) unwrapped.getClass(); return id == null ? null : new Key<T>(aClass, getCollectionName(aClass), id); }
private Key<?> getKey(final Object entity, final Mapper mapper) { try { if (entity instanceof ProxiedEntityReference) { final ProxiedEntityReference proxy = (ProxiedEntityReference) entity; return proxy.__getKey(); } final MappedClass mappedClass = mapper.getMappedClass(entity); Object id = mappedClass.getIdField().get(entity); if (id == null) { throw new MappingException("@Id field cannot be null!"); } return new Key(mappedClass.getClazz(), mappedClass.getCollectionName(), id); } catch (IllegalAccessException iae) { throw new RuntimeException(iae); } }
/** * Converts a DBRef to a Key * * @param ref the DBRef to convert * @param <T> the type of the referenced entity * @return the Key */ public <T> Key<T> refToKey(final DBRef ref) { return ref == null ? null : new Key<T>((Class<? extends T>) getClassFromCollection(ref.getCollectionName()), ref.getCollectionName(), ref.getId()); }
<T> Key<T> createKey(final Class<T> clazz, final Object id) { if (id instanceof Serializable) { return createKey(clazz, (Serializable) id); } //TODO: cache the encoders, maybe use the pool version of the buffer that the driver does. final BSONEncoder enc = new BasicBSONEncoder(); return new Key<T>(clazz, getCollectionName(clazz), enc.encode(toDBObject(id))); }
@Override public Object decode(final Class targetClass, final Object o, final MappedField optionalExtraInfo) { if (o == null) { return null; } if (!(o instanceof DBRef)) { throw new ConverterException(String.format("cannot convert %s to Key because it isn't a DBRef", o.toString())); } DBRef ref = (DBRef) o; MappedField actualType = getActualType(optionalExtraInfo); final Class<?> keyType = actualType != null ? actualType.getConcreteType() : getMapper().getClassFromCollection(ref.getCollectionName()); final Key<?> key = new Key<Object>(keyType, ref.getCollectionName(), ref.getId()); return key; }
final Key<T> key = new Key(entity.getClass(), getCollectionName(entity.getClass()), dbObject.get(ID_KEY)); final T cachedInstance = cache.getEntity(key); if (cachedInstance != null) { final Key key = new Key(entity.getClass(), getCollectionName(entity.getClass()), updated.get(ID_KEY)); cache.putEntity(key, entity);
@Override @SuppressWarnings("unchecked") protected Key<T> convertItem(final DBObject dbObj) { Object id = dbObj.get(Mapper.ID_KEY); if (id instanceof DBObject) { Class type = getMapper().getMappedClass(getClazz()).getMappedIdField().getType(); id = getMapper().fromDBObject(getDatastore(), type, (DBObject) id, getMapper().createEntityCache()); } return new Key<T>(getClazz(), getCollection(), id); } }