use of fr.romitou.mongosk.elements.MongoSKDocument in project MongoSK by Romitou.
the class ExprMongoCommandResult method get.
@Override
protected MongoSKDocument[] get(@Nonnull Event e) {
String jsonString = exprJsonString.getSingle(e);
MongoSKDatabase mongoSKDatabase = exprMongoSKDatabase.getSingle(e);
if (mongoSKDatabase == null)
return new MongoSKDocument[0];
SubscriberHelpers.ObservableSubscriber<Document> documentObservableSubscriber = new SubscriberHelpers.OperationSubscriber<>();
try {
mongoSKDatabase.getMongoDatabase().runCommand(Document.parse(jsonString).toBsonDocument()).subscribe(documentObservableSubscriber);
} catch (BSONException | JsonParseException ex) {
LoggerHelper.severe("An error occurred when changing the document's JSON: " + ex.getMessage(), "Provided JSON: " + jsonString, "Please check its validity on https://jsonlint.com.");
}
// Nobody has subscribed. :(
if (documentObservableSubscriber.getSubscription() == null)
return new MongoSKDocument[0];
return documentObservableSubscriber.get().stream().map(MongoSKDocument::new).toArray(MongoSKDocument[]::new);
}
use of fr.romitou.mongosk.elements.MongoSKDocument in project MongoSK by Romitou.
the class MongoSKAdapter method serializeObject.
public static Object serializeObject(Object unsafeObject) {
if (unsafeObject == null)
return null;
if (unsafeObject instanceof MongoSKDocument)
return ((MongoSKDocument) unsafeObject).getBsonDocument();
LoggerHelper.debug("Searching codec for " + unsafeObject.getClass() + " class...");
MongoSKCodec<Object> codec = MongoSKAdapter.getCodecByClass(unsafeObject.getClass());
if (codec == null) {
try {
MongoClientSettings.getDefaultCodecRegistry().get(unsafeObject.getClass());
} catch (CodecConfigurationException ignore) {
LoggerHelper.debug("No codec found for this class. " + (SAFE_DESERIALIZATION ? "It has been removed from the document." : "No changes have been made to it."));
return SAFE_DESERIALIZATION ? null : unsafeObject;
}
LoggerHelper.debug("The Mongo driver directly supports this type. Next!");
// We're safe!
return unsafeObject;
}
LoggerHelper.debug("A codec has been found: " + codec.getName());
Document serializedDocument = codec.serialize(unsafeObject);
serializedDocument.put(DOCUMENT_FIELD, codec.getName());
LoggerHelper.debug("Result of the serialization: ", "Initial object: " + unsafeObject, "Serialized document: " + serializedDocument.toJson());
return serializedDocument;
}
use of fr.romitou.mongosk.elements.MongoSKDocument in project MongoSK by Romitou.
the class ExprMongoDocumentField method change.
@Override
public void change(@Nonnull final Event e, Object[] delta, @Nonnull Changer.ChangeMode mode) {
String fieldName = exprFieldName.getSingle(e);
MongoSKDocument mongoSKDocument = exprMongoSKDocument.getSingle(e);
List<?> omega = new ArrayList<>();
if (delta != null)
omega = Arrays.asList(MongoSKAdapter.serializeArray(delta));
if (fieldName == null || mongoSKDocument == null || mongoSKDocument.getBsonDocument() == null)
return;
switch(mode) {
case ADD:
try {
ArrayList<Object> addList = new ArrayList<>(mongoSKDocument.getBsonDocument().getList(fieldName, Object.class));
addList.addAll(omega);
mongoSKDocument.getBsonDocument().put(fieldName, addList);
} catch (NullPointerException ex) {
mongoSKDocument.getBsonDocument().put(fieldName, omega);
} catch (RuntimeException ex) {
reportException("adding objects", fieldName, mongoSKDocument, omega, ex);
}
break;
case SET:
try {
mongoSKDocument.getBsonDocument().put(fieldName, isSingle ? omega.get(0) : omega);
} catch (RuntimeException ex) {
reportException("setting field", fieldName, mongoSKDocument, omega, ex);
}
break;
case REMOVE:
try {
ArrayList<Object> removeList = new ArrayList<>(mongoSKDocument.getBsonDocument().getList(fieldName, Object.class));
removeList.removeAll(omega);
mongoSKDocument.getBsonDocument().put(fieldName, removeList);
} catch (RuntimeException ex) {
reportException("removing objects", fieldName, mongoSKDocument, omega, ex);
}
break;
case DELETE:
try {
mongoSKDocument.getBsonDocument().remove(fieldName);
} catch (RuntimeException ex) {
reportException("deleting field", fieldName, mongoSKDocument, omega, ex);
}
break;
default:
break;
}
}
use of fr.romitou.mongosk.elements.MongoSKDocument in project MongoSK by Romitou.
the class ExprMongoDocumentField method get.
@Override
protected Object[] get(@Nonnull final Event e) {
String fieldName = exprFieldName.getSingle(e);
MongoSKDocument mongoSKDocument = exprMongoSKDocument.getSingle(e);
if (fieldName == null || mongoSKDocument == null || mongoSKDocument.getBsonDocument() == null)
return new Object[0];
if (!mongoSKDocument.getBsonDocument().containsKey(fieldName)) {
LoggerHelper.debug("The specified field does not exist in the document.", "Document: " + mongoSKDocument.getBsonDocument().toJson(), "Keys: " + mongoSKDocument.getBsonDocument().keySet());
return new Object[0];
}
try {
if (isSingle) {
Object value = mongoSKDocument.getBsonDocument().get(fieldName);
return new Object[] { MongoSKAdapter.deserializeValue(value) };
}
List<Object> values = mongoSKDocument.getBsonDocument().getList(fieldName, Object.class);
return MongoSKAdapter.deserializeValues(values.toArray());
} catch (ClassCastException ex) {
LoggerHelper.severe("The type of item you are querying is not correct. " + "This can happen if you want to retrieve a list, but it is a single value.", "Document: " + mongoSKDocument.getBsonDocument().toJson(), "Exception: " + ex.getMessage());
return new Object[0];
}
}
use of fr.romitou.mongosk.elements.MongoSKDocument in project MongoSK by Romitou.
the class ExprMongoFindResult method get.
@Override
protected MongoSKDocument[] get(@Nonnull final Event e) {
MongoSKQuery query = buildQuery(e);
if (query == null || query.getMongoSKCollection() == null)
return new MongoSKDocument[0];
long getQuery = System.currentTimeMillis();
SubscriberHelpers.ObservableSubscriber<Document> observableSubscriber = new SubscriberHelpers.OperationSubscriber<>();
if (isFirstDocument)
query.buildFindPublisher().first().subscribe(observableSubscriber);
else
query.buildFindPublisher().subscribe(observableSubscriber);
List<Document> documents = observableSubscriber.get();
LoggerHelper.debug("Simple get query executed in " + (System.currentTimeMillis() - getQuery) + "ms.");
return documents.stream().map(document -> new MongoSKDocument(document, query.getMongoSKCollection())).toArray(MongoSKDocument[]::new);
}
Aggregations