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

File WatchListEventMimeMessageFactory.java

 

Coverage histogram

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

Code metrics

0
9
1
1
144
63
1
0.11
9
1
1

Classes

Class Line # Actions
WatchListEventMimeMessageFactory 55 9 0% 1 0
1.0100%
 

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.watchlist.internal.notification;
21   
22    import java.util.Iterator;
23    import java.util.Map;
24   
25    import javax.inject.Inject;
26    import javax.inject.Named;
27    import javax.inject.Singleton;
28    import javax.mail.MessagingException;
29    import javax.mail.internet.MimeMessage;
30   
31    import org.xwiki.component.annotation.Component;
32    import org.xwiki.context.Execution;
33    import org.xwiki.mail.MimeMessageFactory;
34    import org.xwiki.mail.SessionFactory;
35    import org.xwiki.mail.internal.factory.AbstractIteratorMimeMessageFactory;
36    import org.xwiki.model.reference.DocumentReferenceResolver;
37    import org.xwiki.model.reference.EntityReferenceSerializer;
38    import org.xwiki.watchlist.internal.UserAvatarAttachmentExtractor;
39    import org.xwiki.watchlist.internal.WatchListEventMatcher;
40   
41    import com.xpn.xwiki.internal.plugin.rightsmanager.UserIterator;
42   
43    /**
44    * Takes a list of subscribers and a list of {@link org.xwiki.watchlist.internal.api.WatchListEvent WatchListEvent}s
45    * occurred in a certain period and, for each subscriber, determines the sublist of events that are interesting to him
46    * (i.e. that match his WatchList preferences) anc creates one {@link MimeMessage} to be used to notify him of these
47    * events.
48    *
49    * @version $Id: 8f804a04ddc4463534bf07e1094f468bc627040c $
50    * @since 7.1M1
51    */
52    @Component
53    @Named(WatchListEventMimeMessageFactory.FACTORY_ID)
54    @Singleton
 
55    public class WatchListEventMimeMessageFactory extends AbstractIteratorMimeMessageFactory
56    {
57    /**
58    * Component ID.
59    */
60    public static final String FACTORY_ID = "watchlistevents";
61   
62    /**
63    * Hint parameter name. String used to identify the
64    * {@link org.xwiki.mail.internal.factory.template.TemplateMimeMessageFactory TemplateMimeMessageFactory}.
65    */
66    public static final String HINT_PARAMETER = "hint";
67   
68    /**
69    * Template parameter name. String used to determine the source of the
70    * {@link org.xwiki.mail.internal.factory.template.TemplateMimeMessageFactory TemplateMimeMessageFactory}.
71    */
72    public static final String TEMPLATE_PARAMETER = "template";
73   
74    /**
75    * SkipContxtUser parameter name. Boolean used to skip notifying the current context user if he should be found in
76    * the list of subscribers to be notified. Default is {@code false}.
77    */
78    public static final String SKIP_CONTEXT_USER_PARAMETER = "skipContextUser";
79   
80    /**
81    * Attach author avatars parameter name. Boolean used to specify if the avatars of the authors of the events we are
82    * notifying about should be attached to the message. Default is {@code false}.
83    */
84    public static final String ATTACH_AUTHOR_AVATARS_PARAMETER = "attachAuthorAvatars";
85   
86    /**
87    * Parameters parameter name. Map used as parameters for the
88    * {@link org.xwiki.mail.internal.factory.template.TemplateMimeMessageFactory TemplateMimeMessageFactory}.
89    */
90    public static final String PARAMETERS_PARAMETER = "parameters";
91   
92    @Inject
93    @Named("explicit")
94    private DocumentReferenceResolver<String> explicitDocumentReferenceResolver;
95   
96    @Inject
97    private EntityReferenceSerializer<String> serializer;
98   
99    @Inject
100    private Execution execution;
101   
102    @Inject
103    private WatchListEventMatcher eventMatcher;
104   
105    @Inject
106    private UserAvatarAttachmentExtractor avatarExtractor;
107   
108    @Inject
109    private SessionFactory sessionFactory;
110   
 
111  30 toggle @Override
112    public Iterator<MimeMessage> createMessage(Object sourceObject, Map<String, Object> parameters)
113    throws MessagingException
114    {
115  30 Map<String, Object> source = getTypedSource(sourceObject, Map.class);
116  30 validateParameters(parameters, HINT_PARAMETER, TEMPLATE_PARAMETER, PARAMETERS_PARAMETER);
117   
118    // Extract from the passed parameters the MimeMessageFactory to use to create a single mail
119  30 String factoryHint = (String) parameters.get(HINT_PARAMETER);
120   
121    // TODO: is this configurable or should we always use "template" instead?
122  30 MimeMessageFactory factory = getInternalMimeMessageFactory(factoryHint);
123   
124    // Parse the source.
125  30 EventsAndSubscribersSource sourceData = EventsAndSubscribersSource.parse(source);
126   
127    // UserDataExtractor to be used for each subscriber.
128  30 WatchListMessageDataExtractor userDataExtractor =
129    new WatchListMessageDataExtractor(sourceData, parameters, eventMatcher, execution,
130    explicitDocumentReferenceResolver);
131   
132    // The iterator that will be checking each subscriber and that will extract the WatchListMessageData.
133  30 UserIterator<WatchListMessageData> userIterator =
134    new UserIterator<WatchListMessageData>(EventsAndSubscribersSource.parse(source).getSubscribers(), null,
135    userDataExtractor, explicitDocumentReferenceResolver, execution);
136   
137    // The iterator that will be producing a MimeMessage for each WatchListMessageData produced by the userIterator.
138  30 WatchListEventMimeMessageIterator messageIterator =
139    new WatchListEventMimeMessageIterator(userIterator, factory, parameters, avatarExtractor, serializer,
140    sessionFactory);
141   
142  30 return messageIterator;
143    }
144    }