Java tutorial
/* * Copyright 2004-2008 the Seasar Foundation and the Others. * * 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 churashima.action.manage; import java.math.BigDecimal; import java.sql.SQLException; import java.sql.Timestamp; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.List; import javax.annotation.Resource; import org.apache.commons.lang.StringUtils; import org.apache.struts.action.ActionMessage; import org.apache.struts.action.ActionMessages; import org.seasar.framework.container.SingletonS2Container; import org.seasar.framework.exception.SQLRuntimeException; import org.seasar.struts.annotation.ActionForm; import org.seasar.struts.annotation.Execute; import org.seasar.struts.util.ActionMessagesUtil; import org.seasar.struts.util.RequestUtil; import churashima.dao.ExpensesDao; import churashima.dao.SubjectDao; import churashima.dao.TaxDao; import churashima.dto.LoginInfoDto; import churashima.entity.Expenses; import churashima.form.ExpensesForm; import churashima.service.Const; public class ExpensesAction { @ActionForm @Resource public ExpensesForm expensesForm; @Resource public LoginInfoDto loginInfoDto; @Execute(validator = false) public String index() { Date date = new Date(); if (StringUtils.isEmpty(expensesForm.strDate)) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd"); expensesForm.strDate = sdf.format(date); } Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); ExpensesDao expensesDao = SingletonS2Container.getComponent(ExpensesDao.class); List<Expenses> expensesList = expensesDao.selectForSearch(null, null, null, null, null, null, null, null, new Timestamp(cal.getTimeInMillis())); expensesForm.expensesList = expensesList; // expensesForm.subjectId = null; expensesForm.place = null; expensesForm.name = null; expensesForm.price = BigDecimal.valueOf(0); expensesForm.comment = null; expensesForm.isExcludeTax = null; SubjectDao subjectDao = SingletonS2Container.getComponent(SubjectDao.class); expensesForm.subjectList = subjectDao.selectForSearch(null, Const.STATUS_STARTING); return "expenses.jsp"; } @Execute(validator = false) public String searchInit() { ExpensesDao expensesDao = SingletonS2Container.getComponent(ExpensesDao.class); List<Expenses> expensesList = expensesDao.selectForSearch(null, null, null, null, null, null, expensesForm.searchPrice, null, null); expensesForm.expensesList = expensesList; // ?? BigDecimal sumPriceTotal = BigDecimal.valueOf(0); for (Expenses expenses : expensesList) { sumPriceTotal = sumPriceTotal.add(expenses.price); } expensesForm.sumPriceTotal = sumPriceTotal; SubjectDao subjectDao = SingletonS2Container.getComponent(SubjectDao.class); expensesForm.subjectList = subjectDao.selectForSearch(null, null); return "expensesList.jsp"; } @Execute(validator = false) public String search() { String strSerachPlace = null; if (StringUtils.isNotEmpty(expensesForm.searchPlace)) { strSerachPlace = "%" + expensesForm.searchPlace + "%"; } String strSerachName = null; if (StringUtils.isNotEmpty(expensesForm.searchName)) { strSerachName = "%" + expensesForm.searchName + "%"; } Long longSubjectId = null; if (expensesForm.searchSubjectId != null && !"all".equals(expensesForm.searchSubjectId)) { longSubjectId = Long.valueOf(expensesForm.searchSubjectId); } SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd"); Date dateFrom = null; if (StringUtils.isNotEmpty(expensesForm.searchDayFrom)) { try { dateFrom = sdf.parse(expensesForm.searchDayFrom); } catch (ParseException e) { e.printStackTrace(); } } Date dateTo = null; if (StringUtils.isNotEmpty(expensesForm.searchDayTo)) { try { dateTo = sdf.parse(expensesForm.searchDayTo); } catch (ParseException e) { e.printStackTrace(); } } ExpensesDao expensesDao = SingletonS2Container.getComponent(ExpensesDao.class); List<Expenses> expensesList = expensesDao.selectForSearch(null, longSubjectId, strSerachPlace, strSerachName, dateFrom, dateTo, expensesForm.searchPrice, null, null); SubjectDao subjectDao = SingletonS2Container.getComponent(SubjectDao.class); expensesForm.subjectList = subjectDao.selectForSearch(null, null); expensesForm.expensesList = expensesList; // ?? BigDecimal sumPriceTotal = BigDecimal.valueOf(0); for (Expenses expenses : expensesList) { sumPriceTotal = sumPriceTotal.add(expenses.price); } expensesForm.sumPriceTotal = sumPriceTotal; return "expensesList.jsp"; } @Execute(input = "expenses.jsp") public String entryExecute() { ExpensesDao expensesDao = SingletonS2Container.getComponent(ExpensesDao.class); SubjectDao subjectDao = SingletonS2Container.getComponent(SubjectDao.class); Expenses expenses = new Expenses(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd"); try { expenses.date = sdf.parse(expensesForm.strDate); } catch (ParseException e) { e.printStackTrace(); } if (expensesForm.subjectId == null) { ActionMessages errors = new ActionMessages(); errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("errors.required", //?????? new Object[] { "" })); ActionMessagesUtil.addErrors(RequestUtil.getRequest(), errors); expensesForm.subjectList = subjectDao.selectForSearch(null, null); return "expenses.jsp"; } expenses.subjectId = expensesForm.subjectId; expenses.place = expensesForm.place; expenses.name = expensesForm.name; expenses.comment = expensesForm.comment; expenses.category = expensesForm.category; expenses.categoryDetail = expensesForm.detail; expenses.openFlg = expensesForm.openFlg; expenses.isExcludeTax = expensesForm.isExcludeTax; TaxDao taxDao = SingletonS2Container.getComponent(TaxDao.class); BigDecimal tax = taxDao.selectByDate(expenses.date); if (Const.TAX_EXCLUDE.equals(expensesForm.isExcludeTax)) { // ????? expenses.priceTax = expensesForm.price.multiply(tax); expenses.priceExcludeTax = expensesForm.price; expenses.price = expenses.priceExcludeTax.add(expenses.priceTax); } else { // ???? expenses.price = expensesForm.price; expenses.priceExcludeTax = expensesForm.price.divide(BigDecimal.valueOf(1).add(tax), 3, BigDecimal.ROUND_HALF_UP); // ? expenses.priceTax = expenses.price.subtract(expenses.priceExcludeTax); } // Timestamp now = new Timestamp(System.currentTimeMillis()); expenses.deleted = false; expenses.updDate = now; expenses.updId = null; expenses.insDate = now; expenses.insId = null; try { expensesDao.insert(expenses); } catch (SQLRuntimeException e) { SQLException e2 = (SQLException) e.getCause(); if (e2.getErrorCode() == 0) { //??? ActionMessages errors = new ActionMessages(); errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("errors.already.entried", //?????? new Object[] { "????" })); ActionMessagesUtil.addErrors(RequestUtil.getRequest(), errors); return "/manage/expenses"; } } return "/manage/expenses"; } @Execute(validator = false, urlPattern = "delete/{expensesId}") public String delete() { ExpensesDao expensesDao = SingletonS2Container.getComponent(ExpensesDao.class); Expenses expenses = expensesDao.selectById(Long.valueOf(expensesForm.expensesId)); expensesDao.delete(expenses); return "/manage/expenses/searchInit"; } @Execute(validator = false, urlPattern = "edit/{expensesId}/{fromPageId}") public String edit() { ExpensesDao expensesDao = SingletonS2Container.getComponent(ExpensesDao.class); Expenses expenses = expensesDao.selectById(Long.valueOf(expensesForm.expensesId)); expensesForm.expensesId = String.valueOf(expenses.expensesId); expensesForm.kind = String.valueOf(expenses.kind); SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd"); expensesForm.strDate = sdf.format(expenses.date); expensesForm.place = expenses.place; expensesForm.subjectId = expenses.subjectId; expensesForm.name = expenses.name; expensesForm.category = expenses.category; expensesForm.detail = expenses.categoryDetail; expensesForm.openFlg = expenses.openFlg; expensesForm.comment = expenses.comment; expensesForm.isExcludeTax = expenses.isExcludeTax; if (Const.TAX_EXCLUDE.equals(expenses.isExcludeTax)) { expensesForm.price = expenses.priceExcludeTax; } else { expensesForm.price = expenses.price; } SubjectDao subjectDao = SingletonS2Container.getComponent(SubjectDao.class); expensesForm.subjectList = subjectDao.selectForSearch(null, null); // // ?????subject? // boolean isExist = false; // for (Subject subject : expensesForm.subjectList) { // if (subject.subjectId == expenses.subjectId) { // isExist = true; // } // } // if (!isExist) { // // ?????? // Subject s = new Subject(); // s.subjectId = expenses.subjectId; // Subject ret = subjectDao.selectById(expenses.subjectId); // s.name = ret.name; // expensesForm.subjectList.add(s); // } return "expensesEdit.jsp"; } @Execute(validator = false) public String editExecute() { ExpensesDao expensesDao = SingletonS2Container.getComponent(ExpensesDao.class); Expenses expenses = expensesDao.selectById(Long.valueOf(expensesForm.expensesId)); SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd"); try { expenses.date = sdf.parse(expensesForm.strDate); } catch (ParseException e) { e.printStackTrace(); } expenses.subjectId = expensesForm.subjectId; expenses.place = expensesForm.place; expenses.name = expensesForm.name; expenses.price = expensesForm.price; expenses.category = expensesForm.category; expenses.categoryDetail = expensesForm.detail; expenses.openFlg = expensesForm.openFlg; expenses.comment = expensesForm.comment; expenses.isExcludeTax = expensesForm.isExcludeTax; TaxDao taxDao = SingletonS2Container.getComponent(TaxDao.class); BigDecimal tax = taxDao.selectByDate(expenses.date); if (Const.TAX_EXCLUDE.equals(expensesForm.isExcludeTax)) { // ????? expenses.priceTax = expensesForm.price.multiply(tax); expenses.priceExcludeTax = expensesForm.price; expenses.price = expenses.priceExcludeTax.add(expenses.priceTax); } else { // ???? expenses.price = expensesForm.price; expenses.priceExcludeTax = expensesForm.price.divide(BigDecimal.valueOf(1).add(tax), 3, BigDecimal.ROUND_HALF_UP); // ? expenses.priceTax = expenses.price.subtract(expenses.priceExcludeTax); } expensesDao.update(expenses); if ("expensesList".equals(expensesForm.fromPageId)) { // ?????? return "search"; } else { // ?????? return "/manage/expenses"; } } }