CheckRules.java

/*
 *        @ Copyright 2001 FA Software;
 *        All right reserved. No part of this program may be reproduced or
 *        transmitted in any form or by any means, electronic or
 *        mechanical, including photocopying, recording, or by any
 *        information storage or retrieval system without written
 *        permission from FA Software, except for inclusion of brief
 *        quotations in a review.
 */
package com.mycim.valueobject.sys;

import java.util.Map;

public abstract class CheckRules {

    protected final static int CHK_LENGTH = 0;

    protected final static int CHK_REQUIRED = 1;

    protected final static int CHK_NUMERIC = 2;

    protected final static int CHK_DATE = 3;

    protected final static int CHK_TIME = 4;

    protected final static int CHK_RANGE = 5;

    protected final static int CHK_MASK = 6;

    protected final static int CHK_EMAIL = 7;

    protected String text;

    protected String property;

    protected String context;

    protected Map messages;

    public static CheckRules create(String context, String text, String property, Map messages) {
        switch (Integer.parseInt(context.substring(0, 1))) {
            case CHK_LENGTH:
                return new CheckLengthRule(context, text, property, messages);

            case CHK_REQUIRED:
                return new CheckRequiredRule(context, text, property, messages);

            case CHK_NUMERIC:
                return new CheckNumericRule(context, text, property, messages);

            case CHK_DATE:
                return new CheckDateRule(context, text, property, messages);

            case CHK_TIME:
                return new CheckTimeRule(context, text, property, messages);

            case CHK_RANGE:
                return new CheckRangeRule(context, text, property, messages);

            case CHK_MASK:
                return new CheckMaskRule(context, text, property, messages);

            case CHK_EMAIL:
                return new CheckEmailRule(context, text, property, messages);

            default:
                throw new IllegalArgumentException("Incorrect type code value!");
        }
    }

    public abstract String constructCheckMethod();

    private String getMessageContext(int messageType, Map messages) {
        if (messages == null) {
            return null;
        }

        if (messageType == CHK_LENGTH) {
            return (messages.get("MSG_LENGTH_RULE") != null) ? "$1 should smaller than $2" : (String) messages
                    .get("MSG_LENGTH_RULE");
        }

        if (messageType == CHK_REQUIRED) {
            return (messages.get("MSG_REQUIRED_RULE") != null) ? "$1 is required" : (String) messages
                    .get("MSG_REQUIRED_RULE");
        }

        if (messageType == CHK_NUMERIC) {
            return (messages.get("MSG_NUMERIC_RULE") != null) ? "$1 is numeric" : (String) messages
                    .get("MSG_NUMERIC_RULE");
        }

        if (messageType == CHK_DATE) {
            return (messages.get("MSG_DATE_RULE") != null) ? "$1 is date" : (String) messages.get("MSG_DATE_RULE");
        }

        if (messageType == CHK_TIME) {
            return (messages.get("MSG_TIME_RULE") != null) ? "$1 is time" : (String) messages.get("MSG_TIME_RULE");
        }

        if (messageType == CHK_RANGE) {
            return (messages.get("MSG_RANGE_RULE") != null) ? "$1 is in range($2-$3)" : (String) messages
                    .get("MSG_RANGE_RULE");
        }

        if (messageType == CHK_MASK) {
            return (messages.get("MSG_MASK_RULE") != null) ? "$1 is mask" : (String) messages.get("MSG_MASK_RULE");
        }

        if (messageType == CHK_EMAIL) {
            return (messages.get("MSG_EMAIL_RULE") != null) ? "$1 is email" : (String) messages.get("MSG_EMAIL_RULE");
        }

        // if(messageType.equals(CHK_DELETE))
        // return (messages.get("MSG_DELETE")!=null)?"Are you sure?":(String)messages.get("MSG_DELETE");
        return null;
    }

