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.text.ParseException; import java.util.Date; import java.util.List; import java.util.Map; 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.RequestBody; 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.util.UriComponents; import org.springframework.web.util.UriComponentsBuilder; import com.nec.core.exception.ObjectNotFoundException; import com.nec.core.exception.TooManyObjectsException; import com.nec.harvest.bean.mapping.PurchaseBean; import com.nec.harvest.bean.mapping.json.JSONBean; import com.nec.harvest.bean.mapping.json.JSONPurchase; import com.nec.harvest.constant.Constants; import com.nec.harvest.constant.MsgConstants; import com.nec.harvest.constant.SqlConstants; import com.nec.harvest.exception.ServiceException; import com.nec.harvest.helper.BusinessDayHelper; import com.nec.harvest.helper.MessageHelper; import com.nec.harvest.menu.group.DailyReportingProGroup; import com.nec.harvest.model.Tighten; import com.nec.harvest.model.User; import com.nec.harvest.service.MonthlyPurchaseService; import com.nec.harvest.service.PurchaseService; import com.nec.harvest.service.TightenService; import com.nec.harvest.stereotype.SessionAttribute; import com.nec.harvest.stereotype.UserPrincipal; import com.nec.harvest.util.DateFormatUtil; import com.nec.harvest.util.DateFormatUtil.DateFormat; import com.nec.harvest.util.DateUtil; /** * A controller that listen every request after process logic for (kounyu) After * reponse to page html. * * @author hungpd * */ @Controller @RequestMapping(value = Constants.KOUNYU_PATH) public class KounyuController extends DailyReportingProGroup implements AbstractRenderer, TitleRenderer { private static final Logger logger = LoggerFactory.getLogger(KounyuController.class); private static final String LIST_KOUNYU_HEADER_BEAN = "listPurchaseHeaderBean"; private static final String LIST_KOUNYU_DATA_BEAN = "listPurchaserDataBean"; private static final String ACTUAL_MAXIMUM_DAYS_OF_MONTH = "actualMaximumOfMonth"; private final PurchaseService purchaseService; private final TightenService tightenService; private final MonthlyPurchaseService monthlyPurchaseService; @Inject public KounyuController(PurchaseService purchaseService, TightenService tightenService, MonthlyPurchaseService monthlyPurchaseService) { this.purchaseService = purchaseService; this.tightenService = tightenService; this.monthlyPurchaseService = monthlyPurchaseService; } /** {@inheritDoc} */ @Override @RequestMapping(value = "", method = RequestMethod.GET) public String render(@SessionAttribute(Constants.SESS_ORGANIZATION_CODE) String orgCode, @SessionAttribute(Constants.SESS_BUSINESS_DAY) Date businessDay, @PathVariable String proGNo) { if (logger.isDebugEnabled()) { logger.debug("Redering kounyu report..."); } // Automatically build a redirect link UriComponents uriComponents = UriComponentsBuilder .fromUriString(Constants.KOUNYU_PATH + "/{orgCode}/{monthly}").build(); String businessDate = DateFormatUtil.format(businessDay, DateFormat.DATE_WITHOUT_DAY); URI uri = uriComponents.expand(proGNo, orgCode, businessDate).encode().toUri(); logger.info("Redirect to cash management screen with params in session..."); return "redirect:" + uri.toString(); } /** * This function will mapping without path variables default render * * @param userOrgCode * @param businessDay * @param orgCode * @param month * @param model * @return */ @RequestMapping(value = "/{orgCode}/{monthly}", method = RequestMethod.GET) public String render(@SessionAttribute(Constants.SESS_ORGANIZATION_CODE) String userOrgCode, @SessionAttribute(Constants.SESS_BUSINESS_DAY) Date businessDay, @PathVariable String orgCode, @PathVariable @DateTimeFormat(pattern = "yyyyMM") Date monthly, final Model model) { logger.info("Get a organization based on organization's code..."); // model.addAttribute(PAGE_VIEW, "kounyu"); model.addAttribute(PROCESSING_MONTH, monthly); model.addAttribute(ACTUAL_MAXIMUM_DAYS_OF_MONTH, DateUtil.getActualMaximumOfMonth(monthly)); try { // Check empty value on next month String nextMontly = BusinessDayHelper.getNextMonthly(monthly, DateFormat.DATE_WITHOUT_DAY); if (!purchaseService.checkEmptyPurchase(userOrgCode, nextMontly)) { nextMontly = null; } model.addAttribute(NEXT_MONTH, nextMontly); // Check empty value on prev month String previousMonthly = BusinessDayHelper.getPreviousMonthly(monthly, DateFormat.DATE_WITHOUT_DAY); if (!purchaseService.checkEmptyPurchase(userOrgCode, previousMonthly)) { previousMonthly = null; } model.addAttribute(PREVIOUS_MONTH, previousMonthly); } catch (IllegalArgumentException ex) { // } catch (ServiceException ex) { logger.error(ex.getMessage(), ex); // ??????????? model.addAttribute(ERROR_MESSAGE, getSystemError()); model.addAttribute(ERROR, true); return getViewName(); } // Check and set editable page Date getSudoDate = null; try { Tighten tighten = tightenService.findByClassifyAndMonth("1"); getSudoDate = DateFormatUtil.parse(tighten.getGetSudo(), DateFormat.DATE_WITHOUT_DAY); model.addAttribute(EDITABLE, monthly.after(getSudoDate)); } catch (ObjectNotFoundException ex) { logger.warn(ex.getMessage()); getSudoDate = DateUtil.monthsToSubtract(businessDay, 3); model.addAttribute(EDITABLE, monthly.after(getSudoDate)); } catch (IllegalArgumentException | NullPointerException | ParseException ex) { logger.warn(ex.getMessage()); model.addAttribute(EDITABLE, Boolean.FALSE); } catch (TooManyObjectsException | ServiceException ex) { logger.error(ex.getMessage(), ex); // ??????????? model.addAttribute(ERROR_MESSAGE, getSystemError()); model.addAttribute(ERROR, true); return getViewName(); } Map<String, PurchaseBean> listPurchaseData = null; List<PurchaseBean> listPurchaseHeader = null; try { // Get purchase header String getSudo = DateFormatUtil.format(monthly, DateFormat.DATE_WITHOUT_DAY); listPurchaseHeader = purchaseService.findByOrgCodeAndMonthAndSQL(orgCode, getSudo, SqlConstants.SQL_FIND_PURCHASE_DATA_HEADER_BY_ORGCODE_AND_MONTH); // Get purchase report Data listPurchaseData = purchaseService.findByOrgCodeAndMonth(orgCode, getSudo, SqlConstants.SQL_FIND_PURCHASE_DATA_BY_ORGCODE_AND_MONTH); } catch (IllegalArgumentException | ObjectNotFoundException ex) { logger.warn(ex.getMessage()); model.addAttribute(ERROR_MESSAGE, MessageHelper.get(MsgConstants.CM_QRY_M01)); model.addAttribute(ERROR, true); model.addAttribute(EDITABLE, Boolean.FALSE); } catch (ServiceException ex) { logger.error(ex.getMessage(), ex); // ??????????? model.addAttribute(ERROR_MESSAGE, getSystemError()); model.addAttribute(ERROR, true); return getViewName(); } model.addAttribute(LIST_KOUNYU_HEADER_BEAN, listPurchaseHeader); model.addAttribute(LIST_KOUNYU_DATA_BEAN, listPurchaseData); return getViewName(); } /** * Check purchase date in monthly of table data AT031 * * @param orgCode * @param monthly * @param params * @param model * @return JsonBean */ @RequestMapping(value = "/check/{orgCode:[a-z0-9]+}/{monthly:[\\d]+}", method = RequestMethod.GET) public @ResponseBody JSONBean checkPurchaseDataMonthly(@PathVariable String orgCode, @PathVariable @DateTimeFormat(pattern = "yyyyMM") Date monthly) { String processMonthly = DateFormatUtil.format(monthly, DateFormat.DATE_WITHOUT_DAY); if (logger.isDebugEnabled()) { logger.debug("Checking sales data on month: " + processMonthly + " of organizetion code: " + orgCode); } try { int saleDataOfMonthly = monthlyPurchaseService.findByOrgCodeAndMonth(orgCode, processMonthly); if (saleDataOfMonthly > 0) { // ???????????????? return new JSONBean(Boolean.TRUE, "", MessageHelper.get(MsgConstants.CM_CFM_REG_M02)); } else { // ?????????? return new JSONBean(Boolean.TRUE, "", MessageHelper.get(MsgConstants.CM_CFM_REG_M01)); } } catch (IllegalArgumentException ex) { logger.warn(ex.getMessage()); } catch (ServiceException ex) { // ??????????? logger.error(ex.getMessage(), ex); } return new JSONBean(Boolean.FALSE, "", getSystemError()); } /** * This function will be mapped with URL {@link /uriage/save/{organizationCode} * /{businessDay}}, it can be used to save sales data changed. * Will be displayed a message when have a exception or an error * occurred. * @param orgCode * @param monthly * @param params * @param model * @return JSonBean */ @RequestMapping(value = "/save/{orgCode}/{monthly}", method = RequestMethod.POST) public @ResponseBody JSONBean onSubmit(@UserPrincipal User user, @PathVariable String proGNo, @PathVariable String orgCode, @PathVariable @DateTimeFormat(pattern = "yyyyMM") String monthly, @RequestBody final JSONPurchase[] arrJSONPurchases, final Model model) { if (logger.isDebugEnabled()) { logger.debug("Updating the cash data in monthly {} into the DB for organization {}...", monthly, orgCode); } try { int actualUpdateState = purchaseService.updatePurchasesByOrgCodeAndMonth(arrJSONPurchases, user.getUsrCode(), monthly, orgCode); if (actualUpdateState == 1) { // ?\r\n\r\n?????????????\r\ n?????????? return new JSONBean(Boolean.FALSE, "", MessageHelper.get(MsgConstants.CM_UPD_M01)); } else if (actualUpdateState == 2) { // Update not success return new JSONBean(Boolean.FALSE, "", MessageHelper.get(MsgConstants.CM_UPD_M03)); } } catch (ServiceException ex) { logger.error("An exception occured while updating the cash data in monthly for organization" + orgCode, ex); // ??????????? return new JSONBean(Boolean.FALSE, "", getSystemError()); } return new JSONBean(Boolean.TRUE, "", MessageHelper.get(MsgConstants.CM_UPD_M02)); } @Override public String getViewName() { return "kounyu/kounyu"; } @Override public String getTitleName() { return ""; } }