Java Code Examples for com.mongodb.DBObject#containsField()

The following examples show how to use com.mongodb.DBObject#containsField() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DataService.java    From canal-mongo with Apache License 2.0 6 votes vote down vote up
public void update(List<CanalEntry.Column> data, String schemaName, String tableName) {
    DBObject obj = DBConvertUtil.columnToJson(data);
    logger.debug("update:{}", obj.toString());
    //订单库单独处理
    if (schemaName.equals("order")) {
        if (tableName.startsWith("order_base_info")) {
            tableName = "order_base_info";
        } else if (tableName.startsWith("order_detail_info")) {
            tableName = "order_detail_info";
        } else {
            logger.info("unknown data:{}.{}:{}", schemaName, tableName, obj);
        }
        updateData(schemaName, tableName, new BasicDBObject("orderId", obj.get("orderId")), obj);
    } else {
        if (obj.containsField("id")) {
            updateData(schemaName, tableName, new BasicDBObject("_id", obj.get("id")), obj);
        } else {
            logger.info("unknown data structure");
        }
    }
}
 
Example 2
Source File: DataService.java    From canal-mongo with Apache License 2.0 6 votes vote down vote up
public void deleteData(String schemaName, String tableName, DBObject obj) {
    int i = 0;
    String path = "/" + schemaName + "/" + tableName + "/" + CanalEntry.EventType.DELETE.getNumber();
    DBObject newObj = (DBObject) ObjectUtils.clone(obj);
    DBObject logObj = (DBObject) ObjectUtils.clone(obj);
    //保存原始数据
    try {
        i++;
        if (obj.containsField("id")) {
            naiveMongoTemplate.getCollection(tableName).remove(new BasicDBObject("_id", obj.get("id")));
        }
        i++;
        SpringUtil.doEvent(path, newObj);
    } catch (MongoClientException | MongoSocketException clientException) {
        //客户端连接异常抛出,阻塞同步,防止mongodb宕机
        throw clientException;
    } catch (Exception e) {
        logError(schemaName, tableName, 3, i, logObj, e);
    }
}
 
Example 3
Source File: DataService.java    From canal-mongo with Apache License 2.0 6 votes vote down vote up
public void insert(List<CanalEntry.Column> data, String schemaName, String tableName) {
    DBObject obj = DBConvertUtil.columnToJson(data);
    logger.debug("insert :{}", obj.toString());
    //订单库单独处理
    if (schemaName.equals("order")) {
        //保存原始数据
        if (tableName.startsWith("order_base_info")) {
            tableName = "order_base_info";
        } else if (tableName.startsWith("order_detail_info")) {
            tableName = "order_detail_info";
        } else {
            logger.info("unknown data :{}.{}:{}", schemaName, tableName, obj);
            return;
        }
        insertData(schemaName, tableName, obj, obj);
    } else {
        DBObject newObj = (DBObject) ObjectUtils.clone(obj);
        if (newObj.containsField("id")) {
            newObj.put("_id", newObj.get("id"));
            newObj.removeField("id");
        }
        insertData(schemaName, tableName, newObj, obj);
    }
}
 
Example 4
Source File: MongoCollectionRemover.java    From bluima with Apache License 2.0 6 votes vote down vote up
@Override
public void getNext(JCas jCas) throws IOException, CollectionException {

	DBObject doc = cur.next();

	Header h = new Header(jCas);
	h.setDocId(doc.get("_id").toString());
	h.addToIndexes();

	Map<String, Integer> dbDeleteKeys = new HashMap<String, Integer>();
	for (String deleteKeyName : keysToDelete) {
		if (doc.containsField(deleteKeyName))
			dbDeleteKeys.put(deleteKeyName, 1);
	}

	// insert all dbLists
	BasicDBObject updateQuery = new BasicDBObject("_id", doc.get("_id")
			.toString());
	BasicDBObject updateCommands = new BasicDBObject();
	updateCommands.put("$unset", dbDeleteKeys);
	coll.update(updateQuery, updateCommands, false, false);
}
 
