net.navasoft.madcoin.backend.services.vo.request.SuccessRequestVOWrapper.java Source code

Java tutorial

Introduction

Here is the source code for net.navasoft.madcoin.backend.services.vo.request.SuccessRequestVOWrapper.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.vo.request;

import java.lang.annotation.Annotation;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import net.navasoft.madcoin.backend.annotations.ProcessedField;
import net.navasoft.madcoin.backend.annotations.ProcessingErrorValue;
import net.navasoft.madcoin.backend.annotations.ProcessingField;
import net.navasoft.madcoin.backend.annotations.ProcessorService;
import net.navasoft.madcoin.backend.services.controller.exception.ControllerExceptionArgument;
import net.navasoft.madcoin.backend.services.controller.exception.WrapperException;
import net.navasoft.madcoin.backend.services.rest.IService;

import org.apache.commons.lang.ArrayUtils;

/**
 * net.navasoft.madcoin.backend.services.vo.request Class class
 * SuccessRequestVOWrapper. 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:49:08 PM
 */
public class SuccessRequestVOWrapper implements SuccessRequestVO {

    /**
     * processed.
     * 
     * @since 21/08/2014, 01:36:01 AM
     */
    private Map<String, Object> processed = new HashMap<String, Object>();
    /**
     * hidden.
     * 
     * @since 27/07/2014, 06:49:08 PM
     */
    private SuccessRequestVO hidden;

    /**
     * helper.
     * 
     * @since 27/07/2014, 06:49:08 PM
     */
    private Class<?> helper;

    private String[] alreadyProcessed = new String[] {};

    /**
     * Gets the attribute.
     * 
     * @param field
     *            the field
     * @return the attribute
     * @throws WrapperException
     *             the wrapper exception
     * @since 27/07/2014, 06:49:08 PM
     */
    public Object getAttribute(final String field) throws WrapperException {
        try {
            return helper.cast(hidden).getClass().getDeclaredMethod("get" + field, ArrayUtils.EMPTY_CLASS_ARRAY)
                    .invoke(helper, ArrayUtils.EMPTY_OBJECT_ARRAY);
        } catch (IllegalAccessException e) {
            throw new WrapperException("Not accessible", e);
        } catch (IllegalArgumentException e) {
            throw new WrapperException("Incorrect parameters", e);
        } catch (InvocationTargetException e) {
            throw new WrapperException("Unexpected VO", e);
        } catch (NoSuchMethodException e) {
            throw new WrapperException("Bad typing of bean", e);
        } catch (SecurityException e) {
            throw new WrapperException("Bad runtime access", e);
        }
    }

    /**
     * Instantiates a new success request vo wrapper.
     * 
     * @since 27/07/2014, 06:49:08 PM
     */
    protected SuccessRequestVOWrapper() {
    }

    /**
     * Instantiates a new success request vo wrapper.
     * 
     * @param original
     *            the original
     * @since 27/07/2014, 06:49:08 PM
     */
    public SuccessRequestVOWrapper(SuccessRequestVO original) {
        this.hidden = original;
        this.helper = original.getClass();
    }

    /**
     * Trace.
     * 
     * @since 27/07/2014, 06:49:08 PM
     */
    @Override
    public void trace() {
        this.hidden.trace();
    }

    /**
     * Finalize.
     * 
     * @throws Throwable
     *             the throwable
     * @since 27/07/2014, 06:49:08 PM
     */
    @Override
    protected void finalize() throws Throwable {
        this.helper = null;
        this.hidden = null;
        this.alreadyProcessed = null;
        super.finalize();
    }

    private boolean isValidService(IService service) {
        return service.getClass().isAnnotationPresent(ProcessorService.class);
    }

    private Method[] filterMethods() {
        Method[] filtered = new Method[] {};
        for (Method all : this.helper.getMethods()) {
            if (all.getName().startsWith("get") && !all.getName().equals("getClass")) {
                filtered = (Method[]) ArrayUtils.add(filtered, all);
            }
        }
        return filtered;
    }

    private ProcessedField[] getTarget(IService service) {
        ProcessedField[] scope = new ProcessedField[] {};
        for (Field attendanceProperty : service.getClass().getDeclaredFields()) {
            if (attendanceProperty.getAnnotation(ProcessedField.class) != null) {
                scope = (ProcessedField[]) ArrayUtils.add(scope,
                        attendanceProperty.getAnnotation(ProcessedField.class));
            }
        }
        return scope;
    }

    private boolean validateProcessAnnotations(ProcessedField var, ProcessingField entry) {
        return var.expectedType().equals(entry.expectedType()) && var.precedence().equals(entry.precedence())
                && ArrayUtils.contains(var.possibleVariables(), entry.expectedVariable());
    }

    /**
     * Extract value.
     * 
     * @param affected
     *            the affected
     * @return the object
     * @since 21/08/2014, 01:36:12 AM
     */
    @Override
    public Object extractValue(String affected) {
        return processed.get(affected);
    }

