1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.module.admininterface.pages;
35
36 import info.magnolia.cms.beans.config.ContentRepository;
37 import info.magnolia.cms.core.Content;
38 import info.magnolia.cms.core.search.Query;
39 import info.magnolia.cms.util.ContentUtil;
40 import info.magnolia.cms.util.DumperUtil;
41 import info.magnolia.cms.util.QueryUtil;
42 import info.magnolia.context.MgnlContext;
43 import info.magnolia.module.admininterface.TemplatedMVCHandler;
44 import org.apache.commons.lang.StringUtils;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47
48 import javax.jcr.RepositoryException;
49 import javax.servlet.http.HttpServletRequest;
50 import javax.servlet.http.HttpServletResponse;
51 import java.util.Collection;
52 import java.util.Iterator;
53
54
55 public class JCRUtilsPage extends TemplatedMVCHandler {
56 private static final Logger log = LoggerFactory.getLogger(JCRUtilsPage.class);
57
58 public JCRUtilsPage(String name, HttpServletRequest request, HttpServletResponse response) {
59 super(name, request, response);
60 }
61
62 private String repository = "";
63
64 private int level = 1;
65
66 private String path = "/";
67
68 private String result = "";
69
70 private String statement = "";
71
72 private String language = Query.SQL;
73
74 private String itemType = "nt:base";
75
76 public String dump() {
77 if (StringUtils.isNotEmpty(repository) && StringUtils.isNotEmpty(path)) {
78 Content node = ContentUtil.getContent(repository, path);
79 if (node == null) {
80 return "path not found: " + this.path;
81 }
82 result = DumperUtil.dump(node, level);
83 }
84 return VIEW_SHOW;
85 }
86
87 public String query() {
88 final long start = System.currentTimeMillis();
89 final Collection nodes;
90 try {
91 nodes = QueryUtil.exceptionThrowingQuery(repository, statement, language, this.itemType);
92 } catch (RepositoryException e) {
93 this.result = e.getMessage();
94 log.error("Error in JCR query:", e);
95 return VIEW_SHOW;
96 }
97 final StringBuffer sb = new StringBuffer();
98 sb.append(nodes.size());
99 sb.append(" nodes returned in ");
100 sb.append(System.currentTimeMillis() - start);
101 sb.append("ms\n");
102
103 for (Iterator iter = nodes.iterator(); iter.hasNext();) {
104 Content node = (Content) iter.next();
105 sb.append(node.getHandle());
106 sb.append("\n");
107 }
108
109 this.result = sb.toString();
110 return VIEW_SHOW;
111 }
112
113 public String delete() {
114 try {
115 MgnlContext.getHierarchyManager(repository).delete(path);
116 }
117 catch (Exception e) {
118 result = e.toString();
119 }
120 return VIEW_SHOW;
121 }
122
123 public Iterator getRepositories() {
124 return ContentRepository.getAllRepositoryNames();
125 }
126
127 public String[] getLanguages() throws RepositoryException {
128 return MgnlContext.getQueryManager(ContentRepository.WEBSITE).getSupportedQueryLanguages();
129 }
130
131 public int getLevel() {
132 return level;
133 }
134
135
136 public void setLevel(int level) {
137 this.level = level;
138 }
139
140
141 public String getPath() {
142 return path;
143 }
144
145
146 public void setPath(String path) {
147 this.path = path;
148 }
149
150
151 public String getRepository() {
152 return repository;
153 }
154
155
156 public void setRepository(String repositroy) {
157 this.repository = repositroy;
158 }
159
160
161 public String getResult() {
162 return result;
163 }
164
165
166 public String getStatement() {
167 return statement;
168 }
169
170
171 public void setStatement(String statement) {
172 this.statement = statement;
173 }
174
175
176 public String getLanguage() {
177 return language;
178 }
179
180
181 public void setLanguage(String language) {
182 this.language = language;
183 }
184
185
186 public String getItemType() {
187 return itemType;
188 }
189
190
191 public void setItemType(String itemType) {
192 this.itemType = itemType;
193 }
194
195 }