1. Project Clover database Thu May 10 2018 23:28:11 CEST
  2. Package org.xwiki.rest.internal.resources.wikis

File WikiPagesResourceImpl.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

16
57
1
1
162
107
10
0.18
57
1
10

Classes

Class Line # Actions
WikiPagesResourceImpl 51 57 0% 10 5
0.932432493.2%
 

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.wikis;
21   
22    import java.net.URL;
23    import java.util.Formatter;
24    import java.util.HashMap;
25    import java.util.List;
26    import java.util.Map;
27   
28    import javax.inject.Named;
29   
30    import org.xwiki.component.annotation.Component;
31    import org.xwiki.query.Query;
32    import org.xwiki.query.QueryException;
33    import org.xwiki.rest.Relations;
34    import org.xwiki.rest.XWikiResource;
35    import org.xwiki.rest.XWikiRestException;
36    import org.xwiki.rest.internal.Utils;
37    import org.xwiki.rest.model.jaxb.Link;
38    import org.xwiki.rest.model.jaxb.PageSummary;
39    import org.xwiki.rest.model.jaxb.Pages;
40    import org.xwiki.rest.resources.pages.PageResource;
41    import org.xwiki.rest.resources.wikis.WikiPagesResource;
42   
43    import com.xpn.xwiki.api.Document;
44    import com.xpn.xwiki.doc.XWikiDocument;
45   
46    /**
47    * @version $Id: 1a7e96b0489e70283f50951c6710959b612b13d3 $
48    */
49    @Component
50    @Named("org.xwiki.rest.internal.resources.wikis.WikiPagesResourceImpl")
 
51    public class WikiPagesResourceImpl extends XWikiResource implements WikiPagesResource
52    {
 
53  3 toggle @Override
54    public Pages getPages(String wikiName, Integer start, String name, String space, String author, Integer number)
55    throws XWikiRestException
56    {
57  3 String database = Utils.getXWikiContext(componentManager).getWikiId();
58   
59  3 Pages pages = objectFactory.createPages();
60   
61    /* This try is just needed for executing the finally clause. */
62  3 try {
63  3 Map<String, String> filters = new HashMap<String, String>();
64  3 if (!name.equals("")) {
65  2 filters.put("name", name);
66    }
67  3 if (!space.equals("")) {
68  1 filters.put("space", Utils.getLocalSpaceId(parseSpaceSegments(space)));
69    }
70  3 if (!author.equals("")) {
71  0 filters.put("author", author);
72    }
73   
74    /* Build the query */
75  3 Formatter f = new Formatter();
76  3 f.format("select doc from XWikiDocument as doc");
77   
78  3 if (filters.keySet().size() > 0) {
79  2 f.format(" where (");
80   
81  2 int i = 0;
82  2 for (String param : filters.keySet()) {
83  3 if (param.equals("name")) {
84  2 f.format(" upper(doc.fullName) like :name ");
85    }
86   
87  3 if (param.equals("space")) {
88  1 f.format(" upper(doc.space) like :space ");
89    }
90   
91  3 if (param.equals("author")) {
92  0 f.format(" upper(doc.contentAuthor) like :author ");
93    }
94   
95  3 i++;
96   
97  3 if (i < filters.keySet().size()) {
98  1 f.format(" and ");
99    }
100    }
101   
102  2 f.format(")");
103    }
104   
105  3 String queryString = f.toString();
106   
107    /* Execute the query by filling the parameters */
108  3 List<Object> queryResult = null;
109  3 try {
110  3 Query query = queryManager.createQuery(queryString, Query.XWQL).setLimit(number).setOffset(start);
111  3 for (String param : filters.keySet()) {
112  3 query.bindValue(param, String.format("%%%s%%", filters.get(param).toUpperCase()));
113    }
114   
115  3 queryResult = query.execute();
116    } catch (QueryException e) {
117  0 throw new XWikiRestException(e);
118    }
119   
120    /* Get the results and populate the returned representation */
121  3 for (Object object : queryResult) {
122  27 XWikiDocument xwikiDocument = (XWikiDocument) object;
123  27 xwikiDocument.setDatabase(wikiName);
124   
125  27 Document doc = new Document(xwikiDocument, Utils.getXWikiContext(componentManager));
126   
127    /*
128    * We manufacture page summaries in place because we don't have all the data for calling the
129    * DomainObjectFactory method (doing so would require to retrieve an actual Document)
130    */
131  27 PageSummary pageSummary = objectFactory.createPageSummary();
132  27 pageSummary.setId(doc.getPrefixedFullName());
133  27 pageSummary.setFullName(doc.getFullName());
134  27 pageSummary.setWiki(wikiName);
135  27 pageSummary.setSpace(doc.getSpace());
136  27 pageSummary.setName(doc.getName());
137  27 pageSummary.setTitle(doc.getTitle());
138  27 pageSummary.setParent(doc.getParent());
139   
140  27 URL absoluteUrl =
141    Utils.getXWikiContext(componentManager).getURLFactory().createExternalURL(doc.getSpace(),
142    doc.getName(), "view", null, null, Utils.getXWikiContext(componentManager));
143  27 pageSummary.setXwikiAbsoluteUrl(absoluteUrl.toString());
144  27 pageSummary.setXwikiRelativeUrl(Utils.getXWikiContext(componentManager).getURLFactory().getURL(
145    absoluteUrl, Utils.getXWikiContext(componentManager)));
146   
147  27 String pageUri = Utils.createURI(uriInfo.getBaseUri(), PageResource.class, doc.getWiki(),
148    Utils.getSpacesFromSpaceId(doc.getSpace()), doc.getName()).toString();
149  27 Link pageLink = objectFactory.createLink();
150  27 pageLink.setHref(pageUri);
151  27 pageLink.setRel(Relations.PAGE);
152  27 pageSummary.getLinks().add(pageLink);
153   
154  27 pages.getPageSummaries().add(pageSummary);
155    }
156    } finally {
157  3 Utils.getXWikiContext(componentManager).setWikiId(database);
158    }
159   
160  3 return pages;
161    }
162    }