%@ page contentType="text/html; charset=UTF-8" %> <%@ include file="../common/common.jsp" %><%@ page import="java.util.*" %><%@ page import="java.text.*" %><%@ page import="com.lavans.lacoder.model.Package" %><%@ page import="com.lavans.lacoder.model.*" %><% Package pkg = (Package)request.getAttribute("lacoder.package"); Entity entity = (Entity)request.getAttribute("lacoder.entity"); String className = entity.getClassName(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); Attribute attrId = entity.get(0); %>
/* $Id: Dao.jsp 474 2011-08-21 10:48:48Z dobashi $ * created: <%= sdf.format(new Date()) %> * * This source code is generated by lacoder. * @see <%= entity.getClassName() %>Service * @see http://www.lavans.com/soft/lacoder/ */ package <%= pkg.getDomainSubPackagePath() %>.dao; import java.lang.reflect.InvocationTargetException; import java.sql.SQLException; import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.HashMap; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import com.lavans.luz2.commons.DaoUtils; import com.lavans.luz2.sql.dao.CommonDao; import com.lavans.luz2.util.BeanManager; import com.lavans.luz2.util.PageInfo; import com.lavans.luz2.util.Pager; import <%= pkg.getDomainSubPackagePath() %>.entity.<%= className %>; <% if(entity.hasBackup()){ %>import <%= pkg.getDomainSubPackagePath() %>.entity.<%= className %>Bak;<% } %> /** * <%= entity.getClassName() %>Daoクラス。 * @author ${user} * */ public class <%= className %>Dao{ private static Log logger = LogFactory.getLog(<%= className %>Dao.class.getName()+"Base"); private CommonDao dao = BeanManager.getBean(CommonDao.class); /** Constructor */ public <%= className %>Dao(){ } /** * load */ public <%= className %> load(<%= className %>.PK pk) throws SQLException{ String sql = DaoUtils.getSql(<%= className %>Dao.class.getName()+"Base", "load"); List<<%= className %>> list = dao.list(<%= className %>.class, sql, pk.getAttributeMap()); if(list.size()==0){ logger.debug("target not found."); return null; } return list.get(0); } /** * insert * @param entity * @return * @throws SQLException * @throws InstantiationException * @throws IllegalAccessException * @throws NoSuchMethodException * @throws InvocationTargetException */ public int insert(<%= className %> entity) throws SQLException{ <% if(attrId.isSequence()){ // get nextseq %> // get next sequence String seqSql = DaoUtils.getSql(<%= className %>Dao.class.getName()+"Base", "nextval"); List<Map<String, Object>> seqResult = dao.executeQuery(seqSql); long seq = (Long)seqResult.get(0).values().toArray()[0]; entity.<%= attrId.getSetterName() %>(seq); <% } %> // String sql = DaoUtils.getSql(<%= className %>Dao.class.getName()+"Base", "insert"); int result = dao.executeUpdate(sql, entity.getAttributeMap()); if(result!=0){ logger.debug("insert failure."); } return result; } /** * update * @param entity * @return * @throws SQLException * @throws InstantiationException * @throws IllegalAccessException * @throws NoSuchMethodException * @throws InvocationTargetException */ public int update(<%= className %> entity) throws SQLException{ // update String sql = DaoUtils.getSql(<%= className %>Dao.class.getName()+"Base", "update"); int result = dao.executeUpdate(sql, entity.getAttributeMap()); if(result!=0){ logger.debug("update failure.["+ result +"]"); } return result; } /** * delete * * @param pk * @return * @throws SQLException */ public int delete(<%= className %>.PK pk) throws SQLException{ // delete String sql = DaoUtils.getSql(<%= className %>Dao.class.getName()+"Base", "delete"); int result = dao.executeUpdate(sql, pk.getAttributeMap()); if(result!=1){ logger.debug("delete failure.["+ result +"]"); } return result; } /** * list with conditions. * @param searchCondMap * @return * @throws SQLException * @throws InvocationTargetException * @throws NoSuchMethodException * @throws IllegalAccessException * @throws InstantiationException */ public List<<%= className %>> list(Map<String, String[]> cond) throws SQLException{ // copy for editng key. ex) "name" to "%name%" Map<String, String[]> condWork = new HashMap<String, String[]>(); for(Map.Entry<String, String[]> entry: cond.entrySet()){ condWork.put(entry.getKey(), Arrays.copyOf(entry.getValue(), entry.getValue().length)); } // data String sql = DaoUtils.getSql(<%= className %>Dao.class.getName()+"Base", "list"); String condition = DaoUtils.getWherePhrase(<%= className %>.getAttributeInfo().keySet(), condWork); if(condition.length()>4){ sql += "\nWHERE "+ condition.substring(4); } List<<%= className %>> list = dao.list(<%= className %>.class, sql, DaoUtils.convertSearchCond(condWork, <%= className %>.getAttributeInfo())); return list; } /** * list with conditions. * @param searchCondMap * @return * @throws SQLException * @throws InvocationTargetException * @throws NoSuchMethodException * @throws IllegalAccessException * @throws InstantiationException */ public Pager<<%= className %>> pager(PageInfo pageInfo, Map<String, String[]> cond) throws SQLException{ // copy for editng key. ex) "name" to "%name%" Map<String, String[]> condWork = new HashMap<String, String[]>(); for(Map.Entry<String, String[]> entry: cond.entrySet()){ condWork.put(entry.getKey(), Arrays.copyOf(entry.getValue(), entry.getValue().length)); } // query condition String condition = DaoUtils.getWherePhrase(<%= className %>.getAttributeInfo().keySet(), condWork); if(condition.length()>4){ condition = "WHERE "+ condition.substring(4); } Map<String, Object> searchCond = DaoUtils.convertSearchCond(condWork, <%= className %>.getAttributeInfo()); // count String seqSql = DaoUtils.getSql(<%= className %>Dao.class.getName()+"Base", "count"); seqSql = seqSql.replace("$condition",condition); List<Map<String, Object>> seqResult = dao.executeQuery(seqSql, searchCond); long count = (Long)seqResult.get(0).values().toArray()[0]; // data String sql = DaoUtils.getSql(<%= className %>Dao.class.getName()+"Base", "pager"); sql = sql.replace("$condition",condition); searchCond.put("_rows", pageInfo.getRows()); searchCond.put("_offset", pageInfo.getPage()*pageInfo.getRows()); logger.debug(searchCond); List<<%= className %>> list = dao.list(<%= className %>.class, sql, searchCond); // add to pager Pager<<%= className %>> pager = new Pager<<%= className %>>(pageInfo); pager.setTotalCount(count); for(<%= className %> entity: list){ pager.add(entity); } return pager; } <% if(entity.hasBackup()){ %> /** * backup entity which has _BAK table. * @param entity * @return * @throws SQLException */ public int backup(<%= className %>.PK pk) throws SQLException{ // get backup next sequence String seqSql = DaoUtils.getSql(<%= className %>Dao.class.getName()+"Base", "backupNextval"); List<Map<String, Object>> seqResult = dao.executeQuery(seqSql); long seq = (Long)seqResult.get(0).values().toArray()[0]; // insert to backup table String sql = DaoUtils.getSql(<%= className %>Dao.class.getName()+"Base", "backup"); Map<String, Object> searchCond = new HashMap<String, Object>(); searchCond.put("pk", seq); searchCond.put("<%= attrId.getName() %>", pk.<%= attrId.getGetterName() %>()); int result = dao.executeUpdate(sql, searchCond); if(result!=0){ logger.debug("backup failure.["+ result +"]"); } return result; } /** * list with conditions. * @param searchCondMap * @return * @throws SQLException * @throws InvocationTargetException * @throws NoSuchMethodException * @throws IllegalAccessException * @throws InstantiationException */ public Pager<<%= className %>Bak> pagerBak(PageInfo pageInfo, Map<String, String[]> cond) throws SQLException{ // copy for editng key. ex) "name" to "%name%" Map<String, String[]> condWork = new HashMap<String, String[]>(); for(Map.Entry<String, String[]> entry: cond.entrySet()){ condWork.put(entry.getKey(), Arrays.copyOf(entry.getValue(), entry.getValue().length)); } // query condition String condition = DaoUtils.getWherePhrase(<%= className %>.getAttributeInfo().keySet(), condWork); if(condition.length()>4){ condition = "WHERE "+ condition.substring(4); } Map<String, Object> searchCond = DaoUtils.convertSearchCond(condWork, <%= className %>.getAttributeInfo()); // count String seqSql = DaoUtils.getSql(<%= className %>Dao.class.getName()+"Base", "count"); seqSql = seqSql.replace("$condition",condition); List<Map<String, Object>> seqResult = dao.executeQuery(seqSql, searchCond); long count = (Long)seqResult.get(0).values().toArray()[0]; // data String sql = DaoUtils.getSql(<%= className %>Dao.class.getName()+"Base", "pager"); sql = sql.replace("$condition",condition); searchCond.put("_rows", pageInfo.getRows()); searchCond.put("_offset", pageInfo.getPage()*pageInfo.getRows()); logger.debug(searchCond); List<<%= className %>Bak> list = dao.list(<%= className %>Bak.class, sql, searchCond); // add to pager Pager<<%= className %>Bak> pager = new Pager<<%= className %>Bak>(pageInfo); pager.setTotalCount(count); for(<%= className %>Bak entity: list){ pager.add(entity); } return pager; } /** * restore entity from _BAK table. * @param entity * @return * @throws SQLException */ public int from(<%= className %>.PK pk) throws SQLException{ // insert to backup table String sql = DaoUtils.getSql(<%= className %>Dao.class.getName()+"Base", "restore"); Map<String, Object> searchCond = new HashMap<String, Object>(); searchCond.put("<%= attrId.getName() %>", pk.<%= attrId.getGetterName() %>()); int result = dao.executeUpdate(sql, searchCond); if(result!=0){ logger.debug("backup failure.["+ result +"]"); } return result; } <% } %> }