net.navasoft.madcoin.backend.services.controller.BusinessController.java Source code

Java tutorial

Introduction

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

import javax.servlet.http.HttpServletRequest;

import net.navasoft.madcoin.backend.services.controller.exception.impl.BusinessControllerException;
import net.navasoft.madcoin.backend.services.controller.exception.impl.InexistentLOBException;
import net.navasoft.madcoin.backend.services.controller.exception.impl.InsufficientCategoryException;
import net.navasoft.madcoin.backend.services.controller.exception.impl.InsufficientLOBException;
import net.navasoft.madcoin.backend.services.rest.IBusinessService;
import net.navasoft.madcoin.backend.services.vo.response.FailedResponseVO;
import net.navasoft.madcoin.backend.services.vo.response.SuccessResponseVO;
import net.navasoft.madcoin.backend.services.vo.response.impl.BusinessFailedResponseVO;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

/**
 * net.navasoft.madcoin.backend.services.controller Class class
 * BusinessController. Description:
 * 
 * @author Juan Diego Navarre Gonzalez - (${authorMail})
 * @version 1.0
 * @since 24/08/2014 06:11:30 PM
 */
@RequestMapping(value = "/cnd-api/business")
@Controller
public class BusinessController {

    /**
     * Constant categories.
     * 
     * @since 24/08/2014, 06:11:30 PM
     */
    private static final String categories = "/categories";

    /**
     * Constant lobVariable.
     * 
     * @since 24/08/2014, 06:35:48 PM
     */
    private static final String lobVariable = "{category}";

    /**
     * Constant subCategories.
     * 
     * @since 24/08/2014, 06:11:30 PM
     */
    private static final String subCategories = "/" + lobVariable + "/sub-categories";

    private static final String all = "/all";

    /**
     * service.
     * 
     * @since 24/08/2014, 06:11:30 PM
     */
    @Autowired(required = true)
    @Qualifier("BusinessService")
    private IBusinessService service;

    /**
     * Categories.
     * 
     * @param request
     *            the request
     * @param fromSystem
     *            the from system
     * @param fromToken
     *            the from token
     * @param userAgent
     *            the user agent
     * @return the success response vo
     * @since 24/08/2014, 06:11:30 PM
     */
    @RequestMapping(value = { categories }, method = RequestMethod.GET)
    @ResponseStatus(value = HttpStatus.OK)
    public @ResponseBody SuccessResponseVO categories(HttpServletRequest request,
            @RequestHeader("X-Origin-OS") String fromSystem, @RequestHeader("X-Origin-Token") String fromToken,
            @RequestHeader("User-Agent") String userAgent) {
        return service.getBusinessLines();
    }

    /**
     * Gets the all.
     * 
     * @param request
     *            the request
     * @param fromSystem
     *            the from system
     * @param fromToken
     *            the from token
     * @param userAgent
     *            the user agent
     * @return the all
     * @since 1/09/2014, 10:51:59 PM
     */
    @RequestMapping(value = { all }, method = RequestMethod.GET)
    @ResponseStatus(value = HttpStatus.OK)
    public @ResponseBody SuccessResponseVO getAll(HttpServletRequest request,
            @RequestHeader("X-Origin-OS") String fromSystem, @RequestHeader("X-Origin-Token") String fromToken,
            @RequestHeader("User-Agent") String userAgent) {
        return service.getAll();
    }

    /**
     * Sub categories.
     * 
     * @param request
     *            the request
     * @param fromSystem
     *            the from system
     * @param fromToken
     *            the from token
     * @param userAgent
     *            the user agent
     * @param category
     *            the line of business
     * @return the success response value object
     * @since 24/08/2014, 06:13:15 PM
     */
    @RequestMapping(value = { subCategories }, method = RequestMethod.GET)
    @ResponseStatus(value = HttpStatus.OK)
    public @ResponseBody SuccessResponseVO subCategories(HttpServletRequest request,
            @RequestHeader("X-Origin-OS") String fromSystem, @RequestHeader("X-Origin-Token") String fromToken,
            @RequestHeader("User-Agent") String userAgent, @PathVariable String category) {
        return service.getCategories(category);
    }

}

@ControllerAdvice
class BusinessControllerExceptionHandler {

    @ExceptionHandler(InsufficientCategoryException.class)
    @ResponseStatus(value = HttpStatus.NO_CONTENT)
    private @ResponseBody FailedResponseVO handleNoCategories(BusinessControllerException e) {
        FailedResponseVO value = new BusinessFailedResponseVO();
        value.setErrorMessage(e.getMessage());
        value.setCauses(e.getCauses());
        value.setTip(e.formulateTips());
        return value;
    }

    @ExceptionHandler(InsufficientLOBException.class)
    @ResponseStatus(value = HttpStatus.NO_CONTENT)
    private @ResponseBody FailedResponseVO handleNoLOBs(BusinessControllerException e) {
        FailedResponseVO value = new BusinessFailedResponseVO();
        value.setErrorMessage(e.getMessage());
        value.setCauses(e.getCauses());
        value.setTip(e.formulateTips());
        return value;
    }

    @ExceptionHandler(InexistentLOBException.class)
    @ResponseStatus(value = HttpStatus.NOT_FOUND)
    private @ResponseBody FailedResponseVO handleWrongLOB(BusinessControllerException e) {
        FailedResponseVO value = new BusinessFailedResponseVO();
        value.setErrorMessage(e.getMessage());
        value.setCauses(e.getCauses());
        value.setTip(e.formulateTips());
        return value;
    }

}