Java Code Examples for org.bson.BsonDocument#put()

The following examples show how to use org.bson.BsonDocument#put() . 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: MongoWriterUtil.java    From epcis with Apache License 2.0 6 votes vote down vote up
static BsonArray getQuantityObjectList(List<QuantityElementType> qetList, Integer gcpLength) {
	BsonArray quantityList = new BsonArray();
	for (int i = 0; i < qetList.size(); i++) {
		BsonDocument quantity = new BsonDocument();
		QuantityElementType qet = qetList.get(i);
		if (qet.getEpcClass() != null)
			quantity.put("epcClass", new BsonString(getClassEPC(qet.getEpcClass().toString(), gcpLength)));
		if (qet.getQuantity().doubleValue() != 0) {
			quantity.put("quantity", new BsonDouble(qet.getQuantity().doubleValue()));
		}
		if (qet.getUom() != null)
			quantity.put("uom", new BsonString(qet.getUom().toString()));
		quantityList.add(quantity);
	}
	return quantityList;
}
 
Example 2
Source File: MongoWriterUtil.java    From epcis with Apache License 2.0 6 votes vote down vote up
static BsonDocument getBaseExtensionObject(EPCISEventExtensionType baseExtensionType) {
	BsonDocument baseExtension = new BsonDocument();
	/*
	 * May be deprecated if (baseExtensionType.getAny() != null &&
	 * baseExtensionType.getAny().isEmpty() == false) { List<Object> objList =
	 * baseExtensionType.getAny(); BsonDocument map2Save = getAnyMap(objList); if
	 * (map2Save.isEmpty() == false) baseExtension.put("any", map2Save); }
	 */
	if (baseExtensionType.getOtherAttributes() != null
			&& baseExtensionType.getOtherAttributes().isEmpty() == false) {
		Map<QName, String> map = baseExtensionType.getOtherAttributes();
		BsonDocument map2Save = getOtherAttributesMap(map);
		if (map2Save.isEmpty() == false)
			baseExtension.put("otherAttributes", map2Save);
	}
	return baseExtension;
}
 
Example 3
Source File: BsonToRecord.java    From octarine with Apache License 2.0 6 votes vote down vote up
@Test public void
convert_bson_to_record() {
    BsonDocument doc = new BsonDocument();
    doc.put("name", new BsonString("Dominic"));
    doc.put("age", new BsonInt32(39));

    BsonRecordDeserialiser deserialiser = BsonRecordDeserialiser.builder()
            .readString(Person.name)
            .readInteger(Person.age)
            .get();

    Record result = deserialiser.apply(doc);
    assertTrue("name not set", Person.name.get(result).isPresent());
    assertEquals("incorrect name", "Dominic", Person.name.get(result).get());
    assertTrue("age not set", Person.age.get(result).isPresent());
    assertEquals("incorrect age", 39, (int) Person.age.get(result).get());
}
 
Example 4
Source File: AvroJsonSchemafulRecordConverter.java    From kafka-connect-mongodb with Apache License 2.0 6 votes vote down vote up
private void handleMapField(BsonDocument doc, Map m, Field field) {
    logger.trace("handling complex type 'map'");
    if(m==null) {
        logger.trace("no field in struct -> adding null");
        doc.put(field.name(), BsonNull.VALUE);
        return;
    }
    BsonDocument bd = new BsonDocument();
    for(Object entry : m.keySet()) {
        String key = (String)entry;
        Schema.Type valueSchemaType = field.schema().valueSchema().type();
        if(valueSchemaType.isPrimitive()) {
            bd.put(key, getConverter(field.schema().valueSchema()).toBson(m.get(key),field.schema()));
        } else if (valueSchemaType.equals(Schema.Type.ARRAY)) {
            final Field elementField = new Field(key, 0, field.schema().valueSchema());
            final List list = (List)m.get(key);
            logger.trace("adding array values to {} of type valueSchema={} value='{}'",
               elementField.name(), elementField.schema().valueSchema(), list);
            bd.put(key, handleArrayField(list, elementField));
        } else {
            bd.put(key, toBsonDoc(field.schema().valueSchema(), m.get(key)));
        }
    }
    doc.put(field.name(), bd);
}
 
