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

io.vertx.tp.crud.init.IxValidator Maven / Gradle / Ivy

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

import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.vertx.tp.crud.cv.IxFolder;
import io.vertx.tp.crud.refine.Ix;
import io.vertx.up.atom.Rule;
import io.vertx.up.log.Annal;
import io.vertx.zero.eon.FileSuffix;
import io.vertx.zero.eon.Strings;
import io.zero.epic.Ut;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/*
 * Post validation in worker here
 * Because this module is dynamic inject rules for validation, here could not be
 * implemented with Zero @Codex, instead it should be implemented with extension.
 * But the configuration file format is the same as default @Codex.
 */
class IxValidator {

    private static final Annal LOGGER = Annal.get(IxValidator.class);
    /*
     * uri -> field1 = List
     *        field2 = List
     */
    private static final ConcurrentMap>> RULE_MAP =
            new ConcurrentHashMap<>();

    static void init() {
        final List files = Ut.ioFiles(IxFolder.VALIDATOR, FileSuffix.YML);
        files.forEach(file -> {
            /* 1. Validator file under classpath */
            final String path = IxFolder.VALIDATOR + file;
            /* 2. JsonArray process */
            final JsonObject rules = Ut.ioYaml(path);
            final ConcurrentMap> ruleMap = new ConcurrentHashMap<>();
            rules.fieldNames().forEach(field -> {
                /* 3. Rule List */
                final JsonArray ruleArray = rules.getJsonArray(field);
                ruleMap.put(field, getRules(ruleArray));
            });
            /* 4. Append rules */
            final String key = file.replace(Strings.DOT + FileSuffix.YML, Strings.EMPTY);

            /* 4. Logger */
            Ix.infoInit(LOGGER, "--- file = {0}, key = {1}", path, key);
            RULE_MAP.put(key, ruleMap);
        });
        Ix.infoInit(LOGGER, "IxValidator Finished ! Size = {0}", RULE_MAP.size());
    }

    private static List getRules(final JsonArray ruleArray) {
        final List ruleList = new ArrayList<>();
        Ut.itJArray(ruleArray, (item, index) -> {
            final Rule rule = Rule.create(item);
            ruleList.add(rule);
        });
        return ruleList;
    }

    static ConcurrentMap> getRules(final String actor) {
        ConcurrentMap> rules = RULE_MAP.get(actor);
        if (null == rules) {
            rules = new ConcurrentHashMap<>();
        }
        return rules;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy