View Javadoc
1   /**
2    * This file Copyright (c) 2012-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.dam.app.assets.column;
35  
36  import static info.magnolia.dam.jcr.AssetNodeTypes.Asset.ASSET_NAME;
37  import static info.magnolia.dam.jcr.AssetNodeTypes.AssetResource.*;
38  import static org.apache.commons.lang3.StringUtils.EMPTY;
39  
40  import info.magnolia.cms.beans.config.MIMEMapping;
41  import info.magnolia.dam.jcr.AssetNodeTypes.Asset;
42  import info.magnolia.jcr.util.PropertyUtil;
43  import info.magnolia.objectfactory.Components;
44  import info.magnolia.ui.imageprovider.ImageProvider;
45  import info.magnolia.ui.vaadin.integration.jcr.JcrItemUtil;
46  import info.magnolia.ui.workbench.column.AbstractColumnFormatter;
47  
48  import javax.inject.Inject;
49  import javax.jcr.Item;
50  import javax.jcr.Node;
51  import javax.jcr.RepositoryException;
52  
53  import org.apache.commons.lang3.StringUtils;
54  import org.slf4j.Logger;
55  import org.slf4j.LoggerFactory;
56  
57  import com.google.common.net.MediaType;
58  import com.vaadin.v7.ui.Table;
59  
60  /**
61   * ColumnFormatter for names of assets.<br>
62   * - For folders, return the folder node name <br>
63   * - For assets, return the asset name. If empty, return the asset node name. <br>
64   * - Display the thumbnail before the asset name, fallback to asset's icon if the thumbnail is not available.
65   */
66  public class AssetNameColumnFormatter extends AbstractColumnFormatter<AssetNameColumnDefinition> {
67  
68      private static final Logger log = LoggerFactory.getLogger(AssetNameColumnFormatter.class);
69      private static final String ICON_TAG = "<span class=\"%s v-table-icon-element\"></span>";
70      private static final String IMG_TAG = "<span class=\"v-table-icon-element\"><img alt=\"thumbnail\" src=\"%s\" class=\"inline-thumbnail\" ></span>";
71  
72      private final ImageProvider imageProvider;
73  
74      @Inject
75      public AssetNameColumnFormatter(AssetNameColumnDefinition definition, ImageProvider imageProvider) {
76          super(definition);
77          this.imageProvider = imageProvider;
78      }
79  
80      /**
81       * @deprecated since 2.2.1, use {@link #AssetNameColumnFormatter(AssetNameColumnDefinition, ImageProvider)} instead.
82       */
83      @Deprecated
84      public AssetNameColumnFormatter(AssetNameColumnDefinition definition) {
85          this(definition, Components.getComponent(ImageProvider.class));
86      }
87  
88      @Override
89      public Object generateCell(Table source, Object itemId, Object columnId) {
90          final Item jcrItem = getJcrItem(source, itemId);
91          if (jcrItem != null && jcrItem.isNode()) {
92              Node node = (Node) jcrItem;
93              try {
94                  String icon = getIcon((Node) jcrItem);
95                  if (node.hasProperty(ASSET_NAME) && StringUtils.isNotBlank(node.getProperty(ASSET_NAME).getString())) {
96                      return icon + node.getProperty(ASSET_NAME).getString();
97                  }
98                  return icon + node.getName();
99              } catch (RepositoryException e) {
100                 log.warn("Unable to get the displayed value for the name column ", e);
101             }
102         }
103         return EMPTY;
104     }
105 
106     private String getIcon(Node node) throws RepositoryException {
107         String mimeType = getMimeType(node);
108         if (definition.isThumbnails() && isImage(mimeType) && !MediaType.SVG_UTF_8.is(MediaType.parse(mimeType))) {
109             return getThumbnail(JcrItemUtil.getItemId(node));
110         }
111 
112         if (isAssetNode(node)) {
113             return String.format(ICON_TAG, MIMEMapping.getMIMETypeIconStyle(mimeType));
114         }
115 
116         return EMPTY;
117     }
118 
119     private String getThumbnail(Object itemId) {
120         if (imageProvider == null) {
121             return EMPTY;
122         }
123 
124         String thumbnailPath = imageProvider.getThumbnailPath(itemId);
125 
126         if (StringUtils.isBlank(thumbnailPath)) {
127             return EMPTY;
128         }
129 
130         return String.format(IMG_TAG, thumbnailPath);
131     }
132 
133     private String getMimeType(Node node) throws RepositoryException {
134         if (isAssetNode(node)) {
135             String mimeType = PropertyUtil.getString(node.getNode(RESOURCE_NAME), MIMETYPE, "");
136             if (StringUtils.isBlank(mimeType)) {
137                 String extension = PropertyUtil.getString(node.getNode(RESOURCE_NAME), EXTENSION, "");
138                 return MIMEMapping.getMIMEType(extension);
139             }
140 
141             return mimeType;
142         }
143 
144         return null;
145     }
146 
147     private boolean isImage(String mimeType) {
148         return mimeType != null && mimeType.matches("image.*");
149     }
150 
151     private boolean isAssetNode(Node node) throws RepositoryException {
152         return node.isNodeType(Asset.NAME) && node.hasNode(RESOURCE_NAME);
153     }
154 }