Example 5
Source File: CaptureUtil.java    From epcis with Apache License 2.0 6 votes vote down vote up
public BsonDocument putInputQuantityList(BsonDocument base, List<QuantityElement> inputQuantityList) {
	BsonArray quantityArray = new BsonArray();
	for (QuantityElement quantityElement : inputQuantityList) {
		BsonDocument bsonQuantityElement = new BsonDocument("epcClass",
				new BsonString(quantityElement.getEpcClass()));
		if (quantityElement.getQuantity() != null) {
			bsonQuantityElement.put("quantity", new BsonDouble(quantityElement.getQuantity()));
		}
		if (quantityElement.getUom() != null) {
			bsonQuantityElement.put("uom", new BsonString(quantityElement.getUom()));
		}
		quantityArray.add(bsonQuantityElement);
	}
	base.put("inputQuantityList", quantityArray);
	return base;
}
 
Example 6
Source File: ChronoVertex.java    From epcis with Apache License 2.0 6 votes vote down vote up
/**
 * Return in-going edges (Internal Method)
 * 
 * @param labels
 * @return in-going edges
 */
private Iterable<ChronoEdge> getInChronoEdges(final BsonArray labels, final int branchFactor) {
	HashSet<ChronoEdge> edgeSet = new HashSet<ChronoEdge>();
	BsonDocument filter = new BsonDocument();
	BsonDocument inner = new BsonDocument();
	filter.put(Tokens.IN_VERTEX, new BsonString(this.toString()));
	if (labels != null && labels.size() != 0) {
		inner.put(Tokens.FC.$in.toString(), labels);
		filter.put(Tokens.LABEL, inner);
	}

	Iterator<BsonDocument> it = null;
	if (branchFactor == Integer.MAX_VALUE)
		it = graph.getEdgeCollection().find(filter).projection(Tokens.PRJ_ONLY_ID).iterator();
	else
		it = graph.getEdgeCollection().find(filter).projection(Tokens.PRJ_ONLY_ID).limit(branchFactor).iterator();

	while (it.hasNext()) {
		BsonDocument d = it.next();
		edgeSet.add(new ChronoEdge(d.getString(Tokens.ID).getValue(), this.graph));
	}
	return edgeSet;
}
 
Example 7
Source File: MongoQueryUtil.java    From epcis with Apache License 2.0 6 votes vote down vote up
static BsonDocument getExistsQueryObject(String[] fieldArr, String str, BsonBoolean isExist) {
	BsonArray conjQueries = new BsonArray();
	for (String field : fieldArr) {
		BsonDocument query = new BsonDocument();
		if (str != null) {
			str = encodeMongoObjectKey(str);
			query.put(field + "." + str, new BsonDocument("$exists", isExist));
		} else {
			query.put(field, new BsonDocument("$exists", isExist));
		}
		conjQueries.add(query);
	}
	if (conjQueries.size() != 0) {
		BsonDocument queryObject = new BsonDocument();
		if (isExist.equals(BsonBoolean.TRUE))
			queryObject.put("$or", conjQueries);
		else {
			queryObject.put("$and", conjQueries);
		}
		return queryObject;
	} else {
		return null;
	}
}
 
Example 8
Source File: CaptureUtil.java    From epcis with Apache License 2.0 6 votes vote down vote up
public BsonDocument putChildQuantityList(BsonDocument base, List<QuantityElement> childQuantityList) {
	BsonArray quantityArray = new BsonArray();
	for (QuantityElement quantityElement : childQuantityList) {
		BsonDocument bsonQuantityElement = new BsonDocument("epcClass",
				new BsonString(quantityElement.getEpcClass()));
		if (quantityElement.getQuantity() != null) {
			bsonQuantityElement.put("quantity", new BsonDouble(quantityElement.getQuantity()));
		}
		if (quantityElement.getUom() != null) {
			bsonQuantityElement.put("uom", new BsonString(quantityElement.getUom()));
		}
		quantityArray.add(bsonQuantityElement);
	}
	base.put("childQuantityList", quantityArray);
	return base;
}
 
