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