gt:                        this.ied.addEmailInDB(sii, username, Def.PROTOCOL_IMAP);
context: /*
   * Funambol is a mobile platform developed by Funambol, Inc.
   * Copyright (C) 2005 - 2007 Funambol, Inc.
   *
   * This program is free software; you can redistribute it and/or modify it under
   * the terms of the GNU Affero General Public License version 3 as published by
   * the Free Software Foundation with the addition of the following permission
   * added to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED
   * WORK IN WHICH THE COPYRIGHT IS OWNED BY FUNAMBOL, FUNAMBOL DISCLAIMS THE
   * WARRANTY OF NON INFRINGEMENT  OF THIRD PARTY RIGHTS.
   *
   * This program 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 General Public License for more
   * details.
   *
   * You should have received a copy of the GNU Affero General Public License
   * along with this program; if not, see http://www.gnu.org/licenses or write to
   * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
   * MA 02110-1301 USA.
   *
   * You can contact Funambol, Inc. headquarters at 643 Bair Island Road, Suite
   * 305, Redwood City, CA 94063, USA, or at email address info@funambol.com.
   *
   * The interactive user interfaces in modified source and object code versions
   * of this program must display Appropriate Legal Notices, as required under
   * Section 5 of the GNU Affero General Public License version 3.
   *
   * In accordance with Section 7(b) of the GNU Affero General Public License
   * version 3, these Appropriate Legal Notices must retain the display of the
   * "Powered by Funambol" logo. If the display of the logo is not reasonably
   * feasible for technical reasons, the Appropriate Legal Notices must display
   * the words "Powered by Funambol".
   */
  package com.funambol.email.items.manager;
  
  
  import com.funambol.common.pim.common.Property;
  import com.funambol.email.engine.source.EmailSyncSource;
  import com.funambol.email.exception.EmailAccessException;
  import com.funambol.email.exception.EntityException;
  import com.funambol.email.exception.SendingException;
  import com.funambol.email.items.dao.ImapEntityDAO;
  import com.funambol.email.model.DefaultFolder;
  import com.funambol.email.model.EmailFilter;
  import com.funambol.email.model.FlagProperties;
  import com.funambol.email.model.ItemFolder;
  import com.funambol.email.model.ItemMessage;
  import com.funambol.email.model.SyncItemInfo;
  import com.funambol.email.model.UpdatedMessage;
  import com.funambol.email.pdi.mail.Email;
  import com.funambol.email.transport.ImapMailServerWrapper;
  import com.funambol.email.util.Def;
  import com.funambol.email.util.Utility;
  import com.funambol.email.util.UtilityDate;
  import com.funambol.framework.engine.SyncItemKey;
  import com.funambol.framework.engine.source.RefusedItemException;
  import com.funambol.framework.tools.id.DBIDGenerator;
  import com.sun.mail.imap.IMAPFolder;
  import javax.mail.Folder;
  import javax.mail.Message;
  import javax.mail.MessagingException;
  import javax.mail.Session;
  import java.util.ArrayList;
  import java.util.LinkedHashMap;
  import java.util.Locale;
  import java.util.Map;
  import javax.mail.FetchProfile;
  import javax.mail.Flags;
  import org.apache.commons.lang.StringUtils;
  
  /**
   * @version $Id: ImapEntityManager.java,v 1.4 2008-06-03 09:22:51 testa Exp $
   */
  public class ImapEntityManager extends EntityManagerFilter {
  
      // -------------------------------------------------------------- Properties
  
      /**     */
      public ImapMailServerWrapper imsw = null;
  
      /**     */
      public ImapEntityDAO ied = null;
  
      // ------------------------------------------------------------ Costructors
  
      /**
       *
       */
      public ImapEntityManager() {
      }
  
      /**
       * @param ess EmailSyncSource
       */
      public ImapEntityManager(EmailSyncSource ess)
       throws EmailAccessException {
          imsw       = (ImapMailServerWrapper) ess.getMSWrapper();
          serverType = ess.getMailServerAccount().getMailServer().getMailServerType();
          try {
              ied = new ImapEntityDAO(serverType);
          } catch (EntityException e) {
              throw new EmailAccessException("Error creating the ImapEntityDAO", e);
          }
      }
  
  
  
      // ---------------------------------------------------------- Public Methods
  
      /**
       * deletes an email
       *
       * @param parentId String
       * @param mailId String
       * @param GUID String
       * @param serverItems map with all in the Mail Server CrcSyncItemsInfo
       * @param source_uri  String
       * @param principalId long
       * @throws EntityException
       */
      public void removeEmail(String parentId,
                              String mailId,
                              String GUID,
                              Map serverItems,
                              String username,
                              String source_uri,
                              long principalId)
              throws EntityException {
  
          String fullpath = null;
  
          try {
              fullpath = this.ied.getFullPathFromFID(parentId, source_uri, principalId, username);
          } catch (EntityException e) {
              throw new EntityException(e);
          }
  
          if (log.isTraceEnabled()) {
              log.trace("remove item in FID: " + fullpath + " with FMID: " + mailId);
          }
  
          if (fullpath != null) {
              if (parentId.equalsIgnoreCase(Def.FOLDER_INBOX_ID)) {
                  try {
  
                      // INBOX
  
                      timeStart = System.currentTimeMillis();
  
                      IMAPFolder inbox = this.imsw.folderInboxOpened;
                      long uid = Long.parseLong(mailId);
  
                      if (!inbox.isOpen()) {
                          inbox.open(javax.mail.Folder.READ_WRITE);
                      }
  
                      Message message = inbox.getMessageByUID(uid);
                      IMAPFolder trashFolder = this.imsw.getTrashFolder();
                      
                      //
                      // If the trash folder does not exists, then create it.
                      //
                      if (!trashFolder.exists()){
                          
                          if (log.isTraceEnabled()){
                              log.trace("Trash folder does not exist. Creating Trash folder with path "
                                      + trashFolder.getFullName());
                          }
                          
                          if (trashFolder.create(Folder.HOLDS_MESSAGES)) {
                              trashFolder.open(Folder.READ_WRITE);
                          }
                      }
                      
                      if (message != null) {
                          if (message.isExpunged()) {
                              //
                              // GMail mail server sometimes says an email is expunged
                              // even if it has not been previously deleted.
                              //
                              // Workaround:
                              //
                              // - such behaviour seems to appear after the first time
                              //   IMAPFolder.copyMessage method is executed on an open 
                              //   folder.
                              //
                              //   More precisely: the first time method is executed,
                              //   the given message is moved correctly; each of 
                              //   the subsequent execution could work correctly
                              //   or not.
                              // 
                              // - So: reopen the inbox folder (as: "nothing has
                              //   happened"), extract the message, call copyMessage
                              //   with that message.
                              //
  
                              if (inbox.isOpen()) {
                                  //
                                  // close folder expunging all emails to be expunged 
                                  // in order to reset state of each email
                                  //
                                  inbox.close(true);
                                  inbox.open(Folder.READ_WRITE);
                              }
  
                              message = ((IMAPFolder) inbox).getMessageByUID(uid);
                          }
                          
                          //
                          // Following check: message != null is needed in the case
                          // message was expunged (see above).
                          //
                          if (message != null) {
                              
                              // if TRASH folder exists, then copy email to TRASH folder
                              if (trashFolder.exists()) {
                                  this.ied.copyEmail(inbox, trashFolder, uid);
                              }
                              
                              //
                              // - With some mail servers the copyEmail method
                              //   copies the original message in the destination
                              //   folder and leaves the original one in the source
                              //   one.
                              // - Other mail servers move the message from the
                              //   source folder to the destination folder; thus
                              //   the original message cannot be deleted from the
                              //   source folder, since it has been already deleted. 
                              //
                              // For example: consider the Gmail mail server; when
                              // moving an email from the INBOX folder to the default
                              // trash folder, the copy operation will automatically 
                              // delete and expunge the original message from the
                              // source folder; note also that this behavior does
                              // not happen when destination folder is not the default
                              // trash folder).
                              //
                              if (!message.isExpunged()) {
                                  // remove email from inbox folder
                                  this.ied.removeEmail(inbox, uid);
                              }
                          }
                      }
  
                      // remove from Cache Inbox Table
                      this.ied.removeEmailInDB(username, Def.PROTOCOL_IMAP, GUID);
  
                      // remove serverItemsList
                      this.ied.removeItemInServerItems(GUID, serverItems);
  
                      timeStop = System.currentTimeMillis();
                      if (log.isTraceEnabled()) {
                          log.trace("removeItem Execution Time: " +
                                  (timeStop - timeStart) + " ms");
                      }
                  } catch (MessagingException e) {
                      throw new EntityException(e);
                  } catch (EntityException e) {
                      throw new EntityException(e);
                  } catch (EmailAccessException e) {
                      throw new EntityException(e);
                  }
              } else {
  
                  // SENT - OUTBOX - DRAFTS - TRASH
  
                  IMAPFolder f = null;
  
                  EntityException finalExc = null;
  
                  try {
  
                      timeStart = System.currentTimeMillis();
                      // rest of the folders
                      f = (IMAPFolder) this.imsw.getMailDefaultFolder().getFolder(fullpath);
  
                      if (f.exists()) {
  
                          if (!f.isOpen()) {
                              f.open(Folder.READ_WRITE);
                          }
  
                          // remove email from mail server
                          this.ied.removeEmail(f, Long.parseLong(mailId));
  
                          // remove serverItemsList
                          this.ied.removeItemInServerItems(GUID, serverItems);
  
                      }
  
                  } catch (MessagingException me) {
                      throw new EntityException(me);
                  } catch (EntityException e) {
                      throw new EntityException(e);
                  } finally {
                      if (f != null) {
                          try {
                              if (f.isOpen()) {
                                  f.close(true);
                                  timeStop = System.currentTimeMillis();
                                  if (log.isTraceEnabled()) {
                                      log.trace("removeItem Execution Time: " +
                                              (timeStop - timeStart) + " ms");
                                  }
                              }
                          } catch (MessagingException me) {
                              throw new EntityException(me);
                          }
                      }
                  }
              }
          }
      }
  
      /**
       * deletes an email
       *
       * @param parentId String
       * @param GUID String
       * @param username String
       * @throws EntityException
       */
      public void removeEmailSoft(String parentId,
                                  String GUID,
                                  String username)
        throws EntityException {
  
  
          if (log.isTraceEnabled()) {
              log.trace("remove item soft in FID: " + parentId +
                      " with FMID: " + GUID);
          }
  
          if (parentId.equalsIgnoreCase(Def.FOLDER_INBOX_ID)) {
             try {
                  timeStart = System.currentTimeMillis();
                  this.ied.setDeletedEmailInInbox(username, Def.PROTOCOL_IMAP, GUID);
                  timeStop = System.currentTimeMillis();
                  if (log.isTraceEnabled()) {
                      log.trace("removeItemSoft Execution Time: " +
                              (timeStop - timeStart) + " ms");
                  }
              } catch (EntityException e) {
                  throw new EntityException(e);
              }
          } else {
              // @todo
              // do nothing
          }
  
      }
  
      /**
       * deletes a Folder
       *
       * @param folderId    String
       * @param source_uri  String
       * @param principalId long
       * @throws EntityException
       */
      public void removeFolder(String folderId,
                               Map serverItems,
                               String source_uri,
                               long principalId,
                               String username)
        throws EntityException {
  
          String fullpath;
          IMAPFolder f;
  
          if (folderId.equalsIgnoreCase(Def.FOLDER_INBOX_ID) ||
              folderId.equalsIgnoreCase(Def.FOLDER_OUTBOX_ID) ||
              folderId.equalsIgnoreCase(Def.FOLDER_SENT_ID) ||
              folderId.equalsIgnoreCase(Def.FOLDER_DRAFTS_ID) ||
              folderId.equalsIgnoreCase(Def.FOLDER_TRASH_ID)) {
  
              throw new EntityException("You can't delete this Folder");
  
          } else {
  
              try {
  
                  fullpath = this.ied.getFullPathFromGUID(folderId, source_uri, principalId, username);
  
                  if (fullpath != null) {
  
                      // remove folder from MailServer
                      f = (IMAPFolder) this.imsw.getMailDefaultFolder().getFolder(fullpath);
                      if (f.exists()) {
                          this.ied.removeFolderInServer(f);
                      }
  
                      // remove folder from Local DB
                      this.ied.removeFolderInDB(folderId, source_uri, principalId, username);
  
                      // remove folder from serverItems
                      this.ied.removeItemInServerItems(folderId, serverItems);
  
                  }
  
              } catch (MessagingException me) {
                  throw new EntityException(me);
              }
          }
      }
  
      /**
       * deletes a Folder
       *
       * @param folderId    String
       * @param source_uri  String
       * @param principalId long
       * @throws EntityException
       */
      public void removeFolderSoft(String folderId,
                                   String source_uri,
                                   long principalId)
        throws EntityException {
  
           // @todo
           // do nothing
  
      }
  
      /**
       * updates a document defined by UID
       *
       * @param FID String
       * @param FMID String
       * @param emailToUpdate Email
       * @param source_uri  String
       * @param principalId long
       * @return Email
       * @throws EntityException
       */
      public Email updateEmail(String FID,
                               String FMID,
                               Email emailToUpdate,
                               Map serverItems,
                               String username,
                               String protocol,
                               String source_uri,
                               long principalId)
        throws EntityException {
  
          String fullpath = null;
  
          try {
              fullpath = this.ied.getFullPathFromFID(FID, source_uri, principalId, username);
          } catch (Exception me) {
              throw new EntityException(me);
          }
  
          if (log.isTraceEnabled()) {
              log.trace("updateItem fullpath " + fullpath + "FID: " + FID);
          }
  
          if (fullpath != null) {
  
              // the emailItem could be null
              // i.e. the client send only the read property get flags
              FlagProperties fp = Utility.getFlags(emailToUpdate);
  
              Message msgNew = null;
              if (emailToUpdate.getEmailItem().getStringRFC2822() != null) {
                  msgNew = createMessage(this.imsw.getSession(), emailToUpdate, FID, false, fp);
              }
  
              if (FID.equalsIgnoreCase(Def.FOLDER_INBOX_ID)) {
  
                  // INBOX
  
                  try {
  
                      timeStart = System.currentTimeMillis();
  
                      if (!this.imsw.folderInboxOpened.isOpen()){
                          this.imsw.folderInboxOpened.open(javax.mail.Folder.READ_WRITE);
                      }
  
                      // update in the Mail Server
                      UpdatedMessage um = this.ied.updateEmail(FID, FMID,
                                                      this.imsw.folderInboxOpened,
                                                      msgNew, fp);
  
                      long uid = um.getFMID();
  
                      long uidv = this.imsw.folderInboxOpened.getUIDValidity();
  
                      String GUID = Utility.createIMAPGUID(FID,
                                                           String.valueOf(uid),
                                                           String.valueOf(uidv));
  
                      emailToUpdate.setUID(new Property(GUID));
  
                      timeStop = System.currentTimeMillis();
                      if (log.isTraceEnabled()) {
                          log.trace("updateItem Execution Time: " +
                                  (timeStop - timeStart) + " ms");
                      }
  
                      // set the values for the post update operation
                      long crc = createInfo(um, protocol);
  
                      // update the fnbl_email_inbox
                      this.ied.updateCRCInDB(crc, username, protocol, GUID);
  
                      // update email to the servetItems list
                      this.ied.updateCRCInServerItems(crc, GUID, serverItems);
  
                  } catch (MessagingException me) {
                      throw new EntityException(me);
                  } catch (EntityException e) {
                      throw new EntityException(e);
                  }
  
              } else {
  
                  // SENT - DRFATS - TRASH - OUTBOX
  
                  IMAPFolder f = null;
  
                  try {
  
                      timeStart = System.currentTimeMillis();
  
                      f = (IMAPFolder) this.imsw.getMailDefaultFolder().getFolder(fullpath);
  
                      if (f.exists()) {
  
                          if (!f.isOpen()){
                             f.open(Folder.READ_WRITE);
                          }
  
                          // update in the Mail Server
                          UpdatedMessage um = this.ied.updateEmail(FID, FMID, f,
                                                                   msgNew, fp);
  
                          long uid = um.getFMID();
  
                          long uidv = f.getUIDValidity();
  
                          String GUID = Utility.createIMAPGUID(FID,
                                                               String.valueOf(uid),
                                                               String.valueOf(uidv));
  
                          emailToUpdate.setUID(new Property(GUID));
  
  
                          // set the values for the post update operation
                          SyncItemInfo sii = createInfo(GUID, null, um, Def.PROTOCOL_IMAP);
  
                          // update email to the servetItems list
                          this.ied.updateItemInServerItems(sii, GUID, serverItems);
  
                      }
  
                  } catch (MessagingException me) {
                      throw new EntityException(me);
                  } catch (EntityException e) {
                      throw new EntityException(e);
                  } finally {
                      if (f != null) {
                          try {
                              if (f.isOpen()) {
                                  f.close(true);
                                  timeStop = System.currentTimeMillis();
                                  if (log.isTraceEnabled()) {
                                      log.trace("updateItem Execution Time: " +
                                                 (timeStop - timeStart) + " ms");
                                  }
                              }
                          } catch (MessagingException me) {
                              throw new EntityException(me);
                          }
                      }
                  }
              }
          }
  
          return emailToUpdate;
  
      }
  
      /**
       * updates a folder defined by folder id and parent id
       *
       * @param parentId  folder id String
       * @param folderId  mail id in folder String
       * @param folderNew Folder
       * @return Folder
       * @throws EntityException
       */
      public com.funambol.email.pdi.folder.Folder updateFolder(
              String parentId,
              String folderId,
              com.funambol.email.pdi.folder.Folder folderNew,
              Map serverItems)
              throws EntityException {
  
          com.funambol.email.pdi.folder.Folder folderOut = null;
  
          if (log.isTraceEnabled()) {
              log.trace("updateFolder start for FID: " + folderId);
          }
  
          String name       = folderNew.getName().getPropertyValueAsString();
          String created    = folderNew.getCreated().getPropertyValueAsString();
          String modified   = folderNew.getModified().getPropertyValueAsString();
          String accessed   = folderNew.getAccessed().getPropertyValueAsString();
          String attributes = null;
  
          ItemFolder ifout = null;
  
          if        (folderId.equals(Def.FOLDER_INBOX_ID)){
              ifout =  new ItemFolder(Def.FOLDER_INBOX_GUID,
                                      name,
                                      Def.FOLDER_ROOT_ID,
                                      created,
                                      accessed,
                                      modified,
                                      Def.FOLDER_INBOX_ROLE,
                                      attributes);
              // set the values for the post update operation
              SyncItemInfo sii = createInfo(Def.FOLDER_INBOX_GUID);
              // update email to the servetItems list
              this.ied.updateItemInServerItems(sii, Def.FOLDER_INBOX_GUID, serverItems);
          } else if (folderId.equals(Def.FOLDER_OUTBOX_ID)){
              ifout =  new ItemFolder(Def.FOLDER_OUTBOX_GUID,
                                      name,
                                      Def.FOLDER_ROOT_ID,
                                      created,
                                      accessed,
                                      modified,
                                      Def.FOLDER_OUTBOX_ROLE,
                                      attributes) ;
              // set the values for the post update operation
              SyncItemInfo sii = createInfo(Def.FOLDER_OUTBOX_ID);
              // update email to the servetItems list
              this.ied.updateItemInServerItems(sii, Def.FOLDER_OUTBOX_ID, serverItems);
          } else if (folderId.equals(Def.FOLDER_SENT_ID)){
              ifout =  new ItemFolder(Def.FOLDER_SENT_GUID,
                                      name,
                                      Def.FOLDER_ROOT_ID,
                                      created,
                                      accessed,
                                      modified,
                                      Def.FOLDER_SENT_ROLE,
                                      attributes) ;
              // set the values for the post update operation
              SyncItemInfo sii = createInfo(Def.FOLDER_SENT_ID);
              // update email to the servetItems list
              this.ied.updateItemInServerItems(sii, Def.FOLDER_SENT_ID, serverItems);
          } else if (folderId.equals(Def.FOLDER_DRAFTS_ID)){
              ifout =  new ItemFolder(Def.FOLDER_DRAFTS_GUID,
                                      name,
                                      Def.FOLDER_ROOT_ID,
                                      created,
                                      accessed,
                                      modified,
                                      Def.FOLDER_DRAFTS_ROLE,
                                      attributes) ;
              // set the values for the post update operation
              SyncItemInfo sii = createInfo(Def.FOLDER_DRAFTS_ID);
              // update email to the servetItems list
              this.ied.updateItemInServerItems(sii, Def.FOLDER_DRAFTS_ID, serverItems);
          } else if (folderId.equals(Def.FOLDER_TRASH_ID)){
              ifout =  new ItemFolder(Def.FOLDER_TRASH_GUID,
                                      name,
                                      Def.FOLDER_ROOT_ID,
                                      created,
                                      accessed,
                                      modified,
                                      Def.FOLDER_TRASH_ROLE,
                                      attributes) ;
              // set the values for the post update operation
              SyncItemInfo sii = createInfo(Def.FOLDER_TRASH_GUID);
              // update email to the servetItems list
              this.ied.updateItemInServerItems(sii, Def.FOLDER_TRASH_GUID, serverItems);
  
          }
  
  
          // convert Folder in foundation Folder
          try {
              folderOut = createFoundationFolder(ifout);
          } catch (Exception e) {
              throw new EntityException(e);
          }
  
          if (log.isTraceEnabled()) {
              log.trace("updateFolder end");
          }
  
  
          return folderOut;
  
      }
  
  
      /**
       * clean all folder and email except the 5 main folders
       *
       * @param allMailboxActivation boolean
       * @param source_uri           String
       * @param principalId          long
       * @throws EntityException
       */
      public void clearAllItems(boolean allMailboxActivation,
              String source_uri,
              long principalId,
              String username)
              throws EntityException {
  
          IMAPFolder f = null;
          EntityException finalExc = null;
  
          String[] folders = {this.imsw.getDefaultFolder().getInboxName(),
          this.imsw.getDefaultFolder().getOutboxName(),
          this.imsw.getDefaultFolder().getSentName(),
          this.imsw.getDefaultFolder().getDraftsName(),
          this.imsw.getDefaultFolder().getTrashName()};
  
          String folderName;
  
          // clean all emails in the main folders
          for (int y = 0; y < folders.length; y++) {
              folderName = folders[y];
              try {
                  f = (IMAPFolder)
                  this.imsw.getMailDefaultFolder().getFolder(folderName);
                  if (log.isTraceEnabled()) {
                      log.trace("ImapEmail Delete all emails in " + folderName);
                  }
                  if (f.exists()) {
                      timeStart = System.currentTimeMillis();
                      f.open(Folder.READ_WRITE);
                      this.ied.removeAllEmail(f);
                  }
              } catch (MessagingException me) {
                  throw new EntityException(me);
              } finally {
                  if (f != null) {
                      try {
                          if (f.isOpen()) {
                              f.close(true);
                              timeStop = System.currentTimeMillis();
                              if (log.isTraceEnabled()) {
                                  log.trace("insertItem Execution Time: " +
                                          (timeStop - timeStart) + " ms");
                              }
                          }
                      } catch (MessagingException me) {
                          finalExc = new EntityException(me);
                      }
                  }
              }
              if (finalExc != null) {
                  throw finalExc;
              }
          }
  
          // clean all folders in the main folders
          // only if allMalboxActivation is on
          if (allMailboxActivation) {
  
              ArrayList fs;
              String tmpname;
              String GUID;
              IMAPFolder tmpfolder;
  
              for (int y = 0; y < folders.length; y++) {
  
                  folderName = folders[y];
  
                  try {
                      f = (IMAPFolder)
                      this.imsw.getMailDefaultFolder().getFolder(folderName);
                      if (log.isTraceEnabled()) {
                          log.trace("ImapEmail Delete all folders in " + folderName);
                      }
                      if (f.exists()) {
                          timeStart = System.currentTimeMillis();
                          f.open(Folder.READ_WRITE);
  
                          fs = new ArrayList();
                          this.ied.searchSubFolderFullNames(f, fs);
  
                          for (int i = 0; i < fs.size(); i++) {
  
                              tmpname = (String) fs.get(i);
  
                              if (log.isTraceEnabled()) {
                                  log.trace("remove folder " + tmpname);
                              }
  
                              // remove folder from Mail Server
                              tmpfolder = (IMAPFolder)
                              this.imsw.getMailDefaultFolder().getFolder(tmpname);
                              if (tmpfolder.exists()) {
                                  // the folder must be closed
                                  this.ied.removeFolderInServer(tmpfolder);
                              }
  
                              // remove folder from Local DB
                              GUID = this.ied.getGUIDFromFullPath(Def.SEPARATOR_FIRST + tmpname,
                                      source_uri,
                                      principalId,
                                      username);
  
                              this.ied.removeFolderInDB(GUID, source_uri, principalId, username);
  
                          }
  
                      }
                  } catch (MessagingException me) {
                      throw new EntityException(me);
                  } finally {
                      if (f != null) {
                          try {
                              if (f.isOpen()) {
                                  f.close(true);
                                  timeStop = System.currentTimeMillis();
                                  if (log.isTraceEnabled()) {
                                      log.trace("insertItem Execution Time: " +
                                              (timeStop - timeStart) + " ms");
                                  }
                              }
                          } catch (MessagingException me) {
                              finalExc = new EntityException(me);
                          }
                      }
                  }
                  if (finalExc != null) {
                      throw finalExc;
                  }
              }
          }
  
  
      }
  
  
      /**
       *
       *
       * @param FID folder id String
       * @param LUID  String
       * @param emailToAdd  Email
       * @param filter EmailFilter
       * @param funSignature  String
       * @param from  String
       * @param firstname  String
       * @param lastname  String
       * @param username  String
       * @param source_uri  String
       * @param principalId long
       * @return Email
       * @throws EntityException
       */
      public Email addEmail(String FID,
                            String LUID,
                            Email emailToAdd,
                            Map serverItems,
                            EmailFilter filter,
                            String funSignature,
                            String from,
                            String firstname,
                            String lastname,
                            String username,
                            String source_uri,
                            long principalId)
        throws EntityException, SendingException, RefusedItemException {
  
  
          IMAPFolder      f        = null;
          String          fullpath = null;
  
          // the emailItem could be null
          // i.e. the client send only the read property
          FlagProperties fp = Utility.getFlags(emailToAdd);
  
          if (emailToAdd != null &&
              emailToAdd.getEmailItem() != null &&
              Def.ENCODE_QUOTED_PRINTABLE.equals(emailToAdd.getEmailItem().getEncoding())) {
              try {
                  emailToAdd.getEmailItem().setPropertyValue(
                          Utility.decode(emailToAdd.getEmailItem().getStringRFC2822(),
                          Def.ENCODE_QUOTED_PRINTABLE));
                  emailToAdd.getEmailItem().setEncoding(null);
              } catch (MessagingException e) {
                  throw new EntityException(e);
              }
          }
  
          try {
  
              if (FID.equalsIgnoreCase(Def.FOLDER_OUTBOX_ID)){
  
                  // OUTBOX - sending procedure (email with id starts with "O")
  
                  FID = Def.FOLDER_SENT_ID;
  
                  // convert Email into Message (javaMail)
                  Message msg = createMessage(this.imsw.getSession(), emailToAdd,
                                              FID, false, fp);
  
                  // save message-ID
                  String messageID = Utility.getHeaderMessageID(msg);
  
                  // send message
                  Message newMsg = null;
                  try {
                     newMsg = sendEmail(msg, this.imsw.getSession(), funSignature,
                                        from, firstname, lastname);
                  } catch (SendingException se) {
                     throw new SendingException(se);
                  }
  
                  if (newMsg != null){
                      if (messageID != null){
                          newMsg.setHeader(Def.HEADER_MESSAGE_ID,messageID);
                      }
                      newMsg.setFlag(Flags.Flag.SEEN, true);
                  } else {
                      if (messageID != null){
                          msg.setHeader(Def.HEADER_MESSAGE_ID,messageID);
                      }
                  }
  
                  if (Def.SERVER_TYPE_AOL.equals(this.serverType) ||
                      Def.SERVER_TYPE_GMAIL.equals(this.serverType)) {
                      
                      //
                      // The email is stored in the SENT folder by the AOL server
                      // and Gmail imap.
                      //
                      
                      // don't save the email in the sent folder
                      emailToAdd.setParentId(new Property(Def.FOLDER_OUTBOX_ID));
  
                      String GUID = Utility.createIMAPGUID(Def.FOLDER_OUTBOX_ID,
                                                           String.valueOf(LUID),
                                                           String.valueOf(0) );
                      emailToAdd.setUID(new Property(GUID));
  
                      // if the item is not saved in the sent folder the system
                      // doesn't add it into the servetItems list
  
                      return emailToAdd;
                      
                  } else {
  
                      // the SENT folder could be disable
                      // if it's enabled the system adds the item in the sent
                      // folder of the Mail Server
                      
                      // get Sent folder name
                      fullpath = this.ied.getFullPathFromFID(FID, source_uri, principalId, username);
                      if (fullpath == null) {
                          emailToAdd.setParentId(new Property(Def.FOLDER_OUTBOX_ID));
                          String GUID = Utility.createIMAPGUID(Def.FOLDER_OUTBOX_ID,
                                                               String.valueOf(LUID),
                                                               String.valueOf(0) );
                          emailToAdd.setUID(new Property(GUID));
                          return emailToAdd;
                      }
                      
                      // open Sent Folder on mail server
                      f = (IMAPFolder)  this.imsw.getMailDefaultFolder().getFolder(fullpath);
                      
                      if (!f.exists()) {
                          emailToAdd.setParentId(new Property(Def.FOLDER_OUTBOX_ID));
                          String GUID = Utility.createIMAPGUID(Def.FOLDER_OUTBOX_ID,
                                                               String.valueOf(LUID),
                                                               String.valueOf(0) );
                          emailToAdd.setUID(new Property(GUID));
                          return emailToAdd;
                      }
                      
                      timeStart = System.currentTimeMillis();
                      
                      f.open(Folder.READ_WRITE);
                      
                      long uid = -1;
                      if(this.serverType.equals(Def.SERVER_TYPE_COURIER)) {
                          if (newMsg != null){
                              uid = this.ied.addEmailByNextUID(f, newMsg);
                          } else {
                              uid = this.ied.addEmailByNextUID(f, msg);                            
                          }
                      } else {
                          if (newMsg != null){
                              uid = this.ied.addEmailByListener(f, newMsg);
                          } else {
                              uid = this.ied.addEmailByListener(f, msg);                            
                          }                        
                      }
                      
                      emailToAdd.setParentId(new Property(Def.FOLDER_OUTBOX_ID));
                      
                      long uidv = f.getUIDValidity();
                      
                      String GUID = Utility.createIMAPGUID(Def.FOLDER_OUTBOX_ID,
                              String.valueOf(uid),
                              String.valueOf(uidv) );
                      
                      emailToAdd.setUID(new Property(GUID));
                      
                      // create the object to add in db and in serverItems
                      SyncItemInfo sii = createInfo(GUID, msg, null, Def.PROTOCOL_IMAP);
                      
                      // add email to the servetItems list
                      this.ied.addItemInServerItems(sii, serverItems);
                      
                      return emailToAdd;
                  }
                  
              } else {
  
                  /**
                   * this procedure should be tested with client that syncs
                   * items into the the folders: INBOX - SENT - DRAFT - TRASH
                   * At the moment the Funambol client doesn't sync the
                   * previous folders.
                   */
  
                  // INBOX - SENT - DRAFT - TRASH
  
                  if (this.isFolderActive(FID, filter)){
  
                      fullpath = this.ied.getFullPathFromFID(FID, source_uri, principalId, username);
  
                      if (fullpath == null) {
                          return emailToAdd;
                      }
  
                      f = (IMAPFolder)this.imsw.getMailDefaultFolder().getFolder(fullpath);
  
                      if (!f.exists()) {
                          //emailToAdd.setUID(new Property(LUID));
                          return emailToAdd;
                      }
  
                      // convert Email into Message (javaMail)
                      Message msg = createMessage(this.imsw.getSession(),
                                                  emailToAdd, FID, false, fp);
                      
                      checkIfItHasToBeRefused(msg);
  
                      // add in the folder of the Mail Server
                      timeStart = System.currentTimeMillis();
  
                      f.open(Folder.READ_WRITE);
  
                      long uid = -1;
                      if(this.serverType.equals(Def.SERVER_TYPE_COURIER)) {
                          uid = this.ied.addEmailByNextUID(f, msg);
                      } else {
                          uid = this.ied.addEmailByListener(f, msg);
                      }
  
                      long uidv = f.getUIDValidity();
  
                      String GUID = Utility.createIMAPGUID(FID,
                                                           String.valueOf(uid),
                                                           String.valueOf(uidv) );
  
                      emailToAdd.setUID(new Property(GUID));
  
  
                      // create the object to add in db and in serverItems
                      SyncItemInfo sii = createInfo(GUID, msg, null, Def.PROTOCOL_IMAP);
  
                      if (FID.equalsIgnoreCase(Def.FOLDER_INBOX_ID)){
                         // add email into the inbox table
  