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

File ModelFactory.java

 

Coverage histogram

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

Code metrics

94
428
16
1
896
711
65
0.15
26.75
16
4.06

Classes

Class Line # Actions
ModelFactory 109 428 0% 65 64
0.881040988.1%
 

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.net.URL;
24    import java.util.ArrayList;
25    import java.util.Calendar;
26    import java.util.Formatter;
27    import java.util.HashSet;
28    import java.util.List;
29    import java.util.Set;
30    import java.util.Vector;
31   
32    import javax.inject.Inject;
33    import javax.inject.Provider;
34    import javax.inject.Singleton;
35   
36    import org.apache.commons.lang3.StringUtils;
37    import org.xwiki.component.annotation.Component;
38    import org.xwiki.model.reference.DocumentReference;
39    import org.xwiki.model.reference.EntityReferenceSerializer;
40    import org.xwiki.model.reference.ObjectReference;
41    import org.xwiki.rest.Relations;
42    import org.xwiki.rest.model.jaxb.Attachment;
43    import org.xwiki.rest.model.jaxb.Attribute;
44    import org.xwiki.rest.model.jaxb.Class;
45    import org.xwiki.rest.model.jaxb.Link;
46    import org.xwiki.rest.model.jaxb.Object;
47    import org.xwiki.rest.model.jaxb.ObjectFactory;
48    import org.xwiki.rest.model.jaxb.ObjectSummary;
49    import org.xwiki.rest.model.jaxb.Page;
50    import org.xwiki.rest.model.jaxb.PageSummary;
51    import org.xwiki.rest.model.jaxb.Property;
52    import org.xwiki.rest.model.jaxb.Space;
53    import org.xwiki.rest.model.jaxb.Translation;
54    import org.xwiki.rest.model.jaxb.Translations;
55    import org.xwiki.rest.model.jaxb.Wiki;
56    import org.xwiki.rest.resources.ModificationsResource;
57    import org.xwiki.rest.resources.SyntaxesResource;
58    import org.xwiki.rest.resources.attachments.AttachmentResource;
59    import org.xwiki.rest.resources.attachments.AttachmentVersionResource;
60    import org.xwiki.rest.resources.attachments.AttachmentsAtPageVersionResource;
61    import org.xwiki.rest.resources.attachments.AttachmentsResource;
62    import org.xwiki.rest.resources.classes.ClassPropertiesResource;
63    import org.xwiki.rest.resources.classes.ClassPropertyResource;
64    import org.xwiki.rest.resources.classes.ClassResource;
65    import org.xwiki.rest.resources.classes.ClassesResource;
66    import org.xwiki.rest.resources.comments.CommentsResource;
67    import org.xwiki.rest.resources.comments.CommentsVersionResource;
68    import org.xwiki.rest.resources.objects.AllObjectsForClassNameResource;
69    import org.xwiki.rest.resources.objects.ObjectAtPageVersionResource;
70    import org.xwiki.rest.resources.objects.ObjectPropertiesAtPageVersionResource;
71    import org.xwiki.rest.resources.objects.ObjectPropertiesResource;
72    import org.xwiki.rest.resources.objects.ObjectPropertyAtPageVersionResource;
73    import org.xwiki.rest.resources.objects.ObjectPropertyResource;
74    import org.xwiki.rest.resources.objects.ObjectResource;
75    import org.xwiki.rest.resources.objects.ObjectsAtPageVersionResource;
76    import org.xwiki.rest.resources.objects.ObjectsResource;
77    import org.xwiki.rest.resources.pages.PageChildrenResource;
78    import org.xwiki.rest.resources.pages.PageHistoryResource;
79    import org.xwiki.rest.resources.pages.PageResource;
80    import org.xwiki.rest.resources.pages.PageTagsResource;
81    import org.xwiki.rest.resources.pages.PageTranslationHistoryResource;
82    import org.xwiki.rest.resources.pages.PageTranslationResource;
83    import org.xwiki.rest.resources.pages.PagesResource;
84    import org.xwiki.rest.resources.spaces.SpaceResource;
85    import org.xwiki.rest.resources.spaces.SpaceSearchResource;
86    import org.xwiki.rest.resources.spaces.SpacesResource;
87    import org.xwiki.rest.resources.wikis.WikiSearchQueryResource;
88    import org.xwiki.rest.resources.wikis.WikiSearchResource;
89   
90    import com.xpn.xwiki.XWikiContext;
91    import com.xpn.xwiki.XWikiException;
92    import com.xpn.xwiki.api.Document;
93    import com.xpn.xwiki.api.PropertyClass;
94    import com.xpn.xwiki.doc.XWikiDocument;
95    import com.xpn.xwiki.objects.BaseObject;
96    import com.xpn.xwiki.objects.BaseProperty;
97    import com.xpn.xwiki.objects.PropertyInterface;
98    import com.xpn.xwiki.objects.classes.BaseClass;
99    import com.xpn.xwiki.objects.classes.ListClass;
100   
101    /**
102    * Various common tools for resources.
103    *
104    * @version $Id: c55159bf17c77ef10723a3b1409748e75b1ba474 $
105    * @since 7.3M1
106    */
107    @Component(roles = ModelFactory.class)
108    @Singleton
 