Example 5
Source File: DataServlet.java    From BLELocalization with MIT License 6 votes vote down vote up
private void updateSampleInfo(DBObject sampling, DBObject refpoint) {
	DBObject info = (DBObject) sampling.get("information");
	if (info.containsField("refid") && info.containsField("x") && info.containsField("y")) {
		if (refpoint == null) {
			refpoint = mCollRef.findOne(new BasicDBObject("_id", info.get("refid")));
			if (refpoint == null) {
				System.err.println("No refpoint: " + JSON.serialize(info));
				return;
			}
		}
		DBObject set = new BasicDBObject();
		AffineTransform at = new AffineTransform();
		at.translate(((Number) refpoint.get("x")).doubleValue(), ((Number) refpoint.get("y")).doubleValue());
		at.rotate(Math.toRadians(((Number) refpoint.get("rotate")).doubleValue()));
		Point2D.Double src = new Point2D.Double(((Number) info.get("x")).doubleValue(), ((Number) info.get("y")).doubleValue());
		Point2D.Double dst = new Point2D.Double();
		at.transform(src, dst);
		set.put("information.absx", dst.getX());
		set.put("information.absy", dst.getY());
		set.put("information.floor", refpoint.get("floor"));
		set.put("information.floor_num", refpoint.get("floor_num"));
		mCollSamp.update(new BasicDBObject("_id", sampling.get("_id")), new BasicDBObject("$set", set));
		System.out.println(JSON.serialize(set));
	}

}
 
Example 6
Source File: SampleValidation.java    From hvdf with Apache License 2.0 6 votes vote down vote up
public void validate(DBObject sample){
	
	if(sample != null && sample.containsField(TARGET_FIELD_KEY)){
		int xValue = (Integer)sample.get(TARGET_FIELD_KEY);
		
		// Throw a standard exception if an illegal value is encountered
		if(xValue == illegalValue){
			throw new ServiceException("Illegal value for field_x", 
					SampleError.INVALID_SAMPLE).set(TARGET_FIELD_KEY, xValue);
		}
		
		// Change the value to clip to a configured maximum
		if(xValue > maxValue){
			sample.put(TARGET_FIELD_KEY, maxValue);
		}
	}
	else{
		// The field does not exist
		throw new ServiceException("Sample missing value for field_x", SampleError.INVALID_SAMPLE);
	}		
}
 
Example 7
Source File: EntitySessionController.java    From restfiddle with Apache License 2.0 6 votes vote down vote up
private void dbRefToRelation(DBObject dbObject) {
if (dbObject == null) {
    return;
}
if (dbObject.containsField("_id")) 
    dbObject.put("_id", ((ObjectId) dbObject.get("_id")).toHexString());
for (String key : dbObject.keySet()) {
    Object obj = dbObject.get(key);
    if (obj instanceof DBRef) {
	DBRef ref = (DBRef) obj;
	dbObject.put(key, dbRefToRel(ref));
    } else if (obj instanceof DBObject) {
	dbRefToRelation((DBObject) obj);
    }
}

   }
 
Example 8
Source File: EntityDataController.java    From restfiddle with Apache License 2.0 6 votes vote down vote up
private void dbRefToRelation(DBObject dbObject) {
if (dbObject == null) {
    return;
}
if (dbObject.containsField("_id")) 
    dbObject.put("_id", ((ObjectId) dbObject.get("_id")).toHexString());
for (String key : dbObject.keySet()) {
    Object obj = dbObject.get(key);
    if (obj instanceof DBRef) {
	DBRef ref = (DBRef) obj;
	dbObject.put(key, dbRefToRel(ref));
    } else if (obj instanceof DBObject) {
	dbRefToRelation((DBObject) obj);
    }
}

   }
 
Example 9
Source File: QueryModel.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
private static void validateDBCommand( DBObject commandObj ) throws OdaException
{
    // check that the db command is one of the supported ones
    boolean hasSupportedCommand = false;
    for( int i=0; i < SUPPORTED_DB_COMMANDS.length; i++ )
    {
        String supportedCommand = SUPPORTED_DB_COMMANDS[i];
        if( commandObj.containsField( supportedCommand ) || 
            commandObj.containsField( supportedCommand.toLowerCase() ) )
        {
            hasSupportedCommand = true;
            break;
        }
    }

    if( ! hasSupportedCommand )
        throw new OdaException( Messages.bind( Messages.queryModel_nonSupportedDbCmd, commandObj.toString() ));
    
    // only supports eval command w/ {nolock : true}
    if( commandObj.containsField( EVAL_KEY ) )
    {
        boolean noLockValue = getBooleanValueOfKey( commandObj, NOLOCK_KEY, false );
        if( noLockValue != true )
            throw new OdaException( Messages.bind( Messages.queryModel_invalidDbCmdKeyValue, 
                    EVAL_KEY, "{" + NOLOCK_KEY + " : true}" )); //$NON-NLS-1$ //$NON-NLS-2$
    }
}
 
