1
2
3
4
5
6
7
8
9
10
11 package com.atlassian.mail.server.managers;
12
13 import com.atlassian.core.ofbiz.CoreFactory;
14 import com.atlassian.core.ofbiz.util.EntityUtils;
15 import com.atlassian.core.user.preferences.DefaultPreferences;
16 import com.atlassian.mail.MailException;
17 import com.atlassian.mail.server.MailServer;
18 import com.atlassian.mail.server.PopMailServer;
19 import com.atlassian.mail.server.SMTPMailServer;
20 import com.atlassian.mail.server.impl.PopMailServerImpl;
21 import com.atlassian.mail.server.impl.SMTPMailServerImpl;
22 import com.opensymphony.util.TextUtils;
23 import org.ofbiz.core.entity.EntityUtil;
24 import org.ofbiz.core.entity.GenericEntityException;
25 import org.ofbiz.core.entity.GenericValue;
26 import org.ofbiz.core.util.UtilMisc;
27
28 import java.util.ArrayList;
29 import java.util.HashMap;
30 import java.util.List;
31 import java.util.Map;
32
33 public class OFBizMailServerManager extends AbstractMailServerManager
34 {
35 public MailServer getMailServer(Long id) throws MailException
36 {
37 try
38 {
39 GenericValue gv = CoreFactory.getGenericDelegator().findByPrimaryKeyCache("MailServer", UtilMisc.toMap("id", id));
40 if (gv == null)
41 return null;
42 else
43 return constructMailServer(gv);
44 }
45 catch (GenericEntityException e)
46 {
47 throw new MailException(e);
48 }
49 }
50
51 public MailServer getMailServer(String name) throws MailException
52 {
53 try
54 {
55 GenericValue gv = EntityUtil.getOnly(CoreFactory.getGenericDelegator().findByAndCache("MailServer", UtilMisc.toMap("name", name)));
56 if (gv == null)
57 return null;
58 else
59 return constructMailServer(gv);
60 }
61 catch (GenericEntityException e)
62 {
63 throw new MailException(e);
64 }
65 }
66
67 public List getServerNames() throws MailException
68 {
69 try
70 {
71 List mailServerGVs = CoreFactory.getGenericDelegator().findAllCache("MailServer", UtilMisc.toList("id asc"));
72 List mailServers = new ArrayList();
73 for (int i = 0; i < mailServerGVs.size(); i++)
74 {
75 GenericValue emailServerGV = (GenericValue) mailServerGVs.get(i);
76 mailServers.add(constructMailServer(emailServerGV));
77 }
78 return mailServers;
79 }
80 catch (GenericEntityException e)
81 {
82 throw new MailException(e);
83 }
84 }
85
86 public List getSmtpMailServers() throws MailException
87 {
88 return getMailServersByType(SERVER_TYPES[1]);
89 }
90
91 public List getPopMailServers() throws MailException
92 {
93 return getMailServersByType(SERVER_TYPES[0]);
94 }
95
96 public Long create(MailServer mailServer) throws MailException
97 {
98 try
99 {
100 GenericValue storedMailServer = EntityUtils.createValue("MailServer", getMapFromColumns(mailServer));
101 return storedMailServer.getLong("id");
102 }
103 catch (GenericEntityException e)
104 {
105 throw new MailException(e);
106 }
107 }
108
109 public void update(MailServer mailServer) throws MailException
110 {
111 try
112 {
113 GenericValue storedMailServer = getMailServerGV(mailServer.getId());
114 storedMailServer.setFields(getMapFromColumns(mailServer));
115 storedMailServer.store();
116 }
117 catch (GenericEntityException e)
118 {
119 throw new MailException(e);
120 }
121 }
122
123 public void delete(Long mailServerId) throws MailException
124 {
125 try
126 {
127 GenericValue storedMailServer = getMailServerGV(mailServerId);
128 storedMailServer.remove();
129 }
130 catch (GenericEntityException e)
131 {
132 throw new MailException(e);
133 }
134 }
135
136
137 public SMTPMailServer getDefaultSMTPMailServer()
138 {
139 SMTPMailServer smtps = null;
140 try
141 {
142 DefaultPreferences dp = new DefaultPreferences();
143 smtps = (SMTPMailServer) getMailServer(dp.getString("DefaultSmtpServer"));
144 if (smtps != null)
145 return smtps;
146 }
147 catch (Exception e)
148 {
149 }
150
151 try
152 {
153 if (getSmtpMailServers() == null || getSmtpMailServers().size() == 0)
154 return null;
155 return (SMTPMailServer) getSmtpMailServers().get(0);
156 }
157 catch (MailException e)
158 {
159 }
160
161 return null;
162 }
163
164 public PopMailServer getDefaultPopMailServer()
165 {
166 PopMailServer pops = null;
167 try
168 {
169 DefaultPreferences dp = new DefaultPreferences();
170 pops = (PopMailServer) getMailServer(dp.getString("DefaultPopServer"));
171 if (pops != null)
172 return pops;
173 }
174 catch (Exception e)
175 {
176 }
177
178 try
179 {
180 if (getPopMailServers() == null || getPopMailServers().size() == 0)
181 return null;
182 return (PopMailServer) getPopMailServers().get(0);
183 }
184 catch (MailException e)
185 {
186 }
187
188 return null;
189 }
190
191 protected List getMailServersByType(String serverType) throws MailException
192 {
193 List returnValue = new ArrayList();
194 List mailServers = getServerNames();
195 for (int i = 0; i < mailServers.size(); i++)
196 {
197 MailServer mailServer = (MailServer) mailServers.get(i);
198 if (serverType.equals(mailServer.getType()))
199 returnValue.add(mailServer);
200 }
201 return returnValue;
202 }
203
204 protected GenericValue getMailServerGV(Long id) throws MailException
205 {
206 try
207 {
208 return CoreFactory.getGenericDelegator().findByPrimaryKeyCache("MailServer", UtilMisc.toMap("id", id));
209 }
210 catch (GenericEntityException e)
211 {
212 throw new MailException(e);
213 }
214 }
215
216 protected MailServer constructMailServer(GenericValue gv)
217 {
218 if (SERVER_TYPES[0].equals(gv.getString("type")))
219 return new PopMailServerImpl(gv.getLong("id"), gv.getString("name"), gv.getString("description"), gv.getString("servername"), gv.getString("username"), gv.getString("password"));
220 else if (SERVER_TYPES[1].equals(gv.getString("type")))
221 {
222
223 String port = gv.getString("smtpPort");
224 if(port == null)
225 port = SMTPMailServer.DEFAULT_SMTP_PORT;
226
227 if (TextUtils.stringSet(gv.getString("servername")))
228 {
229 return new SMTPMailServerImpl(gv.getLong("id"), gv.getString("name"), gv.getString("description"), gv.getString("from"), gv.getString("prefix"), false, gv.getString("servername"), gv.getString("username"), gv.getString("password"), port);
230 }
231 else
232 {
233 return new SMTPMailServerImpl(gv.getLong("id"), gv.getString("name"), gv.getString("description"), gv.getString("from"), gv.getString("prefix"), true, gv.getString("jndilocation"), gv.getString("username"), gv.getString("password"), port);
234 }
235 }
236 else
237 return null;
238 }
239
240 protected Map getMapFromColumns(MailServer mailServer) throws MailException
241 {
242 Map columns = new HashMap();
243 columns.put("name", mailServer.getName());
244 columns.put("description", mailServer.getDescription());
245 columns.put("username", mailServer.getUsername());
246 columns.put("password", mailServer.getPassword());
247 columns.put("type", mailServer.getType());
248 columns.put("servername", mailServer.getHostname());
249
250 if (SERVER_TYPES[0].equals(mailServer.getType()))
251 {
252
253 }
254 else if (SERVER_TYPES[1].equals(mailServer.getType()))
255 {
256 SMTPMailServer smtp = (SMTPMailServer) mailServer;
257 columns.put("from", smtp.getDefaultFrom());
258 columns.put("prefix", smtp.getPrefix());
259 columns.put("smtpPort", smtp.getSmtpPort());
260 if (smtp.isSessionServer())
261 {
262 columns.put("jndilocation", smtp.getJndiLocation());
263 }
264 else
265 {
266 columns.put("servername", smtp.getHostname());
267 }
268 }
269 else
270 {
271 throw new MailException("The Type of Mail Server is not recognised");
272 }
273 return columns;
274 }
275
276
277 }