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

File DomainObjectFactory.java

 

Coverage histogram

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

Code metrics

24
117
18
1
364
255
30
0.26
6.5
18
1.67

Classes

Class Line # Actions
DomainObjectFactory 65 117 0% 30 23
0.855345985.5%
 

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.rest.internal;
21   
22    import java.net.URI;
23    import java.util.Calendar;
24    import java.util.Collection;
25    import java.util.Date;
26    import java.util.List;
27   
28    import org.apache.commons.lang3.StringUtils;
29    import org.suigeneris.jrcs.rcs.Version;
30    import org.xwiki.logging.event.LogEvent;
31    import org.xwiki.rest.Relations;
32    import org.xwiki.rest.model.jaxb.Attachment;
33    import org.xwiki.rest.model.jaxb.Comment;
34    import org.xwiki.rest.model.jaxb.HistorySummary;
35    import org.xwiki.rest.model.jaxb.JobLog;
36    import org.xwiki.rest.model.jaxb.JobProgress;
37    import org.xwiki.rest.model.jaxb.JobStatus;
38    import org.xwiki.rest.model.jaxb.Link;
39    import org.xwiki.rest.model.jaxb.Object;
40    import org.xwiki.rest.model.jaxb.ObjectFactory;
41    import org.xwiki.rest.model.jaxb.ObjectSummary;
42    import org.xwiki.rest.model.jaxb.Page;
43    import org.xwiki.rest.model.jaxb.PageSummary;
44    import org.xwiki.rest.model.jaxb.Space;
45    import org.xwiki.rest.model.jaxb.Translations;
46    import org.xwiki.rest.model.jaxb.Wiki;
47    import org.xwiki.rest.model.jaxb.Xwiki;
48    import org.xwiki.rest.resources.SyntaxesResource;
49    import org.xwiki.rest.resources.pages.PageResource;
50    import org.xwiki.rest.resources.pages.PageTranslationVersionResource;
51    import org.xwiki.rest.resources.pages.PageVersionResource;
52    import org.xwiki.rest.resources.wikis.WikisResource;
53   
54    import com.xpn.xwiki.XWikiContext;
55    import com.xpn.xwiki.XWikiException;
56    import com.xpn.xwiki.api.Document;
57    import com.xpn.xwiki.api.XWiki;
58    import com.xpn.xwiki.objects.BaseObject;
59   
60    /**
61    * This class contains utility methods for building representations using model objects.
62    *
63    * @version $Id: f933e9934df7721192e16c8e06d2603a039f23a3 $
64    */
 