    /**
     * Gets the processing values.
     * 
     * @param service
     *            the service
     * @since 27/07/2014, 06:49:08 PM
     */
    @Override
    public void processValues(IService service) {
        if (isValidService(service)) {
            for (ProcessedField annotatedField : getTarget(service)) {
                for (Method accessor : filterMethods()) {
                    Annotation annot = accessor.getAnnotation(ProcessingField.class);
                    if (annot != null) {
                        ProcessingField expectedReal = ((ProcessingField) annot);
                        if (!ArrayUtils.contains(alreadyProcessed, expectedReal.expectedVariable())) {
                            try {
                                if (validateProcessAnnotations(annotatedField, expectedReal)) {
                                    Object requestValue = null;
                                    switch (expectedReal.precedence()) {
                                    case BASIC_OPTIONAL:
                                        requestValue = accessor.invoke(hidden, ArrayUtils.EMPTY_OBJECT_ARRAY);
                                        processed.put(expectedReal.expectedVariable(), requestValue);
                                        alreadyProcessed = (String[]) ArrayUtils.add(alreadyProcessed,
                                                expectedReal.expectedVariable());
                                        break;
                                    case BASIC_REQUIRED:
                                        requestValue = accessor.invoke(this.hidden, ArrayUtils.EMPTY_OBJECT_ARRAY);
                                        processed.put(expectedReal.expectedVariable(), requestValue);
                                        alreadyProcessed = (String[]) ArrayUtils.add(alreadyProcessed,
                                                expectedReal.expectedVariable());
                                        break;
                                    case COMPLEX_OPTIONAL:
                                        requestValue = expectedReal.helperClass().newInstance();
                                        processed.put(expectedReal.expectedVariable(),
                                                expectedReal.helperClass()
                                                        .getMethod(expectedReal.converterMethod(),
                                                                ArrayUtils.EMPTY_CLASS_ARRAY)
                                                        .invoke(requestValue, ArrayUtils.EMPTY_OBJECT_ARRAY));
                                        alreadyProcessed = (String[]) ArrayUtils.add(alreadyProcessed,
                                                expectedReal.expectedVariable());
                                        break;
                                    case COMPLEX_REQUIRED:
                                        requestValue = expectedReal.helperClass().newInstance();
                                        processed.put(expectedReal.expectedVariable(),
                                                expectedReal.helperClass()
                                                        .getMethod(expectedReal.converterMethod(),
                                                                ArrayUtils.EMPTY_CLASS_ARRAY)
                                                        .invoke(requestValue, ArrayUtils.EMPTY_OBJECT_ARRAY));
                                        alreadyProcessed = (String[]) ArrayUtils.add(alreadyProcessed,
                                                expectedReal.expectedVariable());
                                        break;
                                    default:
                                        break;
                                    }
                                }
                            } catch (IllegalAccessException e1) {
                            } catch (IllegalArgumentException e1) {
                            } catch (InvocationTargetException e1) {
                            } catch (NoSuchMethodException e) {
                            } catch (SecurityException e) {
                            } catch (InstantiationException e) {
                            }
                        }
                    }
                }
            }
        }
    }

    /**
     * Gets the error processing values.
     * 
     * @return the error processing values
     * @since 27/07/2014, 06:49:08 PM
     */
    @Override
    public ControllerExceptionArgument[] getErrorProcessingValues() {
        ControllerExceptionArgument[] errors = (ControllerExceptionArgument[]) Array
                .newInstance(ControllerExceptionArgument.class, 0);
        for (Method accessor : helper.getDeclaredMethods()) {
            for (Annotation expected : accessor.getAnnotations()) {
                if (expected.annotationType() == ProcessingErrorValue.class) {
                    try {
                        ProcessingErrorValue expectedReal = ((ProcessingErrorValue) expected);
                        Object requestValue = null;
                        switch (expectedReal.precedence()) {
                        case BASIC_OPTIONAL:

                            requestValue = accessor.invoke(hidden, ArrayUtils.EMPTY_OBJECT_ARRAY);

                            requestValue = (requestValue != null) ? requestValue : "No data provided";
                            errors = (ControllerExceptionArgument[]) ArrayUtils.add(errors,
                                    new ControllerExceptionArgument(requestValue));
                            break;
                        case BASIC_REQUIRED:
                            requestValue = accessor.invoke(hidden, ArrayUtils.EMPTY_OBJECT_ARRAY);
                            errors = (ControllerExceptionArgument[]) ArrayUtils.add(errors,
                                    new ControllerExceptionArgument(requestValue));
                            break;
                        case COMPLEX_OPTIONAL:
                            break;
                        case COMPLEX_REQUIRED:
                            break;
                        default:
                            break;
                        }
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    } catch (IllegalArgumentException e) {
                        e.printStackTrace();
                    } catch (InvocationTargetException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return errors;
    }

    /**
     * Extract values.
     * 
     * @param affected
     *            the affected
     * @return the object
     * @since 21/08/2014, 01:43:01 AM
     */
    @Override
    public Object extractValues(String affected) {
        return processed.get(affected);
    }
}