public DBRef keyToRef(Key key) { if (key == null) return null; if (key.getKindClass() == null && key.getKind() == null) throw new IllegalStateException("How can it be missing both?"); if (key.getKind() == null) key.setKind(getCollectionName(key.getKindClass())); return new DBRef(null, key.getKind(), toMongoObject(key.getId(), false)); }
@Override protected Key<T> convertItem(DBObject dbObj) { Key<T> key = new Key<T>(kind, dbObj.get(Mapper.ID_KEY)); key.setKindClass(this.clazz); return key; }
/** * * @param obj * @return */ @Override public boolean equals(Object obj) { if (obj == null) return false; if (!(obj instanceof Key<?>)) return false; return this.compareTo((Key<?>) obj) == 0; }
public String updateKind(Key key) { if (key.getKind() == null && key.getKindClass() == null) throw new IllegalStateException("Key is invalid! " + toString()); else if (key.getKind() == null) key.setKind(getMappedClass(key.getKindClass()).getCollectionName()); return key.getKind(); }
/** * @param key * @param ops * @param <T> * @return */ public <T> UpdateResults<T> update(Key<T> key, UpdateOperations<T> ops) { Class<T> clazz = (Class<T>) key.getKindClass(); if (clazz == null) clazz = (Class<T>) mapr.getClassFromKind(key.getKind()); return updateFirst(createQuery(clazz).disableValidation().filter(Mapper.ID_KEY, key.getId()), ops); }
/** * Converts from a List<Key> to their id values * * @param keys * @return The ids of the keys */ protected List<?> keysToIds(List<Key<T>> keys) { ArrayList ids = new ArrayList(keys.size() * 2); for (Key<T> key : keys) ids.add(key.getId()); return ids; }
public <T> Key<T> createKey(Class<T> clazz, Serializable id) { return new Key<T>(clazz, id); }
checkState(this); checkState(other); return cmp; cmp = compareNullable(this.kind, other.kind); if (cmp != 0) return cmp; cmp = compareNullable((Comparable<?>) this.id, (Comparable<?>) other.id); if (cmp != 0) return cmp;
/** * @param entityOrKey * @return */ public Key<?> exists(Object entityOrKey) { entityOrKey = ProxyHelper.unwrap(entityOrKey); Key<?> key = mapr.getKey(entityOrKey); Object id = key.getId(); if (id == null) throw new MappingException("Could not get id for " + entityOrKey.getClass().getName()); String collName = key.getKind(); if (collName == null) collName = getCollection(key.getKindClass()).getName(); return find(collName, key.getKindClass()).filter(Mapper.ID_KEY, key.getId()).getKey(); }
/** * @param clazz * @param key * @param <T> * @return */ public <T> T getByKey(Class<T> clazz, Key<T> key) { String kind = mapr.getCollectionName(clazz); String keyKind = mapr.updateKind(key); if (!kind.equals(keyKind)) throw new RuntimeException("collection names don't match for key and class: " + kind + " != " + keyKind); return get(clazz, key.getId()); }
public <T> Key<T> refToKey(DBRef ref) { if (ref == null) return null; return new Key<T>(ref.getRef(), ref.getId()); }
mapr.updateKind(key); if (clazzKind != null && !key.getKind().equals(clazzKind)) throw new IllegalArgumentException("Types are not equal (" + clazz + "!=" + key.getKindClass() + ") for key and method parameter clazz"); if (kindMap.containsKey(key.getKind())) kindMap.get(key.getKind()).add(key); else kindMap.put(key.getKind(), new ArrayList<Key>(Collections.singletonList((Key) key))); Class<T> kindClass = clazz == null ? kindKeys.get(0).getKindClass() : clazz; for (Key key : kindKeys) { objIds.add(key.getId());
/** * call postSaveOperations and returns Key for entity. */ protected <T> Key<T> postSaveGetKey(T entity, DBObject dbObj, DBCollection dbColl, Map<Object, DBObject> involvedObjects) { if (dbObj.get(Mapper.ID_KEY) == null) throw new MappingException("Missing _id after save!"); postSaveOperations(entity, dbObj, involvedObjects); Key<T> key = new Key<T>(dbColl.getName(), mapr.getId(entity)); key.setKindClass((Class<? extends T>) entity.getClass()); return key; }
public <T> Key<T> getKey(T entity) { if (entity == null) throw new MappingException("getKey has been passed a null entity"); if (entity instanceof ProxiedEntityReference) { return getKey((ProxiedEntityReference) entity); } entity = ProxyHelper.unwrap(entity); if (entity instanceof Key) return (Key<T>) entity; Object id = getId(entity); if (id == null) throw new MappingException("Could not get id for " + entity.getClass().getName()); return new Key<T>((Class<T>) entity.getClass(), id); }
private Key<?> getKey(final Object entity, Mapper mapr) { try { if (entity instanceof ProxiedEntityReference) { ProxiedEntityReference proxy = (ProxiedEntityReference) entity; return proxy.__getKey(); } MappedClass mappedClass = mapr.getMappedClass(entity); Object id = mappedClass.getIdField().get(entity); if (id == null) throw new MappingException("@Id field cannot be null!"); Key key = new Key(mappedClass.getCollectionName(), id); return key; } catch (IllegalAccessException iae) { throw new RuntimeException(iae); } }
public <T> Key<T> createKey(Class<T> clazz, 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. BSONEncoder enc = new BasicBSONEncoder(); return new Key<T>(clazz, enc.encode(toDBObject(id))); }