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

File DocumentsModifiedDuringDistributionListener.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart2.png
81% of files have more coverage

Code metrics

30
61
14
3
254
176
29
0.48
4.36
4.67
2.07

Classes

Class Line # Actions
DocumentsModifiedDuringDistributionListener 59 50 0% 22 68
0.2183908121.8%
DocumentsModifiedDuringDistributionListener.DocumentStatus 75 11 0% 7 18
0.00%
DocumentsModifiedDuringDistributionListener.DocumentStatus.Action 77 0 - 0 0
-1.0 -
 

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.extension.distribution.internal;
21   
22    import java.util.HashMap;
23    import java.util.Map;
24   
25    import javax.inject.Inject;
26    import javax.inject.Named;
27    import javax.inject.Singleton;
28   
29    import org.apache.commons.lang3.StringUtils;
30    import org.xwiki.bridge.event.DocumentCreatedEvent;
31    import org.xwiki.bridge.event.DocumentDeletedEvent;
32    import org.xwiki.bridge.event.DocumentUpdatedEvent;
33    import org.xwiki.component.annotation.Component;
34    import org.xwiki.context.Execution;
35    import org.xwiki.context.ExecutionContext;
36    import org.xwiki.extension.Extension;
37    import org.xwiki.extension.LocalExtension;
38    import org.xwiki.extension.distribution.internal.DocumentsModifiedDuringDistributionListener.DocumentStatus.Action;
39    import org.xwiki.extension.xar.internal.handler.XarExtensionPlan;
40    import org.xwiki.job.JobContext;
41    import org.xwiki.job.Request;
42    import org.xwiki.model.reference.DocumentReference;
43    import org.xwiki.observation.AbstractEventListener;
44    import org.xwiki.observation.event.Event;
45   
46    import com.xpn.xwiki.XWikiContext;
47    import com.xpn.xwiki.doc.XWikiDocument;
48   
49    /**
50    * Gather the pages modified during the Distribution Wizard execution.
51    *
52    * @version $Id: 1e48286d3df3cad78918405ba0580e635a9cdcbe $
53    * @since 5.4RC1
54    */
55    @Component
56    @Named(DocumentsModifiedDuringDistributionListener.NAME)
57    @Singleton
58    // TODO: replace this by the concatenation and analysis of the distribution log
 
