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