Object keyToId(final Key key) { return key == null ? null : key.getId(); }
/** * Converts from a List<Key> to their id values */ protected List<?> keysToIds(final List<Key<T>> keys) { final List<Object> ids = new ArrayList<Object>(keys.size() * 2); for (final Key<T> key : keys) { ids.add(key.getId()); } return ids; }
public Object getId(Object object) { if (object == null) return ""; return datastore.getKey(object).getId(); }
@Override public <T> T getByKey(final Class<T> clazz, final Key<T> key) { final String collectionName = mapper.getCollectionName(clazz); final String keyCollection = mapper.updateCollection(key); if (!collectionName.equals(keyCollection)) { throw new RuntimeException("collection names don't match for key and class: " + collectionName + " != " + keyCollection); } Object id = key.getId(); if (id instanceof DBObject) { ((DBObject) id).removeField(Mapper.CLASS_NAME_FIELDNAME); } return get(clazz, id); }
@Override public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable { if ("finalize".equals(method.getName()) && args != null && args.length == 0) { return null; } /* * If the method being invoked is annotated with @IdGetter and the delegate reference is an EntityObjectReference, * return the id of the EntityObjectReference's key. This allows us to return the referenced entity's id without * fetching the entity from the datastore. */ if (method.getAnnotation(IdGetter.class) != null) { ObjectReference<Object> delegateReference = getDelegateReference(); if (delegateReference instanceof EntityObjectReference) { EntityObjectReference entityObjectReference = (EntityObjectReference) delegateReference; return entityObjectReference.__getKey().getId(); } } return super.invoke(proxy, method, args); }
private Object getDBRefs(final MappedField field, final Iterable value) { final List<Object> refs = new ArrayList<Object>(); Reference annotation = field.getAnnotation(Reference.class); boolean idOnly = annotation != null && annotation.idOnly(); for (final Object o : value) { Key<?> key = (o instanceof Key) ? (Key<?>) o : getKey(o); refs.add(idOnly ? key.getId() : keyToDBRef(key)); } return refs; }
private Query<?> buildExistsQuery(final Object entityOrKey) { final Object unwrapped = ProxyHelper.unwrap(entityOrKey); final Key<?> key = mapper.getKey(unwrapped); final Object id = key.getId(); if (id == null) { throw new MappingException("Could not get id for " + unwrapped.getClass().getName()); } return find(key.getCollection(), key.getType()).filter(Mapper.ID_KEY, key.getId()); }
objIds.add(key.getId());
@SuppressWarnings("unchecked") @Override public void reloadObject(DbConnection con, String registryName, Object obj) throws MException { Object id = datastore.getKey(obj).getId(); Object clone = datastore.get(obj.getClass(), id); try { PojoModel model = getModelFor(obj.getClass()); for ( PojoAttribute<Object> f : model) { Object v = f.get(clone); f.set(obj, v); } } catch (IOException e) { throw new MException(e); } }
@Override public boolean objectChanged(Object obj) throws MException { Object id = datastore.getKey(obj).getId(); Object clone = datastore.get(obj.getClass(), id); try { PojoModel model = getModelFor(obj.getClass()); for ( PojoAttribute<?> f : model) { Object v1 = f.get(obj); Object v2 = f.get(clone); if (!MSystem.equals(v1, v2)) return true; } } catch (IOException e) { throw new MException(e); } return false; }
@Override @SuppressWarnings("unchecked") public <T> UpdateResults update(final Key<T> key, final UpdateOperations<T> operations) { Class<T> clazz = (Class<T>) key.getType(); if (clazz == null) { clazz = (Class<T>) mapper.getClassFromCollection(key.getCollection()); } return updateFirst(createQuery(clazz).disableValidation().filter(Mapper.ID_KEY, key.getId()), operations); }
/** * Converts a Key to a DBRef * * @param key the Key to convert * @return the DBRef */ public DBRef keyToDBRef(final Key key) { if (key == null) { return null; } if (key.getType() == null && key.getCollection() == null) { throw new IllegalStateException("How can it be missing both?"); } if (key.getCollection() == null) { key.setCollection(getCollectionName(key.getType())); } Object id = key.getId(); if (isMapped(id.getClass())) { id = toMongoObject(id, true); } return new DBRef(key.getCollection(), id); }