Java tutorial
/******************************************************************************* * 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.model.controller.exceptions.AlreadyAcceptedException; import net.navasoft.madcoin.backend.model.controller.exceptions.AlreadyOnSourceException; import net.navasoft.madcoin.backend.model.controller.exceptions.InexistentOrderException; import net.navasoft.madcoin.backend.services.IFilterSelector; import net.navasoft.madcoin.backend.services.controller.exception.impl.BusinessControllerException; import net.navasoft.madcoin.backend.services.controller.exception.impl.InactiveUserException; import net.navasoft.madcoin.backend.services.controller.exception.impl.InexistentCategoryException; import net.navasoft.madcoin.backend.services.controller.exception.impl.InexistentLOBException; import net.navasoft.madcoin.backend.services.controller.exception.impl.InexistentUserException; import net.navasoft.madcoin.backend.services.controller.exception.impl.SessionControllerException; import net.navasoft.madcoin.backend.services.controller.exception.impl.UnmatchedLoginException; import net.navasoft.madcoin.backend.services.rest.IOrdersService; import net.navasoft.madcoin.backend.services.vo.WorkRequestListVO; import net.navasoft.madcoin.backend.services.vo.WorkRequestQueryVO; import net.navasoft.madcoin.backend.services.vo.request.impl.AcceptServiceSuccessRequestVO; import net.navasoft.madcoin.backend.services.vo.request.impl.AskServiceSuccessRequestVO; 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.AuthenticationFailedResponseVO; import net.navasoft.madcoin.backend.services.vo.response.impl.BusinessFailedResponseVO; import net.navasoft.madcoin.backend.services.vo.response.impl.SignUpFailedResponseVO; 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.RequestBody; 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.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseStatus; /** * net.navasoft.madcoin.backend.services.controller Class class * WorkRequestController. 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:24 PM */ @Controller @RequestMapping("/cnd-api/service") public class WorkRequestController extends ClicknDoneController { /** * service. * * @since 27/07/2014, 06:48:24 PM */ @Autowired @Qualifier("ordersService") private IOrdersService service; /** * filter service. * * @since 27/07/2014, 06:48:24 PM */ @Autowired @Qualifier("WorkRequestFilterService") private IFilterSelector<WorkRequestQueryVO> filterService; /** * Gets the orders by segment. * * @param userType * the user type * @param loggedToken * the logged token * @param filters * the filters * @param business_line * the business_line * @param service_category * the service_category * @return the orders by segment * @since 27/07/2014, 06:48:24 PM */ @RequestMapping(value = "/{category}-{subCategory}/work_requests/{authToken}", method = RequestMethod.POST, headers = { "Content-Type=application/json", "Accept=application/json" }) public @ResponseBody WorkRequestListVO getOrdersBySegment( @RequestParam(defaultValue = "END_USER_TYPE", value = "userAbbrev", required = false) String userType, @PathVariable(value = "authToken") String loggedToken, @RequestBody WorkRequestQueryVO filters, @PathVariable(value = "category") String business_line, @PathVariable(value = "subCategory") String service_category) { return service.getAvailableRequestsBySegment(userType, business_line, service_category, filterService.chooseFilter(filters).filter()); } /** * Gets the orders. * * @param userType * the user type * @param loggedToken * the logged token * @param filters * the filters * @return the orders * @since 27/07/2014, 06:48:24 PM */ @RequestMapping(value = "/all/work_requests", method = RequestMethod.POST, headers = { "Content-Type=application/json", "Accept=application/json" }) public @ResponseBody WorkRequestListVO getOrders( @RequestParam(defaultValue = "END_USER_TYPE", value = "userAbbrev", required = false) String userType, @PathVariable(value = "authToken") String loggedToken, @RequestBody WorkRequestQueryVO filters) { return service.getAvailableRequests(userType, filterService.chooseFilter(filters).filter()); } /** * Gets the all orders by segment. * * @param userType * the user type * @param token * the token * @param business_line * the business_line * @param service_category * the service_category * @return the all orders by segment * @since 27/07/2014, 06:48:24 PM */ @RequestMapping(value = "/provider/{category}-{subCategory}/work_requests/all", method = RequestMethod.GET) public @ResponseBody WorkRequestListVO getAllOrdersBySegment( @RequestParam(defaultValue = "END_USER_TYPE", value = "userAbbrev") String userType, @PathVariable(value = "authToken") String token, @PathVariable(value = "category") String business_line, @PathVariable(value = "subCategory") String service_category) { return service.getAllRequestsBySegment(userType, business_line, service_category); } /** * Gets the all orders. * * @return the all orders * @since 27/07/2014, 06:48:24 PM */ @RequestMapping(value = "/all", method = RequestMethod.GET, headers = { "Content-Type=application/json", "Accept=application/json" }) public @ResponseBody WorkRequestListVO getAllOrders() { return service.getAllRequests(); } /** * Gets the assigned orders by provider. * * @param userType * the user type * @param token * the token * @param filters * the filters * @return the assigned orders by provider * @since 27/07/2014, 06:48:24 PM */ @RequestMapping(value = "/provider/work_requests/all", method = RequestMethod.POST) public @ResponseBody WorkRequestListVO getAssignedOrdersByProvider( @RequestParam(defaultValue = "SERVICE_PROVIDER_TYPE", value = "userAbbrev") String userType, @PathVariable(value = "authToken") String token, @RequestBody WorkRequestQueryVO filters) { return service.getAllAssignedRequests(filterService.chooseFilter(filters).filter()); } /** * Register order. * * @param request * the request * @param registerOrder * the register order * @param fromSystem * the from system * @param fromToken * the from token * @param userAgent * the user agent * @param category * the category * @param subCategory * the sub category * @return the success response vo * @since 25/08/2014, 11:01:03 PM */ @RequestMapping(value = "/{category}-{subCategory}/bookRequest", method = RequestMethod.PUT, headers = { "Content-Type=application/json", "Accept=application/json" }) @ResponseBody public SuccessResponseVO registerOrder(HttpServletRequest request, @RequestBody(required = true) AskServiceSuccessRequestVO registerOrder, @RequestHeader("X-Origin-OS") String fromSystem, @RequestHeader("X-Origin-Token") String fromToken, @RequestHeader("User-Agent") String userAgent, @PathVariable String category, @PathVariable String subCategory) { String loadBalanceIP = request.getHeader("X-Forwarded-For"); String ipAddress = request.getRemoteAddr(); if (ipAddress.equals("127.0.0.1") || ipAddress.equals("localhost")) { registerOrder.setIpAddress(ipAddress); } else { registerOrder.setIpAddress(loadBalanceIP); } registerOrder.setGadget(userAgent); registerOrder.setOs(fromSystem); registerOrder.setSerial(fromToken); registerOrder.setVersion(userAgent); registerOrder.setCategory(category); registerOrder.setSubCategory(subCategory); return service.bookRequest(buildWrapper(registerOrder)); } /** * Attend order. * * @param request * the request * @param acceptedOrder * the accepted order * @param fromSystem * the from system * @param fromToken * the from token * @param userAgent * the user agent * @param category * the category * @param subCategory * the sub category * @return the success response vo * @since 8/09/2014, 03:16:01 AM */ @RequestMapping(value = "/{category}-{subCategory}/completeRequest", method = RequestMethod.POST, headers = { "Content-Type=application/json", "Accept=application/json" }) @ResponseBody public SuccessResponseVO attendOrder(HttpServletRequest request, @RequestBody(required = true) AcceptServiceSuccessRequestVO acceptedOrder, @RequestHeader("X-Origin-OS") String fromSystem, @RequestHeader("X-Origin-Token") String fromToken, @RequestHeader("User-Agent") String userAgent, @PathVariable String category, @PathVariable String subCategory) { String loadBalanceIP = request.getHeader("X-Forwarded-For"); String ipAddress = request.getRemoteAddr(); if (ipAddress.equals("127.0.0.1") || ipAddress.equals("localhost")) { acceptedOrder.setIpAddress(ipAddress); } else { acceptedOrder.setIpAddress(loadBalanceIP); } acceptedOrder.setGadget(userAgent); acceptedOrder.setOs(fromSystem); acceptedOrder.setSerial(fromToken); acceptedOrder.setVersion(userAgent); acceptedOrder.setCategory(category); acceptedOrder.setSubCategory(subCategory); return service.acceptRequest(buildWrapper(acceptedOrder)); } } @ControllerAdvice class OrderControllerExceptionHandler { @ExceptionHandler(InactiveUserException.class) @ResponseStatus(value = HttpStatus.UNAUTHORIZED) private @ResponseBody FailedResponseVO handleInactiveUser(SessionControllerException e) { FailedResponseVO value = new AuthenticationFailedResponseVO(); value.setErrorMessage(e.getMessage()); value.setCauses(e.getCauses()); value.setTip(e.formulateTips()); return value; } @ExceptionHandler(InexistentUserException.class) @ResponseStatus(value = HttpStatus.LOCKED) private @ResponseBody FailedResponseVO handleInexistentUser(SessionControllerException e) { FailedResponseVO value = new AuthenticationFailedResponseVO(); value.setErrorMessage(e.getMessage()); value.setCauses(e.getCauses()); value.setTip(e.formulateTips()); return value; } @ExceptionHandler(UnmatchedLoginException.class) @ResponseStatus(value = HttpStatus.BAD_REQUEST) private @ResponseBody FailedResponseVO handleBadUser(SessionControllerException e) { FailedResponseVO value = new AuthenticationFailedResponseVO(); 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; } @ExceptionHandler(InexistentCategoryException.class) @ResponseStatus(value = HttpStatus.NOT_FOUND) private @ResponseBody FailedResponseVO handleWrongCategory(BusinessControllerException e) { FailedResponseVO value = new BusinessFailedResponseVO(); value.setErrorMessage(e.getMessage()); value.setCauses(e.getCauses()); value.setTip(e.formulateTips()); return value; } @ExceptionHandler(AlreadyAcceptedException.class) @ResponseStatus(value = HttpStatus.NOT_ACCEPTABLE) private @ResponseBody FailedResponseVO handleAlreadyAccepted(AlreadyAcceptedException e) { FailedResponseVO value = new BusinessFailedResponseVO(); value.setErrorMessage(e.getMessage()); value.setCauses("La orden fue aceptada por otro proveedor antes de que ud la aceptara."); String tips[] = new String[] { "Solicite la asignacion manual de ordenes a su usuario proveedor", "Comuniquese con un administrador si desea que se le asignen manualmente las ordenes." }; value.setTip(tips); return value; } @ExceptionHandler(InexistentOrderException.class) @ResponseStatus(value = HttpStatus.NOT_FOUND) private @ResponseBody FailedResponseVO handleInexistentOrder(InexistentOrderException e) { FailedResponseVO value = new BusinessFailedResponseVO(); value.setErrorMessage(e.getMessage()); value.setCauses("Est intentando aceptar una orden que no existe en el sistema."); String tips[] = new String[] { "Por favor, consuma el servicio especificando una orden que haya sido generada en el sistema", "Verifique la subcategoria y el id de la orden que est intentando aceptar." }; value.setTip(tips); return value; } @ExceptionHandler(AlreadyOnSourceException.class) @ResponseStatus(value = HttpStatus.BAD_REQUEST) private @ResponseBody FailedResponseVO handleSQL(AlreadyOnSourceException e) { FailedResponseVO value = new SignUpFailedResponseVO(); value.setErrorMessage(e.getMessage()); value.setCauses("Esta orden ya ha sido previamente aceptada por su usuario"); String[] tips = new String[] { "Verifique que no este aceptando el mismo servicio mas de una vez.", "Comuniquese con el administrador por cualquier asunto de indole tecnica." }; value.setTip(tips); return value; } }