Example 9
Source File: BsonToRecord.java    From octarine with Apache License 2.0 5 votes vote down vote up
@Test public void
convert_nested_bson_to_record() {
    BsonDocument doc = new BsonDocument();
    doc.put("name", new BsonString("Dominic"));
    doc.put("age", new BsonInt32(39));
    BsonDocument address = new BsonDocument();
    BsonArray addressLines = new BsonArray(ADDRESS_LINES.stream()
            .map(s -> new BsonString(s))
            .collect(Collectors.toList()));
    address.put("addressLines", addressLines);
    doc.put("address", address);

    BsonRecordDeserialiser addressDeserialiser = BsonRecordDeserialiser.builder()
            .readList(Address.addressLines, BsonDeserialisers.ofString)
            .get();

    BsonRecordDeserialiser deserialiser = BsonRecordDeserialiser.builder()
            .readString(Person.name)
            .readInteger(Person.age)
            .readValidRecord(Person.address, addressDeserialiser.validAgainst(Address.schema))
            .get();

    Record result = deserialiser.apply(doc);
    assertTrue("name not set", Person.name.get(result).isPresent());
    assertEquals("incorrect name", "Dominic", Person.name.get(result).get());
    assertTrue("age not set", Person.age.get(result).isPresent());
    assertEquals("incorrect age", 39, (int) Person.age.get(result).get());
    Optional<Valid<Address>> maybeAddress = Person.address.get(result);
    assertTrue("Address not present", maybeAddress.isPresent());
    Optional<PVector<String>> maybeLines = Address.addressLines.get(maybeAddress.get());
    assertThat(maybeLines, not(isEmpty()));
    assertEquals("Address lines don't match", ADDRESS_LINES, maybeLines.get());
}
 
Example 10
Source File: ChronoGraph.java    From epcis with Apache License 2.0 5 votes vote down vote up
/**
 * Return an iterable to all the edges in the graph. If this is not possible for
 * the implementation, then an UnsupportedOperationException can be thrown.
 * 
 * @param labels
 * @return iterable Edge Identifiers having the given label sets
 */
public HashSet<ChronoEdge> getEdges(final String label) {

	HashSet<ChronoEdge> edgeSet = new HashSet<ChronoEdge>();
	BsonDocument filter = new BsonDocument();
	filter.put(Tokens.LABEL, new BsonString(label));
	Iterator<BsonDocument> it = edges.find(filter).iterator();
	while (it.hasNext()) {
		BsonDocument d = it.next();
		edgeSet.add(new ChronoEdge(d.getString(Tokens.ID).getValue(), this));
	}
	return edgeSet;
}
 
Example 11
Source File: MongoWriterUtil.java    From epcis with Apache License 2.0 5 votes vote down vote up
static BsonDocument getOtherAttributesMap(Map<QName, String> map) {
	BsonDocument map2Save = new BsonDocument();
	Iterator<QName> iter = map.keySet().iterator();
	while (iter.hasNext()) {
		QName qName = iter.next();
		String value = map.get(qName);
		map2Save.put(qName.toString(), new BsonString(value));
	}
	return map2Save;
}
 
Example 12
Source File: DataSynchronizer.java    From stitch-android-sdk with Apache License 2.0 5 votes vote down vote up
/**
 * Adds and returns a document with a new version to the given document.
 *
 * @param document   the document to attach a new version to.
 * @param newVersion the version to attach to the document
 * @return a document with a new version to the given document.
 */
private static BsonDocument withNewVersion(
    final BsonDocument document,
    final BsonDocument newVersion
) {
  final BsonDocument newDocument = BsonUtils.copyOfDocument(document);
  newDocument.put(DOCUMENT_VERSION_FIELD, newVersion);
  return newDocument;
}
 
Example 13
Source File: CaptureUtil.java    From epcis with Apache License 2.0 5 votes vote down vote up
public BsonDocument putILMD(BsonDocument base, Map<String, String> namespaces, BsonDocument ilmds) {
	BsonDocument bsonILMD = convertToExtensionDocument(namespaces, ilmds);
	BsonDocument any = new BsonDocument();
	any.put("any", bsonILMD);
	base.put("ilmd", any);
	return base;
}
 
Example 14
Source File: CaptureUtil.java    From epcis with Apache License 2.0 5 votes vote down vote up
public BsonDocument putOutputEPCList(BsonDocument base, List<String> outputEPCList) {
	BsonArray bsonEPCList = new BsonArray();
	for (String epc : outputEPCList) {
		bsonEPCList.add(new BsonDocument("epc", new BsonString(epc)));
	}
	base.put("outputEPCList", bsonEPCList);
	return base;
}
 
