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

de.tsl2.nano.bean.def.Status Maven / Gradle / Ivy

Go to download

TSL2 Framework Descriptor (currency-handling, generic formatter, descriptors for beans, collections, actions and values)

There is a newer version: 2.5.1
Show newest version
/*
 * File: $HeadURL$
 * Id  : $Id$
 * 
 * created by: Thomas Schneider
 * created on: Feb 17, 2012
 * 
 * Copyright: (c) Thomas Schneider 2012, all rights reserved
 */
package de.tsl2.nano.bean.def;

import de.tsl2.nano.action.IStatus;
import de.tsl2.nano.core.ENV;

/**
 * default implementation of {@link IStatus} for a status of type warning or error. if you want to create a ok-status
 * use {@link IStatus#STATUS_OK}.
 * 
 * @author Thomas Schneider
 * @version $Revision$
 */
public class Status implements IStatus {
    /** serialVersionUID */
    private static final long serialVersionUID = -7981328274166216957L;

    int status = OK;
    String msg = null;
    Throwable ex = null;

    /**
     * constructor
     */
    protected Status() {
        super();
    }

    /**
     * constructor
     * 
     * @param ex error description
     */
    public Status(Throwable ex) {
        super();
        this.ex = ex;
        this.msg = TAG[ERROR] + ": " + ex.toString();
        this.status = ERROR;
    }

    /**
     * creates a warning status
     * 
     * @param msg warning message
     */
    public Status(String msg) {
        super();
        this.msg = TAG[WARN] + ": " + msg;
        this.status = WARN;
    }

    /**
     * constructor
     * 
     * @param status {@link IStatus#OK}, {@link IStatus#WARN} or {@link IStatus#ERROR}
     * @param msg warning or error message
     * @param ex error exception
     */
    public Status(int status, String msg, Throwable ex) {
        super();
        this.status = status;
        this.msg = msg;
        this.ex = ex;
    }

    /**
     * usable to create an error status with an illegal argument description
     * 
     * @param name attribute name
     * @param value attribute value that involves the error
     * @param assertion an assertion that was not fulfilled by the value.
     * @return new error status instance
     */
    public static Status illegalArgument(String name, Object value, Object assertion) {
        /* 
         * the toString() conversion should not break the application flow.
         * if a PersistentSet is not initialized but already serialized, the toString()
         * would throw a LazyInitializationException
         */
        String strValue;
        try {
            strValue = String.valueOf(value);
        } catch (Exception ex) {
            strValue = (value != null ? value.getClass() : "null") + ": " + ex.toString();
        }
        return new Status(new IllegalArgumentException(ENV.translate("tsl2nano.assertion.failed", true,
            strValue,
            name,
            assertion)));
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean ok() {
        return status == OK;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String message() {
        return msg;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Throwable error() {
        return ex;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String toString() {
        return ok() ? TAG[OK] : msg;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy