@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); } }
/** * 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); }
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()); }
Object keyToId(final Key key) { return key == null ? null : key.getId(); }
/** * Updates the collection value on a Key with the mapped value on the Key's type Class * * @param key the Key to update * @return the collection name on the Key */ public String updateCollection(final Key key) { if (key.getCollection() == null && key.getType() == null) { throw new IllegalStateException("Key is invalid! " + toString()); } else if (key.getCollection() == null) { key.setCollection(getMappedClass(key.getType()).getCollectionName()); } return key.getCollection(); }
if (kindMap.containsKey(key.getCollection())) { kindMap.get(key.getCollection()).add(key); } else { kindMap.put(key.getCollection(), new ArrayList<Key>(singletonList((Key) key))); objIds.add(key.getId());
@Override public int compareTo(final Key<T> other) { checkState(this); checkState(other); int cmp; // First collection if (other.type != null && type != null) { cmp = type.getName().compareTo(other.type.getName()); if (cmp != 0) { return cmp; } } cmp = compareNullable(collection, other.collection); if (cmp != 0) { return cmp; } try { cmp = compareNullable((Comparable<?>) id, (Comparable<?>) other.id); if (cmp != 0) { return cmp; } } catch (Exception e) { // Not a comparable, use equals and String.compareTo(). cmp = id.equals(other.id) ? 0 : 1; if (cmp != 0) { return id.toString().compareTo(other.id.toString()); } } return 0; }
@Override protected void validate(final Class<?> type, final Object value, final List<ValidationFailure> validationFailures) { if (!type.equals(((Key) value).getType()) && !type.equals(Key.class)) { validationFailures.add(new ValidationFailure(format("When value is a Key, the type needs to be the right kind of class. " + "Type was %s and value was '%s'", type, value) )); } } }
@Override @SuppressWarnings("unchecked") public boolean equals(final Object obj) { return obj != null && obj instanceof Key<?> && compareTo((Key<T>) obj) == 0; }
@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 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; }
collection = datastore.getCollection(key.getType()); id = ref; } else {
/** * 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); }
public Object getId(Object object) { if (object == null) return ""; return datastore.getKey(object).getId(); }
<T> Key<T> manualRefToKey(final String collection, final Object id) { return id == null ? null : new Key<T>((Class<? extends T>) getClassFromCollection(collection), collection, id); }
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; }
<T> Key<T> createKey(final Class<T> clazz, final Serializable id) { return new Key<T>(clazz, getCollectionName(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); }
public Project findOne(ObjectId id, QueryParams requestParams) { return datastore.getByKey(Project.class, new Key<>(Project.class, id)); }
@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); }