    protected String makeMethod(String method, String content) {
        StringBuffer sb = new StringBuffer();
        sb.append("function " + method + "{                \n");
        sb.append("   msg = \"\";                         \n");
        sb.append(content + "\n");
        sb.append("   return msg;                         \n");
        sb.append("}                                      \n");

        return sb.toString();
    }

    protected String getMessage(String lblText, String value, String value1, int messageType, Map messages) {
        if ((messages == null) || (messages.size() == 0)) {
            return null;
        }

        StringBuffer sb = new StringBuffer();
        sb.append(getMessageContext(messageType, messages));
        int pos1 = getMessageContext(messageType, messages).indexOf("$1");
        int pos2 = getMessageContext(messageType, messages).indexOf("$2");
        int pos3 = getMessageContext(messageType, messages).indexOf("$3");

        if (pos3 >= 0) {
            sb.replace(pos3, pos3 + 2, value1);
        }

        if (pos2 >= 0) {
            sb.replace(pos2, pos2 + 2, value);
        }

        if (pos1 >= 0) {
            sb.replace(pos1, pos1 + 2, lblText);
        }

        return sb.toString();
    }

}

class CheckLengthRule extends CheckRules {

    CheckLengthRule(String context, String text, String property, Map messages) {
        this.text = text;
        this.property = property;
        this.context = context;
        this.messages = messages;
    }

    @Override
    public String constructCheckMethod() {
        if ((context.indexOf("(") == -1) || (context.indexOf(")") == -1)) {
            return null;
        }

        StringBuffer sb = new StringBuffer();
        sb.append("try{ if(Trim(form.elements[\"");
        sb.append(property);
        sb.append("\"].value).length>0&&getLength(form.elements[\"");
        sb.append(property);
        sb.append("\"].value)" + ">");
        sb.append(context.substring(context.indexOf("(") + 1, context.indexOf(")")));
        sb.append(")\n msg +=\"");
        sb.append(getMessage(text, context.substring(context.indexOf("(") + 1, context.indexOf(")")), null, CHK_LENGTH,
                             messages));
        sb.append("</br>\";\n}catch(e){}");

        return makeMethod("validateLength(form)", sb.toString());
    }

}

class CheckRequiredRule extends CheckRules {

    CheckRequiredRule(String context, String text, String property, Map messages) {
        this.text = text;
        this.property = property;
        this.context = context;
        this.messages = messages;
    }

    @Override
    public String constructCheckMethod() {
        StringBuffer sb = new StringBuffer();
        sb.append("try{ if(Trim(form.elements[\"");
        sb.append(property);
        sb.append("\"].value).length==0)\n msg +=\"");
        sb.append(getMessage(text, null, null, CHK_REQUIRED, messages));
        sb.append("</br>\";\n}catch(e){}");

        return makeMethod("validateRequired(form)", sb.toString());
    }

}

class CheckNumericRule extends CheckRules {

    CheckNumericRule(String context, String text, String property, Map messages) {
        this.text = text;
        this.property = property;
        this.context = context;
        this.messages = messages;
    }

    @Override
    public String constructCheckMethod() {
        StringBuffer sb = new StringBuffer();
        sb.append("try{ if(Trim(form.elements[\"");
        sb.append(property);
        sb.append("\"].value).length>0&&!IsNumeric(form.elements[\"");
        sb.append(property);
        sb.append("\"].value))\n msg +=\"");
        sb.append(getMessage(text, null, null, CHK_NUMERIC, messages));
        sb.append("</br>\";\n}catch(e){}");

        return makeMethod("validateNumeric(form)", sb.toString());
    }

}

class CheckDateRule extends CheckRules {

    CheckDateRule(String context, String text, String property, Map messages) {
        this.text = text;
        this.property = property;
        this.context = context;
        this.messages = messages;
    }

    @Override
    public String constructCheckMethod() {
        StringBuffer sb = new StringBuffer();
        sb.append("try{ if(Trim(form.elements[\"");
        sb.append(property);
        sb.append("\"].value).length>0&&!IsDate(form.elements[\"");
        sb.append(property);
        sb.append("\"].value))\n msg +=\"");
        sb.append(getMessage(text, null, null, CHK_DATE, messages));
        sb.append("</br>\";\n}catch(e){}");

        return makeMethod("validateDate(form)", sb.toString());
    }

}

