View Javadoc

1   /**
2    * This file Copyright (c) 2012-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.pages.app.field;
35  
36  import info.magnolia.cms.i18n.Messages;
37  import info.magnolia.cms.i18n.MessagesManager;
38  import info.magnolia.jcr.util.NodeTypes;
39  import info.magnolia.objectfactory.Components;
40  import info.magnolia.rendering.template.TemplateDefinition;
41  import info.magnolia.rendering.template.assignment.TemplateDefinitionAssignment;
42  import info.magnolia.ui.form.field.definition.SelectFieldOptionDefinition;
43  import info.magnolia.ui.form.field.factory.SelectFieldFactory;
44  import info.magnolia.ui.vaadin.integration.jcr.JcrNewNodeAdapter;
45  import info.magnolia.ui.vaadin.integration.jcr.JcrNodeAdapter;
46  
47  import java.util.ArrayList;
48  import java.util.Collection;
49  import java.util.Collections;
50  import java.util.List;
51  
52  import javax.inject.Inject;
53  import javax.jcr.Node;
54  import javax.jcr.RepositoryException;
55  
56  import org.slf4j.Logger;
57  import org.slf4j.LoggerFactory;
58  
59  import com.vaadin.data.Item;
60  
61  /**
62   * Define a Template selector field.
63   * The values displayed in the field are initialized based on the
64   * related Item (Image of a JCR node) and {@link TemplateDefinitionAssignment}.
65   */
66  public class TemplateSelectorFieldFactory extends SelectFieldFactory<TemplateSelectorDefinition> {
67  
68      private static final Logger log = LoggerFactory.getLogger(TemplateSelectorFieldFactory.class);
69      private TemplateDefinitionAssignment templateAssignment;
70  
71      /**
72       * @deprecated since 5.3.4 use the other constructor instead.
73       */
74      @Deprecated
75      public TemplateSelectorFieldFactory(TemplateSelectorDefinition definition, Item relatedFieldItem) {
76          this(definition, relatedFieldItem, Components.getComponent(TemplateDefinitionAssignment.class));
77      }
78  
79      @Inject
80      public TemplateSelectorFieldFactory(TemplateSelectorDefinition definition, Item relatedFieldItem, TemplateDefinitionAssignment templateDefinitionAssignment) {
81          super(definition, relatedFieldItem);
82          this.templateAssignment = templateDefinitionAssignment;
83      }
84  
85      /**
86       * Returns the available templates based on the current node.
87       */
88      @Override
89      public List<SelectFieldOptionDefinition> getSelectFieldOptionDefinition() {
90          List<SelectFieldOptionDefinition> res = new ArrayList<SelectFieldOptionDefinition>();
91  
92          if (item instanceof JcrNodeAdapter) {
93              Node associatedNode = ((JcrNodeAdapter) item).getJcrItem();
94  
95              Collection<TemplateDefinition> templates = Collections.EMPTY_SET;
96  
97              if (item instanceof JcrNewNodeAdapter) {
98                  // creates a temporary node underneath the parent to overcome a restriction in template availability,
99                  // see MGNLSTK-1185
100                 try {
101                     Node tempNode = associatedNode.addNode("temp", NodeTypes.Page.NAME);
102                     templates = templateAssignment.getAvailableTemplates(tempNode);
103                     associatedNode.getSession().removeItem(tempNode.getPath());
104                 } catch (RepositoryException e) {
105                     log.error("Could not create temporary node to get available templates.", e);
106                 }
107             }
108 
109             else {
110                 templates = templateAssignment.getAvailableTemplates(associatedNode);
111             }
112 
113             for (TemplateDefinition templateDefinition : templates) {
114                 SelectFieldOptionDefinition option = new SelectFieldOptionDefinition();
115                 option.setValue(templateDefinition.getId());
116                 option.setLabel(getI18nTitle(templateDefinition));
117                 res.add(option);
118             }
119         }
120         return res;
121     }
122 
123     @Override
124     protected Class<?> getDefaultFieldType() {
125         return String.class;
126     }
127 
128     /**
129      * Get i18n Template title.
130      */
131     // FIXME: SCRUM-1635 (ehe) review PageEditorPresenter and way Templates are parsed.
132     public static synchronized String getI18nTitle(TemplateDefinition templateDefinition) {
133         Messages messages = MessagesManager.getMessages(templateDefinition.getI18nBasename());
134         return messages.getWithDefault(templateDefinition.getTitle(), templateDefinition.getTitle());
135     }
136 }