1 30 package com.genimen.djeneric.repository.rdbms; 31 32 import java.sql.ResultSet ; 33 import java.sql.SQLException ; 34 import java.util.ArrayList ; 35 36 import com.genimen.djeneric.language.Messages; 37 import com.genimen.djeneric.repository.DjContext; 38 import com.genimen.djeneric.repository.DjContextManager; 39 import com.genimen.djeneric.repository.DjPersistenceManager; 40 import com.genimen.djeneric.repository.DjUser; 41 import com.genimen.djeneric.repository.DjUserContextAssociation; 42 import com.genimen.djeneric.repository.exceptions.DjenericException; 43 import com.genimen.djeneric.repository.exceptions.LogonException; 44 45 public class RdbmsContextManager extends DjContextManager 46 { 47 public RdbmsContextManager(DjPersistenceManager mgr) 48 { 49 super(mgr); 50 } 51 52 public DjContext[] getContexts() throws DjenericException 53 { 54 try 55 { 56 ArrayList result = new ArrayList (); 57 RdbmsSession session = (RdbmsSession) getPersistenceManager().createSession(); 58 try 59 { 60 SqlStatement stmt = session.getInternalSqlStatement("select id, code, name from " 61 + RdbmsPersistenceManager.CONTEXT_TABLE + " order by name"); 62 ResultSet rs = stmt.executeQuery(); 63 while (rs.next()) 64 { 65 RdbmsContext ctxt = new RdbmsContext(getPersistenceManager(), rs.getLong("id"), rs.getString("code"), rs 66 .getString("name")); 67 result.add(ctxt); 68 } 69 rs.close(); 70 stmt.close(); 71 } 72 finally 73 { 74 session.close(); 75 } 76 return (DjContext[]) result.toArray(new DjContext[0]); 77 } 78 catch (SQLException x) 79 { 80 throw new DjenericException(x); 81 } 82 } 83 84 public DjUser getUser(String userCode) throws DjenericException 85 { 86 if(userCode == null) userCode = "null"; 87 88 DjUser result = null; 89 try 90 { 91 RdbmsSession session = (RdbmsSession) getPersistenceManager().createSession(); 92 try 93 { 94 SqlStatement stmt = session 95 .getInternalSqlStatement("select id, code, name, password, administrator_yn, modeler_yn " + "from " 96 + RdbmsPersistenceManager.USER_TABLE + " dev " + "where code = :devCode "); 97 stmt.setString("devCode", userCode.toLowerCase()); 98 ResultSet rs = stmt.executeQuery(); 99 if (rs.next()) 100 { 101 result = new RdbmsUser(getPersistenceManager(), rs.getLong("id"), rs.getString("code"), rs.getString("name"), 102 rs.getString("password"), "Y".equalsIgnoreCase(rs.getString("administrator_yn")), "Y".equalsIgnoreCase(rs 103 .getString("modeler_yn"))); 104 } 105 rs.close(); 106 stmt.close(); 107 } 108 finally 109 { 110 session.close(); 111 } 112 113 if (result == null) throw new LogonException(Messages.getString("RdbmsContextManager.noAccess", userCode 114 .toLowerCase(), getPersistenceManager().getCurrentRepository())); 115 116 return result; 117 } 118 catch (SQLException x) 119 { 120 throw new DjenericException(x); 121 } 122 } 123 124 public DjUser[] getUsers() throws DjenericException 125 { 126 try 127 { 128 ArrayList result = new ArrayList (); 129 RdbmsSession session = (RdbmsSession) getPersistenceManager().createSession(); 130 try 131 { 132 SqlStatement stmt = session 133 .getInternalSqlStatement("select id, code, name, password, administrator_yn, modeler_yn from " 134 + RdbmsPersistenceManager.USER_TABLE + " order by code"); 135 ResultSet rs = stmt.executeQuery(); 136 while (rs.next()) 137 { 138 DjUser dev = new RdbmsUser(getPersistenceManager(), rs.getLong("id"), rs.getString("code"), rs 139 .getString("name"), rs.getString("password"), "Y".equalsIgnoreCase(rs.getString("administrator_yn")), "Y" 140 .equalsIgnoreCase(rs.getString("modeler_yn"))); 141 result.add(dev); 142 } 143 rs.close(); 144 stmt.close(); 145 } 146 finally 147 { 148 session.close(); 149 } 150 return (DjUser[]) result.toArray(new DjUser[0]); 151 } 152 catch (SQLException x) 153 { 154 throw new DjenericException(x); 155 } 156 } 157 158 public DjUserContextAssociation createNewUserContextAssociation() 159 { 160 RdbmsUserContextAssociation c = new RdbmsUserContextAssociation(); 161 return c; 162 } 163 164 public DjContext createNewContext() 165 { 166 return new RdbmsContext(getPersistenceManager()); 167 } 168 169 public DjUser createNewUser() 170 { 171 return new RdbmsUser(getPersistenceManager()); 172 } 173 174 }
| Popular Tags
|