org.opentestsystem.delivery.testreg.domain.ImplicitEligibilityRule.java Source code

Java tutorial

Introduction

Here is the source code for org.opentestsystem.delivery.testreg.domain.ImplicitEligibilityRule.java

Source

/*
Educational Online Test Delivery System Copyright (c) 2013 American Institutes for Research
    
Distributed under the AIR Open Source License, Version 1.0 See accompanying file AIR-License-1_0.txt or at
http://www.smarterapp.org/documents/American_Institutes_for_Research_Open_Source_Software_License.pdf
 */

package org.opentestsystem.delivery.testreg.domain;

import static org.apache.commons.lang.StringUtils.isNotEmpty;

import java.io.Serializable;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.TimeZone;

import javax.validation.constraints.NotNull;

import org.hibernate.validator.constraints.NotBlank;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.opentestsystem.delivery.testreg.domain.constraints.Ascii;
import org.opentestsystem.delivery.testreg.domain.constraints.JexlAssert;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@SuppressWarnings({ "PMD.ShortVariable" })
@JsonIgnoreProperties(ignoreUnknown = true)
@JexlAssert.List({
        @JexlAssert(alias = "_", expression = "if(_.getField() !=null && _.getField().equalsIgnoreCase('externalSsid')) exAscii.matcher(_.getValue()).matches() else true", message = "{ImplicitEligibilityRule.value.alphanumeric}"),

        @JexlAssert(alias = "_", expression = "if(_.getField() !=null && !_.getField().equalsIgnoreCase('externalSsid')) ascii.matcher(_.getValue()).matches() else true", message = "{ImplicitEligibilityRule.value.alphanumeric}") })
public class ImplicitEligibilityRule implements Serializable {

    private static final long serialVersionUID = -160139093143574051L;

    private static final String DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSX";

    @NotBlank(message = "{ImplicitEligibilityRule.field.required}")
    private String field;

    @NotBlank(message = "{ImplicitEligibilityRule.value.required}")
    @Ascii(isExtended = true, message = "{ImplicitEligibilityRule.value.alphanumeric}")
    private String value;

    @NotNull(message = "{ImplicitEligibilityRule.operatorType.required}")
    private OperatorType operatorType;

    @NotNull(message = "{ImplicitEligibilityRule.ruleType.required}")
    private RuleType ruleType;

    public enum OperatorType {
        EQUALS(String.class, Enum.class, Number.class, DateTime.class), GREATER_THAN(Number.class,
                DateTime.class), LESS_THAN(Number.class, DateTime.class), GREATER_THAN_EQUALS(Number.class,
                        DateTime.class), LESS_THAN_EQUALS(Number.class, DateTime.class);

        private final Set<Class<?>> validTypes = new HashSet<Class<?>>();

        private static Map<String, OperatorType> operatorTypeMap = new HashMap<String, OperatorType>();

        static {
            operatorTypeMap.put("=", EQUALS);
            operatorTypeMap.put(">", GREATER_THAN);
            operatorTypeMap.put("<", LESS_THAN);
            operatorTypeMap.put(">=", GREATER_THAN_EQUALS);
            operatorTypeMap.put("<=", LESS_THAN_EQUALS);
        }

        private OperatorType(final Class<?>... validType) {
            this.validTypes.addAll(Arrays.asList(validType));
        }

        @JsonCreator
        public static OperatorType forValue(final String value) {
            return operatorTypeMap.get(value);
        }

        public boolean isValidFor(final Class<?> clazz) {

            boolean isValid = false;

            for (final Class<?> cls : this.validTypes) {
                if (cls.isAssignableFrom(clazz)) {
                    isValid = true;
                    break;
                }
            }

            return isValid;
        }

    }

    public enum RuleType {
        ENABLER, DISABLER;

        private static Map<String, RuleType> ruleTypeMap = new HashMap<String, RuleType>();
        static {
            ruleTypeMap.put("ENABLER", ENABLER);
            ruleTypeMap.put("DISABLER", DISABLER);
        }

        @JsonCreator
        public static RuleType forValue(final String value) {
            return ruleTypeMap.get(value);
        }

    }

    public ImplicitEligibilityRule() {
        this(null, null, null);
    }

    public ImplicitEligibilityRule(final String inField, final String inValue, final RuleType inRuleType) {
        this.field = inField;
        this.value = inValue;
        this.operatorType = OperatorType.EQUALS;
        this.ruleType = inRuleType;
    }

    public String getField() {
        return this.field;
    }

    public void setField(final String inField) {
        this.field = inField;
    }

    public String getValue() {
        String val = null;
        if (this.field.toUpperCase().contains("DATE")) {
            val = getDateInStringFormat(this.value);
        } else {
            val = this.value;
        }
        return val;
    }

    public void setValue(final String inValue) {
        this.value = inValue == null ? "" : inValue;
    }

    public OperatorType getOperatorType() {
        return this.operatorType;
    }

    public void setOperatorType(final OperatorType operatorType) {
        if (operatorType == null) {
            this.operatorType = OperatorType.EQUALS; // This is default for now
        } else {
            this.operatorType = operatorType;
        }
    }

    public RuleType getRuleType() {
        return this.ruleType;
    }

    public void setRuleType(final RuleType inRuleType) {
        this.ruleType = inRuleType;
    }

    @Override
    public int hashCode() {
        int hashValue = 1;
        if (getField() != null) {
            hashValue = hashValue + getField().hashCode() * 32;
        }
        if (getField() != null) {
            hashValue = hashValue + getField().hashCode() * 32;
        }
        return hashValue;
    }

    @Override
    public boolean equals(final Object obj) {
        final ImplicitEligibilityRule anotherObj = (ImplicitEligibilityRule) obj;
        if (anotherObj != null) {
            if (getField() != null && getValue() != null) {
                if (getField().equals(anotherObj.getField()) && getValue().equals(anotherObj.getValue())) {
                    return true;
                }
            }
        }
        return false;
    }

    private String getDateInStringFormat(final String strDate) {
        DateTime dtValue = null;
        if (isNotEmpty(strDate)) {
            dtValue = DateTime.parse(strDate);
            final DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZZ");
            return fmt.print(dtValue);
        }
        return strDate;
    }

}