View Javadoc
1   /**
2    * This file Copyright (c) 2017 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.column;
35  
36  import info.magnolia.cms.beans.config.URI2RepositoryManager;
37  import info.magnolia.cms.beans.config.VirtualURIManager;
38  import info.magnolia.i18nsystem.SimpleTranslator;
39  import info.magnolia.objectfactory.ComponentProvider;
40  import info.magnolia.repository.RepositoryConstants;
41  import info.magnolia.ui.workbench.column.AbstractColumnFormatter;
42  import info.magnolia.ui.workbench.column.StatusColumnFormatter.ActivationStatus;
43  import info.magnolia.ui.workbench.column.definition.ColumnFormatter;
44  import info.magnolia.ui.workbench.column.definition.PropertyColumnDefinition;
45  
46  import javax.inject.Inject;
47  import javax.jcr.Item;
48  import javax.jcr.Node;
49  import javax.jcr.RepositoryException;
50  
51  import org.apache.commons.lang3.StringUtils;
52  import org.slf4j.Logger;
53  import org.slf4j.LoggerFactory;
54  
55  import com.vaadin.ui.Table;
56  
57  /**
58   * Status column formatter, optionally warning user also about conflicting URI2Repository mappings.
59   */
60  public class PageNameColumnFormatter extends AbstractColumnFormatter<PropertyColumnDefinition> {
61  
62      private static Logger log = LoggerFactory.getLogger(PageNameColumnFormatter.class);
63  
64      private ColumnFormatter innerFormatter;
65  
66      private final URI2RepositoryManager uriManager;
67      private final VirtualURIManager vuriManager;
68      private final Definition definition;
69      private final SimpleTranslator i18n;
70  
71      @Inject
72      public PageNameColumnFormatter(Definition definition, SimpleTranslator i18n, URI2RepositoryManager uriManager, VirtualURIManager vuriManager, ComponentProvider componentProvider) {
73          super(definition);
74          this.i18n = i18n;
75          this.definition = definition;
76          this.uriManager = uriManager;
77          this.vuriManager = vuriManager;
78          if (definition.getInnerFormatter() != null) {
79              this.innerFormatter = componentProvider.newInstance(definition.getInnerFormatter(), definition, i18n);
80          }
81      }
82  
83      @Override
84      public Object generateCell(Table source, Object itemId, Object columnId) {
85          String basicStatus = null;
86          final Item jcrItem = getJcrItem(source, itemId);
87          if (innerFormatter != null) {
88              basicStatus = (String) innerFormatter.generateCell(source, itemId, columnId);
89          } else if (jcrItem != null) {
90              try {
91                  basicStatus = jcrItem.getName();
92              } catch (RepositoryException re) {
93                  log.debug("Failed to retrieve path for item '{}':", jcrItem.toString(), re);
94              }
95          }
96  
97          // URI mapping conflict status
98          String conflictStatus = null;
99          try {
100             if (jcrItem != null && jcrItem.isNode()) {
101                 Node node = (Node) jcrItem;
102                 String nodePath = node.getPath();
103                 if (!RepositoryConstants.WEBSITE.equals(uriManager.getRepository(nodePath))) {
104                     String conflictStatusMessage = i18n.translate("pages.browser.status-column.uriConflictDetected");
105                     conflictStatus = "<span style=\"float:right;\" class=\"" + ActivationStatus.NOT_ACTIVATED.getBaseStyleName()
106                     + " icon-error color-red\" title=\"" + conflictStatusMessage + "\"></span>";
107                 }
108                 if (definition.isReportVirtualURIConflicts()) {
109                     String uri = vuriManager.getURIMapping(nodePath);
110                     if (StringUtils.isNotEmpty(uri) && !nodePath.equals(uri)) {
111                         String conflictStatusMessage = i18n.translate("pages.browser.status-column.virtualUriConflictDetected");
112                         conflictStatus = "<span style=\"float:right;\" class=\"" + ActivationStatus.MODIFIED.getBaseStyleName()
113                         + " icon-warning color-yellow\" title=\"" + conflictStatusMessage + "\"></span>";
114                     }
115                 }
116             }
117         } catch (RepositoryException e) {
118             log.debug("Could not access the JCR item for the following node identifier " + itemId, e);
119         }
120 
121         return StringUtils.defaultIfEmpty((basicStatus == null ? "" : basicStatus) + (conflictStatus == null ? "" : conflictStatus), null);
122     }
123 
124     /**
125      * Definition for outer class introducing configurable virtual uri mapping conflict checks.
126      */
127     public static class Definition extends PropertyColumnDefinition {
128         private boolean isVirtualURICheckEnabled;
129         private Class<? extends ColumnFormatter> innerFormatter;
130 
131         @Override
132         public void setFormatterClass(Class<? extends ColumnFormatter> formatterClass) {
133             this.innerFormatter = formatterClass;
134         }
135 
136         public Class<? extends ColumnFormatter> getInnerFormatter() {
137             return innerFormatter;
138         }
139 
140         @Override
141         public Class<? extends ColumnFormatter> getFormatterClass() {
142             return PageNameColumnFormatter.class;
143         }
144 
145         public boolean isReportVirtualURIConflicts() {
146             return isVirtualURICheckEnabled;
147         }
148 
149         public void setReportVirtualURIConflicts(boolean isVirtualURICheckEnabled) {
150             this.isVirtualURICheckEnabled = isVirtualURICheckEnabled;
151         }
152 
153     }
154 }