View Javadoc

1   /**
2    * This file Copyright (c) 2008-2014 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.trees;
35  
36  import info.magnolia.cms.core.Content;
37  import info.magnolia.cms.core.ItemType;
38  import info.magnolia.cms.gui.control.Select;
39  import info.magnolia.cms.gui.control.TreeColumn;
40  import info.magnolia.cms.i18n.Messages;
41  import info.magnolia.cms.i18n.MessagesManager;
42  import info.magnolia.cms.i18n.MessagesUtil;
43  import info.magnolia.context.MgnlContext;
44  import info.magnolia.module.admininterface.AdminTreeMVCHandler;
45  import info.magnolia.objectfactory.Components;
46  import info.magnolia.rendering.template.TemplateDefinition;
47  import info.magnolia.rendering.template.assignment.TemplateDefinitionAssignment;
48  import info.magnolia.rendering.template.registry.TemplateDefinitionRegistry;
49  
50  import java.util.Collection;
51  import java.util.Collections;
52  
53  import javax.jcr.Node;
54  import javax.jcr.RepositoryException;
55  import javax.servlet.http.HttpServletRequest;
56  import javax.servlet.http.HttpServletResponse;
57  
58  import org.slf4j.Logger;
59  import org.slf4j.LoggerFactory;
60  
61  /**
62   * @author philipp
63   * @version $Id$
64   */
65  public class WebsiteTreeHandler extends AdminTreeMVCHandler {
66      private static final Logger log = LoggerFactory.getLogger(WebsiteTreeHandler.class);
67  
68      private static final String COMMAND_SHOW_TEMPLATES = "getTemplatesHtml";
69  
70      private TemplateDefinitionRegistry templateRegistry;
71      private TemplateDefinitionAssignment templateAssignment;
72      private boolean getAvailableTemplates;
73  
74      public WebsiteTreeHandler(String name, HttpServletRequest request, HttpServletResponse response) {
75          super(name, request, response);
76          // TODO dlipp: use IoC.
77          templateAssignment = Components.getComponent(TemplateDefinitionAssignment.class);
78          templateRegistry = Components.getComponent(TemplateDefinitionRegistry.class);
79      }
80  
81      @Override
82      public String createNode() {
83          String view = super.createNode();
84          try {
85              // todo: default template
86              if (this.getCreateItemType().equals(ItemType.PAGE.getSystemName())) {
87                  // TODO dlipp: use JCR-API directly
88                  Content parentNode = this.getHierarchyManager().getContent(this.getPath());
89                  Content newNode = parentNode.getContent(this.getNewNodeName());
90                  TemplateDefinition newTemplate = getDefaultTemplate(newNode.getJCRNode());
91                  if (newTemplate != null) {
92                      newNode.getMetaData().setTemplate(newTemplate.getId());
93                      newNode.save();
94                  }
95              }
96          } catch (RepositoryException e) {
97              log.error("can't set template", e);
98          }
99          return view;
100     }
101 
102     protected TemplateDefinition getDefaultTemplate(Node node) {
103         return templateAssignment.getDefaultTemplate(node);
104     }
105 
106     @Override
107     public String show() {
108         // show start page if no templates present yet or if only available template is mgnlDeleted
109         Collection<TemplateDefinition> templateDefinitions = templateRegistry.getTemplateDefinitions();
110         // TODO I know this hardcoded check is ugly but could not find a better way
111         boolean isDeletedNodeTemplate = templateDefinitions.size() == 1 && "adminInterface:mgnlDeleted".equals(templateDefinitions.iterator().next().getId());
112         if (templateDefinitions.isEmpty() || isDeletedNodeTemplate) {
113             try {
114                 request.getRequestDispatcher("/.magnolia/pages/quickstart.html").forward(request, response);
115                 return "";
116             } catch (Exception e) {
117                 log.error("Couldn't forward to quickstart page: " + e.getMessage());
118             }
119         }
120         return super.show();
121     }
122 
123     @Override
124     public String getCommand() {
125         if (isGetAvailableTemplates()) {
126             return COMMAND_SHOW_TEMPLATES;
127         }
128         return super.getCommand();
129     }
130 
131     public String getTemplatesHtml() {
132         String javascriptTree = getTree().getJavascriptTree();
133         Select templateSelect = new Select();
134         templateSelect.setName(javascriptTree + TreeColumn.EDIT_NAMEADDITION);
135         templateSelect.setSaveInfo(false);
136         templateSelect.setCssClass(TreeColumn.EDIT_CSSCLASS_SELECT);
137 
138         templateSelect.setEvent("onblur", javascriptTree + ".saveNodeData(this.value,this.options[this.selectedIndex].text)");
139         templateSelect.setEvent("onchange", javascriptTree + ".saveNodeData(this.value,this.options[this.selectedIndex].text)");
140 
141         Collection<TemplateDefinition> templateDefinitons = Collections.emptyList();
142         try {
143             templateDefinitons = templateAssignment.getAvailableTemplates(MgnlContext.getJCRSession(this.getRepository()).getNode(this.getPath()));
144         } catch (RepositoryException e) {
145             log.error(e.getMessage(), e);
146         }
147 
148         templateSelect.getOptions().clear();
149 
150         for (TemplateDefinition templateDefinition : templateDefinitons) {
151             String title = MessagesUtil.javaScriptString(getI18nTitle(templateDefinition));
152             templateSelect.setOptions(title, templateDefinition.getId());
153         }
154         this.displayValue = templateSelect.getHtml();
155         return VIEW_VALUE;
156     }
157 
158     private String getI18nTitle(TemplateDefinition template) {
159         Messages messages = MessagesManager.getMessages(template.getI18nBasename());
160         String title = template.getTitle();
161         return messages.getWithDefault(title, title);
162     }
163 
164     public boolean isGetAvailableTemplates() {
165         return getAvailableTemplates;
166     }
167 
168     public void setGetAvailableTemplates(boolean getAvailableTemplates) {
169         this.getAvailableTemplates = getAvailableTemplates;
170     }
171 }