1 package org.apache.turbine.om.security.peer; 2 3 18 19 import com.workingdogs.village.Record; 20 import java.util.ArrayList ; 21 import java.util.List ; 22 import java.util.Map ; 23 import org.apache.torque.TorqueException; 24 import org.apache.torque.om.BaseObject; 25 import org.apache.torque.om.NumberKey; 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.SecurityObject; 30 import org.apache.turbine.om.security.TurbineGroup; 31 import org.apache.turbine.services.security.TurbineSecurity; 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.GroupSet; 36 37 47 public class GroupPeer extends BasePeer 48 { 49 50 private static final TurbineMapBuilder MAP_BUILDER = (TurbineMapBuilder) 51 getMapBuilder(TurbineMapBuilder.class.getName()); 52 53 54 private static final String TABLE_NAME = MAP_BUILDER.getTableGroup(); 55 56 57 public static final String GROUP_ID = MAP_BUILDER.getGroup_GroupId(); 58 59 60 public static final String NAME = MAP_BUILDER.getGroup_Name(); 61 62 63 public static final String OBJECTDATA = MAP_BUILDER.getGroup_ObjectData(); 64 65 71 public static GroupSet retrieveSet() throws Exception 72 { 73 return retrieveSet(new Criteria()); 74 } 75 76 83 public static GroupSet retrieveSet(Criteria criteria) throws Exception 84 { 85 List results = GroupPeer.doSelect(criteria); 86 GroupSet rs = new GroupSet(); 87 for (int i = 0; i < results.size(); i++) 88 { 89 rs.add((Group) results.get(i)); 90 } 91 return rs; 92 } 93 94 102 public static List doSelect(Criteria criteria) throws TorqueException 103 { 104 try 105 { 106 criteria.addSelectColumn(GROUP_ID) 107 .addSelectColumn(NAME) 108 .addSelectColumn(OBJECTDATA); 109 110 if (criteria.getOrderByColumns() == null 111 || criteria.getOrderByColumns().size() == 0) 112 { 113 criteria.addAscendingOrderByColumn(NAME); 114 } 115 116 125 List rows = BasePeer.doSelect(criteria); 129 List results = new ArrayList (); 130 131 for (int i = 0; i < rows.size(); i++) 133 { 134 Group obj = TurbineSecurity.getGroupInstance(null); 135 Record row = (Record) rows.get(i); 136 ((SecurityObject) obj).setPrimaryKey( 137 new NumberKey(row.getValue(1).asInt())); 138 ((SecurityObject) obj).setName(row.getValue(2).asString()); 139 byte[] objectData = row.getValue(3).asBytes(); 140 Map temp = (Map ) ObjectUtils.deserialize(objectData); 141 if (temp != null) 142 { 143 ((SecurityObject) obj).setAttributes(temp); 144 } 145 results.add(obj); 146 } 147 148 return results; 149 } 150 catch (Exception ex) 151 { 152 throw new TorqueException(ex); 153 } 154 } 155 156 163 public static void doUpdate(Criteria criteria) 164 throws TorqueException 165 { 166 Criteria selectCriteria = new Criteria(2); 167 selectCriteria.put(GROUP_ID, criteria.remove(GROUP_ID)); 168 BasePeer.doUpdate(selectCriteria, criteria); 169 } 170 171 181 public static boolean checkExists(Group group) 182 throws DataBackendException, Exception 183 { 184 Criteria criteria = new Criteria(); 185 criteria.addSelectColumn(GROUP_ID); 186 criteria.add(NAME, ((SecurityObject) group).getName()); 187 List results = BasePeer.doSelect(criteria); 188 if (results.size() > 1) 189 { 190 throw new DataBackendException("Multiple groups named '" 191 + ((TurbineGroup) group).getName() + "' exist!"); 192 } 193 return (results.size() == 1); 194 } 195 196 201 public static String getTableName() 202 { 203 return TABLE_NAME; 204 } 205 206 212 public static String getColumnName(String name) 213 { 214 StringBuffer sb = new StringBuffer (); 215 sb.append(TABLE_NAME); 216 sb.append("."); 217 sb.append(name); 218 return sb.toString(); 219 } 220 221 227 public static Criteria buildCriteria(Group group) 228 { 229 Criteria criteria = new Criteria(); 230 criteria.add(NAME, ((SecurityObject) group).getName()); 231 if (!((BaseObject) group).isNew()) 232 { 233 criteria.add(GROUP_ID, ((BaseObject) group).getPrimaryKey()); 234 } 235 return criteria; 239 } 240 } 241 | Popular Tags |