1
22
23 package com.liferay.portlet.mail.util.multiaccount;
24
25 import com.liferay.portal.kernel.util.StringPool;
26
27 import java.io.Serializable;
28
29 import javax.mail.Folder;
30 import javax.mail.Message;
31 import javax.mail.MessagingException;
32 import javax.mail.Store;
33
34 import org.apache.commons.lang.builder.ToStringBuilder;
35 import org.apache.commons.logging.Log;
36 import org.apache.commons.logging.LogFactory;
37
38
49 public class MailAccount implements Serializable {
50
51 public MailAccount(String accountName, long userId, String password,
52 String emailAddress) {
53
54 this(accountName, String.valueOf(userId), password, emailAddress);
55 }
56
57 public MailAccount(String accountName, String userId, String password,
58 String emailAddress) {
59
60 _name = accountName;
61 _userId = userId;
62 _password = password;
63 _emailAddress = emailAddress;
64 }
65
66 public String getName() {
67 return _name;
68 }
69
70 public String getUserId() {
71 return _userId;
72 }
73
74 public String getPassword() {
75 return _password;
76 }
77
78 public String getEmailAddress() {
79 return _emailAddress;
80 }
81
82 public String getRole() {
83 return _role;
84 }
85
86 public void setRole(String role) {
87 _role = role;
88 }
89
90 public Store getStore() {
91 return _store;
92 }
93
94 public void setStore(Store store) {
95 _store = store;
96 _size = calculateStoreSize(store);
97 }
98
99 public boolean isActive() {
100 return _active;
101 }
102
103 public void setActive(boolean active) {
104 _active = active;
105 }
106
107 public long getQuota() {
108 return _quota;
109 }
110
111 public void setQuota(long quota) {
112 _quota = quota;
113 }
114
115 public long getQuotaInMb() {
116 return Math.round((float)getQuota() / _MB);
117 }
118
119 public long getSize() {
120 return _size;
121 }
122
123 public long getSizeInMb() {
124 return _size / _MB;
125 }
126
127 public long getFreeSpace() {
128 return Math.max(0, getQuota() - getSize());
129 }
130
131 public long getFreeSpaceInMb() {
132 return Math.round((float)getFreeSpace() / _MB);
133 }
134
135 public long getFreeSpaceInPercentage() {
136 return Math.round(((float)getSize() / getQuota()) * 100);
137 }
138
139 private static long calculateStoreSize(Store store) {
140 return 1;
141
142
148 }
149
150 private static long calculateFolderSize(Folder folder)
151 throws MessagingException {
152
153 long size = 0;
154
155 if ((folder.getType() & Folder.HOLDS_MESSAGES) != 0) {
156 folder.open(Folder.READ_ONLY);
157
158 try {
159 Message[] folderMsgs = folder.getMessages();
160
161 for (int i = 0; i < folderMsgs.length; i++) {
162 int msgSize = folderMsgs[i].getSize();
163
164 if (msgSize != -1) {
165 size += msgSize;
166 }
167 else {
168 if (_log.isWarnEnabled()) {
169 _log.warn(
170 "Could not determine the size of message " +
171 folderMsgs[i].getSubject());
172 }
173 }
174 }
175 }
176 catch (Exception e) {
177 if (_log.isWarnEnabled()) {
178 _log.warn(
179 "Error calculating the size of the folder " +
180 folder.getName(),
181 e);
182 }
183 }
184 finally {
185 folder.close(false);
186 }
187 }
188
189 if ((folder.getType() & Folder.HOLDS_FOLDERS) != 0) {
190 Folder[] folderChildren = folder.list();
191
192 for (int i = 0; i < folderChildren.length; i++) {
193 size += calculateFolderSize(folderChildren[i]);
194 }
195 }
196
197 return size;
198 }
199
200 public String toString() {
201 return ToStringBuilder.reflectionToString(this);
202 }
203
204 public static final long _MB = 1024 * 1024;
205
206 private static Log _log = LogFactory.getLog(MailAccount.class);
207
208 private String _name;
209 private String _userId;
210 private String _password;
211 private String _emailAddress;
212 private String _role = StringPool.BLANK;
213 private Store _store;
214 private boolean _active = true;
215 private long _quota = 0;
216 private long _size = 0;
217
218 }