KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > communication > message > impl > MessageServiceImpl


1 /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

5 package org.exoplatform.services.communication.message.impl;
6
7 import java.util.*;
8 import net.sf.hibernate.Hibernate;
9 import net.sf.hibernate.Session;
10 import net.sf.hibernate.type.Type;
11 import org.apache.commons.logging.Log;
12 import org.exoplatform.commons.exception.ExoMessageException;
13 import org.exoplatform.commons.utils.IdentifierUtil;
14 import org.exoplatform.commons.utils.PageList;
15 import org.exoplatform.services.communication.message.*;
16 import org.exoplatform.services.database.DBObjectPageList;
17 import org.exoplatform.services.database.HibernateService;
18 import org.exoplatform.services.log.LogService;
19 /**
20  * @author Tuan Nguyen (tuan08@users.sourceforge.net)
21  * @author Benjamin Mestrallet (benjamin.mestrallet@exoplatform.com)
22  * @since Aug 28, 2004
23  * @version $Id: MessageServiceImpl.java,v 1.17 2004/11/03 04:24:56 tuan08 Exp $
24  */

25 public class MessageServiceImpl implements MessageService {
26   private static String JavaDoc[] MAPPING =
27   {
28     "org/exoplatform/services/communication/message/impl/AccountImpl.hbm.xml" ,
29     "org/exoplatform/services/communication/message/impl/FolderImpl.hbm.xml" ,
30     "org/exoplatform/services/communication/message/impl/MessageHeaderImpl.hbm.xml" ,
31     "org/exoplatform/services/communication/message/impl/MessageImpl.hbm.xml" ,
32     "org/exoplatform/services/communication/message/impl/AttachmentImpl.hbm.xml"
33   };
34   
35   private static final String JavaDoc queryAccountByName =
36     "from acc in class org.exoplatform.services.communication.message.impl.AccountImpl " +
37     "where acc.owner = ? and acc.accountName = ?";
38
39   private static final String JavaDoc queryFoldersByAccount =
40     "from folder in class org.exoplatform.services.communication.message.impl.FolderImpl " +
41     "where folder.accountId = ? order by folder.createdDate asc";
42
43     private static final String JavaDoc queryFolderByName =
44         "from folder in class org.exoplatform.services.communication.message.impl.FolderImpl " +
45         "where folder.accountId = ? and folder.name = ?";
46
47     private static final String JavaDoc queryAttachmentByName =
48         "from attachment in class org.exoplatform.services.communication.message.impl.AttachmentImpl " +
49         "where attachment.messageId = ? and attachment.name = ?";
50
51   private static final String JavaDoc queryAttachmentsByMessage =
52     "from attachment in class org.exoplatform.services.communication.message.impl.AttachmentImpl " +
53     "where attachment.messageId = ?";
54
55   private Log log_ ;
56   private HibernateService hservice_ ;
57   private Map plugins_ ;
58   private MessageIndexerPluginImpl indexer_ ;
59   private String JavaDoc[] supportedFlags_ = MessageHeader.SUPPORTED_FLAGS ;
60
61   public MessageServiceImpl(HibernateService hservice,
62                             LogService lservice) throws Exception JavaDoc {
63     hservice.addMappingFiles(MAPPING) ;
64     log_ = lservice.getLog(getClass()) ;
65     hservice_ = hservice ;
66     plugins_ = new HashMap() ;
67   }
68
69   void setIndexer(MessageIndexerPluginImpl indexer) {
70     indexer_ = indexer ;
71   }
72   
73   public Account createAccountInstance() { return new AccountImpl() ; }
74   
75   public Account getAccount(String JavaDoc userName , String JavaDoc accountName) throws Exception JavaDoc {
76     Object JavaDoc[] args = new Object JavaDoc[]{userName, accountName};
77         Type[] types = new Type[]{Hibernate.STRING, Hibernate.STRING};
78         List l = hservice_.openSession().find(queryAccountByName, args, types);
79      if(l.size() == 0) {
80         Object JavaDoc[] errorargs = {accountName, userName} ;
81         throw new ExoMessageException("MessageService.account-not-found", errorargs) ;
82       }
83     return (Account) l.get(0);
84   }
85   
86   public List getAccounts(String JavaDoc userName) throws Exception JavaDoc {
87     String JavaDoc queryAccountsByOwner =
88       "from account in class org.exoplatform.services.communication.message.impl.AccountImpl " +
89       "where account.owner = '" + userName + "'";
90     Session session = hservice_.openSession();
91     return session.find(queryAccountsByOwner);
92   }
93
94   public Account removeAccount(Account account) throws Exception JavaDoc {
95     Session session = hservice_.openSession();
96     List folders =
97       session.find(queryFoldersByAccount, account.getId(), Hibernate.STRING);
98     for(int i = 0; i < folders.size(); i++) {
99       FolderImpl folderImpl = (FolderImpl)folders.get(i) ;
100       removeFolder(session, folderImpl) ;
101     }
102     session.delete(account) ;
103     session.flush() ;
104     indexer_.removeAccount(account) ;
105     return account ;
106   }
107
108   public void createAccount(Account account) throws Exception JavaDoc {
109     Session session = hservice_.openSession() ;
110     createAccount(session, account) ;
111     session.flush() ;
112   }
113   
114   public void createAccount(Session session, Account account) throws Exception JavaDoc {
115     AccountImpl impl = (AccountImpl) account ;
116     impl.setId(IdentifierUtil.generateUUID(impl));
117     session.save(impl);
118     long time = System.currentTimeMillis() ;
119     FolderImpl inbox = new FolderImpl(INBOX_FOLDER, INBOX_FOLDER , impl.getId()) ;
120     inbox.setId(IdentifierUtil.generateUUID(inbox)) ;
121     inbox.setRemoveable(false) ;
122     inbox.setCreatedDate(new Date(time)) ;
123     session.save(inbox) ;
124     FolderImpl sent = new FolderImpl(SENT_FOLDER, SENT_FOLDER , impl.getId()) ;
125     sent.setId(IdentifierUtil.generateUUID(sent)) ;
126     sent.setRemoveable(false) ;
127     sent.setCreatedDate(new Date(time + 1000)) ;
128     session.save(sent) ;
129     FolderImpl trash = new FolderImpl(TRASH_FOLDER, TRASH_FOLDER , impl.getId()) ;
130     trash.setId(IdentifierUtil.generateUUID(trash)) ;
131     trash.setRemoveable(false) ;
132     trash.setCreatedDate(new Date(time + 2000)) ;
133     session.save(trash) ;
134     FolderImpl archived = new FolderImpl(ARCHIVED_FOLDER, ARCHIVED_FOLDER , impl.getId()) ;
135     archived.setId(IdentifierUtil.generateUUID(archived)) ;
136     archived.setRemoveable(false) ;
137     archived.setCreatedDate(new Date(time + 3000)) ;
138     session.save(archived) ;
139   }
140   
141   public void updateAccount(Account account) throws Exception JavaDoc {
142     hservice_.update(account) ;
143   }
144
145   public int countNewMessages(Account account) throws Exception JavaDoc {
146     Folder folder = getFolder(account, INBOX_FOLDER) ;
147     Session session = hservice_.openSession() ;
148     String JavaDoc query =
149       "select count(m) from org.exoplatform.services.communication.message.impl.MessageImpl m " +
150       "where m.folderId = '" + folder.getId() + "' "+
151       "and m.flags like '%"+ MessageHeader.RECENT_FLAG +"%'";
152     List l = session.find(query) ;
153     Integer JavaDoc count = (Integer JavaDoc) l.get(0) ;
154     return count.intValue() ;
155   }
156   
157   public Folder createFolderInstance() { return new FolderImpl() ; }
158
159   public Folder getFolder(Account account, String JavaDoc folderName) throws Exception JavaDoc {
160         Object JavaDoc[] args = new Object JavaDoc[]{account.getId(), folderName};
161         Type[] types = new Type[]{Hibernate.STRING, Hibernate.STRING};
162         List l = hservice_.openSession().find(queryFolderByName, args, types);
163         if (l.size() == 0) {
164             throw new Exception JavaDoc("Folder is not found");
165         }
166     return (Folder) l.get(0);
167   }
168   
169   public List getFolders(String JavaDoc accountId) throws Exception JavaDoc {
170     Session session = hservice_.openSession();
171     return session.find(queryFoldersByAccount, accountId, Hibernate.STRING);
172   }
173
174   public Folder removeFolder(Folder folder) throws Exception JavaDoc {
175     FolderImpl impl = (FolderImpl) folder ;
176     if(!impl.getRemoveable()) {
177       throw new Exception JavaDoc("Cannot remove folder: inbox, sent, trash, archive") ;
178     }
179     Session session = hservice_.openSession();
180     removeFolder(session, impl) ;
181     session.flush() ;
182     return impl ;
183   }
184   
185   private FolderImpl removeFolder(Session session, FolderImpl folder) throws Exception JavaDoc {
186     String JavaDoc deleteMessageInFolder =
187         "from m in class org.exoplatform.services.communication.message.impl.MessageImpl " +
188         "where m.folderId = '" + folder.getId() + "'";
189     String JavaDoc deleteAttachmentInFolder =
190         "from att in class org.exoplatform.services.communication.message.impl.AttachmentImpl " +
191         "where att.folderId = '" + folder.getId() + "'" ;
192     session.delete(deleteMessageInFolder);
193     session.delete(deleteAttachmentInFolder);
194     session.delete(folder) ;
195     indexer_.removeFolder(folder) ;
196     return folder ;
197   }
198   
199   public void createFolder(Account account, Folder folder) throws Exception JavaDoc {
200     FolderImpl impl = (FolderImpl) folder;
201     impl.setId(IdentifierUtil.generateUUID(impl));
202     impl.setAccountId(account.getId());
203     impl.setCreatedDate(new Date()) ;
204     impl.setRemoveable(true) ;
205     hservice_.create(impl);
206   }
207
208   public void updateFolder(Folder folder) throws Exception JavaDoc {
209     hservice_.save(folder) ;
210   }
211   
212   public Message createMessageInstance() { return new MessageImpl() ; }
213
214   public PageList getMessages(Folder folder) throws Exception JavaDoc {
215     String JavaDoc queryMessageByFolder =
216       "from message in class org.exoplatform.services.communication.message.impl.MessageImpl " +
217       "where message.folderId = '" + folder.getId() + "' order by message.receivedDate desc";
218     String JavaDoc countMessageInFolder =
219       "select count(m) " +
220       "from m in class org.exoplatform.services.communication.message.impl.MessageImpl " +
221       "where m.folderId = '" + folder.getId() + "'";
222     return new DBObjectPageList(hservice_, 15,queryMessageByFolder, countMessageInFolder ) ;
223   }
224
225   public Message getMessage(String JavaDoc messageId) throws Exception JavaDoc {
226         return (Message)hservice_.findOne(MessageImpl.class, messageId) ;
227   }
228
229   public void moveMessage(Account account, Folder folder, Message message) throws Exception JavaDoc {
230     FolderImpl folderImpl = (FolderImpl) folder ;
231     MessageImpl messageImpl = (MessageImpl) message ;
232     String JavaDoc oldFolderId = messageImpl.getFolderId() ;
233     messageImpl.setFolderId(folderImpl.getId()) ;
234     hservice_.save(messageImpl) ;
235     indexer_.moveMesasge(account, oldFolderId, messageImpl) ;
236   }
237   
238   public Message removeMessage(Message message) throws Exception JavaDoc {
239     Session session = hservice_.openSession();
240     session.delete(queryAttachmentsByMessage, ((MessageImpl)message).getId(), Hibernate.STRING);
241     session.delete(message) ;
242     session.flush() ;
243     indexer_.removeMessage(message) ;
244     return message;
245   }
246
247   public void createMessage(Account account, Folder folder, Message message) throws Exception JavaDoc {
248     Session session = hservice_.openSession() ;
249     MessageImpl messageImpl = (MessageImpl) message;
250     FolderImpl folderImpl = (FolderImpl)folder ;
251     messageImpl.setId(IdentifierUtil.generateUUID(messageImpl)) ;
252     messageImpl.setFolderId(folderImpl.getId()) ;
253     session.save(messageImpl) ;
254     List attachments = messageImpl.getAttachment() ;
255     if(attachments != null) {
256       for(int i = 0; i < attachments.size(); i++) {
257         AttachmentImpl attachment = (AttachmentImpl) attachments.get(i) ;
258         attachment.setMessageId(messageImpl.getId()) ;
259         attachment.setFolderId(messageImpl.getFolderId()) ;
260         attachment.setId(IdentifierUtil.generateUUID(attachment)) ;
261         session.save(attachment) ;
262       }
263     }
264     session.flush() ;
265     indexer_.createMesasge(account, messageImpl) ;
266   }
267   
268   public void updateMessage(Message message) throws Exception JavaDoc {
269     hservice_.update(message) ;
270   }
271   
272   public List searchMessages(Folder folder, SearchTerm term) {
273     return null ;
274   }
275   
276   public void sendMessage(Account account, Message message) throws Exception JavaDoc {
277     MessageProtocolPlugin plugin =
278       (MessageProtocolPlugin)plugins_.get(account.getProtocol()) ;
279     plugin.sendMessage(account, message) ;
280   }
281
282   public Attachment createAttachment() { return new AttachmentImpl(); }
283
284   public List getAttachments(Message message) throws Exception JavaDoc {
285     Session session = hservice_.openSession();
286     return session.find(queryAttachmentsByMessage, ((MessageImpl)message).getId(), Hibernate.STRING);
287   }
288
289   public void synchronizeAccount(Account account) throws Exception JavaDoc {
290     MessageProtocolPlugin plugin =
291       (MessageProtocolPlugin)plugins_.get(account.getProtocol()) ;
292     plugin.synchronize(account) ;
293   }
294   
295   public synchronized void addMessageProtocolPlugin(MessageProtocolPlugin plugin) {
296     plugins_.put(plugin.getProtocol(), plugin) ;
297   }
298   
299   public Collection getMessageProtocolPlugins() { return plugins_.values() ; }
300   
301   public String JavaDoc[] getSupportedFlags() { return supportedFlags_ ; }
302 }
303
Popular Tags