All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.vertx.tp.crud.actor.UniqueActor Maven / Gradle / Ivy

There is a newer version: 0.9.0
Show newest version
package io.vertx.tp.crud.actor;

import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.vertx.tp.crud.atom.IxConfig;
import io.vertx.tp.crud.atom.IxField;
import io.vertx.tp.crud.refine.Ix;
import io.vertx.zero.eon.Strings;
import io.vertx.zero.eon.Values;
import io.zero.epic.Ut;

class UniqueActor extends AbstractActor {

    @Override
    public JsonObject proc(final JsonObject data, final IxConfig config) {
        /* Unique Keys */
        final IxField field = config.getField();
        final JsonArray unique = field.getUnique();
        /* Each Unique */
        final JsonObject filters = new JsonObject();
        if (Values.ONE == unique.size()) {
            final JsonArray fields = unique.getJsonArray(Values.IDX);
            filters.mergeIn(this.getFilters(fields, data));
        } else {
            filters.put(Strings.EMPTY, Boolean.FALSE);
            Ut.itJArray(unique, JsonArray.class,
                    (each, index) -> filters.put("$" + index, this.getFilters(each, data)));
        }
        Ix.infoFilters(this.getLogger(), "\n{0}", filters.encodePrettily());
        return filters;
    }

    private JsonObject getFilters(final JsonArray fields, final JsonObject data) {
        final JsonObject filters = new JsonObject();
        filters.put(Strings.EMPTY, Boolean.TRUE);
        Ut.itJArray(fields, String.class, (field, index) -> {
            final Object value = data.getValue(field);
            filters.put(field, value);
        });
        return filters;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy