net.navasoft.madcoin.backend.services.controller.exception.ControllerException.java Source code

Java tutorial

Introduction

Here is the source code for net.navasoft.madcoin.backend.services.controller.exception.ControllerException.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;

import java.lang.reflect.Array;

import net.navasoft.madcoin.backend.UserTypes;
import net.navasoft.madcoin.backend.services.exception.ICustomRestException;

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

/**
 * net.navasoft.madcoin.backend.services.controller.exception Class class
 * ControllerException. Description:
 * 
 * @author Juan Diego Navarre Gonzalez - (<a
 *         href="mailto:jdnavarreg@outlook.com">{@literal jdnavarreg@outlook.com}
 *         </a>)
 * @version 1.0
 * @since 27/07/2014 06:48:30 PM
 */
@Component
public abstract class ControllerException extends RuntimeException implements ICustomRestException {

    /**
     * Constant serialVersionUID.
     * 
     * @since 27/07/2014, 06:48:30 PM
     */

    protected int allowedTipsQuantity = -1;
    private static final long serialVersionUID = 1L;
    protected MessageSource tips;

    /**
     * Instantiates a new net.navasoft.madcoin.backend.model.controller.impl
     * exception.
     * 
     * @param message
     *            the message
     * @since 27/07/2014, 06:48:30 PM
     */
    public ControllerException(String message) {
        super(message);
    }

    /**
     * Sets the allowed tips.
     * 
     * @param allowedByService
     *            the new allowed tips
     * @since 24/08/2014, 07:52:19 PM
     */
    protected void setAllowedTips(Object allowedByService) {
        if (allowedByService instanceof Integer) {
            allowedTipsQuantity = (int) allowedByService;
        }
    }

    /**
     * Literal.
     * 
     * @param genericReplacements
     *            the generic replacements
     * @return the string[]
     * @since 24/08/2014, 08:21:59 PM
     */
    protected static String[] literal(Object[] genericReplacements) {
        String[] results = (String[]) Array.newInstance(String.class, 0);
        for (Object translated : genericReplacements) {
            if (translated instanceof UserTypes) {
                results = (String[]) ArrayUtils.add(results, ((UserTypes) translated).getExternalValue().name());
            } else if (translated instanceof Integer) {
            } else {
                results = (String[]) ArrayUtils.add(results, translated.toString());
            }
        }
        return results;
    }

    protected void setTipMessages(MessageSource additional) {
        tips = additional;
    }

    /**
     * Prepare tips.
     * 
     * @param allowedTips
     *            the allowed tips
     * @param tips
     *            the tips
     * @since 24/08/2014, 08:25:46 PM
     */
    public void prepareTips(int allowedTips, MessageSource tips) {
        setAllowedTips(allowedTips);
        setTipMessages(tips);
    }

}