View Javadoc
1   /**
2    * This file Copyright (c) 2018 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.ui.contentapp.column.jcr;
35  
36  import info.magnolia.i18nsystem.SimpleTranslator;
37  import info.magnolia.jcr.util.NodeTypes;
38  import info.magnolia.ui.contentapp.configuration.column.ColumnType;
39  import info.magnolia.ui.contentapp.configuration.column.ConfiguredColumnDefinition;
40  import info.magnolia.ui.field.ComboBoxFieldDefinition;
41  import info.magnolia.ui.datasource.enumeration.EnumDatasourceDefinition;
42  
43  import java.security.AccessControlException;
44  
45  import javax.inject.Inject;
46  import javax.jcr.Item;
47  import javax.jcr.Node;
48  import javax.jcr.RepositoryException;
49  import javax.jcr.Session;
50  
51  import org.slf4j.Logger;
52  import org.slf4j.LoggerFactory;
53  
54  import com.machinezoo.noexception.Exceptions;
55  import com.vaadin.data.ValueProvider;
56  import com.vaadin.ui.renderers.HtmlRenderer;
57  
58  import lombok.Getter;
59  import lombok.Setter;
60  
61  /**
62   * Defines a column that displays the activation status of an item.
63   */
64  @Getter
65  @Setter
66  @ColumnType("jcrStatusColumn")
67  public class JcrStatusColumnDefinition extends ConfiguredColumnDefinition<Item> {
68  
69      private static final Logger log = LoggerFactory.getLogger(JcrStatusColumnDefinition.class);
70  
71      // Show Activation Status
72      private boolean activation = true;
73      // Show Permission Status
74      private boolean permissions = true;
75  
76      public JcrStatusColumnDefinition() {
77          setName("activationStatus");
78          setRenderer(HtmlRenderer.class);
79          setValueProvider(StatusValueProvider.class);
80          ComboBoxFieldDefinition<String> filterComponent = new ComboBoxFieldDefinition<>();
81          filterComponent.setName("filterComponent");
82          EnumDatasourceDefinition<ActivationStatus> datasource = new EnumDatasourceDefinition<>();
83          datasource.setEnumeration(ActivationStatus.class);
84          filterComponent.setEmptySelectionAllowed(true);
85          filterComponent.setDatasource(datasource);
86          setFilterComponent(filterComponent);
87          setWidth(75d);
88      }
89  
90      /**
91       * {@link ValueProvider} implementation yielding a JCR node's activation status.
92       */
93      public static class StatusValueProvider implements ValueProvider<Item, String> {
94  
95          private final SimpleTranslator i18n;
96          private final JcrStatusColumnDefinition definition;
97  
98          @Inject
99          public StatusValueProvider(SimpleTranslator simpleTranslator, JcrStatusColumnDefinition columnDefinition) {
100             this.i18n = simpleTranslator;
101             this.definition = columnDefinition;
102         }
103 
104         @Override
105         public String apply(Item node) {
106             if (!node.isNode()) {
107                 return null;
108             }
109             String activationStatus = "";
110             ActivationStatus activationType;
111             String activationStatusMessage;
112             String permissionStatus = "";
113 
114             // activation status
115             if (definition.isActivation()) {
116                 int status = Exceptions.wrap().get(() -> NodeTypes.Activatable.getActivationStatus((Node) node));
117 
118                 switch (status) {
119                 case NodeTypes.Activatable.ACTIVATION_STATUS_MODIFIED:
120                     activationType = ActivationStatus.Modified;
121                     activationStatusMessage = i18n.translate("activation-status.columns.modified");
122                     break;
123                 case NodeTypes.Activatable.ACTIVATION_STATUS_ACTIVATED:
124                     activationType = ActivationStatus.Activated;
125                     activationStatusMessage = i18n.translate("activation-status.columns.activated");
126                     break;
127                 default:
128                     activationType = ActivationStatus.NotActivated;
129                     activationStatusMessage = i18n.translate("activation-status.columns.not-activated");
130                 }
131                 activationStatus = "<span class=\"" + activationType.getStyleName() + "\" title=\"" + activationStatusMessage + "\"></span>";
132                 activationStatus = activationStatus + "<span class=\"hidden-for-aria\">" + activationStatusMessage + "</span>";
133             }
134 
135             // permission status
136             if (definition.isPermissions()) {
137                 try {
138                     node.getSession().checkPermission(node.getPath(), Session.ACTION_ADD_NODE + "," + Session.ACTION_REMOVE + "," + Session.ACTION_SET_PROPERTY);
139                 } catch (AccessControlException e) {
140                     permissionStatus = "<span class=\"icon-read-only\"></span>";
141                 } catch (RepositoryException e) {
142                     log.debug("Failed to resolve permissions status of the node [{}], yielding empty value", node, e);
143                     return "";
144                 }
145             }
146             return activationStatus + permissionStatus;
147         }
148     }
149 
150     /**
151      * Helper for representing activation status in the UI.
152      */
153     @Getter
154     public enum ActivationStatus {
155 
156         Activated("icon-status-green", "color-green", NodeTypes.Activatable.ACTIVATION_STATUS_ACTIVATED),
157         Modified("icon-status-orange", "color-yellow", NodeTypes.Activatable.ACTIVATION_STATUS_MODIFIED),
158         NotActivated("icon-status-red", "color-red", NodeTypes.Activatable.ACTIVATION_STATUS_NOT_ACTIVATED);
159 
160         private final String baseStyleName = "activation-status";
161         private String icon;
162         private String color;
163         private int id;
164 
165         ActivationStatus(String icon, String color, int id) {
166             this.icon = icon;
167             this.color = color;
168             this.id = id;
169         }
170 
171         public String getStyleName() {
172             return baseStyleName + " " + icon + " " + color;
173         }
174     }
175 }