1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.mail.internal

File DatabaseMailStatusStore.java

 

Coverage histogram

../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

16
73
12
1
250
191
24
0.33
6.08
12
2

Classes

Class Line # Actions
DatabaseMailStatusStore 54 73 0% 24 6
0.940594194.1%
 

Contributing tests

This file is covered by 2 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
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    * Stores mail results in the database using Hibernate.
47    *
48    * @version $Id: 8fdc8729597d72b9e3657ab166012ca76041b4bb $
49    * @since 6.4M3
50    */
51    @Component
52    @Named("database")
53    @Singleton
 
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   
 
65  11 toggle @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    // Save in the main wiki
72  11 String currentWiki = xwikiContext.getWikiId();
73  11 xwikiContext.setWikiId(xwikiContext.getMainXWiki());
74   
75  11 try {
76    // Delete any previous state of the message
77  11 delete(status.getMessageId(), parameters);
78   
79  11 store.executeWrite(xwikiContext, new XWikiHibernateBaseStore.HibernateCallback<Object>()
80    {
 
81  11 toggle @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   
 
95  7 toggle @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   
 
106  25 toggle @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    // Load from the main wiki
115  25 String currentWiki = xwikiContext.getWikiId();
116  25 xwikiContext.setWikiId(xwikiContext.getMainXWiki());
117   
118    // Compute the Query string based on the passed filter map
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    {
 
125  25 toggle @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   
 
149  14 toggle @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    // Count in the main wiki
156  14 String currentWiki = xwikiContext.getWikiId();
157  14 xwikiContext.setWikiId(xwikiContext.getMainXWiki());
158   
159    // Compute the Query string based on the passed filter map
160  14 final String queryString = computeCountQueryString(filterMap);
161   
162  14 try {
163  14 Long count = store.executeRead(xwikiContext,
164    new XWikiHibernateBaseStore.HibernateCallback<Long>()
165    {
 
166  14 toggle @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   
 
184  14 toggle @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    // Delete from the main wiki
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    {
 
197  14 toggle @Override
198    public Object doInHibernate(Session session) throws HibernateException, XWikiException
199    {
200    // Delete the message
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   
 
214  41 toggle 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   
 
239  15 toggle 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   
 
245  26 toggle 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    }