Example 10
Source File: QueryModel.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
private static void validateMapReduceCommand( DBObject commandObj ) throws OdaException
{
    for( int i=0; i < REQUIRED_MAPREDUCE_KEYS.length; i++ )
    {
        String requiredKey = REQUIRED_MAPREDUCE_KEYS[i];
        if( ! commandObj.containsField( requiredKey ) )
            throw new OdaException( Messages.bind( Messages.queryModel_missingMapReduceKey, requiredKey ));
        if( commandObj.get( requiredKey ) == null )
            throw new OdaException( Messages.bind( Messages.queryModel_missingMapReduceValue, requiredKey ));
    }        
}
 
Example 11
Source File: Recents.java    From XBDD with Apache License 2.0 5 votes vote down vote up
@PUT
@Path("/build/{product}/{major}.{minor}.{servicePack}/{build}")
@Produces(MediaType.APPLICATION_JSON)
public Response addBuildToRecents(@BeanParam final Coordinates coordinates) {

	final DBObject buildCoords = coordinates.getReportCoordinates();

	final DBCollection collection = this.mongoLegacyDb.getCollection("users");

	final BasicDBObject user = new BasicDBObject();
	user.put("user_id", LoggedInUserUtil.getLoggedInUser().getUserId());

	final DBObject blank = new BasicDBObject();
	final DBObject doc = collection.findAndModify(user, blank, blank, false, new BasicDBObject("$set", user), true, true);

	if (doc.containsField("recentBuilds")) {
		final BasicDBList buildArray = (BasicDBList) doc.get("recentBuilds");
		if (buildArray.contains(buildCoords)) {
			// BasicDBObject toMove = (BasicDBObject) featureArray.get(featureArray.indexOf(featureDetails));
			buildArray.remove(buildCoords);
			buildArray.add(buildCoords);
			collection.update(user, new BasicDBObject("$set", new BasicDBObject("recentBuilds", buildArray)));
		} else {
			if (buildArray.size() >= 5) {
				collection.update(user, new BasicDBObject("$pop", new BasicDBObject("recentBuilds", "-1")));
			}
			collection.update(user, new BasicDBObject("$addToSet", new BasicDBObject("recentBuilds", buildCoords)));
		}
	} else {
		collection.update(user, new BasicDBObject("$addToSet", new BasicDBObject("recentBuilds", buildCoords)));
	}

	return Response.ok().build();
}
 
Example 12
Source File: Recents.java    From XBDD with Apache License 2.0 5 votes vote down vote up
@PUT
@Path("/feature/{product}/{major}.{minor}.{servicePack}/{build}/{id:.+}")
@Produces(MediaType.APPLICATION_JSON)
public Response addFeatureToRecents(@QueryParam("name") final String featureName,
		@BeanParam final Coordinates coordinates,
		@PathParam("id") final String featureID) {

	final BasicDBObject featureDetails = new BasicDBObject("name", featureName);
	featureDetails.put("product", coordinates.getProduct());
	featureDetails.put("version", coordinates.getVersionString());
	featureDetails.put("build", coordinates.getBuild());
	featureDetails.put("id", featureID);

	final DBCollection collection = this.mongoLegacyDb.getCollection("users");

	final BasicDBObject user = new BasicDBObject();
	user.put("user_id", LoggedInUserUtil.getLoggedInUser().getUserId());

	final DBObject blank = new BasicDBObject();
	final DBObject doc = collection.findAndModify(user, blank, blank, false, new BasicDBObject("$set", user), true, true);

	if (doc.containsField("recentFeatures")) {
		final BasicDBList featureArray = (BasicDBList) doc.get("recentFeatures");
		if (featureArray.contains(featureDetails)) {
			featureArray.remove(featureDetails);
			featureArray.add(featureDetails);
			collection.update(user, new BasicDBObject("$set", new BasicDBObject("recentFeatures", featureArray)));
		} else {
			if (featureArray.size() >= 5) {
				collection.update(user, new BasicDBObject("$pop", new BasicDBObject("recentFeatures", "-1")));
			}
			collection.update(user, new BasicDBObject("$addToSet", new BasicDBObject("recentFeatures", featureDetails)));
		}
	} else {
		collection.update(user, new BasicDBObject("$addToSet", new BasicDBObject("recentFeatures", featureDetails)));
	}

	return Response.ok().build();
}
 
