1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
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 |
48 |
|
|
49 |
|
@Component |
50 |
|
@Named("org.xwiki.rest.internal.resources.wikis.WikiPagesResourceImpl") |
|
|
| 93.2% |
Uncovered Elements: 5 (74) |
Complexity: 10 |
Complexity Density: 0.18 |
|
51 |
|
public class WikiPagesResourceImpl extends XWikiResource implements WikiPagesResource |
52 |
|
{ |
|
|
| 93.2% |
Uncovered Elements: 5 (73) |
Complexity: 10 |
Complexity Density: 0.18 |
|
53 |
3 |
@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 |
|
|
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 |
|
|
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 |
|
|
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 |
|
|
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 |
|
|
129 |
|
|
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 |
|
} |