1 30 package com.genimen.djeneric.repository.rdbms; 31 32 import java.sql.ResultSet ; 33 import java.sql.SQLException ; 34 35 import com.genimen.djeneric.repository.DjContext; 36 import com.genimen.djeneric.repository.DjSession; 37 import com.genimen.djeneric.repository.DjUser; 38 import com.genimen.djeneric.repository.DjUserContextAssociation; 39 import com.genimen.djeneric.repository.exceptions.DjenericException; 40 41 public class RdbmsUserContextAssociation extends DjUserContextAssociation 42 { 43 protected RdbmsUserContextAssociation() 45 { 46 } 47 48 protected RdbmsUserContextAssociation(DjUser dev, DjContext ctxt, boolean canCreate, boolean canModify, 49 boolean canDelete, boolean canQuery) 50 { 51 super(dev, ctxt, canCreate, canModify, canDelete, canQuery); 52 } 53 54 public void delete(DjSession session) throws DjenericException 55 { 56 try 57 { 58 SqlStatement stmt = ((RdbmsSession) session).getInternalSqlStatement("delete from " 59 + RdbmsPersistenceManager.USER_CTX_TABLE 60 + " where user_id = :devId " 61 + "and context_id = :ctxtId "); 62 stmt.setLong("devId", getUser().getId()); 63 stmt.setLong("ctxtId", getContext().getId()); 64 stmt.executeUpdate(); 65 } 66 catch (SQLException x) 67 { 68 throw new DjenericException(x); 69 } 70 } 71 72 public void reload(DjSession session) throws DjenericException 73 { 74 try 75 { 76 SqlStatement stmt = ((RdbmsSession) session).getInternalSqlStatement("select cre_yn, mod_yn, del_yn, qry_yn " 77 + "from " 78 + RdbmsPersistenceManager.USER_CTX_TABLE 79 + " where user_id = :devId " 80 + "and context_id = :ctxtId "); 81 stmt.setLong("devId", getUser().getId()); 82 stmt.setLong("ctxtId", getContext().getId()); 83 ResultSet rs = stmt.executeQuery(); 84 if (rs.next()) 85 { 86 setCreate(rs.getString("cre_yn").equalsIgnoreCase("Y")); 87 setModify(rs.getString("mod_yn").equalsIgnoreCase("Y")); 88 setDelete(rs.getString("del_yn").equalsIgnoreCase("Y")); 89 setQuery(rs.getString("qry_yn").equalsIgnoreCase("Y")); 90 } 91 rs.close(); 92 stmt.close(); 93 } 94 catch (SQLException x) 95 { 96 throw new DjenericException(x); 97 } 98 } 99 100 public void persist(DjSession session) throws DjenericException 101 { 102 try 103 { 104 SqlStatement stmt = ((RdbmsSession) session) 105 .getInternalSqlStatement("update " + RdbmsPersistenceManager.USER_CTX_TABLE 106 + " set cre_yn = :cre_yn, mod_yn = :mod_yn, del_yn = :del_yn, qry_yn = :qry_yn " 107 + "where user_id = :devId " + "and context_id = :ctxtId "); 108 109 stmt.setLong("devId", getUser().getId()); 110 stmt.setLong("ctxtId", getContext().getId()); 111 stmt.setString("cre_yn", isCreate() ? "Y" : "N"); 112 stmt.setString("mod_yn", isModify() ? "Y" : "N"); 113 stmt.setString("del_yn", isDelete() ? "Y" : "N"); 114 stmt.setString("qry_yn", isQuery() ? "Y" : "N"); 115 if (stmt.executeUpdate() == 0) 116 { 117 insert(session); 119 } 120 } 121 catch (SQLException x) 122 { 123 throw new DjenericException(x); 124 } 125 } 126 127 public void insert(DjSession session) throws DjenericException 128 { 129 try 130 { 131 SqlStatement stmt = ((RdbmsSession) session) 132 .getInternalSqlStatement("insert into " + RdbmsPersistenceManager.USER_CTX_TABLE 133 + "(user_id, context_id, cre_yn, mod_yn, del_yn, qry_yn) " 134 + "values (:user_id, :context_id, :cre_yn, :mod_yn, :del_yn, :qry_yn)"); 135 stmt.setLong("user_id", getUser().getId()); 136 stmt.setLong("context_id", getContext().getId()); 137 stmt.setString("cre_yn", isCreate() ? "Y" : "N"); 138 stmt.setString("mod_yn", isModify() ? "Y" : "N"); 139 stmt.setString("del_yn", isDelete() ? "Y" : "N"); 140 stmt.setString("qry_yn", isQuery() ? "Y" : "N"); 141 stmt.executeUpdate(); 142 session.commit(); 143 } 144 catch (SQLException x) 145 { 146 throw new DjenericException(x); 147 } 148 } 149 } | Popular Tags |