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

File ObjectPropertyResourceImpl.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart8.png
54% of files have more coverage

Code metrics

10
36
2
1
130
87
9
0.25
18
2
4.5

Classes

Class Line # Actions
ObjectPropertyResourceImpl 48 36 0% 9 12
0.7575%
 

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.resources.objects;
21   
22    import javax.inject.Named;
23    import javax.ws.rs.WebApplicationException;
24    import javax.ws.rs.core.Response;
25    import javax.ws.rs.core.Response.Status;
26   
27    import org.xwiki.component.annotation.Component;
28    import org.xwiki.rest.Relations;
29    import org.xwiki.rest.XWikiResource;
30    import org.xwiki.rest.XWikiRestException;
31    import org.xwiki.rest.internal.DomainObjectFactory;
32    import org.xwiki.rest.internal.Utils;
33    import org.xwiki.rest.model.jaxb.Link;
34    import org.xwiki.rest.model.jaxb.Object;
35    import org.xwiki.rest.model.jaxb.Property;
36    import org.xwiki.rest.resources.objects.ObjectPropertyResource;
37    import org.xwiki.rest.resources.objects.ObjectResource;
38   
39    import com.xpn.xwiki.XWikiException;
40    import com.xpn.xwiki.api.Document;
41    import com.xpn.xwiki.doc.XWikiDocument;
42   
43    /**
44    * @version $Id: ee12e7908de2a4490f11df08bcf4a6b9e0e30a8a $
45    */
46    @Component
47    @Named("org.xwiki.rest.internal.resources.objects.ObjectPropertyResourceImpl")
 
48    public class ObjectPropertyResourceImpl extends XWikiResource implements ObjectPropertyResource
49    {
 
50  1 toggle @Override
51    public Property getObjectProperty(String wikiName, String spaceName, String pageName, String className,
52    Integer objectNumber, String propertyName, Boolean withPrettyNames) throws XWikiRestException
53    {
54  1 try {
55  1 DocumentInfo documentInfo = getDocumentInfo(wikiName, spaceName, pageName, null, null, true, false);
56   
57  1 Document doc = documentInfo.getDocument();
58   
59  1 XWikiDocument xwikiDocument = Utils.getXWiki(componentManager)
60    .getDocument(doc.getDocumentReference(), Utils.getXWikiContext(componentManager));
61   
62  1 com.xpn.xwiki.objects.BaseObject baseObject = xwikiDocument.getObject(className, objectNumber);
63  1 if (baseObject == null) {
64  0 throw new WebApplicationException(Status.NOT_FOUND);
65    }
66   
67  1 Object object = DomainObjectFactory.createObject(objectFactory, uriInfo.getBaseUri(), Utils.getXWikiContext(
68    componentManager), doc, baseObject, false, Utils.getXWikiApi(componentManager), withPrettyNames);
69   
70  1 for (Property property : object.getProperties()) {
71  1 if (property.getName().equals(propertyName)) {
72  1 String objectUri = Utils.createURI(uriInfo.getBaseUri(), ObjectResource.class, doc.getWiki(),
73    Utils.getSpacesFromSpaceId(doc.getSpace()), doc.getName(), object.getClassName(),
74    object.getNumber()).toString();
75  1 Link objectLink = objectFactory.createLink();
76  1 objectLink.setHref(objectUri);
77  1 objectLink.setRel(Relations.OBJECT);
78  1 property.getLinks().add(objectLink);
79   
80  1 return property;
81    }
82    }
83   
84  0 throw new WebApplicationException(Status.NOT_FOUND);
85    } catch (XWikiException e) {
86  0 throw new XWikiRestException(e);
87    }
88    }
89   
 
90  3 toggle @Override
91    public Response updateObjectProperty(String wikiName, String spaceName, String pageName, String className,
92    Integer objectNumber, String propertyName, Property property) throws XWikiRestException
93    {
94  3 try {
95  3 DocumentInfo documentInfo = getDocumentInfo(wikiName, spaceName, pageName, null, null, true, false);
96   
97  3 Document doc = documentInfo.getDocument();
98   
99  3 if (!doc.hasAccessLevel("edit", Utils.getXWikiUser(componentManager))) {
100  0 throw new WebApplicationException(Status.UNAUTHORIZED);
101    }
102   
103  3 XWikiDocument xwikiDocument = Utils.getXWiki(componentManager)
104    .getDocument(doc.getDocumentReference(), Utils.getXWikiContext(componentManager));
105   
106  3 com.xpn.xwiki.objects.BaseObject baseObject = xwikiDocument.getObject(className, objectNumber);
107  3 if (baseObject == null) {
108  0 throw new WebApplicationException(Status.NOT_FOUND);
109    }
110   
111  3 baseObject.set(propertyName, property.getValue(), Utils.getXWikiContext(componentManager));
112   
113  3 doc.save();
114   
115  3 baseObject = xwikiDocument.getObject(className, objectNumber);
116  3 Object object = DomainObjectFactory.createObject(objectFactory, uriInfo.getBaseUri(), Utils.getXWikiContext(
117    componentManager), doc, baseObject, false, Utils.getXWikiApi(componentManager), false);
118   
119  3 for (Property p : object.getProperties()) {
120  3 if (p.getName().equals(propertyName)) {
121  3 return Response.status(Status.ACCEPTED).entity(p).build();
122    }
123    }
124   
125  0 throw new WebApplicationException(Status.INTERNAL_SERVER_ERROR);
126    } catch (XWikiException e) {
127  0 throw new XWikiRestException(e);
128    }
129    }
130    }