11 Replies Latest reply on May 19, 2006 5:17 PM by Jay Glass

    There is an Error

    Ritchie Koh Newbie

      Dear.

      I've installed the JBMS M5 Pre1.
      There is an Error in the log file as below.
      There exists the "flag" table in the database.

      ---------------------------------------------------
      2006-05-16 16:12:30,958 ERROR [STDERR] Exception in thread "Thread-47"
      2006-05-16 16:12:30,958 ERROR [STDERR] javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: could not execute update query
      2006-05-16 16:12:30,958 ERROR [STDERR] at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:562)
      2006-05-16 16:12:30,958 ERROR [STDERR] at org.hibernate.ejb.QueryImpl.executeUpdate(QueryImpl.java:46)
      2006-05-16 16:12:30,958 ERROR [STDERR] at org.jboss.mail.mailbox.MailboxServiceImpl.org$jboss$mail$mailbox$MailboxServiceImpl$fastExpunge$aop(MailboxServiceImpl.java:709)
      2006-05-16 16:12:30,958 ERROR [STDERR] at org.jboss.mail.mailbox.MailboxServiceImpl.access$3(MailboxServiceImpl.java)
      2006-05-16 16:12:30,958 ERROR [STDERR] at org.jboss.mail.mailbox.MailboxServiceImpl$fastExpunge_1383726929380080844.invokeNext(MailboxServiceImpl$fastExpunge_1383726929380080844.java)
      2006-05-16 16:12:30,958 ERROR [STDERR] at org.jboss.aspects.tx.TxPolicy.invokeInCallerTx(TxPolicy.java:126)
      2006-05-16 16:12:30,958 ERROR [STDERR] at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:196)
      2006-05-16 16:12:30,958 ERROR [STDERR] at org.jboss.mail.mailbox.MailboxServiceImpl$fastExpunge_1383726929380080844.invokeNext(MailboxServiceImpl$fastExpunge_1383726929380080844.java)
      2006-05-16 16:12:30,958 ERROR [STDERR] at org.jboss.mail.mailbox.MailboxServiceImpl.fastExpunge(MailboxServiceImpl.java)
      2006-05-16 16:12:30,958 ERROR [STDERR] at org.jboss.mail.mailbox.MailboxServiceImpl.org$jboss$mail$mailbox$MailboxServiceImpl$deleteMarked$aop(MailboxServiceImpl.java:480)
      2006-05-16 16:12:30,958 ERROR [STDERR] at org.jboss.mail.mailbox.MailboxServiceImpl$deleteMarked_N734570016776585460.invokeNext(MailboxServiceImpl$deleteMarked_N734570016776585460.java)
      2006-05-16 16:12:30,958 ERROR [STDERR] at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:79)
      2006-05-16 16:12:30,958 ERROR [STDERR] at org.jboss.aspects.tx.TxInterceptor$RequiresNew.invoke(TxInterceptor.java:262)
      2006-05-16 16:12:30,958 ERROR [STDERR] at org.jboss.mail.mailbox.MailboxServiceImpl$deleteMarked_N734570016776585460.invokeNext(MailboxServiceImpl$deleteMarked_N734570016776585460.java)
      2006-05-16 16:12:30,958 ERROR [STDERR] at org.jboss.mail.mailbox.MailboxServiceImpl.deleteMarked(MailboxServiceImpl.java)
      2006-05-16 16:12:30,958 ERROR [STDERR] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      2006-05-16 16:12:30,958 ERROR [STDERR] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      2006-05-16 16:12:30,958 ERROR [STDERR] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      2006-05-16 16:12:30,958 ERROR [STDERR] at java.lang.reflect.Method.invoke(Method.java:585)
      2006-05-16 16:12:30,974 ERROR [STDERR] at org.jboss.mx.interceptor.ReflectedDispatcher.invoke(ReflectedDispatcher.java:155)
      2006-05-16 16:12:30,974 ERROR [STDERR] at org.jboss.mx.server.Invocation.dispatch(Invocation.java:94)
      2006-05-16 16:12:30,974 ERROR [STDERR] at org.jboss.mx.interceptor.AbstractInterceptor.invoke(AbstractInterceptor.java:133)
      2006-05-16 16:12:30,974 ERROR [STDERR] at org.jboss.mx.server.Invocation.invoke(Invocation.java:88)
      2006-05-16 16:12:30,974 ERROR [STDERR] at org.jboss.mx.interceptor.ModelMBeanOperationInterceptor.invoke(ModelMBeanOperationInterceptor.java:142)
      2006-05-16 16:12:30,974 ERROR [STDERR] at org.jboss.mx.server.Invocation.invoke(Invocation.java:88)
      2006-05-16 16:12:30,974 ERROR [STDERR] at org.jboss.mx.server.AbstractMBeanInvoker.invoke(AbstractMBeanInvoker.java:260)
      2006-05-16 16:12:30,974 ERROR [STDERR] at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:659)
      2006-05-16 16:12:30,974 ERROR [STDERR] at org.jboss.mx.util.MBeanProxyExt.invoke(MBeanProxyExt.java:190)
      2006-05-16 16:12:30,974 ERROR [STDERR] at $Proxy107.deleteMarked(Unknown Source)
      2006-05-16 16:12:30,974 ERROR [STDERR] at org.jboss.mail.pop3.POP3ProtocolInstance.deleteMarked(POP3ProtocolInstance.java:427)
      2006-05-16 16:12:30,974 ERROR [STDERR] at org.jboss.mail.pop3.handlers.CmdQUIT.handleRequest(CmdQUIT.java:66)
      2006-05-16 16:12:30,974 ERROR [STDERR] at org.jboss.mail.pop3.POP3ProtocolInstance.handleRequest(POP3ProtocolInstance.java:211)
      2006-05-16 16:12:30,974 ERROR [STDERR] at org.jboss.mail.ConnectionHandler.runSocket(ConnectionHandler.java:205)
      2006-05-16 16:12:30,974 ERROR [STDERR] at org.jboss.mail.ConnectionHandler.run(ConnectionHandler.java:97)
      2006-05-16 16:12:30,974 ERROR [STDERR] at EDU.oswego.cs.dl.util.concurrent.PooledExecutor$Worker.run(PooledExecutor.java:743)
      2006-05-16 16:12:30,974 ERROR [STDERR] at java.lang.Thread.run(Thread.java:595)
      2006-05-16 16:12:30,974 ERROR [STDERR] Caused by: org.hibernate.exception.GenericJDBCException: could not execute update query
      2006-05-16 16:12:30,974 ERROR [STDERR] at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)
      2006-05-16 16:12:30,974 ERROR [STDERR] at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91)
      2006-05-16 16:12:30,974 ERROR [STDERR] at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
      2006-05-16 16:12:30,974 ERROR [STDERR] at org.hibernate.hql.ast.exec.BasicExecutor.execute(BasicExecutor.java:84)
      2006-05-16 16:12:30,974 ERROR [STDERR] at org.hibernate.hql.ast.QueryTranslatorImpl.executeUpdate(QueryTranslatorImpl.java:391)
      2006-05-16 16:12:30,974 ERROR [STDERR] at org.hibernate.engine.query.HQLQueryPlan.performExecuteUpdate(HQLQueryPlan.java:259)
      2006-05-16 16:12:30,974 ERROR [STDERR] at org.hibernate.impl.SessionImpl.executeUpdate(SessionImpl.java:1129)
      2006-05-16 16:12:30,974 ERROR [STDERR] at org.hibernate.impl.QueryImpl.executeUpdate(QueryImpl.java:94)
      2006-05-16 16:12:30,974 ERROR [STDERR] at org.hibernate.ejb.QueryImpl.executeUpdate(QueryImpl.java:43)
      2006-05-16 16:12:30,974 ERROR [STDERR] ... 34 more
      2006-05-16 16:12:30,974 ERROR [STDERR] Caused by: java.sql.SQLException: You can't specify target table 'flag' for update in FROM clause
      2006-05-16 16:12:30,974 ERROR [STDERR] at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2928)
      2006-05-16 16:12:30,974 ERROR [STDERR] at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1571)
      2006-05-16 16:12:30,974 ERROR [STDERR] at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1666)
      2006-05-16 16:12:30,974 ERROR [STDERR] at com.mysql.jdbc.Connection.execSQL(Connection.java:2994)
      2006-05-16 16:12:30,974 ERROR [STDERR] at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:936)
      2006-05-16 16:12:30,974 ERROR [STDERR] at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1166)
      2006-05-16 16:12:30,974 ERROR [STDERR] at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1082)
      2006-05-16 16:12:30,974 ERROR [STDERR] at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1067)
      2006-05-16 16:12:30,974 ERROR [STDERR] at org.jboss.resource.adapter.jdbc.WrappedPreparedStatement.executeUpdate(WrappedPreparedStatement.java:251)
      2006-05-16 16:12:30,974 ERROR [STDERR] at org.hibernate.hql.ast.exec.BasicExecutor.execute(BasicExecutor.java:75)
      2006-05-16 16:12:30,974 ERROR [STDERR] ... 39 more

        • 1. Re: There is an Error
          Andrew Oliver Master

          Yes. you will need to drop and recreate the database. M5 is not table-compatable with M4.

          • 2. Re: There is an Error
            Jay Glass Novice

            Hi Andrew and Team, great job on this prerel, it looks nice, though I havnt seen the webmail front end yet, I am going to run m4 and m5pre on same server, for evaluation and testing (though i have to cut the memory down for now)

            But Andrew, I am also getting the "Flag" error...

            heres what I did...

            during install, selected create data source, and gave it a new DSN, filled in user auth info, checked mySql, and named the database

            installed

            when running for first time, it did not create the new database, neither did it on the second run. so as with m4 release, i manually copied over the mysql-ds.xml file

            next time i ran it

            it still did not create the database

            i manually created an empty database

            next time i ran it

            it complained about the tables not existing
            it then created the tables
            and started up fine, and is responsive to outlook

            (?Bug - why do i have to manually place the mysql-ds.xml file, if it is asking for the info in the installer)

            (?Bug - why do I have to create the database, if it is being specified in installer)

            Note: though it started fine, ever since M4, when i start, i always get an error that the DSN already exists, and that it can create the mBean

            Note the only thing i touched, in database, was I updated an alias record, as I entered wrong mailbox id, and spelled alias wrong

            So why am i getting the same "flag error"

            Why do I have to drop database, if I created the empty database, and let the system create it itself?

            should I post my output?


            i think is exact same...

            • 3. Re: There is an Error
              sappenin Novice

              I'm getting the same error. I have a fresh M5_pre1 install, with brand new database tables. This error occurs if there are no messages in the inbox, but I attempt to read my mail with the pop3 client. I'm using mysql for the jbms store, but jms is running on hsql.

              09:36:50,484 INFO [Server] Got request
              09:36:50,484 INFO [Server] waiting for request
              09:36:50,750 INFO [ConnectionHandler] connected: /192.168.100.249 to: /192.168.100.249 until: 1148045930484
              09:36:50,968 WARN [CmdUSER] pop username set to guest
              09:36:52,031 WARN [FromElementType] Using non-qualified column reference [id -> ([id])]
              09:36:52,156 WARN [JDBCExceptionReporter] SQL Error: 1093, SQLState: HY000
              09:36:52,156 ERROR [JDBCExceptionReporter] You can't specify target table 'flag' for update in FROM clause
              09:36:52,218 WARN [FromElementType] Using non-qualified column reference [deleted -> ([deleted])]
              09:36:52,218 WARN [FromElementType] Using non-qualified column reference [folder -> ([folder_id])]
              09:36:52,328 INFO [ConnectionHandler] ConnectionHandler finished
              09:36:54,781 ERROR [STDERR] Exception in thread "Thread-11"
              09:36:54,781 ERROR [STDERR] javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: could not execute update query
              09:36:54,781 ERROR [STDERR] at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:562)
              09:36:54,781 ERROR [STDERR] at org.hibernate.ejb.QueryImpl.executeUpdate(QueryImpl.java:46)
              09:36:54,781 ERROR [STDERR] at org.jboss.mail.mailbox.MailboxServiceImpl.org$jboss$mail$mailbox$MailboxServiceImpl$fastExpunge$aop(MailboxServiceImpl.java:709)
              09:36:54,781 ERROR [STDERR] at org.jboss.mail.mailbox.MailboxServiceImpl.access$3(MailboxServiceImpl.java)
              09:36:54,781 ERROR [STDERR] at org.jboss.mail.mailbox.MailboxServiceImpl$fastExpunge_1383726929380080844.invokeNext(MailboxServiceImpl$fastExpunge_1383726929380080844.java)
              09:36:54,781 ERROR [STDERR] at org.jboss.aspects.tx.TxPolicy.invokeInCallerTx(TxPolicy.java:126)
              09:36:54,781 ERROR [STDERR] at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:196)
              09:36:54,781 ERROR [STDERR] at org.jboss.mail.mailbox.MailboxServiceImpl$fastExpunge_1383726929380080844.invokeNext(MailboxServiceImpl$fastExpunge_1383726929380080844.java)
              09:36:54,828 ERROR [STDERR] at org.jboss.mail.mailbox.MailboxServiceImpl.fastExpunge(MailboxServiceImpl.java)
              09:36:54,828 ERROR [STDERR] at org.jboss.mail.mailbox.MailboxServiceImpl.org$jboss$mail$mailbox$MailboxServiceImpl$deleteMarked$aop(MailboxServiceImpl.java:480)
              09:36:54,828 ERROR [STDERR] at org.jboss.mail.mailbox.MailboxServiceImpl$deleteMarked_N734570016776585460.invokeNext(MailboxServiceImpl$deleteMarked_N734570016776585460.java)
              09:36:54,828 ERROR [STDERR] at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:79)
              09:36:54,828 ERROR [STDERR] at org.jboss.aspects.tx.TxInterceptor$RequiresNew.invoke(TxInterceptor.java:262)
              09:36:54,828 ERROR [STDERR] at org.jboss.mail.mailbox.MailboxServiceImpl$deleteMarked_N734570016776585460.invokeNext(MailboxServiceImpl$deleteMarked_N734570016776585460.java)
              09:36:54,828 ERROR [STDERR] at org.jboss.mail.mailbox.MailboxServiceImpl.deleteMarked(MailboxServiceImpl.java)
              09:36:54,828 ERROR [STDERR] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
              09:36:54,828 ERROR [STDERR] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
              09:36:54,828 ERROR [STDERR] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
              09:36:54,828 ERROR [STDERR] at java.lang.reflect.Method.invoke(Method.java:585)
              09:36:54,828 ERROR [STDERR] at org.jboss.mx.interceptor.ReflectedDispatcher.invoke(ReflectedDispatcher.java:155)
              09:36:54,828 ERROR [STDERR] at org.jboss.mx.server.Invocation.dispatch(Invocation.java:94)
              09:36:54,828 ERROR [STDERR] at org.jboss.mx.interceptor.AbstractInterceptor.invoke(AbstractInterceptor.java:133)
              09:36:54,828 ERROR [STDERR] at org.jboss.mx.server.Invocation.invoke(Invocation.java:88)
              09:36:54,828 ERROR [STDERR] at org.jboss.mx.interceptor.ModelMBeanOperationInterceptor.invoke(ModelMBeanOperationInterceptor.java:142)
              09:36:54,828 ERROR [STDERR] at org.jboss.mx.server.Invocation.invoke(Invocation.java:88)
              09:36:54,828 ERROR [STDERR] at org.jboss.mx.server.AbstractMBeanInvoker.invoke(AbstractMBeanInvoker.java:260)
              09:36:54,828 ERROR [STDERR] at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:659)
              09:36:54,828 ERROR [STDERR] at org.jboss.mx.util.MBeanProxyExt.invoke(MBeanProxyExt.java:190)
              09:36:54,828 ERROR [STDERR] at $Proxy112.deleteMarked(Unknown Source)
              09:36:54,828 ERROR [STDERR] at org.jboss.mail.pop3.POP3ProtocolInstance.deleteMarked(POP3ProtocolInstance.java:427)
              09:36:54,828 ERROR [STDERR] at org.jboss.mail.pop3.handlers.CmdQUIT.handleRequest(CmdQUIT.java:66)
              09:36:54,828 ERROR [STDERR] at org.jboss.mail.pop3.POP3ProtocolInstance.handleRequest(POP3ProtocolInstance.java:211)
              09:36:54,828 ERROR [STDERR] at org.jboss.mail.ConnectionHandler.runSocket(ConnectionHandler.java:205)
              09:36:54,828 ERROR [STDERR] at org.jboss.mail.ConnectionHandler.run(ConnectionHandler.java:97)
              09:36:54,828 ERROR [STDERR] at EDU.oswego.cs.dl.util.concurrent.PooledExecutor$Worker.run(PooledExecutor.java:748)
              09:36:54,828 ERROR [STDERR] at java.lang.Thread.run(Thread.java:595)
              09:36:54,828 ERROR [STDERR] Caused by: org.hibernate.exception.GenericJDBCException: could not execute update query
              09:36:54,828 ERROR [STDERR] at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)
              09:36:54,828 ERROR [STDERR] at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91)
              09:36:54,828 ERROR [STDERR] at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
              09:36:54,828 ERROR [STDERR] at org.hibernate.hql.ast.exec.BasicExecutor.execute(BasicExecutor.java:84)
              09:36:54,828 ERROR [STDERR] at org.hibernate.hql.ast.QueryTranslatorImpl.executeUpdate(QueryTranslatorImpl.java:391)
              09:36:54,828 ERROR [STDERR] at org.hibernate.engine.query.HQLQueryPlan.performExecuteUpdate(HQLQueryPlan.java:259)
              09:36:54,828 ERROR [STDERR] at org.hibernate.impl.SessionImpl.executeUpdate(SessionImpl.java:1129)
              09:36:54,828 ERROR [STDERR] at org.hibernate.impl.QueryImpl.executeUpdate(QueryImpl.java:94)
              09:36:54,828 ERROR [STDERR] at org.hibernate.ejb.QueryImpl.executeUpdate(QueryImpl.java:43)
              09:36:54,828 ERROR [STDERR] ... 34 more
              09:36:54,828 ERROR [STDERR] Caused by: java.sql.SQLException: You can't specify target table 'flag' for update in FROM clause
              09:36:54,828 ERROR [STDERR] at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2928)
              09:36:54,828 ERROR [STDERR] at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1571)
              09:36:54,828 ERROR [STDERR] at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1666)
              09:36:54,828 ERROR [STDERR] at com.mysql.jdbc.Connection.execSQL(Connection.java:2994)
              09:36:54,828 ERROR [STDERR] at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:936)
              09:36:54,828 ERROR [STDERR] at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1166)
              09:36:54,828 ERROR [STDERR] at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1082)
              09:36:54,828 ERROR [STDERR] at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1067)
              09:36:54,828 ERROR [STDERR] at org.jboss.resource.adapter.jdbc.WrappedPreparedStatement.executeUpdate(WrappedPreparedStatement.java:251)
              09:36:54,828 ERROR [STDERR] at org.hibernate.hql.ast.exec.BasicExecutor.execute(BasicExecutor.java:75)
              09:36:54,828 ERROR [STDERR] ... 39 more

              • 4. Re: There is an Error
                sappenin Novice

                Actually, the error above occurs even if I have messages in my inbox.

                • 5. Re: There is an Error
                  Andrew Oliver Master

                  Cool. If someone has a suggested patch (this looks like expunge code) for MySQL please send it. Otherwise, I'll work on this when I get home from San Francisco. Thanks guys.

                  • 6. Re: There is an Error
                    Andrew Oliver Master

                    (note that this is obviously a show stopper for 1.5-final)

                    • 7. Re: There is an Error
                      Jay Glass Novice

                      i narrowed it down to this...

                      delete from Flag where id in (select flags2_.id from MessageData messagedat1_, Flag flags2_ where messagedat1_.id=flags2_.message_id and messagedat1_.deleted=1 and messagedat1_.folder_id=1)

                      but dont know what to do about it

                      • 8. Re: There is an Error
                        Jay Glass Novice

                        delete from Flag where id in (select flags2_.id from MessageData messagedat1_, Flag flags2_ where messagedat1_.id=flags2_.message_id and messagedat1_.deleted=1 and messagedat1_.folder_id=1)
                        the bolded is returning an empty set, thus nothing to delete in Flag

                        not a SQL guru, how would you change this to make it conditional?

                        I.e.

                        delete from flag if (inner sql statement retuns anything)

                        • 9. Re: There is an Error
                          Jay Glass Novice

                          DELETE D
                          FROM Flag AS D
                          JOIN MessageData AS M
                          ON M.id = D.message_id
                          AND M.deleted = 1
                          AND M.folder_id = 1

                          this works, with no error, guess I'll try finding the pertinent source, change it, build it, and cross fingers

                          • 10. Re: There is an Error
                            Jay Glass Novice

                            I dont know how to check in this...

                            I fixed the SQL statement though, i think, testing now

                            I bolded the fixed code

                            /*
                             * JBoss, Home of Professional Open Source
                             * Copyright 2006, JBoss Inc., and individual contributors as indicated
                             * by the @authors tag. See the copyright.txt in the distribution for a
                             * full listing of individual contributors.
                             *
                             * This is free software; you can redistribute it and/or modify it
                             * under the terms of the GNU Lesser General Public License as
                             * published by the Free Software Foundation; either version 2.1 of
                             * the License, or (at your option) any later version.
                             *
                             * This software is distributed in the hope that it will be useful,
                             * but WITHOUT ANY WARRANTY; without even the implied warranty of
                             * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
                             * Lesser General Public License for more details.
                             *
                             * You should have received a copy of the GNU Lesser General Public
                             * License along with this software; if not, write to the Free
                             * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
                             * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
                             */
                            package org.jboss.mail.mailbox;
                            
                            import static org.jboss.mail.util.HibernateUtil.singleResult;
                            
                            import java.util.ArrayList;
                            import java.util.Date;
                            import java.util.List;
                            
                            import javax.naming.InitialContext;
                            import javax.persistence.EntityManager;
                            import javax.persistence.Query;
                            import javax.persistence.TemporalType;
                            
                            import org.hibernate.ScrollMode;
                            import org.hibernate.ScrollableResults;
                            import org.hibernate.Session;
                            import org.jboss.aspects.tx.Tx;
                            import org.jboss.aspects.tx.TxType;
                            import org.jboss.ejb3.entity.HibernateSession;
                            import org.jboss.mail.imap4.IMAP4ProtocolInstance;
                            import org.jboss.mail.message.MailBody;
                            import org.jboss.mail.message.MailBodyManagerMBean;
                            
                            /**
                             * Default Mailbox service impelmentation using hibernate.
                             *
                             * @author Andrew C. Oliver
                             * @author Jason Pugsley
                             * @version $Revision: 1.33 $
                             */
                            // @Service
                            // @Management(org.jboss.mail.mailbox.MailboxService.class)
                            public class MailboxServiceImpl implements MailboxService {
                             EntityManager session = null;
                            
                             MailBodyManagerMBean mgr;
                            
                             // @Resource UserTransaction ut;
                            
                             public MailboxServiceImpl() {
                            
                             }
                            
                             /**
                             * this is a horrible kludge used to init the entity manager (the initEM
                             * method itself is a bit of a kludge) which should be removed when JBAS
                             * supports injecting dependencies via external descriptors.
                             */
                             public void emInit() {
                             if (session == null) {
                             initEM();
                             }
                             }
                            
                             public synchronized void initEM() {
                             if (session == null) {
                             try { // this is a big fat kludge to deal with EJB3 issues
                             InitialContext ctx = new InitialContext();
                             session = ((EntityManager) ctx
                             .lookup("java:/EntityManagers/mail"));
                             } catch (Exception e) {
                             e.printStackTrace();
                             }
                             }
                             }
                            
                             /*
                             * (non-Javadoc)
                             *
                             * @see org.jboss.mail.mailbox.MailboxService#getMailboxById(long)
                             */
                             @Tx(TxType.REQUIRED)
                             public Mailbox getMailboxById(long id) {
                             emInit();
                             return (Mailbox) session.find(Mailbox.class, id);
                             }
                            
                             /*
                             * (non-Javadoc)
                             *
                             * @see org.jboss.mail.mailbox.MailboxService#getMailboxByAlias(java.lang.String)
                             */
                             @Tx(TxType.REQUIRED)
                             public Mailbox getMailboxByAlias(String alias) {
                             emInit();
                             String query = "from Mailbox as mbox join fetch mbox.defaultInFolder where mbox.aliases.name=:alias";
                             return singleResult(session.createQuery(query).setParameter("alias",
                             alias), Mailbox.class);
                             }
                            
                             /*
                             * (non-Javadoc)
                             *
                             * @see org.jboss.mail.mailbox.MailboxService#getDefaultInFolderByAlias(java.lang.String)
                             */
                             @Tx(TxType.REQUIRED)
                             public Folder getDefaultInFolderByAlias(String alias) {
                             emInit();
                             return getMailboxByAlias(alias).getDefaultInFolder();
                             }
                            
                             /*
                             * (non-Javadoc)
                             *
                             * @see org.jboss.mail.mailbox.MailboxService#getDefaultOutFolderByAlias(java.lang.String)
                             */
                             @Tx(TxType.REQUIRED)
                             public Folder getDefaultOutFolderByAlias(String alias) {
                             emInit();
                             return getMailboxByAlias(alias).getDefaultOutFolder();
                             }
                            
                             /*
                             * (non-Javadoc)
                             *
                             * @see org.jboss.mail.mailbox.MailboxService#createMailbox(java.lang.String)
                             */
                             @Tx(TxType.REQUIRED)
                             public Mailbox createMailbox(String alias) {
                             emInit();
                             Mailbox box = this.getMailboxByAlias(alias);
                             if (box != null) {
                             throw new RuntimeException(
                             "You cannot create a SECOND mailbox by that name");
                             }
                             box = new Mailbox();
                             Alias aliasObj = new Alias();
                             aliasObj.setName(alias);
                             box.addAlias(aliasObj);
                             Folder inbox = new Folder();
                             inbox.setName("INBOX");
                             inbox.setParent(box);
                             Folder sent = new Folder();
                             sent.setName("Sent");
                             sent.setParent(box);
                             box.setDefaultInFolder(inbox);
                             box.setDefaultOutFolder(sent);
                             Folder trash = new Folder();
                             trash.setName("Trash");
                             trash.setParent(box);
                             Folder draft = new Folder();
                             draft.setName("Drafts");
                             draft.setParent(box);
                             box.addFolder(inbox);
                             box.addFolder(sent);
                             box.addFolder(trash);
                             box.addFolder(draft);
                             session.persist(box);
                             session.persist(inbox);
                             session.persist(sent);
                             session.persist(trash);
                             session.persist(draft);
                             session.persist(aliasObj);
                             return box;
                             }
                            
                             /*
                             * (non-Javadoc)
                             *
                             * @see org.jboss.mail.mailbox.MailboxService#createFolder(org.jboss.mail.mailbox.Folder,
                             * java.lang.String)
                             */
                             @Tx(TxType.REQUIRED)
                             public Folder createFolder(Folder xfolder, String foldername) {
                             emInit();
                             Folder folder = session.find(Folder.class, xfolder.getId());
                             Folder newFolder = new Folder();
                             newFolder.setName(foldername);
                             folder.addFolder(newFolder);
                             newFolder.setParent(folder);
                             session.persist(newFolder);
                             return newFolder;
                             }
                            
                             /*
                             * (non-Javadoc)
                             *
                             * @see org.jboss.mail.mailbox.MailboxService#deleteMailboxById(long)
                             */
                             @Tx(TxType.REQUIRED)
                             public void deleteMailboxById(long id) {
                             emInit();
                             session.remove(session.find(Mailbox.class, id));
                             }
                            
                             /*
                             * (non-Javadoc)
                             *
                             * @see org.jboss.mail.mailbox.MailboxService#deleteMailboxByAlias(java.lang.String)
                             */
                             @Tx(TxType.REQUIRED)
                             public void deleteMailboxByAlias(String alias) {
                             emInit();
                             session.remove(getMailboxByAlias(alias));
                             }
                            
                             /*
                             * (non-Javadoc)
                             *
                             * @see org.jboss.mail.mailbox.MailboxService#getMailBody(org.jboss.mail.mailbox.MessageData)
                             */
                             @Tx(TxType.REQUIRED)
                             public MailBody getMailBody(MessageData message) {
                             emInit();
                             MailBody retval = mgr.createMailBody(MailBody.STORED_TYPE, null,
                             message.getBodyId());
                             return retval;
                             }
                            
                             /*
                             * (non-Javadoc)
                             *
                             * @see org.jboss.mail.mailbox.MailboxService#createMail(org.jboss.mail.mailbox.Folder,
                             * org.jboss.mail.message.Mail)
                             */
                             @Tx(TxType.REQUIRED)
                             public void createMail(Folder xfolder, MessageData mail) {
                             emInit();
                             Folder folder = session.find(Folder.class, xfolder.getId());
                             folder.addMessage(mail);
                             mail.setFolder(folder);
                             session.persist(mail);
                             }
                            
                             /*
                             * (non-Javadoc)
                             *
                             * @see org.jboss.mail.mailbox.MailboxService#lock(org.jboss.mail.mailbox.Folder)
                             */
                             @Tx(TxType.REQUIRESNEW)
                             public boolean lock(String user, Folder folder, Date expiration) {
                             emInit();
                             try {
                             // InitialContext ctx = new InitialContext();
                            
                             boolean lockAcquired = false;
                             // session.lock(folder, LockMode.UPGRADE_NOWAIT);
                             // first see if there is a lock
                             Lock lock = findUnexpiredLock(folder);
                             // if there is
                             if (lock != null) {
                             // see if the locks thread is still active if it is a thread on
                             // this server..
                             // not yet implemented assume it is still valid
                             // boolean valid = true;
                             // if the lock is still valid then we can't acquire it
                             // lockAcquired = false;
                             return false;
                             } else {
                             lock = createNewLockForFolder(user, folder, expiration);
                             if (lock != null) {
                             lockAcquired = true;
                             }
                             }
                             } catch (Exception e) {
                             e.printStackTrace();
                             return false;
                             }
                             return true;
                             }
                            
                             /**
                             * @param folder
                             * @return
                             */
                             @Tx(TxType.REQUIRED)
                             private Lock createNewLockForFolder(String user, Folder folder,
                             Date expiration) {
                             emInit();
                             Lock lock = new Lock();
                             lock.setFolder((Folder) session.find(Folder.class, folder.getId()));
                             lock.setExpiration(expiration);
                             lock.setThreadId("" + Thread.currentThread().getId()); // TODO should
                             // include
                             // server name
                             lock.setUsername(user);
                             session.persist(lock);
                             return lock;
                             }
                            
                             /**
                             * @param folder
                             * @return
                             */
                             private Lock findUnexpiredLock(Folder folder) {
                             emInit();
                             // session.evict(folder);
                             // TODO make named query
                             String query = "from Lock lock where lock.folder.id = :folder and lock.expiration > :currentDate";
                             List locklist = session.createQuery(query).setParameter("folder",
                             folder.getId()).setParameter("currentDate", new Date(),
                             TemporalType.TIMESTAMP).getResultList();
                            
                             if (locklist.size() == 0) {
                             return null;
                             }
                            
                             return (Lock) locklist.get(0);
                             }
                            
                             private Lock findLockForUser(String username, Folder folder) {
                             emInit();
                             // TODO make named query
                             String query = "from Lock lock where lock.folder.id = :folder and "
                             + "username = :user";
                             Lock lock = (Lock) singleResult(session.createQuery(query)
                             .setParameter("folder", folder.getId()) // set entity
                             .setParameter("user", username), Lock.class);
                             return lock;
                             }
                            
                             /*
                             * (non-Javadoc)
                             *
                             * @see org.jboss.mail.mailbox.MailboxService#commit()
                             */
                             @Tx(TxType.REQUIRED)
                             public boolean commit(String username) {
                             emInit();
                             try {
                             // ut.commit();
                             String query = "delete from Lock lock where lock.username = :user";
                             session.createQuery(query).setParameter("user", username)
                             .executeUpdate();
                             } catch (Exception e) {
                             e.printStackTrace();
                             return false;
                             }
                             return true;
                             }
                            
                             /*
                             * (non-Javadoc)
                             *
                             * @see org.jboss.mail.mailbox.MailboxService#release(org.jboss.mail.mailbox.Folder)
                             */
                             @Tx(TxType.REQUIRESNEW)
                             public void release(String username, Folder folder) {
                             emInit();
                             try {
                             // TODO make this a bulk update and check thread id
                             Lock lock = findLockForUser(username, folder);
                             session.remove(lock);
                             // ut.rollback();
                             } catch (Exception e) {
                             throw new RuntimeException(e);
                             }
                             }
                            
                             /*
                             * (non-Javadoc)
                             *
                             * @see org.jboss.mail.mailbox.MailboxService#getMailCountForFolder(org.jboss.mail.mailbox.Folder)
                             */
                             @Tx(TxType.REQUIRED)
                             public int getMailCountForFolder(Folder folder) {
                             emInit();
                             String query = "select count(m) from MessageData m where m.folder.id = :folder";
                             int count = (Integer) singleResult(session.createQuery(query)
                             .setParameter("folder", folder.getId()), Integer.class);
                             return count;
                             }
                            
                             /*
                             * (non-Javadoc)
                             *
                             * @see org.jboss.mail.mailbox.MailboxService#getMailForFolder(org.jboss.mail.mailbox.Folder)
                             */
                             @Tx(TxType.REQUIRED)
                             public List<MessageData> getMailListForFolder(Folder folder,
                             boolean includeFlags) {
                             emInit();
                             String fetchpart = includeFlags ? "left outer join fetch m.flags " : "";
                             String query = "Select distinct m from MessageData m " + fetchpart
                             + "where m.folder.id = :folder order by m.id";
                             Query q = (Query) session.createQuery(query).setParameter("folder",
                             folder.getId());
                             List<MessageData> result = (List<MessageData>) q.getResultList();
                             return result;
                             }
                            
                             /*
                             * (non-Javadoc)
                             *
                             * @see org.jboss.mail.mailbox.MailboxService#getMailAt(org.jboss.mail.mailbox.Folder,
                             * int)
                             */
                             @Tx(TxType.REQUIRED)
                             public MessageData getMailAt(Folder folder, int id) {
                             ScrollableResults res = null;
                             try {
                             emInit();
                             MessageData data = null;
                             Session hs = ((HibernateSession) session).getHibernateSession();
                             String query = "from MessageData m where m.folder.id = :folder order by m.id";
                             org.hibernate.Query q = hs.createQuery(query).setParameter(
                             "folder", folder.getId());
                             res = q.scroll(ScrollMode.SCROLL_SENSITIVE);
                            
                             // res.first();
                             // if (id > 0) {
                             res.scroll(id);
                             // }
                             data = (MessageData) res.get(0);
                            
                             return data;
                             } finally {
                             res.close();
                             }
                             }
                            
                             /*
                             * (non-Javadoc)
                             *
                             * @see org.jboss.mail.mailbox.MailboxService#setBodyManager(org.jboss.mail.message.MailBodyManagerMBean)
                             */
                             public void setBodyManager(MailBodyManagerMBean bodymgr) {
                             this.mgr = bodymgr;
                            
                             }
                            
                             /*
                             * (non-Javadoc)
                             *
                             * @see org.jboss.mail.mailbox.MailboxService#getBodyManager()
                             */
                             public MailBodyManagerMBean getBodyManager() {
                             return this.mgr;
                             }
                            
                             /*
                             * (non-Javadoc)
                             *
                             * @see org.jboss.mail.mailbox.MailboxService#markDeleted(org.jboss.mail.mailbox.Folder,
                             * int)
                             */
                             @Tx(TxType.REQUIRED)
                             public boolean markDeleted(Mailbox mailbox, int i) {
                             emInit();
                             // session.refresh(mailbox);
                             MessageData m = getMailAt(mailbox.getDefaultInFolder(), i);
                             if (m != null) {
                             m.setDeleted(true);
                             return true;
                             }
                             return false;
                             }
                            
                             /*
                             * (non-Javadoc)
                             *
                             * @see org.jboss.mail.mailbox.MailboxService#deleteMarked(org.jboss.mail.mailbox.Folder)
                             */
                             @Tx(TxType.REQUIRESNEW)
                             public boolean deleteMarked(Mailbox mailbox) {
                             emInit();
                             // session.refresh(mailbox);
                             Folder folder = mailbox.getDefaultInFolder();
                             fastExpunge(folder);
                            /*
                             // TODO the store is not also purged
                            // String query = "delete from MessageData m join m.flags f where m.folder.id = :folder and m.deleted = true)";
                            // session.createQuery(query).setParameter("folder", folder.getId())
                            // .executeUpdate();
                             String query = "delete from MessageData m where m.folder.id = :folder and m.deleted = true";
                             session.createQuery(query).setParameter("folder", folder.getId())
                             .executeUpdate();
                            */
                             return true;
                             }
                            
                             /*
                             * (non-Javadoc)
                             *
                             * @see org.jboss.mail.mailbox.MailboxService#unmarkDeleted(org.jboss.mail.mailbox.Mailbox)
                             */
                             @Tx(TxType.REQUIRESNEW)
                             public boolean unmarkDeleted(Mailbox mailbox) {
                             emInit();
                             // session.refresh(mailbox);
                             Folder folder = mailbox.getDefaultInFolder();
                             String query = "update MessageData m set m.deleted = false where m.folder.id = :folderid";
                             session.createQuery(query).setParameter("folderid", folder.getId())
                             .executeUpdate();
                             return true;
                             }
                            
                             /*
                             * (non-Javadoc)
                             *
                             * @see org.jboss.mail.mailbox.MailboxService#getFolderIdByAlias(java.lang.String)
                             */
                             @Tx(TxType.REQUIRED)
                             public long getMailboxIdByAlias(String alias) {
                             emInit();
                             String query = "select mbox.id from Mailbox mbox where mbox.aliases.name=:alias";
                             return (Long) singleResult(session.createQuery(query).setParameter(
                             "alias", alias), Long.class);
                            
                             }
                            
                             /*
                             * (non-Javadoc)
                             *
                             * @see org.jboss.mail.mailbox.MailboxService#createAlias(long,
                             * java.lang.String)
                             */
                             @Tx(TxType.REQUIRED)
                             public boolean createAlias(long id, String aliasName) {
                             emInit();
                             Mailbox m = session.find(Mailbox.class, id);
                             Alias alias = new Alias();
                             alias.setName(aliasName);
                             m.addAlias(alias);
                             session.persist(alias);
                             session.persist(m);
                             return true;
                             }
                            
                             @Tx(TxType.REQUIRED)
                             public Folder getSubfolderByName(Folder folder, String name) {
                             emInit();
                             String query = "from Folder f where f.parent.id = :folderid and f.name = :name";
                             List result = session.createQuery(query).setParameter("folderid",
                             folder.getId()).setParameter("name", name).getResultList();
                             if (result.size() == 0) {
                             return null;
                             }
                             return (Folder) result.get(0);
                             }
                            
                             @Tx(TxType.REQUIRED)
                             public List<Folder> getSubfolders(Folder folder) {
                             emInit();
                             String query = "from Folder f where f.parent.id = :folderid";
                             List<Folder> result = (List<Folder>) session.createQuery(query)
                             .setParameter("folderid", folder.getId()).getResultList();
                             if (result.size() == 0) {
                             return null;
                             }
                             return result;
                             }
                            
                             /*
                             * (non-Javadoc)
                             *
                             * @see org.jboss.mail.mailbox.MailboxService#getRecentCount(org.jboss.mail.mailbox.Folder)
                             */
                             public int getRecentCountForFolder(Folder folder) {
                             emInit();
                             String query = "select count(m) from MessageData m where m.recent = true and m.folder.id = :folder";
                             int count = (Integer) singleResult(session.createQuery(query)
                             .setParameter("folder", folder.getId()), Integer.class);
                             return count;
                             }
                            
                             public long getLikelyUID(Folder folder) {
                             emInit();
                             String query = "select max(id) from MessageData m"; // uid's are
                             // generally global
                             // to the folder --
                             // this is most
                             // likely a bogus
                             // number
                             List list = session.createQuery(query).getResultList();
                             long uid = 0;
                             if (list != null && list.size() > 0) {
                             if (list.get(0) != null) {
                             uid = (Long) list.get(0);
                             }
                             }
                             // long uid = (list != null && list.size() >0 && list.get(0) != null) ?
                             // (Long)list.get(0) : 0; //workaround for hibernate bug of null
                             // containing list
                             return uid + 1;
                             }
                            
                             /*
                             * (non-Javadoc)
                             *
                             * @see org.jboss.mail.mailbox.MailboxService#updateMessageMetadata(org.jboss.mail.mailbox.MessageData)
                             */
                             @Tx(TxType.REQUIRED)
                             public void updateMessageMetaData(MessageData md) {
                             emInit();
                             session.merge(md);
                            
                             }
                            
                             /*
                             * (non-Javadoc)
                             *
                             * @see org.jboss.mail.mailbox.MailboxService#copy(org.jboss.mail.mailbox.MessageData,
                             * org.jboss.mail.mailbox.Folder, org.jboss.mail.mailbox.Folder)
                             */
                             @Tx(TxType.REQUIRED)
                             public void copy(MessageData m, Folder source, Folder target) {
                             emInit();
                             MessageData loaded = session.find(MessageData.class, m.getId());
                             Folder loadedTarget = session.find(Folder.class, target.getId());
                             MessageData cloned = MessageData.clone(loaded);
                             cloned.setFolder(loadedTarget);
                             session.persist(cloned);
                             // loadedTarget.addMessage(cloned);
                             }
                            
                             @Tx(TxType.REQUIRED)
                             public List<Object[]> folderSummary(String alias) {
                             Mailbox mbox = getMailboxByAlias(alias);
                             List<Object[]> retval = new ArrayList<Object[]>();
                             retSummary(mbox.getId(), "", retval);
                             return retval;
                             }
                            
                             @Tx(TxType.REQUIRED)
                             private void retSummary(long pid, String path, List<Object[]> retval) {
                             String query = "select f.name as name, " + "f.id as id, "
                             + "f.parent.id as pid, " + "'" + path + "' as path, "
                             + "count(m.id) as messages, " + "count(u.id) as unread "
                             + "from Folder as f " + "left join f.messages m "
                             + "left join f.messages u " + " with u.seen = false "
                             + "where " + " f.parent.id = :pid " + "group by "
                             + "f.name, f.id, f.parent.id " + "order by " + "f.parent.id";
                            
                             List<Object[]> results = (List<Object[]>) session.createQuery(query)
                             .setParameter("pid", pid).getResultList();
                            
                             for (int i = 0; i < results.size(); i++) {
                             Object[] oa = results.get(i);
                             long xpid = (Long) oa[1];
                             String rpath = path + "/" + oa[0];
                             retval.add(oa);
                             retSummary(xpid, rpath, retval);
                             }
                             }
                            
                             /*
                             * (non-Javadoc)
                             *
                             * @see org.jboss.mail.mailbox.MailboxService#expunge(org.jboss.mail.mailbox.Folder,
                             * boolean)
                             */
                             @Tx(TxType.REQUIRED)
                             public List<Integer> expunge(Folder folder, boolean returnMessages) {
                             if (returnMessages == false) {
                             fastExpunge(folder);
                             return null;
                             }
                             List<MessageData> mails = this.getMailListForFolder(folder, true);
                             List<Integer> retval = new ArrayList<Integer>();
                            
                             // org.jboss.mail.imap4.commands.ExpungeCommand.execute() relies on this method
                             // returning the List<> in reverse order. If this behavior is changed please
                             // reimplement ExpungeCommand.execute() as well.
                             for (int i = mails.size(); i > 0; i--) {
                             int idx = i - 1;
                             MessageData mail = mails.get(idx);
                             if (mail.isDeleted()) {
                             retval.add(i); // convieniently this happens to also be the 1
                             // based idx we need
                             session.remove(mail);
                             if (this.mgr.getUseStore()) {
                             if (getCountReferencingBody(mail.getBodyId()) < 1) {
                             this.mgr.getStore().delete(mail.getBodyId());
                             }
                             }
                             }
                             }
                             // TODO I think we could replace this with set logic and still capture
                             // the IDs
                             return retval;
                             }
                            
                             /*
                             * (non-Javadoc)
                             *
                             * @see org.jboss.mail.mailbox.MailboxService#getMail(long)
                             */
                             @Tx(TxType.REQUIRED)
                             public MessageData getMail(long uid) {
                             MessageData data = session.find(MessageData.class, uid);
                             return data;
                             }
                            
                             @Tx(TxType.REQUIRED)
                             private void fastExpunge(Folder f) {
                             emInit();
                             //session.createQuery(
                             // "delete from Flag f where f.id in (select m.flags.id "+
                             // "from MessageData m where m.deleted = true and m.folder.id = :folder)")
                             session.createQuery(
                             "DELETE D FROM Flag AS D " +
                             "JOIN MessageData AS M " +
                             "ON M.id = D.message_id " +
                             "AND M.deleted = true " +
                             "AND M.folder_id = :folder")
                             .setParameter("folder", f.getId()).executeUpdate();
                             session.createQuery(
                             "delete from MessageData m where m.deleted=true and "+
                             "m.folder.id = :folder")
                             .setParameter("folder", f.getId()).executeUpdate();
                             this.mgr.getStore().cleanUp();
                             }
                            
                             /**
                             * @param bodyId
                             * @return count of messages referencing a given bodyId (if this is 0 then
                             * it is SAFE to delete the body)
                             */
                             @Tx(TxType.REQUIRED)
                             private int getCountReferencingBody(long bodyId) {
                             int count = 0;
                             List<Integer> result = (List<Integer>) session
                             .createQuery(
                             "select count(msg) from MessageData msg where msg.bodyId = :bodyId")
                             .setParameter("bodyId", bodyId).getResultList();
                            
                             if (result != null && result.size() != 0 && result.get(0) != null) {
                             count = result.get(0);
                             }
                             return count;
                             }
                            
                             /*
                             * (non-Javadoc)
                             *
                             * @see org.jboss.mail.mailbox.MailboxService#getUnseenCountForFolder(org.jboss.mail.mailbox.Folder)
                             */
                             @Tx(TxType.REQUIRED)
                             public int getUnseenCountForFolder(Folder folder) {
                             emInit();
                             int count = 0;
                             String query = "select count(m) from MessageData m where m.seen = false and m.folder.id = :folder";
                             List<Integer> result = (List<Integer>) session.createQuery(query)
                             .setParameter("folder", folder.getId()).getResultList();
                             if (result != null && result.size() != 0 && result.get(0) != null) {
                             count = result.get(0);
                             }
                             return count;
                             }
                            
                             /*
                             * (non-Javadoc)
                             *
                             * @see org.jboss.mail.mailbox.MailboxService#moveFolder(org.jboss.mail.mailbox.Folder,
                             * org.jboss.mail.mailbox.Folder, java.lang.String)
                             */
                             @Tx(TxType.REQUIRED)
                             public Folder moveFolder(Folder folder, Folder target, String name) {
                             Folder f = session.find(Folder.class, folder.getId());
                             Folder t = session.find(Folder.class, target.getId());
                             f.setName(name);
                             if(folder.getId() != t.getId()) {
                             f.getParent().getFolders().remove(f);
                             t.addFolder(t);
                             f.setParent(t);
                             }
                             session.persist(f);
                             return f;
                             }
                            
                             /*
                             * (non-Javadoc)
                             *
                             * @see org.jboss.mail.mailbox.MailboxService#getPathFor(org.jboss.mail.mailbox.Folder)
                             */
                             @Tx(TxType.REQUIRED)
                             public String getPathFor(Folder folder) {
                             if (folder.getParent() == null) {
                             return "/";
                             }
                             String ancestorpath = getPath("",folder.getParent());
                             String retval = ancestorpath.endsWith("/") ? ancestorpath+folder.getName() : ancestorpath + "/" + folder.getName();
                             return retval;
                             }
                            
                             @Tx(TxType.REQUIRED)
                             private String getPath(String path, Folder folder) {
                             if (folder.getParent() == null) {
                             return "/" + path;
                             } else {
                             return getPath(folder.getName() + "/" + path, folder.getParent());
                             }
                             }
                            
                             /*
                             * (non-Javadoc)
                             *
                             * @see org.jboss.mail.mailbox.MailboxService#deleteMessage(long)
                             */
                             @Tx(TxType.REQUIRED)
                             public void deleteMessage(long uid) {
                             MessageData data = this.getMail(uid);
                             session.remove(data);
                             }
                            
                             /*
                             * (non-Javadoc)
                             *
                             * @see org.jboss.mail.mailbox.MailboxService#deleteFolder(org.jboss.mail.mailbox.Folder)
                             */
                             @Tx(TxType.REQUIRED)
                             public void deleteFolder(Folder folder) {
                             Folder f = session.find(Folder.class, folder.getId());
                             f.getParent().getFolders().remove(f);
                             session.remove(f);
                             }
                            
                             /* (non-Javadoc)
                             * @see org.jboss.mail.mailbox.MailboxService#getSubfolderByPath(org.jboss.mail.mailbox.Mailbox, java.lang.String[])
                             */
                             @Tx(TxType.REQUIRED)
                             public Folder getSubfolderByPath(Folder parent, String[] path) {
                             Folder f = parent;
                             for (int i = 0; i < path.length; i++) {
                             f = this.getSubfolderByName(f, path);
                             }
                             return f;
                             }
                            
                             }


                            • 11. Re: There is an Error
                              Jay Glass Novice

                              it doesnt like my sql

                              anyone know how to make this org. hibernate.hql compliant...

                              17:15:38,851 ERROR [STDERR] Caused by: org.hibernate.hql.ast.QuerySyntaxExceptio
                              n: unexpected token: from near line 1, column 10 [delete d from org.jboss.mail.m
                              ailbox.Flag as d join MessageData as m on m.id=d.message_id and m.deleted=true a
                              nd m.folder_id = :folder]