Example 15
Source File: MongoWriterUtil.java    From epcis with Apache License 2.0 5 votes vote down vote up
static BsonArray getBizTransactionObjectList(List<BusinessTransactionType> bizList) {
	BsonArray bizTranList = new BsonArray();
	for (int i = 0; i < bizList.size(); i++) {
		BusinessTransactionType bizTranType = bizList.get(i);
		if (bizTranType.getType() != null && bizTranType.getValue() != null) {
			BsonDocument dbObj = new BsonDocument();
			dbObj.put(bizTranType.getType(), new BsonString(
					bizTranType.getValue().replaceAll("\n", "").replaceAll("\t", "").replaceAll("\\s", "")));
			bizTranList.add(dbObj);
		}
	}
	return bizTranList;
}
 
Example 16
Source File: GridFSTest.java    From mongo-java-driver-reactivestreams with Apache License 2.0 5 votes vote down vote up
private BsonDocument parseHexDocument(final BsonDocument document, final String hexDocument) {
    if (document.containsKey(hexDocument) && document.get(hexDocument).isDocument()) {
        byte[] bytes = DatatypeConverter.parseHexBinary(document.getDocument(hexDocument).getString("$hex").getValue());
        document.put(hexDocument, new BsonBinary(bytes));
    }
    return document;
}
 
Example 17
Source File: CaptureUtil.java    From epcis with Apache License 2.0 4 votes vote down vote up
public BsonDocument putType(BsonDocument base, VocabularyType type) {
	base.put("type", new BsonString(type.getVocabularyType()));
	return base;
}
 
Example 18
Source File: CaptureUtil.java    From epcis with Apache License 2.0 4 votes vote down vote up
public BsonDocument putID(BsonDocument base, String id) {
	base.put("id", new BsonString(id));
	return base;
}
 
Example 19
Source File: CaptureUtil.java    From epcis with Apache License 2.0 4 votes vote down vote up
public BsonDocument putBizLocation(BsonDocument base, String bizLocation) {
	base.put("bizLocation", new BsonDocument("id", new BsonString(bizLocation)));
	return base;
}
 
Example 20
Source File: AggregationEvent.java    From epcis with Apache License 2.0 4 votes vote down vote up
public BsonDocument asBsonDocument() {
	CaptureUtil util = new CaptureUtil();

	BsonDocument aggregationEvent = super.asBsonDocument();
	// Required Fields
	aggregationEvent = util.putEventType(aggregationEvent, "AggregationEvent");
	aggregationEvent = util.putAction(aggregationEvent, action);

	// Optional Fields
	if (this.parentID != null) {
		aggregationEvent = util.putParentID(aggregationEvent, parentID);
	}
	if (this.childEPCs != null && this.childEPCs.size() != 0) {
		aggregationEvent = util.putChildEPCs(aggregationEvent, childEPCs);
	}
	if (this.bizStep != null) {
		aggregationEvent = util.putBizStep(aggregationEvent, bizStep);
	}
	if (this.disposition != null) {
		aggregationEvent = util.putDisposition(aggregationEvent, disposition);
	}
	if (this.readPoint != null) {
		aggregationEvent = util.putReadPoint(aggregationEvent, readPoint);
	}
	if (this.bizLocation != null) {
		aggregationEvent = util.putBizLocation(aggregationEvent, bizLocation);
	}
	if (this.bizTransactionList != null && this.bizTransactionList.isEmpty() == false) {
		aggregationEvent = util.putBizTransactionList(aggregationEvent, bizTransactionList);
	}
	if (this.extensions != null && this.extensions.isEmpty() == false) {
		aggregationEvent = util.putExtensions(aggregationEvent, namespaces, extensions);
	}

	BsonDocument extension = new BsonDocument();
	if (this.childQuantityList != null && this.childQuantityList.isEmpty() == false) {
		extension = util.putChildQuantityList(extension, childQuantityList);
	}
	if (this.sourceList != null && this.sourceList.isEmpty() == false) {
		extension = util.putSourceList(extension, sourceList);
	}
	if (this.destinationList != null && this.destinationList.isEmpty() == false) {
		extension = util.putDestinationList(extension, destinationList);
	}
	if (extension.isEmpty() == false)
		aggregationEvent.put("extension", extension);

	return aggregationEvent;
}