@Deprecated protected Object getId(final Object entity) { return mapper.getId(entity); }
@Override @SuppressWarnings("unchecked") public <T> T get(final T entity) { final T unwrapped = ProxyHelper.unwrap(entity); final Object id = mapper.getId(unwrapped); if (id == null) { throw new MappingException("Could not get id for " + unwrapped.getClass().getName()); } return (T) get(unwrapped.getClass(), id); }
@Override public <T> DBRef createRef(final T entity) { final T wrapped = ProxyHelper.unwrap(entity); final Object id = mapper.getId(wrapped); if (id == null) { throw new MappingException("Could not get id for " + wrapped.getClass().getName()); } return createRef(wrapped.getClass(), id); }
/** * Deletes the given entity (by @Id), with the WriteConcern * * @param entity the entity to delete * @param options the options to use when deleting * @return results of the delete */ @Override public <T> WriteResult delete(final T entity, final DeleteOptions options) { final T wrapped = ProxyHelper.unwrap(entity); if (wrapped instanceof Class<?>) { throw new MappingException("Did you mean to delete all documents? -- delete(ds.createQuery(???.class))"); } try { return delete(wrapped.getClass(), mapper.getId(wrapped), options); } catch (Exception e) { throw new RuntimeException(e); } }
@Override public ID_TYPE getId(MODEL_TYPE entity) { if (entity instanceof MorphiaModel) { return (ID_TYPE) ((MorphiaModel) entity).getId(); } else if (entity instanceof Model) { return (ID_TYPE) ((Model) entity)._id(); } else { MorphiaService service = MorphiaService.findByModelClass(entity.getClass()); return null == service ? null : (ID_TYPE) service.mapper().getId(entity); } }
@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 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); }
/** * 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); }
final DBObject newStub = createStub(mapper, fieldValue, mapper.getId(fieldValue)); involvedObjects.put(fieldValue, newStub); cachedStub = newStub;
@Override @SuppressWarnings("unchecked") public <T> UpdateResults update(final T entity, final UpdateOperations<T> operations) { if (entity instanceof Query) { return update((Query<T>) entity, operations); } final MappedClass mc = mapper.getMappedClass(entity); Query<?> query = createQuery(mapper.getMappedClass(entity).getClazz()) .disableValidation() .filter(Mapper.ID_KEY, mapper.getId(entity)); if (!mc.getFieldsAnnotatedWith(Version.class).isEmpty()) { final MappedField field = mc.getFieldsAnnotatedWith(Version.class).get(0); query.field(field.getNameToStore()).equal(field.getFieldValue(entity)); } return update((Query<T>) query, operations); }
final Key<T> key = mapper.getKey(unwrapped); unwrapped = ProxyHelper.unwrap(unwrapped); final Object id = mapper.getId(unwrapped); if (id == null) { throw new MappingException("Could not get id for " + unwrapped.getClass().getName());