net.navasoft.madcoin.backend.services.controller.exception.impl.SessionControllerException.java Source code

Java tutorial

Introduction

Here is the source code for net.navasoft.madcoin.backend.services.controller.exception.impl.SessionControllerException.java

Source

/*******************************************************************************
 * Copyright 2014 Juan Diego Navarre Gonzalez
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package net.navasoft.madcoin.backend.services.controller.exception.impl;

import java.lang.reflect.Array;
import java.text.MessageFormat;
import java.util.Locale;

import net.navasoft.madcoin.backend.HardCodedConstants;
import net.navasoft.madcoin.backend.services.controller.exception.AllowedExceptionMessage;
import net.navasoft.madcoin.backend.services.controller.exception.ControllerException;

import org.apache.commons.lang.ArrayUtils;
import org.springframework.context.MessageSource;

/**
 * net.navasoft.madcoin.backend.services.controller.exception.impl Class class
 * SessionControllerException. Description:
 * 
 * @author Juan Diego Navarre Gonzalez - (<a
 *         href="mailto:jdnavarreg@outlook.com">{@literal jdnavarreg@outlook.com}
 *         </a>)
 * @version 1.0
 * @since 28/07/2014 11:34:20 PM
 */
public class SessionControllerException extends ControllerException implements HardCodedConstants {

    /**
     * Constant serialVersionUID.
     * 
     * @since 28/07/2014, 11:34:20 PM
     */
    private static final long serialVersionUID = 1L;
    private Object[] reasonsToFail;
    private String locatedMessage;
    private Locale language;

    /**
     * Instantiates a new session
     * net.navasoft.madcoin.backend.model.controller.impl exception.
     * 
     * @param exceptionMessages
     *            the exception messages
     * @param injected
     *            the injected
     * @param key
     *            the key
     * @param parameters
     *            the parameters
     * @since 28/07/2014, 11:34:20 PM
     */
    public SessionControllerException(final MessageSource exceptionMessages, final Locale injected,
            final AllowedExceptionMessage key, Object... parameters) {
        super(exceptionMessages.getMessage(key.getPropertyKey(), literal(parameters), injected));
        locatedMessage = key.getPropertyKey();
        language = injected;
        reasonsToFail = (Object[]) Array.newInstance(Object.class, 0);
        reasonsToFail = ArrayUtils.add(reasonsToFail, parameters[0]);
        reasonsToFail = ArrayUtils.add(reasonsToFail, parameters[1]);
        setAllowedTips(parameters[parameters.length - 1]);
        formulateTips();
    }

    /**
     * Gets the causes.
     * 
     * @return the causes
     * @since 28/07/2014, 11:34:20 PM
     */
    @Override
    public Object[] getCauses() {
        return reasonsToFail;
    }

    /**
     * Formulate tips.
     * 
     * @return the string
     * @since 28/07/2014, 11:34:20 PM
     */
    @Override
    public String[] formulateTips() {
        String[] finalTips = (String[]) Array.newInstance(String.class, 0);
        if (allowedTipsQuantity != -1) {
            for (int availableTip = 1; availableTip < allowedTipsQuantity + 1; availableTip++) {
                finalTips = (String[]) ArrayUtils.add(finalTips,
                        tips.getMessage(MessageFormat.format(locatedMessage + tipSuffix, availableTip),
                                new Object[] { availableTip }, "", language));
            }
        } // Add the filter chain or REGEXP case handling, if applies.
        return finalTips;
    }
}