View Javadoc
1 /* 2 * $Header: /cvsroot/ebxmlrr/ebxmlrr/src/share/com/sun/ebxml/registry/persistence/rdb/SlotDAO.java,v 1.20 2003/05/30 22:42:38 waikei Exp $ 3 */ 4 5 package com.sun.ebxml.registry.persistence.rdb; 6 7 import java.util.*; 8 import java.sql.*; 9 10 import org.oasis.ebxml.registry.bindings.rim.*; 11 import org.oasis.ebxml.registry.bindings.rim.types.*; 12 13 import com.sun.ebxml.registry.*; 14 import com.sun.ebxml.registry.lcm.*; 15 import com.sun.ebxml.registry.util.*; 16 17 18 public class SlotDAO { 19 20 protected SlotDAO(){} 21 22 public static String getTableNameStatic() { 23 return "Slot"; 24 } 25 26 public String getTableName() { 27 return getTableNameStatic(); 28 } 29 30 ArrayList getSlotsByParent(java.sql.Connection conn, String parentId) throws RegistryException { 31 ArrayList slots = new ArrayList(); 32 Statement stmt = null; 33 34 try { 35 stmt = conn.createStatement(); 36 ResultSet rs = stmt.executeQuery("SELECT * FROM " + getTableName() + " WHERE parent = '" + parentId + "' ORDER BY name_, sequenceId ASC"); 37 38 String lastName = ""; 39 Slot slot = null; 40 ValueList valueList = null; 41 while(rs.next()) { 42 //int sequenceId = rs.getInt("sequenceId"); 43 String name = rs.getString("name_"); 44 String slotType = rs.getString("slotType"); 45 String value = rs.getString("value"); 46 47 if (!name.equals(lastName)) { 48 slot = new Slot(); 49 slot.setName(name); 50 if (slotType != null) { 51 slot.setSlotType(slotType); 52 } 53 valueList = new ValueList(); 54 slot.setValueList(valueList); 55 slots.add(slot); 56 } 57 58 lastName = name; 59 60 if (value != null) { 61 ValueListTypeItem item = new ValueListTypeItem(); 62 item.setValue(value); 63 valueList.addValueListTypeItem(item); 64 } 65 } 66 67 } 68 catch (SQLException e) { 69 e.printStackTrace(); 70 throw new RegistryException(e); 71 } finally { 72 try { 73 if (stmt != null) 74 stmt.close(); 75 } catch (SQLException sqle) { 76 sqle.printStackTrace(); 77 } 78 } 79 80 81 return slots; 82 } 83 84 /*** 85 Get the ArrayList of the names of a Slots that already exist in database 86 @param parentId the id of the parent of those slots 87 */ 88 public ArrayList slotsExist (java.sql.Connection connection, String parentId 89 , ArrayList slots) throws RegistryException { 90 91 Statement stmt = null; 92 ArrayList slotsNames = new ArrayList(); 93 if (slots.size() > 0) { 94 try { 95 Iterator slotsIter = slots.iterator(); 96 String names = ""; 97 while(slotsIter.hasNext()) { 98 names += "'" + Utility.escapeSQLChars( 99 ((Slot)slotsIter.next()).getName()) + "'"; 100 if (slotsIter.hasNext()) { 101 names += ","; 102 } 103 } 104 String sql = "SELECT name_ FROM " + getTableName() + " WHERE" + 105 " parent=" + "'" + parentId+ "'" + " AND " + 106 " name_ IN (" + names +")"; 107 108 //System.err.println("stmt= '" + sql + "'"); 109 stmt = connection.createStatement(); 110 ResultSet rs = stmt.executeQuery(sql); 111 while(rs.next()) { 112 slotsNames.add(rs.getString(1)); 113 } 114 return slotsNames; 115 } 116 catch(SQLException e) { 117 throw new RegistryException(e); 118 } 119 } 120 else { 121 return slotsNames; 122 } 123 124 } 125 126 /*** 127 It checks whether there exists more than a slot having the same name 128 @returns ArrayList of duplidate slots names 129 */ 130 public ArrayList getDuplicateSlots (ArrayList slots) { 131 132 ArrayList duplicateSlotsNames = new ArrayList(); 133 ArrayList slotsNames = new ArrayList(); 134 if (slots.size() > 0) { 135 Iterator iter = slots.iterator(); 136 while(iter.hasNext()) { 137 String slotName = ((Slot)iter.next()).getName(); 138 if (slotsNames.contains(slotName)) { 139 duplicateSlotsNames.add(slotName); 140 } 141 slotsNames.add(slotName); 142 } 143 return duplicateSlotsNames; 144 } 145 else { 146 return duplicateSlotsNames; 147 } 148 149 } 150 151 /*** 152 @param parentInsert It should be set to true if Slot insert is part of new 153 RegistryObject insert (i.e. in the case of SubmitObjectsRequest). It should 154 be set to false in the case of AddSlotsRequest because the parent of the 155 slot is expected to be already submitted by previous SubmitObjectRequest. 156 In the latter case whether the parents of the slots exist will be checked. 157 */ 158 159 public void insert(java.sql.Connection connection, String parentId 160 , ArrayList slots, boolean parentInsert) throws RegistryException { 161 162 PreparedStatement stmt = null; 163 164 if (slots.size()==0) { 165 return; 166 } 167 168 try { 169 String sql = "INSERT INTO " + getTableName() + " (sequenceId, " + 170 "name_, slotType, value, parent)" + 171 " VALUES(?, ?, ?, ?, ?)"; 172 stmt = connection.prepareStatement(sql); 173 174 ArrayList duplicateSlotsNames = getDuplicateSlots(slots); 175 if (duplicateSlotsNames.size() > 0) { 176 // Some slots have duplicate name 177 throw new DuplicateSlotsException(parentId, 178 duplicateSlotsNames); 179 } 180 181 RegistryObjectDAO roDAO = new RegistryObjectDAO(); 182 // Check whether the parent exist in database, in case the parent 183 // has been inserted by the previous SubmitObjectsRequest 184 // (i.e. in the case of AddSlotsRequest) 185 if (!parentInsert && !roDAO.registryObjectExist (connection 186 , parentId)) { 187 // The parent does not exist 188 throw new SlotsParentNotExistException(parentId); 189 } 190 191 ArrayList slotsNamesAlreadyExist = slotsExist(connection 192 , parentId, slots); 193 if (slotsNamesAlreadyExist.size() > 0) { 194 // Some slots for this RegistryObject already exist 195 throw new SlotsExistException(parentId 196 , slotsNamesAlreadyExist); 197 } 198 199 Iterator iter = slots.iterator(); 200 Vector slotNames = new Vector(); 201 while (iter.hasNext()) { 202 Slot slot = (Slot)iter.next(); 203 String slotName = slot.getName(); 204 String slotType = slot.getSlotType(); 205 ValueList values = slot.getValueList(); 206 ValueListTypeItem[] vltis = values.getValueListTypeItem(); 207 208 for (int j=0; j<vltis.length; j++) { 209 String value = vltis[j].getValue(); 210 stmt.setInt(1, j); 211 stmt.setString(2, slotName); 212 stmt.setString(3, slotType); 213 stmt.setString(4, value); 214 stmt.setString(5, parentId); 215 System.err.println("stmt = " + stmt.toString()); 216 stmt.addBatch(); 217 } 218 } 219 if (slots.size() > 0) { 220 int [] updateCounts = stmt.executeBatch(); 221 //stmt.close(); 222 } 223 } 224 catch (java.sql.SQLException e) { 225 e.printStackTrace(); 226 RegistryException exception = new RegistryException(e); 227 throw exception; 228 } finally { 229 try { 230 if (stmt != null) 231 stmt.close(); 232 } catch (SQLException sqle) { 233 sqle.printStackTrace(); 234 } 235 } 236 } 237 238 public void deleteByParentIds(java.sql.Connection connection, ArrayList parentIds) throws RegistryException { 239 240 Statement stmt = null; 241 try { 242 stmt = connection.createStatement(); 243 244 Iterator iter = parentIds.iterator(); 245 while (iter.hasNext()) { 246 String parentId = (String)iter.next(); 247 248 249 stmt.addBatch("DELETE from " + getTableName() + " WHERE parent = '" + 250 parentId + "' "); 251 } 252 253 int [] updateCounts = stmt.executeBatch(); 254 //stmt.close(); 255 } 256 catch (java.sql.SQLException e) { 257 RegistryException exception = new RegistryException(e); 258 throw exception; 259 } finally { 260 try { 261 if (stmt != null) 262 stmt.close(); 263 } catch (SQLException sqle) { 264 sqle.printStackTrace(); 265 } 266 } 267 } 268 269 270 public void deleteByParentIdAndSlots(java.sql.Connection connection, String parentId, ArrayList slots) throws RegistryException { 271 272 Statement stmt = null; 273 try { 274 stmt = connection.createStatement(); 275 276 String str = "DELETE from " + getTableName() + " WHERE parent = '" + parentId + "' AND ("; 277 Iterator iter = slots.iterator(); 278 279 while (iter.hasNext()) { 280 Slot slot = (Slot)iter.next(); 281 String slotName = slot.getName(); 282 283 if (iter.hasNext()) { 284 str = str + "name_ = '" + Utility.escapeSQLChars(slotName) + "' OR "; 285 } 286 else { 287 str = str + "name_ = '" + Utility.escapeSQLChars(slotName) + "' )"; 288 } 289 } 290 291 System.err.println("stmt = " + str); 292 stmt.execute(str); 293 //stmt.close(); 294 int updateCount = stmt.getUpdateCount(); 295 if (updateCount < slots.size()) { 296 throw new SlotNotExistException(parentId); 297 } 298 } 299 catch (java.sql.SQLException e) { 300 RegistryException exception = new RegistryException(e); 301 throw exception; 302 } finally { 303 try { 304 if (stmt != null) 305 stmt.close(); 306 } catch (SQLException sqle) { 307 sqle.printStackTrace(); 308 } 309 } 310 } 311 }

This page was automatically generated by Maven