1 package org.apache.turbine.om.security.peer; 2 3 18 19 import java.util.ArrayList ; 20 import java.util.List ; 21 import java.util.Map ; 22 import org.apache.torque.TorqueException; 23 import org.apache.torque.om.BaseObject; 24 import org.apache.torque.om.NumberKey; 25 import org.apache.torque.om.Persistent; 26 import org.apache.torque.util.BasePeer; 27 import org.apache.torque.util.Criteria; 28 import org.apache.turbine.om.security.Group; 29 import org.apache.turbine.om.security.Role; 30 import org.apache.turbine.om.security.TurbineRole; 31 import org.apache.turbine.om.security.User; 32 import org.apache.turbine.util.ObjectUtils; 33 import org.apache.turbine.util.db.map.TurbineMapBuilder; 34 import org.apache.turbine.util.security.DataBackendException; 35 import org.apache.turbine.util.security.RoleSet; 36 import com.workingdogs.village.Record; 37 38 39 48 public class RolePeer extends BasePeer 49 { 50 51 private static final TurbineMapBuilder MAP_BUILDER = (TurbineMapBuilder) 52 getMapBuilder(TurbineMapBuilder.class.getName()); 53 54 55 private static final String TABLE_NAME = MAP_BUILDER.getTableRole(); 56 57 58 public static final String ROLE_ID = MAP_BUILDER.getRole_RoleId(); 59 60 61 public static final String NAME = MAP_BUILDER.getRole_Name(); 62 63 64 public static final String OBJECTDATA = MAP_BUILDER.getRole_ObjectData(); 65 66 73 public static RoleSet retrieveSet(Criteria criteria) throws Exception 74 { 75 List results = RolePeer.doSelect(criteria); 76 RoleSet rs = new RoleSet(); 77 for (int i = 0; i < results.size(); i++) 78 { 79 rs.add((Role) results.get(i)); 80 } 81 return rs; 82 } 83 84 92 public static RoleSet retrieveSet(User user, Group group) throws Exception 93 { 94 Criteria criteria = new Criteria(); 95 criteria.add(UserGroupRolePeer.USER_ID, 96 ((Persistent) user).getPrimaryKey()); 97 criteria.add(UserGroupRolePeer.GROUP_ID, 98 ((Persistent) group).getPrimaryKey()); 99 criteria.addJoin(UserGroupRolePeer.ROLE_ID, RolePeer.ROLE_ID); 100 return retrieveSet(criteria); 101 } 102 103 111 public static List doSelect(Criteria criteria) throws TorqueException 112 { 113 try 114 { 115 criteria.addSelectColumn(ROLE_ID) 116 .addSelectColumn(NAME) 117 .addSelectColumn(OBJECTDATA); 118 119 if (criteria.getOrderByColumns() == null 120 || criteria.getOrderByColumns().size() == 0) 121 { 122 criteria.addAscendingOrderByColumn(NAME); 123 } 124 125 134 List rows = BasePeer.doSelect(criteria); 138 List results = new ArrayList (); 139 140 for (int i = 0; i < rows.size(); i++) 142 { 143 Role obj = new TurbineRole(); 145 Record row = (Record) rows.get(i); 146 ((TurbineRole) obj).setPrimaryKey( 147 new NumberKey(row.getValue(1).asInt())); 148 ((TurbineRole) obj).setName(row.getValue(2).asString()); 149 byte[] objectData = row.getValue(3).asBytes(); 150 Map temp = (Map ) ObjectUtils.deserialize(objectData); 151 if (temp != null) 152 { 153 ((TurbineRole) obj).setAttributes(temp); 154 } 155 results.add(obj); 156 } 157 158 return results; 159 } 160 catch (Exception ex) 161 { 162 throw new TorqueException (ex); 163 } 164 } 165 166 172 public static Criteria buildCriteria(Role role) 173 { 174 Criteria criteria = new Criteria(); 175 if (!((BaseObject) role).isNew()) 176 { 177 criteria.add(ROLE_ID, ((BaseObject) role).getPrimaryKey()); 178 } 179 criteria.add(NAME, role.getName()); 180 return criteria; 184 } 185 186 193 public static void doUpdate(Criteria criteria) 194 throws TorqueException 195 { 196 Criteria selectCriteria = new Criteria(2); 197 selectCriteria.put(ROLE_ID, criteria.remove(ROLE_ID)); 198 BasePeer.doUpdate(selectCriteria, criteria); 199 } 200 201 211 public static boolean checkExists(Role role) 212 throws DataBackendException, Exception 213 { 214 Criteria criteria = new Criteria(); 215 criteria.addSelectColumn(ROLE_ID); 216 criteria.add(NAME, role.getName()); 217 List results = BasePeer.doSelect(criteria); 218 if (results.size() > 1) 219 { 220 throw new DataBackendException("Multiple roles named '" 221 + role.getName() + "' exist!"); 222 } 223 return (results.size() == 1); 224 } 225 226 231 public static String getTableName() 232 { 233 return TABLE_NAME; 234 } 235 236 242 public static String getColumnName (String name) 243 { 244 StringBuffer sb = new StringBuffer (); 245 sb.append(TABLE_NAME); 246 sb.append("."); 247 sb.append(name); 248 return sb.toString(); 249 } 250 } 251 | Popular Tags |