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.core.Path;
39  import info.magnolia.cms.exchange.ExchangeException;
40  import info.magnolia.cms.gui.control.Select;
41  import info.magnolia.cms.gui.control.TreeColumn;
42  import info.magnolia.cms.i18n.Messages;
43  import info.magnolia.cms.i18n.MessagesManager;
44  import info.magnolia.cms.i18n.MessagesUtil;
45  import info.magnolia.context.MgnlContext;
46  import info.magnolia.module.admininterface.AdminTreeMVCHandler;
47  import info.magnolia.objectfactory.Components;
48  import info.magnolia.registry.RegistrationException;
49  import info.magnolia.rendering.template.TemplateAvailability;
50  import info.magnolia.rendering.template.TemplateDefinition;
51  import info.magnolia.rendering.template.assignment.TemplateDefinitionAssignment;
52  import info.magnolia.rendering.template.registry.TemplateDefinitionRegistry;
53  
54  import java.util.Collection;
55  import java.util.Collections;
56  
57  import javax.jcr.Node;
58  import javax.jcr.RepositoryException;
59  import javax.jcr.Session;
60  import javax.servlet.http.HttpServletRequest;
61  import javax.servlet.http.HttpServletResponse;
62  
63  import org.slf4j.Logger;
64  import org.slf4j.LoggerFactory;
65  
66  /**
67   * @author philipp
68   * @version $Id$
69   */
70  public class WebsiteTreeHandler extends AdminTreeMVCHandler {
71      private static final Logger log = LoggerFactory.getLogger(WebsiteTreeHandler.class);
72  
73      private static final String COMMAND_SHOW_TEMPLATES = "getTemplatesHtml";
74  
75      private TemplateDefinitionRegistry templateRegistry;
76      private TemplateDefinitionAssignment templateAssignment;
77      private boolean getAvailableTemplates;
78  
79      public WebsiteTreeHandler(String name, HttpServletRequest request, HttpServletResponse response) {
80          super(name, request, response);
81          // TODO dlipp: use IoC.
82          templateAssignment = Components.getComponent(TemplateDefinitionAssignment.class);
83          templateRegistry = Components.getComponent(TemplateDefinitionRegistry.class);
84      }
85  
86      @Override
87      public String createNode() {
88          String view = super.createNode();
89          try {
90              // todo: default template
91              if (this.getCreateItemType().equals(ItemType.PAGE.getSystemName())) {
92                  // TODO dlipp: use JCR-API directly
93                  Content parentNode = this.getHierarchyManager().getContent(this.getPath());
94                  Content newNode = parentNode.getContent(this.getNewNodeName());
95                  TemplateDefinition newTemplate = getDefaultTemplate(newNode.getJCRNode());
96                  if (newTemplate != null) {
97                      newNode.getMetaData().setTemplate(newTemplate.getId());
98                      newNode.save();
99                  }
100             }
101         } catch (RepositoryException e) {
102             log.error("can't set template", e);
103         }
104         return view;
105     }
106 
107     protected TemplateDefinition getDefaultTemplate(Node node) {
108         return templateAssignment.getDefaultTemplate(node);
109     }
110 
111     @Override
112     public String show() {
113         // show start page if no templates present yet or if only available template is mgnlDeleted
114         Collection<TemplateDefinition> templateDefinitions = templateRegistry.getTemplateDefinitions();
115         // TODO I know this hardcoded check is ugly but could not find a better way
116         boolean isDeletedNodeTemplate = templateDefinitions.size() == 1 && "adminInterface:mgnlDeleted".equals(templateDefinitions.iterator().next().getId());
117         if (templateDefinitions.isEmpty() || isDeletedNodeTemplate) {
118             try {
119                 request.getRequestDispatcher("/.magnolia/pages/quickstart.html").forward(request, response);
120                 return "";
121             } catch (Exception e) {
122                 log.error("Couldn't forward to quickstart page: " + e.getMessage());
123             }
124         }
125         return super.show();
126     }
127 
128     @Override
129     public String getCommand() {
130         if (isGetAvailableTemplates()) {
131             return COMMAND_SHOW_TEMPLATES;
132         }
133         return super.getCommand();
134     }
135 
136     public String getTemplatesHtml() {
137         String javascriptTree = getTree().getJavascriptTree();
138         Select templateSelect = new Select();
139         templateSelect.setName(javascriptTree + TreeColumn.EDIT_NAMEADDITION);
140         templateSelect.setSaveInfo(false);
141         templateSelect.setCssClass(TreeColumn.EDIT_CSSCLASS_SELECT);
142 
143         templateSelect.setEvent("onblur", javascriptTree + ".saveNodeData(this.value,this.options[this.selectedIndex].text)");
144         templateSelect.setEvent("onchange", javascriptTree + ".saveNodeData(this.value,this.options[this.selectedIndex].text)");
145 
146         Collection<TemplateDefinition> templateDefinitons = Collections.emptyList();
147         try {
148             templateDefinitons = templateAssignment.getAvailableTemplates(MgnlContext.getJCRSession(this.getRepository()).getNode(this.getPath()));
149         } catch (RepositoryException e) {
150             log.error(e.getMessage(), e);
151         }
152 
153         templateSelect.getOptions().clear();
154 
155         for (TemplateDefinition templateDefinition : templateDefinitons) {
156             String title = MessagesUtil.javaScriptString(getI18nTitle(templateDefinition));
157             templateSelect.setOptions(title, templateDefinition.getId());
158         }
159         this.displayValue = templateSelect.getHtml();
160         return VIEW_VALUE;
161     }
162 
163     private String getI18nTitle(TemplateDefinition template) {
164         Messages messages = MessagesManager.getMessages(template.getI18nBasename());
165         String title = template.getTitle();
166         return messages.getWithDefault(title, title);
167     }
168 
169     @Override
170     protected void copyOrMove(int action) throws ExchangeException, RepositoryException {
171         String pathClipboard = this.getRequest().getParameter("pathClipboard");
172         if(isCopyOrMoveAvailable(pathSelected, pathClipboard)) {
173             super.copyOrMove(action);
174         } else {
175             throw new RuntimeException("Template of the page is not available on this level");
176         }
177     }
178 
179     protected boolean isCopyOrMoveAvailable(String targetPath, String pagePath) throws RepositoryException {
180         boolean result = false;
181         try {
182             Session session = MgnlContext.getJCRSession(getRepository());
183             String tempNodeName = Path.getUniqueLabel(session, targetPath, "temporaryNode");
184             Node targetPage = session.getNode(targetPath).addNode(tempNodeName, "mgnl:page");
185             Node movedPage = session.getNode(pagePath);
186 
187             TemplateDefinitionRegistry registry = Components.getComponent(TemplateDefinitionRegistry.class);
188             TemplateDefinition definition = registry.getTemplateDefinition(movedPage.getNode("MetaData").getProperty("mgnl:template").getString());
189 
190             result = templateAssignment.getAvailableTemplates(targetPage).contains(definition);
191 
192             session.refresh(false);
193         } catch (RegistrationException e) {
194             throw new RuntimeException("Template availability could not be resolved");
195         }
196         return result;
197     }
198 
199     public boolean isGetAvailableTemplates() {
200         return getAvailableTemplates;
201     }
202 
203     public void setGetAvailableTemplates(boolean getAvailableTemplates) {
204         this.getAvailableTemplates = getAvailableTemplates;
205     }
206 }