1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package com.xpn.xwiki.internal.observation.remote.converter

File DocumentEventConverter.java

 

Coverage histogram

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

Code metrics

10
33
4
1
138
85
11
0.33
8.25
4
2.75

Classes

Class Line # Actions
DocumentEventConverter 57 33 0% 11 5
0.8936170389.4%
 

Contributing tests

This file is covered by 1 test. .

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 com.xpn.xwiki.internal.observation.remote.converter;
21   
22    import java.io.Serializable;
23    import java.util.HashSet;
24    import java.util.Map;
25    import java.util.Set;
26   
27    import javax.inject.Named;
28    import javax.inject.Singleton;
29   
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.model.reference.DocumentReference;
35    import org.xwiki.observation.event.Event;
36    import org.xwiki.observation.remote.LocalEventData;
37    import org.xwiki.observation.remote.RemoteEventData;
38   
39    import com.xpn.xwiki.XWiki;
40    import com.xpn.xwiki.XWikiContext;
41    import com.xpn.xwiki.XWikiException;
42    import com.xpn.xwiki.doc.XWikiDeletedDocument;
43    import com.xpn.xwiki.doc.XWikiDocument;
44    import com.xpn.xwiki.store.XWikiRecycleBinStoreInterface;
45   
46    /**
47    * Convert all document event to remote events and back to local events.
48    * <p>
49    * It also make sure the context contains the proper information like the user or the wiki.
50    *
51    * @version $Id: 15a488e9bc18acef3457f8e8346b26607f6dbfe8 $
52    * @since 2.0M3
53    */
54    @Component
55    @Singleton
56    @Named("document")
 
57    public class DocumentEventConverter extends AbstractXWikiEventConverter
58    {
59    /**
60    * The events supported by this converter.
61    */
62    private static final Set<Class<? extends Event>> EVENTS = new HashSet<Class<? extends Event>>()
63    {
 
64  4 toggle {
65  4 add(DocumentDeletedEvent.class);
66  4 add(DocumentCreatedEvent.class);
67  4 add(DocumentUpdatedEvent.class);
68    }
69    };
70   
 
71  1043 toggle @Override
72    public boolean toRemote(LocalEventData localEvent, RemoteEventData remoteEvent)
73    {
74  1041 if (EVENTS.contains(localEvent.getEvent().getClass())) {
75    // fill the remote event
76  34 remoteEvent.setEvent((Serializable) localEvent.getEvent());
77  34 remoteEvent.setSource(serializeXWikiDocument((XWikiDocument) localEvent.getSource()));
78  34 remoteEvent.setData(serializeXWikiContext((XWikiContext) localEvent.getData()));
79   
80  34 return true;
81    }
82   
83  1007 return false;
84    }
85   
 
86  9 toggle @Override
87    public boolean fromRemote(RemoteEventData remoteEvent, LocalEventData localEvent)
88    {
89  9 if (EVENTS.contains(remoteEvent.getEvent().getClass())) {
90    // fill the local event
91  9 XWikiContext xcontext = unserializeXWikiContext(remoteEvent.getData());
92   
93  9 try {
94  9 if (xcontext != null) {
95  9 localEvent.setData(xcontext);
96  9 localEvent.setEvent((Event) remoteEvent.getEvent());
97   
98  9 if (remoteEvent.getEvent() instanceof DocumentDeletedEvent) {
99  1 localEvent.setSource(unserializeDeletdDocument(remoteEvent.getSource(), xcontext));
100    } else {
101  8 localEvent.setSource(unserializeDocument(remoteEvent.getSource()));
102    }
103    }
104    } catch (XWikiException e) {
105  0 this.logger.error("Failed to convert remote event [{}]", remoteEvent, e);
106    }
107   
108  9 return true;
109    }
110   
111  0 return false;
112    }
113   
 
114  1 toggle private XWikiDocument unserializeDeletdDocument(Serializable remoteData, XWikiContext xcontext)
115    throws XWikiException
116    {
117  1 Map<String, Serializable> remoteDataMap = (Map<String, Serializable>) remoteData;
118   
119  1 DocumentReference docReference = (DocumentReference) remoteDataMap.get(DOC_NAME);
120   
121  1 XWikiDocument doc = new XWikiDocument(docReference);
122   
123  1 XWikiDocument origDoc = new XWikiDocument(docReference);
124   
125    // We have to get deleted document from the trash (hoping it is in the trash...)
126  1 XWiki xwiki = xcontext.getWiki();
127  1 XWikiRecycleBinStoreInterface store = xwiki.getRecycleBinStore();
128  1 XWikiDeletedDocument[] deletedDocuments = store.getAllDeletedDocuments(origDoc, xcontext, true);
129  1 if (deletedDocuments != null && deletedDocuments.length > 0) {
130  1 long index = deletedDocuments[0].getId();
131  1 origDoc = store.restoreFromRecycleBin(doc, index, xcontext, true);
132    }
133   
134  1 doc.setOriginalDocument(origDoc);
135   
136  1 return doc;
137    }
138    }