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

org.jboss.as.console.client.shared.general.validation.AbstractValidationStep Maven / Gradle / Ivy

Go to download

Bundles the core AS7 console as a GWT module. Includes minor customizations to support extensions.

There is a newer version: 0.7.0.Final
Show newest version
package org.jboss.as.console.client.shared.general.validation;

import com.google.gwt.autobean.shared.AutoBean;
import com.google.gwt.autobean.shared.AutoBeanUtils;
import org.jboss.as.console.client.shared.general.model.Interface;
import org.jboss.ballroom.client.widgets.forms.FormItem;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @author Heiko Braun
 * @date 11/16/11
 */
abstract class AbstractValidationStep implements ValidationStep {

    static Decision SUCCESS = new Decision() {
        @Override
        public boolean evaluate(Interface entity) {
            return true;
        }
    };
    static Decision FAILURE = new Decision() {
        @Override
        public boolean evaluate(Interface entity) {
            return false;
        }
    };

    private DecisionTree.DecisionLog decisionLog = null;

    public void setLog(DecisionTree.DecisionLog decisionLog) {
        this.decisionLog = decisionLog;
    }

    @Override
    public ValidationResult validate(T entity, Map changedValues) {

        DecisionTree tree = buildDecisionTree(entity, changedValues);
        tree.setDecisionLog(decisionLog);

        //tree.outputBinTree();
        tree.queryBinTree();

        // create result
        ValidationResult result = new ValidationResult(tree.getFinalOutcome());
        result.addMessage(tree.getLastNode().getQuestOrAns());
        return result;
    }

    protected abstract DecisionTree buildDecisionTree(T entity, Map changedValues);

    public static boolean isSet(String value)
    {
        return value!=null && !value.isEmpty();
    }

    protected static Map clearChangeset(Map changedValues) {

        Map clean = new HashMap();
        Set keys = changedValues.keySet();
        for(String key : keys)
        {
            Object value = changedValues.get(key);
            if(! (value == FormItem.VALUE_SEMANTICS.UNDEFINED))
                clean.put(key, value);
        }
        return clean;
    }

    protected Map asProperties(T entity) {
        AutoBean autoBean = AutoBeanUtils.getAutoBean(entity);
        if(null==autoBean)
            throw new RuntimeException("Not an auto bean: "+entity.getClass());

        return AutoBeanUtils.getAllProperties(autoBean);
    }

    protected static boolean isEmpty(Map changedValues) {

        changedValues.remove("name"); // default

        boolean empty = changedValues.isEmpty();

        if(!empty)
        {
            // treat any boolean=false as empty too
            // it will written as undefined
            boolean conflictingItem = false;
            Set keys = changedValues.keySet();
            for(String key : keys)
            {
                Object value = changedValues.get(key);
                if(value instanceof Boolean)
                {
                    conflictingItem  = (Boolean)value; // any boolean=true values are considered changes
                    if(conflictingItem)
                    {
                        //System.out.println(key + " is conflicting");
                        break;
                    }
                }
                else if(value instanceof String)
                {
                    conflictingItem  = !((String) value).isEmpty(); // any non empty values are considered changes
                    if(conflictingItem)
                    {
                        //System.out.println(key + " is conflicting");
                        break;
                    }
                }
            }

            empty = !conflictingItem;
        }

        return empty;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy