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.fckeditor.pages;
35  
36  import info.magnolia.cms.beans.config.URI2RepositoryManager;
37  import info.magnolia.module.ModuleRegistry;
38  import info.magnolia.module.admininterface.InvalidTreeHandlerException;
39  import info.magnolia.module.admininterface.TemplatedMVCHandler;
40  import info.magnolia.module.admininterface.TreeHandlerManager;
41  import info.magnolia.module.admininterface.AdminTreeMVCHandler;
42  import info.magnolia.module.fckeditor.FCKEditorModule;
43  
44  import java.util.Collection;
45  
46  import javax.servlet.http.HttpServletRequest;
47  import javax.servlet.http.HttpServletResponse;
48  
49  import org.apache.commons.lang.StringUtils;
50  
51  
52  /**
53   * @author vsteller
54   * @version $Id: RepositoryBrowserPage.java 32667 2010-03-13 00:37:06Z gjoseph $
55   *
56   */
57  public class RepositoryBrowserPage extends TemplatedMVCHandler {
58  
59      private final FCKEditorModule moduleConfig;
60      private String selectedPath;
61      private String selectedRepository;
62      private String absoluteURI;
63  
64      public RepositoryBrowserPage(String name, HttpServletRequest request, HttpServletResponse response) {
65          super(name, request, response);
66          moduleConfig = (FCKEditorModule) ModuleRegistry.Factory.getInstance().getModuleInstance(FCKEditorModule.MODULE_FCKEDITOR);
67      }
68  
69      public String resolveAbsoluteURI() {
70          final String treeName = getSelectedRepository();
71          if (StringUtils.isNotEmpty(treeName) && StringUtils.isNotEmpty(getSelectedPath())) {
72              final URI2RepositoryManager manager = URI2RepositoryManager.getInstance();
73  
74              String repoName;
75              try {
76                  final AdminTreeMVCHandler treeHandler = TreeHandlerManager.getInstance().getTreeHandler(treeName, request, response);
77                  repoName = treeHandler.getRepository();
78              } catch (InvalidTreeHandlerException e) {
79                  // should never happen, but let's be careful with invalid configurations
80                  // getSelectedRepository() returns the *tree* name
81                  repoName = treeName;
82              }
83  
84  
85              final String absoluteURI = manager.getURI(repoName, getSelectedPath());
86              setAbsoluteURI(absoluteURI);
87          } else {
88              setAbsoluteURI(StringUtils.EMPTY);
89          }
90  
91          return "submit";
92      }
93  
94      public String select() {
95          final URI2RepositoryManager manager = URI2RepositoryManager.getInstance();
96          
97          if (StringUtils.isNotEmpty(absoluteURI)) {
98              final String repository = manager.getRepository(absoluteURI);
99              final String path = manager.getHandle(absoluteURI);
100 
101             setSelectedRepository(repository);
102             setSelectedPath(path);
103         }
104 
105         return "select";
106     }
107 
108     // TODO This is actually a collection of tree names
109     public Collection getRepositories() {
110         return moduleConfig.getBrowsableRepositories();
111     }
112 
113     public String getSelectedPath() {
114         return selectedPath;
115     }
116 
117     public void setSelectedPath(String selectedPath) {
118         this.selectedPath = selectedPath;
119     }
120 
121     // TODO this is actually a tree name
122     public String getSelectedRepository() {
123         return selectedRepository;
124     }
125 
126     // TODO this is actually a tree name
127     public void setSelectedRepository(String selectedRepository) {
128         this.selectedRepository = selectedRepository;
129     }
130 
131     public String getAbsoluteURI() {
132         return absoluteURI;
133     }
134     
135     public void setAbsoluteURI(String absoluteURI) {
136         this.absoluteURI = absoluteURI;
137     }
138 }