james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From e...@apache.org
Subject svn commit: r1418609 [1/2] - in /james: mailbox/trunk/api/src/main/java/org/apache/james/mailbox/ mailbox/trunk/hbase/src/main/java/org/apache/james/mailbox/hbase/mail/ mailbox/trunk/jcr/src/main/java/org/apache/james/mailbox/jcr/mail/ mailbox/trunk/jp...
Date Sat, 08 Dec 2012 06:46:08 GMT
Author: eric
Date: Sat Dec  8 06:46:05 2012
New Revision: 1418609

URL: http://svn.apache.org/viewvc?rev=1418609&view=rev
Log:
First code to support the upcoming MOVE extension, mailbox implementations are missing, patch contributed by Andrzej Rusin (IMAP-370)

Added:
    james/protocols/trunk/imap/src/main/java/org/apache/james/imap/decode/parser/MoveCommandParser.java
    james/protocols/trunk/imap/src/main/java/org/apache/james/imap/message/request/MoveRequest.java
    james/protocols/trunk/imap/src/main/java/org/apache/james/imap/processor/MoveProcessor.java
Modified:
    james/mailbox/trunk/api/src/main/java/org/apache/james/mailbox/MailboxManager.java
    james/mailbox/trunk/hbase/src/main/java/org/apache/james/mailbox/hbase/mail/HBaseMessageMapper.java
    james/mailbox/trunk/jcr/src/main/java/org/apache/james/mailbox/jcr/mail/JCRMessageMapper.java
    james/mailbox/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/mail/JPAMessageMapper.java
    james/mailbox/trunk/maildir/src/main/java/org/apache/james/mailbox/maildir/mail/MaildirMessageMapper.java
    james/mailbox/trunk/memory/src/main/java/org/apache/james/mailbox/inmemory/mail/InMemoryMessageMapper.java
    james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/StoreMailboxManager.java
    james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/StoreMessageManager.java
    james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/mail/MessageMapper.java
    james/mailbox/trunk/store/src/test/java/org/apache/james/mailbox/store/StoreMessageResultIteratorTest.java
    james/protocols/trunk/imap/src/main/java/org/apache/james/imap/api/ImapConstants.java
    james/protocols/trunk/imap/src/main/java/org/apache/james/imap/decode/parser/CopyCommandParser.java
    james/protocols/trunk/imap/src/main/java/org/apache/james/imap/decode/parser/ImapParserFactory.java
    james/protocols/trunk/imap/src/main/java/org/apache/james/imap/processor/CopyProcessor.java
    james/protocols/trunk/imap/src/main/java/org/apache/james/imap/processor/DefaultProcessorChain.java
    james/protocols/trunk/imap/src/test/java/org/apache/james/imap/processor/base/MailboxEventAnalyserTest.java

Modified: james/mailbox/trunk/api/src/main/java/org/apache/james/mailbox/MailboxManager.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/api/src/main/java/org/apache/james/mailbox/MailboxManager.java?rev=1418609&r1=1418608&r2=1418609&view=diff
==============================================================================
--- james/mailbox/trunk/api/src/main/java/org/apache/james/mailbox/MailboxManager.java (original)
+++ james/mailbox/trunk/api/src/main/java/org/apache/james/mailbox/MailboxManager.java Sat Dec  8 06:46:05 2012
@@ -145,6 +145,23 @@ public interface MailboxManager extends 
     List<MessageRange> copyMessages(MessageRange set, MailboxPath from, MailboxPath to, MailboxSession session) throws MailboxException;
 
     /**
+     * Move the given {@link MessageRange} from one Mailbox to the other. 
+     * 
+     * Be aware that the moved Messages MUST get the \RECENT flag set!
+     * 
+     * @param set
+     *            messages to move
+     * @param from
+     *            name of the source mailbox
+     * @param to
+     *            name of the destination mailbox
+     * @param session
+     *            <code>MailboxSession</code>, not null
+     * @return a list of MessageRange - uids assigned to moved messages
+     */
+    List<MessageRange> moveMessages(MessageRange set, MailboxPath from, MailboxPath to, MailboxSession session) throws MailboxException;
+
+    /**
      * Searches for mailboxes matching the given query.
      * 
      * @param expression

Modified: james/mailbox/trunk/hbase/src/main/java/org/apache/james/mailbox/hbase/mail/HBaseMessageMapper.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/hbase/src/main/java/org/apache/james/mailbox/hbase/mail/HBaseMessageMapper.java?rev=1418609&r1=1418608&r2=1418609&view=diff
==============================================================================
--- james/mailbox/trunk/hbase/src/main/java/org/apache/james/mailbox/hbase/mail/HBaseMessageMapper.java (original)
+++ james/mailbox/trunk/hbase/src/main/java/org/apache/james/mailbox/hbase/mail/HBaseMessageMapper.java Sat Dec  8 06:46:05 2012
@@ -555,6 +555,16 @@ public class HBaseMessageMapper extends 
 
     /*
      * (non-Javadoc)
+     * @see org.apache.james.mailbox.store.mail.MessageMapper#copy(org.apache.james.mailbox.store.mail.model.Mailbox, org.apache.james.mailbox.store.mail.model.Message)
+     */
+    @Override
+    public MessageMetaData move(Mailbox<UUID> mailbox, Message<UUID> original) throws MailboxException {
+    	//TODO implement if possible
+    	throw new UnsupportedOperationException();
+    }
+
+    /*
+     * (non-Javadoc)
      * @see org.apache.james.mailbox.store.mail.MessageMapper#getLastUid(org.apache.james.mailbox.store.mail.model.Mailbox)
      */
     @Override

