View Javadoc

1   /**
2    * This file Copyright (c) 2003-2010 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 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 }