1 <!-- $Id: Dao.jsp 474 2011-08-21 10:48:48Z dobashi $ -->
\r
2 <!doctype html public "-//w3c//dtd html 4.0 transitional//en"
\r
3 "http://www.w3.org/tr/rec-html40/loose.dtd">
\r
4 <%@ page contentType="text/html; charset=UTF-8" %>
\r
5 <%@ include file="../common/common.jsp"
\r
6 %><%@ page import="java.util.*"
\r
7 %><%@ page import="java.text.*"
\r
8 %><%@ page import="com.lavans.lacoder.model.Package"
\r
9 %><%@ page import="com.lavans.lacoder.model.*"
\r
11 Package pkg = (Package)request.getAttribute("lacoder.package");
\r
12 Entity entity = (Entity)request.getAttribute("lacoder.entity");
\r
13 String className = entity.getClassName();
\r
14 SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
\r
15 Attribute attrId = entity.get(0);
\r
20 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
\r
21 <LINK href="red.css" rel="stylesheet" type="text/css">
\r
23 <body text="#000000" leftmargin="10" topmargin="10" marginwidth="10" marginheight="10">
\r
25 /* $Id: Dao.jsp 474 2011-08-21 10:48:48Z dobashi $
\r
26 * created: <%= sdf.format(new Date()) %>
\r
28 * This source code is generated by lacoder.
\r
29 * @see <%= entity.getClassName() %>Service
\r
30 * @see http://www.lavans.com/soft/lacoder/
\r
32 package <%= pkg.getDomainSubPackagePath() %>.dao;
\r
34 import java.lang.reflect.InvocationTargetException;
\r
35 import java.sql.SQLException;
\r
36 import java.util.Arrays;
\r
37 import java.util.List;
\r
38 import java.util.Map;
\r
39 import java.util.HashMap;
\r
40 import org.apache.commons.logging.Log;
\r
41 import org.apache.commons.logging.LogFactory;
\r
42 import com.lavans.luz2.commons.DaoUtils;
\r
43 import com.lavans.luz2.sql.dao.CommonDao;
\r
44 import com.lavans.luz2.util.BeanManager;
\r
45 import com.lavans.luz2.util.PageInfo;
\r
46 import com.lavans.luz2.util.Pager;
\r
48 import <%= pkg.getDomainSubPackagePath() %>.entity.<%= className %>;
\r
49 <% if(entity.hasBackup()){ %>import <%= pkg.getDomainSubPackagePath() %>.entity.<%= className %>Bak;<% } %>
\r
52 * <%= entity.getClassName() %>Daoクラス。
\r
56 public class <%= className %>Dao{
\r
57 private static Log logger = LogFactory.getLog(<%= className %>Dao.class.getName()+"Base");
\r
58 private CommonDao dao = BeanManager.getBean(CommonDao.class);
\r
61 public <%= className %>Dao(){
\r
67 public <%= className %> load(<%= className %>.PK pk) throws SQLException{
\r
68 String sql = DaoUtils.getSql(<%= className %>Dao.class.getName()+"Base", "load");
\r
69 List<<%= className %>> list = dao.list(<%= className %>.class, sql, pk.getAttributeMap());
\r
71 logger.debug("target not found.");
\r
82 * @throws SQLException
\r
83 * @throws InstantiationException
\r
84 * @throws IllegalAccessException
\r
85 * @throws NoSuchMethodException
\r
86 * @throws InvocationTargetException
\r
88 public int insert(<%= className %> entity) throws SQLException{
\r
89 <% if(attrId.isSequence()){ // get nextseq
\r
90 %> // get next sequence
\r
91 String seqSql = DaoUtils.getSql(<%= className %>Dao.class.getName()+"Base", "nextval");
\r
92 List<Map<String, Object>> seqResult = dao.executeQuery(seqSql);
\r
93 long seq = (Long)seqResult.get(0).values().toArray()[0];
\r
94 entity.<%= attrId.getSetterName() %>(seq);
\r
98 String sql = DaoUtils.getSql(<%= className %>Dao.class.getName()+"Base", "insert");
\r
99 int result = dao.executeUpdate(sql, entity.getAttributeMap());
\r
101 logger.debug("insert failure.");
\r
111 * @throws SQLException
\r
112 * @throws InstantiationException
\r
113 * @throws IllegalAccessException
\r
114 * @throws NoSuchMethodException
\r
115 * @throws InvocationTargetException
\r
117 public int update(<%= className %> entity) throws SQLException{
\r
119 String sql = DaoUtils.getSql(<%= className %>Dao.class.getName()+"Base", "update");
\r
120 int result = dao.executeUpdate(sql, entity.getAttributeMap());
\r
122 logger.debug("update failure.["+ result +"]");
\r
133 * @throws SQLException
\r
135 public int delete(<%= className %>.PK pk) throws SQLException{
\r
137 String sql = DaoUtils.getSql(<%= className %>Dao.class.getName()+"Base", "delete");
\r
138 int result = dao.executeUpdate(sql, pk.getAttributeMap());
\r
140 logger.debug("delete failure.["+ result +"]");
\r
147 * list with conditions.
\r
148 * @param searchCondMap
\r
150 * @throws SQLException
\r
151 * @throws InvocationTargetException
\r
152 * @throws NoSuchMethodException
\r
153 * @throws IllegalAccessException
\r
154 * @throws InstantiationException
\r
156 public List<<%= className %>> list(Map<String, String[]> cond) throws SQLException{
\r
157 // copy for editng key. ex) "name" to "%name%"
\r
158 Map<String, String[]> condWork = new HashMap<String, String[]>();
\r
159 for(Map.Entry<String, String[]> entry: cond.entrySet()){
\r
160 condWork.put(entry.getKey(), Arrays.copyOf(entry.getValue(), entry.getValue().length));
\r
164 String sql = DaoUtils.getSql(<%= className %>Dao.class.getName()+"Base", "list");
\r
165 String condition = DaoUtils.getWherePhrase(<%= className %>.getAttributeInfo().keySet(), condWork);
\r
166 if(condition.length()>4){
\r
167 sql += "\nWHERE "+ condition.substring(4);
\r
169 List<<%= className %>> list = dao.list(<%= className %>.class, sql, DaoUtils.convertSearchCond(condWork, <%= className %>.getAttributeInfo()));
\r
175 * list with conditions.
\r
176 * @param searchCondMap
\r
178 * @throws SQLException
\r
179 * @throws InvocationTargetException
\r
180 * @throws NoSuchMethodException
\r
181 * @throws IllegalAccessException
\r
182 * @throws InstantiationException
\r
184 public Pager<<%= className %>> pager(PageInfo pageInfo, Map<String, String[]> cond) throws SQLException{
\r
185 // copy for editng key. ex) "name" to "%name%"
\r
186 Map<String, String[]> condWork = new HashMap<String, String[]>();
\r
187 for(Map.Entry<String, String[]> entry: cond.entrySet()){
\r
188 condWork.put(entry.getKey(), Arrays.copyOf(entry.getValue(), entry.getValue().length));
\r
192 String condition = DaoUtils.getWherePhrase(<%= className %>.getAttributeInfo().keySet(), condWork);
\r
193 if(condition.length()>4){
\r
194 condition = "WHERE "+ condition.substring(4);
\r
196 Map<String, Object> searchCond = DaoUtils.convertSearchCond(condWork, <%= className %>.getAttributeInfo());
\r
199 String seqSql = DaoUtils.getSql(<%= className %>Dao.class.getName()+"Base", "count");
\r
200 seqSql = seqSql.replace("$condition",condition);
\r
201 List<Map<String, Object>> seqResult = dao.executeQuery(seqSql, searchCond);
\r
202 long count = (Long)seqResult.get(0).values().toArray()[0];
\r
205 String sql = DaoUtils.getSql(<%= className %>Dao.class.getName()+"Base", "pager");
\r
206 sql = sql.replace("$condition",condition);
\r
207 searchCond.put("_rows", pageInfo.getRows());
\r
208 searchCond.put("_offset", pageInfo.getPage()*pageInfo.getRows());
\r
209 logger.debug(searchCond);
\r
210 List<<%= className %>> list = dao.list(<%= className %>.class, sql, searchCond);
\r
213 Pager<<%= className %>> pager = new Pager<<%= className %>>(pageInfo);
\r
214 pager.setTotalCount(count);
\r
215 for(<%= className %> entity: list){
\r
221 <% if(entity.hasBackup()){ %>
\r
223 * backup entity which has _BAK table.
\r
226 * @throws SQLException
\r
228 public int backup(<%= className %>.PK pk) throws SQLException{
\r
229 // get backup next sequence
\r
230 String seqSql = DaoUtils.getSql(<%= className %>Dao.class.getName()+"Base", "backupNextval");
\r
231 List<Map<String, Object>> seqResult = dao.executeQuery(seqSql);
\r
232 long seq = (Long)seqResult.get(0).values().toArray()[0];
\r
234 // insert to backup table
\r
235 String sql = DaoUtils.getSql(<%= className %>Dao.class.getName()+"Base", "backup");
\r
236 Map<String, Object> searchCond = new HashMap<String, Object>();
\r
237 searchCond.put("pk", seq);
\r
238 searchCond.put("<%= attrId.getName() %>", pk.<%= attrId.getGetterName() %>());
\r
239 int result = dao.executeUpdate(sql, searchCond);
\r
241 logger.debug("backup failure.["+ result +"]");
\r
248 * list with conditions.
\r
249 * @param searchCondMap
\r
251 * @throws SQLException
\r
252 * @throws InvocationTargetException
\r
253 * @throws NoSuchMethodException
\r
254 * @throws IllegalAccessException
\r
255 * @throws InstantiationException
\r
257 public Pager<<%= className %>Bak> pagerBak(PageInfo pageInfo, Map<String, String[]> cond) throws SQLException{
\r
258 // copy for editng key. ex) "name" to "%name%"
\r
259 Map<String, String[]> condWork = new HashMap<String, String[]>();
\r
260 for(Map.Entry<String, String[]> entry: cond.entrySet()){
\r
261 condWork.put(entry.getKey(), Arrays.copyOf(entry.getValue(), entry.getValue().length));
\r
265 String condition = DaoUtils.getWherePhrase(<%= className %>.getAttributeInfo().keySet(), condWork);
\r
266 if(condition.length()>4){
\r
267 condition = "WHERE "+ condition.substring(4);
\r
269 Map<String, Object> searchCond = DaoUtils.convertSearchCond(condWork, <%= className %>.getAttributeInfo());
\r
272 String seqSql = DaoUtils.getSql(<%= className %>Dao.class.getName()+"Base", "count");
\r
273 seqSql = seqSql.replace("$condition",condition);
\r
274 List<Map<String, Object>> seqResult = dao.executeQuery(seqSql, searchCond);
\r
275 long count = (Long)seqResult.get(0).values().toArray()[0];
\r
278 String sql = DaoUtils.getSql(<%= className %>Dao.class.getName()+"Base", "pager");
\r
279 sql = sql.replace("$condition",condition);
\r
280 searchCond.put("_rows", pageInfo.getRows());
\r
281 searchCond.put("_offset", pageInfo.getPage()*pageInfo.getRows());
\r
282 logger.debug(searchCond);
\r
283 List<<%= className %>Bak> list = dao.list(<%= className %>Bak.class, sql, searchCond);
\r
286 Pager<<%= className %>Bak> pager = new Pager<<%= className %>Bak>(pageInfo);
\r
287 pager.setTotalCount(count);
\r
288 for(<%= className %>Bak entity: list){
\r
296 * restore entity from _BAK table.
\r
299 * @throws SQLException
\r
301 public int from(<%= className %>.PK pk) throws SQLException{
\r
302 // insert to backup table
\r
303 String sql = DaoUtils.getSql(<%= className %>Dao.class.getName()+"Base", "restore");
\r
304 Map<String, Object> searchCond = new HashMap<String, Object>();
\r
305 searchCond.put("<%= attrId.getName() %>", pk.<%= attrId.getGetterName() %>());
\r
306 int result = dao.executeUpdate(sql, searchCond);
\r
308 logger.debug("backup failure.["+ result +"]");
\r
316 <!-- <%= debugStr %> -->
\r