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

The following examples show how to use org.bson.BsonDocument#getArray() . 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: ResultDecoders.java    From stitch-android-sdk with Apache License 2.0 5 votes vote down vote up
public RemoteInsertManyResult decode(
    final BsonReader reader,
    final DecoderContext decoderContext
) {
  final BsonDocument document = (new BsonDocumentCodec()).decode(reader, decoderContext);
  keyPresent(Fields.INSERTED_IDS_FIELD, document);
  final BsonArray arr = document.getArray(Fields.INSERTED_IDS_FIELD);
  final Map<Long, BsonValue> insertedIds = new HashMap<>();
  for (int i = 0; i < arr.size(); i++) {
    insertedIds.put((long) i, arr.get(i));
  }

  return new RemoteInsertManyResult(insertedIds);
}
 
Example 2
Source File: UpdateDescription.java    From stitch-android-sdk with Apache License 2.0 5 votes vote down vote up
/**
 * Converts an update description BSON document from a MongoDB Change Event into an
 * UpdateDescription object.
 *
 * @param document the
 * @return the converted UpdateDescription
 */
public static UpdateDescription fromBsonDocument(final BsonDocument document) {
  keyPresent(Fields.UPDATED_FIELDS_FIELD, document);
  keyPresent(Fields.REMOVED_FIELDS_FIELD, document);

  final BsonArray removedFieldsArr =
      document.getArray(Fields.REMOVED_FIELDS_FIELD);
  final Set<String> removedFields = new HashSet<>(removedFieldsArr.size());
  for (final BsonValue field : removedFieldsArr) {
    removedFields.add(field.asString().getValue());
  }

  return new UpdateDescription(document.getDocument(Fields.UPDATED_FIELDS_FIELD), removedFields);
}
 
Example 3
Source File: ResultDecoders.java    From stitch-android-sdk with Apache License 2.0 5 votes vote down vote up
@Override
public FcmSendMessageResult decode(
    final BsonReader reader,
    final DecoderContext decoderContext
) {
  final BsonDocument document = (new BsonDocumentCodec()).decode(reader, decoderContext);
  keyPresent(Fields.SUCCESSES_FIELD, document);
  keyPresent(Fields.FAILURES_FIELD, document);

  final List<FcmSendMessageResultFailureDetail> failureDetails;
  if (document.containsKey(Fields.FAILURE_DETAILS_FIELD)) {
    final BsonArray detailsArr = document.getArray(Fields.FAILURE_DETAILS_FIELD);
    failureDetails = new ArrayList<>(detailsArr.size());
    for (final BsonValue detail: detailsArr) {
      final BsonDocument detailDoc = detail.asDocument();
      final String userId;
      if (detailDoc.containsKey(Fields.FAILURE_DETAIL_USER_ID_FIELD)) {
        userId = detailDoc.getString(Fields.FAILURE_DETAIL_USER_ID_FIELD).getValue();
      } else {
        userId = null;
      }
      failureDetails.add(new FcmSendMessageResultFailureDetail(
          detailDoc.getNumber(Fields.FAILURE_DETAIL_INDEX_FIELD).longValue(),
          detailDoc.getString(Fields.FAILURE_DETAIL_ERROR_FIELD).getValue(),
          userId));
    }
  } else {
    failureDetails = null;
  }

  return new FcmSendMessageResult(
      document.getNumber(Fields.SUCCESSES_FIELD).longValue(),
      document.getNumber(Fields.FAILURES_FIELD).longValue(),
      failureDetails);
}
 
