Clover Coverage Report - Atlassian Mail
Coverage timestamp: Mon Sep 29 2008 21:26:36 CDT
95   277   43   6.79
32   240   0.45   14
14     3.07  
1    
 
 
  OFBizMailServerManager       Line # 33 95 43 78% 0.78014183
 
  (17)
 
1    /*
2    * Created by IntelliJ IDEA.
3    * User: owen
4    * Date: Nov 22, 2002
5    * Time: 3:33:35 PM
6    * CVS Revision: $Revision: 1.7 $
7    * Last CVS Commit: $Date: 2005/05/10 00:41:22 $
8    * Author of last CVS Commit: $Author: schang $
9    * To change this template use Options | File Templates.
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  8 toggle public MailServer getMailServer(Long id) throws MailException
36    {
37  8 try
38    {
39  8 GenericValue gv = CoreFactory.getGenericDelegator().findByPrimaryKeyCache("MailServer", UtilMisc.toMap("id", id));
40  8 if (gv == null)
41  2 return null;
42    else
43  6 return constructMailServer(gv);
44    }
45    catch (GenericEntityException e)
46    {
47  0 throw new MailException(e);
48    }
49    }
50   
 
51  9 toggle public MailServer getMailServer(String name) throws MailException
52    {
53  9 try
54    {
55  9 GenericValue gv = EntityUtil.getOnly(CoreFactory.getGenericDelegator().findByAndCache("MailServer", UtilMisc.toMap("name", name)));
56  9 if (gv == null)
57  5 return null;
58    else
59  4 return constructMailServer(gv);
60    }
61    catch (GenericEntityException e)
62    {
63  0 throw new MailException(e);
64    }
65    }
66   
 
67  17 toggle public List getServerNames() throws MailException
68    {
69  17 try
70    {
71  17 List mailServerGVs = CoreFactory.getGenericDelegator().findAllCache("MailServer", UtilMisc.toList("id asc"));
72  17 List mailServers = new ArrayList();
73  32 for (int i = 0; i < mailServerGVs.size(); i++)
74    {
75  15 GenericValue emailServerGV = (GenericValue) mailServerGVs.get(i);
76  15 mailServers.add(constructMailServer(emailServerGV));
77    }
78  17 return mailServers;
79    }
80    catch (GenericEntityException e)
81    {
82  0 throw new MailException(e);
83    }
84    }
85   
 
86  10 toggle public List getSmtpMailServers() throws MailException
87    {
88  10 return getMailServersByType(SERVER_TYPES[1]);
89    }
90   
 
91  7 toggle public List getPopMailServers() throws MailException
92    {
93  7 return getMailServersByType(SERVER_TYPES[0]);
94    }
95   
 
96  13 toggle public Long create(MailServer mailServer) throws MailException
97    {
98  13 try
99    {
100  13 GenericValue storedMailServer = EntityUtils.createValue("MailServer", getMapFromColumns(mailServer));
101  13 return storedMailServer.getLong("id");
102    }
103    catch (GenericEntityException e)
104    {
105  0 throw new MailException(e);
106    }
107    }
108   
 
109  2 toggle public void update(MailServer mailServer) throws MailException
110    {
111  2 try
112    {
113  2 GenericValue storedMailServer = getMailServerGV(mailServer.getId());
114  2 storedMailServer.setFields(getMapFromColumns(mailServer));
115  2 storedMailServer.store();
116    }
117    catch (GenericEntityException e)
118    {
119  0 throw new MailException(e);
120    }
121    }
122   
 
123  6 toggle public void delete(Long mailServerId) throws MailException
124    {
125  6 try
126    {
127  6 GenericValue storedMailServer = getMailServerGV(mailServerId);
128  6 storedMailServer.remove();
129    }
130    catch (GenericEntityException e)
131    {
132  0 throw new MailException(e);
133    }
134    }
135   
136   
 
137  4 toggle public SMTPMailServer getDefaultSMTPMailServer()
138    {
139  4 SMTPMailServer smtps = null;
140  4 try
141    {
142  4 DefaultPreferences dp = new DefaultPreferences();
143  4 smtps = (SMTPMailServer) getMailServer(dp.getString("DefaultSmtpServer"));
144  4 if (smtps != null)
145  1 return smtps;
146    }
147    catch (Exception e)
148    {
149    }
150   
151  3 try
152    {
153  3 if (getSmtpMailServers() == null || getSmtpMailServers().size() == 0)
154  1 return null;
155  2 return (SMTPMailServer) getSmtpMailServers().get(0);
156    }
157    catch (MailException e)
158    {
159    }
160   
161  0 return null;
162    }
163   
 
164  3 toggle public PopMailServer getDefaultPopMailServer()
165    {
166  3 PopMailServer pops = null;
167  3 try
168    {
169  3 DefaultPreferences dp = new DefaultPreferences();
170  3 pops = (PopMailServer) getMailServer(dp.getString("DefaultPopServer"));
171  3 if (pops != null)
172  1 return pops;
173    }
174    catch (Exception e)
175    {
176    }
177   
178  2 try
179    {
180  2 if (getPopMailServers() == null || getPopMailServers().size() == 0)
181  1 return null;
182  1 return (PopMailServer) getPopMailServers().get(0);
183    }
184    catch (MailException e)
185    {
186    }
187   
188  0 return null;
189    }
190   
 
191  17 toggle protected List getMailServersByType(String serverType) throws MailException
192    {
193  17 List returnValue = new ArrayList();
194  17 List mailServers = getServerNames();
195  32 for (int i = 0; i < mailServers.size(); i++)
196    {
197  15 MailServer mailServer = (MailServer) mailServers.get(i);
198  15 if (serverType.equals(mailServer.getType()))
199  15 returnValue.add(mailServer);
200    }
201  17 return returnValue;
202    }
203   
 
204  10 toggle protected GenericValue getMailServerGV(Long id) throws MailException
205    {
206  10 try
207    {
208  10 return CoreFactory.getGenericDelegator().findByPrimaryKeyCache("MailServer", UtilMisc.toMap("id", id));
209    }
210    catch (GenericEntityException e)
211    {
212  0 throw new MailException(e);
213    }
214    }
215   
 
216  0 toggle protected MailServer constructMailServer(GenericValue gv)
217    {
218  0 if (SERVER_TYPES[0].equals(gv.getString("type")))
219  0 return new PopMailServerImpl(gv.getLong("id"), gv.getString("name"), gv.getString("description"), gv.getString("servername"), gv.getString("username"), gv.getString("password"));
220  0 else if (SERVER_TYPES[1].equals(gv.getString("type")))
221    {
222    // this is to fix upgrade errors when smtp port is null in the DB
223  0 String port = gv.getString("smtpPort");
224  0 if(port == null)
225  0 port = SMTPMailServer.DEFAULT_SMTP_PORT;
226   
227  0 if (TextUtils.stringSet(gv.getString("servername")))
228    {
229  0 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  0 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  0 return null;
238    }
239   
 
240  21 toggle protected Map getMapFromColumns(MailServer mailServer) throws MailException
241    {
242  21 Map columns = new HashMap();
243  21 columns.put("name", mailServer.getName());
244  21 columns.put("description", mailServer.getDescription());
245  21 columns.put("username", mailServer.getUsername());
246  21 columns.put("password", mailServer.getPassword());
247  21 columns.put("type", mailServer.getType());
248  21 columns.put("servername", mailServer.getHostname());
249   
250  21 if (SERVER_TYPES[0].equals(mailServer.getType()))
251    {
252    //Do nothing different
253    }
254  17 else if (SERVER_TYPES[1].equals(mailServer.getType()))
255    {
256  17 SMTPMailServer smtp = (SMTPMailServer) mailServer;
257  17 columns.put("from", smtp.getDefaultFrom());
258  17 columns.put("prefix", smtp.getPrefix());
259  17 columns.put("smtpPort", smtp.getSmtpPort());
260  17 if (smtp.isSessionServer())
261    {
262  2 columns.put("jndilocation", smtp.getJndiLocation());
263    }
264    else
265    {
266  15 columns.put("servername", smtp.getHostname());
267    }
268    }
269    else
270    {
271  0 throw new MailException("The Type of Mail Server is not recognised");
272    }
273  21 return columns;
274    }
275   
276   
277    }