Example usage for io.vertx.core.json JsonObject getValue

List of usage examples for io.vertx.core.json JsonObject getValue

Introduction

In this page you can find the example usage for io.vertx.core.json JsonObject getValue.

Prototype

public Object getValue(String key) 

Source Link

Document

Get the value with the specified key, as an Object with types respecting the limitations of JSON.

Usage

From source file:com.github.jackygurui.vertxredissonrepository.repository.Impl.RedisRepositoryImpl.java

License:Apache License

private void prepareEnsuerIndex(String id, T r, JsonObject instance, RBatch redissonBatch, RedissonIndex index,
        String fieldName, Object fieldValue) throws RuntimeException {
    try {/*from www  . ja  v  a 2s . c o  m*/
        Object value = instance.getValue(fieldName);
        String resolve = index.valueResolver().newInstance().resolve(value, instance, id, fieldName, index);
        if (index.scoreResolver().isAssignableFrom(LexicalScoreResolver.class)) {
            redissonBatch.getLexSortedSet(getScoredSortedIndexKey(fieldName, index.name())).addAsync(resolve);
            if (r != null && !value.equals(fieldValue)) {
                redissonBatch.getLexSortedSet(getScoredSortedIndexKey(fieldName, index.name()))
                        .removeAsync(index.valueResolver().newInstance().resolve(fieldValue, instance, id,
                                fieldName, index));
            }
        } else {
            Double score = index.scoreResolver().newInstance().resolve(value, instance, id, fieldName, index);
            redissonBatch
                    .getScoredSortedSet(getScoredSortedIndexKey(fieldName, index.name()), StringCodec.INSTANCE)
                    .addAsync(score, resolve);
            if (r != null && !value.equals(fieldValue)) {
                redissonBatch
                        .getScoredSortedSet(getScoredSortedIndexKey(fieldName, index.name()),
                                StringCodec.INSTANCE)
                        .removeAsync(index.valueResolver().newInstance().resolve(fieldValue, instance, id,
                                fieldName, index));
            }
        }
    } catch (InstantiationException | IllegalAccessException | RuntimeException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.github.jackygurui.vertxredissonrepository.repository.Impl.RedisRepositoryImpl.java

License:Apache License

private void indexValue(String id, String fieldName, RedissonIndex index, JsonObject instance,
        AsyncResultHandler<Boolean> resultHandler) {
    try {/*from   w  ww  .j  ava  2 s.c o m*/
        Object value = instance.getValue(fieldName);
        String resolve = index.valueResolver().newInstance().resolve(value, instance, id, fieldName, index);
        GenericFutureListener<io.netty.util.concurrent.Future<Boolean>> futureListener = (
                io.netty.util.concurrent.Future<Boolean> future) -> {
            resultHandler
                    .handle(future.isSuccess() && future.get() != null ? Future.succeededFuture(future.get())
                            : Future.failedFuture(future.cause()));
        };
        if (index.scoreResolver().isAssignableFrom(LexicalScoreResolver.class)) {
            redissonOther.getLexSortedSet(getScoredSortedIndexKey(fieldName, index.name())).addAsync(resolve)
                    .addListener(futureListener);
        } else {
            Double score = index.scoreResolver().newInstance().resolve(value, instance, id, fieldName, index);
            redissonOther
                    .getScoredSortedSet(getScoredSortedIndexKey(fieldName, index.name()), StringCodec.INSTANCE)
                    .addAsync(score, resolve).addListener(futureListener);
        }
    } catch (InstantiationException | IllegalAccessException | RuntimeException e) {
        resultHandler.handle(Future.failedFuture(e));
    }
}

From source file:com.github.jackygurui.vertxredissonrepository.repository.Impl.RedisRepositoryImpl.java

License:Apache License

private void unindexValue(String id, String fieldName, RedissonIndex index, JsonObject instance,
        AsyncResultHandler<Boolean> resultHandler) {
    try {//from   ww w  .j ava  2 s . co  m
        Object value = instance.getValue(fieldName);
        String resolve = index.valueResolver().newInstance().resolve(value, instance, id, fieldName, index);
        GenericFutureListener<io.netty.util.concurrent.Future<Boolean>> futureListener = (
                io.netty.util.concurrent.Future<Boolean> future) -> {
            resultHandler
                    .handle(future.isSuccess() && future.get() != null ? Future.succeededFuture(future.get())
                            : Future.failedFuture(future.cause()));
        };
        if (index.scoreResolver().isAssignableFrom(LexicalScoreResolver.class)) {
            redissonOther.getLexSortedSet(getScoredSortedIndexKey(fieldName, index.name())).removeAsync(resolve)
                    .addListener(futureListener);
        } else {
            redissonOther
                    .getScoredSortedSet(getScoredSortedIndexKey(fieldName, index.name()), StringCodec.INSTANCE)
                    .removeAsync(resolve).addListener(futureListener);
        }
    } catch (InstantiationException | IllegalAccessException | RuntimeException e) {
        resultHandler.handle(Future.failedFuture(e));
    }
}

From source file:com.github.jackygurui.vertxredissonrepository.repository.Impl.RedisRepositoryImpl.java

License:Apache License

private void prepareEnsuerCounter(String id, T r, JsonObject instance, Boolean isNew, RBatch redissonBatch,
        RedissonCounter counter, String fieldName, Object fieldValue) throws RuntimeException {
    try {/*from   w w w . j  av a2  s.  c o m*/
        if (isNew) {
            redissonBatch.getAtomicLongAsync(getCounterKey(fieldName, counter.name())).incrementAndGetAsync();
        }
        Object value = instance.getValue(fieldName);
        if (counter.expiryResolver().isAssignableFrom(RedisPreciseTimeExpiryResolver.class)) {
            redissonBatch.getAtomicLongAsync(getCounterKey(fieldName, counter.name())).expireAtAsync(
                    counter.expiryResolver().newInstance().resolve(value, instance, id, fieldName, counter));
        } else if (isNew || counter.resetOnUpdate()) {
            redissonBatch.getAtomicLongAsync(getCounterKey(fieldName, counter.name())).expireAsync(
                    counter.expiryResolver().newInstance().resolve(value, instance, id, fieldName, counter),
                    TimeUnit.MILLISECONDS);
        }
    } catch (InstantiationException | IllegalAccessException | RuntimeException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.github.jackygurui.vertxredissonrepository.repository.Impl.RedisRepositoryImpl.java

License:Apache License

private void persistBlocking(String id, JsonObject data, RBatch redissonBatch,
        Handler<AsyncResult<Boolean>> resultHandler) {
    RBatch batch = redissonBatch == null ? redissonWrite.createBatch() : redissonBatch;
    AtomicBoolean failed = new AtomicBoolean(false);
    try {/*from  w ww . j a  v  a  2 s  . c o  m*/
        BeanMap pMap = new BeanMap(cls.newInstance());
        //remove the indexes;
        if (isRedisEntity()) {
            AtomicBoolean finished = new AtomicBoolean(false);
            AtomicBoolean hasNested = new AtomicBoolean(false);
            AtomicLong stack = new AtomicLong();
            pMap.forEach((k, v) -> {
                if ("class".equals(k)) {
                    return;
                }
                Class<?> type = pMap.getType((String) k);
                if (!isRedisEntity(type)) {
                    //recreate the indexes;
                    if ("id".equals(k)) {
                        batch.getMap(getStorageKey(), StringCodec.INSTANCE).fastPutAsync(id, id);
                    } else {
                        batch.getMap(getStorageKey((String) k)).fastPutAsync(id, data.getValue((String) k));
                    }
                } else {
                    hasNested.set(true);
                    stack.incrementAndGet();
                    RedisRepositoryImpl<?> innerRepo;
                    try {
                        innerRepo = (RedisRepositoryImpl) factory.instance(type);
                    } catch (RepositoryException e) {
                        throw new RuntimeException(e);
                    }
                    JsonObject value = data.getJsonObject((String) k);
                    final boolean newOne = !value.containsKey("id") || value.getString("id") == null
                            || "null".equals(value.getString("id"));
                    final String ID = newOne ? id : value.getString("id");
                    innerRepo.persist(ID, value, batch, c -> {//making the nested entity shares the same id as the parent when its 1:1 relation. This makes fetch a lot faster since it doesn't not need to resolve the reference when fetching 1:1 nested objects.
                        if (c.succeeded()) {
                            long s = stack.decrementAndGet();
                            if (newOne) {
                                batch.getMap(getStorageKey((String) k)).fastPutAsync(id, ID);//different to the update, create needs to add the reference field to batch
                            }
                            if (s == 0 && finished.get() && !failed.get()) { //finished iterating and no outstanding processes. 
                                if (redissonBatch == null) {//if it's not inside a nested process.
                                    finishPersist(id, data, batch, resultHandler);
                                } else {//if it is inside a nested process.
                                    resultHandler.handle(Future.succeededFuture(true));
                                }
                            }
                            //else wait for others to complete
                        } else {
                            boolean firstToFail = failed.compareAndSet(false, true);
                            if (firstToFail) {
                                resultHandler.handle(Future.failedFuture(c.cause()));
                            }
                        }
                    });
                }
            });
            batch.getAtomicLongAsync(getCounterKey()).incrementAndGetAsync();
            finished.set(true);
            if (!hasNested.get()) {//does not have nested RedissonEntity within
                if (redissonBatch == null) {//if it's not inside a nested process.
                    finishPersist(id, data, batch, resultHandler);
                } else {//if it is inside a nested process.
                    resultHandler.handle(Future.succeededFuture(true));
                }
            }
        } else {//not a RedissonEntity class, persist as json string.
            //recreate the indexes;
            batch.<String, String>getMap(getStorageKey(), StringCodec.INSTANCE).fastPutAsync(id,
                    Json.encode(data));
            batch.getAtomicLongAsync(getCounterKey()).incrementAndGetAsync();
            if (redissonBatch == null) {//if it's not inside a nested process.
                finishPersist(id, data, batch, resultHandler);
            } else {//if it is inside a nested process.
                resultHandler.handle(Future.succeededFuture(true));
            }
        }
    } catch (InstantiationException | IllegalAccessException | RuntimeException ex) {
        failed.set(true);
        resultHandler.handle(Future.failedFuture(ex));
    }
}

From source file:com.github.jackygurui.vertxredissonrepository.repository.index.DefaultCompoundValueResolver.java

License:Apache License

@Override
public String resolve(Object value, JsonObject root, String id, String fieldName, RedissonIndex index) {
    AtomicReference<String> s = new AtomicReference<>();
    if (StringUtils.isBlank(index.valueField())) {
        s.set(value instanceof String ? (String) value : Json.encode(value));
    } else {//  w ww .  ja  va  2 s. co m
        s.set(root.getString(index.valueField()));
    }
    Arrays.stream(index.compoundIndexFields()).forEach(
            e -> s.set(s.get().concat(RedisRepository.DEFAULT_SEPERATOR).concat(root.getValue(e).toString())));
    return s.get().concat(RedisRepository.DEFAULT_SEPERATOR).concat(id);
}

From source file:com.github.meshuga.vertx.neo4j.rbac.auth.AuthServiceVertxProxyHandler.java

License:Apache License

public void handle(Message<JsonObject> msg) {
    JsonObject json = msg.body();
    String action = msg.headers().get("action");
    if (action == null) {
        throw new IllegalStateException("action not specified");
    }//from   w  w  w .  j a v a 2 s .co m
    accessed();
    switch (action) {

    case "hasPermission": {
        service.hasPermission((java.lang.String) json.getValue("userName"),
                (java.lang.String) json.getValue("permission"), createHandler(msg));
        break;
    }
    default: {
        throw new IllegalStateException("Invalid action: " + action);
    }
    }
}

From source file:com.github.vertx.node.example.HelloWorldServiceInterfaceVertxProxyHandler.java

License:Apache License

public void handle(Message<JsonObject> msg) {
    try {//  w w  w  .  jav  a  2 s .c om
        JsonObject json = msg.body();
        String action = msg.headers().get("action");
        if (action == null) {
            throw new IllegalStateException("action not specified");
        }
        accessed();
        switch (action) {

        case "hello": {
            service.hello((java.lang.String) json.getValue("message"), createHandler(msg));
            break;
        }
        case "close": {
            service.close();
            close();
            break;
        }
        default: {
            throw new IllegalStateException("Invalid action: " + action);
        }
        }
    } catch (Throwable t) {
        msg.fail(-1, t.getMessage());
        throw t;
    }
}

From source file:com.glt.cronjob.JobServiceVertxProxyHandler.java

License:Apache License

public void handle(Message<JsonObject> msg) {
    JsonObject json = msg.body();
    String action = msg.headers().get("action");
    if (action == null) {
        throw new IllegalStateException("action not specified");
    }/*from w  w  w  .  j  a  va  2s  . co  m*/
    accessed();
    switch (action) {

    case "close": {
        service.close();
        break;
    }
    case "schedule": {
        service.schedule((io.vertx.core.json.JsonObject) json.getValue("jobDescriptor"), createHandler(msg));
        break;
    }
    case "unschedule": {
        service.unschedule((io.vertx.core.json.JsonObject) json.getValue("jobDescriptor"), createHandler(msg));
        break;
    }
    default: {
        throw new IllegalStateException("Invalid action: " + action);
    }
    }
}

From source file:com.glt.rest.client.RestServiceVertxProxyHandler.java

License:Apache License

public void handle(Message<JsonObject> msg) {
    JsonObject json = msg.body();
    String action = msg.headers().get("action");
    if (action == null) {
        throw new IllegalStateException("action not specified");
    }/*  w  w w  . j  a va  2  s  .co  m*/
    accessed();
    switch (action) {

    case "get": {
        service.get((io.vertx.core.json.JsonObject) json.getValue("command"), createHandler(msg));
        break;
    }
    case "post": {
        service.post((io.vertx.core.json.JsonObject) json.getValue("command"), createHandler(msg));
        break;
    }
    case "put": {
        service.put((io.vertx.core.json.JsonObject) json.getValue("command"), createHandler(msg));
        break;
    }
    case "delete": {
        service.delete((io.vertx.core.json.JsonObject) json.getValue("command"), createHandler(msg));
        break;
    }
    case "getJson": {
        service.getJson((io.vertx.core.json.JsonObject) json.getValue("command"), createHandler(msg));
        break;
    }
    case "postJson": {
        service.postJson((io.vertx.core.json.JsonObject) json.getValue("command"), createHandler(msg));
        break;
    }
    case "putJson": {
        service.putJson((io.vertx.core.json.JsonObject) json.getValue("command"), createHandler(msg));
        break;
    }
    case "deleteJson": {
        service.deleteJson((io.vertx.core.json.JsonObject) json.getValue("command"), createHandler(msg));
        break;
    }
    case "request": {
        service.request((io.vertx.core.json.JsonObject) json.getValue("command"), createHandler(msg));
        break;
    }
    default: {
        throw new IllegalStateException("Invalid action: " + action);
    }
    }
}