Example 13
Source File: AdminUtils.java    From XBDD with Apache License 2.0 5 votes vote down vote up
private DBObject renameDoc(final String product, final String newname, final DBObject doc) {
	doc.put("_id", ((String) doc.get("_id")).replaceAll(product + "/", newname + "/"));
	if (doc.containsField("coordinates")) {
		final DBObject coordinates = (DBObject) doc.get("coordinates");
		coordinates.put("product", newname);
		doc.put("coordinates", coordinates);
	}
	return doc;
}
 
Example 14
Source File: DataServlet.java    From BLELocalization with MIT License 5 votes vote down vote up
private void updateSamplingData(DBObject data) {
	if (data.containsField("information")) {
		DBObject info = (DBObject) data.get("information");
	
		if (info.containsField("refid") && info.containsField("x") && info.containsField("y")) {
			DBObject refpoint = mCollRef.findOne(info.get("refid"));
			
			if (refpoint != null && refpoint.containsField("x") && refpoint.containsField("y") && refpoint.containsField("floor")
					&& refpoint.containsField("floor_num")) {
				
				AffineTransform at = new AffineTransform();
				at.translate(((Number) refpoint.get("x")).doubleValue(), ((Number) refpoint.get("y")).doubleValue());
				at.rotate(Math.toRadians(((Number) refpoint.get("rotate")).doubleValue()));
				Point2D.Double src = new Point2D.Double(((Number) info.get("x")).doubleValue(), ((Number) info.get("y")).doubleValue());
				Point2D.Double dst = new Point2D.Double();
				at.transform(src, dst);
				
				info.put("absx", dst.getX());
				info.put("absy", dst.getY());
				info.put("floor", refpoint.get("floor"));
				info.put("floor_num", refpoint.get("floor_num"));

				System.out.println(JSON.serialize(info));
			} else {
				info.put("absx", ((Number) info.get("x")).doubleValue());
				info.put("absy", ((Number) info.get("y")).doubleValue());
			}
		}
	}
}
 
Example 15
Source File: DataServlet.java    From BLELocalization with MIT License 5 votes vote down vote up
private void onRefpointChange(DBObject queryRef) {
	for (DBCursor refpoints = mCollRef.find(queryRef); refpoints.hasNext();) {
		DBObject refpoint = refpoints.next();
		if (refpoint.containsField("_id") && refpoint.containsField("x") && refpoint.containsField("y") && refpoint.containsField("floor")
				&& refpoint.containsField("floor_num")) {
			for (DBCursor samplings = mCollSamp.find(new BasicDBObject("information.refid", refpoint.get("_id")), new BasicDBObject("information", 1)); samplings
					.hasNext();) {
				updateSampleInfo(samplings.next(), refpoint);
			}
		}
	}
}
 
Example 16
Source File: MongoEntityRepository.java    From secure-data-service with Apache License 2.0 5 votes vote down vote up
private Query addEmbededFields(Query query, Set<String> embededFields) {
    if (query == null) {
        return null;
    }
    DBObject fieldObjects = query.getFieldsObject();
    if (fieldObjects != null) {
        for (String embededField : embededFields) {
            if (!fieldObjects.containsField(embededField)) {
                fieldObjects.put(embededField, 1);
            }
        }
    }
    return query;
}
 
Example 17
Source File: JenkinsEmbeddedMapper.java    From DotCi with MIT License 4 votes vote down vote up
private boolean isFullySerializedObject(final DBObject cachedStub, final Mapper mapper) {
    return cachedStub.keySet().size() > 2
        && cachedStub.containsField(mapper.ID_KEY)
        && cachedStub.containsField(mapper.CLASS_NAME_FIELDNAME);
}
 
Example 18
Source File: EntityAuthService.java    From restfiddle with Apache License 2.0 4 votes vote down vote up
public JSONObject authorize(String projectId, String authToken, String... roles) {

JSONObject response = new JSONObject();
if(authToken == null){
    return response.put(SUCCESS, false).put("msg", UNAUTHORIZED);
}

List<String> roleList = Arrays.asList(roles);

DBCollection dbCollection = mongoTemplate.getCollection(ENTITY_AUTH);

BasicDBObject queryObject = new BasicDBObject();
queryObject.append("_id", new ObjectId(authToken));

DBObject authData = dbCollection.findOne(queryObject);

if(authData != null && projectId.equals(authData.get("projectId"))) {
    DBRef userRef = (DBRef)authData.get("user");
    DBObject user = mongoTemplate.getCollection(userRef.getCollectionName()).findOne(userRef.getId());
    
    DBObject roleObj = null;
    if(user.containsField("role")){
	DBRef roleRef = (DBRef)user.get("role");
	roleObj = mongoTemplate.getCollection(roleRef.getCollectionName()).findOne(roleRef.getId());
    }
    
    if((roleObj != null && roleList.contains(roleObj.get("name"))) || roleList.contains("USER")){
	response.put(SUCCESS, true);
	response.put("user", userRef);
	
	authData.put("expireAt", new Date(System.currentTimeMillis() + 3600 * 1000));
	dbCollection.save(authData);
    } else {
	response.put(SUCCESS, false).put("msg", UNAUTHORIZED);
    }
} else {
    response.put(SUCCESS, false).put("msg", UNAUTHORIZED);
}

return response;
   }
 
