Source Code Cross Referenced for ImapFolderControllerImpl.java in  » Chat » claros-chat-1.0 » org » claros » mini » controllers » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Chat » claros chat 1.0 » org.claros.mini.controllers 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.claros.mini.controllers;
002:
003:        import java.util.ArrayList;
004:        import java.util.Collections;
005:        import java.util.Comparator;
006:        import java.util.List;
007:
008:        import javax.mail.Folder;
009:        import javax.mail.MessagingException;
010:
011:        import org.apache.commons.logging.Log;
012:        import org.apache.commons.logging.LogFactory;
013:        import org.claros.commons.exception.SystemException;
014:        import org.claros.commons.mail.models.ConnectionMetaHandler;
015:        import org.claros.commons.mail.models.ConnectionProfile;
016:        import org.claros.commons.mail.protocols.ImapProtocolImpl;
017:        import org.claros.commons.mail.protocols.Protocol;
018:        import org.claros.commons.mail.protocols.ProtocolFactory;
019:        import org.claros.commons.models.AuthProfile;
020:        import org.claros.mini.models.FolderDbItem;
021:        import org.claros.mini.models.FolderDbItemWrapper;
022:        import org.claros.mini.utility.Constants;
023:
024:        /**
025:         * @author Umut Gokbayrak
026:         */
027:        public class ImapFolderControllerImpl implements  FolderController {
028:            private AuthProfile auth;
029:            private ConnectionProfile profile;
030:            private ConnectionMetaHandler handler;
031:            private static Log log = LogFactory
032:                    .getLog(ImapFolderControllerImpl.class);
033:
034:            /**
035:             * @param auth
036:             * @param profile
037:             * @param handler
038:             */
039:            public ImapFolderControllerImpl(AuthProfile auth,
040:                    ConnectionProfile profile, ConnectionMetaHandler handler) {
041:                this .auth = auth;
042:                this .profile = profile;
043:                this .handler = handler;
044:            }
045:
046:            /**
047:             * used to disable it.
048:             */
049:            private ImapFolderControllerImpl() {
050:                super ();
051:            }
052:
053:            /* (non-Javadoc)
054:             * @see org.claros.groupware.webmail.controllers.FolderController#getFolders()
055:             */
056:            public List getFolders() throws Exception {
057:                ProtocolFactory factory = new ProtocolFactory(profile, auth,
058:                        handler);
059:                ImapProtocolImpl protocol = (ImapProtocolImpl) factory
060:                        .getImap(null);
061:                Folder folders[] = protocol.listFolders();
062:
063:                ArrayList res = new ArrayList();
064:                boolean inboxAdded = false;
065:                if (folders != null) {
066:                    Folder tmp = null;
067:                    String n, fn = null;
068:                    for (int i = 0; i < folders.length; i++) {
069:                        try {
070:                            tmp = folders[i];
071:                            if (tmp != null) {
072:                                n = tmp.getName();
073:                                fn = tmp.getFullName();
074:                                if (fn != null && fn.startsWith("INBOX.")) {
075:                                    //	n = fn;
076:                                }
077:                                Integer type = determineFolderType(n);
078:                                if (type.equals(Constants.FOLDER_TYPE_INBOX)) {
079:                                    inboxAdded = true;
080:                                }
081:                                FolderDbItem item = new FolderDbItem(null,
082:                                        null, auth.getUsername(), n, type);
083:                                FolderDbItemWrapper wr = new FolderDbItemWrapper(
084:                                        item);
085:                                wr.setUnreadItemCount(new Integer(tmp
086:                                        .getUnreadMessageCount()));
087:                                wr.setTotalItemCount(new Integer(tmp
088:                                        .getMessageCount()));
089:                                res.add(wr);
090:                            }
091:                        } catch (MessagingException e) {
092:                            // do not worry about this. 
093:                            log.debug(e);
094:                        }
095:                    }
096:                }
097:
098:                // inbox is not added in this server implementation(weird!!!) . Please add it.
099:                if (!inboxAdded) {
100:                    FolderDbItem item = new FolderDbItem(
101:                            null,
102:                            null,
103:                            auth.getUsername(),
104:                            org.claros.commons.mail.utility.Constants.FOLDER_INBOX
105:                                    .toUpperCase(), Constants.FOLDER_TYPE_INBOX);
106:                    FolderDbItemWrapper wr = new FolderDbItemWrapper(item);
107:                    Folder inbox = protocol.getFolder();
108:                    if (inbox.exists()) {
109:                        wr.setUnreadItemCount(new Integer(inbox
110:                                .getUnreadMessageCount()));
111:                        wr.setTotalItemCount(new Integer(inbox
112:                                .getMessageCount()));
113:                        res.add(wr);
114:                    }
115:                }
116:
117:                Collections.sort(res, new Comparator() {
118:                    public int compare(Object f1, Object f2) {
119:                        FolderDbItemWrapper fw1 = (FolderDbItemWrapper) f1;
120:                        FolderDbItemWrapper fw2 = (FolderDbItemWrapper) f2;
121:
122:                        Integer t1 = fw1.getType();
123:                        Integer t2 = fw2.getType();
124:
125:                        if (t1.equals(Constants.FOLDER_TYPE_CUSTOM)
126:                                && t2.equals(Constants.FOLDER_TYPE_CUSTOM)) {
127:                            return fw1.getFolderName().compareTo(
128:                                    fw2.getFolderName());
129:                        } else {
130:                            return t1.compareTo(t2);
131:                        }
132:                    }
133:                });
134:
135:                return res;
136:            }
137:
138:            /**
139:             * 
140:             * @param folderName
141:             * @return
142:             */
143:            private Integer determineFolderType(String folderName) {
144:                if (folderName == null) {
145:                    return null;
146:                }
147:                if (folderName.toUpperCase().equals(
148:                        org.claros.commons.mail.utility.Constants.FOLDER_INBOX
149:                                .toUpperCase())) {
150:                    return Constants.FOLDER_TYPE_INBOX;
151:                } else if (folderName.toUpperCase().equals(
152:                        org.claros.commons.mail.utility.Constants.FOLDER_SENT
153:                                .toUpperCase())) {
154:                    return Constants.FOLDER_TYPE_SENT;
155:                } else if (folderName.toUpperCase().equals(
156:                        org.claros.commons.mail.utility.Constants.FOLDER_JUNK
157:                                .toUpperCase())) {
158:                    return Constants.FOLDER_TYPE_JUNK;
159:                } else {
160:                    return Constants.FOLDER_TYPE_CUSTOM;
161:                }
162:            }
163:
164:            /* (non-Javadoc)
165:             * @see org.claros.groupware.webmail.controllers.FolderController#getFolder(java.lang.String)
166:             */
167:            public FolderDbItem getFolder(String folder) throws Exception {
168:                ProtocolFactory factory = new ProtocolFactory(profile, auth,
169:                        handler);
170:                ImapProtocolImpl protocol = (ImapProtocolImpl) factory
171:                        .getImap(folder);
172:                Folder f = protocol.getFolder();
173:
174:                FolderDbItem item = null;
175:                if (f.exists()) {
176:                    item = new FolderDbItem(null, null, auth.getUsername(), f
177:                            .getName(), determineFolderType(f.getName()));
178:                }
179:                ImapProtocolImpl.closeFolder(f);
180:                return item;
181:            }
182:
183:            /* (non-Javadoc)
184:             * @see org.claros.groupware.webmail.controllers.FolderController#getMailsByFolder(java.lang.String)
185:             */
186:            public List getMailsByFolder(String folder) throws Exception {
187:                throw new SystemException(
188:                        "Method not supported at this protocol");
189:            }
190:
191:            /* (non-Javadoc)
192:             * @see org.claros.groupware.webmail.controllers.FolderController#createFolder(org.claros.groupware.webmail.models.FolderDbItem)
193:             */
194:            public void createFolder(FolderDbItem item) throws Exception {
195:                String name = item.getFolderName();
196:                ProtocolFactory factory = new ProtocolFactory(profile, auth,
197:                        handler);
198:                ImapProtocolImpl protocol = (ImapProtocolImpl) factory
199:                        .getImap(name);
200:                protocol.createFolder();
201:            }
202:
203:            /* (non-Javadoc)
204:             * @see org.claros.groupware.webmail.controllers.FolderController#countUnreadMessages(java.lang.String)
205:             */
206:            public Integer countUnreadMessages(String folder) throws Exception {
207:                ProtocolFactory factory = new ProtocolFactory(profile, auth,
208:                        handler);
209:                ImapProtocolImpl protocol = (ImapProtocolImpl) factory
210:                        .getImap(folder);
211:                int count = protocol.getUnreadMessageCount();
212:                return new Integer(count);
213:            }
214:
215:            /* (non-Javadoc)
216:             * @see org.claros.groupware.webmail.controllers.FolderController#countTotalMessages(java.lang.String)
217:             */
218:            public Integer countTotalMessages(String folder) throws Exception {
219:                ProtocolFactory factory = new ProtocolFactory(profile, auth,
220:                        handler);
221:                ImapProtocolImpl protocol = (ImapProtocolImpl) factory
222:                        .getImap(folder);
223:                int count = protocol.getTotalMessageCount();
224:                return new Integer(count);
225:            }
226:
227:            /* (non-Javadoc)
228:             * @see org.claros.groupware.webmail.controllers.FolderController#emptyFolder(java.lang.String)
229:             */
230:            public void emptyFolder(String folder) throws Exception {
231:                ProtocolFactory factory = new ProtocolFactory(profile, auth,
232:                        handler);
233:                ImapProtocolImpl protocol = (ImapProtocolImpl) factory
234:                        .getImap(folder);
235:                protocol.emptyFolder();
236:            }
237:
238:            /* (non-Javadoc)
239:             * @see org.claros.groupware.webmail.controllers.FolderController#deleteFolder(java.lang.String)
240:             */
241:            public void deleteFolder(String folder) throws Exception {
242:                ProtocolFactory factory = new ProtocolFactory(profile, auth,
243:                        handler);
244:                ImapProtocolImpl protocol = (ImapProtocolImpl) factory
245:                        .getImap(folder);
246:                protocol.deleteFolder();
247:            }
248:
249:            /* (non-Javadoc)
250:             * @see org.claros.groupware.webmail.controllers.FolderController#renameFolder(java.lang.String, java.lang.String)
251:             */
252:            public void renameFolder(String oldName, String newName)
253:                    throws Exception {
254:                ProtocolFactory factory = new ProtocolFactory(profile, auth,
255:                        handler);
256:                ImapProtocolImpl protocol = (ImapProtocolImpl) factory
257:                        .getImap(oldName);
258:                protocol.renameFolder(newName);
259:            }
260:
261:            /* (non-Javadoc)
262:             * @see org.claros.groupware.webmail.controllers.FolderController#getHeadersByFolder(java.lang.String)
263:             */
264:            public ArrayList getHeadersByFolder(String folder) throws Exception {
265:                ProtocolFactory factory = new ProtocolFactory(profile, auth,
266:                        handler);
267:                Protocol protocol = factory.getProtocol(folder);
268:                ArrayList headers = protocol.fetchAllHeaders();
269:
270:                return headers;
271:            }
272:
273:            /* (non-Javadoc)
274:             * @see org.claros.groupware.webmail.controllers.FolderController#createDefaultFolders()
275:             */
276:            public void createDefaultFolders() throws Exception {
277:                ProtocolFactory factory = new ProtocolFactory(profile, auth,
278:                        handler);
279:
280:                ImapProtocolImpl imapJunk = (ImapProtocolImpl) factory
281:                        .getImap(org.claros.commons.mail.utility.Constants.FOLDER_JUNK);
282:                imapJunk.createFolder();
283:
284:                ImapProtocolImpl imapSent = (ImapProtocolImpl) factory
285:                        .getImap(org.claros.commons.mail.utility.Constants.FOLDER_SENT);
286:                imapSent.createFolder();
287:            }
288:
289:            /**
290:             * @return
291:             * @throws Exception
292:             */
293:            public FolderDbItem getJunkFolder() throws Exception {
294:                return new FolderDbItem(null, null, null,
295:                        org.claros.commons.mail.utility.Constants.FOLDER_JUNK,
296:                        null);
297:            }
298:
299:            /**
300:             * @param auth
301:             * @return
302:             */
303:            public FolderDbItem getInboxFolder() throws Exception {
304:                return new FolderDbItem(null, null, null,
305:                        org.claros.commons.mail.utility.Constants.FOLDER_INBOX,
306:                        null);
307:            }
308:
309:            /**
310:             * @param auth
311:             * @return
312:             */
313:            public FolderDbItem getSentItems() throws Exception {
314:                return new FolderDbItem(null, null, null,
315:                        org.claros.commons.mail.utility.Constants.FOLDER_SENT,
316:                        null);
317:            }
318:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.