List of usage examples for com.mongodb DBObject removeField
Object removeField(String key);
From source file:org.sipfoundry.sipxconfig.commserver.imdb.SpeedDials.java
License:Open Source License
public boolean generate(Replicable entity, DBObject top) { if (!(entity instanceof User)) { return false; }//from w ww . j a v a 2s .co m User user = (User) entity; DBObject speedDialDBO = new BasicDBObject(); SpeedDial speedDial = m_speedDialManager.getSpeedDialForUser(user, false); if (speedDial != null) { speedDialDBO.put(USER, speedDial.getResourceListId(false)); speedDialDBO.put(USER_CONS, speedDial.getResourceListId(true)); List<DBObject> buttonsList = new ArrayList<DBObject>(); List<Button> buttons = speedDial.getButtons(); for (Button button : buttons) { if (!button.isBlf()) { continue; } DBObject buttonDBO = new BasicDBObject(); buttonDBO.put(URI, buildUri(button, getSipDomain())); String name = StringUtils.defaultIfEmpty(button.getLabel(), button.getNumber()); buttonDBO.put(NAME, name); buttonsList.add(buttonDBO); } if (!buttonsList.isEmpty()) { speedDialDBO.put(BUTTONS, buttonsList); } top.put(SPEEDDIAL, speedDialDBO); } else { top.removeField(SPEEDDIAL); } return true; }
From source file:org.sipfoundry.sipxconfig.commserver.imdb.UserLocation.java
License:Open Source License
@Override public boolean generate(Replicable entity, DBObject top) { if (entity instanceof User) { User user = (User) entity;// w w w. ja v a 2 s. c o m Branch site = user.getSite(); if (site != null) { top.put(USER_LOCATION, site.getName()); } else { top.removeField(USER_LOCATION); } UserProfile profile = user.getUserProfile(); if (profile != null) { putOnlyIfNotNull(top, ALT_IM_ID, (profile.getAlternateImId() != null) ? (profile.getAlternateImId().toLowerCase()) : (null)); putOnlyIfNotNull(top, JOB_TITLE, profile.getJobTitle()); putOnlyIfNotNull(top, JOB_DEPT, profile.getJobDept()); putOnlyIfNotNull(top, COMPANY_NAME, profile.getCompanyName()); putOnlyIfNotNull(top, ASSISTANT_NAME, profile.getAssistantName()); putOnlyIfNotNull(top, ASSISTANT_PHONE, profile.getAssistantPhoneNumber()); putOnlyIfNotNull(top, FAX_NUMBER, profile.getFaxNumber()); putOnlyIfNotNull(top, HOME_PHONE_NUMBER, profile.getHomePhoneNumber()); putOnlyIfNotNull(top, CELL_PHONE_NUMBER, profile.getCellPhoneNumber()); putOnlyIfNotNull(top, AVATAR, profile.getAvatar()); putOnlyIfNotNull(top, LOCATION, profile.getLocation()); // FIXME abe.getOfficeAddress should be accurate enough to get real office address // complete fix should be when XX-8002 gets solved Address officeAddress = null; if (profile.getUseBranchAddress() && site != null) { officeAddress = new Address(); officeAddress.setCity(site.getAddress().getCity()); officeAddress.setCountry(site.getAddress().getCountry()); officeAddress.setOfficeDesignation(site.getAddress().getOfficeDesignation()); officeAddress.setState(site.getAddress().getState()); officeAddress.setStreet(site.getAddress().getStreet()); officeAddress.setZip(site.getAddress().getZip()); } else { officeAddress = profile.getOfficeAddress(); } Address home = profile.getHomeAddress(); if (home != null) { putOnlyIfNotNull(top, HOME_STREET, home.getStreet()); putOnlyIfNotNull(top, HOME_CITY, home.getCity()); putOnlyIfNotNull(top, HOME_COUNTRY, home.getCountry()); putOnlyIfNotNull(top, HOME_STATE, home.getState()); putOnlyIfNotNull(top, HOME_ZIP, home.getZip()); } if (officeAddress != null) { putOnlyIfNotNull(top, OFFICE_STREET, officeAddress.getStreet()); putOnlyIfNotNull(top, OFFICE_CITY, officeAddress.getCity()); putOnlyIfNotNull(top, OFFICE_COUNTRY, officeAddress.getCountry()); putOnlyIfNotNull(top, OFFICE_STATE, officeAddress.getState()); putOnlyIfNotNull(top, OFFICE_ZIP, officeAddress.getZip()); putOnlyIfNotNull(top, OFFICE_DESIGNATION, officeAddress.getOfficeDesignation()); } } return true; } return false; }
From source file:org.sipfoundry.sipxconfig.commserver.imdb.UserStatic.java
License:Contributor Agreement License
public boolean generate(Replicable entity, DBObject top) { if (entity instanceof User) { User user = (User) entity;//from w ww. j a va 2 s . c o m String domainName = getSipDomain(); String externalMwi = user.getSettingValue(EXTERNAL_MWI); if (externalMwi != null) { top.put(STATIC, new UserStaticMapping(domainName, user.getUserName(), externalMwi)); } else { top.removeField(STATIC); } return true; } return false; }
From source file:org.springframework.data.document.mongodb.convert.MappingMongoConverter.java
License:Apache License
@SuppressWarnings({ "unchecked" }) protected Object getValueInternal(MongoPersistentProperty prop, DBObject dbo, StandardEvaluationContext ctx, String spelExpr) {//from w w w. ja va 2 s .c o m Object o; if (null != spelExpr) { Expression x = spelExpressionParser.parseExpression(spelExpr); o = x.getValue(ctx); } else { Object dbObj = dbo.get(prop.getKey()); if (dbObj == null) { return null; } Class<?> propertyType = prop.getType(); Class<?> customTarget = getCustomTarget(dbObj.getClass(), propertyType); if (customTarget != null) { return conversionService.convert(dbObj, propertyType); } if (dbObj instanceof DBRef) { dbObj = ((DBRef) dbObj).fetch(); } if (dbObj instanceof DBObject) { if (prop.isMap() && dbObj instanceof DBObject) { // We have to find a potentially stored class to be used first. Class<?> toType = findTypeToBeUsed((DBObject) dbObj); Map<String, Object> m = new LinkedHashMap<String, Object>(); for (Map.Entry<String, Object> entry : ((Map<String, Object>) ((DBObject) dbObj).toMap()) .entrySet()) { if (entry.getKey().equals(CUSTOM_TYPE_KEY)) { continue; } if (null != entry.getValue() && entry.getValue() instanceof DBObject) { m.put(entry.getKey(), read((null != toType ? toType : prop.getMapValueType()), (DBObject) entry.getValue())); } else { m.put(entry.getKey(), entry.getValue()); } } return m; } else if (prop.isArray() && dbObj instanceof BasicDBObject && ((DBObject) dbObj).keySet().size() == 0) { // It's empty return Array.newInstance(prop.getComponentType(), 0); } else if (prop.isCollection() && dbObj instanceof BasicDBList) { BasicDBList dbObjList = (BasicDBList) dbObj; List<Object> items = new LinkedList<Object>(); for (int i = 0; i < dbObjList.size(); i++) { Object dbObjItem = dbObjList.get(i); if (dbObjItem instanceof DBRef) { items.add(read(prop.getComponentType(), ((DBRef) dbObjItem).fetch())); } else if (dbObjItem instanceof DBObject) { items.add(read(prop.getComponentType(), (DBObject) dbObjItem)); } else { items.add(dbObjItem); } } List<Object> itemsToReturn = new LinkedList<Object>(); for (Object obj : items) { itemsToReturn.add(obj); } return itemsToReturn; } Class<?> toType = findTypeToBeUsed((DBObject) dbObj); // It's a complex object, have to read it in if (toType != null) { dbo.removeField(CUSTOM_TYPE_KEY); o = read(toType, (DBObject) dbObj); } else { o = read(mappingContext.getPersistentEntity(prop.getTypeInformation()), (DBObject) dbObj); } } else { o = dbObj; } } return o; }
From source file:org.springframework.data.document.mongodb.MongoTemplate.java
License:Apache License
protected WriteResult doUpdate(final String collectionName, final Query query, final Update update, final Class<?> entityClass, final boolean upsert, final boolean multi) { return execute(collectionName, new CollectionCallback<WriteResult>() { public WriteResult doInCollection(DBCollection collection) throws MongoException, DataAccessException { DBObject queryObj = query.getQueryObject(); DBObject updateObj = update.getUpdateObject(); String idProperty = "id"; if (null != entityClass) { idProperty = getPersistentEntity(entityClass).getIdProperty().getName(); }//from ww w . j a va 2 s.com for (String key : queryObj.keySet()) { if (idProperty.equals(key)) { // This is an ID field queryObj.put(ID, mongoConverter.maybeConvertObject(queryObj.get(key))); queryObj.removeField(key); } else { queryObj.put(key, mongoConverter.maybeConvertObject(queryObj.get(key))); } } for (String key : updateObj.keySet()) { updateObj.put(key, mongoConverter.maybeConvertObject(updateObj.get(key))); } if (LOGGER.isDebugEnabled()) { LOGGER.debug("calling update using query: " + queryObj + " and update: " + updateObj + " in collection: " + collectionName); } WriteResult wr; if (writeConcern == null) { if (multi) { wr = collection.updateMulti(queryObj, updateObj); } else { wr = collection.update(queryObj, updateObj); } } else { wr = collection.update(queryObj, updateObj, upsert, multi, writeConcern); } handleAnyWriteResultErrors(wr, queryObj, "update with '" + updateObj + "'"); return wr; } }); }
From source file:org.springframework.data.document.mongodb.MongoTemplate.java
License:Apache License
/** * Substitutes the id key if it is found in he query. Any 'id' keys will be replaced with '_id' and the value converted * to an ObjectId if possible. This conversion should match the way that the id fields are converted during read * operations./*from ww w . ja v a 2 s .c o m*/ * * @param query * @param targetClass * @param reader */ protected void substituteMappedIdIfNecessary(DBObject query, Class<?> targetClass, MongoReader<?> reader) { MongoConverter converter = null; if (reader instanceof SimpleMongoConverter) { converter = (MongoConverter) reader; } else if (reader instanceof MappingMongoConverter) { converter = (MappingMongoConverter) reader; } else { return; } String idKey = null; if (query.containsField("id")) { idKey = "id"; } if (query.containsField("_id")) { idKey = "_id"; } if (idKey == null) { // no ids in this query return; } MongoPropertyDescriptor descriptor; try { MongoPropertyDescriptor mpd = new MongoPropertyDescriptor(new PropertyDescriptor(idKey, targetClass), targetClass); descriptor = mpd; } catch (IntrospectionException e) { // no property descriptor for this key - try the other try { String theOtherIdKey = "id".equals(idKey) ? "_id" : "id"; MongoPropertyDescriptor mpd2 = new MongoPropertyDescriptor( new PropertyDescriptor(theOtherIdKey, targetClass), targetClass); descriptor = mpd2; } catch (IntrospectionException e2) { // no property descriptor for this key either - bail return; } } if (descriptor.isIdProperty() && descriptor.isOfIdType()) { Object value = query.get(idKey); if (value instanceof DBObject) { DBObject dbo = (DBObject) value; if (dbo.containsField("$in")) { List<Object> ids = new ArrayList<Object>(); int count = 0; for (Object o : (Object[]) dbo.get("$in")) { count++; ObjectId newValue = convertIdValue(converter, o); if (newValue != null) { ids.add(newValue); } } if (ids.size() > 0 && ids.size() != count) { throw new InvalidDataAccessApiUsageException("Inconsistent set of id values provided " + Arrays.asList((Object[]) dbo.get("$in"))); } if (ids.size() > 0) { dbo.removeField("$in"); dbo.put("$in", ids.toArray()); } } query.removeField(idKey); query.put(MongoPropertyDescriptor.ID_KEY, value); } else { ObjectId newValue = convertIdValue(converter, value); query.removeField(idKey); if (newValue != null) { query.put(MongoPropertyDescriptor.ID_KEY, newValue); } else { query.put(MongoPropertyDescriptor.ID_KEY, value); } } } }
From source file:org.springframework.data.mongodb.core.convert.MappingMongoConverter.java
License:Apache License
/** * Removes the type information from the entire conversion result. * // ww w . j a va2 s.c o m * @param object * @param recursively whether to apply the removal recursively * @return */ private Object removeTypeInfo(Object object, boolean recursively) { if (!(object instanceof DBObject)) { return object; } DBObject dbObject = (DBObject) object; String keyToRemove = null; for (String key : dbObject.keySet()) { if (recursively) { Object value = dbObject.get(key); if (value instanceof BasicDBList) { for (Object element : (BasicDBList) value) { removeTypeInfo(element, recursively); } } else { removeTypeInfo(value, recursively); } } if (typeMapper.isTypeKey(key)) { keyToRemove = key; if (!recursively) { break; } } } if (keyToRemove != null) { dbObject.removeField(keyToRemove); } return dbObject; }
From source file:org.springframework.data.mongodb.core.convert.MongoExampleMapper.java
License:Apache License
/** * Returns the given {@link Example} as {@link DBObject} holding matching values extracted from * {@link Example#getProbe()}./*from www . j a v a 2 s . co m*/ * * @param example must not be {@literal null}. * @param entity must not be {@literal null}. * @return */ public DBObject getMappedExample(Example<?> example, MongoPersistentEntity<?> entity) { Assert.notNull(example, "Example must not be null!"); Assert.notNull(entity, "MongoPersistentEntity must not be null!"); DBObject reference = (DBObject) converter.convertToMongoType(example.getProbe()); if (entity.hasIdProperty() && entity.getIdentifierAccessor(example.getProbe()).getIdentifier() == null) { reference.removeField(entity.getIdProperty().getFieldName()); } ExampleMatcherAccessor matcherAccessor = new ExampleMatcherAccessor(example.getMatcher()); applyPropertySpecs("", reference, example.getProbeType(), matcherAccessor); DBObject flattened = ObjectUtils.nullSafeEquals(NullHandler.INCLUDE, matcherAccessor.getNullHandler()) ? reference : new BasicDBObject(SerializationUtils.flattenMap(reference)); DBObject result = example.getMatcher().isAllMatching() ? flattened : orConcatenate(flattened); this.converter.getTypeMapper().writeTypeRestrictions(result, getTypesToMatch(example)); return result; }
From source file:org.springframework.data.services.rest.DataController.java
License:Apache License
@RequestMapping(value = "/{collection}", method = RequestMethod.GET) @ResponseBody/*www . j av a 2 s . c o m*/ public String list(@PathVariable String collection) { List<DBObject> results = this.mongoTemplate.findAll(DBObject.class, collection); for (DBObject result : results) { result.removeField("_class"); result.removeField("_id"); } return JSON.serialize(results); }
From source file:org.springframework.data.services.rest.DataController.java
License:Apache License
@RequestMapping(value = "/{collection}/{uid}", method = RequestMethod.GET) @ResponseBody//from ww w.j a v a 2 s . c om public String get(@PathVariable String collection, @PathVariable long uid) { Query query = new Query(Criteria.where("uid").is(uid)); List<DBObject> results = this.mongoTemplate.find(query, DBObject.class, collection); if (CollectionUtils.isEmpty(results)) { return null; // 404? } DBObject result = results.get(0); result.removeField("_class"); result.removeField("_id"); return JSON.serialize(result); }