1 16 package org.outerj.daisy.repository.serverimpl.variant; 17 18 import org.outerj.daisy.repository.commonimpl.variant.VariantStrategy; 19 import org.outerj.daisy.repository.commonimpl.variant.BranchImpl; 20 import org.outerj.daisy.repository.commonimpl.variant.LanguageImpl; 21 import org.outerj.daisy.repository.commonimpl.AuthenticatedUser; 22 import org.outerj.daisy.repository.serverimpl.LocalRepositoryManager; 23 import org.outerj.daisy.jdbcutil.SqlCounter; 24 import org.outerj.daisy.repository.serverimpl.EventHelper; 25 import org.outerj.daisy.repository.variant.BranchNotFoundException; 26 import org.outerj.daisy.repository.variant.LanguageNotFoundException; 27 import org.outerj.daisy.repository.RepositoryException; 28 import org.outerj.daisy.repository.RepositoryEventType; 29 import org.outerj.daisy.jdbcutil.JdbcHelper; 30 import org.apache.xmlbeans.XmlObject; 31 import org.outerx.daisy.x10.*; 32 33 import java.sql.*; 34 import java.util.*; 35 import java.util.Date ; 36 37 public class LocalVariantStrategy implements VariantStrategy { 38 private LocalRepositoryManager.Context context; 39 private JdbcHelper jdbcHelper; 40 private static final String SELECT_BRANCH = "select id, \"name\", description, last_modified, last_modifier, updatecount from branches"; 41 private static final String SELECT_LANGUAGE = "select id, \"name\", description, last_modified, last_modifier, updatecount from languages"; 42 private SqlCounter branchCounter; 43 private SqlCounter languageCounter; 44 private EventHelper eventHelper; 45 46 public LocalVariantStrategy(LocalRepositoryManager.Context context, JdbcHelper jdbcHelper) { 47 this.context = context; 48 this.jdbcHelper = jdbcHelper; 49 this.eventHelper = new EventHelper(context, jdbcHelper); 50 branchCounter = new SqlCounter("branch_sequence", context.getDataSource(), context.getLogger()); 51 languageCounter = new SqlCounter("language_sequence", context.getDataSource(), context.getLogger()); 52 } 53 54 public BranchImpl getBranch(long id, AuthenticatedUser user) throws RepositoryException { 55 Connection conn = null; 56 PreparedStatement stmt = null; 57 try { 58 conn = context.getDataSource().getConnection(); 59 return loadBranchUsingConnection(id, user, conn); 60 } catch (Throwable e) { 61 if (e instanceof RepositoryException) 62 throw (RepositoryException)e; 63 throw new RepositoryException("Error loading branch.", e); 64 } finally { 65 jdbcHelper.closeStatement(stmt); 66 jdbcHelper.closeConnection(conn); 67 } 68 } 69 70 public BranchImpl loadBranchUsingConnection(long id, AuthenticatedUser user, Connection conn) throws SQLException, BranchNotFoundException { 71 PreparedStatement stmt = null; 72 try { 73 stmt = conn.prepareStatement(SELECT_BRANCH + " where id = ?"); 74 stmt.setLong(1, id); 75 ResultSet rs = stmt.executeQuery(); 76 77 if (!rs.next()) 78 throw new BranchNotFoundException(id); 79 80 return getBranchFromResultSet(rs, user); 81 } finally { 82 jdbcHelper.closeStatement(stmt); 83 } 84 85 } 86 87 public BranchImpl getBranchByName(String name, AuthenticatedUser user) throws RepositoryException { 88 Connection conn = null; 89 PreparedStatement stmt = null; 90 try { 91 conn = context.getDataSource().getConnection(); 92 jdbcHelper.startTransaction(conn); 93 94 stmt = conn.prepareStatement(SELECT_BRANCH + " where \"name\" = ?"); 95 stmt.setString(1, name); 96 ResultSet rs = stmt.executeQuery(); 97 98 if (!rs.next()) 99 throw new BranchNotFoundException(name); 100 101 return getBranchFromResultSet(rs, user); 102 } catch (Throwable e) { 103 if (e instanceof BranchNotFoundException) 104 throw (BranchNotFoundException)e; 105 throw new RepositoryException("Error loading branch.", e); 106 } finally { 107 jdbcHelper.closeStatement(stmt); 108 jdbcHelper.closeConnection(conn); 109 } 110 } 111 112 public BranchImpl[] getAllBranches(AuthenticatedUser user) throws RepositoryException { 113 Connection conn = null; 114 PreparedStatement stmt = null; 115 try { 116 conn = context.getDataSource().getConnection(); 117 jdbcHelper.startTransaction(conn); 118 119 stmt = conn.prepareStatement(SELECT_BRANCH + " order by \"name\""); 120 ResultSet rs = stmt.executeQuery(); 121 122 List branches = new ArrayList(); 123 124 while (rs.next()) { 125 branches.add(getBranchFromResultSet(rs, user)); 126 } 127 128 return (BranchImpl[])branches.toArray(new BranchImpl[branches.size()]); 129 } catch (Throwable e) { 130 throw new RepositoryException("Error loading branches.", e); 131 } finally { 132 jdbcHelper.closeStatement(stmt); 133 jdbcHelper.closeConnection(conn); 134 } 135 } 136 137 private BranchImpl getBranchFromResultSet(ResultSet rs, AuthenticatedUser user) throws SQLException { 138 BranchImpl branch = new BranchImpl(this, rs.getString("name"), user); 139 BranchImpl.IntimateAccess branchInt = branch.getIntimateAccess(this); 140 branchInt.setId(rs.getLong("id")); 141 branch.setDescription(rs.getString("description")); 142 branchInt.setLastModified(rs.getTimestamp("last_modified")); 143 branchInt.setLastModifier(rs.getLong("last_modifier")); 144 branchInt.setUpdateCount(rs.getLong("updatecount")); 145 return branch; 146 } 147 148 public void storeBranch(BranchImpl branch) throws RepositoryException { 149 BranchImpl.IntimateAccess branchInt = branch.getIntimateAccess(this); 150 151 if (!branchInt.getCurrentUser().isInAdministratorRole()) 152 throw new RepositoryException("Only Administrators can create or update branches."); 153 154 if (branch.getId() == 1) 155 throw new RepositoryException("The system branch with ID 1 cannot be modified."); 156 157 Connection conn = null; 158 PreparedStatement stmt = null; 159 ResultSet rs = null; 160 try { 161 conn = context.getDataSource().getConnection(); 162 jdbcHelper.startTransaction(conn); 163 164 stmt = conn.prepareStatement("select id from branches where \"name\" = ?"); 166 stmt.setString(1, branch.getName()); 167 rs = stmt.executeQuery(); 168 if (rs.next()) { 169 long id = rs.getLong(1); 170 if (id != branch.getId()) 171 throw new RepositoryException("There is already a branch with the following name: \"" + branch.getName() + "\", the branch with id " + id); 172 } 173 rs.close(); 174 stmt.close(); 175 176 boolean isNew = false; 178 long id = branch.getId(); 179 java.util.Date lastModified = new java.util.Date (); 180 long lastModifier = branchInt.getCurrentUser().getId(); 181 XmlObject eventDescription; 182 183 if (id == -1) { 184 isNew = true; 185 id = getNextBranchId(); 187 188 stmt = conn.prepareStatement("insert into branches(id, \"name\", description, last_modified, last_modifier, updatecount) values(?,?,?,?,?,?)"); 189 stmt.setLong(1, id); 190 stmt.setString(2, branch.getName()); 191 stmt.setString(3, branch.getDescription()); 192 stmt.setTimestamp(4, new Timestamp(lastModified.getTime())); 193 stmt.setLong(5, lastModifier); 194 stmt.setLong(6, 1L); 195 stmt.execute(); 196 stmt.close(); 197 198 eventDescription = createBranchCreatedEvent(branch, id, lastModified); 199 } else { 200 202 stmt = conn.prepareStatement("select updatecount from branches where id = ? " + jdbcHelper.getSharedLockClause()); 204 stmt.setLong(1, id); 205 rs = stmt.executeQuery(); 206 if (!rs.next()) { 207 throw new BranchNotFoundException(id); 208 } else { 209 long dbUpdateCount = rs.getLong(1); 210 if (dbUpdateCount != branch.getUpdateCount()) 211 throw new RepositoryException("The branch has been modified concurrently."); 212 } 213 stmt.close(); 215 BranchImpl oldBranch = getBranch(branch.getId(), branchInt.getCurrentUser()); 216 long newUpdateCount = branch.getUpdateCount() + 1; 217 218 stmt = conn.prepareStatement("update branches set \"name\"=?, description=?, last_modified=?, last_modifier=?, updatecount=? where id = ?"); 220 stmt.setString(1, branch.getName()); 221 stmt.setString(2, branch.getDescription()); 222 stmt.setTimestamp(3, new Timestamp(lastModified.getTime())); 223 stmt.setLong(4, lastModifier); 224 stmt.setLong(5, branch.getUpdateCount() + 1); 225 stmt.setLong(6, id); 226 stmt.execute(); 227 stmt.close(); 228 229 eventDescription = createBranchUpdatedEvent(oldBranch, branch, lastModified, newUpdateCount); 230 } 231 232 eventHelper.createEvent(eventDescription, isNew ? "BranchCreated" : "BranchUpdated", conn); 233 234 conn.commit(); 235 236 branchInt.setId(id); 237 branchInt.setLastModified(lastModified); 238 branchInt.setLastModifier(lastModifier); 239 branchInt.setUpdateCount(branch.getUpdateCount() + 1); 240 241 if (isNew) 242 context.getCommonRepository().fireRepositoryEvent(RepositoryEventType.BRANCH_CREATED, id, branch.getUpdateCount()); 243 else 244 context.getCommonRepository().fireRepositoryEvent(RepositoryEventType.BRANCH_UPDATED, id, branch.getUpdateCount()); 245 } catch (Throwable e) { 246 jdbcHelper.rollback(conn); 247 throw new RepositoryException("Problem storing branch", e); 248 } finally { 249 jdbcHelper.closeStatement(stmt); 250 jdbcHelper.closeConnection(conn); 251 } 252 } 253 254 private BranchCreatedDocument createBranchCreatedEvent(BranchImpl branch, long id, java.util.Date lastModified) { 255 BranchCreatedDocument branchCreatedDocument = BranchCreatedDocument.Factory.newInstance(); 256 BranchCreatedDocument.BranchCreated branchCreated = branchCreatedDocument.addNewBranchCreated(); 257 258 BranchDocument.Branch branchXml = branch.getXml().getBranch(); 259 branchXml.setLastModified(getCalendar(lastModified)); 260 branchXml.setLastModifier(branch.getIntimateAccess(this).getCurrentUser().getId()); 261 branchXml.setUpdateCount(1); 262 branchXml.setId(id); 263 branchCreated.addNewNewBranch().setBranch(branchXml); 264 265 return branchCreatedDocument; 266 } 267 268 private BranchUpdatedDocument createBranchUpdatedEvent(BranchImpl oldBranch, BranchImpl branch, java.util.Date lastModified, long newUpdateCount) { 269 BranchUpdatedDocument branchUpdatedDocument = BranchUpdatedDocument.Factory.newInstance(); 270 BranchUpdatedDocument.BranchUpdated branchUpdated = branchUpdatedDocument.addNewBranchUpdated(); 271 272 branchUpdated.addNewOldBranch().setBranch(oldBranch.getXml().getBranch()); 273 274 BranchDocument.Branch branchXml = branch.getXml().getBranch(); 275 branchXml.setLastModified(getCalendar(lastModified)); 276 branchXml.setLastModifier(branch.getIntimateAccess(this).getCurrentUser().getId()); 277 branchXml.setUpdateCount(newUpdateCount); 278 branchUpdated.addNewNewBranch().setBranch(branchXml); 279 280 return branchUpdatedDocument; 281 } 282 283 public void deleteBranch(long id, AuthenticatedUser user) throws RepositoryException { 284 if (!user.isInAdministratorRole()) 285 throw new RepositoryException("Only Administrators can delete branches."); 286 287 if (id == 1) 288 throw new RepositoryException("The system branch with ID 1 cannot be deleted."); 289 290 Connection conn = null; 293 PreparedStatement stmt = null; 294 try { 295 conn = context.getDataSource().getConnection(); 296 jdbcHelper.startTransaction(conn); 297 298 BranchImpl deletedBranch = loadBranchUsingConnection(id, user, conn); 299 300 stmt = conn.prepareStatement("select count(*) from document_variants where branch_id = ?"); 301 stmt.setLong(1, id); 302 ResultSet rs = stmt.executeQuery(); 303 rs.next(); 304 if (rs.getLong(1) > 0) 305 throw new RepositoryException("Branch " + id + " is still in use by " + rs.getLong(1) + " document variants."); 306 stmt.close(); 307 308 stmt = conn.prepareStatement("delete from branches where id = ?"); 309 stmt.setLong(1, id); 310 stmt.execute(); 311 312 XmlObject eventDescription = createBranchDeletedEvent(deletedBranch, user); 313 eventHelper.createEvent(eventDescription, "BranchDeleted", conn); 314 315 conn.commit(); 316 } catch (Throwable e) { 317 jdbcHelper.rollback(conn); 318 if (e instanceof BranchNotFoundException) 319 throw (BranchNotFoundException)e; 320 throw new RepositoryException("Error deleting branch " + id, e); 321 } finally { 322 jdbcHelper.closeStatement(stmt); 323 jdbcHelper.closeConnection(conn); 324 } 325 context.getCommonRepository().fireRepositoryEvent(RepositoryEventType.BRANCH_DELETED, id, -1); 326 } 327 328 private BranchDeletedDocument createBranchDeletedEvent(BranchImpl branch, AuthenticatedUser user) { 329 BranchDeletedDocument branchDeletedDocument = BranchDeletedDocument.Factory.newInstance(); 330 BranchDeletedDocument.BranchDeleted branchDeletedXml = branchDeletedDocument.addNewBranchDeleted(); 331 332 branchDeletedXml.setDeleterId(user.getId()); 333 branchDeletedXml.setDeletedTime(getCalendar(new Date ())); 334 branchDeletedXml.addNewDeletedBranch().setBranch(branch.getXml().getBranch()); 335 336 return branchDeletedDocument; 337 } 338 339 private Calendar getCalendar(java.util.Date date) { 340 GregorianCalendar calendar = new GregorianCalendar(); 341 calendar.setTime(date); 342 return calendar; 343 } 344 345 private long getNextBranchId() throws SQLException { 346 return branchCounter.getNextId(); 347 } 348 349 private long getNextLanguageId() throws SQLException { 350 return languageCounter.getNextId(); 351 } 352 353 public LanguageImpl getLanguage(long id, AuthenticatedUser user) throws RepositoryException { 354 Connection conn = null; 355 PreparedStatement stmt = null; 356 try { 357 conn = context.getDataSource().getConnection(); 358 return loadLanguageUsingConnection(id, user, conn); 359 } catch (Throwable e) { 360 if (e instanceof RepositoryException) 361 throw (RepositoryException)e; 362 throw new RepositoryException("Error loading language.", e); 363 } finally { 364 jdbcHelper.closeStatement(stmt); 365 jdbcHelper.closeConnection(conn); 366 } 367 } 368 369 370 private LanguageImpl loadLanguageUsingConnection(long id, AuthenticatedUser user, Connection conn) throws RepositoryException { 371 PreparedStatement stmt = null; 372 try { 373 stmt = conn.prepareStatement(SELECT_LANGUAGE + " where id = ?"); 374 stmt.setLong(1, id); 375 ResultSet rs = stmt.executeQuery(); 376 377 if (!rs.next()) 378 throw new LanguageNotFoundException(id); 379 380 return getLanguageFromResultSet(rs, user); 381 } catch (Throwable e) { 382 if (e instanceof LanguageNotFoundException) 383 throw (LanguageNotFoundException)e; 384 throw new RepositoryException("Error loading language.", e); 385 } finally { 386 jdbcHelper.closeStatement(stmt); 387 } 388 } 389 390 public LanguageImpl getLanguageByName(String name, AuthenticatedUser user) throws RepositoryException { 391 Connection conn = null; 392 PreparedStatement stmt = null; 393 try { 394 conn = context.getDataSource().getConnection(); 395 jdbcHelper.startTransaction(conn); 396 397 stmt = conn.prepareStatement(SELECT_LANGUAGE + " where \"name\" = ?"); 398 stmt.setString(1, name); 399 ResultSet rs = stmt.executeQuery(); 400 401 if (!rs.next()) 402 throw new LanguageNotFoundException(name); 403 404 return getLanguageFromResultSet(rs, user); 405 } catch (Throwable e) { 406 if (e instanceof LanguageNotFoundException) 407 throw (LanguageNotFoundException)e; 408 throw new RepositoryException("Error loading language.", e); 409 } finally { 410 jdbcHelper.closeStatement(stmt); 411 jdbcHelper.closeConnection(conn); 412 } 413 } 414 415 public LanguageImpl[] getAllLanguages(AuthenticatedUser user) throws RepositoryException { 416 Connection conn = null; 417 PreparedStatement stmt = null; 418 try { 419 conn = context.getDataSource().getConnection(); 420 jdbcHelper.startTransaction(conn); 421 422 stmt = conn.prepareStatement(SELECT_LANGUAGE + " order by \"name\""); 423 ResultSet rs = stmt.executeQuery(); 424 425 List languages = new ArrayList(); 426 427 while (rs.next()) { 428 languages.add(getLanguageFromResultSet(rs, user)); 429 } 430 431 return (LanguageImpl[])languages.toArray(new LanguageImpl[languages.size()]); 432 } catch (Throwable e) { 433 throw new RepositoryException("Error loading languages.", e); 434 } finally { 435 jdbcHelper.closeStatement(stmt); 436 jdbcHelper.closeConnection(conn); 437 } 438 } 439 440 private LanguageImpl getLanguageFromResultSet(ResultSet rs, AuthenticatedUser user) throws SQLException { 441 LanguageImpl language = new LanguageImpl(this, rs.getString("name"), user); 442 LanguageImpl.IntimateAccess languageInt = language.getIntimateAccess(this); 443 languageInt.setId(rs.getLong("id")); 444 language.setDescription(rs.getString("description")); 445 languageInt.setLastModified(rs.getTimestamp("last_modified")); 446 languageInt.setLastModifier(rs.getLong("last_modifier")); 447 languageInt.setUpdateCount(rs.getLong("updatecount")); 448 return language; 449 } 450 451 public void storeLanguage(LanguageImpl language) throws RepositoryException { 452 LanguageImpl.IntimateAccess languageInt = language.getIntimateAccess(this); 453 454 if (!languageInt.getCurrentUser().isInAdministratorRole()) 455 throw new RepositoryException("Only Administrators can create or update languages."); 456 457 if (language.getId() == 1) 458 throw new RepositoryException("The system language with ID 1 cannot be modified."); 459 460 Connection conn = null; 461 PreparedStatement stmt = null; 462 ResultSet rs = null; 463 try { 464 conn = context.getDataSource().getConnection(); 465 jdbcHelper.startTransaction(conn); 466 467 stmt = conn.prepareStatement("select id from languages where \"name\" = ?"); 469 stmt.setString(1, language.getName()); 470 rs = stmt.executeQuery(); 471 if (rs.next()) { 472 long id = rs.getLong(1); 473 if (id != language.getId()) 474 throw new RepositoryException("There is already a language with the following name: \"" + language.getName() + "\", the language with id " + id); 475 } 476 rs.close(); 477 stmt.close(); 478 479 boolean isNew = false; 481 long id = language.getId(); 482 java.util.Date lastModified = new java.util.Date (); 483 long lastModifier = languageInt.getCurrentUser().getId(); 484 XmlObject eventDescription; 485 486 if (id == -1) { 487 isNew = true; 488 id = getNextLanguageId(); 490 491 stmt = conn.prepareStatement("insert into languages(id, \"name\", description, last_modified, last_modifier, updatecount) values(?,?,?,?,?,?)"); 492 stmt.setLong(1, id); 493 stmt.setString(2, language.getName()); 494 stmt.setString(3, language.getDescription()); 495 stmt.setTimestamp(4, new Timestamp(lastModified.getTime())); 496 stmt.setLong(5, lastModifier); 497 stmt.setLong(6, 1L); 498 stmt.execute(); 499 stmt.close(); 500 501 eventDescription = createLanguageCreatedEvent(language, id, lastModified); 502 } else { 503 505 stmt = conn.prepareStatement("select updatecount from languages where id = ? " + jdbcHelper.getSharedLockClause()); 507 stmt.setLong(1, id); 508 rs = stmt.executeQuery(); 509 if (!rs.next()) { 510 throw new LanguageNotFoundException(id); 511 } else { 512 long dbUpdateCount = rs.getLong(1); 513 if (dbUpdateCount != language.getUpdateCount()) 514 throw new RepositoryException("The language has been modified concurrently."); 515 } 516 stmt.close(); 518 LanguageImpl oldLanguage = getLanguage(language.getId(), languageInt.getCurrentUser()); 519 long newUpdateCount = language.getUpdateCount() + 1; 520 521 stmt = conn.prepareStatement("update languages set \"name\"=?, description=?, last_modified=?, last_modifier=?, updatecount=? where id = ?"); 523 stmt.setString(1, language.getName()); 524 stmt.setString(2, language.getDescription()); 525 stmt.setTimestamp(3, new Timestamp(lastModified.getTime())); 526 stmt.setLong(4, lastModifier); 527 stmt.setLong(5, language.getUpdateCount() + 1); 528 stmt.setLong(6, id); 529 stmt.execute(); 530 stmt.close(); 531 532 eventDescription = createLanguageUpdatedEvent(oldLanguage, language, lastModified, newUpdateCount); 533 } 534 535 eventHelper.createEvent(eventDescription, isNew ? "LanguageCreated" : "LanguageUpdated", conn); 536 537 conn.commit(); 538 539 languageInt.setId(id); 540 languageInt.setLastModified(lastModified); 541 languageInt.setLastModifier(lastModifier); 542 languageInt.setUpdateCount(language.getUpdateCount() + 1); 543 544 if (isNew) 545 context.getCommonRepository().fireRepositoryEvent(RepositoryEventType.LANGUAGE_CREATED, id, language.getUpdateCount()); 546 else 547 context.getCommonRepository().fireRepositoryEvent(RepositoryEventType.LANGUAGE_UPDATED, id, language.getUpdateCount()); 548 } catch (Throwable e) { 549 jdbcHelper.rollback(conn); 550 throw new RepositoryException("Problem storing language", e); 551 } finally { 552 jdbcHelper.closeStatement(stmt); 553 jdbcHelper.closeConnection(conn); 554 } 555 } 556 557 private LanguageCreatedDocument createLanguageCreatedEvent(LanguageImpl language, long id, java.util.Date lastModified) { 558 LanguageCreatedDocument languageCreatedDocument = LanguageCreatedDocument.Factory.newInstance(); 559 LanguageCreatedDocument.LanguageCreated languageCreated = languageCreatedDocument.addNewLanguageCreated(); 560 561 LanguageDocument.Language languageXml = language.getXml().getLanguage(); 562 languageXml.setLastModified(getCalendar(lastModified)); 563 languageXml.setLastModifier(language.getIntimateAccess(this).getCurrentUser().getId()); 564 languageXml.setUpdateCount(1); 565 languageXml.setId(id); 566 languageCreated.addNewNewLanguage().setLanguage(languageXml); 567 568 return languageCreatedDocument; 569 } 570 571 private LanguageUpdatedDocument createLanguageUpdatedEvent(LanguageImpl oldLanguage, LanguageImpl language, java.util.Date lastModified, long newUpdateCount) { 572 LanguageUpdatedDocument languageUpdatedDocument = LanguageUpdatedDocument.Factory.newInstance(); 573 LanguageUpdatedDocument.LanguageUpdated languageUpdated = languageUpdatedDocument.addNewLanguageUpdated(); 574 575 languageUpdated.addNewOldLanguage().setLanguage(oldLanguage.getXml().getLanguage()); 576 577 LanguageDocument.Language languageXml = language.getXml().getLanguage(); 578 languageXml.setLastModified(getCalendar(lastModified)); 579 languageXml.setLastModifier(language.getIntimateAccess(this).getCurrentUser().getId()); 580 languageXml.setUpdateCount(newUpdateCount); 581 languageUpdated.addNewNewLanguage().setLanguage(languageXml); 582 583 return languageUpdatedDocument; 584 } 585 586 public void deleteLanguage(long id, AuthenticatedUser user) throws RepositoryException { 587 if (!user.isInAdministratorRole()) 588 throw new RepositoryException("Only Administrators can delete languages."); 589 590 if (id == 1) 591 throw new RepositoryException("The system language with ID 1 cannot be deleted."); 592 593 Connection conn = null; 596 PreparedStatement stmt = null; 597 try { 598 conn = context.getDataSource().getConnection(); 599 jdbcHelper.startTransaction(conn); 600 601 LanguageImpl deletedLanguage = loadLanguageUsingConnection(id, user, conn); 602 603 stmt = conn.prepareStatement("select count(*) from document_variants where lang_id = ?"); 604 stmt.setLong(1, id); 605 ResultSet rs = stmt.executeQuery(); 606 rs.next(); 607 if (rs.getLong(1) > 0) 608 throw new RepositoryException("Language " + id + " is still in use by " + rs.getLong(1) + " document variants."); 609 stmt.close(); 610 611 stmt = conn.prepareStatement("delete from languages where id = ?"); 612 stmt.setLong(1, id); 613 stmt.execute(); 614 615 XmlObject eventDescription = createLanguageDeletedEvent(deletedLanguage, user); 616 eventHelper.createEvent(eventDescription, "LanguageDeleted", conn); 617 618 conn.commit(); 619 } catch (Throwable e) { 620 jdbcHelper.rollback(conn); 621 if (e instanceof BranchNotFoundException) 622 throw (BranchNotFoundException)e; 623 throw new RepositoryException("Error deleting language " + id, e); 624 } finally { 625 jdbcHelper.closeStatement(stmt); 626 jdbcHelper.closeConnection(conn); 627 } 628 context.getCommonRepository().fireRepositoryEvent(RepositoryEventType.LANGUAGE_DELETED, id, -1); 629 } 630 631 private LanguageDeletedDocument createLanguageDeletedEvent(LanguageImpl language, AuthenticatedUser user) { 632 LanguageDeletedDocument languageDeletedDocument = LanguageDeletedDocument.Factory.newInstance(); 633 LanguageDeletedDocument.LanguageDeleted languageDeletedXml = languageDeletedDocument.addNewLanguageDeleted(); 634 635 languageDeletedXml.setDeleterId(user.getId()); 636 languageDeletedXml.setDeletedTime(getCalendar(new Date ())); 637 languageDeletedXml.addNewDeletedLanguage().setLanguage(language.getXml().getLanguage()); 638 639 return languageDeletedDocument; 640 } 641 } 642 | Popular Tags |