View Javadoc
1   /**
2    * This file Copyright (c) 2012-2016 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.i18nsystem.I18nizer;
39  import info.magnolia.jcr.util.NodeTypes;
40  import info.magnolia.objectfactory.Components;
41  import info.magnolia.rendering.template.TemplateDefinition;
42  import info.magnolia.rendering.template.assignment.TemplateDefinitionAssignment;
43  import info.magnolia.ui.api.context.UiContext;
44  import info.magnolia.ui.api.i18n.I18NAuthoringSupport;
45  import info.magnolia.ui.form.field.definition.SelectFieldOptionDefinition;
46  import info.magnolia.ui.form.field.factory.SelectFieldFactory;
47  import info.magnolia.ui.vaadin.integration.jcr.JcrNewNodeAdapter;
48  import info.magnolia.ui.vaadin.integration.jcr.JcrNodeAdapter;
49  
50  import java.util.ArrayList;
51  import java.util.Collection;
52  import java.util.Collections;
53  import java.util.List;
54  
55  import javax.inject.Inject;
56  import javax.jcr.Node;
57  import javax.jcr.RepositoryException;
58  
59  import org.apache.commons.lang3.StringUtils;
60  import org.slf4j.Logger;
61  import org.slf4j.LoggerFactory;
62  
63  import com.vaadin.data.Item;
64  
65  /**
66   * Define a Template selector field.
67   * The values displayed in the field are initialized based on the
68   * related Item (Image of a JCR node) and {@link TemplateDefinitionAssignment}.
69   */
70  public class TemplateSelectorFieldFactory extends SelectFieldFactory<TemplateSelectorDefinition> {
71  
72      private static final Logger log = LoggerFactory.getLogger(TemplateSelectorFieldFactory.class);
73  
74      private final TemplateDefinitionAssignment templateAssignment;
75      private final I18nizer i18nizer;
76  
77      @Inject
78      public TemplateSelectorFieldFactory(TemplateSelectorDefinition definition, Item relatedFieldItem, UiContext uiContext, I18NAuthoringSupport i18nAuthoringSupport, TemplateDefinitionAssignment templateDefinitionAssignment, I18nizer i18nizer) {
79          super(definition, relatedFieldItem, uiContext, i18nAuthoringSupport);
80          this.templateAssignment = templateDefinitionAssignment;
81          this.i18nizer = i18nizer;
82      }
83  
84      /**
85       * @deprecated since 5.4.6 - use {@link #TemplateSelectorFieldFactory(TemplateSelectorDefinition, Item, UiContext, I18NAuthoringSupport, TemplateDefinitionAssignment, I18nizer)} instead.
86       */
87      @Deprecated
88      public TemplateSelectorFieldFactory(TemplateSelectorDefinition definition, Item relatedFieldItem, TemplateDefinitionAssignment templateDefinitionAssignment, I18nizer i18nizer) {
89          this(definition, relatedFieldItem, null, Components.getComponent(I18NAuthoringSupport.class), templateDefinitionAssignment, i18nizer);
90      }
91  
92      /**
93       * @deprecated since 5.4.3 - use {@link #TemplateSelectorFieldFactory(TemplateSelectorDefinition, Item, UiContext, I18NAuthoringSupport, TemplateDefinitionAssignment, I18nizer)} instead.
94       */
95      @Deprecated
96      public TemplateSelectorFieldFactory(TemplateSelectorDefinition definition, Item relatedFieldItem, TemplateDefinitionAssignment templateDefinitionAssignment) {
97          this(definition, relatedFieldItem, null, Components.getComponent(I18NAuthoringSupport.class), templateDefinitionAssignment, Components.getComponent(I18nizer.class));
98      }
99  
100     /**
101      * @deprecated since 5.3.4 - use {@link #TemplateSelectorFieldFactory(TemplateSelectorDefinition, Item, UiContext, I18NAuthoringSupport, TemplateDefinitionAssignment, I18nizer)} instead.
102      */
103     @Deprecated
104     public TemplateSelectorFieldFactory(TemplateSelectorDefinition definition, Item relatedFieldItem) {
105         this(definition, relatedFieldItem, null, Components.getComponent(I18NAuthoringSupport.class), Components.getComponent(TemplateDefinitionAssignment.class), Components.getComponent(I18nizer.class));
106     }
107 
108     /**
109      * Returns the available templates based on the current node.
110      */
111     @Override
112     public List<SelectFieldOptionDefinition> getSelectFieldOptionDefinition() {
113         List<SelectFieldOptionDefinition> res = new ArrayList<SelectFieldOptionDefinition>();
114 
115         if (item instanceof JcrNodeAdapter) {
116             Node associatedNode = ((JcrNodeAdapter) item).getJcrItem();
117 
118             Collection<TemplateDefinition> templates = Collections.emptySet();
119 
120             if (item instanceof JcrNewNodeAdapter) {
121                 // creates a temporary node underneath the parent to overcome a restriction in template availability,
122                 // see MGNLSTK-1185
123                 try {
124                     Node tempNode = associatedNode.addNode("temp", NodeTypes.Page.NAME);
125                     templates = templateAssignment.getAvailableTemplates(tempNode);
126                     associatedNode.getSession().removeItem(tempNode.getPath());
127                 } catch (RepositoryException e) {
128                     log.error("Could not create temporary node to get available templates.", e);
129                 }
130             } else {
131                 templates = templateAssignment.getAvailableTemplates(associatedNode);
132             }
133 
134             for (TemplateDefinition templateDefinition : templates) {
135                 SelectFieldOptionDefinition option = new SelectFieldOptionDefinition();
136                 option.setValue(templateDefinition.getId());
137                 option.setLabel(this.getTranslatedTitle(templateDefinition));
138                 res.add(option);
139             }
140         }
141         return res;
142     }
143 
144     @Override
145     protected Class<?> getDefaultFieldType() {
146         return String.class;
147     }
148 
149     private String getTranslatedTitle(TemplateDefinition definition) {
150         String translatedTitle = i18nizer.decorate(definition).getTitle();
151         if (isMessageKey(translatedTitle)) { //compatibility with old i18n mechanism, remove when we get rid of i18nBasename
152             translatedTitle = getI18nTitle(definition);
153         }
154         if (isMessageKey(translatedTitle)) { //fallback to id
155             translatedTitle = definition.getId();
156         }
157         return translatedTitle;
158     }
159 
160     private boolean isMessageKey(String key) {
161         return !StringUtils.endsWith(key, ".") && StringUtils.contains(key, ".") && !StringUtils.contains(key, " ");
162     }
163 
164     /**
165      * Get i18n Template title.
166      *
167      * @deprecated since 5.4.3 - use {@link #getTranslatedTitle(info.magnolia.rendering.template.TemplateDefinition)} instead.
168      */
169     // FIXME: SCRUM-1635 (ehe) review PageEditorPresenter and way Templates are parsed.
170     @Deprecated
171     public static synchronized String getI18nTitle(TemplateDefinition definition) {
172         Messages messages = MessagesManager.getMessages(definition.getI18nBasename());
173         // fallback to id (the latter should never be null) in case title is blank.
174         String templateTitle = StringUtils.isNotBlank(definition.getTitle()) ? definition.getTitle() : definition.getId();
175         return messages.getWithDefault(templateTitle, templateTitle);
176     }
177 }