View Javadoc

1   /**
2    * This file Copyright (c) 2003-2011 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
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 info.magnolia.repository.RepositoryConstants;
45  
46  import java.util.ArrayList;
47  import java.util.Collection;
48  import java.util.Collections;
49  import java.util.Iterator;
50  import java.util.List;
51  
52  import javax.jcr.RepositoryException;
53  import javax.servlet.http.HttpServletRequest;
54  import javax.servlet.http.HttpServletResponse;
55  
56  import org.apache.commons.lang.ArrayUtils;
57  import org.apache.commons.lang.StringUtils;
58  import org.slf4j.Logger;
59  import org.slf4j.LoggerFactory;
60  
61  
62  public class JCRUtilsPage extends TemplatedMVCHandler {
63      private static final Logger log = LoggerFactory.getLogger(JCRUtilsPage.class);
64  
65      private String repository = "";
66  
67      private int level = 1;
68  
69      private String path = "/";
70  
71      private String result = "";
72  
73      private String statement = "";
74  
75      private String language = Query.SQL;
76  
77      private String[] supportedLanguages = new String[]{};
78  
79      private String itemType = "nt:base";
80  
81      private List<String> repositories = new ArrayList<String>();
82  
83      public JCRUtilsPage(String name, HttpServletRequest request, HttpServletResponse response) {
84          super(name, request, response);
85          try {
86              supportedLanguages = MgnlContext.getQueryManager(RepositoryConstants.WEBSITE).getSupportedQueryLanguages();
87              supportedLanguages = (String[]) ArrayUtils.removeElement(supportedLanguages, javax.jcr.query.Query.JCR_JQOM) ;
88  
89              final Iterator<String> iter = ContentRepository.getAllRepositoryNames();
90              while(iter.hasNext()) {
91                  repositories.add(iter.next());
92              }
93              Collections.sort(repositories, String.CASE_INSENSITIVE_ORDER);
94  
95          } catch (RepositoryException e) {
96              this.result = e.getMessage();
97              log.error("An error occurred while retrieving supported query languages.", e);
98          }
99      }
100 
101 
102     public String dump() {
103         if (StringUtils.isNotEmpty(repository) && StringUtils.isNotEmpty(path)) {
104             Content node = ContentUtil.getContent(repository, path);
105             if (node == null) {
106                 return "path not found: " + this.path;
107             }
108             result = DumperUtil.dump(node, level);
109         }
110         return VIEW_SHOW;
111     }
112 
113     public String query() {
114         final long start = System.currentTimeMillis();
115         final Collection<Content> nodes;
116         try {
117             nodes = QueryUtil.exceptionThrowingQuery(repository, statement, language, this.itemType);
118         } catch (Throwable e) {
119             this.result = e.getMessage() != null ? e.getMessage() : e.toString();
120             log.error("Error in JCR query:", e);
121             return VIEW_SHOW;
122         }
123         final StringBuilder sb = new StringBuilder();
124         sb.append(nodes.size());
125         sb.append(" nodes returned in ");
126         sb.append(System.currentTimeMillis() - start);
127         sb.append("ms\n");
128 
129         for (Iterator<Content> iter = nodes.iterator(); iter.hasNext();) {
130             Content node = iter.next();
131             sb.append(node.getHandle());
132             sb.append("\n");
133         }
134 
135         this.result = sb.toString();
136         return VIEW_SHOW;
137     }
138 
139     public String delete() {
140         try {
141             MgnlContext.getHierarchyManager(repository).delete(path);
142         }
143         catch (Exception e) {
144             result = e.toString();
145         }
146         return VIEW_SHOW;
147     }
148 
149     public Iterator getRepositories() {
150         return repositories.iterator();
151     }
152 
153     public String[] getLanguages() throws RepositoryException {
154         return supportedLanguages;
155     }
156 
157     public int getLevel() {
158         return level;
159     }
160 
161     public void setLevel(int level) {
162         this.level = level;
163     }
164 
165 
166     public String getPath() {
167         return path;
168     }
169 
170 
171     public void setPath(String path) {
172         this.path = path;
173     }
174 
175 
176     public String getRepository() {
177         return repository;
178     }
179 
180 
181     public void setRepository(String repositroy) {
182         this.repository = repositroy;
183     }
184 
185 
186     public String getResult() {
187         return result;
188     }
189 
190 
191     public String getStatement() {
192         return statement;
193     }
194 
195 
196     public void setStatement(String statement) {
197         this.statement = statement;
198     }
199 
200 
201     public String getLanguage() {
202         return language;
203     }
204 
205 
206     public void setLanguage(String language) {
207         this.language = language;
208     }
209 
210 
211     public String getItemType() {
212         return itemType;
213     }
214 
215 
216     public void setItemType(String itemType) {
217         this.itemType = itemType;
218     }
219 
220 }