class CheckTimeRule extends CheckRules {

    CheckTimeRule(String context, String text, String property, Map messages) {
        this.text = text;
        this.property = property;
        this.context = context;
        this.messages = messages;
    }

    @Override
    public String constructCheckMethod() {
        StringBuffer sb = new StringBuffer();
        sb.append("try{ if(Trim(form.elements[\"");
        sb.append(property);
        sb.append("\"].value).length>0&&!IsTime(form.elements[\"");
        sb.append(property);
        sb.append("\"].value))\n msg +=\"");
        sb.append(getMessage(text, null, null, CHK_TIME, messages));
        sb.append("</br>\";\n}catch(e){}");

        return makeMethod("validateTime(form)", sb.toString());
    }

}

class CheckRangeRule extends CheckRules {

    CheckRangeRule(String context, String text, String property, Map messages) {
        this.text = text;
        this.property = property;
        this.context = context;
        this.messages = messages;
    }

    @Override
    public String constructCheckMethod() {
        String min = context.substring(context.indexOf("(") + 1, context.indexOf("#"));
        String max = context.substring(context.indexOf("#") + 1, context.indexOf(")"));
        StringBuffer sb = new StringBuffer();

        if (!min.equals("*")) {
            sb.append("try{ if(Trim(form.elements[\"");
            sb.append(property);
            sb.append("\"].value).length>0&&form.elements[\"");
            sb.append(property);
            sb.append("\"].value" + "<");
            sb.append(min);
            sb.append(")\n msg +=\"");
            sb.append(getMessage(text, min, max, CHK_RANGE, messages));
            sb.append("</br>\";\n}catch(e){}");
        } else if (!max.equals("*")) {
            sb.append("try{ if(Trim(form.elements[\"");
            sb.append(property);
            sb.append("\"].value).length>0&&form.elements[\"");
            sb.append(property);
            sb.append("\"].value" + ">");
            sb.append(max);
            sb.append(")\n msg +=\"");
            sb.append(getMessage(text, min, max, CHK_RANGE, messages));
            sb.append("</br>\";\n}catch(e){}");
        }

        return makeMethod("validateRange(form)", sb.toString());
    }

}

class CheckMaskRule extends CheckRules {

    CheckMaskRule(String context, String text, String property, Map messages) {
        this.text = text;
        this.property = property;
        this.context = context;
        this.messages = messages;
    }

    @Override
    public String constructCheckMethod() {
        StringBuffer sb = new StringBuffer();
        sb.append("try{ if(Trim(form.elements[\"");
        sb.append(property);
        sb.append("\"].value).length>0&&(form.elements[\"");
        sb.append(property);
        sb.append("\"].value.search(");
        sb.append(context.substring(context.indexOf("(", 1) + 1, context.length() - 1));
        sb.append(")==-1))\n msg +=\"");
        sb.append(getMessage(text, null, null, CHK_MASK, messages));
        sb.append("</br>\";\n}catch(e){}");

        return makeMethod("validateMask(form)", sb.toString());
    }

}

class CheckEmailRule extends CheckRules {

    CheckEmailRule(String context, String text, String property, Map messages) {
        this.text = text;
        this.property = property;
        this.context = context;
        this.messages = messages;
    }

    @Override
    public String constructCheckMethod() {
        StringBuffer sb = new StringBuffer();
        sb.append("try{ if(Trim(form.elements[\"");
        sb.append(property);
        sb.append("\"].value).length>0&&!IsEmail(form.elements[\"");
        sb.append(property);
        sb.append("\"].value))\n msg +=\"");
        sb.append(getMessage(text, null, null, CHK_EMAIL, messages));
        sb.append("</br>\";\n}catch(e){}");

        return makeMethod("validateEmail(form)", sb.toString());
    }

}