65    public class DomainObjectFactory
66    {
 
67  780 toggle public static ModelFactory getModelUtils()
68    {
69  780 return com.xpn.xwiki.web.Utils.getComponent(ModelFactory.class);
70    }
71   
 
72  1 toggle public static Xwiki createXWikiRoot(ObjectFactory objectFactory, URI baseUri, String version)
73    {
74  1 Xwiki xwiki = objectFactory.createXwiki().withVersion(version);
75   
76  1 String wikisUri = uri(baseUri, WikisResource.class);
77  1 Link wikisLink = objectFactory.createLink();
78  1 wikisLink.setHref(wikisUri);
79  1 wikisLink.setRel(Relations.WIKIS);
80  1 xwiki.getLinks().add(wikisLink);
81   
82  1 String syntaxesUri = uri(baseUri, SyntaxesResource.class);
83  1 Link syntaxesLink = objectFactory.createLink();
84  1 syntaxesLink.setHref(syntaxesUri);
85  1 syntaxesLink.setRel(Relations.SYNTAXES);
86  1 xwiki.getLinks().add(syntaxesLink);
87   
88  1 return xwiki;
89    }
90   
91    /**
92    * @deprecated since 7.3M1, use {@link ModelFactory#toRestWiki(URI, String)} instead
93    */
 
94  235 toggle @Deprecated
95    public static Wiki createWiki(ObjectFactory objectFactory, URI baseUri, String wikiName)
96    {
97  235 return getModelUtils().toRestWiki(baseUri, wikiName);
98    }
99   
100    /**
101    * @deprecated since 7.3M1, use {@link ModelFactory#toRestSpace(URI, String, List, Document)} instead
102    */
 
103  82 toggle @Deprecated
104    public static Space createSpace(ObjectFactory objectFactory, URI baseUri, String wikiName, List<String> spaces,
105    Document home)
106    {
107  82 return getModelUtils().toRestSpace(baseUri, wikiName, spaces, home);
108    }
109   
110    /**
111    * @deprecated since 7.3M1, use {@link ModelFactory#toRestTranslations(URI, Document)}
112    */
 
113  0 toggle @Deprecated
114    public static Translations createTranslations(ObjectFactory objectFactory, URI baseUri, Document doc)
115    throws XWikiException
116    {
117  0 return getModelUtils().toRestTranslations(baseUri, doc);
118    }
119   
120    /**
121    * @deprecated since 7.3M1, use {@link ModelFactory#toRestPageSummary(URI, Document, XWiki, Boolean)}
122    */
 
123  138 toggle @Deprecated
124    public static PageSummary createPageSummary(ObjectFactory objectFactory, URI baseUri, Document doc, XWiki xwikiApi,
125    Boolean withPrettyNames) throws XWikiException
126    {
127  138 return getModelUtils().toRestPageSummary(baseUri, doc, withPrettyNames);
128    }
129   
130    /**
131    * @deprecated since 7.3M1, use {@link ModelFactory#toRestPage(URI, URI, Document, boolean, XWiki, Boolean)}
132    */
 
133  5 toggle @Deprecated
134    public static Page createPage(ObjectFactory objectFactory, URI baseUri, URI self, Document doc, boolean useVersion,
135    XWiki xwikiApi, Boolean withPrettyNames) throws XWikiException
136    {
137  5 return getModelUtils().toRestPage(baseUri, self, doc, useVersion, withPrettyNames, false, false, false);
138    }
139   
 
140  33 toggle public static HistorySummary createHistorySummary(ObjectFactory objectFactory, URI baseUri, String wikiName,
141    List<String> spaces, String pageName, String language, Version version, String modifier, Date modified,
142    String comment, XWiki xwikiApi, Boolean withPrettyNames)
143    {
144  33 HistorySummary historySummary = objectFactory.createHistorySummary();
145   
146  33 String pageId = Utils.getPageId(wikiName, spaces, pageName);
147   
148  33 historySummary.setPageId(pageId);
149  33 historySummary.setWiki(wikiName);
150  33 historySummary.setSpace(Utils.getLocalSpaceId(spaces));
151  33 historySummary.setName(pageName);
152  33 historySummary.setVersion(version.toString());
153  33 historySummary.setMajorVersion(version.at(0));
154  33 historySummary.setMinorVersion(version.at(1));
155  33 historySummary.setComment(comment);
156  33 historySummary.setModifier(modifier);
157  33 if (withPrettyNames) {
158  0 historySummary.setModifierName(xwikiApi.getUserName(modifier, false));
159    }
160   
161  33 historySummary.setLanguage(language);
162   
163  33 Calendar calendar = Calendar.getInstance();
164  33 calendar.setTime(modified);
165  33 historySummary.setModified(calendar);
166   
167  33 if (language == null) {
168  32 String pageUri = uri(baseUri, PageVersionResource.class, wikiName, spaces, pageName, version);
169  32 Link pageLink = objectFactory.createLink();
170  32 pageLink.setHref(pageUri);
171  32 pageLink.setRel(Relations.PAGE);
172  32 historySummary.getLinks().add(pageLink);
173    } else {
174  1 String pageUri =
175    uri(baseUri, PageTranslationVersionResource.class, wikiName, spaces, pageName, language, version);
176  1 Link pageLink = objectFactory.createLink();
177  1 pageLink.setHref(pageUri);
178  1 pageLink.setRel(Relations.PAGE);
179  1 historySummary.getLinks().add(pageLink);
180    }
181   
182  33 return historySummary;
183    }
184   
185    /**
186    * @deprecated since 7.3M1, use
187    * {@link ModelFactory#toRestAttachment(URI, com.xpn.xwiki.api.Attachment, String, String, XWiki, Boolean, boolean)}
188    * instead
189    */
 
190  55 toggle @Deprecated
191    public static Attachment createAttachment(ObjectFactory objectFactory, URI baseUri,
192    com.xpn.xwiki.api.Attachment xwikiAttachment, String xwikiRelativeUrl, String xwikiAbsoluteUrl, XWiki xwikiApi,
193    Boolean withPrettyNames)
194    {
195  55 return getModelUtils().toRestAttachment(baseUri, xwikiAttachment, xwikiRelativeUrl, xwikiAbsoluteUrl,
196    withPrettyNames, false);
197    }
198   
199    /**
200    * @deprecated since 7.3M1, use
201    * {@link ModelFactory#toRestAttachment(URI, com.xpn.xwiki.api.Attachment, String, String, XWiki, Boolean, boolean)}
202    * instead
203    */
 
204  4 toggle @Deprecated
205    public static Attachment createAttachmentAtVersion(ObjectFactory objectFactory, URI baseUri,
206    com.xpn.xwiki.api.Attachment xwikiAttachment, String xwikiRelativeUrl, String xwikiAbsoluteUrl, XWiki xwikiApi,
207    Boolean withPrettyNames)
208    {
209  4 return getModelUtils().toRestAttachment(baseUri, xwikiAttachment, xwikiRelativeUrl, xwikiAbsoluteUrl,
210    withPrettyNames, true);
211    }
212   
 
213  6 toggle public static Comment createComment(ObjectFactory objectFactory, URI baseUri, Document doc,
214    com.xpn.xwiki.api.Object xwikiComment, XWiki xwikiApi, Boolean withPrettyNames)
215    {
216  6 Comment comment = objectFactory.createComment();
217  6 comment.setId(xwikiComment.getNumber());
218   
219  6 com.xpn.xwiki.api.Property property = xwikiComment.getProperty("author");
220  6 if (property != null) {
221  6 comment.setAuthor((String) property.getValue());
222  6 if (withPrettyNames) {
223  0 comment.setAuthorName(xwikiApi.getUserName((String) property.getValue(), false));
224    }
225    }
226   
227  6 property = xwikiComment.getProperty("date");
228  6 if (property != null) {
229  6 Calendar calendar = Calendar.getInstance();
230  6 calendar.setTime((Date) property.getValue());
231  6 comment.setDate(calendar);
232    }
233   
234  6 property = xwikiComment.getProperty("highlight");
235  6 if (property != null) {
236  0 comment.setHighlight((String) property.getValue());
237    }
238   
239  6 property = xwikiComment.getProperty("comment");
240  6 if (property != null) {
241  6 comment.setText((String) property.getValue());
242    }
243   
244  6 property = xwikiComment.getProperty("replyto");
245  6 if (property != null) {
246  0 comment.setReplyTo((Integer) property.getValue());
247    }
248   
249  6 String pageUri =
250    uri(baseUri, PageResource.class, doc.getWiki(), Utils.getSpacesFromSpaceId(doc.getSpace()), doc.getName());
251  6 Link pageLink = objectFactory.createLink();
252  6 pageLink.setHref(pageUri);
253  6 pageLink.setRel(Relations.PAGE);
254  6 comment.getLinks().add(pageLink);
255   
256  6 return comment;
257    }
258   
259    /**
260    * @deprecated since 7.3M1, use
261    * {@link ModelFactory#toRestObjectSummary(ObjectFactory, URI, XWikiContext, Document, BaseObject, boolean, XWiki, Boolean)}
262    * instead
263    */
 
264  16 toggle @Deprecated
265    public static ObjectSummary createObjectSummary(ObjectFactory objectFactory, URI baseUri,
266    XWikiContext xwikiContext, Document doc, BaseObject xwikiObject, boolean useVersion, XWiki xwikiApi,
267    Boolean withPrettyNames) throws XWikiException
268    {
269  16 return getModelUtils().toRestObjectSummary(baseUri, doc, xwikiObject, useVersion, withPrettyNames);
270    }
271   
272    /**
273    * @deprecated since 7.3M1, use
274    * {@link ModelFactory#toRestObject(URI, XWikiContext, Document, BaseObject, boolean, XWiki, Boolean, Boolean)}
275    * instead
276    */
 
277  37 toggle @Deprecated
278    public static Object createObject(ObjectFactory objectFactory, URI baseUri, XWikiContext xwikiContext,
279    Document doc, BaseObject xwikiObject, boolean useVersion, XWiki xwikiApi, Boolean withPrettyNames)
280    throws XWikiException
281    {
282  37 return getModelUtils().toRestObject(baseUri, doc, xwikiObject, useVersion, withPrettyNames);
283    }
284   
 
285  35 toggle public static JobStatus createJobStatus(ObjectFactory objectFactory, URI self,
286    org.xwiki.job.event.status.JobStatus jobStatus)
287    {
288  35 JobStatus status = objectFactory.createJobStatus();
289  35 status.setId(StringUtils.join(jobStatus.getRequest().getId(), "/"));
290  35 status.setState(jobStatus.getState().name());
291  35 status.setProgress(createJobProgress(objectFactory, jobStatus.getProgress()));
292  35 if (jobStatus.getStartDate() != null) {
293  35 Calendar calendarStartDate = Calendar.getInstance();
294  35 calendarStartDate.setTime(jobStatus.getStartDate());
295  35 status.setStartDate(calendarStartDate);
296    }
297  35 if (jobStatus.getEndDate() != null) {
298  27 Calendar calendarEndDate = Calendar.getInstance();
299  27 calendarEndDate.setTime(jobStatus.getEndDate());
300  27 status.setEndDate(calendarEndDate);
301    }
302  35 if (self != null) {
303  35 Link link = objectFactory.createLink();
304  35 link.setHref(self.toString());
305  35 link.setRel(Relations.SELF);
306  35 status.getLinks().add(link);
307    }
308   
309  35 return status;
310    }
311   
 
312  35 toggle public static JobProgress createJobProgress(ObjectFactory objectFactory,
313    org.xwiki.job.event.status.JobProgress jobProgress)
314    {
315  35 JobProgress progress = objectFactory.createJobProgress();
316  35 progress.setOffset(jobProgress.getOffset());
317  35 progress.setCurrentLevelOffset(jobProgress.getCurrentLevelOffset());
318  35 return progress;
319    }
320   
 
321  7 toggle public static JobLog createLog(ObjectFactory objectFactory, URI self, Collection<LogEvent> logs)
322    {
323  7 JobLog log = objectFactory.createJobLog();
324  7 for (LogEvent logEvent : logs) {
325  0 org.xwiki.rest.model.jaxb.LogEvent event = objectFactory.createLogEvent();
326  0 event.setLevel(logEvent.getLevel().name());
327  0 Calendar calendarDate = Calendar.getInstance();
328  0 calendarDate.setTimeInMillis(logEvent.getTimeStamp());
329  0 event.setDate(calendarDate);
330  0 event.setFormattedMessage(logEvent.getFormattedMessage());
331  0 log.getLogEvents().add(event);
332    }
333  7 if (self != null) {
334  7 Link link = objectFactory.createLink();
335  7 link.setHref(self.toString());
336  7 link.setRel(Relations.SELF);
337  7 log.getLinks().add(link);
338    }
339  7 return log;
340    }
341   
342    /**
343    * @deprecated since 7.3M1, use {@link ModelFactory#toRestClass(URI, String, com.xpn.xwiki.api.Class)}
344    */
 
345  208 toggle public static org.xwiki.rest.model.jaxb.Class createClass(ObjectFactory objectFactory, URI baseUri,
346    String wikiName, com.xpn.xwiki.api.Class xwikiClass)
347    {
348  208 return getModelUtils().toRestClass(baseUri, xwikiClass);
349    }
350   
351    /**
352    * Creates an URI to access the specified resource. The given path elements are encoded before being inserted into
353    * the resource path.
354    *
355    * @param baseURI the base URI
356    * @param resourceClass the resource class, used to get the URI path
357    * @param pathElements the path elements to insert in the resource path
358    * @return an URI that can be used to access the specified resource
359    */
 
360  41 toggle private static String uri(URI baseURI, java.lang.Class<?> resourceClass, java.lang.Object... pathElements)
361    {
362  41 return Utils.createURI(baseURI, resourceClass, pathElements).toString();
363    }
364    }