Search in sources :

Example 1 with MongoSKDocument

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);
}
Also used : SubscriberHelpers(fr.romitou.mongosk.SubscriberHelpers) BSONException(org.bson.BSONException) Document(org.bson.Document) MongoSKDocument(fr.romitou.mongosk.elements.MongoSKDocument) JsonParseException(org.bson.json.JsonParseException) MongoSKDatabase(fr.romitou.mongosk.elements.MongoSKDatabase)

Example 2 with MongoSKDocument

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;
}
Also used : CodecConfigurationException(org.bson.codecs.configuration.CodecConfigurationException) MongoSKDocument(fr.romitou.mongosk.elements.MongoSKDocument) Document(org.bson.Document) MongoSKDocument(fr.romitou.mongosk.elements.MongoSKDocument) BsonDocument(org.bson.BsonDocument)

Example 3 with MongoSKDocument

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;
    }
}
Also used : ArrayList(java.util.ArrayList) MongoSKDocument(fr.romitou.mongosk.elements.MongoSKDocument)

Example 4 with MongoSKDocument

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];
    }
}
Also used : MongoSKDocument(fr.romitou.mongosk.elements.MongoSKDocument)

Example 5 with MongoSKDocument

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);
}
Also used : Document(org.bson.Document) MongoSKCollection(fr.romitou.mongosk.elements.MongoSKCollection) Name(ch.njol.skript.doc.Name) Event(org.bukkit.event.Event) Changer(ch.njol.skript.classes.Changer) ExpressionType(ch.njol.skript.lang.ExpressionType) Since(ch.njol.skript.doc.Since) Expression(ch.njol.skript.lang.Expression) Examples(ch.njol.skript.doc.Examples) UpdateResult(com.mongodb.client.result.UpdateResult) LoggerHelper(fr.romitou.mongosk.LoggerHelper) MongoSKFilter(fr.romitou.mongosk.elements.MongoSKFilter) Skript(ch.njol.skript.Skript) Description(ch.njol.skript.doc.Description) Kleenean(ch.njol.util.Kleenean) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) SubscriberHelpers(fr.romitou.mongosk.SubscriberHelpers) MongoSKDocument(fr.romitou.mongosk.elements.MongoSKDocument) SkriptParser(ch.njol.skript.lang.SkriptParser) List(java.util.List) CollectionUtils(ch.njol.util.coll.CollectionUtils) MongoSKQuery(fr.romitou.mongosk.elements.MongoSKQuery) DeleteResult(com.mongodb.client.result.DeleteResult) SimpleExpression(ch.njol.skript.lang.util.SimpleExpression) MongoSKQuery(fr.romitou.mongosk.elements.MongoSKQuery) SubscriberHelpers(fr.romitou.mongosk.SubscriberHelpers) MongoSKDocument(fr.romitou.mongosk.elements.MongoSKDocument) Document(org.bson.Document) MongoSKDocument(fr.romitou.mongosk.elements.MongoSKDocument)

Aggregations

MongoSKDocument (fr.romitou.mongosk.elements.MongoSKDocument)9 SubscriberHelpers (fr.romitou.mongosk.SubscriberHelpers)4 Document (org.bson.Document)4 DeleteResult (com.mongodb.client.result.DeleteResult)3 UpdateResult (com.mongodb.client.result.UpdateResult)3 MongoSKCollection (fr.romitou.mongosk.elements.MongoSKCollection)2 MongoSKQuery (fr.romitou.mongosk.elements.MongoSKQuery)2 BSONException (org.bson.BSONException)2 BsonDocument (org.bson.BsonDocument)2 JsonParseException (org.bson.json.JsonParseException)2 Skript (ch.njol.skript.Skript)1 Changer (ch.njol.skript.classes.Changer)1 Description (ch.njol.skript.doc.Description)1 Examples (ch.njol.skript.doc.Examples)1 Name (ch.njol.skript.doc.Name)1 Since (ch.njol.skript.doc.Since)1 Expression (ch.njol.skript.lang.Expression)1 ExpressionType (ch.njol.skript.lang.ExpressionType)1 SkriptParser (ch.njol.skript.lang.SkriptParser)1 SimpleExpression (ch.njol.skript.lang.util.SimpleExpression)1