View Javadoc
1   /**
2    * This file Copyright (c) 2013-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.module.resources.app;
35  
36  import info.magnolia.i18nsystem.SimpleTranslator;
37  import info.magnolia.jcr.util.NodeTypes;
38  import info.magnolia.jcr.util.NodeTypes.Renderable;
39  import info.magnolia.jcr.util.NodeUtil;
40  import info.magnolia.module.resources.ResourceTypes;
41  import info.magnolia.ui.workbench.column.AbstractColumnFormatter;
42  
43  import javax.inject.Inject;
44  import javax.jcr.Item;
45  import javax.jcr.Node;
46  import javax.jcr.RepositoryException;
47  
48  import org.apache.commons.lang3.StringUtils;
49  import org.slf4j.Logger;
50  import org.slf4j.LoggerFactory;
51  
52  import com.vaadin.v7.ui.Table;
53  
54  /**
55   * Column formatter that displays either the type of resource or a folder name.
56   *
57   * @see ResourceColumnDefinition
58   * @deprecated Since Resources 2.4 (Magnolia 5.4), the JCR-based resources app is deprecated.
59   * Instead, the new resources-app lists all resources across classpath, file-system and the JCR 'resources' workspace.
60   */
61  @Deprecated
62  public class ResourceTemplateColumnFormatter extends AbstractColumnFormatter<ResourceColumnDefinition> {
63  
64      private static final long serialVersionUID = 4761675760647565415L;
65      private static final Logger log = LoggerFactory.getLogger(ResourceTemplateColumnFormatter.class);
66      private SimpleTranslator simpleTranslator;
67  
68      @Inject
69      public ResourceTemplateColumnFormatter(ResourceColumnDefinition definition, SimpleTranslator simpleTranslator) {
70          super(definition);
71          this.simpleTranslator = simpleTranslator;
72      }
73  
74      /**
75       * Uses an i18n key to get the label for a given resource template. I.e. given a template <code>resources:foo</code> it will look up for a key in the
76       * module's i18n properties file named <code>resources.content.mgnl-template.options.foo</code>.
77       */
78      @Override
79      public Object generateCell(Table source, Object itemId, Object columnId) {
80          final Item jcrItem = getJcrItem(source, itemId);
81          if (jcrItem != null && jcrItem.isNode()) {
82              Node node = (Node) jcrItem;
83              try {
84                  if (NodeUtil.isNodeType(node, NodeTypes.Content.NAME)) {
85                      String resourceType = StringUtils.substringAfter(Renderable.getTemplate(node), ResourceTypes.RESOURCES_PREFIX);
86                      if (NodeUtil.hasMixin(node, NodeTypes.Deleted.NAME)) {
87                          return simpleTranslator.translate("processed-resources.content.mgnl-template.options.deleted");
88                      }
89                      if (resourceType == null) {
90                          return simpleTranslator.translate("processed-resources.content.mgnl-template.options.noTemplate");
91                      }
92                      if (ResourceTypes.BINARY_SUFFIX.equals(resourceType) && node.hasNode(ResourceTypes.BINARY_SUFFIX) && node.getNode(ResourceTypes.BINARY_SUFFIX).hasProperty("extension")) {
93                          return simpleTranslator.translate("processed-resources.content.mgnl-template.options.binary.extension", node.getNode(ResourceTypes.BINARY_SUFFIX).getProperty("extension").getString());
94                      }
95                      return simpleTranslator.translate("processed-resources.content.mgnl-template.options." + resourceType);
96                  }
97              } catch (RepositoryException e) {
98                  log.warn("Unable to get name of resource template for column", e);
99              }
100         }
101         return "";
102     }
103 }