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

File AbstractXWikiEventConverter.java

 

Coverage histogram

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

Code metrics

16
47
6
1
211
113
15
0.32
7.83
6
2.5

Classes

Class Line # Actions
AbstractXWikiEventConverter 45 47 0% 15 8
0.88405888.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.HashMap;
24    import java.util.Map;
25   
26    import javax.inject.Inject;
27   
28    import org.slf4j.Logger;
29    import org.xwiki.context.Execution;
30    import org.xwiki.context.ExecutionContext;
31    import org.xwiki.model.reference.DocumentReference;
32    import org.xwiki.observation.remote.converter.AbstractEventConverter;
33   
34    import com.xpn.xwiki.XWikiContext;
35    import com.xpn.xwiki.XWikiException;
36    import com.xpn.xwiki.doc.XWikiDocument;
37    import com.xpn.xwiki.util.XWikiStubContextProvider;
38   
39    /**
40    * Provide some serialization tools for old apis like {@link XWikiDocument} and {@link XWikiContext}.
41    *
42    * @version $Id: 4059a6daf2cdb3633c9e17dd21abf4ebcbb08e7d $
43    * @since 2.0M4
44    */
 
45    public abstract class AbstractXWikiEventConverter extends AbstractEventConverter
46    {
47    protected static final String CONTEXT_WIKI = "contextwiki";
48   
49    protected static final String CONTEXT_USER = "contextuser";
50   
51    protected static final String DOC_NAME = "docname";
52   
53    protected static final String DOC_VERSION = "docversion";
54   
55    protected static final String DOC_LANGUAGE = "doclanguage";
56   
57    protected static final String ORIGDOC_VERSION = "origdocversion";
58   
59    protected static final String ORIGDOC_LANGUAGE = "origdoclanguage";
60   
61    /**
62    * The logger to log.
63    */
64    @Inject
65    protected Logger logger;
66   
67    /**
68    * Used to set some proper context informations.
69    */
70    @Inject
71    private Execution execution;
72   
73    /**
74    * Generate stub XWikiContext.
75    */
76    @Inject
77    private XWikiStubContextProvider stubContextProvider;
78   
79    /**
80    * @param context the XWiki context to serialize
81    * @return the serialized version of the context
82    */
 
83  34 toggle protected Serializable serializeXWikiContext(XWikiContext context)
84    {
85  34 HashMap<String, Serializable> remoteDataMap = new HashMap<String, Serializable>();
86   
87  34 remoteDataMap.put(CONTEXT_WIKI, context.getWikiId());
88  34 remoteDataMap.put(CONTEXT_USER, context.getUser());
89   
90  34 return remoteDataMap;
91    }
92   
93    /**
94    * @return a stub XWikiContext, null if none can be generated (XWiki has never been accessed yet)
95    */
 
96  21 toggle private XWikiContext getXWikiStubContext()
97    {
98  21 ExecutionContext context = this.execution.getContext();
99  21 XWikiContext xcontext = (XWikiContext) context.getProperty(XWikiContext.EXECUTIONCONTEXT_KEY);
100   
101  21 if (xcontext == null) {
102  0 xcontext = this.stubContextProvider.createStubContext();
103   
104  0 if (xcontext != null) {
105  0 xcontext.declareInExecutionContext(context);
106    }
107    }
108   
109  21 return xcontext;
110    }
111   
112    /**
113    * @param remoteData the serialized version of the context
114    * @return the XWiki context
115    */
 
116  9 toggle protected XWikiContext unserializeXWikiContext(Serializable remoteData)
117    {
118  9 XWikiContext xcontext = getXWikiStubContext();
119   
120  9 Map<String, Serializable> remoteDataMap = (Map<String, Serializable>) remoteData;
121   
122  9 if (xcontext != null) {
123  9 xcontext.setWikiId((String) remoteDataMap.get(CONTEXT_WIKI));
124  9 xcontext.setUser((String) remoteDataMap.get(CONTEXT_USER));
125    } else {
126  0 this.logger.warn("Can't get a proper XWikiContext."
127    + " It generally mean that the wiki has never been fully initialized,"
128    + " i.e. has never been accesses at least once");
129    }
130   
131  9 return xcontext;
132    }
133   
134    /**
135    * @param document the document to serialize
136    * @return the serialized version of the document
137    */
 
138  34 toggle protected Serializable serializeXWikiDocument(XWikiDocument document)
139    {
140  34 HashMap<String, Serializable> remoteDataMap = new HashMap<String, Serializable>();
141   
142  34 remoteDataMap.put(DOC_NAME, document.getDocumentReference());
143   
144  34 if (!document.isNew()) {
145  32 remoteDataMap.put(DOC_VERSION, document.getVersion());
146  32 remoteDataMap.put(DOC_LANGUAGE, document.getLanguage());
147    }
148   
149  34 XWikiDocument originalDocument = document.getOriginalDocument();
150   
151  34 if (originalDocument != null && !originalDocument.isNew()) {
152  6 remoteDataMap.put(ORIGDOC_VERSION, originalDocument.getVersion());
153  6 remoteDataMap.put(ORIGDOC_LANGUAGE, originalDocument.getLanguage());
154    }
155   
156  34 return remoteDataMap;
157    }
158   
 
159  12 toggle protected XWikiDocument getDocument(DocumentReference documentReference, String language, String version)
160    throws XWikiException
161    {
162  12 XWikiContext xcontext = getXWikiStubContext();
163   
164  12 XWikiDocument document = new XWikiDocument(documentReference);
165  12 document.setLanguage(language);
166   
167    // Force bypassing the cache to make extra sure we get the last version of the document.
168  12 XWikiDocument targetDocument = xcontext.getWiki().getNotCacheStore().loadXWikiDoc(document, xcontext);
169   
170  12 if (!targetDocument.getVersion().equals(version)) {
171    // It's not the last version of the document, ask versioning store.
172  3 targetDocument = xcontext.getWiki().getVersioningStore().loadXWikiDoc(document, version, xcontext);
173    }
174   
175  12 return targetDocument;
176    }
177   
178    /**
179    * @param remoteData the serialized version of the document
180    * @return the document
181    * @throws XWikiException when failing to unserialize document
182    */
 
183  8 toggle protected XWikiDocument unserializeDocument(Serializable remoteData) throws XWikiException
184    {
185  8 Map<String, Serializable> remoteDataMap = (Map<String, Serializable>) remoteData;
186   
187  8 DocumentReference docReference = (DocumentReference) remoteDataMap.get(DOC_NAME);
188   
189  8 XWikiDocument doc;
190  8 if (remoteDataMap.get(DOC_VERSION) == null) {
191  1 doc = new XWikiDocument(docReference);
192    } else {
193  7 doc =
194    getDocument(docReference, (String) remoteDataMap.get(DOC_LANGUAGE),
195    (String) remoteDataMap.get(DOC_VERSION));
196    }
197   
198  8 XWikiDocument origDoc;
199  8 if (remoteDataMap.get(ORIGDOC_VERSION) == null) {
200  3 origDoc = new XWikiDocument(docReference);
201    } else {
202  5 origDoc =
203    getDocument(docReference, (String) remoteDataMap.get(ORIGDOC_LANGUAGE),
204    (String) remoteDataMap.get(ORIGDOC_VERSION));
205    }
206   
207  8 doc.setOriginalDocument(origDoc);
208   
209  8 return doc;
210    }
211    }