View Javadoc

1   /**
2    * This file Copyright (c) 2003-2012 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.fckeditor.pages;
35  
36  import info.magnolia.cms.beans.config.URI2RepositoryManager;
37  import info.magnolia.context.MgnlContext;
38  import info.magnolia.jcr.util.PropertyUtil;
39  import info.magnolia.module.ModuleRegistry;
40  import info.magnolia.module.admininterface.AdminTreeMVCHandler;
41  import info.magnolia.module.admininterface.InvalidTreeHandlerException;
42  import info.magnolia.module.admininterface.TemplatedMVCHandler;
43  import info.magnolia.module.admininterface.TreeHandlerManager;
44  import info.magnolia.module.fckeditor.FCKEditorModule;
45  
46  import java.util.Collection;
47  
48  import javax.jcr.Node;
49  import javax.jcr.RepositoryException;
50  import javax.servlet.http.HttpServletRequest;
51  import javax.servlet.http.HttpServletResponse;
52  
53  import org.apache.commons.lang.StringUtils;
54  import org.slf4j.Logger;
55  import org.slf4j.LoggerFactory;
56  
57  
58  /**
59   * Repository browser page.
60   * @author vsteller
61   * @version $Id$
62   *
63   */
64  public class RepositoryBrowserPage extends TemplatedMVCHandler {
65  
66      private final FCKEditorModule moduleConfig;
67      private String selectedPath;
68      private String selectedRepository;
69      private String absoluteURI;
70      private String imgAltText;
71      private static Logger log = LoggerFactory.getLogger(RepositoryBrowserPage.class);
72  
73      public RepositoryBrowserPage(String name, HttpServletRequest request, HttpServletResponse response) {
74          super(name, request, response);
75          moduleConfig = (FCKEditorModule) ModuleRegistry.Factory.getInstance().getModuleInstance(FCKEditorModule.MODULE_FCKEDITOR);
76      }
77  
78      public String resolveAbsoluteURI() {
79          final String treeName = getSelectedRepository();
80          if (StringUtils.isNotEmpty(treeName) && StringUtils.isNotEmpty(getSelectedPath())) {
81              final URI2RepositoryManager manager = URI2RepositoryManager.getInstance();
82  
83              String repoName;
84              try {
85                  final AdminTreeMVCHandler treeHandler = TreeHandlerManager.getInstance().getTreeHandler(treeName, request, response);
86                  repoName = treeHandler.getRepository();
87              } catch (InvalidTreeHandlerException e) {
88                  // should never happen, but let's be careful with invalid configurations
89                  // getSelectedRepository() returns the *tree* name
90                  repoName = treeName;
91              }
92  
93              final String absoluteURI = manager.getURI(repoName, getSelectedPath());
94              setAbsoluteURI(MgnlContext.getContextPath() + absoluteURI);
95  
96              Node img = null;
97              try {
98                  img = MgnlContext.getJCRSession(getSelectedRepository()).getNode(getSelectedPath());
99              } catch (RepositoryException e) {
100                 log.warn("Error obtaining node [" + getSelectedPath() + "] from repository [" + getSelectedPath() + "]");
101             }
102             if (img != null) {
103                 final String imgAltText = PropertyUtil.getString(img, "subject", "");
104                 setImgAltText(imgAltText);
105             }
106         } else {
107             setAbsoluteURI(StringUtils.EMPTY);
108         }
109 
110         return "submit";
111     }
112 
113     public String select() {
114         final URI2RepositoryManager manager = URI2RepositoryManager.getInstance();
115 
116         if (StringUtils.isNotEmpty(absoluteURI)) {
117             String absoluteURIWithoutContextPath = StringUtils.removeStart(absoluteURI, MgnlContext.getContextPath());
118             final String repository = manager.getRepository(absoluteURIWithoutContextPath);
119             final String path = manager.getHandle(absoluteURIWithoutContextPath);
120 
121             setSelectedRepository(repository);
122             setSelectedPath(path);
123         }
124 
125         return "select";
126     }
127 
128     // TODO This is actually a collection of tree names
129     public Collection getRepositories() {
130         return moduleConfig.getBrowsableRepositories();
131     }
132 
133     public String getSelectedPath() {
134         return selectedPath;
135     }
136 
137     public void setSelectedPath(String selectedPath) {
138         this.selectedPath = selectedPath;
139     }
140 
141     // TODO this is actually a tree name
142     public String getSelectedRepository() {
143         return selectedRepository;
144     }
145 
146     // TODO this is actually a tree name
147     public void setSelectedRepository(String selectedRepository) {
148         this.selectedRepository = selectedRepository;
149     }
150 
151     public String getAbsoluteURI() {
152         return absoluteURI;
153     }
154 
155     public void setAbsoluteURI(String absoluteURI) {
156         this.absoluteURI = absoluteURI;
157     }
158 
159     public String getImgAltText() {
160         return this.imgAltText;
161     }
162 
163     public void setImgAltText(String text) {
164         this.imgAltText = text;
165     }
166 }