| 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 |
|
|
|
|
|
| 78% |
Uncovered Elements: 31 (141) |
Complexity: 43 |
Complexity Density: 0.45 |
|
| 33 |
|
public class OFBizMailServerManager extends AbstractMailServerManager |
| 34 |
|
{ |
|
|
|
| 87.5% |
Uncovered Elements: 1 (8) |
Complexity: 3 |
Complexity Density: 0.5 |
|
| 35 |
8
|
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 |
|
|
|
|
|
| 87.5% |
Uncovered Elements: 1 (8) |
Complexity: 3 |
Complexity Density: 0.5 |
|
| 51 |
9
|
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 |
|
|
|
|
|
| 90% |
Uncovered Elements: 1 (10) |
Complexity: 3 |
Complexity Density: 0.38 |
|
| 67 |
17
|
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 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 86 |
10
|
public List getSmtpMailServers() throws MailException... |
| 87 |
|
{ |
| 88 |
10
|
return getMailServersByType(SERVER_TYPES[1]); |
| 89 |
|
} |
| 90 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 91 |
7
|
public List getPopMailServers() throws MailException... |
| 92 |
|
{ |
| 93 |
7
|
return getMailServersByType(SERVER_TYPES[0]); |
| 94 |
|
} |
| 95 |
|
|
|
|
|
| 75% |
Uncovered Elements: 1 (4) |
Complexity: 2 |
Complexity Density: 0.5 |
|
| 96 |
13
|
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 |
|
|
|
|
|
| 80% |
Uncovered Elements: 1 (5) |
Complexity: 2 |
Complexity Density: 0.4 |
|
| 109 |
2
|
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 |
|
|
|
|
|
| 75% |
Uncovered Elements: 1 (4) |
Complexity: 2 |
Complexity Density: 0.5 |
|
| 123 |
6
|
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 |
|
|
|
|
|
| 93.3% |
Uncovered Elements: 1 (15) |
Complexity: 6 |
Complexity Density: 0.55 |
|
| 137 |
4
|
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 |
|
|
|
|
|
| 93.3% |
Uncovered Elements: 1 (15) |
Complexity: 6 |
Complexity Density: 0.55 |
|
| 164 |
3
|
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 |
|
|
|
|
|
| 90.9% |
Uncovered Elements: 1 (11) |
Complexity: 3 |
Complexity Density: 0.43 |
|
| 191 |
17
|
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 |
|
|
|
|
|
| 66.7% |
Uncovered Elements: 1 (3) |
Complexity: 2 |
Complexity Density: 0.67 |
|
| 204 |
10
|
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 |
|
|
|
|
|
| 0% |
Uncovered Elements: 18 (18) |
Complexity: 5 |
Complexity Density: 0.5 |
|
| 216 |
0
|
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 |
|
|
| 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 |
|
|
|
|
|
| 91.7% |
Uncovered Elements: 2 (24) |
Complexity: 4 |
Complexity Density: 0.22 |
|
| 240 |
21
|
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 |
|
|
| 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 |
|
} |