59    public class DocumentsModifiedDuringDistributionListener extends AbstractEventListener
60    {
61    public static final String NAME = "distribution.DocumentsModifiedDuringDistributionListener";
62   
63    @Inject
64    private Execution execution;
65   
66    @Inject
67    private JobContext jobContext;
68   
69    /**
70    * Map<wiki, Map<document, extension>>.
71    */
72    private Map<String, Map<DocumentReference, DocumentStatus>> documents =
73    new HashMap<String, Map<DocumentReference, DocumentStatus>>();
74   
 
75    public static class DocumentStatus
76    {
 
77    public enum Action
78    {
79    CREATED,
80    DELETED,
81    MODIFIED
82    }
83   
84    private final DocumentReference reference;
85   
86    private final String previousVersion;
87   
88    private final Action action;
89   
90    private final Extension previousExtension;
91   
92    private final Extension nextExtension;
93   
 
94  0 toggle public DocumentStatus(DocumentReference reference, String version, Action action)
95    {
96  0 this(reference, version, action, null, null);
97    }
98   
 
99  0 toggle public DocumentStatus(DocumentReference reference, String previousVersion, Action action,
100    Extension previousExtension, Extension nextExtension)
101    {
102  0 this.reference = reference;
103  0 this.previousVersion = previousVersion;
104  0 this.action = action;
105  0 this.previousExtension = previousExtension;
106  0 this.nextExtension = nextExtension;
107    }
108   
 
109  0 toggle public DocumentReference getReference()
110    {
111  0 return this.reference;
112    }
113   
 
114  0 toggle public String getPreviousVersion()
115    {
116  0 return this.previousVersion;
117    }
118   
 
119  0 toggle public Action getAction()
120    {
121  0 return this.action;
122    }
123   
 
124  0 toggle public Extension getNextExtension()
125    {
126  0 return this.nextExtension;
127    }
128   
 
129  0 toggle public Extension getPreviousExtension()
130    {
131  0 return this.previousExtension;
132    }
133    }
134   
135    /**
136    * Setup event listener.
137    */
 
138  1 toggle public DocumentsModifiedDuringDistributionListener()
139    {
140  1 super("DocumentsModifiedDuringDistributionListener", new DocumentCreatedEvent(), new DocumentDeletedEvent(),
141    new DocumentUpdatedEvent());
142    }
143   
 
144  310 toggle @Override
145    public void onEvent(Event event, Object source, Object data)
146    {
147  310 checkXARHandler(event, (XWikiDocument) source, (XWikiContext) data);
148  310 checkDistributionAction(event, (XWikiDocument) source, (XWikiContext) data);
149    }
150   
 
151  0 toggle private static Action toAction(Event event)
152    {
153  0 DocumentStatus.Action action;
154  0 if (event instanceof DocumentCreatedEvent) {
155  0 action = Action.CREATED;
156  0 } else if (event instanceof DocumentDeletedEvent) {
157  0 action = Action.DELETED;
158    } else {
159  0 action = Action.MODIFIED;
160    }
161   
162  0 return action;
163    }
164   
 
165  310 toggle private void checkXARHandler(Event event, XWikiDocument document, XWikiContext xcontext)
166    {
167  310 ExecutionContext context = this.execution.getContext();
168   
169  310 if (context != null) {
170  310 XarExtensionPlan xarExtensionPlan =
171    (XarExtensionPlan) context.getProperty(XarExtensionPlan.CONTEXTKEY_XARINSTALLPLAN);
172   
173  310 if (xarExtensionPlan != null) {
174  25 Request request = this.jobContext.getCurrentJob().getRequest();
175   
176    // It's a job started by the Distribution Wizard
177  25 if (StringUtils.equals(request.<String> getProperty("context.action"), "distribution")) {
178  0 String distributionWiki = request.getProperty("context.wiki");
179   
180  0 if (distributionWiki != null) {
181  0 DocumentReference reference = document.getDocumentReferenceWithLocale();
182   
183  0 DocumentStatus.Action action = toAction(event);
184  0 LocalExtension previousExtension = xarExtensionPlan.getPreviousXarExtension(reference);
185  0 LocalExtension nextExtension = xarExtensionPlan.getNextXarExtension(reference);
186   
187  0 addDocument(distributionWiki, document, action, previousExtension, nextExtension);
188    }
189    }
190    }
191    }
192    }
193   
 
194  310 toggle private void checkDistributionAction(Event event, XWikiDocument document, XWikiContext xcontext)
195    {
196  310 if (DistributionAction.DISTRIBUTION_ACTION.equals(xcontext.getAction())) {
197  0 String distributionWiki = xcontext.getOriginalWikiId();
198   
199  0 DocumentStatus.Action action = toAction(event);
200   
201  0 addDocument(distributionWiki, document, action, null, null);
202    }
203    }
204   
 
205  0 toggle private void addDocument(String distributionWiki, XWikiDocument document, Action action,
206    LocalExtension previousExtension, LocalExtension nextExtension)
207    {
208  0 Map<DocumentReference, DocumentStatus> wikiDocuments = this.documents.get(distributionWiki);
209   
210  0 if (wikiDocuments == null) {
211  0 wikiDocuments =
212    new HashMap<DocumentReference, DocumentsModifiedDuringDistributionListener.DocumentStatus>();
213  0 this.documents.put(distributionWiki, wikiDocuments);
214    }
215   
216  0 DocumentReference reference = document.getDocumentReferenceWithLocale();
217   
218  0 DocumentStatus currentStatus = wikiDocuments.get(reference);
219   
220  0 String previousVersion;
221  0 if (currentStatus != null) {
222  0 previousVersion = currentStatus.getPreviousVersion();
223   
224  0 if (action == Action.CREATED) {
225  0 if (previousVersion != null) {
226  0 action = Action.MODIFIED;
227    }
228  0 } else if (action == Action.DELETED) {
229  0 if (previousVersion == null) {
230    // Back to square one
231  0 wikiDocuments.remove(reference);
232   
233  0 return;
234    }
235  0 } else if (action == Action.MODIFIED) {
236  0 action = currentStatus.getAction();
237    }
238    } else {
239  0 if (action != Action.CREATED) {
240  0 previousVersion = document.getOriginalDocument().getVersion();
241    } else {
242  0 previousVersion = null;
243    }
244    }
245   
246  0 wikiDocuments.put(reference, new DocumentStatus(reference, previousVersion, action, previousExtension,
247    nextExtension));
248    }
249   
 
250  0 toggle public Map<String, Map<DocumentReference, DocumentStatus>> getDocuments()
251    {
252  0 return this.documents;
253    }
254    }