109    public class ModelFactory
110    {
111    private final ObjectFactory objectFactory;
112   
113    @Inject
114    private Provider<XWikiContext> xcontextProvider;
115   
116    @Inject
117    private EntityReferenceSerializer<String> serializer;
118   
 
119  12 toggle public ModelFactory()
120    {
121  12 this.objectFactory = new ObjectFactory();
122    }
123   
 
124  186 toggle public boolean toDocument(Document doc, org.xwiki.rest.model.jaxb.Page restPage) throws XWikiException
125    {
126  186 boolean modified = false;
127   
128  186 if (restPage.getContent() != null) {
129  31 doc.setContent(restPage.getContent());
130  31 modified = true;
131    }
132   
133  186 if (restPage.getTitle() != null) {
134  17 doc.setTitle(restPage.getTitle());
135  17 modified = true;
136    }
137   
138  186 if (restPage.getParent() != null) {
139  21 doc.setParent(restPage.getParent());
140  21 modified = true;
141    }
142   
143  186 if (restPage.getSyntax() != null) {
144  3 doc.setSyntaxId(restPage.getSyntax());
145  3 modified = true;
146    }
147   
148  186 doc.setHidden(restPage.isHidden());
149   
150    // Set objects
151  186 if (restPage.getObjects() != null) {
152  87 Set<ObjectReference> newReferences = new HashSet<>();
153   
154    // Add/update objects
155  87 for (ObjectSummary restObjectSummary : restPage.getObjects().getObjectSummaries()) {
156  109 if (restObjectSummary != null) {
157  109 org.xwiki.rest.model.jaxb.Object restObject = (org.xwiki.rest.model.jaxb.Object) restObjectSummary;
158  109 com.xpn.xwiki.api.Object xwikiObject =
159    doc.getObject(restObject.getClassName(), restObject.getNumber());
160  109 if (xwikiObject == null) {
161  40 xwikiObject = doc.newObject(restObject.getClassName());
162    }
163  109 toObject(xwikiObject, restObject);
164  109 modified = true;
165   
166  109 newReferences.add(xwikiObject.getReference());
167    }
168    }
169   
170    // Remove objects
171  87 List<com.xpn.xwiki.api.Object> toRemove = new ArrayList<>();
172  87 for (Vector<com.xpn.xwiki.api.Object> objects : doc.getxWikiObjects().values()) {
173  106 for (com.xpn.xwiki.api.Object object : objects) {
174  111 if (!newReferences.contains(object.getReference())) {
175  2 toRemove.add(object);
176    }
177    }
178    }
179  87 for (com.xpn.xwiki.api.Object obj : toRemove) {
180  2 doc.removeObject(obj);
181  2 modified = true;
182    }
183    }
184   
185    // TODO: add support for class, see http://jira.xwiki.org/browse/XWIKI-12597
186   
187    // TODO: add attachment content to the REST model ?
188   
189  186 return modified;
190    }
191   
 
192  86 toggle private void fillObjectSummary(ObjectSummary objectSummary, Document doc, BaseObject xwikiObject,
193    Boolean withPrettyNames)
194    {
195  86 objectSummary.setId(String.format("%s:%s", doc.getPrefixedFullName(), xwikiObject.getGuid()));
196  86 objectSummary.setGuid(xwikiObject.getGuid());
197  86 objectSummary.setPageId(doc.getPrefixedFullName());
198  86 objectSummary.setPageVersion(doc.getVersion());
199  86 objectSummary.setPageAuthor(doc.getAuthor());
200  86 if (withPrettyNames) {
201  0 XWikiContext xwikiContext = this.xcontextProvider.get();
202  0 objectSummary.setPageAuthorName(xwikiContext.getWiki().getUserName(doc.getAuthor(), null, false,
203    xwikiContext));
204    }
205  86 objectSummary.setWiki(doc.getWiki());
206  86 objectSummary.setSpace(doc.getSpace());
207  86 objectSummary.setPageName(doc.getName());
208  86 objectSummary.setClassName(xwikiObject.getClassName());
209  86 objectSummary.setNumber(xwikiObject.getNumber());
210   
211  86 String[] propertyNames = xwikiObject.getPropertyNames();
212  86 if (propertyNames.length > 0) {
213  83 try {
214  83 objectSummary.setHeadline(serializePropertyValue(xwikiObject.get(propertyNames[0])));
215    } catch (XWikiException e) {
216    // Should never happen
217    }
218    }
219    }
220   
 
221  16 toggle public ObjectSummary toRestObjectSummary(URI baseUri, Document doc, BaseObject xwikiObject, boolean useVersion,
222    Boolean withPrettyNames)
223    {
224  16 ObjectSummary objectSummary = objectFactory.createObjectSummary();
225  16 fillObjectSummary(objectSummary, doc, xwikiObject, withPrettyNames);
226   
227  16 Link objectLink = getObjectLink(objectFactory, baseUri, doc, xwikiObject, useVersion, Relations.OBJECT);
228  16 objectSummary.getLinks().add(objectLink);
229   
230  16 String propertiesUri;
231  16 if (useVersion) {
232  0 propertiesUri =
233    Utils.createURI(baseUri, ObjectPropertiesAtPageVersionResource.class, doc.getWiki(),
234    Utils.getSpacesFromSpaceId(doc.getSpace()), doc.getName(), doc.getVersion(),
235    xwikiObject.getClassName(), xwikiObject.getNumber()).toString();
236    } else {
237  16 propertiesUri =
238    Utils.createURI(baseUri, ObjectPropertiesResource.class, doc.getWiki(),
239    Utils.getSpacesFromSpaceId(doc.getSpace()), doc.getName(), xwikiObject.getClassName(),
240    xwikiObject.getNumber()).toString();
241    }
242   
243  16 Link propertyLink = objectFactory.createLink();
244  16 propertyLink.setHref(propertiesUri);
245  16 propertyLink.setRel(Relations.PROPERTIES);
246  16 objectSummary.getLinks().add(propertyLink);
247   
248  16 return objectSummary;
249    }
250   
 
251  116 toggle public void toObject(com.xpn.xwiki.api.Object xwikiObject, org.xwiki.rest.model.jaxb.Object restObject)
252    {
253  116 for (Property restProperty : restObject.getProperties()) {
254  409 xwikiObject.set(restProperty.getName(), restProperty.getValue());
255    }
256    }
257   
 
258  70 toggle public Object toRestObject(URI baseUri, Document doc, BaseObject xwikiObject, boolean useVersion,
259    Boolean withPrettyNames)
260    {
261  70 Object object = this.objectFactory.createObject();
262  70 fillObjectSummary(object, doc, xwikiObject, withPrettyNames);
263   
264  70 XWikiContext xwikiContext = this.xcontextProvider.get();
265  70 BaseClass xwikiClass = xwikiObject.getXClass(xwikiContext);
266   
267  70 for (java.lang.Object propertyClassObject : xwikiClass.getProperties()) {
268  73 com.xpn.xwiki.objects.classes.PropertyClass propertyClass =
269    (com.xpn.xwiki.objects.classes.PropertyClass) propertyClassObject;
270   
271  73 Property property = this.objectFactory.createProperty();
272   
273  73 for (java.lang.Object o : propertyClass.getProperties()) {
274  940 BaseProperty baseProperty = (BaseProperty) o;
275  940 Attribute attribute = this.objectFactory.createAttribute();
276  940 attribute.setName(baseProperty.getName());
277   
278    /* Check for null values in order to prevent NPEs */
279  940 if (baseProperty.getValue() != null) {
280  940 attribute.setValue(baseProperty.getValue().toString());
281    } else {
282  0 attribute.setValue("");
283    }
284   
285  940 property.getAttributes().add(attribute);
286    }
287   
288  73 if (propertyClass instanceof ListClass) {
289  72 ListClass listClass = (ListClass) propertyClass;
290   
291  72 List allowedValueList = listClass.getList(xwikiContext);
292   
293  72 if (!allowedValueList.isEmpty()) {
294  2 Formatter f = new Formatter();
295  14 for (int i = 0; i < allowedValueList.size(); i++) {
296  12 if (i != allowedValueList.size() - 1) {
297  10 f.format("%s,", allowedValueList.get(i).toString());
298    } else {
299  2 f.format("%s", allowedValueList.get(i).toString());
300    }
301    }
302   
303  2 Attribute attribute = this.objectFactory.createAttribute();
304  2 attribute.setName(Constants.ALLOWED_VALUES_ATTRIBUTE_NAME);
305  2 attribute.setValue(f.toString());
306  2 property.getAttributes().add(attribute);
307    }
308    }
309   
310  73 property.setName(propertyClass.getName());
311  73 property.setType(propertyClass.getClassType());
312  73 try {
313  73 property.setValue(serializePropertyValue(xwikiObject.get(propertyClass.getName())));
314    } catch (XWikiException e) {
315    // Should never happen
316    }
317   
318  73 String propertyUri;
319   
320  73 if (useVersion) {
321  15 propertyUri =
322    Utils.createURI(baseUri, ObjectPropertyAtPageVersionResource.class, doc.getWiki(),
323    Utils.getSpacesFromSpaceId(doc.getSpace()), doc.getName(), doc.getVersion(),
324    xwikiObject.getClassName(), xwikiObject.getNumber(), propertyClass.getName()).toString();
325    } else {
326  58 propertyUri =
327    Utils.createURI(baseUri, ObjectPropertyResource.class, doc.getWiki(),
328    Utils.getSpacesFromSpaceId(doc.getSpace()), doc.getName(), xwikiObject.getClassName(),
329    xwikiObject.getNumber(), propertyClass.getName()).toString();
330    }
331  73 Link propertyLink = this.objectFactory.createLink();
332  73 propertyLink.setHref(propertyUri);
333  73 propertyLink.setRel(Relations.SELF);
334  73 property.getLinks().add(propertyLink);
335   
336  73 object.getProperties().add(property);
337    }
338   
339  70 Link objectLink = getObjectLink(this.objectFactory, baseUri, doc, xwikiObject, useVersion, Relations.SELF);
340  70 object.getLinks().add(objectLink);
341   
342  70 return object;
343    }
344   
 
345  86 toggle private static Link getObjectLink(ObjectFactory objectFactory, URI baseUri, Document doc, BaseObject xwikiObject,
346    boolean useVersion, String relation)
347    {
348  86 String objectUri;
349   
350  86 if (useVersion) {
351  15 objectUri =
352    Utils.createURI(baseUri, ObjectAtPageVersionResource.class, doc.getWiki(),
353    Utils.getSpacesFromSpaceId(doc.getSpace()), doc.getName(), doc.getVersion(),
354    xwikiObject.getClassName(), xwikiObject.getNumber()).toString();
355    } else {
356  71 objectUri =
357    Utils.createURI(baseUri, ObjectResource.class, doc.getWiki(),
358    Utils.getSpacesFromSpaceId(doc.getSpace()), doc.getName(), xwikiObject.getClassName(),
359    xwikiObject.getNumber()).toString();
360    }
361  86 Link objectLink = objectFactory.createLink();
362  86 objectLink.setHref(objectUri);
363  86 objectLink.setRel(relation);
364   
365  86 return objectLink;
366    }
367   
368    // To REST
369   
 
370  235 toggle public Wiki toRestWiki(URI baseUri, String wikiName)
371    {
372  235 Wiki wiki = this.objectFactory.createWiki().withId(wikiName).withName(wikiName);
373   
374  235 String spacesUri = Utils.createURI(baseUri, SpacesResource.class, wikiName).toString();
375  235 Link spacesLink = this.objectFactory.createLink();
376  235 spacesLink.setHref(spacesUri);
377  235 spacesLink.setRel(Relations.SPACES);
378  235 wiki.getLinks().add(spacesLink);
379   
380  235 String classesUri = Utils.createURI(baseUri, ClassesResource.class, wikiName).toString();
381  235 Link classesLink = this.objectFactory.createLink();
382  235 classesLink.setHref(classesUri);
383  235 classesLink.setRel(Relations.CLASSES);
384  235 wiki.getLinks().add(classesLink);
385   
386  235 String modificationsUri = Utils.createURI(baseUri, ModificationsResource.class, wikiName).toString();
387  235 Link modificationsLink = this.objectFactory.createLink();
388  235 modificationsLink.setHref(modificationsUri);
389  235 modificationsLink.setRel(Relations.MODIFICATIONS);
390  235 wiki.getLinks().add(modificationsLink);
391   
392  235 String searchUri = Utils.createURI(baseUri, WikiSearchResource.class, wikiName).toString();
393  235 Link searchLink = this.objectFactory.createLink();
394  235 searchLink.setHref(searchUri);
395  235 searchLink.setRel(Relations.SEARCH);
396  235 wiki.getLinks().add(searchLink);
397   
398  235 String queryUri = Utils.createURI(baseUri, WikiSearchQueryResource.class, wikiName).toString();
399  235 Link queryLink = this.objectFactory.createLink();
400  235 queryLink.setHref(queryUri);
401  235 queryLink.setRel(Relations.QUERY);
402  235 wiki.getLinks().add(queryLink);
403   
404  235 return wiki;
405    }
406   
 
407  82 toggle public Space toRestSpace(URI baseUri, String wikiName, List<String> spaces, Document home)
408    {
409  82 Space space = this.objectFactory.createSpace();
410  82 space.setId(Utils.getSpaceId(wikiName, spaces));
411  82 space.setWiki(wikiName);
412    // the name of the space is the last element of the spaces list
413  82 space.setName(spaces.get(spaces.size() - 1));
414  82 if (home != null) {
415  0 space.setHome(home.getPrefixedFullName());
416  0 space.setXwikiRelativeUrl(home.getURL("view"));
417  0 space.setXwikiAbsoluteUrl(home.getExternalURL("view"));
418    }
419   
420  82 String pagesUri = Utils.createURI(baseUri, PagesResource.class, wikiName, spaces).toString();
421  82 Link pagesLink = this.objectFactory.createLink();
422  82 pagesLink.setHref(pagesUri);
423  82 pagesLink.setRel(Relations.PAGES);
424  82 space.getLinks().add(pagesLink);
425   
426  82 if (home != null) {
427  0 String homeUri = Utils.createURI(baseUri, PageResource.class, wikiName, spaces, home.getName()).toString();
428  0 Link homeLink = this.objectFactory.createLink();
429  0 homeLink.setHref(homeUri);
430  0 homeLink.setRel(Relations.HOME);
431  0 space.getLinks().add(homeLink);
432    }
433   
434  82 String searchUri = Utils.createURI(baseUri, SpaceSearchResource.class, wikiName, spaces).toString();
435  82 Link searchLink = this.objectFactory.createLink();
436  82 searchLink.setHref(searchUri);
437  82 searchLink.setRel(Relations.SEARCH);
438  82 space.getLinks().add(searchLink);
439   
440  82 return space;
441    }
442   
 
443  553 toggle public Translations toRestTranslations(URI baseUri, Document doc) throws XWikiException
444    {
445  553 Translations translations = this.objectFactory.createTranslations();
446   
447  553 List<String> languages = doc.getTranslationList();
448   
449  553 List<String> spaces = Utils.getSpacesFromSpaceId(doc.getSpace());
450   
451  553 if (!languages.isEmpty()) {
452  11 if (!doc.getDefaultLanguage().equals("")) {
453  0 translations.setDefault(doc.getDefaultLanguage());
454   
455  0 Translation translation = this.objectFactory.createTranslation();
456  0 translation.setLanguage(doc.getDefaultLanguage());
457   
458    /* Add the default page with the default translation explicitely */
459  0 String pageTranslationUri =
460    Utils.createURI(baseUri, PageResource.class, doc.getWiki(), spaces, doc.getName()).toString();
461  0 Link pageTranslationLink = this.objectFactory.createLink();
462  0 pageTranslationLink.setHref(pageTranslationUri);
463  0 pageTranslationLink.setRel(Relations.PAGE);
464  0 translation.getLinks().add(pageTranslationLink);
465   
466  0 String historyUri =
467    Utils.createURI(baseUri, PageHistoryResource.class, doc.getWiki(), spaces, doc.getName())
468    .toString();
469  0 Link historyLink = this.objectFactory.createLink();
470  0 historyLink.setHref(historyUri);
471  0 historyLink.setRel(Relations.HISTORY);
472  0 translation.getLinks().add(historyLink);
473   
474  0 translations.getTranslations().add(translation);
475    }
476    }
477   
478  553 for (String language : languages) {
479  11 Translation translation = this.objectFactory.createTranslation();
480  11 translation.setLanguage(language);
481   
482  11 String pageTranslationUri =
483    Utils.createURI(baseUri, PageTranslationResource.class, doc.getWiki(), spaces, doc.getName(), language)
484    .toString();
485  11 Link pageTranslationLink = this.objectFactory.createLink();
486  11 pageTranslationLink.setHref(pageTranslationUri);
487  11 pageTranslationLink.setRel(Relations.PAGE);
488  11 translation.getLinks().add(pageTranslationLink);
489   
490  11 String historyUri =
491    Utils.createURI(baseUri, PageTranslationHistoryResource.class, doc.getWiki(), spaces, doc.getName(),
492    language).toString();
493  11 Link historyLink = this.objectFactory.createLink();
494  11 historyLink.setHref(historyUri);
495  11 historyLink.setRel(Relations.HISTORY);
496  11 translation.getLinks().add(historyLink);
497   
498  11 translations.getTranslations().add(translation);
499    }
500   
501  553 return translations;
502    }
503   
504    /**
505    * This method is used to fill the "common part" of a Page and a PageSummary.
506    */
 
507  553 toggle private void toRestPageSummary(PageSummary pageSummary, URI baseUri, Document doc, boolean useVersion,
508    Boolean withPrettyNames) throws XWikiException
509    {
510  553 XWikiContext xwikiContext = this.xcontextProvider.get();
511   
512  553 pageSummary.setWiki(doc.getWiki());
513  553 pageSummary.setFullName(doc.getFullName());
514  553 pageSummary.setId(doc.getPrefixedFullName());
515  553 pageSummary.setSpace(doc.getSpace());
516  553 pageSummary.setName(doc.getName());
517  553 pageSummary.setTitle(doc.getDisplayTitle());
518  553 pageSummary.setXwikiRelativeUrl(doc.getURL("view"));
519  553 pageSummary.setXwikiAbsoluteUrl(doc.getExternalURL("view"));
520  553 pageSummary.setTranslations(toRestTranslations(baseUri, doc));
521  553 pageSummary.setSyntax(doc.getSyntax().toIdString());
522  553 pageSummary.setVersion(doc.getVersion());
523  553 pageSummary.setAuthor(doc.getAuthor());
524  553 if (withPrettyNames) {
525  0 pageSummary.setAuthorName(xwikiContext.getWiki().getUserName(doc.getAuthor(), null, false, xwikiContext));
526    }
527   
528  553 pageSummary.setParent(doc.getParent());
529  553 DocumentReference parentReference = doc.getParentReference();
530  553 boolean parentExist = parentReference != null && xwikiContext.getWiki().exists(parentReference, xwikiContext);
531    // parentId must not be set if the parent document does not exist.
532  553 if (parentExist) {
533  32 pageSummary.setParentId(this.serializer.serialize(parentReference));
534    } else {
535  521 pageSummary.setParentId("");
536    }
537   
538  553 List<String> spaces = Utils.getSpacesFromSpaceId(doc.getSpace());
539  553 String spaceUri = Utils.createURI(baseUri, SpaceResource.class, doc.getWiki(), spaces).toString();
540  553 Link spaceLink = this.objectFactory.createLink();
541  553 spaceLink.setHref(spaceUri);
542  553 spaceLink.setRel(Relations.SPACE);
543  553 pageSummary.getLinks().add(spaceLink);
544   
545  553 if (parentExist) {
546  32 String parentUri =
547    Utils.createURI(baseUri, PageResource.class, parentReference.getWikiReference().getName(), spaces,
548    parentReference.getName()).toString();
549  32 Link parentLink = this.objectFactory.createLink();
550  32 parentLink.setHref(parentUri);
551  32 parentLink.setRel(Relations.PARENT);
552  32 pageSummary.getLinks().add(parentLink);
553    }
554   
555  553 String historyUri =
556    Utils.createURI(baseUri, PageHistoryResource.class, doc.getWiki(),
557    Utils.getSpacesFromSpaceId(doc.getSpace()), doc.getName()).toString();
558  553 Link historyLink = this.objectFactory.createLink();
559  553 historyLink.setHref(historyUri);
560  553 historyLink.setRel(Relations.HISTORY);
561  553 pageSummary.getLinks().add(historyLink);
562   
563  553 if (!doc.getChildren().isEmpty()) {
564  26 String pageChildrenUri =
565    Utils.createURI(baseUri, PageChildrenResource.class, doc.getWiki(), spaces, doc.getName()).toString();
566  26 Link pageChildrenLink = this.objectFactory.createLink();
567  26 pageChildrenLink.setHref(pageChildrenUri);
568  26 pageChildrenLink.setRel(Relations.CHILDREN);
569  26 pageSummary.getLinks().add(pageChildrenLink);
570    }
571   
572  553 if (!doc.getComments().isEmpty()) {
573  3 String commentsUri;
574  3 if (useVersion) {
575  0 commentsUri =
576    Utils.createURI(baseUri, CommentsVersionResource.class, doc.getWiki(), spaces, doc.getName(),
577    doc.getVersion()).toString();
578    } else {
579  3 commentsUri =
580    Utils.createURI(baseUri, CommentsResource.class, doc.getWiki(), spaces, doc.getName()).toString();
581    }
582   
583  3 Link commentsLink = this.objectFactory.createLink();
584  3 commentsLink.setHref(commentsUri);
585  3 commentsLink.setRel(Relations.COMMENTS);
586  3 pageSummary.getLinks().add(commentsLink);
587    }
588   
589  553 if (!doc.getAttachmentList().isEmpty()) {
590  36 String attachmentsUri;
591  36 if (useVersion) {
592  0 attachmentsUri =
593    Utils.createURI(baseUri, AttachmentsAtPageVersionResource.class, doc.getWiki(), spaces,
594    doc.getName(), doc.getVersion()).toString();
595    } else {
596  36 attachmentsUri =
597    Utils.createURI(baseUri, AttachmentsResource.class, doc.getWiki(), spaces, doc.getName())
598    .toString();
599    }
600   
601  36 Link attachmentsLink = this.objectFactory.createLink();
602  36 attachmentsLink.setHref(attachmentsUri);
603  36 attachmentsLink.setRel(Relations.ATTACHMENTS);
604  36 pageSummary.getLinks().add(attachmentsLink);
605    }
606   
607  553 if (!doc.getxWikiObjects().keySet().isEmpty()) {
608  160 String objectsUri;
609   
610  160 if (useVersion) {
611  0 objectsUri =
612    Utils.createURI(baseUri, ObjectsAtPageVersionResource.class, doc.getWiki(), spaces, doc.getName(),
613    doc.getVersion()).toString();
614    } else {
615  160 objectsUri =
616    Utils.createURI(baseUri, ObjectsResource.class, doc.getWiki(), spaces, doc.getName()).toString();
617    }
618  160 Link objectsLink = this.objectFactory.createLink();
619  160 objectsLink.setHref(objectsUri);
620  160 objectsLink.setRel(Relations.OBJECTS);
621  160 pageSummary.getLinks().add(objectsLink);
622    }
623   
624  553 com.xpn.xwiki.api.Object tagsObject = doc.getObject("XWiki.TagClass", 0);
625  553 if (tagsObject != null) {
626  43 if (tagsObject.getProperty("tags") != null) {
627  43 String tagsUri =
628    Utils.createURI(baseUri, PageTagsResource.class, doc.getWiki(), spaces, doc.getName()).toString();
629  43 Link tagsLink = this.objectFactory.createLink();
630  43 tagsLink.setHref(tagsUri);
631  43 tagsLink.setRel(Relations.TAGS);
632  43 pageSummary.getLinks().add(tagsLink);
633    }
634    }
635   
636  553 String syntaxesUri = Utils.createURI(baseUri, SyntaxesResource.class).toString();
637  553 Link syntaxesLink = this.objectFactory.createLink();
638  553 syntaxesLink.setHref(syntaxesUri);
639  553 syntaxesLink.setRel(Relations.SYNTAXES);
640  553 pageSummary.getLinks().add(syntaxesLink);
641    }
642   
 
643  138 toggle public PageSummary toRestPageSummary(URI baseUri, Document doc, Boolean withPrettyNames) throws XWikiException
644    {
645  138 PageSummary pageSummary = this.objectFactory.createPageSummary();
646  138 toRestPageSummary(pageSummary, baseUri, doc, false, withPrettyNames);
647   
648  138 String pageUri =
649    Utils.createURI(baseUri, PageResource.class, doc.getWiki(), Utils.getSpacesFromSpaceId(doc.getSpace()),
650    doc.getName()).toString();
651  138 Link pageLink = this.objectFactory.createLink();
652  138 pageLink.setHref(pageUri);
653  138 pageLink.setRel(Relations.PAGE);
654  138 pageSummary.getLinks().add(pageLink);
655   
656  138 return pageSummary;
657    }
658   
 
659  415 toggle public Page toRestPage(URI baseUri, URI self, Document doc, boolean useVersion, Boolean withPrettyNames,
660    Boolean withObjects, Boolean withXClass, Boolean withAttachments) throws XWikiException
661    {
662  415 Page page = this.objectFactory.createPage();
663  415 toRestPageSummary(page, baseUri, doc, useVersion, withPrettyNames);
664   
665  415 XWikiContext xwikiContext = this.xcontextProvider.get();
666   
667  415 page.setMajorVersion(doc.getRCSVersion().at(0));
668  415 page.setMinorVersion(doc.getRCSVersion().at(1));
669  415 page.setHidden(doc.isHidden());
670  415 page.setLanguage(doc.getLocale().toString());
671  415 page.setCreator(doc.getCreator());
672  415 if (withPrettyNames) {
673  0 page.setCreatorName(xwikiContext.getWiki().getUserName(doc.getCreator(), null, false, xwikiContext));
674    }
675   
676  415 Calendar calendar = Calendar.getInstance();
677  415 calendar.setTime(doc.getCreationDate());
678  415 page.setCreated(calendar);
679   
680  415 page.setModifier(doc.getContentAuthor());
681  415 if (withPrettyNames) {
682  0 page.setModifierName(xwikiContext.getWiki().getUserName(doc.getContentAuthor(), null, false, xwikiContext));
683    }
684   
685  415 calendar = Calendar.getInstance();
686  415 calendar.setTime(doc.getContentUpdateDate());
687  415 page.setModified(calendar);
688   
689  415 page.setComment(doc.getComment());
690  415 page.setContent(doc.getContent());
691   
692  415 if (self != null) {
693  415 Link pageLink = this.objectFactory.createLink();
694  415 pageLink.setHref(self.toString());
695  415 pageLink.setRel(Relations.SELF);
696  415 page.getLinks().add(pageLink);
697    }
698   
699  415 com.xpn.xwiki.api.Class xwikiClass = doc.getxWikiClass();
700  415 if (xwikiClass != null) {
701  415 String classUri =
702    Utils.createURI(baseUri, ClassResource.class, doc.getWiki(), xwikiClass.getName()).toString();
703  415 Link classLink = this.objectFactory.createLink();
704  415 classLink.setHref(classUri);
705  415 classLink.setRel(Relations.CLASS);
706  415 page.getLinks().add(classLink);
707    }
708   
709  415 XWikiContext xcontext = xcontextProvider.get();
710   
711    // Add attachments
712  415 if (withAttachments) {
713  0 page.setAttachments(objectFactory.createAttachments());
714   
715  0 for (com.xpn.xwiki.api.Attachment attachment : doc.getAttachmentList()) {
716  0 URL url =
717    xcontext.getURLFactory().createAttachmentURL(attachment.getFilename(), doc.getSpace(),
718    doc.getName(), "download", null, doc.getWiki(), xcontext);
719  0 String attachmentXWikiAbsoluteUrl = url.toString();
720  0 String attachmentXWikiRelativeUrl = xcontext.getURLFactory().getURL(url, xcontext);
721   
722  0 page.getAttachments()
723    .getAttachments()
724    .add(
725    toRestAttachment(baseUri, attachment, attachmentXWikiRelativeUrl, attachmentXWikiAbsoluteUrl,
726    withPrettyNames, false));
727    }
728    }
729   
730    // Add objects
731  415 if (withObjects) {
732  2 page.setObjects(objectFactory.createObjects());
733   
734  2 XWikiDocument xwikiDocument = xcontext.getWiki().getDocument(doc.getDocumentReference(), xcontext);
735   
736  2 for (List<BaseObject> objects : xwikiDocument.getXObjects().values()) {
737  1 for (BaseObject object : objects) {
738  1 page.getObjects().getObjectSummaries()
739    .add(toRestObject(baseUri, doc, object, false, withPrettyNames));
740    }
741    }
742    }
743   
744    // Add xclass
745  415 if (withXClass) {
746  0 page.setClazz(toRestClass(baseUri, doc.getxWikiClass()));
747    }
748   
749  415 return page;
750    }
751   
 
752  286 toggle public Class toRestClass(URI baseUri, com.xpn.xwiki.api.Class xwikiClass)
753    {
754  286 Class clazz = this.objectFactory.createClass();
755  286 clazz.setId(xwikiClass.getName());
756  286 clazz.setName(xwikiClass.getName());
757   
758  286 DocumentReference reference = xwikiClass.getReference();
759  286 String wikiName = reference.getWikiReference().getName();
760   
761  286 for (java.lang.Object xwikiPropertyClassObject : xwikiClass.getProperties()) {
762  7627 PropertyClass xwikiPropertyClass = (PropertyClass) xwikiPropertyClassObject;
763   
764  7627 Property property = this.objectFactory.createProperty();
765  7627 property.setName(xwikiPropertyClass.getName());
766  7627 property.setType(xwikiPropertyClass.getxWikiClass().getName());
767   
768  7627 for (java.lang.Object xwikiPropertyObject : xwikiPropertyClass.getProperties()) {
769  58910 com.xpn.xwiki.api.Property xwikiProperty = (com.xpn.xwiki.api.Property) xwikiPropertyObject;
770  58910 java.lang.Object value = xwikiProperty.getValue();
771   
772  58910 Attribute attribute = this.objectFactory.createAttribute();
773  58910 attribute.setName(xwikiProperty.getName());
774   
775  58910 if (value != null) {
776  58910 attribute.setValue(value.toString());
777    } else {
778  0 attribute.setValue("");
779    }
780   
781  58910 property.getAttributes().add(attribute);
782    }
783   
784  7627 String propertyUri =
785    Utils.createURI(baseUri, ClassPropertyResource.class, wikiName, xwikiClass.getName(),
786    xwikiPropertyClass.getName()).toString();
787  7627 Link propertyLink = this.objectFactory.createLink();
788  7627 propertyLink.setHref(propertyUri);
789  7627 propertyLink.setRel(Relations.SELF);
790  7627 property.getLinks().add(propertyLink);
791   
792  7627 clazz.getProperties().add(property);
793    }
794   
795  286 String classUri = Utils.createURI(baseUri, ClassResource.class, wikiName, xwikiClass.getName()).toString();
796  286 Link classLink = this.objectFactory.createLink();
797  286 classLink.setHref(classUri);
798  286 classLink.setRel(Relations.SELF);
799  286 clazz.getLinks().add(classLink);
800   
801  286 String propertiesUri =
802    Utils.createURI(baseUri, ClassPropertiesResource.class, wikiName, xwikiClass.getName()).toString();
803  286 Link propertyLink = this.objectFactory.createLink();
804  286 propertyLink.setHref(propertiesUri);
805  286 propertyLink.setRel(Relations.PROPERTIES);
806  286 clazz.getLinks().add(propertyLink);
807   
808  286 String objectsUri =
809    Utils.createURI(baseUri, AllObjectsForClassNameResource.class, wikiName, xwikiClass.getName()).toString();
810  286 Link objectsLink = this.objectFactory.createLink();
811  286 objectsLink.setHref(objectsUri);
812  286 objectsLink.setRel(Relations.OBJECTS);
813  286 clazz.getLinks().add(objectsLink);
814   
815  286 return clazz;
816    }
817   
 
818  59 toggle public Attachment toRestAttachment(URI baseUri, com.xpn.xwiki.api.Attachment xwikiAttachment,
819    String xwikiRelativeUrl, String xwikiAbsoluteUrl, Boolean withPrettyNames, boolean versionURL)
820    {
821  59 Attachment attachment = this.objectFactory.createAttachment();
822   
823  59 Document doc = xwikiAttachment.getDocument();
824   
825  59 attachment.setId(String.format("%s@%s", doc.getPrefixedFullName(), xwikiAttachment.getFilename()));
826  59 attachment.setName(xwikiAttachment.getFilename());
827  59 attachment.setSize(xwikiAttachment.getFilesize());
828  59 attachment.setVersion(xwikiAttachment.getVersion());
829  59 attachment.setPageId(doc.getPrefixedFullName());
830  59 attachment.setPageVersion(doc.getVersion());
831  59 attachment.setMimeType(xwikiAttachment.getMimeType());
832  59 attachment.setAuthor(xwikiAttachment.getAuthor());
833  59 if (withPrettyNames) {
834  0 XWikiContext xcontext = xcontextProvider.get();
835  0 attachment
836    .setAuthorName(xcontext.getWiki().getUserName(xwikiAttachment.getAuthor(), null, false, xcontext));
837    }
838   
839  59 Calendar calendar = Calendar.getInstance();
840  59 calendar.setTime(xwikiAttachment.getDate());
841  59 attachment.setDate(calendar);
842   
843  59 attachment.setXwikiRelativeUrl(xwikiRelativeUrl);
844  59 attachment.setXwikiAbsoluteUrl(xwikiAbsoluteUrl);
845   
846  59 String pageUri =
847    Utils.createURI(baseUri, PageResource.class, doc.getWiki(), Utils.getSpacesFromSpaceId(doc.getSpace()),
848    doc.getName()).toString();
849  59 Link pageLink = objectFactory.createLink();
850  59 pageLink.setHref(pageUri);
851  59 pageLink.setRel(Relations.PAGE);
852  59 attachment.getLinks().add(pageLink);
853   
854  59 String attachmentUri;
855  59 if (versionURL) {
856  4 attachmentUri =
857    Utils.createURI(baseUri, AttachmentVersionResource.class, doc.getWiki(),
858    Utils.getSpacesFromSpaceId(doc.getSpace()), doc.getName(), xwikiAttachment.getFilename(),
859    xwikiAttachment.getVersion()).toString();
860    } else {
861  55 attachmentUri =
862    Utils.createURI(baseUri, AttachmentResource.class, doc.getWiki(),
863    Utils.getSpacesFromSpaceId(doc.getSpace()), doc.getName(), xwikiAttachment.getFilename())
864    .toString();
865    }
866   
867  59 Link attachmentLink = objectFactory.createLink();
868  59 attachmentLink.setHref(attachmentUri);
869  59 attachmentLink.setRel(Relations.ATTACHMENT_DATA);
870  59 attachment.getLinks().add(attachmentLink);
871   
872  59 return attachment;
873    }
874   
875    /**
876    * Serializes the value of the given XObject property.
877    *
878    * @param property an XObject property
879    * @return the String representation of the property value
880    */
 
881  156 toggle private static String serializePropertyValue(PropertyInterface property)
882    {
883  156 if (property == null) {
884  1 return "";
885    }
886   
887  155 java.lang.Object value = ((BaseProperty) property).getValue();
888  155 if (value instanceof List) {
889  151 return StringUtils.join((List) value, "|");
890  4 } else if (value != null) {
891  4 return value.toString();
892    } else {
893  0 return "";
894    }
895    }
896    }