1 28 29 package com.opencms.defaults.master.genericsql; 30 31 import org.opencms.db.CmsDbUtil; 32 import org.opencms.db.CmsPublishedResource; 33 import org.opencms.db.CmsUserSettings; 34 import org.opencms.file.CmsGroup; 35 import org.opencms.file.CmsObject; 36 import org.opencms.file.CmsProject; 37 import org.opencms.file.CmsResource; 38 import org.opencms.file.CmsUser; 39 import org.opencms.main.CmsException; 40 import org.opencms.main.CmsLog; 41 import org.opencms.main.OpenCms; 42 import org.opencms.security.CmsSecurityException; 43 import org.opencms.setup.CmsSetupDb; 44 import org.opencms.util.CmsUUID; 45 46 import com.opencms.defaults.master.CmsMasterContent; 47 import com.opencms.defaults.master.CmsMasterDataSet; 48 import com.opencms.defaults.master.CmsMasterMedia; 49 import com.opencms.legacy.CmsLegacyException; 50 import com.opencms.legacy.CmsLegacySecurityException; 51 52 import java.io.ByteArrayInputStream ; 53 import java.lang.reflect.Constructor ; 54 import java.sql.Connection ; 55 import java.sql.PreparedStatement ; 56 import java.sql.ResultSet ; 57 import java.sql.SQLException ; 58 import java.sql.Timestamp ; 59 import java.sql.Types ; 60 import java.util.Iterator ; 61 import java.util.Map ; 62 import java.util.Vector ; 63 64 69 public class CmsDbAccess { 70 71 72 protected String m_rootChannel = "/"; 73 74 75 private String m_poolUrl; 76 77 78 protected com.opencms.defaults.master.genericsql.CmsSqlManager m_sqlManager; 79 80 84 public CmsDbAccess() {} 85 86 90 public CmsDbAccess(String dbPool) { 91 init(dbPool); 92 } 93 94 97 public com.opencms.defaults.master.genericsql.CmsSqlManager init(String dbPool) { 98 m_sqlManager = initQueries(dbPool, getClass()); 99 m_poolUrl = dbPool; 100 101 return m_sqlManager; 102 } 103 104 108 public com.opencms.defaults.master.genericsql.CmsSqlManager initQueries(String dbPoolUrl, Class currentClass) { 109 return new com.opencms.defaults.master.genericsql.CmsSqlManager(dbPoolUrl, currentClass); 110 } 111 112 118 public boolean checkTables() { 119 PreparedStatement stmt = null; 120 Connection conn = null; 121 try { 122 conn = m_sqlManager.getConnection(); 123 stmt = m_sqlManager.getPreparedStatement(conn, "check_module_master"); 124 stmt.executeQuery(); 125 return true; 126 127 } catch (SQLException exc) { 128 return false; 129 130 } finally { 131 m_sqlManager.closeAll(null, conn, stmt, null); 132 } 133 } 134 135 142 public void updateDatabase(String updateScript, Map replacers) throws CmsException { 143 CmsSetupDb setup = new CmsSetupDb(""); 144 Connection conn = null; 145 146 try { 147 conn = m_sqlManager.getConnection(); 148 setup.setConnection(conn); 149 setup.updateDatabase(updateScript, replacers, true); 150 151 Vector errors = setup.getErrors(); 152 if (!errors.isEmpty()) { 153 StringBuffer errorMessages = new StringBuffer (); 154 for (Iterator i = errors.iterator(); i.hasNext();) { 155 errorMessages.append((String )i.next()); 156 errorMessages.append("\n"); 157 } 158 throw new CmsLegacyException(errorMessages.toString(), CmsLegacyException.C_SQL_ERROR); 159 } 160 } catch (SQLException exc) { 161 throw new CmsLegacyException(CmsLegacyException.C_SQL_ERROR, exc); 162 } finally { 163 m_sqlManager.closeAll(null, conn, null, null); 164 } 165 } 166 167 172 public void setRootChannel(String newRootChannel) { 173 m_rootChannel = newRootChannel; 174 } 175 176 181 public String getRootChannel() { 182 return m_rootChannel; 183 } 184 185 193 public void insert(CmsObject cms, CmsMasterContent content, CmsMasterDataSet dataset) throws CmsException { 194 if (isOnlineProject(cms)) { 195 throw new CmsSecurityException(Messages.get().container(Messages.ERR_SECURITY_NO_MODIFY_IN_ONLINE_PROJECT_0)); 197 } 198 199 if (dataset.m_masterId == null || CmsUUID.getNullUUID().equals(dataset.m_masterId)) { 200 dataset.m_masterId = new CmsUUID(); 202 } 203 204 int projectId = cms.getRequestContext().currentProject().getId(); 205 long currentTime = new java.util.Date ().getTime(); 206 207 CmsUUID currentUserId = cms.getRequestContext().currentUser().getId(); 208 dataset.m_userId = currentUserId; 209 210 CmsUUID defaultGroupId = CmsUUID.getNullUUID(); 211 String defaultGroupName = null; 212 213 try { 214 defaultGroupName = (String )cms.getRequestContext().currentUser().getAdditionalInfo( 215 CmsUserSettings.ADDITIONAL_INFO_DEFAULTGROUP); 216 217 if (defaultGroupName == null || "".equalsIgnoreCase(defaultGroupName)) { 218 if (CmsLog.getLog(this).isWarnEnabled()) { 219 CmsLog.getLog(this).warn( 220 "Error reading default group of user " 221 + cms.getRequestContext().currentUser().getName() 222 + ", using group " 223 + OpenCms.getDefaultUsers().getGroupUsers() 224 + " instead"); 225 } 226 227 defaultGroupName = OpenCms.getDefaultUsers().getGroupUsers(); 228 } 229 230 CmsGroup defaultGroup = cms.readGroup(defaultGroupName); 231 defaultGroupId = defaultGroup.getId(); 232 } catch (CmsException e) { 233 if (CmsLog.getLog(this).isErrorEnabled()) { 234 CmsLog.getLog(this).error( 235 "Error reading default group " 236 + defaultGroupName 237 + " of user " 238 + cms.getRequestContext().currentUser().getName(), 239 e); 240 } 241 242 defaultGroupId = CmsUUID.getNullUUID(); 243 } 244 245 dataset.m_groupId = defaultGroupId; 246 247 dataset.m_projectId = projectId; 248 dataset.m_lockedInProject = projectId; 249 dataset.m_state = CmsResource.STATE_NEW; 250 dataset.m_lockedBy = currentUserId; 251 dataset.m_lastModifiedBy = currentUserId; 252 dataset.m_dateCreated = currentTime; 253 dataset.m_dateLastModified = currentTime; 254 255 PreparedStatement stmt = null; 256 Connection conn = null; 257 try { 258 conn = m_sqlManager.getConnection(); 259 stmt = m_sqlManager.getPreparedStatement(conn, "insert_offline"); 260 sqlFillValues(stmt, content.getSubId(), dataset); 261 stmt.executeUpdate(); 262 updateMedia(dataset.m_masterId, dataset.m_mediaToAdd, new Vector (), new Vector ()); 264 updateChannels(cms, dataset.m_masterId, dataset.m_channelToAdd, dataset.m_channelToDelete); 265 } catch (SQLException exc) { 266 throw new CmsLegacyException(CmsLegacyException.C_SQL_ERROR, exc); 267 } finally { 268 m_sqlManager.closeAll(null, conn, stmt, null); 269 } 270 } 271 272 283 public CmsUUID copy(CmsObject cms, CmsMasterContent content, CmsMasterDataSet dataset, Vector mediaToAdd, Vector channelToAdd) throws CmsException { 284 if (isOnlineProject(cms)) { 285 throw new CmsSecurityException(Messages.get().container(Messages.ERR_SECURITY_NO_MODIFY_IN_ONLINE_PROJECT_0)); 287 } 288 if (dataset.m_versionId != CmsDbUtil.UNKNOWN_ID) { 289 throw new CmsLegacySecurityException("Can't update a cd with a backup cd ", CmsLegacySecurityException.C_SECURITY_NO_PERMISSIONS); 292 } 293 if (!content.isWriteable()) { 294 throw new CmsLegacySecurityException("Not writeable", CmsLegacySecurityException.C_SECURITY_NO_PERMISSIONS); 296 } 297 CmsUUID newMasterId = new CmsUUID(); 298 int projectId = cms.getRequestContext().currentProject().getId(); 299 CmsUUID currentUserId = cms.getRequestContext().currentUser().getId(); 300 long currentTime = new java.util.Date ().getTime(); 301 dataset.m_masterId = newMasterId; 303 dataset.m_projectId = projectId; 304 dataset.m_lockedInProject = projectId; 305 dataset.m_state = CmsResource.STATE_NEW; 306 dataset.m_lockedBy = currentUserId; 307 dataset.m_lastModifiedBy = currentUserId; 308 dataset.m_dateCreated = currentTime; 309 dataset.m_dateLastModified = currentTime; 310 311 PreparedStatement stmt = null; 312 Connection conn = null; 313 try { 314 conn = m_sqlManager.getConnection(); 315 stmt = m_sqlManager.getPreparedStatement(conn, "insert_offline"); 316 sqlFillValues(stmt, content.getSubId(), dataset); 317 stmt.executeUpdate(); 318 updateMedia(dataset.m_masterId, mediaToAdd, new Vector (), new Vector ()); 320 updateChannels(cms, dataset.m_masterId, channelToAdd, new Vector ()); 321 } catch (SQLException exc) { 322 throw new CmsLegacyException(CmsLegacyException.C_SQL_ERROR, exc); 323 } finally { 324 m_sqlManager.closeAll(null, conn, stmt, null); 325 } 326 return newMasterId; 327 } 328 329 336 public void writeLockstate(CmsObject cms, CmsMasterContent content, CmsMasterDataSet dataset) throws CmsException { 337 if (isOnlineProject(cms)) { 338 throw new CmsSecurityException(Messages.get().container(Messages.ERR_SECURITY_NO_MODIFY_IN_ONLINE_PROJECT_0)); 340 } 341 if (!content.isWriteable()) { 342 throw new CmsLegacySecurityException("Not writeable", CmsLegacySecurityException.C_SECURITY_NO_PERMISSIONS); 344 } 345 if (!dataset.m_lockedBy.isNullUUID()) { 346 dataset.m_lockedInProject = cms.getRequestContext().currentProject().getId(); 348 } 349 350 PreparedStatement stmt = null; 351 Connection conn = null; 352 try { 353 conn = m_sqlManager.getConnection(); 354 stmt = m_sqlManager.getPreparedStatement(conn, "update_lockstate_offline"); 355 stmt.setString(1, dataset.m_lockedBy.toString()); 356 stmt.setInt(2, dataset.m_lockedInProject); 357 stmt.setString(3, dataset.m_masterId.toString()); 358 stmt.setInt(4, content.getSubId()); 359 stmt.executeUpdate(); 360 } catch (SQLException exc) { 361 throw new CmsLegacyException(CmsLegacyException.C_SQL_ERROR, exc); 362 } finally { 363 m_sqlManager.closeAll(null, conn, stmt, null); 364 } 365 } 366 367 374 public void write(CmsObject cms, CmsMasterContent content, CmsMasterDataSet dataset) throws CmsException { 375 if (isOnlineProject(cms)) { 376 throw new CmsSecurityException(Messages.get().container(Messages.ERR_SECURITY_NO_MODIFY_IN_ONLINE_PROJECT_0)); 378 } 379 if (dataset.m_versionId != CmsDbUtil.UNKNOWN_ID) { 380 throw new CmsLegacySecurityException("Can't update a cd with a backup cd", CmsLegacySecurityException.C_SECURITY_NO_PERMISSIONS); 383 } 384 readLockstate(dataset, content.getSubId()); 386 if (!dataset.m_lockedBy.equals(cms.getRequestContext().currentUser().getId())) { 387 throw new CmsLegacySecurityException("Not locked by this user", CmsLegacySecurityException.C_SECURITY_NO_PERMISSIONS); 389 } 390 if (dataset.m_lockedInProject != dataset.m_projectId) { 391 throw new CmsLegacySecurityException("Not locked in this project", CmsLegacySecurityException.C_SECURITY_NO_PERMISSIONS); 393 } 394 if (!content.isWriteable()) { 395 throw new CmsLegacySecurityException("Not writeable", CmsLegacySecurityException.C_SECURITY_NO_PERMISSIONS); 397 } 398 399 long currentTime = new java.util.Date ().getTime(); 400 CmsUUID currentUserId = cms.getRequestContext().currentUser().getId(); 401 if (dataset.m_state != CmsResource.STATE_NEW) { 403 dataset.m_state = CmsResource.STATE_CHANGED; 405 } 406 dataset.m_lastModifiedBy = currentUserId; 407 dataset.m_dateLastModified = currentTime; 408 409 PreparedStatement stmt = null; 410 Connection conn = null; 411 try { 412 conn = m_sqlManager.getConnection(); 413 stmt = m_sqlManager.getPreparedStatement(conn, "update_offline"); 414 int rowcounter = sqlFillValues(stmt, content.getSubId(), dataset); 415 stmt.setString(rowcounter++, dataset.m_masterId.toString()); 416 stmt.setInt(rowcounter++, content.getSubId()); 417 stmt.executeUpdate(); 418 updateMedia(dataset.m_masterId, dataset.m_mediaToAdd, dataset.m_mediaToUpdate, dataset.m_mediaToDelete); 420 updateChannels(cms, dataset.m_masterId, dataset.m_channelToAdd, dataset.m_channelToDelete); 421 } catch (SQLException exc) { 422 throw new CmsLegacyException(CmsLegacyException.C_SQL_ERROR, exc); 423 } finally { 424 m_sqlManager.closeAll(null, conn, stmt, null); 425 } 426 } 427 428 436 public void read(CmsObject cms, CmsMasterContent content, CmsMasterDataSet dataset, CmsUUID contentId) throws CmsException { 437 if (!content.isReadable()) { 438 throw new CmsLegacySecurityException("Not readable", CmsLegacySecurityException.C_SECURITY_NO_PERMISSIONS); 440 } 441 String statement_key = "read_offline"; 442 if (isOnlineProject(cms)) { 443 statement_key = "read_online"; 444 } 445 446 PreparedStatement stmt = null; 447 ResultSet res = null; 448 Connection conn = null; 449 try { 450 conn = m_sqlManager.getConnection(); 451 stmt = m_sqlManager.getPreparedStatement(conn, statement_key); 452 stmt.setString(1, contentId.toString()); 453 stmt.setInt(2, content.getSubId()); 454 res = stmt.executeQuery(); 455 if (res.next()) { 456 sqlFillValues(res, cms, dataset); 457 } else { 458 throw new CmsLegacyException("[" + this.getClass().getName() + ".read] no content found for CID:" + contentId + ", SID: " + content.getSubId() + ", statement: " + statement_key, CmsLegacyException.C_NOT_FOUND); 459 } 460 if (!checkAccess(content, false)) { 461 throw new CmsLegacySecurityException("Not readable", CmsLegacySecurityException.C_SECURITY_NO_PERMISSIONS); 462 } 463 } catch (SQLException exc) { 464 throw new CmsLegacyException(CmsLegacyException.C_SQL_ERROR, exc); 465 } finally { 466 m_sqlManager.closeAll(null, conn, stmt, res); 467 } 468 } 469 470 477 protected void readLockstate(CmsMasterDataSet dataset, int subId) throws CmsException { 478 PreparedStatement stmt = null; 479 ResultSet res = null; 480 Connection conn = null; 481 try { 482 conn = m_sqlManager.getConnection(); 483 stmt = m_sqlManager.getPreparedStatement(conn, "read_lockstate_offline"); 484 stmt.setString(1, dataset.m_masterId.toString()); 485 stmt.setInt(2, subId); 486 res = stmt.executeQuery(); 487 if (res.next()) { 488 dataset.m_lockedInProject = res.getInt(1); 490 dataset.m_lockedBy = new CmsUUID(res.getString(2)); 491 } else { 492 } 494 } catch (SQLException exc) { 495 throw new CmsLegacyException(CmsLegacyException.C_SQL_ERROR, exc); 496 } finally { 497 m_sqlManager.closeAll(null, conn, stmt, res); 498 } 499 } 500 501 508 public Vector readMedia(CmsObject cms, CmsMasterContent content) throws CmsException { 509 if (!content.isReadable()) { 510 throw new CmsLegacySecurityException("Not readable", CmsLegacySecurityException.C_SECURITY_NO_PERMISSIONS); 512 } 513 Vector retValue = new Vector (); 514 String statement_key = "read_media_offline"; 515 if (isOnlineProject(cms)) { 516 statement_key = "read_media_online"; 517 } 518 519 PreparedStatement stmt = null; 520 ResultSet res = null; 521 Connection conn = null; 522 try { 523 conn = m_sqlManager.getConnection(); 524 stmt = m_sqlManager.getPreparedStatement(conn, statement_key); 525 stmt.setString(1, content.getId().toString()); 526 res = stmt.executeQuery(); 527 while (res.next()) { 528 int i = 1; 529 retValue.add(new CmsMasterMedia(res.getInt(i++), new CmsUUID(res.getString(i++)), res.getInt(i++), res.getInt(i++), res.getInt(i++), res.getInt(i++), res.getString(i++), res.getInt(i++), res.getString(i++), res.getString(i++), res.getString(i++), res.getBytes(i++))); 530 } 531 } catch (SQLException exc) { 532 throw new CmsLegacyException(CmsLegacyException.C_SQL_ERROR, exc); 533 } finally { 534 m_sqlManager.closeAll(null, conn, stmt, res); 535 } 536 return retValue; 537 } 538 539 546 public Vector readChannels(CmsObject cms, CmsMasterContent content) throws CmsException { 547 if (!content.isReadable()) { 548 throw new CmsLegacySecurityException("Not readable", CmsLegacySecurityException.C_SECURITY_NO_PERMISSIONS); 550 } 551 Vector retValue = new Vector (); 552 String statement_key = "read_channel_names_offline"; 553 if (isOnlineProject(cms)) { 554 statement_key = "read_channel_names_online"; 555 } 556 557 PreparedStatement stmt = null; 558 ResultSet res = null; 559 Connection conn = null; 560 try { 561 conn = m_sqlManager.getConnection(); 562 stmt = m_sqlManager.getPreparedStatement(conn, statement_key); 563 stmt.setString(1, content.getId().toString()); 564 res = stmt.executeQuery(); 565 566 while (res.next()) { 567 String channeldName = res.getString(1); 569 retValue.add(channeldName ); 570 } 571 572 } catch (SQLException exc) { 573 throw new CmsLegacyException(CmsLegacyException.C_SQL_ERROR, exc); 574 } finally { 575 m_sqlManager.closeAll(null, conn, stmt, res); 576 } 577 return retValue; 578 } 579 580 588 public Vector readAllByChannel(CmsObject cms, String channelId, int subId) throws CmsException { 589 Vector theDataSets = new Vector (); 590 String statement_key = "readallbychannel_offline"; 591 if (isOnlineProject(cms)) { 592 statement_key = "readallbychannel_online"; 593 } 594 595 PreparedStatement stmt = null; 596 ResultSet res = null; 597 Connection conn = null; 598 try { 599 conn = m_sqlManager.getConnection(); 600 stmt = m_sqlManager.getPreparedStatement(conn, statement_key); 601 stmt.setInt(1, subId); 602 stmt.setString(2, channelId); 603 res = stmt.executeQuery(); 604 while (res.next()) { 605 CmsMasterDataSet dataset = new CmsMasterDataSet(); 606 sqlFillValues(res, cms, dataset); 607 theDataSets.add(dataset); 608 } 609 } catch (SQLException exc) { 610 throw new CmsLegacyException(CmsLegacyException.C_SQL_ERROR, exc); 611 } finally { 612 m_sqlManager.closeAll(null, conn, stmt, res); 613 } 614 return theDataSets; 615 } 616 617 624 public void delete(CmsObject cms, CmsMasterContent content, CmsMasterDataSet dataset) throws CmsException { 625 if (isOnlineProject(cms)) { 626 throw new CmsSecurityException(Messages.get().container(Messages.ERR_SECURITY_NO_MODIFY_IN_ONLINE_PROJECT_0)); 628 } 629 if (dataset.m_versionId != CmsDbUtil.UNKNOWN_ID) { 630 throw new CmsLegacySecurityException("Can't delete a backup cd", CmsLegacySecurityException.C_SECURITY_NO_PERMISSIONS); 633 } 634 readLockstate(dataset, content.getSubId()); 636 if ((!dataset.m_lockedBy.equals(cms.getRequestContext().currentUser().getId()))) { 637 throw new CmsLegacySecurityException("Not locked by this user", CmsLegacySecurityException.C_SECURITY_NO_PERMISSIONS); 639 } 640 if (dataset.m_lockedInProject != dataset.m_projectId) { 641 throw new CmsLegacySecurityException("Not locked in this project", CmsLegacySecurityException.C_SECURITY_NO_PERMISSIONS); 643 } 644 if (!content.isWriteable()) { 645 throw new CmsLegacySecurityException("Not writeable", CmsLegacySecurityException.C_SECURITY_NO_PERMISSIONS); 647 } 648 649 if (dataset.m_state == CmsResource.STATE_NEW) { 650 String statement_key = "delete_offline"; 652 PreparedStatement stmt = null; 653 Connection conn = null; 654 try { 655 conn = m_sqlManager.getConnection(); 656 stmt = m_sqlManager.getPreparedStatement(conn, statement_key); 657 stmt.setString(1, dataset.m_masterId.toString()); 658 stmt.setInt(2, content.getSubId()); 659 if (stmt.executeUpdate() != 1) { 660 throw new CmsLegacyException("Row not found: " + dataset.m_masterId + " " + content.getSubId(), CmsLegacyException.C_NOT_FOUND); 662 } 663 deleteAllMedia(dataset.m_masterId); 665 deleteAllChannels(dataset.m_masterId); 666 } catch (SQLException exc) { 667 throw new CmsLegacyException(CmsLegacyException.C_SQL_ERROR, exc); 668 } finally { 669 m_sqlManager.closeAll(null, conn, stmt, null); 670 } 671 } else { 672 dataset.m_state = CmsResource.STATE_DELETED; 674 dataset.m_lockedBy = CmsUUID.getNullUUID(); 675 PreparedStatement stmt = null; 676 Connection conn = null; 677 try { 678 conn = m_sqlManager.getConnection(); 679 stmt = m_sqlManager.getPreparedStatement(conn, "update_offline"); 680 int rowcounter = sqlFillValues(stmt, content.getSubId(), dataset); 681 stmt.setString(rowcounter++, dataset.m_masterId.toString()); 682 stmt.setInt(rowcounter++, content.getSubId()); 683 stmt.executeUpdate(); 684 } catch (SQLException exc) { 685 throw new CmsLegacyException(CmsLegacyException.C_SQL_ERROR, exc); 686 } finally { 687 m_sqlManager.closeAll(null, conn, stmt, null); 688 } 689 } 690 } 691 692 699 public void undelete(CmsObject cms, CmsMasterContent content, CmsMasterDataSet dataset) throws CmsException { 700 if (isOnlineProject(cms)) { 701 throw new CmsSecurityException(Messages.get().container(Messages.ERR_SECURITY_NO_MODIFY_IN_ONLINE_PROJECT_0)); 703 } 704 if (dataset.m_versionId != CmsDbUtil.UNKNOWN_ID) { 705 throw new CmsLegacySecurityException("Can't undelete a backup cd ", CmsLegacySecurityException.C_SECURITY_NO_PERMISSIONS); 708 } 709 if (!content.isWriteable()) { 710 throw new CmsLegacySecurityException("Not writeable", CmsLegacySecurityException.C_SECURITY_NO_PERMISSIONS); 712 } 713 dataset.m_state = CmsResource.STATE_CHANGED; 715 dataset.m_lockedBy = cms.getRequestContext().currentUser().getId(); 716 dataset.m_lockedInProject = cms.getRequestContext().currentProject().getId(); 717 PreparedStatement stmt = null; 718 Connection conn = null; 719 try { 720 conn = m_sqlManager.getConnection(); 721 stmt = m_sqlManager.getPreparedStatement(conn, "update_offline"); 722 int rowcounter = sqlFillValues(stmt, content.getSubId(), dataset); 723 stmt.setString(rowcounter++, dataset.m_masterId.toString()); 724 stmt.setInt(rowcounter++, content.getSubId()); 725 stmt.executeUpdate(); 726 } catch (SQLException exc) { 727 throw new CmsLegacyException(CmsLegacyException.C_SQL_ERROR, exc); 728 } finally { 729 m_sqlManager.closeAll(null, conn, stmt, null); 730 } 731 } 732 733 740 public void changePermissions(CmsObject cms, CmsMasterContent content, CmsMasterDataSet dataset) throws CmsException { 741 if (isOnlineProject(cms)) { 742 throw new CmsSecurityException(Messages.get().container(Messages.ERR_SECURITY_NO_MODIFY_IN_ONLINE_PROJECT_0)); 744 } 745 if (dataset.m_versionId != CmsDbUtil.UNKNOWN_ID) { 746 throw new CmsLegacySecurityException("Can't change permissions of a backup cd ", CmsLegacySecurityException.C_SECURITY_NO_PERMISSIONS); 749 } 750 readLockstate(dataset, content.getSubId()); 752 if (!dataset.m_lockedBy.equals(cms.getRequestContext().currentUser().getId())) { 753 throw new CmsLegacySecurityException("Not locked by this user", CmsLegacySecurityException.C_SECURITY_NO_PERMISSIONS); 755 } 756 if (dataset.m_lockedInProject != dataset.m_projectId) { 757 throw new CmsLegacySecurityException("Not locked in this project", CmsLegacySecurityException.C_SECURITY_NO_PERMISSIONS); 759 } 760 if (!content.isWriteable()) { 761 throw new CmsLegacySecurityException("Not writeable", CmsLegacySecurityException.C_SECURITY_NO_PERMISSIONS); 763 } 764 if (dataset.m_state != CmsResource.STATE_NEW) { 765 dataset.m_state = CmsResource.STATE_CHANGED; 766 } 767 dataset.m_dateLastModified = System.currentTimeMillis(); 768 dataset.m_lastModifiedBy = cms.getRequestContext().currentUser().getId(); 769 PreparedStatement stmt = null; 771 Connection conn = null; 772 try { 773 conn = m_sqlManager.getConnection(); 774 stmt = m_sqlManager.getPreparedStatement(conn, "update_permissions_offline"); 775 stmt.setString(1, dataset.m_userId.toString()); 776 stmt.setString(2, dataset.m_groupId.toString()); 777 stmt.setInt(3, dataset.m_accessFlags); 778 stmt.setInt(4, dataset.m_state); 779 stmt.setString(5, dataset.m_lastModifiedBy.toString()); 780 stmt.setTimestamp(6, new Timestamp (dataset.m_dateLastModified)); 781 stmt.setString(7, dataset.m_masterId.toString()); 782 stmt.setInt(8, content.getSubId()); 783 stmt.executeUpdate(); 784 } catch (SQLException exc) { 785 throw new CmsLegacyException(CmsLegacyException.C_SQL_ERROR, exc); 786 } finally { 787 m_sqlManager.closeAll(null, conn, stmt, null); 788 } 789 } 790 791 797 public String toString() { 798 StringBuffer returnValue = new StringBuffer (); 799 returnValue.append(this.getClass().getName() + "{"); 800 returnValue.append("Used db pool=" + m_poolUrl + ";"); 801 returnValue.append("}"); 802 return returnValue.toString(); 803 } 804 805 813 protected int sqlFillValues(PreparedStatement stmt, int subId, CmsMasterDataSet dataset) throws SQLException { 814 int i = 1; 816 stmt.setString(i++, dataset.m_masterId.toString()); 818 stmt.setInt(i++, subId); 819 stmt.setString(i++, dataset.m_userId.toString()); 820 stmt.setString(i++, dataset.m_groupId.toString()); 821 stmt.setInt(i++, dataset.m_lockedInProject); 822 stmt.setInt(i++, dataset.m_accessFlags); 823 stmt.setInt(i++, dataset.m_state); 824 stmt.setString(i++, dataset.m_lockedBy.toString()); 825 stmt.setString(i++, dataset.m_lastModifiedBy.toString()); 826 stmt.setTimestamp(i++, new Timestamp (dataset.m_dateCreated)); 827 stmt.setTimestamp(i++, new Timestamp (dataset.m_dateLastModified)); 828 stmt.setTimestamp(i++, new Timestamp (dataset.m_publicationDate)); 830 stmt.setTimestamp(i++, new Timestamp (dataset.m_purgeDate)); 831 stmt.setInt(i++, dataset.m_flags); 832 stmt.setInt(i++, dataset.m_feedId); 833 stmt.setInt(i++, dataset.m_feedReference); 834 if (dataset.m_feedFilename == null) { 835 stmt.setNull(i++, Types.VARCHAR); 836 } else { 837 stmt.setString(i++, dataset.m_feedFilename); 838 } 839 if (dataset.m_title == null) { 840 stmt.setNull(i++, Types.VARCHAR); 841 } else { 842 stmt.setString(i++, dataset.m_title); 843 } 844 i = sqlSetTextArray(stmt, dataset.m_dataBig, i); 846 i = sqlSetTextArray(stmt, dataset.m_dataMedium, i); 847 i = sqlSetTextArray(stmt, dataset.m_dataSmall, i); 848 i = sqlSetIntArray(stmt, dataset.m_dataInt, i); 849 i = sqlSetIntArray(stmt, dataset.m_dataReference, i); 850 i = sqlSetDateArray(stmt, dataset.m_dataDate, i); 851 return i; 852 } 853 854 862 protected int sqlFillValues(ResultSet res, CmsObject cms, CmsMasterDataSet dataset) throws SQLException { 863 int i = 1; 865 dataset.m_masterId = new CmsUUID(res.getString(i++)); 867 dataset.m_subId = res.getInt(i++); 869 dataset.m_userId = new CmsUUID(res.getString(i++)); 870 dataset.m_groupId = new CmsUUID(res.getString(i++)); 871 dataset.m_lockedInProject = res.getInt(i++); 872 dataset.m_projectId = computeProjectId(cms, dataset); 874 dataset.m_accessFlags = res.getInt(i++); 875 dataset.m_state = res.getInt(i++); 876 dataset.m_lockedBy = new CmsUUID(res.getString(i++)); 877 dataset.m_lastModifiedBy = new CmsUUID(res.getString(i++)); 878 dataset.m_dateCreated = res.getTimestamp(i++).getTime(); 879 dataset.m_dateLastModified = res.getTimestamp(i++).getTime(); 880 dataset.m_publicationDate = res.getTimestamp(i++).getTime(); 882 dataset.m_purgeDate = res.getTimestamp(i++).getTime(); 883 dataset.m_flags = res.getInt(i++); 884 dataset.m_feedId = res.getInt(i++); 885 dataset.m_feedReference = res.getInt(i++); 886 dataset.m_feedFilename = res.getString(i++); 887 dataset.m_title = res.getString(i++); 888 i = sqlSetTextArray(res, dataset.m_dataBig, i); 890 i = sqlSetTextArray(res, dataset.m_dataMedium, i); 891 i = sqlSetTextArray(res, dataset.m_dataSmall, i); 892 i = sqlSetIntArray(res, dataset.m_dataInt, i); 893 i = sqlSetIntArray(res, dataset.m_dataReference, i); 894 i = sqlSetDateArray(res, dataset.m_dataDate, i); 895 return i; 896 } 897 898 906 protected int computeProjectId(CmsObject cms, CmsMasterDataSet dataset) throws SQLException { 907 int offlineProjectId = CmsDbUtil.UNKNOWN_ID; 909 910 offlineProjectId = cms.getRequestContext().currentProject().getId(); 911 913 if (!isOnlineProject(cms)) { 914 917 if ((cms.getRequestContext().currentUser().getId().equals(dataset.m_userId)) || cms.isAdmin()) { 919 return offlineProjectId; 920 } 921 922 return offlineProjectId; 923 924 } 963 return offlineProjectId; 966 } 967 968 976 protected int sqlSetTextArray(PreparedStatement stmt, String [] array, int columnscounter) throws SQLException { 977 for (int j = 0; j < array.length; j++) { 978 if (array[j] == null) { 979 stmt.setNull(columnscounter++, Types.LONGVARCHAR); 980 } else { 981 stmt.setString(columnscounter++, array[j]); 982 } 983 } 984 return columnscounter; 985 } 986 987 995 protected int sqlSetTextArray(ResultSet res, String [] array, int columnscounter) throws SQLException { 996 for (int j = 0; j < array.length; j++) { 997 array[j] = res.getString(columnscounter++); 998 } 999 return columnscounter; 1000 } 1001 1002 1010 protected int sqlSetIntArray(PreparedStatement stmt, int[] array, int columnscounter) throws SQLException { 1011 for (int j = 0; j < array.length; j++) { 1012 stmt.setInt(columnscounter++, array[j]); 1013 } 1014 return columnscounter; 1015 } 1016 1017 1025 protected int sqlSetIntArray(ResultSet res, int[] array, int columnscounter) throws SQLException { 1026 for (int j = 0; j < array.length; j++) { 1027 array[j] = res.getInt(columnscounter++); 1028 } 1029 return columnscounter; 1030 } 1031 1032 1040 protected int sqlSetDateArray(PreparedStatement stmt, long[] array, int columnscounter) throws SQLException { 1041 for (int j = 0; j < array.length; j++) { 1042 stmt.setTimestamp(columnscounter++, new Timestamp (array[j])); 1043 } 1044 return columnscounter; 1045 } 1046 1047 1055 protected int sqlSetDateArray(ResultSet res, long[] array, int columnscounter) throws SQLException { 1056 for (int j = 0; j < array.length; j++) { 1057 array[j] = res.getTimestamp(columnscounter++).getTime(); 1058 } 1059 return columnscounter; 1060 } 1061 1062 1071 protected Vector createVectorOfCd(ResultSet res, Class contentDefinitionClass, CmsObject cms) throws SQLException { 1072 return createVectorOfCd(res, contentDefinitionClass, cms, false); 1073 } 1074 1075 1084 protected Vector createVectorOfCd(ResultSet res, Class contentDefinitionClass, CmsObject cms, boolean viewonly) throws SQLException { 1085 Constructor constructor; 1086 Vector retValue = new Vector (); 1087 1088 try { constructor = contentDefinitionClass.getConstructor(new Class [] { CmsObject.class, CmsMasterDataSet.class }); 1090 } catch (NoSuchMethodException exc) { 1091 1092 if (CmsLog.getLog(this).isWarnEnabled()) { 1093 CmsLog.getLog(this).warn("Cannot locate constructor", exc); 1094 } 1095 return retValue; 1097 } 1098 while (res.next()) { CmsMasterDataSet dataset = new CmsMasterDataSet(); 1100 try { CmsMasterContent content = (CmsMasterContent)constructor.newInstance(new Object [] { cms, dataset }); 1102 1103 sqlFillValues(res, cms, dataset); 1104 if (checkAccess(content, viewonly)) { 1108 retValue.add(content); 1109 } 1110 } catch (Exception exc) { 1111 if (CmsLog.getLog(this).isWarnEnabled()) { 1112 CmsLog.getLog(this).warn("Cannot invoce constructor", exc); 1113 } 1114 } 1115 } 1116 return retValue; 1117 } 1118 1119 1127 protected Vector createVectorOfCd(Vector datasets, Class contentDefinitionClass, CmsObject cms) throws SQLException { 1128 Constructor constructor; 1129 Vector retValue = new Vector (); 1130 try { constructor = contentDefinitionClass.getConstructor(new Class [] { CmsObject.class, CmsMasterDataSet.class }); 1132 } catch (NoSuchMethodException exc) { 1133 if (CmsLog.getLog(this).isWarnEnabled()) { 1134 CmsLog.getLog(this).warn("Cannot locate constructor", exc); 1135 } 1136 return retValue; 1138 } 1139 for (int i = 0; i < datasets.size(); i++) { 1141 CmsMasterDataSet dataset = (CmsMasterDataSet)datasets.elementAt(i); 1142 try { CmsMasterContent content = (CmsMasterContent)constructor.newInstance(new Object [] { cms, dataset }); 1144 retValue.add(content); 1145 } catch (Exception exc) { 1146 if (CmsLog.getLog(this).isWarnEnabled()) { 1147 CmsLog.getLog(this).warn("Cannot invoce constructor", exc); 1148 } 1149 } 1150 } 1151 return retValue; 1152 } 1153 1154 1162 protected boolean checkAccess(CmsMasterContent content, boolean viewonly) { 1163 if (!content.isReadable()) { 1164 return false; 1166 } else if (viewonly) { 1167 return content.isVisible(); 1169 } else { 1170 return true; 1172 } 1173 } 1174 1175 1181 protected boolean isOnlineProject(CmsObject cms) { 1182 return cms.getRequestContext().currentProject().isOnlineProject(); 1183 } 1184 1185 1191 protected void deleteAllMedia(CmsUUID masterId) throws SQLException { 1192 String statement_key = "delete_all_media_offline"; 1193 PreparedStatement stmt = null; 1194 Connection conn = null; 1195 try { 1196 conn = m_sqlManager.getConnection(); 1197 stmt = m_sqlManager.getPreparedStatement(conn, statement_key); 1198 stmt.setString(1, masterId.toString()); 1199 stmt.executeUpdate(); 1200 } finally { 1201 m_sqlManager.closeAll(null, conn, stmt, null); 1202 } 1203 } 1204 1205 1211 protected void deleteAllChannels(CmsUUID masterId) throws SQLException { 1212 String statement_key = "delete_all_channel_offline"; 1213 PreparedStatement stmt = null; 1214 Connection conn = null; 1215 try { 1216 conn = m_sqlManager.getConnection(); 1217 stmt = m_sqlManager.getPreparedStatement(conn, statement_key); 1218 stmt.setString(1, masterId.toString()); 1219 stmt.executeUpdate(); 1220 } finally { 1221 m_sqlManager.closeAll(null, conn, stmt, null); 1222 } 1223 } 1224 1225 1235 protected void updateMedia(CmsUUID masterId, Vector mediaToAdd, Vector mediaToUpdate, Vector mediaToDelete) throws SQLException , CmsException { 1236 PreparedStatement stmt = null; 1238 Connection conn = null; 1239 try { 1240 conn = m_sqlManager.getConnection(); 1241 stmt = m_sqlManager.getPreparedStatement(conn, "insert_media_offline"); 1242 for (int i = 0; i < mediaToAdd.size(); i++) { 1243 CmsMasterMedia media = (CmsMasterMedia)mediaToAdd.get(i); 1244 media.setId(CmsDbUtil.nextId(m_poolUrl, "CMS_MODULE_MEDIA")); 1245 media.setMasterId(masterId); 1246 sqlFillValues(stmt, media); 1247 stmt.executeUpdate(); 1248 } 1249 } finally { 1250 m_sqlManager.closeAll(null, conn, stmt, null); 1251 } 1252 1253 stmt = null; 1255 conn = null; 1256 try { 1257 conn = m_sqlManager.getConnection(); 1258 stmt = m_sqlManager.getPreparedStatement(conn, "update_media_offline"); 1259 for (int i = 0; i < mediaToUpdate.size(); i++) { 1260 CmsMasterMedia media = (CmsMasterMedia)mediaToUpdate.get(i); 1261 media.setMasterId(masterId); 1262 int rowCounter = sqlFillValues(stmt, media); 1263 stmt.setInt(rowCounter++, media.getId()); 1264 stmt.setString(rowCounter++, masterId.toString()); 1265 stmt.executeUpdate(); 1266 } 1267 } finally { 1268 m_sqlManager.closeAll(null, conn, stmt, null); 1269 } 1270 stmt = null; 1272 conn = null; 1273 try { 1274 conn = m_sqlManager.getConnection(); 1275 stmt = m_sqlManager.getPreparedStatement(conn, "delete_media_offline"); 1276 for (int i = 0; i < mediaToDelete.size(); i++) { 1277 CmsMasterMedia media = (CmsMasterMedia)mediaToDelete.get(i); 1278 stmt.setInt(1, media.getId()); 1279 stmt.setString(2, masterId.toString()); 1280 stmt.executeUpdate(); 1281 } 1282 } finally { 1283 m_sqlManager.closeAll(null, conn, stmt, null); 1284 } 1285 } 1286 1287 1296 protected void updateChannels(CmsObject cms, CmsUUID masterId, Vector channelToAdd, Vector channelToDelete) throws SQLException { 1297 PreparedStatement stmt = null; 1299 Connection conn = null; 1300 cms.getRequestContext().saveSiteRoot(); 1301 cms.getRequestContext().setSiteRoot(CmsResource.VFS_FOLDER_CHANNELS); 1302 conn = m_sqlManager.getConnection(); 1303 stmt = m_sqlManager.getPreparedStatement(conn, "insert_channel_offline"); 1304 for (int i = 0; i < channelToAdd.size(); i++) { 1305 try { 1306 stmt.setString(1, masterId.toString()); 1307 1308 1309 CmsResource channel=cms.readFolder((String )channelToAdd.get(i)); 1312 String id=channel.getResourceId().toString(); 1313 1314 stmt.setString(2, id); 1315 stmt.setString(3,(String )channelToAdd.get(i)); 1316 stmt.executeUpdate(); 1318 } catch (CmsException exc) { 1319 if (CmsLog.getLog(this).isWarnEnabled()) { 1321 CmsLog.getLog(this).warn("Couldn't find channel " + channelToAdd.get(i), exc); 1322 } 1323 } 1324 } 1325 m_sqlManager.closeAll(null, conn, stmt, null); 1326 1327 stmt = null; 1329 conn = null; 1330 try { 1331 conn = m_sqlManager.getConnection(); 1332 stmt = m_sqlManager.getPreparedStatement(conn, "delete_channel_offline"); 1333 for (int i = 0; i < channelToDelete.size(); i++) { 1334 stmt.setString(1, masterId.toString()); 1336 stmt.setString(2, (String )channelToDelete.get(i) ); 1337 stmt.executeUpdate(); 1340 1346 } 1347 } finally { 1348 m_sqlManager.closeAll(null, conn, stmt, null); 1349 cms.getRequestContext().restoreSiteRoot(); 1350 } 1351 } 1352 1353 1361 protected int sqlFillValues(PreparedStatement stmt, CmsMasterMedia media) throws SQLException { 1362 int i = 1; 1363 stmt.setInt(i++, media.getId()); 1364 stmt.setString(i++, media.getMasterId().toString()); 1365 stmt.setInt(i++, media.getPosition()); 1366 stmt.setInt(i++, media.getWidth()); 1367 stmt.setInt(i++, media.getHeight()); 1368 stmt.setInt(i++, media.getSize()); 1369 stmt.setString(i++, media.getMimetype()); 1370 stmt.setInt(i++, media.getType()); 1371 stmt.setString(i++, media.getTitle()); 1372 stmt.setString(i++, media.getName()); 1373 stmt.setString(i++, media.getDescription()); 1374 stmt.setBinaryStream(i++, new ByteArrayInputStream (media.getMedia()), media.getMedia().length); 1375 return i; 1377 } 1378 1379 1388 public Vector getHistory(CmsObject cms, Class contentDefinitionClass, CmsUUID masterId, int subId) throws CmsException { 1389 Vector retVector = new Vector (); 1390 Vector allBackup = new Vector (); 1391 PreparedStatement stmt = null; 1392 ResultSet res = null; 1393 Connection conn = null; 1394 try { 1395 conn = m_sqlManager.getConnection(); 1396 stmt = m_sqlManager.getPreparedStatement(conn, "read_all_backup"); 1397 stmt.setString(1, masterId.toString()); 1398 stmt.setInt(2, subId); 1399 res = stmt.executeQuery(); 1401 while (res.next()) { 1402 CmsMasterDataSet dataset = new CmsMasterDataSet(); 1403 sqlFillValues(res, cms, dataset); 1404 dataset.m_versionId = res.getInt("TAG_ID"); 1405 dataset.m_userName = res.getString("USER_NAME"); 1406 dataset.m_groupName = res.getString("GROUP_NAME"); 1407 dataset.m_lastModifiedByName = res.getString("LASTMODIFIED_BY_NAME"); 1408 allBackup.add(dataset); 1409 } 1410 retVector = createVectorOfCd(allBackup, contentDefinitionClass, cms); 1411 } catch (SQLException e) { 1412 throw new CmsLegacyException(CmsLegacyException.C_SQL_ERROR, e); 1413 } finally { 1414 m_sqlManager.closeAll(null, conn, stmt, res); 1415 } 1416 return retVector; 1417 } 1418 1419 1429 public CmsMasterContent getVersionFromHistory(CmsObject cms, Class contentDefinitionClass, CmsUUID masterId, int subId, int versionId) throws CmsException { 1430 CmsMasterContent content = null; 1431 CmsMasterDataSet dataset = this.getVersionFromHistory(cms, masterId, subId, versionId); 1432 Constructor constructor; 1433 try { constructor = contentDefinitionClass.getConstructor(new Class [] { CmsObject.class, CmsMasterDataSet.class }); 1435 } catch (NoSuchMethodException exc) { 1436 if (CmsLog.getLog(this).isWarnEnabled()) { 1437 CmsLog.getLog(this).warn("Cannot locate constructor", exc); 1438 } 1439 return content; 1441 } 1442 if (dataset != null) { 1444 try { content = (CmsMasterContent)constructor.newInstance(new Object [] { cms, dataset }); 1446 } catch (Exception exc) { 1447 if (CmsLog.getLog(this).isWarnEnabled()) { 1448 CmsLog.getLog(this).warn("Cannot invoke constructor", exc); 1449 } 1450 } 1451 } 1452 return content; 1453 } 1454 1455 1464 public CmsMasterDataSet getVersionFromHistory(CmsObject cms, CmsUUID masterId, int subId, int versionId) throws CmsException { 1465 CmsMasterDataSet dataset = new CmsMasterDataSet(); 1466 PreparedStatement stmt = null; 1467 ResultSet res = null; 1468 Connection conn = null; 1469 try { 1470 conn = m_sqlManager.getConnection(); 1471 stmt = m_sqlManager.getPreparedStatement(conn, "read_backup"); 1472 stmt.setString(1, masterId.toString()); 1473 stmt.setInt(2, subId); 1474 stmt.setInt(3, versionId); 1475 res = stmt.executeQuery(); 1477 if (res.next()) { 1478 sqlFillValues(res, cms, dataset); 1479 dataset.m_versionId = res.getInt("TAG_ID"); 1480 dataset.m_userName = res.getString("USER_NAME"); 1481 dataset.m_groupName = res.getString("GROUP_NAME"); 1482 dataset.m_lastModifiedByName = res.getString("LASTMODIFIED_BY_NAME"); 1483 } else { 1484 throw new CmsLegacyException("Row not found: " + masterId + " " + subId + " version " + versionId, CmsLegacyException.C_NOT_FOUND); 1485 } 1486 } catch (SQLException e) { 1487 throw new CmsLegacyException(CmsLegacyException.C_SQL_ERROR, e); 1488 } finally { 1489 m_sqlManager.closeAll(null, conn, stmt, res); 1490 } 1491 return dataset; 1492 } 1493 1494 1502 public void restore(CmsObject cms, CmsMasterContent content, CmsMasterDataSet dataset, int versionId) throws CmsException { 1503 Connection conn = null; 1504 Connection conn2 = null; 1505 PreparedStatement stmt = null; 1506 PreparedStatement stmt2 = null; 1507 ResultSet res = null; 1508 CmsMasterDataSet backup = getVersionFromHistory(cms, dataset.m_masterId, content.getSubId(), versionId); 1510 dataset.m_accessFlags = backup.m_accessFlags; 1512 dataset.m_dataBig = backup.m_dataBig; 1513 dataset.m_dataInt = backup.m_dataInt; 1514 dataset.m_dataMedium = backup.m_dataMedium; 1515 dataset.m_dataReference = backup.m_dataReference; 1516 dataset.m_dataDate = backup.m_dataDate; 1517 dataset.m_dataSmall = backup.m_dataSmall; 1518 dataset.m_feedFilename = backup.m_feedFilename; 1519 dataset.m_feedId = backup.m_feedId; 1520 dataset.m_feedReference = backup.m_feedReference; 1521 dataset.m_flags = backup.m_flags; 1522 dataset.m_title = backup.m_title; 1523 dataset.m_publicationDate = backup.m_publicationDate; 1524 dataset.m_purgeDate = backup.m_purgeDate; 1525 dataset.m_channel = new Vector (); 1526 dataset.m_channelToAdd = new Vector (); 1527 dataset.m_channelToDelete = new Vector (); 1528 dataset.m_media = new Vector (); 1529 dataset.m_mediaToAdd = new Vector (); 1530 dataset.m_mediaToUpdate = new Vector (); 1531 dataset.m_mediaToDelete = new Vector (); 1532 dataset.m_lastModifiedBy = cms.getRequestContext().currentUser().getId(); 1533 if (dataset.m_state != CmsResource.STATE_NEW) { 1534 dataset.m_state = CmsResource.STATE_CHANGED; 1535 } 1536 CmsUUID groupId = CmsUUID.getNullUUID(); 1538 try { 1539 groupId = cms.readGroup(backup.m_groupId).getId(); 1540 } catch (CmsException exc) { 1541 groupId = dataset.m_groupId; 1542 } 1543 dataset.m_groupId = groupId; 1544 CmsUUID userId = CmsUUID.getNullUUID(); 1546 try { 1547 userId = cms.readUser(backup.m_userId).getId(); 1548 } catch (CmsException exc) { 1549 userId = dataset.m_userId; 1550 } 1551 dataset.m_userId = userId; 1552 this.write(cms, content, dataset); 1554 try { 1556 deleteAllMedia(dataset.m_masterId); 1557 } catch (SQLException exc) { 1558 throw new CmsLegacyException(CmsLegacyException.C_SQL_ERROR, exc); 1559 } 1560 try { 1562 conn = m_sqlManager.getConnection(); 1563 stmt = m_sqlManager.getPreparedStatement(conn, "read_media_backup"); 1564 stmt.setString(1, dataset.m_masterId.toString()); 1565 stmt.setInt(2, versionId); 1566 res = stmt.executeQuery(); 1567 while (res.next()) { 1568 int i = 1; 1569 CmsMasterMedia media = new CmsMasterMedia(res.getInt(i++), new CmsUUID(res.getString(i++)), res.getInt(i++), res.getInt(i++), res.getInt(i++), res.getInt(i++), res.getString(i++), res.getInt(i++), res.getString(i++), res.getString(i++), res.getString(i++), res.getBytes(i++)); 1570 try { 1572 stmt2 = null; 1573 conn2 = null; 1574 conn2 = m_sqlManager.getConnection(); 1575 stmt2 = m_sqlManager.getPreparedStatement(conn2, "insert_media_offline"); 1576 sqlFillValues(stmt2, media); 1577 stmt2.executeUpdate(); 1578 } catch (SQLException ex) { 1579 throw new CmsLegacyException(CmsLegacyException.C_SQL_ERROR, ex); 1580 } finally { 1581 m_sqlManager.closeAll(null, conn2, stmt2, null); 1582 } 1583 } 1584 } catch (SQLException e) { 1585 throw new CmsLegacyException(CmsLegacyException.C_SQL_ERROR, e); 1586 } finally { 1587 m_sqlManager.closeAll(null, conn, stmt, res); 1588 } 1589 } 1590 1591 1605 public void publishResource(CmsObject cms, CmsUUID publishHistoryId, CmsMasterDataSet dataset, int subId, String contentDefinitionName, boolean enableHistory, int versionId, long publishingDate, Vector changedResources, Vector changedModuleData) throws CmsException { 1606 this.publishOneLine(cms, publishHistoryId, dataset, subId, contentDefinitionName, enableHistory, versionId, publishingDate, changedResources, changedModuleData); 1607 } 1608 1624 public void publishProject(CmsObject cms, CmsUUID publishHistoryId, boolean enableHistory, int projectId, int versionId, long publishingDate, int subId, String contentDefinitionName, Vector changedRessources, Vector changedModuleData) throws CmsException { 1625 1626 String statement_key = "read_all_for_publish"; 1627 1628 PreparedStatement stmt = null; 1629 ResultSet res = null; 1630 Connection conn = null; 1631 1632 try { 1633 conn = m_sqlManager.getConnection(); 1634 stmt = m_sqlManager.getPreparedStatement(conn, statement_key); 1635 stmt.setInt(1, subId); 1636 stmt.setInt(2, projectId); 1637 stmt.setInt(3, CmsResource.STATE_UNCHANGED); 1638 res = stmt.executeQuery(); 1641 while (res.next()) { 1642 CmsMasterDataSet dataset = new CmsMasterDataSet(); 1644 sqlFillValues(res, cms, dataset); 1646 publishOneLine(cms, publishHistoryId, dataset, subId, contentDefinitionName, enableHistory, versionId, publishingDate, changedRessources, changedModuleData); 1647 } 1648 } catch (SQLException exc) { 1649 throw new CmsLegacyException(CmsLegacyException.C_SQL_ERROR, exc); 1650 } finally { 1651 m_sqlManager.closeAll(null, conn, stmt, res); 1652 } 1653 } 1654 1655 1670 protected void publishOneLine(CmsObject cms, CmsUUID publishHistoryId, CmsMasterDataSet dataset, int subId, String contentDefinitionName, boolean enableHistory, int versionId, long publishingDate, Vector changedRessources, Vector changedModuleData) throws CmsException { 1671 int state = dataset.m_state; 1672 1673 try { 1674 Class.forName(contentDefinitionName).getMethod("beforePublish", new Class [] { CmsObject.class, Boolean .class, Integer .class, Integer .class, Long .class, Vector .class, Vector .class, CmsMasterDataSet.class }).invoke(null, new Object [] { cms, new Boolean (enableHistory), new Integer (subId), new Integer (versionId), new Long (publishingDate), changedRessources, changedModuleData, dataset }); 1675 } catch (Exception e) { 1676 CmsLog.getLog(this).warn("Error calling method beforePublish() in class " + contentDefinitionName, e); 1677 } 1678 1679 if (enableHistory) { 1681 long backupCreationDate = dataset.m_dateCreated; 1684 publishBackupData(cms, dataset, subId, versionId, publishingDate); 1685 dataset.m_dateCreated = backupCreationDate; 1687 } 1688 1689 publishDeleteData(dataset.m_masterId, subId, "online"); 1691 1692 if (state == CmsResource.STATE_DELETED) { 1693 publishDeleteData(dataset.m_masterId, subId, "offline"); 1696 } else { 1697 publishCopyData(dataset, subId); 1700 } 1701 1702 PreparedStatement stmt = null; 1704 Connection conn = null; 1705 try { 1706 conn = m_sqlManager.getConnection(); 1707 stmt = m_sqlManager.getPreparedStatement(conn, "update_state_offline"); 1708 stmt.setInt(1, CmsResource.STATE_UNCHANGED); 1709 stmt.setString(2, CmsUUID.getNullUUID().toString()); 1710 stmt.setString(3, dataset.m_masterId.toString()); 1711 stmt.setInt(4, subId); 1712 stmt.executeUpdate(); 1713 } catch (SQLException exc) { 1714 throw new CmsLegacyException(CmsLegacyException.C_SQL_ERROR, exc); 1715 } finally { 1716 m_sqlManager.closeAll(null, conn, stmt, null); 1717 } 1718 1719 writePublishHistory( 1721 cms.getRequestContext().currentProject(), 1722 publishHistoryId, 1723 versionId, 1724 contentDefinitionName, 1725 dataset.m_masterId, 1726 subId, 1727 state); 1728 1729 changedModuleData.add( 1731 new CmsPublishedResource( 1732 CmsUUID.getNullUUID(), 1733 dataset.m_masterId, 1734 CmsDbUtil.UNKNOWN_ID, 1735 contentDefinitionName, 1736 subId, 1737 false, 1738 state, 1739 1)); 1740 } 1741 1742 1749 protected void publishDeleteData(CmsUUID masterId, int subId, String table) throws CmsException { 1750 PreparedStatement stmt = null; 1751 Connection conn = null; 1752 String deleteChannel = "delete_all_channel_" + table; 1753 try { 1755 conn = m_sqlManager.getConnection(); 1756 stmt = m_sqlManager.getPreparedStatement(conn, deleteChannel); 1757 stmt.setString(1, masterId.toString()); 1758 stmt.executeUpdate(); 1759 } catch (SQLException exc) { 1760 throw new CmsLegacyException(CmsLegacyException.C_SQL_ERROR, exc); 1761 } finally { 1762 m_sqlManager.closeAll(null, conn, stmt, null); 1763 } 1764 try { 1766 conn = null; 1767 stmt = null; 1768 String deleteMedia = "delete_all_media_" + table; 1769 conn = m_sqlManager.getConnection(); 1770 stmt = m_sqlManager.getPreparedStatement(conn, deleteMedia); 1771 stmt.setString(1, masterId.toString()); 1772 stmt.executeUpdate(); 1773 } catch (SQLException exc) { 1774 throw new CmsLegacyException(CmsLegacyException.C_SQL_ERROR, exc); 1775 } finally { 1776 m_sqlManager.closeAll(null, conn, stmt, null); 1777 } 1778 try { 1780 stmt = null; 1781 conn = null; 1782 String delete = "delete_" + table; 1783 conn = m_sqlManager.getConnection(); 1784 stmt = m_sqlManager.getPreparedStatement(conn, delete); 1785 stmt.setString(1, masterId.toString()); 1786 stmt.setInt(2, subId); 1787 stmt.executeUpdate(); 1788 } catch (SQLException exc) { 1789 throw new CmsLegacyException(CmsLegacyException.C_SQL_ERROR, exc); 1790 } finally { 1791 m_sqlManager.closeAll(null, conn, stmt, null); 1792 } 1793 } 1794 1795 1802 protected void publishCopyData(CmsMasterDataSet dataset, int subId) throws CmsException { 1803 PreparedStatement stmt = null; 1804 PreparedStatement stmt2 = null; 1805 ResultSet res = null; 1806 Connection conn = null; 1807 Connection conn2 = null; 1808 CmsUUID masterId = dataset.m_masterId; 1809 1810 try { 1812 stmt = null; 1813 conn = m_sqlManager.getConnection(); 1814 stmt = m_sqlManager.getPreparedStatement(conn, "insert_online"); 1815 dataset.m_projectId = CmsProject.ONLINE_PROJECT_ID; 1817 dataset.m_lockedInProject = CmsProject.ONLINE_PROJECT_ID; 1818 dataset.m_state = CmsResource.STATE_UNCHANGED; 1819 dataset.m_lockedBy = CmsUUID.getNullUUID(); 1820 sqlFillValues(stmt, subId, dataset); 1821 stmt.executeUpdate(); 1822 } catch (SQLException exc) { 1823 throw new CmsLegacyException(CmsLegacyException.C_SQL_ERROR, exc); 1824 } finally { 1825 m_sqlManager.closeAll(null, conn, stmt, null); 1826 } 1827 try { 1829 stmt = null; 1831 res = null; 1832 conn = null; 1833 conn = m_sqlManager.getConnection(); 1834 stmt = m_sqlManager.getPreparedStatement(conn, "read_media_offline"); 1835 stmt.setString(1, masterId.toString()); 1836 res = stmt.executeQuery(); 1837 while (res.next()) { 1838 int i = 1; 1840 CmsMasterMedia mediaset = new CmsMasterMedia(res.getInt(i++), new CmsUUID(res.getString(i++)), res.getInt(i++), res.getInt(i++), res.getInt(i++), res.getInt(i++), res.getString(i++), res.getInt(i++), res.getString(i++), res.getString(i++), res.getString(i++), res.getBytes(i++)); 1841 try { 1843 stmt2 = null; 1844 conn2 = null; 1845 conn2 = m_sqlManager.getConnection(); 1846 stmt2 = m_sqlManager.getPreparedStatement(conn2, "insert_media_online"); 1847 sqlFillValues(stmt2, mediaset); 1848 stmt2.executeUpdate(); 1849 } catch (SQLException ex) { 1850 throw ex; 1851 } finally { 1852 m_sqlManager.closeAll(null, conn2, stmt2, null); 1853 } 1854 } 1855 } catch (SQLException exc) { 1856 throw new CmsLegacyException(CmsLegacyException.C_SQL_ERROR, exc); 1857 } finally { 1858 m_sqlManager.closeAll(null, conn, stmt, res); 1859 } 1860 1861 try { 1863 stmt = null; 1864 res = null; 1865 conn = null; 1866 conn = m_sqlManager.getConnection(); 1867 stmt = m_sqlManager.getPreparedStatement(conn, "read_channel_offline"); 1869 stmt.setString(1, masterId.toString()); 1870 res = stmt.executeQuery(); 1871 while (res.next()) { 1872 try { 1874 stmt2 = null; 1875 conn2 = null; 1876 conn2 = m_sqlManager.getConnection(); 1877 stmt2 = m_sqlManager.getPreparedStatement(conn2, "insert_channel_online"); 1878 stmt2.setString(1, masterId.toString()); 1879 stmt2.setString(2, res.getString(1)); 1880 stmt2.setString(3, res.getString(2)); 1881 stmt2.executeUpdate(); 1882 } catch (SQLException ex) { 1883 throw ex; 1884 } finally { 1885 m_sqlManager.closeAll(null, conn2, stmt2, null); 1886 } 1887 } 1888 } catch (SQLException exc) { 1889 throw new CmsLegacyException(CmsLegacyException.C_SQL_ERROR, exc); 1890 } finally { 1891 m_sqlManager.closeAll(null, conn, stmt, res); 1892 } 1893 } 1894 1895 1904 protected void publishBackupData(CmsObject cms, CmsMasterDataSet dataset, int subId, int versionId, long publishDate) throws CmsException { 1905 PreparedStatement stmt = null; 1906 PreparedStatement stmt2 = null; 1907 ResultSet res = null; 1908 Connection conn = null; 1909 Connection conn2 = null; 1910 CmsUUID masterId = dataset.m_masterId; 1911 1912 try { 1914 stmt = null; 1915 conn = null; 1916 conn = m_sqlManager.getConnection(); 1917 stmt = m_sqlManager.getPreparedStatement(conn, "insert_backup"); 1918 dataset.m_lockedBy = CmsUUID.getNullUUID(); 1920 dataset.m_dateCreated = publishDate; 1921 String ownerName = ""; 1923 try { 1924 CmsUser owner = cms.readUser(dataset.m_userId); 1925 ownerName = owner.getName() + " " + owner.getFirstname() + " " + owner.getLastname(); 1926 } catch (CmsException ex) { 1927 ownerName = ""; 1928 } 1929 String groupName = ""; 1931 try { 1932 CmsGroup group = cms.readGroup(dataset.m_groupId); 1933 groupName = group.getName(); 1934 } catch (CmsException ex) { 1935 groupName = ""; 1936 } 1937 String userName = ""; 1939 try { 1940 CmsUser user = cms.readUser(dataset.m_lastModifiedBy); 1941 userName = user.getName() + " " + user.getFirstname() + " " + user.getLastname(); 1942 } catch (CmsException ex) { 1943 userName = ""; 1944 } 1945 int lastId = sqlFillValues(stmt, subId, dataset); 1946 stmt.setInt(lastId++, versionId); 1948 stmt.setString(lastId++, ownerName); 1949 stmt.setString(lastId++, groupName); 1950 stmt.setString(lastId++, userName); 1951 stmt.executeUpdate(); 1952 } catch (SQLException exc) { 1953 throw new CmsLegacyException(CmsLegacyException.C_SQL_ERROR, exc); 1954 } finally { 1955 m_sqlManager.closeAll(null, conn, stmt, null); 1956 } 1957 try { 1959 stmt = null; 1961 res = null; 1962 conn = null; 1963 conn = m_sqlManager.getConnection(); 1964 stmt = m_sqlManager.getPreparedStatement(conn, "read_media_offline"); 1965 stmt.setString(1, masterId.toString()); 1966 res = stmt.executeQuery(); 1967 while (res.next()) { 1968 int i = 1; 1970 CmsMasterMedia mediaset = new CmsMasterMedia(res.getInt(i++), new CmsUUID(res.getString(i++)), res.getInt(i++), res.getInt(i++), res.getInt(i++), res.getInt(i++), res.getString(i++), res.getInt(i++), res.getString(i++), res.getString(i++), res.getString(i++), res.getBytes(i++)); 1971 try { 1973 conn2 = null; 1974 stmt2 = null; 1975 conn2 = m_sqlManager.getConnection(); 1976 stmt2 = m_sqlManager.getPreparedStatement(conn2, "insert_media_backup"); 1977 int lastId = sqlFillValues(stmt2, mediaset); 1978 stmt2.setInt(lastId, versionId); 1979 stmt2.executeUpdate(); 1980 } catch (SQLException ex) { 1981 throw ex; 1982 } finally { 1983 m_sqlManager.closeAll(null, conn2, stmt2, null); 1984 } 1985 } 1986 } catch (SQLException exc) { 1987 throw new CmsLegacyException(CmsLegacyException.C_SQL_ERROR, exc); 1988 } finally { 1989 m_sqlManager.closeAll(null, conn, stmt, res); 1990 } 1991 } 1992 1993 2006 public void writePublishHistory(CmsProject project, CmsUUID publishId, int tagId, String contentDefinitionName, CmsUUID masterId, int subId, int state) throws CmsException { 2007 2008 PreparedStatement stmt = null; 2009 ResultSet res = null; 2010 Connection conn = null; 2011 2012 try { 2013 conn = m_sqlManager.getConnection(project.getId()); 2014 stmt = m_sqlManager.getPreparedStatement(conn, "C_RESOURCES_WRITE_PUBLISH_HISTORY"); 2015 stmt.setInt(1, tagId); 2016 stmt.setString(2, CmsUUID.getNullUUID().toString()); 2018 stmt.setString(3, masterId.toString()); 2020 stmt.setString(4, contentDefinitionName); 2022 stmt.setInt(5, state); 2024 stmt.setInt(6, subId); 2026 stmt.setString(7, publishId.toString()); 2027 stmt.setInt(8, 0); 2028 stmt.executeUpdate(); 2029 } catch (SQLException e) { 2030 throw new CmsLegacyException(CmsLegacyException.C_SQL_ERROR, e); 2031 } finally { 2032 m_sqlManager.closeAll(null, conn, stmt, res); 2033 } 2034 } 2035 2036} | Popular Tags |