From: <mb...@re...> - 2004-12-16 15:59:45
|
Author: mbooth Date: 2004-12-16 16:51:05 +0100 (Thu, 16 Dec 2004) New Revision: 156 Modified: ccm-core/trunk/pdl/com/arsdigita/messaging/Thread.pdl ccm-core/trunk/src/com/arsdigita/messaging/MessageThread.java ccm-core/trunk/src/com/arsdigita/messaging/ThreadedMessage.java Log: Update the number of replies in a thread when deleting messages Modified: ccm-core/trunk/pdl/com/arsdigita/messaging/Thread.pdl =================================================================== --- ccm-core/trunk/pdl/com/arsdigita/messaging/Thread.pdl 2004-12-16 15:50:11 UTC (rev 155) +++ ccm-core/trunk/pdl/com/arsdigita/messaging/Thread.pdl 2004-12-16 15:51:05 UTC (rev 156) @@ -53,3 +53,11 @@ where thread_id = :threadID } } + +data operation decrNumReplies { + do { + update message_threads + set num_replies = num_replies - 1 + where thread_id = :threadID + } +} Modified: ccm-core/trunk/src/com/arsdigita/messaging/MessageThread.java =================================================================== --- ccm-core/trunk/src/com/arsdigita/messaging/MessageThread.java 2004-12-16 15:50:11 UTC (rev 155) +++ ccm-core/trunk/src/com/arsdigita/messaging/MessageThread.java 2004-12-16 15:51:05 UTC (rev 156) @@ -121,6 +121,7 @@ /** * Increments the number of messages in the thread by one. + * The reason for the continued existance of this is thread safety */ private void incrNumberOfReplies() { DataOperation op = SessionManager.getSession().retrieveDataOperation( @@ -130,6 +131,17 @@ } /** + * Decrements the number of messages in the thread by one. + * The reason for the continued existance of this is thread safety + */ + private void decrNumberOfReplies() { + DataOperation op = SessionManager.getSession().retrieveDataOperation( + "com.arsdigita.messaging.decrNumReplies"); + op.setParameter("threadID", getID()); + op.execute(); + } + + /** * Gets the Date of the most recent message added to this thread */ public Date getLatestUpdateDate() { @@ -183,7 +195,19 @@ incrNumberOfReplies(); } + /** + * updates the MessageThread as necessary for a Message being removed. + * This is only meant to be called by the ThreadedMessage class + * + * @pre msg.getThread().equals(this) + */ + void removeMessage(ThreadedMessage msg) { + Assert.assertTrue(msg.getThread().equals(this)); + decrNumberOfReplies(); + } + + /** * retrieves the MessageThread object which has the specified * ThreadedMessage as its root * Modified: ccm-core/trunk/src/com/arsdigita/messaging/ThreadedMessage.java =================================================================== --- ccm-core/trunk/src/com/arsdigita/messaging/ThreadedMessage.java 2004-12-16 15:50:11 UTC (rev 155) +++ ccm-core/trunk/src/com/arsdigita/messaging/ThreadedMessage.java 2004-12-16 15:51:05 UTC (rev 156) @@ -19,8 +19,9 @@ package com.arsdigita.messaging; // ACS Core classes -import com.arsdigita.domain.DataObjectNotFoundException; +import com.arsdigita.domain.DomainObjectFactory; import com.arsdigita.kernel.Party; +import com.arsdigita.persistence.DataCollection; import com.arsdigita.persistence.DataObject; import com.arsdigita.persistence.DataQuery; import com.arsdigita.persistence.Filter; @@ -171,9 +172,7 @@ * @param key the id of the message. */ - public ThreadedMessage(BigDecimal id) - throws DataObjectNotFoundException - { + public ThreadedMessage(BigDecimal id) { super(new OID(BASE_DATA_OBJECT_TYPE, id)); } @@ -184,9 +183,7 @@ * @param oid the OID of the message */ - public ThreadedMessage(OID oid) - throws DataObjectNotFoundException - { + public ThreadedMessage(OID oid) { super(oid); } @@ -341,16 +338,7 @@ if (rootID == null) { root = this; } else { - try { - root = new ThreadedMessage(rootID); - } catch (DataObjectNotFoundException e) { - throw new UncheckedWrapperException( - "Tried to retrieve the root message " + - "(believed to have ID #" + rootID + ") of message #" + - getID() + " and failed. This is indicative of " + - "something profoundly wrong with the state of " + - "the database.", e); - } + root = new ThreadedMessage(rootID); } m_thread = MessageThread.getFromRootMessage(root); } @@ -441,4 +429,24 @@ return child; } + + public void beforeDelete() { + if( s_log.isDebugEnabled() ) { + s_log.debug( "Deleting children of " + getOID() ); + } + + DataCollection children = SessionManager.getSession().retrieve + ( BASE_DATA_OBJECT_TYPE ); + children.addEqualsFilter( Message.INREPLYTO, getID() ); + + while( children.next() ) { + DomainObjectFactory.newInstance( children.getDataObject() ).delete(); + } + + getThread().removeMessage( this ); + + if( s_log.isDebugEnabled() ) { + s_log.debug( "Deleting " + getOID() ); + } + } } |