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