Modified: james/mailbox/trunk/jcr/src/main/java/org/apache/james/mailbox/jcr/mail/JCRMessageMapper.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/jcr/src/main/java/org/apache/james/mailbox/jcr/mail/JCRMessageMapper.java?rev=1418609&r1=1418608&r2=1418609&view=diff
==============================================================================
--- james/mailbox/trunk/jcr/src/main/java/org/apache/james/mailbox/jcr/mail/JCRMessageMapper.java (original)
+++ james/mailbox/trunk/jcr/src/main/java/org/apache/james/mailbox/jcr/mail/JCRMessageMapper.java Sat Dec  8 06:46:05 2012
@@ -55,12 +55,12 @@ import org.apache.james.mailbox.store.ma
 import org.apache.james.mailbox.store.mail.model.Message;
 
 /**
- * JCR implementation of a {@link MessageMapper}. The implementation store each message as 
- * a seperate child node under the mailbox
- *
+ * JCR implementation of a {@link MessageMapper}. The implementation store each
+ * message as a seperate child node under the mailbox
+ * 
  */
 public class JCRMessageMapper extends AbstractMessageMapper<String> implements JCRImapConstants {
-    
+
     /**
      * Store the messages directly in the mailbox: .../mailbox/
      */
@@ -97,52 +97,65 @@ public class JCRMessageMapper extends Ab
     public final static int MESSAGE_SCALE_MINUTE = 5;
 
     private final int scaleType;
-    
+
     private final MailboxSessionJCRRepository repository;
 
-    
     /**
      * Construct a new {@link JCRMessageMapper} instance
      * 
-     * @param repository {@link MailboxSessionJCRRepository} to use
-     * @param mSession {@link MailboxSession} to which the mapper is bound
-     * @param uidProvider {@link UidProvider} to use
-     * @param modSeqProvider {@link ModSeqProvider} to use
-     * @param scaleType message scale type either {@link #MESSAGE_SCALE_DAY}, {@link #MESSAGE_SCALE_HOUR}, {@link #MESSAGE_SCALE_MINUTE},
-     *          {@link #MESSAGE_SCALE_MONTH}, {@link #MESSAGE_SCALE_NONE} or {@link #MESSAGE_SCALE_YEAR}
+     * @param repository
+     *            {@link MailboxSessionJCRRepository} to use
+     * @param mSession
+     *            {@link MailboxSession} to which the mapper is bound
+     * @param uidProvider
+     *            {@link UidProvider} to use
+     * @param modSeqProvider
+     *            {@link ModSeqProvider} to use
+     * @param scaleType
+     *            message scale type either {@link #MESSAGE_SCALE_DAY},
+     *            {@link #MESSAGE_SCALE_HOUR}, {@link #MESSAGE_SCALE_MINUTE},
+     *            {@link #MESSAGE_SCALE_MONTH}, {@link #MESSAGE_SCALE_NONE} or
+     *            {@link #MESSAGE_SCALE_YEAR}
      */
-    public JCRMessageMapper(final MailboxSessionJCRRepository repository, MailboxSession mSession, UidProvider<String> uidProvider, ModSeqProvider<String> modSeqProvider, int scaleType) {
+    public JCRMessageMapper(final MailboxSessionJCRRepository repository, MailboxSession mSession,
+            UidProvider<String> uidProvider, ModSeqProvider<String> modSeqProvider, int scaleType) {
         super(mSession, uidProvider, modSeqProvider);
-        this.repository = repository;        
+        this.repository = repository;
         this.scaleType = scaleType;
     }
-    
+
     /**
-     * Construct a new {@link JCRMessageMapper} instance using {@link #MESSAGE_SCALE_DAY} as default
+     * Construct a new {@link JCRMessageMapper} instance using
+     * {@link #MESSAGE_SCALE_DAY} as default
      * 
-     * @param repos {@link MailboxSessionJCRRepository} to use
-     * @param session {@link MailboxSession} to which the mapper is bound
-     * @param uidProvider {@link UidProvider} to use
-     * @param modSeqProvider {@link ModSeqProvider} to use
+     * @param repos
+     *            {@link MailboxSessionJCRRepository} to use
+     * @param session
+     *            {@link MailboxSession} to which the mapper is bound
+     * @param uidProvider
+     *            {@link UidProvider} to use
+     * @param modSeqProvider
+     *            {@link ModSeqProvider} to use
      */
-    public JCRMessageMapper(final MailboxSessionJCRRepository repos, MailboxSession session, UidProvider<String> uidProvider, ModSeqProvider<String> modSeqProvider) {
+    public JCRMessageMapper(final MailboxSessionJCRRepository repos, MailboxSession session,
+            UidProvider<String> uidProvider, ModSeqProvider<String> modSeqProvider) {
         this(repos, session, uidProvider, modSeqProvider, MESSAGE_SCALE_DAY);
     }
 
-    
     /**
      * Return the JCR Session
      * 
      * @return session
      */
-    protected Session getSession() throws RepositoryException{
+    protected Session getSession() throws RepositoryException {
         return repository.login(mailboxSession);
     }
 
     /**
-     * Begin is not supported by level 1 JCR implementations, however we refresh the session
+     * Begin is not supported by level 1 JCR implementations, however we refresh
+     * the session
      */
-    protected void begin() throws MailboxException {  
+    protected void begin() throws MailboxException {
         try {
             getSession().refresh(true);
         } catch (RepositoryException e) {
@@ -152,7 +165,8 @@ public class JCRMessageMapper extends Ab
     }
 
     /**
-     * Just call save on the underlying JCR Session, because level 1 JCR implementation does not offer Transactions
+     * Just call save on the underlying JCR Session, because level 1 JCR
+     * implementation does not offer Transactions
      */
     protected void commit() throws MailboxException {
         try {
@@ -165,7 +179,8 @@ public class JCRMessageMapper extends Ab
     }
 
     /**
-     * Rollback is not supported by level 1 JCR implementations, so just do nothing
+     * Rollback is not supported by level 1 JCR implementations, so just do
+     * nothing
      */
     protected void rollback() throws MailboxException {
         try {
@@ -177,81 +192,82 @@ public class JCRMessageMapper extends Ab
     }
 
     /**
-     * Return the path to the mailbox. This path is escaped to be able to use it in xpath queries
-     * 
-     * See http://wiki.apache.org/jackrabbit/EncodingAndEscaping
-     * 
-     * @param mailbox
-     * @return
-     * @throws ItemNotFoundException
-     * @throws RepositoryException
+     * Logout from open JCR Session
      */
-    private String getMailboxPath(Mailbox<String> mailbox) throws ItemNotFoundException, RepositoryException {
-        return ISO9075.encodePath(getSession().getNodeByIdentifier(mailbox.getMailboxId()).getPath());
+    public void endRequest() {
+        repository.logout(mailboxSession);
     }
+
     /*
      * (non-Javadoc)
      * 
      * @see
-     * org.apache.james.mailbox.store.mail.MessageMapper#countMessagesInMailbox()
+     * org.apache.james.mailbox.store.mail.MessageMapper#countMessagesInMailbox
+     * ()
      */
     public long countMessagesInMailbox(Mailbox<String> mailbox) throws MailboxException {
         try {
-            // we use order by because without it count will always be 0 in jackrabbit
-            String queryString = "/jcr:root" + getMailboxPath(mailbox) + "//element(*,jamesMailbox:message) order by @" + JCRMessage.UID_PROPERTY;
+            // we use order by because without it count will always be 0 in
+            // jackrabbit
+            String queryString = "/jcr:root" + getMailboxPath(mailbox) + "//element(*,jamesMailbox:message) order by @"
+                    + JCRMessage.UID_PROPERTY;
             QueryManager manager = getSession().getWorkspace().getQueryManager();
             QueryResult result = manager.createQuery(queryString, Query.XPATH).execute();
             NodeIterator nodes = result.getNodes();
             long count = nodes.getSize();
             if (count == -1) {
                 count = 0;
-                while(nodes.hasNext()) {
+                while (nodes.hasNext()) {
                     nodes.nextNode();
                     count++;
                 }
-            } 
+            }
             return count;
         } catch (RepositoryException e) {
             throw new MailboxException("Unable to count messages in mailbox " + mailbox, e);
         }
-       
+
     }
 
     /*
      * (non-Javadoc)
      * 
-     * @see
-     * org.apache.james.mailbox.store.mail.MessageMapper#countUnseenMessagesInMailbox
-     * ()
+     * @see org.apache.james.mailbox.store.mail.MessageMapper#
+     * countUnseenMessagesInMailbox ()
      */
     public long countUnseenMessagesInMailbox(Mailbox<String> mailbox) throws MailboxException {
-        
+
         try {
-            // we use order by because without it count will always be 0 in jackrabbit
-            String queryString = "/jcr:root" + getMailboxPath(mailbox) + "//element(*,jamesMailbox:message)[@" + JCRMessage.SEEN_PROPERTY +"='false'] order by @" + JCRMessage.UID_PROPERTY;
+            // we use order by because without it count will always be 0 in
+            // jackrabbit
+            String queryString = "/jcr:root" + getMailboxPath(mailbox) + "//element(*,jamesMailbox:message)[@"
+                    + JCRMessage.SEEN_PROPERTY + "='false'] order by @" + JCRMessage.UID_PROPERTY;
             QueryManager manager = getSession().getWorkspace().getQueryManager();
             QueryResult result = manager.createQuery(queryString, Query.XPATH).execute();
             NodeIterator nodes = result.getNodes();
             long count = nodes.getSize();
-            
+
             if (count == -1) {
                 count = 0;
-                while(nodes.hasNext()) {
+                while (nodes.hasNext()) {
                     nodes.nextNode();
-                    
+
                     count++;
                 }
-            } 
+            }
             return count;
         } catch (RepositoryException e) {
             throw new MailboxException("Unable to count unseen messages in mailbox " + mailbox, e);
         }
     }
 
-
     /*
      * (non-Javadoc)
-     * @see org.apache.james.mailbox.store.mail.MessageMapper#delete(org.apache.james.mailbox.store.mail.model.Mailbox, org.apache.james.mailbox.store.mail.model.Message)
+     * 
+     * @see
+     * org.apache.james.mailbox.store.mail.MessageMapper#delete(org.apache.james
+     * .mailbox.store.mail.model.Mailbox,
+     * org.apache.james.mailbox.store.mail.model.Message)
      */
     public void delete(Mailbox<String> mailbox, Message<String> message) throws MailboxException {
         JCRMessage membership = (JCRMessage) message;
@@ -265,13 +281,17 @@ public class JCRMessageMapper extends Ab
         }
     }
 
-
-
     /*
      * (non-Javadoc)
-     * @see org.apache.james.mailbox.store.mail.MessageMapper#findInMailbox(org.apache.james.mailbox.store.mail.model.Mailbox, org.apache.james.mailbox.MessageRange, org.apache.james.mailbox.store.mail.MessageMapper.FetchType, int)
+     * 
+     * @see
+     * org.apache.james.mailbox.store.mail.MessageMapper#findInMailbox(org.apache
+     * .james.mailbox.store.mail.model.Mailbox,
+     * org.apache.james.mailbox.MessageRange,
+     * org.apache.james.mailbox.store.mail.MessageMapper.FetchType, int)
      */
-    public Iterator<Message<String>> findInMailbox(Mailbox<String> mailbox, MessageRange set, FetchType fType, int max) throws MailboxException {
+    public Iterator<Message<String>> findInMailbox(Mailbox<String> mailbox, MessageRange set, FetchType fType, int max)
+            throws MailboxException {
         try {
             List<Message<String>> results;
             long from = set.getUidFrom();
@@ -292,165 +312,37 @@ public class JCRMessageMapper extends Ab
             case RANGE:
                 results = findMessagesInMailboxBetweenUIDs(mailbox, from, to, max);
                 break;
-                }
+            }
             return results.iterator();
         } catch (RepositoryException e) {
             throw new MailboxException("Unable to search MessageRange " + set + " in mailbox " + mailbox, e);
         }
     }
-   
-    private List<Message<String>> findMessagesInMailboxAfterUID(Mailbox<String> mailbox, long uid, int batchSize) throws RepositoryException {
-        List<Message<String>> list = new ArrayList<Message<String>>();
-        String queryString = "/jcr:root" + getMailboxPath(mailbox) + "//element(*,jamesMailbox:message)[@" + JCRMessage.UID_PROPERTY + ">=" + uid + "] order by @" + JCRMessage.UID_PROPERTY;
-
-        QueryManager manager = getSession().getWorkspace().getQueryManager();
-        Query query = manager.createQuery(queryString, Query.XPATH);
-        if(batchSize > 0)
-        	query.setLimit(batchSize);
-        QueryResult result = query.execute();
-
-        NodeIterator iterator = result.getNodes();
-        while (iterator.hasNext()) {
-            list.add(new JCRMessage(iterator.nextNode(), mailboxSession.getLog()));
-        }
-        return list;
-    }
-
-    private List<Message<String>> findMessageInMailboxWithUID(Mailbox<String> mailbox, long uid) throws RepositoryException  {
-        List<Message<String>> list = new ArrayList<Message<String>>();
-        String queryString = "/jcr:root" + getMailboxPath(mailbox) + "//element(*,jamesMailbox:message)[@" + JCRMessage.UID_PROPERTY + "=" + uid + "]";
-
-        QueryManager manager = getSession().getWorkspace().getQueryManager();
-        Query query = manager.createQuery(queryString, Query.XPATH);
-        query.setLimit(1);
-        QueryResult result = query.execute();
-        NodeIterator iterator = result.getNodes();
-        while (iterator.hasNext()) {
-            list.add(new JCRMessage(iterator.nextNode(), mailboxSession.getLog()));
-        }
-        return list;
-    }
-
-    private List<Message<String>> findMessagesInMailboxBetweenUIDs(Mailbox<String> mailbox, long from, long to, int batchSize) throws RepositoryException {
-        List<Message<String>> list = new ArrayList<Message<String>>();
-        String queryString = "/jcr:root" + getMailboxPath(mailbox) + "//element(*,jamesMailbox:message)[@" + JCRMessage.UID_PROPERTY + ">=" + from + " and @" + JCRMessage.UID_PROPERTY + "<=" + to + "] order by @" + JCRMessage.UID_PROPERTY;
-        
-        QueryManager manager = getSession().getWorkspace().getQueryManager();
-        Query query = manager.createQuery(queryString, Query.XPATH);
-        if(batchSize > 0)
-        	query.setLimit(batchSize);
-        QueryResult result = query.execute();
-
-        NodeIterator iterator = result.getNodes();
-        while (iterator.hasNext()) {
-            list.add(new JCRMessage(iterator.nextNode(), mailboxSession.getLog()));
-        }
-        return list;
-    }
-    
-    private List<Message<String>> findMessagesInMailbox(Mailbox<String> mailbox, int batchSize) throws RepositoryException {        
-        List<Message<String>> list = new ArrayList<Message<String>>();
-        
-        String queryString = "/jcr:root" + getMailboxPath(mailbox) + "//element(*,jamesMailbox:message) order by @" + JCRMessage.UID_PROPERTY;
-        QueryManager manager = getSession().getWorkspace().getQueryManager();
-        Query query = manager.createQuery(queryString, Query.XPATH);
-        if(batchSize > 0)
-        	query.setLimit(batchSize);
-        QueryResult result = query.execute();
-
-        NodeIterator iterator = result.getNodes();
-        while (iterator.hasNext()) {
-            list.add(new JCRMessage(iterator.nextNode(), mailboxSession.getLog()));
-        }
-        return list;
-    }
-
-    
-    
-    private List<Message<String>> findDeletedMessagesInMailboxAfterUID(Mailbox<String> mailbox, long uid) throws RepositoryException {
-        List<Message<String>> list = new ArrayList<Message<String>>();
-        String queryString = "/jcr:root" + getMailboxPath(mailbox) + "//element(*,jamesMailbox:message)[@" + JCRMessage.UID_PROPERTY + ">=" + uid + " and @" + JCRMessage.DELETED_PROPERTY+ "='true'] order by @" + JCRMessage.UID_PROPERTY;
- 
-        QueryManager manager = getSession().getWorkspace().getQueryManager();
-        QueryResult result = manager.createQuery(queryString, Query.XPATH).execute();
-
-        NodeIterator iterator = result.getNodes();
-        while (iterator.hasNext()) {
-            list.add(new JCRMessage(iterator.nextNode(), mailboxSession.getLog()));
-        }
-        return list;
-    }
-
-    private List<Message<String>> findDeletedMessageInMailboxWithUID(Mailbox<String> mailbox, long uid) throws RepositoryException  {
-        List<Message<String>> list = new ArrayList<Message<String>>();
-        String queryString = "/jcr:root" + getMailboxPath(mailbox) + "//element(*,jamesMailbox:message)[@" + JCRMessage.UID_PROPERTY + "=" + uid + " and @" + JCRMessage.DELETED_PROPERTY+ "='true']";
-        QueryManager manager = getSession().getWorkspace().getQueryManager();
-        Query query = manager.createQuery(queryString, Query.XPATH);
-        query.setLimit(1);
-        QueryResult result = query.execute();
-
-        NodeIterator iterator = result.getNodes();
-        while (iterator.hasNext()) {
-            JCRMessage member = new JCRMessage(iterator.nextNode(), mailboxSession.getLog());
-            list.add(member);
-        }
-        return list;
-    }
-
-    private List<Message<String>> findDeletedMessagesInMailboxBetweenUIDs(Mailbox<String> mailbox, long from, long to) throws RepositoryException {
-        List<Message<String>> list = new ArrayList<Message<String>>();
-        String queryString = "/jcr:root" + getMailboxPath(mailbox) + "//element(*,jamesMailbox:message)[@" + JCRMessage.UID_PROPERTY + ">=" + from + " and @" + JCRMessage.UID_PROPERTY + "<=" + to + " and @" + JCRMessage.DELETED_PROPERTY+ "='true'] order by @" + JCRMessage.UID_PROPERTY;
-       
-        QueryManager manager = getSession().getWorkspace().getQueryManager();
-        QueryResult result = manager.createQuery(queryString, Query.XPATH).execute();
-
-        NodeIterator iterator = result.getNodes();
-        while (iterator.hasNext()) {
-            list.add(new JCRMessage(iterator.nextNode(), mailboxSession.getLog()));
-        }
-        return list;
-    }
-    
-    private List<Message<String>> findDeletedMessagesInMailbox(Mailbox<String> mailbox) throws RepositoryException {
-        
-        List<Message<String>> list = new ArrayList<Message<String>>();
-        String queryString = "/jcr:root" + getMailboxPath(mailbox) + "//element(*,jamesMailbox:message)[@" + JCRMessage.DELETED_PROPERTY+ "='true'] order by @" + JCRMessage.UID_PROPERTY;
-        
-        QueryManager manager = getSession().getWorkspace().getQueryManager();
-        QueryResult result = manager.createQuery(queryString, Query.XPATH).execute();
-
-        NodeIterator iterator = result.getNodes();
-        while (iterator.hasNext()) {
-            JCRMessage member = new JCRMessage(iterator.nextNode(), mailboxSession.getLog());
-            list.add(member);
-        }
-        return list;
-    }
-
 
     /*
      * 
-     * TODO: Maybe we should better use an ItemVisitor and just traverse through the child nodes. This could be a way faster
+     * TODO: Maybe we should better use an ItemVisitor and just traverse through
+     * the child nodes. This could be a way faster
      * 
      * (non-Javadoc)
      * 
-     * @see
-     * org.apache.james.mailbox.store.mail.MessageMapper#findRecentMessageUidsInMailbox
-     * ()
+     * @see org.apache.james.mailbox.store.mail.MessageMapper#
+     * findRecentMessageUidsInMailbox ()
      */
     public List<Long> findRecentMessageUidsInMailbox(Mailbox<String> mailbox) throws MailboxException {
-        
+
         try {
- 
+
             List<Long> list = new ArrayList<Long>();
-            String queryString = "/jcr:root" + getMailboxPath(mailbox) + "//element(*,jamesMailbox:message)[@" + JCRMessage.RECENT_PROPERTY +"='true'] order by @" + JCRMessage.UID_PROPERTY;
-            
+            String queryString = "/jcr:root" + getMailboxPath(mailbox) + "//element(*,jamesMailbox:message)[@"
+                    + JCRMessage.RECENT_PROPERTY + "='true'] order by @" + JCRMessage.UID_PROPERTY;
+
             QueryManager manager = getSession().getWorkspace().getQueryManager();
             Query query = manager.createQuery(queryString, Query.XPATH);
             QueryResult result = query.execute();
-            
+
             NodeIterator iterator = result.getNodes();
-            while(iterator.hasNext()) {
+            while (iterator.hasNext()) {
                 list.add(new JCRMessage(iterator.nextNode(), mailboxSession.getLog()).getUid());
             }
             return list;
@@ -460,23 +352,26 @@ public class JCRMessageMapper extends Ab
         }
     }
 
-
     /*
      * (non-Javadoc)
-     * @see org.apache.james.mailbox.store.mail.MessageMapper#findFirstUnseenMessageUid(org.apache.james.mailbox.store.mail.model.Mailbox)
+     * 
+     * @see
+     * org.apache.james.mailbox.store.mail.MessageMapper#findFirstUnseenMessageUid
+     * (org.apache.james.mailbox.store.mail.model.Mailbox)
      */
     public Long findFirstUnseenMessageUid(Mailbox<String> mailbox) throws MailboxException {
         try {
-            String queryString = "/jcr:root" + getMailboxPath(mailbox) + "//element(*,jamesMailbox:message)[@" + JCRMessage.SEEN_PROPERTY +"='false'] order by @" + JCRMessage.UID_PROPERTY;
+            String queryString = "/jcr:root" + getMailboxPath(mailbox) + "//element(*,jamesMailbox:message)[@"
+                    + JCRMessage.SEEN_PROPERTY + "='false'] order by @" + JCRMessage.UID_PROPERTY;
 
             QueryManager manager = getSession().getWorkspace().getQueryManager();
-            
+
             Query query = manager.createQuery(queryString, Query.XPATH);
             query.setLimit(1);
             QueryResult result = query.execute();
 
             NodeIterator iterator = result.getNodes();
-            if(iterator.hasNext()) {
+            if (iterator.hasNext()) {
                 return new JCRMessage(iterator.nextNode(), mailboxSession.getLog()).getUid();
             } else {
                 return null;
@@ -486,71 +381,94 @@ public class JCRMessageMapper extends Ab
         }
     }
 
-    /**
-     * Convert the given int value to a String. If the int value is smaller then
-     * 9 it will prefix the String with 0.
-     * 
-     * @param value
-     * @return stringValue
-     */
-    private String convertIntToString(int value) {
-        if (value <= 9) {
-            return "0" + String.valueOf(value);
-        } else {
-            return String.valueOf(value);
+    @Override
+    public Map<Long, MessageMetaData> expungeMarkedForDeletionInMailbox(Mailbox<String> mailbox, MessageRange set)
+            throws MailboxException {
+        try {
+            final List<Message<String>> results;
+            final long from = set.getUidFrom();
+            final long to = set.getUidTo();
+            final Type type = set.getType();
+            switch (type) {
+            default:
+            case ALL:
+                results = findDeletedMessagesInMailbox(mailbox);
+                break;
+            case FROM:
+                results = findDeletedMessagesInMailboxAfterUID(mailbox, from);
+                break;
+            case ONE:
+                results = findDeletedMessageInMailboxWithUID(mailbox, from);
+                break;
+            case RANGE:
+                results = findDeletedMessagesInMailboxBetweenUIDs(mailbox, from, to);
+                break;
+            }
+            Map<Long, MessageMetaData> uids = new HashMap<Long, MessageMetaData>();
+            for (int i = 0; i < results.size(); i++) {
+                Message<String> m = results.get(i);
+                long uid = m.getUid();
+                uids.put(uid, new SimpleMessageMetaData(m));
+                delete(mailbox, m);
+            }
+            return uids;
+        } catch (RepositoryException e) {
+            throw new MailboxException("Unable to search MessageRange " + set + " in mailbox " + mailbox, e);
         }
     }
 
-
-
-   
-
-
-
     /**
-     * Logout from open JCR Session
+     * (non-Javadoc)
+     * 
+     * @see org.apache.james.mailbox.store.mail.MessageMapper#move(org.apache.james.mailbox.store.mail.model.Mailbox,
+     *      org.apache.james.mailbox.store.mail.model.Message)
      */
-    public void endRequest() {
-       repository.logout(mailboxSession);
+    @Override
+    public MessageMetaData move(Mailbox<String> mailbox, Message<String> original) throws MailboxException {
+        throw new UnsupportedOperationException("Not implemented - see https://issues.apache.org/jira/browse/IMAP-370");
     }
-    
-    
+
     @Override
-    protected MessageMetaData copy(Mailbox<String> mailbox, long uid, long modSeq, Message<String> original) throws MailboxException {
+    protected MessageMetaData copy(Mailbox<String> mailbox, long uid, long modSeq, Message<String> original)
+            throws MailboxException {
         try {
-            String newMessagePath = getSession().getNodeByIdentifier(mailbox.getMailboxId()).getPath() + NODE_DELIMITER + String.valueOf(uid);
-            getSession().getWorkspace().copy(((JCRMessage)original).getNode().getPath(), getSession().getNodeByIdentifier(mailbox.getMailboxId()).getPath() + NODE_DELIMITER + String.valueOf(uid));
+            String newMessagePath = getSession().getNodeByIdentifier(mailbox.getMailboxId()).getPath() + NODE_DELIMITER
+                    + String.valueOf(uid);
+            getSession().getWorkspace().copy(
+                    ((JCRMessage) original).getNode().getPath(),
+                    getSession().getNodeByIdentifier(mailbox.getMailboxId()).getPath() + NODE_DELIMITER
+                            + String.valueOf(uid));
             Node node = getSession().getNode(newMessagePath);
             node.setProperty(JCRMessage.MAILBOX_UUID_PROPERTY, mailbox.getMailboxId());
             node.setProperty(JCRMessage.UID_PROPERTY, uid);
             node.setProperty(JCRMessage.MODSEQ_PROPERTY, modSeq);
-            // A copy of a message is recent 
+            // A copy of a message is recent
             // See MAILBOX-85
             node.setProperty(JCRMessage.RECENT_PROPERTY, true);
             return new SimpleMessageMetaData(new JCRMessage(node, mailboxSession.getLog()));
         } catch (RepositoryException e) {
-            throw new MailboxException("Unable to copy message " +original + " in mailbox " + mailbox, e);
-        }        
+            throw new MailboxException("Unable to copy message " + original + " in mailbox " + mailbox, e);
+        }
     }
 
     @Override
     protected MessageMetaData save(Mailbox<String> mailbox, Message<String> message) throws MailboxException {
         final JCRMessage membership = (JCRMessage) message;
         try {
-
+    
             Node messageNode = null;
-
+    
             if (membership.isPersistent()) {
                 messageNode = getSession().getNodeByIdentifier(membership.getId());
             }
-
+    
             if (messageNode == null) {
-               
+    
                 Date date = message.getInternalDate();
                 if (date == null) {
                     date = new Date();
                 }
-
+    
                 // extracte the date from the message to create node structure
                 // later
                 Calendar cal = Calendar.getInstance();
@@ -560,26 +478,26 @@ public class JCRMessageMapper extends Ab
                 final String day = convertIntToString(cal.get(Calendar.DAY_OF_MONTH));
                 final String hour = convertIntToString(cal.get(Calendar.HOUR_OF_DAY));
                 final String min = convertIntToString(cal.get(Calendar.MINUTE));
-
+    
                 Node mailboxNode = getSession().getNodeByIdentifier(mailbox.getMailboxId());
                 Node node = mailboxNode;
-
+    
                 if (scaleType > MESSAGE_SCALE_NONE) {
                     // we lock the whole mailbox with all its childs while
                     // adding the folder structure for the date
-
+    
                     if (scaleType >= MESSAGE_SCALE_YEAR) {
                         node = JcrUtils.getOrAddFolder(node, year);
-
+    
                         if (scaleType >= MESSAGE_SCALE_MONTH) {
                             node = JcrUtils.getOrAddFolder(node, month);
-
+    
                             if (scaleType >= MESSAGE_SCALE_DAY) {
                                 node = JcrUtils.getOrAddFolder(node, day);
-
+    
                                 if (scaleType >= MESSAGE_SCALE_HOUR) {
                                     node = JcrUtils.getOrAddFolder(node, hour);
-
+    
                                     if (scaleType >= MESSAGE_SCALE_MINUTE) {
                                         node = JcrUtils.getOrAddFolder(node, min);
                                     }
@@ -587,18 +505,18 @@ public class JCRMessageMapper extends Ab
                             }
                         }
                     }
-
+    
                 }
-
+    
                 long uid = membership.getUid();
                 messageNode = mailboxNode.addNode(String.valueOf(uid), "nt:file");
                 messageNode.addMixin("jamesMailbox:message");
                 try {
                     membership.merge(messageNode);
-
+    
                 } catch (IOException e) {
                     throw new RepositoryException("Unable to merge message in to tree", e);
-                }                
+                }
             } else {
                 membership.merge(messageNode);
             }
@@ -607,41 +525,180 @@ public class JCRMessageMapper extends Ab
             throw new MailboxException("Unable to save message " + message + " in mailbox " + mailbox, e);
         } catch (IOException e) {
             throw new MailboxException("Unable to save message " + message + " in mailbox " + mailbox, e);
-        }        
+        }
     }
-    
-    @Override
-    public Map<Long, MessageMetaData> expungeMarkedForDeletionInMailbox(Mailbox<String> mailbox, MessageRange set) throws MailboxException {
-        try {
-            final List<Message<String>> results;
-            final long from = set.getUidFrom();
-            final long to = set.getUidTo();
-            final Type type = set.getType();
-            switch (type) {
-                default:
-                case ALL:
-                    results = findDeletedMessagesInMailbox(mailbox);
-                    break;
-                case FROM:
-                    results = findDeletedMessagesInMailboxAfterUID(mailbox, from);
-                    break;
-                case ONE:
-                    results = findDeletedMessageInMailboxWithUID(mailbox, from);
-                    break;
-                case RANGE:
-                    results = findDeletedMessagesInMailboxBetweenUIDs(mailbox, from, to);
-                    break;       
-            }
-            Map<Long, MessageMetaData> uids = new HashMap<Long, MessageMetaData>();
-            for (int i = 0; i < results.size(); i++) {
-                Message<String> m = results.get(i);
-                long uid = m.getUid();
-                uids.put(uid, new SimpleMessageMetaData(m));
-                delete(mailbox, m);
-            }
-            return uids;
-        } catch (RepositoryException e) {
-            throw new MailboxException("Unable to search MessageRange " + set + " in mailbox " + mailbox, e);
+
+    /**
+     * Return the path to the mailbox. This path is escaped to be able to use it
+     * in xpath queries
+     * 
+     * See http://wiki.apache.org/jackrabbit/EncodingAndEscaping
+     * 
+     * @param mailbox
+     * @return
+     * @throws ItemNotFoundException
+     * @throws RepositoryException
+     */
+    private String getMailboxPath(Mailbox<String> mailbox) throws ItemNotFoundException, RepositoryException {
+        return ISO9075.encodePath(getSession().getNodeByIdentifier(mailbox.getMailboxId()).getPath());
+    }
+
+    private List<Message<String>> findMessagesInMailboxAfterUID(Mailbox<String> mailbox, long uid, int batchSize)
+            throws RepositoryException {
+        List<Message<String>> list = new ArrayList<Message<String>>();
+        String queryString = "/jcr:root" + getMailboxPath(mailbox) + "//element(*,jamesMailbox:message)[@"
+                + JCRMessage.UID_PROPERTY + ">=" + uid + "] order by @" + JCRMessage.UID_PROPERTY;
+
+        QueryManager manager = getSession().getWorkspace().getQueryManager();
+        Query query = manager.createQuery(queryString, Query.XPATH);
+        if (batchSize > 0)
+            query.setLimit(batchSize);
+        QueryResult result = query.execute();
+
+        NodeIterator iterator = result.getNodes();
+        while (iterator.hasNext()) {
+            list.add(new JCRMessage(iterator.nextNode(), mailboxSession.getLog()));
+        }
+        return list;
+    }
+
+    private List<Message<String>> findMessageInMailboxWithUID(Mailbox<String> mailbox, long uid)
+            throws RepositoryException {
+        List<Message<String>> list = new ArrayList<Message<String>>();
+        String queryString = "/jcr:root" + getMailboxPath(mailbox) + "//element(*,jamesMailbox:message)[@"
+                + JCRMessage.UID_PROPERTY + "=" + uid + "]";
+
+        QueryManager manager = getSession().getWorkspace().getQueryManager();
+        Query query = manager.createQuery(queryString, Query.XPATH);
+        query.setLimit(1);
+        QueryResult result = query.execute();
+        NodeIterator iterator = result.getNodes();
+        while (iterator.hasNext()) {
+            list.add(new JCRMessage(iterator.nextNode(), mailboxSession.getLog()));
+        }
+        return list;
+    }
+
+    private List<Message<String>> findMessagesInMailboxBetweenUIDs(Mailbox<String> mailbox, long from, long to,
+            int batchSize) throws RepositoryException {
+        List<Message<String>> list = new ArrayList<Message<String>>();
+        String queryString = "/jcr:root" + getMailboxPath(mailbox) + "//element(*,jamesMailbox:message)[@"
+                + JCRMessage.UID_PROPERTY + ">=" + from + " and @" + JCRMessage.UID_PROPERTY + "<=" + to
+                + "] order by @" + JCRMessage.UID_PROPERTY;
+
+        QueryManager manager = getSession().getWorkspace().getQueryManager();
+        Query query = manager.createQuery(queryString, Query.XPATH);
+        if (batchSize > 0)
+            query.setLimit(batchSize);
+        QueryResult result = query.execute();
+
+        NodeIterator iterator = result.getNodes();
+        while (iterator.hasNext()) {
+            list.add(new JCRMessage(iterator.nextNode(), mailboxSession.getLog()));
+        }
+        return list;
+    }
+
+    private List<Message<String>> findMessagesInMailbox(Mailbox<String> mailbox, int batchSize)
+            throws RepositoryException {
+        List<Message<String>> list = new ArrayList<Message<String>>();
+
+        String queryString = "/jcr:root" + getMailboxPath(mailbox) + "//element(*,jamesMailbox:message) order by @"
+                + JCRMessage.UID_PROPERTY;
+        QueryManager manager = getSession().getWorkspace().getQueryManager();
+        Query query = manager.createQuery(queryString, Query.XPATH);
+        if (batchSize > 0)
+            query.setLimit(batchSize);
+        QueryResult result = query.execute();
+
+        NodeIterator iterator = result.getNodes();
+        while (iterator.hasNext()) {
+            list.add(new JCRMessage(iterator.nextNode(), mailboxSession.getLog()));
+        }
+        return list;
+    }
+
+    private List<Message<String>> findDeletedMessagesInMailboxAfterUID(Mailbox<String> mailbox, long uid)
+            throws RepositoryException {
+        List<Message<String>> list = new ArrayList<Message<String>>();
+        String queryString = "/jcr:root" + getMailboxPath(mailbox) + "//element(*,jamesMailbox:message)[@"
+                + JCRMessage.UID_PROPERTY + ">=" + uid + " and @" + JCRMessage.DELETED_PROPERTY + "='true'] order by @"
+                + JCRMessage.UID_PROPERTY;
+
+        QueryManager manager = getSession().getWorkspace().getQueryManager();
+        QueryResult result = manager.createQuery(queryString, Query.XPATH).execute();
+
+        NodeIterator iterator = result.getNodes();
+        while (iterator.hasNext()) {
+            list.add(new JCRMessage(iterator.nextNode(), mailboxSession.getLog()));
+        }
+        return list;
+    }
+
+    private List<Message<String>> findDeletedMessageInMailboxWithUID(Mailbox<String> mailbox, long uid)
+            throws RepositoryException {
+        List<Message<String>> list = new ArrayList<Message<String>>();
+        String queryString = "/jcr:root" + getMailboxPath(mailbox) + "//element(*,jamesMailbox:message)[@"
+                + JCRMessage.UID_PROPERTY + "=" + uid + " and @" + JCRMessage.DELETED_PROPERTY + "='true']";
+        QueryManager manager = getSession().getWorkspace().getQueryManager();
+        Query query = manager.createQuery(queryString, Query.XPATH);
+        query.setLimit(1);
+        QueryResult result = query.execute();
+
+        NodeIterator iterator = result.getNodes();
+        while (iterator.hasNext()) {
+            JCRMessage member = new JCRMessage(iterator.nextNode(), mailboxSession.getLog());
+            list.add(member);
+        }
+        return list;
+    }
+
+    private List<Message<String>> findDeletedMessagesInMailboxBetweenUIDs(Mailbox<String> mailbox, long from, long to)
+            throws RepositoryException {
+        List<Message<String>> list = new ArrayList<Message<String>>();
+        String queryString = "/jcr:root" + getMailboxPath(mailbox) + "//element(*,jamesMailbox:message)[@"
+                + JCRMessage.UID_PROPERTY + ">=" + from + " and @" + JCRMessage.UID_PROPERTY + "<=" + to + " and @"
+                + JCRMessage.DELETED_PROPERTY + "='true'] order by @" + JCRMessage.UID_PROPERTY;
+
+        QueryManager manager = getSession().getWorkspace().getQueryManager();
+        QueryResult result = manager.createQuery(queryString, Query.XPATH).execute();
+
+        NodeIterator iterator = result.getNodes();
+        while (iterator.hasNext()) {
+            list.add(new JCRMessage(iterator.nextNode(), mailboxSession.getLog()));
+        }
+        return list;
+    }
+
+    private List<Message<String>> findDeletedMessagesInMailbox(Mailbox<String> mailbox) throws RepositoryException {
+
+        List<Message<String>> list = new ArrayList<Message<String>>();
+        String queryString = "/jcr:root" + getMailboxPath(mailbox) + "//element(*,jamesMailbox:message)[@"
+                + JCRMessage.DELETED_PROPERTY + "='true'] order by @" + JCRMessage.UID_PROPERTY;
+
+        QueryManager manager = getSession().getWorkspace().getQueryManager();
+        QueryResult result = manager.createQuery(queryString, Query.XPATH).execute();
+
+        NodeIterator iterator = result.getNodes();
+        while (iterator.hasNext()) {
+            JCRMessage member = new JCRMessage(iterator.nextNode(), mailboxSession.getLog());
+            list.add(member);
+        }
+        return list;
+    }
+
+    /**
+     * Convert the given int value to a String. If the int value is smaller then
+     * 9 it will prefix the String with 0.
+     * 
+     * @param value
+     * @return stringValue
+     */
+    private String convertIntToString(int value) {
+        if (value <= 9) {
+            return "0" + String.valueOf(value);
+        } else {
+            return String.valueOf(value);
         }
     }
 

Modified: james/mailbox/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/mail/JPAMessageMapper.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/mail/JPAMessageMapper.java?rev=1418609&r1=1418608&r2=1418609&view=diff
==============================================================================
--- james/mailbox/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/mail/JPAMessageMapper.java (original)
+++ james/mailbox/trunk/jpa/src/main/java/org/apache/james/mailbox/jpa/mail/JPAMessageMapper.java Sat Dec  8 06:46:05 2012
@@ -54,15 +54,16 @@ import org.apache.openjpa.persistence.Ar
 public class JPAMessageMapper extends AbstractMessageMapper<Long> implements MessageMapper<Long> {
     protected EntityManagerFactory entityManagerFactory;
     protected EntityManager entityManager;
-    
-    public JPAMessageMapper(final MailboxSession session, final UidProvider<Long> uidProvider, ModSeqProvider<Long> modSeqProvider, final EntityManagerFactory entityManagerFactory) {
+
+    public JPAMessageMapper(final MailboxSession session, final UidProvider<Long> uidProvider,
+            ModSeqProvider<Long> modSeqProvider, final EntityManagerFactory entityManagerFactory) {
         super(session, uidProvider, modSeqProvider);
         this.entityManagerFactory = entityManagerFactory;
     }
 
-
     /**
-     * Return the currently used {@link EntityManager} or a new one if none exists.
+     * Return the currently used {@link EntityManager} or a new one if none
+     * exists.
      * 
      * @return entitymanger
      */
@@ -91,7 +92,7 @@ public class JPAMessageMapper extends Ab
         try {
             getEntityManager().getTransaction().commit();
         } catch (PersistenceException e) {
-            throw new MailboxException("Commit of transaction failed",e);
+            throw new MailboxException("Commit of transaction failed", e);
         }
     }
 
@@ -118,9 +119,12 @@ public class JPAMessageMapper extends Ab
     }
 
     /**
-     * @see org.apache.james.mailbox.store.mail.MessageMapper#findInMailbox(org.apache.james.mailbox.store.mail.model.Mailbox, org.apache.james.mailbox.model.MessageRange, org.apache.james.mailbox.store.mail.MessageMapper.FetchType, int)
+     * @see org.apache.james.mailbox.store.mail.MessageMapper#findInMailbox(org.apache.james.mailbox.store.mail.model.Mailbox,
+     *      org.apache.james.mailbox.model.MessageRange,
+     *      org.apache.james.mailbox.store.mail.MessageMapper.FetchType, int)
      */
-    public Iterator<Message<Long>> findInMailbox(Mailbox<Long> mailbox, MessageRange set, FetchType fType, int max) throws MailboxException {
+    public Iterator<Message<Long>> findInMailbox(Mailbox<Long> mailbox, MessageRange set, FetchType fType, int max)
+            throws MailboxException {
         try {
             List<Message<Long>> results;
             long from = set.getUidFrom();
@@ -150,112 +154,13 @@ public class JPAMessageMapper extends Ab
         }
     }
 
-    @SuppressWarnings("unchecked")
-    private List<Message<Long>> findMessagesInMailboxAfterUID(Mailbox<Long> mailbox, long uid, int batchSize) {
-        Query query = getEntityManager().createNamedQuery("findMessagesInMailboxAfterUID")
-        .setParameter("idParam", mailbox.getMailboxId())
-        .setParameter("uidParam", uid);
-        
-        if(batchSize > 0)
-        	query.setMaxResults(batchSize);
-        
-        return query.getResultList();
-    }
-
-    @SuppressWarnings("unchecked")
-    private List<Message<Long>> findMessagesInMailboxWithUID(Mailbox<Long> mailbox, long uid) {
-        return getEntityManager().createNamedQuery("findMessagesInMailboxWithUID")
-        .setParameter("idParam", mailbox.getMailboxId())
-        .setParameter("uidParam", uid).setMaxResults(1).getResultList();
-    }
-
-    @SuppressWarnings("unchecked")
-    private List<Message<Long>> findMessagesInMailboxBetweenUIDs(Mailbox<Long> mailbox, long from, long to, int batchSize) {
-        Query query = getEntityManager().createNamedQuery("findMessagesInMailboxBetweenUIDs").setParameter("idParam", mailbox.getMailboxId()).setParameter("fromParam", from).setParameter("toParam", to);
-
-        if (batchSize > 0)
-            query.setMaxResults(batchSize);
-
-        return query.getResultList();
-    }
-
-    @SuppressWarnings("unchecked")
-    private List<Message<Long>> findMessagesInMailbox(Mailbox<Long> mailbox, int batchSize) {
-         Query query = getEntityManager().createNamedQuery("findMessagesInMailbox").setParameter("idParam", mailbox.getMailboxId());
-         if(batchSize > 0)
-        	 query.setMaxResults(batchSize);
-         return query.getResultList();
-    }
-
-
-    
-    private Map<Long, MessageMetaData> createMetaData(List<Message<Long>> uids) {
-        final Map<Long, MessageMetaData> data = new HashMap<Long, MessageMetaData>();
-        for (int i = 0; i < uids.size(); i++) {
-            Message<Long> m = uids.get(i);
-            data.put(m.getUid(),  new SimpleMessageMetaData(m));
-        }
-        return data;
-    }
-
-
-    private int deleteDeletedMessagesInMailbox(Mailbox<Long> mailbox) {
-        return getEntityManager().createNamedQuery("deleteDeletedMessagesInMailbox").setParameter("idParam", mailbox.getMailboxId()).executeUpdate();
-    }
-
-    private int deleteDeletedMessagesInMailboxAfterUID(Mailbox<Long> mailbox, long uid) {
-        return getEntityManager().createNamedQuery("deleteDeletedMessagesInMailboxAfterUID")
-        .setParameter("idParam", mailbox.getMailboxId())
-        .setParameter("uidParam", uid).executeUpdate();
-    }
-
-    private int deleteDeletedMessagesInMailboxWithUID(Mailbox<Long> mailbox, long uid) {
-        return getEntityManager().createNamedQuery("deleteDeletedMessagesInMailboxWithUID")
-        .setParameter("idParam", mailbox.getMailboxId())
-        .setParameter("uidParam", uid).executeUpdate();
-    }
-
-    private int deleteDeletedMessagesInMailboxBetweenUIDs(Mailbox<Long> mailbox, long from, long to) {
-        return getEntityManager().createNamedQuery("deleteDeletedMessagesInMailboxBetweenUIDs")
-        .setParameter("idParam", mailbox.getMailboxId())
-        .setParameter("fromParam", from)
-        .setParameter("toParam", to).executeUpdate();
-    }
-
-    
-    @SuppressWarnings("unchecked")
-    private List<Message<Long>> findDeletedMessagesInMailbox(Mailbox<Long> mailbox) {
-        return getEntityManager().createNamedQuery("findDeletedMessagesInMailbox").setParameter("idParam", mailbox.getMailboxId()).getResultList();
-    }
-
-    @SuppressWarnings("unchecked")
-    private List<Message<Long>> findDeletedMessagesInMailboxAfterUID(Mailbox<Long> mailbox, long uid) {
-        return getEntityManager().createNamedQuery("findDeletedMessagesInMailboxAfterUID")
-        .setParameter("idParam", mailbox.getMailboxId())
-        .setParameter("uidParam", uid).getResultList();
-    }
-
-    @SuppressWarnings("unchecked")
-    private List<Message<Long>> findDeletedMessagesInMailboxWithUID(Mailbox<Long> mailbox, long uid) {
-        return getEntityManager().createNamedQuery("findDeletedMessagesInMailboxWithUID")
-        .setParameter("idParam", mailbox.getMailboxId())
-        .setParameter("uidParam", uid).setMaxResults(1).getResultList();
-    }
-
-    @SuppressWarnings("unchecked")
-    private List<Message<Long>> findDeletedMessagesInMailboxBetweenUIDs(Mailbox<Long> mailbox, long from, long to) {
-        return getEntityManager().createNamedQuery("findDeletedMessagesInMailboxBetweenUIDs")
-        .setParameter("idParam", mailbox.getMailboxId())
-        .setParameter("fromParam", from)
-        .setParameter("toParam", to).getResultList();
-    }
-
     /**
      * @see org.apache.james.mailbox.store.mail.MessageMapper#countMessagesInMailbox(Mailbox)
      */
     public long countMessagesInMailbox(Mailbox<Long> mailbox) throws MailboxException {
         try {
-            return (Long) getEntityManager().createNamedQuery("countMessagesInMailbox").setParameter("idParam", mailbox.getMailboxId()).getSingleResult();
+            return (Long) getEntityManager().createNamedQuery("countMessagesInMailbox")
+                    .setParameter("idParam", mailbox.getMailboxId()).getSingleResult();
         } catch (PersistenceException e) {
             throw new MailboxException("Count of messages failed in mailbox " + mailbox, e);
         }
@@ -266,14 +171,16 @@ public class JPAMessageMapper extends Ab
      */
     public long countUnseenMessagesInMailbox(Mailbox<Long> mailbox) throws MailboxException {
         try {
-            return (Long) getEntityManager().createNamedQuery("countUnseenMessagesInMailbox").setParameter("idParam", mailbox.getMailboxId()).getSingleResult();
+            return (Long) getEntityManager().createNamedQuery("countUnseenMessagesInMailbox")
+                    .setParameter("idParam", mailbox.getMailboxId()).getSingleResult();
         } catch (PersistenceException e) {
             throw new MailboxException("Count of useen messages failed in mailbox " + mailbox, e);
         }
     }
 
     /**
-     * @see org.apache.james.mailbox.store.mail.MessageMapper#delete(org.apache.james.mailbox.store.mail.model.Mailbox, org.apache.james.mailbox.store.mail.model.Message)
+     * @see org.apache.james.mailbox.store.mail.MessageMapper#delete(org.apache.james.mailbox.store.mail.model.Mailbox,
+     *      org.apache.james.mailbox.store.mail.model.Message)
      */
     public void delete(Mailbox<Long> mailbox, Message<Long> message) throws MailboxException {
         try {
@@ -287,9 +194,10 @@ public class JPAMessageMapper extends Ab
      * @see org.apache.james.mailbox.store.mail.MessageMapper#findFirstUnseenMessageUid(Mailbox)
      */
     @SuppressWarnings("unchecked")
-    public Long findFirstUnseenMessageUid(Mailbox<Long> mailbox)  throws MailboxException {
+    public Long findFirstUnseenMessageUid(Mailbox<Long> mailbox) throws MailboxException {
         try {
-            Query query = getEntityManager().createNamedQuery("findUnseenMessagesInMailboxOrderByUid").setParameter("idParam", mailbox.getMailboxId());
+            Query query = getEntityManager().createNamedQuery("findUnseenMessagesInMailboxOrderByUid").setParameter(
+                    "idParam", mailbox.getMailboxId());
             query.setMaxResults(1);
             List<Message<Long>> result = query.getResultList();
             if (result.isEmpty()) {
@@ -301,94 +209,205 @@ public class JPAMessageMapper extends Ab
             throw new MailboxException("Search of first unseen message failed in mailbox " + mailbox, e);
         }
     }
-    
+
     /**
      * @see org.apache.james.mailbox.store.mail.MessageMapper#findRecentMessageUidsInMailbox(Mailbox)
      */
     @SuppressWarnings("unchecked")
     public List<Long> findRecentMessageUidsInMailbox(Mailbox<Long> mailbox) throws MailboxException {
         try {
-            Query query = getEntityManager().createNamedQuery("findRecentMessageUidsInMailbox").setParameter("idParam", mailbox.getMailboxId());
+            Query query = getEntityManager().createNamedQuery("findRecentMessageUidsInMailbox").setParameter("idParam",
+                    mailbox.getMailboxId());
             return query.getResultList();
         } catch (PersistenceException e) {
             throw new MailboxException("Search of recent messages failed in mailbox " + mailbox, e);
         }
     }
 
+    @Override
+    public Map<Long, MessageMetaData> expungeMarkedForDeletionInMailbox(Mailbox<Long> mailbox, MessageRange set)
+            throws MailboxException {
+        try {
+            final Map<Long, MessageMetaData> data;
+            final List<Message<Long>> results;
+            final long from = set.getUidFrom();
+            final long to = set.getUidTo();
+
+            switch (set.getType()) {
+            case ONE:
+                results = findDeletedMessagesInMailboxWithUID(mailbox, from);
+                data = createMetaData(results);
+                deleteDeletedMessagesInMailboxWithUID(mailbox, from);
+                break;
+            case RANGE:
+                results = findDeletedMessagesInMailboxBetweenUIDs(mailbox, from, to);
+                data = createMetaData(results);
+                deleteDeletedMessagesInMailboxBetweenUIDs(mailbox, from, to);
+                break;
+            case FROM:
+                results = findDeletedMessagesInMailboxAfterUID(mailbox, from);
+                data = createMetaData(results);
+                deleteDeletedMessagesInMailboxAfterUID(mailbox, from);
+                break;
+            default:
+            case ALL:
+                results = findDeletedMessagesInMailbox(mailbox);
+                data = createMetaData(results);
+                deleteDeletedMessagesInMailbox(mailbox);
+                break;
+            }
+
+            return data;
+        } catch (PersistenceException e) {
+            throw new MailboxException("Search of MessageRange " + set + " failed in mailbox " + mailbox, e);
+        }
+    }
 
+    /**
+     * (non-Javadoc)
+     * 
+     * @see org.apache.james.mailbox.store.mail.MessageMapper#move(org.apache.james.mailbox.store.mail.model.Mailbox,
+     *      org.apache.james.mailbox.store.mail.model.Message)
+     */
+    @Override
+    public MessageMetaData move(Mailbox<Long> mailbox, Message<Long> original) throws MailboxException {
+        throw new UnsupportedOperationException("Not implemented - see https://issues.apache.org/jira/browse/IMAP-370");
+    }
 
     /**
-     * @see org.apache.james.mailbox.store.mail.AbstractMessageMapper#copy(Mailbox, long, long, Message)
+     * @see org.apache.james.mailbox.store.mail.AbstractMessageMapper#copy(Mailbox,
+     *      long, long, Message)
      */
-    protected MessageMetaData copy(Mailbox<Long> mailbox, long uid, long modSeq, Message<Long> original) throws MailboxException {
+    protected MessageMetaData copy(Mailbox<Long> mailbox, long uid, long modSeq, Message<Long> original)
+            throws MailboxException {
         Message<Long> copy;
         if (original instanceof JPAStreamingMessage) {
             copy = new JPAStreamingMessage((JPAMailbox) mailbox, uid, modSeq, original);
-        } else if(original instanceof JPAEncryptedMessage) {
+        } else if (original instanceof JPAEncryptedMessage) {
             copy = new JPAEncryptedMessage((JPAMailbox) mailbox, uid, modSeq, original);
         } else {
             copy = new JPAMessage((JPAMailbox) mailbox, uid, modSeq, original);
         }
-        return save(mailbox, copy);        
+        return save(mailbox, copy);
     }
 
-
     /**
-     * @see org.apache.james.mailbox.store.mail.AbstractMessageMapper#save(Mailbox, Message)
+     * @see org.apache.james.mailbox.store.mail.AbstractMessageMapper#save(Mailbox,
+     *      Message)
      */
     protected MessageMetaData save(Mailbox<Long> mailbox, Message<Long> message) throws MailboxException {
 
         try {
-            
-            // We need to reload a "JPA attached" mailbox, because the provide mailbox is already "JPA detached"
-            // If we don't this, we will get an org.apache.openjpa.persistence.ArgumentException.
-            ((AbstractJPAMessage) message).setMailbox(getEntityManager().find(JPAMailbox.class, mailbox.getMailboxId()));
-            
+
+            // We need to reload a "JPA attached" mailbox, because the provide
+            // mailbox is already "JPA detached"
+            // If we don't this, we will get an
+            // org.apache.openjpa.persistence.ArgumentException.
+            ((AbstractJPAMessage) message)
+                    .setMailbox(getEntityManager().find(JPAMailbox.class, mailbox.getMailboxId()));
+
             getEntityManager().persist(message);
             return new SimpleMessageMetaData(message);
         } catch (PersistenceException e) {
             throw new MailboxException("Save of message " + message + " failed in mailbox " + mailbox, e);
         } catch (ArgumentException e) {
             throw new MailboxException("Save of message " + message + " failed in mailbox " + mailbox, e);
-        }        
+        }
     }
 
+    @SuppressWarnings("unchecked")
+    private List<Message<Long>> findMessagesInMailboxAfterUID(Mailbox<Long> mailbox, long uid, int batchSize) {
+        Query query = getEntityManager().createNamedQuery("findMessagesInMailboxAfterUID")
+                .setParameter("idParam", mailbox.getMailboxId()).setParameter("uidParam", uid);
 
-    @Override
-    public Map<Long, MessageMetaData> expungeMarkedForDeletionInMailbox(Mailbox<Long> mailbox, MessageRange set) throws MailboxException {
-        try {
-            final Map<Long, MessageMetaData> data;
-            final List<Message<Long>> results;
-            final long from = set.getUidFrom();
-            final long to = set.getUidTo();
-            
-            switch (set.getType()) {
-                case ONE:
-                    results = findDeletedMessagesInMailboxWithUID(mailbox, from);
-                    data = createMetaData(results);
-                    deleteDeletedMessagesInMailboxWithUID(mailbox, from);
-                    break;
-                case RANGE:
-                    results = findDeletedMessagesInMailboxBetweenUIDs(mailbox, from, to);
-                    data = createMetaData(results);
-                    deleteDeletedMessagesInMailboxBetweenUIDs(mailbox, from, to);
-                    break;
-                case FROM:
-                    results = findDeletedMessagesInMailboxAfterUID(mailbox, from);
-                    data = createMetaData(results);
-                    deleteDeletedMessagesInMailboxAfterUID(mailbox, from);
-                    break;
-                default:
-                case ALL:
-                    results = findDeletedMessagesInMailbox(mailbox);
-                    data = createMetaData(results);
-                    deleteDeletedMessagesInMailbox(mailbox);
-                    break;
-            }
-            
-            return data;
-        } catch (PersistenceException e) {
-            throw new MailboxException("Search of MessageRange " + set + " failed in mailbox " + mailbox, e);
+        if (batchSize > 0)
+            query.setMaxResults(batchSize);
+
+        return query.getResultList();
+    }
+
+    @SuppressWarnings("unchecked")
+    private List<Message<Long>> findMessagesInMailboxWithUID(Mailbox<Long> mailbox, long uid) {
+        return getEntityManager().createNamedQuery("findMessagesInMailboxWithUID")
+                .setParameter("idParam", mailbox.getMailboxId()).setParameter("uidParam", uid).setMaxResults(1)
+                .getResultList();
+    }
+
+    @SuppressWarnings("unchecked")
+    private List<Message<Long>> findMessagesInMailboxBetweenUIDs(Mailbox<Long> mailbox, long from, long to,
+            int batchSize) {
+        Query query = getEntityManager().createNamedQuery("findMessagesInMailboxBetweenUIDs")
+                .setParameter("idParam", mailbox.getMailboxId()).setParameter("fromParam", from)
+                .setParameter("toParam", to);
+
+        if (batchSize > 0)
+            query.setMaxResults(batchSize);
+
+        return query.getResultList();
+    }
+
+    @SuppressWarnings("unchecked")
+    private List<Message<Long>> findMessagesInMailbox(Mailbox<Long> mailbox, int batchSize) {
+        Query query = getEntityManager().createNamedQuery("findMessagesInMailbox").setParameter("idParam",
+                mailbox.getMailboxId());
+        if (batchSize > 0)
+            query.setMaxResults(batchSize);
+        return query.getResultList();
+    }
+
+    private Map<Long, MessageMetaData> createMetaData(List<Message<Long>> uids) {
+        final Map<Long, MessageMetaData> data = new HashMap<Long, MessageMetaData>();
+        for (int i = 0; i < uids.size(); i++) {
+            Message<Long> m = uids.get(i);
+            data.put(m.getUid(), new SimpleMessageMetaData(m));
         }
+        return data;
+    }
+
+    private int deleteDeletedMessagesInMailbox(Mailbox<Long> mailbox) {
+        return getEntityManager().createNamedQuery("deleteDeletedMessagesInMailbox")
+                .setParameter("idParam", mailbox.getMailboxId()).executeUpdate();
+    }
+
+    private int deleteDeletedMessagesInMailboxAfterUID(Mailbox<Long> mailbox, long uid) {
+        return getEntityManager().createNamedQuery("deleteDeletedMessagesInMailboxAfterUID")
+                .setParameter("idParam", mailbox.getMailboxId()).setParameter("uidParam", uid).executeUpdate();
+    }
+
+    private int deleteDeletedMessagesInMailboxWithUID(Mailbox<Long> mailbox, long uid) {
+        return getEntityManager().createNamedQuery("deleteDeletedMessagesInMailboxWithUID")
+                .setParameter("idParam", mailbox.getMailboxId()).setParameter("uidParam", uid).executeUpdate();
+    }
+
+    private int deleteDeletedMessagesInMailboxBetweenUIDs(Mailbox<Long> mailbox, long from, long to) {
+        return getEntityManager().createNamedQuery("deleteDeletedMessagesInMailboxBetweenUIDs")
+                .setParameter("idParam", mailbox.getMailboxId()).setParameter("fromParam", from)
+                .setParameter("toParam", to).executeUpdate();
+    }
+
+    @SuppressWarnings("unchecked")
+    private List<Message<Long>> findDeletedMessagesInMailbox(Mailbox<Long> mailbox) {
+        return getEntityManager().createNamedQuery("findDeletedMessagesInMailbox")
+                .setParameter("idParam", mailbox.getMailboxId()).getResultList();
+    }
+
+    @SuppressWarnings("unchecked")
+    private List<Message<Long>> findDeletedMessagesInMailboxAfterUID(Mailbox<Long> mailbox, long uid) {
+        return getEntityManager().createNamedQuery("findDeletedMessagesInMailboxAfterUID")
+                .setParameter("idParam", mailbox.getMailboxId()).setParameter("uidParam", uid).getResultList();
+    }
+
+    @SuppressWarnings("unchecked")
+    private List<Message<Long>> findDeletedMessagesInMailboxWithUID(Mailbox<Long> mailbox, long uid) {
+        return getEntityManager().createNamedQuery("findDeletedMessagesInMailboxWithUID")
+                .setParameter("idParam", mailbox.getMailboxId()).setParameter("uidParam", uid).setMaxResults(1)
+                .getResultList();
+    }
+
+    @SuppressWarnings("unchecked")
+    private List<Message<Long>> findDeletedMessagesInMailboxBetweenUIDs(Mailbox<Long> mailbox, long from, long to) {
+        return getEntityManager().createNamedQuery("findDeletedMessagesInMailboxBetweenUIDs")
+                .setParameter("idParam", mailbox.getMailboxId()).setParameter("fromParam", from)
+                .setParameter("toParam", to).getResultList();
     }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


Mime
View raw message