Example 4
Source File: MongoReaderUtil.java    From epcis with Apache License 2.0 5 votes vote down vote up
static EPCISEventExtensionType putEPCISEventExtensionType(BsonDocument dbObject, int zone) {
	EPCISEventExtensionType eeet = new EPCISEventExtensionType();
	if (dbObject.get("eventID") != null) {
		eeet.setEventID(dbObject.getString("eventID").getValue());
	} else {
		if (dbObject.containsKey("_id")) {
			eeet.setEventID(dbObject.getObjectId("_id").getValue().toHexString());
		}
	}
	if (dbObject.get("errorDeclaration") != null) {
		ErrorDeclarationType edt = new ErrorDeclarationType();
		BsonDocument error = dbObject.getDocument("errorDeclaration");
		if (error.containsKey("declarationTime")) {
			edt.setDeclarationTime(getXMLGregorianCalendar(error.getDateTime("declarationTime")));
		}
		if (error.containsKey("reason")) {
			edt.setReason(error.getString("reason").getValue());
		}
		if (error.containsKey("correctiveEventIDs")) {
			BsonArray correctiveEventIDs = error.getArray("correctiveEventIDs");
			List<String> correctiveIDs = new ArrayList<String>();
			Iterator<BsonValue> cIDIterator = correctiveEventIDs.iterator();
			while (cIDIterator.hasNext()) {
				String cID = cIDIterator.next().asString().getValue();
				correctiveIDs.add(cID);
			}
			if (correctiveIDs.size() != 0) {
				CorrectiveEventIDsType ceit = new CorrectiveEventIDsType();
				ceit.setCorrectiveEventID(correctiveIDs);
				edt.setCorrectiveEventIDs(ceit);
			}
		}
		if (error.containsKey("any")) {
			edt.setAny(putAny(error.getDocument("any"), null));
		}
		eeet.setErrorDeclaration(edt);
	}
	return eeet;
}
 
Example 5
Source File: MongoReaderUtil.java    From epcis with Apache License 2.0 5 votes vote down vote up
static EPCISEventExtensionType putEPCISEventExtensionType(BsonDocument dbObject, int zone) {
	EPCISEventExtensionType eeet = new EPCISEventExtensionType();
	if (dbObject.get("eventID") != null) {
		eeet.setEventID(dbObject.getString("eventID").getValue());
	} else {
		if (dbObject.containsKey("_id")) {
			eeet.setEventID(dbObject.getString("_id").getValue());
		}
	}
	if (dbObject.get("errorDeclaration") != null) {
		ErrorDeclarationType edt = new ErrorDeclarationType();
		BsonDocument error = dbObject.getDocument("errorDeclaration");
		if (error.containsKey("declarationTime")) {
			edt.setDeclarationTime(getXMLGregorianCalendar(error.getDateTime("declarationTime")));
		}
		if (error.containsKey("reason")) {
			edt.setReason(error.getString("reason").getValue());
		}
		if (error.containsKey("correctiveEventIDs")) {
			BsonArray correctiveEventIDs = error.getArray("correctiveEventIDs");
			List<String> correctiveIDs = new ArrayList<String>();
			Iterator<BsonValue> cIDIterator = correctiveEventIDs.iterator();
			while (cIDIterator.hasNext()) {
				String cID = cIDIterator.next().asString().getValue();
				correctiveIDs.add(cID);
			}
			if (correctiveIDs.size() != 0) {
				CorrectiveEventIDsType ceit = new CorrectiveEventIDsType();
				ceit.setCorrectiveEventID(correctiveIDs);
				edt.setCorrectiveEventIDs(ceit);
			}
		}
		if (error.containsKey("any")) {
			edt.setAny(putAny(error.getDocument("any"), null));
		}
		eeet.setErrorDeclaration(edt);
	}
	return eeet;
}
 
Example 6
Source File: RecordToBson.java    From octarine with Apache License 2.0 5 votes vote down vote up
@Test public void
convert_record_with_sub_record_to_bson() {
    Record person = Record.of(
            Person.name.of("Dominic"),
            Person.age.of(39),
            Person.address.of(Address.addressLines.of(ADDRESS_LINES)));

    BsonRecordSerialiser addressSerialiser = BsonRecordSerialiser.builder()
            .writeList(Address.addressLines, BsonSerialisers.toString)
            .get();

    BsonRecordSerialiser serializer = BsonRecordSerialiser.builder()
            .writeString(Person.name)
            .writeInteger(Person.age)
            .write(Person.address, addressSerialiser)
            .get();

    BsonDocument doc = (BsonDocument) serializer.apply(person);
    assertEquals("Invalid name", "Dominic", doc.getString("name").getValue());
    assertEquals("invalid age", 39, doc.getInt32("age").getValue());
    BsonDocument bsonAddress = doc.getDocument("address");
    assertNotNull("address was null", bsonAddress);
    BsonArray bsonLines = bsonAddress.getArray("addressLines");
    assertNotNull("addressLines was null", bsonLines);
    List<String> bsonList = bsonLines.getValues().stream().map(v -> ((BsonString) v).getValue()).collect(Collectors.toList());
    assertEquals("Address contents don't match", ADDRESS_LINES, bsonList);
}