List of usage examples for com.mongodb DBObject containsField
boolean containsField(String s);
From source file:org.exoplatform.chat.services.mongodb.UserServiceImpl.java
License:Open Source License
public void removeTeamUsers(String teamRoomId, List<String> users) { DBCollection coll = db().getCollection(M_USERS_COLLECTION); for (String user : users) { LOG.info("Team Remove : " + user); BasicDBObject query = new BasicDBObject(); query.put("user", user); DBCursor cursor = coll.find(query); if (cursor.hasNext()) { DBObject doc = cursor.next(); if (doc.containsField("teams")) { List<String> teams = (List<String>) doc.get("teams"); if (teams.contains(teamRoomId)) { teams.remove(teamRoomId); doc.put("teams", teams); coll.save(doc, WriteConcern.SAFE); }/*from w w w . j a va 2 s .c o m*/ } } } }
From source file:org.fastmongo.odm.dbobject.mapping.core.DbObjectToDomainConverter.java
License:Apache License
/** * Converts mongo value for the given object field into the corresponding field value. * * @param field the domain object field. * @param obj the domain object.//from w ww . jav a 2 s . c o m * @param db the document which represents the domain object. * @param context the context for this operation, bounded to the root document. */ @SuppressWarnings("unchecked") private void processField(Field field, Object obj, DBObject db, Context context) { Object value = db.get(field.getName()); if (value == null) { return; } Class<?> dbValueType = value.getClass(); Class<?> fieldType = field.getType(); if (isCollection(fieldType)) { if (Set.class.isAssignableFrom(fieldType)) { processSet(field, obj, (List<Object>) value, context); } else { processList(field, obj, (List<Object>) value, context); } } else if (isMap(fieldType)) { Map<String, Object> map = new LinkedHashMap<>(); setFieldValue(field, obj, map); fillMap(map, (DBObject) value, ConverterHelper.getFieldGenericTypes(field)[1], context); } else if (!isMap(dbValueType)) { setFieldValue(field, obj, toSimpleType(fieldType, value)); } else { DBObject fieldDbValue = (DBObject) value; if (fieldDbValue.containsField(LINK_KEY)) { processLink(field, obj, fieldDbValue, context); } else { Object fieldValue = newInstance( classNameResolver.getClassName(field.getGenericType(), fieldDbValue, classPrefix)); setFieldValue(field, obj, fieldValue); fillObject(fieldValue, fieldDbValue, context); } } }
From source file:org.fastmongo.odm.dbobject.mapping.support.classname.ReflectionClassNameResolver.java
License:Apache License
@Override @SuppressWarnings("unchecked") public String getClassName(Type type, DBObject dbObject, String classPrefix) { if (dbObject.containsField(CLASS_KEY)) { return restoreClassName((String) dbObject.get(CLASS_KEY), classPrefix); }/* w w w . ja v a 2 s . c om*/ Class clazz = (Class) type; String className = IMPLEMENTATION_BY_INTERFACE.get(clazz); if (className == null) { Set<Class<?>> subTypes = reflections.getSubTypesOf(clazz); if (subTypes.size() == 1) { className = subTypes.iterator().next().getName(); } else { className = NO_IMPLEMENTATION; } IMPLEMENTATION_BY_INTERFACE.put(clazz, className); } if (!NO_IMPLEMENTATION.equals(className)) { return className; } ResolveStrategy resolveStrategy = findSuitableStrategy(clazz); if (resolveStrategy != null) { className = resolveStrategy.getClassName(clazz, dbObject); className = className.startsWith(classPrefix) ? className : restoreClassName(className, classPrefix); } if (StringUtils.isEmpty(className)) { if (!clazz.isInterface()) { return clazz.getName(); } throw new IllegalArgumentException(String.format( "Can't find appropriate class name for '%s' and DBObject('%s') with classNamePrefix = %s", type, StringUtils.abbreviateMiddle(dbObject.toString(), "<...>", 100), classPrefix)); } return className; }
From source file:org.ff4j.mongo.mapper.MongoPropertyMapper.java
License:Apache License
/** * Map a property./*www. j a v a2s . co m*/ * * @param dbObject * db object * @return * list of property */ public Property<?> fromStore(DBObject dbObject) { PropertyJsonBean pf = new PropertyJsonBean(); pf.setName((String) dbObject.get(PROPERTY_NAME)); pf.setDescription((String) dbObject.get(PROPERTY_DESCRIPTION)); pf.setType((String) dbObject.get(PROPERTY_TYPE)); pf.setValue((String) dbObject.get(PROPERTY_VALUE)); if (dbObject.containsField(PROPERTY_FIXEDVALUES)) { BasicDBList dbList = (BasicDBList) dbObject.get(PROPERTY_FIXEDVALUES); if (dbList != null) { for (Object item : dbList) { pf.addFixedValue((String) item); } } } return pf.asProperty(); }
From source file:org.ff4j.store.mongodb.FeatureDBObjectMapper.java
License:Apache License
/** * Map from {@link DBObject} to authorizations.. * //from ww w . j a va 2 s . co m * @param dbObject * target * @return */ @SuppressWarnings("rawtypes") private Set<String> mapAuthorization(DBObject dbObject) { Set<String> authorisation = new HashSet<String>(); if (dbObject.containsField(ROLES)) { for (Object role : (Iterable) dbObject.get(ROLES)) { authorisation.add(role.toString()); } } return authorisation; }
From source file:org.ff4j.store.mongodb.FeatureDocumentMapper.java
License:Apache License
/** * Map a property./*from w ww. j ava 2s . c om*/ * * @param dbObject * db object * @return * list of property */ public Property<?> mapProperty(DBObject dbObject) { PropertyJsonBean pf = new PropertyJsonBean(); pf.setName((String) dbObject.get(PROPERTY_NAME)); pf.setDescription((String) dbObject.get(PROPERTY_DESCRIPTION)); pf.setType((String) dbObject.get(PROPERTY_TYPE)); pf.setValue((String) dbObject.get(PROPERTY_VALUE)); if (dbObject.containsField(PROPERTY_FIXEDVALUES)) { BasicDBList dbList = (BasicDBList) dbObject.get(PROPERTY_FIXEDVALUES); if (dbList != null) { for (Object item : dbList) { pf.addFixedValue((String) item); } } } return pf.asProperty(); }
From source file:org.forgerock.openidm.repo.mongodb.impl.DocumentUtil.java
License:Open Source License
/** * MongoDB can't store data which has key starting with "$". * So, When key starts with "$", convert "$" to "_$". * //from w ww . j av a 2 s . com * @param obj * @return */ public static DBObject normalizeForWrite(DBObject obj) { String regex = "\"(\\$[^\"]+)\""; String replacement = "\"_$1\""; for (int i = 0; i < KEYS_OF_STRING.length; i++) { if (obj.containsField(KEYS_OF_STRING[i])) { JsonValue jv = new JsonValue(obj.get(KEYS_OF_STRING[i])); String v = jv.toString().replaceAll(regex, replacement); obj.put(KEYS_OF_STRING[i], JSON.parse(v)); } } return obj; }
From source file:org.forgerock.openidm.repo.mongodb.impl.DocumentUtil.java
License:Open Source License
/** * unescape "_$" to "$"./*from ww w .j av a 2 s . c o m*/ * And Json parameter convert to Json formatted String paramter, * Because, OpenIDM expected to String of Json format, not native Json. * * @param obj * @return */ public static DBObject normalizeForRead(DBObject obj) { String regex = "\"([^\"]+)\""; String replacement = "\\\"$1\\\""; for (int i = 0; i < KEYS_OF_STRING.length; i++) { if (obj.containsField(KEYS_OF_STRING[i])) { JsonValue jv = new JsonValue(obj.get(KEYS_OF_STRING[i])); String v = jv.toString().replaceAll(regex, replacement); v = v.replaceAll(" ", ""); v = v.replaceFirst("\"_\\$", "\"\\$"); obj.put(KEYS_OF_STRING[i], v); } } return obj; }
From source file:org.fornax.cartridges.sculptor.framework.accessimpl.mongodb.MongoDbAccessBaseWithException.java
License:Apache License
protected void checkLastError() { DBObject lastError = getDBCollection().getDB().getLastError(); if (lastError.containsField("err") && lastError.get("err") != null) { throw new DatabaseAccessException(lastError.get("err").toString()); }//from w w w. ja v a 2 s . co m }
From source file:org.fornax.cartridges.sculptor.framework.accessimpl.mongodb.MongoDbDeleteAccessImpl.java
License:Apache License
protected void performRemove(T obj) { DBObject dbObj = getDataMapper().toData(obj); if (dbObj.containsField("_id")) { DBObject id = new BasicDBObject("_id", dbObj.get("_id")); getDBCollection().remove(id);//from w w w .jav a 2 s . c om } else { getDBCollection().remove(dbObj); } checkLastError(); }