1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.notifications.notifiers.internal.email

File NotificationEmailJob.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart9.png
41% of files have more coverage

Code metrics

8
30
8
1
139
92
14
0.47
3.75
8
1.75

Classes

Class Line # Actions
NotificationEmailJob 46 30 0% 14 7
0.8478260684.8%
 

Contributing tests

No tests hitting this source file were found.

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.notifications.notifiers.internal.email;
21   
22    import java.util.Arrays;
23    import java.util.Date;
24   
25    import org.quartz.Job;
26    import org.quartz.JobDataMap;
27    import org.quartz.JobExecutionContext;
28    import org.quartz.JobExecutionException;
29    import org.xwiki.bridge.DocumentAccessBridge;
30    import org.xwiki.model.reference.DocumentReference;
31    import org.xwiki.notifications.NotificationConfiguration;
32    import org.xwiki.notifications.notifiers.email.NotificationEmailInterval;
33    import org.xwiki.notifications.notifiers.internal.ModelBridge;
34   
35    import com.xpn.xwiki.objects.BaseObject;
36    import com.xpn.xwiki.objects.BaseObjectReference;
37    import com.xpn.xwiki.plugin.scheduler.AbstractJob;
38    import com.xpn.xwiki.web.Utils;
39   
40    /**
41    * Scheduler job that send emails about notifications.
42    *
43    * @version $Id: fc316aa77ca74f39da09e55cecf6ea81ac80304b $
44    * @since 9.5RC1
45    */
 
46    public class NotificationEmailJob extends AbstractJob implements Job
47    {
48    private static final String LAST_FIRE_TIME = "lastFireTime";
49   
50    private static final String XWIKI_SPACE = "XWiki";
51   
52    private static final String NOTIFICATIONS_SPACE = "Notifications";
53   
 
54  4 toggle @Override
55    protected void executeJob(JobExecutionContext jobContext) throws JobExecutionException
56    {
57  4 NotificationConfiguration configuration = Utils.getComponent(NotificationConfiguration.class);
58  4 if (!configuration.isEnabled() || !configuration.areEmailsEnabled()) {
59    // do nothing
60  0 return;
61    }
62   
63  4 DocumentReference schedulerJobDocument = getSchedulerJobDocument(jobContext);
64  4 BaseObjectReference emailJobObjectReference = getNotificationEmailJobObjectReference(schedulerJobDocument);
65   
66  4 NotificationUserIterator userIterator = Utils.getComponent(NotificationUserIterator.class);
67  4 userIterator.initialize(getJobInterval(schedulerJobDocument));
68   
69  4 NotificationEmailSender mailSender = Utils.getComponent(NotificationEmailSender.class);
70  4 mailSender.sendEmails(getPreviousFireTime(emailJobObjectReference), userIterator);
71   
72  4 setPreviousFireTime(emailJobObjectReference);
73    }
74   
 
75  4 toggle private DocumentReference getSchedulerJobDocument(JobExecutionContext jobContext)
76    {
77  4 JobDataMap data = jobContext.getJobDetail().getJobDataMap();
78   
79  4 BaseObject schedulerJobObject = (BaseObject) data.get("xjob");
80  4 BaseObjectReference schedulerJobObjectReference = schedulerJobObject.getReference();
81  4 return (DocumentReference) schedulerJobObjectReference.getParent();
82    }
83   
 
84  4 toggle private BaseObjectReference getNotificationEmailJobObjectReference(DocumentReference schedulerJobDocument)
85    {
86  4 DocumentReference emailJobDocument = new DocumentReference(
87    schedulerJobDocument.getWikiReference().getName(),
88    Arrays.asList(XWIKI_SPACE, NOTIFICATIONS_SPACE),
89    schedulerJobDocument.getName()
90    );
91   
92  4 return new BaseObjectReference(
93    new DocumentReference(schedulerJobDocument.getWikiReference().getName(),
94    Arrays.asList(XWIKI_SPACE, NOTIFICATIONS_SPACE, "Code"), "EmailJobClass"),
95    0,
96    emailJobDocument
97    );
98    }
99   
 
100  4 toggle private NotificationEmailInterval getJobInterval(DocumentReference schedulerJobDocument)
101    {
102  4 if (schedulerJobDocument.getName().contains("Hourly")) {
103  3 return NotificationEmailInterval.HOURLY;
104  1 } else if (schedulerJobDocument.getName().contains("Weekly")) {
105  0 return NotificationEmailInterval.WEEKLY;
106    }
107  1 return NotificationEmailInterval.DAILY;
108    }
109   
 
110  4 toggle private Date getPreviousFireTime(BaseObjectReference emailJobObject)
111    {
112  4 Object previousFireTime = getDocumentAccessBridge().getProperty(emailJobObject, LAST_FIRE_TIME);
113  4 if (previousFireTime != null) {
114  0 return (Date) previousFireTime;
115    }
116   
117  4 return new Date(0L);
118    }
119   
 
120  4 toggle private void setPreviousFireTime(BaseObjectReference emailJobObject) throws JobExecutionException
121    {
122  4 try {
123  4 getModelBridge().savePropertyInHiddenDocument(emailJobObject, LAST_FIRE_TIME, new Date());
124    } catch (Exception e) {
125  0 throw new JobExecutionException(
126    String.format("Failed to update the last fire time property of [{}].", emailJobObject), e);
127    }
128    }
129   
 
130  4 toggle private DocumentAccessBridge getDocumentAccessBridge()
131    {
132  4 return Utils.getComponent(DocumentAccessBridge.class);
133    }
134   
 
135  4 toggle private ModelBridge getModelBridge()
136    {
137  4 return Utils.getComponent(ModelBridge.class);
138    }
139    }