james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nor...@apache.org
Subject svn commit: r958240 - in /james/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/mail: JPAMailboxMapper.java model/JPAMailbox.java
Date Sat, 26 Jun 2010 15:52:51 GMT
Author: norman
Date: Sat Jun 26 15:52:50 2010
New Revision: 958240

URL: http://svn.apache.org/viewvc?rev=958240&view=rev
Log:
Make sure the mailbox is unique, this is done by a unique key on the mailbox name. Thx to
Tim-Christian Mundt for the patch (IMAP-164)

Modified:
    james/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/mail/JPAMailboxMapper.java
    james/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/mail/model/JPAMailbox.java

Modified: james/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/mail/JPAMailboxMapper.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/mail/JPAMailboxMapper.java?rev=958240&r1=958239&r2=958240&view=diff
==============================================================================
--- james/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/mail/JPAMailboxMapper.java
(original)
+++ james/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/mail/JPAMailboxMapper.java
Sat Jun 26 15:52:50 2010
@@ -21,13 +21,17 @@ package org.apache.james.imap.jpa.mail;
 
 import java.util.List;
 
+import javax.persistence.EntityExistsException;
 import javax.persistence.EntityManagerFactory;
 import javax.persistence.NoResultException;
 import javax.persistence.PersistenceException;
+import javax.persistence.RollbackException;
 
 import org.apache.james.imap.api.display.HumanReadableText;
 import org.apache.james.imap.jpa.JPATransactionalMapper;
 import org.apache.james.imap.jpa.mail.model.JPAMailbox;
+import org.apache.james.imap.mailbox.MailboxException;
+import org.apache.james.imap.mailbox.MailboxExistsException;
 import org.apache.james.imap.mailbox.MailboxNotFoundException;
 import org.apache.james.imap.mailbox.StorageException;
 import org.apache.james.imap.store.mail.MailboxMapper;
@@ -40,6 +44,7 @@ public class JPAMailboxMapper extends JP
 
     private static final char SQL_WILDCARD_CHAR = '%';
     private final char delimiter;
+    private String lastMailboxName;
     
     public JPAMailboxMapper(EntityManagerFactory entityManagerFactory, char delimiter) {
         super(entityManagerFactory);
@@ -47,10 +52,31 @@ public class JPAMailboxMapper extends JP
     }
 
     /**
+     * Commit the transaction. If the commit fails due a conflict in a unique key constraint
a {@link MailboxExistsException}
+     * will get thrown
+     */
+    @Override
+    protected void commit() throws MailboxException {
+        try {
+            getEntityManager().getTransaction().commit();
+        } catch (PersistenceException e) {
+            if (e instanceof EntityExistsException)
+                throw new MailboxExistsException(lastMailboxName);
+            if (e instanceof RollbackException) {
+                Throwable t = e.getCause();
+                if (t != null && t instanceof EntityExistsException)
+                    throw new MailboxExistsException(lastMailboxName);
+            }
+            throw new StorageException(HumanReadableText.COMMIT_TRANSACTION_FAILED, e);
+        }
+    }
+    
+    /**
      * @see org.apache.james.imap.store.mail.MailboxMapper#save(Mailbox)
      */
     public void save(Mailbox<Long> mailbox) throws StorageException {
         try {
+            this.lastMailboxName = mailbox.getName();
             getEntityManager().persist(mailbox);
         } catch (PersistenceException e) {
             throw new StorageException(HumanReadableText.SAVE_FAILED, e);

Modified: james/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/mail/model/JPAMailbox.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/mail/model/JPAMailbox.java?rev=958240&r1=958239&r2=958240&view=diff
==============================================================================
--- james/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/mail/model/JPAMailbox.java
(original)
+++ james/imap/trunk/jpa/src/main/java/org/apache/james/imap/jpa/mail/model/JPAMailbox.java
Sat Jun 26 15:52:50 2010
@@ -19,6 +19,7 @@
 package org.apache.james.imap.jpa.mail.model;
 
 import javax.persistence.Basic;
+import javax.persistence.Column;
 import javax.persistence.Entity;
 import javax.persistence.GeneratedValue;
 import javax.persistence.Id;
@@ -48,7 +49,7 @@ public class JPAMailbox implements Mailb
     @Id @GeneratedValue private long mailboxId;
     
     /** The value for the name field */
-    @Basic(optional=false) private String name;
+    @Basic(optional=false) @Column(unique = true)  private String name;
 
     /** The value for the uidValidity field */
     @Basic(optional=false) private long uidValidity;



---------------------------------------------------------------------
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