Example 19
Source File: MongoCollectionReader.java    From bluima with Apache License 2.0 4 votes vote down vote up
@Override
public void getNext(JCas jCas) throws IOException, CollectionException {

    // text & id
    DBObject doc = cur.next();
    Object text = doc.get(TEXT);
    if (text != null)
        jCas.setDocumentText(doc.get(TEXT).toString());
    else
        jCas.setDocumentText("");
    Header h = new Header(jCas);
    h.setDocId(doc.get(ID).toString());
    if (doc.containsField(TITLE) && doc.get(TITLE) != null)
        h.setTitle(doc.get(TITLE).toString());
    else
        h.setTitle("");
    h.addToIndexes();

    // all other annotations, from mappings
    for (String dbListsName : doc.keySet()) {

        for (String annotClass : ALL_MAPPINGS_KEYS) {
            MongoFieldMapping fm = ALL_MAPPINGS.get(annotClass);

            if (fm.shortName.equals(dbListsName)) {

                BasicDBList dbList = (BasicDBList) doc.get(dbListsName);
                for (Object o : dbList) {
                    BasicDBObject dbO = (BasicDBObject) o;

                    try {
                        Annotation a = getAnnotationByClassName(jCas,
                                annotClass);
                        a.setBegin(dbO.getInt(BEGIN));// LATER maybe opt.
                        a.setEnd(dbO.getInt(END));

                        Type t = a.getType();
                        for (Feature f : t.getFeatures()) {
                            // System.err.println("f.short "
                            // + f.getShortName());

                            if (fm.fieldMappings.containsKey(f
                                    .getShortName())) {

                                String fieldKey = fm.fieldMappings.get(f
                                        .getShortName());
                                String range = f.getRange().getShortName();

                                MongoFieldMapping.readFieldFromDb(fieldKey,
                                        range, a, f, dbO, jCas);
                            }
                        }
                        a.addToIndexes();

                    } catch (Exception e) {
                        LOG.error("while processing docId " + doc.get(ID),
                                e);
                    }
                }
            }
        }
    }
}
 
Example 20
Source File: XMLToImportantChemicals.java    From act with GNU General Public License v3.0 4 votes vote down vote up
private void process(XMLStreamReader xml) throws XMLStreamException {
  String tag;
  String root = null;
  Stack<DBObject> json = new Stack<DBObject>();
  DBObject js;
  while (xml.hasNext()) {
    int eventType = xml.next();
    while (xml.isWhiteSpace() || eventType == XMLEvent.SPACE)
      eventType = xml.next();

    switch (eventType) {
      case XMLEvent.START_ELEMENT:
        tag = xml.getLocalName();
        if (root == null) {
          root = tag;
        } else {
          json.push(new BasicDBObject());
        }
        break;
      case XMLEvent.END_ELEMENT:
        tag = xml.getLocalName();
        if (tag.equals(root)) {
          // will terminate in next iteration
        } else {
          js = json.pop();
          if (json.size() == 0) {
            if (tag.equals(rowTag))
              printEntry(js);
            else
              printUnwantedEntry(js);
          } else {
            putListStrOrJSON(json.peek(), tag, js);
          }
        }
        break;

      case XMLEvent.CHARACTERS:
        String txt = xml.getText();
        js = json.peek();
        if (js.containsField(strTag)) {
          txt = js.get(strTag) + txt;
          js.removeField(strTag);
        }
        js.put(strTag, txt);
        break;

      case XMLEvent.START_DOCUMENT:
        break;
      case XMLEvent.END_DOCUMENT:
        break;
      case XMLEvent.COMMENT:
      case XMLEvent.ENTITY_REFERENCE:
      case XMLEvent.ATTRIBUTE:
      case XMLEvent.PROCESSING_INSTRUCTION:
      case XMLEvent.DTD:
      case XMLEvent.CDATA:
      case XMLEvent.SPACE:
        System.out.format("%s --\n", eventType);
        break;
    }
  }
}