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

File DatabaseMailStatusResult.java

 

Coverage histogram

../../../../img/srcFileCovDistChart8.png
54% of files have more coverage

Code metrics

4
19
6
1
116
66
10
0.53
3.17
6
1.67

Classes

Class Line # Actions
DatabaseMailStatusResult 41 19 0% 10 8
0.724137972.4%
 

Contributing tests

This file is covered by 9 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.HashMap;
24    import java.util.Iterator;
25    import java.util.Map;
26   
27    import org.slf4j.Logger;
28    import org.slf4j.LoggerFactory;
29    import org.xwiki.mail.MailState;
30    import org.xwiki.mail.MailStatus;
31    import org.xwiki.mail.MailStatusStore;
32    import org.xwiki.mail.MailStoreException;
33   
34    /**
35    * This implementation is not meant for scalability. Don't use it if you're sending a large number of emails. Instead
36    * use the Query Manager to perform database queries.
37    *
38    * @version $Id: 138f1e6811917ea853f423e922c4c9c718870131 $
39    * @since 6.4M3
40    */
 
41    public class DatabaseMailStatusResult extends AbstractMailStatusResult
42    {
43    private static final String BATCHID_KEY = "batchId";
44   
45    private static final Logger LOGGER = LoggerFactory.getLogger(DatabaseMailStatusResult.class);
46   
47    private static final String DATE_FIELD = "date";
48   
49    private MailStatusStore mailStatusStore;
50   
51    private String batchId;
52   
53    /**
54    * Constructor initializing the DatabaseMailStatusResult with MailStatusStore.
55    * @param mailStatusStore the MailStatusStore
56    */
 
57  43 toggle public DatabaseMailStatusResult(MailStatusStore mailStatusStore)
58    {
59  43 this.mailStatusStore = mailStatusStore;
60    }
61   
62    /**
63    * Set the batch id of the message statuses to save or load.
64    *
65    * @param batchId the batch id of the message statuses
66    */
 
67  43 toggle public void setBatchId(String batchId)
68    {
69  43 this.batchId = batchId;
70    }
71   
 
72  1 toggle @Override
73    public Iterator<MailStatus> getAll()
74    {
75  1 if (this.batchId == null) {
76  0 return Collections.emptyIterator();
77    }
78   
79  1 try {
80  1 return this.mailStatusStore.load(Collections.<String, Object>singletonMap(BATCHID_KEY, this.batchId),
81    0, 0, DATE_FIELD, true).iterator();
82    } catch (MailStoreException e) {
83  0 LOGGER.error("Failed to get all results. Returning an empty result.", e);
84  0 return Collections.emptyIterator();
85    }
86    }
87   
 
88  2 toggle @Override
89    public Iterator<MailStatus> getAllErrors()
90    {
91  2 return getFilteredState("%_error");
92    }
93   
 
94  2 toggle @Override
95    public Iterator<MailStatus> getByState(MailState state)
96    {
97  2 return getFilteredState(state.toString());
98    }
99   
 
100  4 toggle private Iterator<MailStatus> getFilteredState(String state)
101    {
102  4 if (this.batchId == null) {
103  0 return Collections.emptyIterator();
104    }
105   
106  4 try {
107  4 Map<String, Object> filterMap = new HashMap<>();
108  4 filterMap.put(BATCHID_KEY, this.batchId);
109  4 filterMap.put("state", state);
110  4 return this.mailStatusStore.load(filterMap, 0, 0, DATE_FIELD, true).iterator();
111    } catch (MailStoreException e) {
112  0 LOGGER.error("Failed to get results by state. Returning an empty result.", e);
113  0 return Collections.emptyIterator();
114    }
115    }
116    }