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