Java tutorial
/* * Copyright(C) 2014 * NEC Corporation All rights reserved. * * No permission to use, copy, modify and distribute this software * and its documentation for any purpose is granted. * This software is provided under applicable license agreement only. */ package com.nec.harvest.controller; import java.net.URI; import java.util.Date; import javax.inject.Inject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.format.annotation.DateTimeFormat; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.util.UriComponents; import org.springframework.web.util.UriComponentsBuilder; import com.nec.core.exception.TooManyObjectsException; import com.nec.harvest.bean.mapping.ProfitMonthlyBean; import com.nec.harvest.constant.Constants; import com.nec.harvest.exception.ServiceException; import com.nec.harvest.menu.group.ProfitAndLossManagementProGroup; import com.nec.harvest.service.ProfitByShopService; import com.nec.harvest.stereotype.SessionAttribute; import com.nec.harvest.util.DateFormatUtil; import com.nec.harvest.util.DateFormatUtil.DateFormat; /** * The profit by shop (sonekis) controller * * @author huonghv * */ @Controller @RequestMapping(Constants.SONEKIS_PATH) public class SonekisController extends ProfitAndLossManagementProGroup implements AbstractRenderer, TitleRenderer { private static final Logger logger = LoggerFactory.getLogger(SonekisController.class); private final ProfitByShopService profitByShopService; @Inject public SonekisController(ProfitByShopService profitByShopService) { this.profitByShopService = profitByShopService; } /** {@inheritDoc} */ @Override @RequestMapping(value = "", method = RequestMethod.GET) public String render(@SessionAttribute(Constants.SESS_ORGANIZATION_CODE) String userOrgCode, @SessionAttribute(Constants.SESS_BUSINESS_DAY) Date businessDay, @PathVariable String proGNo) { if (logger.isDebugEnabled()) { logger.debug("Redering sonekis page view without parameters..."); } // Automatically build a redirect link UriComponents uriComponents = UriComponentsBuilder .fromUriString(Constants.SONEKIS_PATH + "/{orgCode}/{month}").build(); String businessMonth = DateFormatUtil.format(businessDay, DateFormat.DATE_WITHOUT_DAY); URI uri = uriComponents.expand(proGNo, userOrgCode, businessMonth).encode().toUri(); return "redirect:" + uri.toString(); } /** * Render profit by shop page view with two parameters: organization code * and current month in action * * @param userOrgCode * Organization code from session * @param businessDay * Business day from session * @param proGNo * Parent group menu * @param orgCode * Organization code on request path * @param month * Current action month on request path * @param request * The http-request * @param model * Model on action * @return Model logic view name */ @RequestMapping(value = "/{orgCode:[a-z0-9]+}/{month:[\\d]+}", method = RequestMethod.GET) public String render(@SessionAttribute(Constants.SESS_ORGANIZATION_CODE) String userOrgCode, @SessionAttribute(Constants.SESS_BUSINESS_DAY) Date businessDay, @PathVariable String proGNo, @PathVariable String orgCode, @PathVariable @DateTimeFormat(pattern = "yyyyMM") Date month, final Model model) { logger.info("Profit by shop rendering..."); // model.addAttribute(PROCESSING_MONTH, month); try { ProfitMonthlyBean profitByShop = null; try { // use profitByShopService to get profit by shop profitByShop = profitByShopService.aggregateByOrgCodeAndMonthly(orgCode, month, businessDay); } catch (NullPointerException | TooManyObjectsException | IllegalArgumentException | ServiceException ex) { logger.error(ex.getMessage(), ex); // ??????????? model.addAttribute(ERROR, Boolean.TRUE); model.addAttribute(ERROR_MESSAGE, getSystemError()); return getViewName(); } // model.addAttribute("profitMonthly", profitByShop); try { // use profitByShopService to check next and previous is available or not Date previousMonthAvailable = profitByShopService.checkAvailableByOrgCodeAndMothly(orgCode, month, businessDay, false); Date nextMonthAvailable = profitByShopService.checkAvailableByOrgCodeAndMothly(orgCode, month, businessDay, true); model.addAttribute(PREVIOUS_MONTH, previousMonthAvailable); model.addAttribute(NEXT_MONTH, nextMonthAvailable); } catch (NullPointerException | IllegalArgumentException ex) { logger.warn(ex.getMessage()); } } catch (ServiceException ex) { logger.error(ex.getMessage(), ex); // ??????????? model.addAttribute(ERROR, Boolean.TRUE); model.addAttribute(ERROR_MESSAGE, getSystemError()); } return getViewName(); } @Override public String getViewName() { return "sonekis/sonekis"; } @Override public String getTitleName() { return "??"; } }