1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.mail.internal; |
21 |
|
|
22 |
|
import java.util.Collections; |
23 |
|
import java.util.Iterator; |
24 |
|
import java.util.List; |
25 |
|
import java.util.Map; |
26 |
|
|
27 |
|
import javax.inject.Inject; |
28 |
|
import javax.inject.Named; |
29 |
|
import javax.inject.Provider; |
30 |
|
import javax.inject.Singleton; |
31 |
|
|
32 |
|
import org.hibernate.HibernateException; |
33 |
|
import org.hibernate.Query; |
34 |
|
import org.hibernate.Session; |
35 |
|
import org.xwiki.component.annotation.Component; |
36 |
|
import org.xwiki.mail.MailStatus; |
37 |
|
import org.xwiki.mail.MailStatusStore; |
38 |
|
import org.xwiki.mail.MailStoreException; |
39 |
|
|
40 |
|
import com.xpn.xwiki.XWikiContext; |
41 |
|
import com.xpn.xwiki.XWikiException; |
42 |
|
import com.xpn.xwiki.store.XWikiHibernateBaseStore; |
43 |
|
import com.xpn.xwiki.store.XWikiStoreInterface; |
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
@version |
49 |
|
@since |
50 |
|
|
51 |
|
@Component |
52 |
|
@Named("database") |
53 |
|
@Singleton |
|
|
| 94.1% |
Uncovered Elements: 6 (101) |
Complexity: 24 |
Complexity Density: 0.33 |
|
54 |
|
public class DatabaseMailStatusStore implements MailStatusStore |
55 |
|
{ |
56 |
|
private static final String ID_PARAMETER_NAME = "id"; |
57 |
|
|
58 |
|
@Inject |
59 |
|
private Provider<XWikiContext> contextProvider; |
60 |
|
|
61 |
|
@Inject |
62 |
|
@Named("hibernate") |
63 |
|
private XWikiStoreInterface hibernateStore; |
64 |
|
|
|
|
| 88.9% |
Uncovered Elements: 1 (9) |
Complexity: 2 |
Complexity Density: 0.22 |
|
65 |
11 |
@Override... |
66 |
|
public void save(final MailStatus status, final Map<String, Object> parameters) throws MailStoreException |
67 |
|
{ |
68 |
11 |
XWikiHibernateBaseStore store = (XWikiHibernateBaseStore) this.hibernateStore; |
69 |
|
|
70 |
11 |
XWikiContext xwikiContext = this.contextProvider.get(); |
71 |
|
|
72 |
11 |
String currentWiki = xwikiContext.getWikiId(); |
73 |
11 |
xwikiContext.setWikiId(xwikiContext.getMainXWiki()); |
74 |
|
|
75 |
11 |
try { |
76 |
|
|
77 |
11 |
delete(status.getMessageId(), parameters); |
78 |
|
|
79 |
11 |
store.executeWrite(xwikiContext, new XWikiHibernateBaseStore.HibernateCallback<Object>() |
80 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
81 |
11 |
@Override... |
82 |
|
public Object doInHibernate(Session session) throws HibernateException, XWikiException |
83 |
|
{ |
84 |
11 |
session.save(status); |
85 |
11 |
return null; |
86 |
|
} |
87 |
|
}); |
88 |
|
} catch (Exception e) { |
89 |
0 |
throw new MailStoreException(String.format("Failed to save mail status [%s] to the database.", status), e); |
90 |
|
} finally { |
91 |
11 |
xwikiContext.setWikiId(currentWiki); |
92 |
|
} |
93 |
|
} |
94 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
95 |
7 |
@Override... |
96 |
|
public MailStatus load(String uniqueMessageId) throws MailStoreException |
97 |
|
{ |
98 |
7 |
List<MailStatus> statuses = load(Collections.<String, Object>singletonMap(ID_PARAMETER_NAME, uniqueMessageId), |
99 |
|
0, 0, null, false); |
100 |
7 |
if (statuses.isEmpty()) { |
101 |
2 |
return null; |
102 |
|
} |
103 |
5 |
return statuses.get(0); |
104 |
|
} |
105 |
|
|
|
|
| 88.9% |
Uncovered Elements: 1 (9) |
Complexity: 2 |
Complexity Density: 0.22 |
|
106 |
25 |
@Override... |
107 |
|
public List<MailStatus> load(final Map<String, Object> filterMap, final int offset, final int count, |
108 |
|
String sortField, boolean sortAscending) |
109 |
|
throws MailStoreException |
110 |
|
{ |
111 |
25 |
XWikiHibernateBaseStore store = (XWikiHibernateBaseStore) this.hibernateStore; |
112 |
|
|
113 |
25 |
final XWikiContext xwikiContext = this.contextProvider.get(); |
114 |
|
|
115 |
25 |
String currentWiki = xwikiContext.getWikiId(); |
116 |
25 |
xwikiContext.setWikiId(xwikiContext.getMainXWiki()); |
117 |
|
|
118 |
|
|
119 |
25 |
final String queryString = computeSelectQueryString(filterMap, sortField, sortAscending); |
120 |
|
|
121 |
25 |
try { |
122 |
25 |
return store.executeRead(xwikiContext, |
123 |
|
new XWikiHibernateBaseStore.HibernateCallback<List<MailStatus>>() |
124 |
|
{ |
|
|
| 83.3% |
Uncovered Elements: 2 (12) |
Complexity: 3 |
Complexity Density: 0.38 |
|
125 |
25 |
@Override... |
126 |
|
public List<MailStatus> doInHibernate(Session session) |
127 |
|
throws HibernateException, XWikiException |
128 |
|
{ |
129 |
25 |
Query query = session.createQuery(queryString); |
130 |
25 |
if (offset > 0) { |
131 |
0 |
query.setFirstResult(offset); |
132 |
|
} |
133 |
25 |
if (count > 0) { |
134 |
14 |
query.setMaxResults(count); |
135 |
|
} |
136 |
25 |
query.setProperties(filterMap); |
137 |
25 |
List<MailStatus> queryResult = (List<MailStatus>) query.list(); |
138 |
25 |
return queryResult; |
139 |
|
} |
140 |
|
}); |
141 |
|
} catch (Exception e) { |
142 |
0 |
throw new MailStoreException(String.format( |
143 |
|
"Failed to load mail statuses matching the filter [%s] from the database.", filterMap), e); |
144 |
|
} finally { |
145 |
25 |
xwikiContext.setWikiId(currentWiki); |
146 |
|
} |
147 |
|
} |
148 |
|
|
|
|
| 90% |
Uncovered Elements: 1 (10) |
Complexity: 2 |
Complexity Density: 0.2 |
|
149 |
14 |
@Override... |
150 |
|
public long count(final Map<String, Object> filterMap) throws MailStoreException |
151 |
|
{ |
152 |
14 |
XWikiHibernateBaseStore store = (XWikiHibernateBaseStore) this.hibernateStore; |
153 |
|
|
154 |
14 |
final XWikiContext xwikiContext = this.contextProvider.get(); |
155 |
|
|
156 |
14 |
String currentWiki = xwikiContext.getWikiId(); |
157 |
14 |
xwikiContext.setWikiId(xwikiContext.getMainXWiki()); |
158 |
|
|
159 |
|
|
160 |
14 |
final String queryString = computeCountQueryString(filterMap); |
161 |
|
|
162 |
14 |
try { |
163 |
14 |
Long count = store.executeRead(xwikiContext, |
164 |
|
new XWikiHibernateBaseStore.HibernateCallback<Long>() |
165 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
166 |
14 |
@Override... |
167 |
|
public Long doInHibernate(Session session) |
168 |
|
throws HibernateException, XWikiException |
169 |
|
{ |
170 |
14 |
Query query = session.createQuery(queryString); |
171 |
14 |
query.setProperties(filterMap); |
172 |
14 |
return (Long) query.uniqueResult(); |
173 |
|
} |
174 |
|
}); |
175 |
14 |
return count; |
176 |
|
} catch (Exception e) { |
177 |
0 |
throw new MailStoreException(String.format( |
178 |
|
"Failed to count mail statuses matching the filter [%s] from the database.", filterMap), e); |
179 |
|
} finally { |
180 |
14 |
xwikiContext.setWikiId(currentWiki); |
181 |
|
} |
182 |
|
} |
183 |
|
|
|
|
| 87.5% |
Uncovered Elements: 1 (8) |
Complexity: 2 |
Complexity Density: 0.25 |
|
184 |
14 |
@Override... |
185 |
|
public void delete(final String uniqueMessageId, Map<String, Object> parameters) throws MailStoreException |
186 |
|
{ |
187 |
14 |
XWikiHibernateBaseStore store = (XWikiHibernateBaseStore) this.hibernateStore; |
188 |
|
|
189 |
14 |
XWikiContext xwikiContext = this.contextProvider.get(); |
190 |
|
|
191 |
14 |
String currentWiki = xwikiContext.getWikiId(); |
192 |
14 |
xwikiContext.setWikiId(xwikiContext.getMainXWiki()); |
193 |
|
|
194 |
14 |
try { |
195 |
14 |
store.executeWrite(xwikiContext, new XWikiHibernateBaseStore.HibernateCallback<Object>() |
196 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
197 |
14 |
@Override... |
198 |
|
public Object doInHibernate(Session session) throws HibernateException, XWikiException |
199 |
|
{ |
200 |
|
|
201 |
14 |
String queryString = String.format("delete from %s where mail_id=:id", MailStatus.class.getName()); |
202 |
14 |
session.createQuery(queryString).setParameter(ID_PARAMETER_NAME, uniqueMessageId).executeUpdate(); |
203 |
14 |
return null; |
204 |
|
} |
205 |
|
}); |
206 |
|
} catch (Exception e) { |
207 |
0 |
throw new MailStoreException(String.format("Failed to delete mail status (message id [%s]) " |
208 |
|
+ "from the database.", uniqueMessageId), e); |
209 |
|
} finally { |
210 |
14 |
xwikiContext.setWikiId(currentWiki); |
211 |
|
} |
212 |
|
} |
213 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (25) |
Complexity: 6 |
Complexity Density: 0.4 |
|
214 |
41 |
protected String computeQueryString(String prefix,... |
215 |
|
Map<String, Object> filterMap, String sortField, boolean sortAscending) |
216 |
|
{ |
217 |
41 |
StringBuilder queryBuilder = new StringBuilder(prefix); |
218 |
41 |
if (!filterMap.isEmpty()) { |
219 |
39 |
queryBuilder.append(" where"); |
220 |
39 |
Iterator<String> iterator = filterMap.keySet().iterator(); |
221 |
130 |
while (iterator.hasNext()) { |
222 |
91 |
String filterKey = iterator.next(); |
223 |
91 |
queryBuilder.append(" mail_").append(filterKey).append(" like ").append(':').append(filterKey); |
224 |
91 |
if (iterator.hasNext()) { |
225 |
52 |
queryBuilder.append(" and"); |
226 |
|
} |
227 |
|
} |
228 |
|
} |
229 |
41 |
if (sortField != null) { |
230 |
19 |
queryBuilder.append(" order by "); |
231 |
19 |
queryBuilder.append(sortField); |
232 |
19 |
if (!sortAscending) { |
233 |
15 |
queryBuilder.append(" desc"); |
234 |
|
} |
235 |
|
} |
236 |
41 |
return queryBuilder.toString(); |
237 |
|
} |
238 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
239 |
15 |
protected String computeCountQueryString(Map<String, Object> filterMap)... |
240 |
|
{ |
241 |
15 |
return computeQueryString(String.format("select count(*) from %s", MailStatus.class.getName()), |
242 |
|
filterMap, null, false); |
243 |
|
} |
244 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
245 |
26 |
protected String computeSelectQueryString(Map<String, Object> filterMap, String sortField, boolean sortAscending)... |
246 |
|
{ |
247 |
26 |
return computeQueryString(String.format("from %s", MailStatus.class.getName()), |
248 |
|
filterMap, sortField, sortAscending); |
249 |
|
} |
250 |
|
} |