View Javadoc
1   /**
2    * This file Copyright (c) 2003-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.cms.util;
35  
36  import info.magnolia.cms.core.Content;
37  import info.magnolia.cms.core.MetaData;
38  
39  import java.util.Calendar;
40  
41  import javax.jcr.Node;
42  
43  import org.apache.commons.lang3.StringUtils;
44  import org.slf4j.Logger;
45  import org.slf4j.LoggerFactory;
46  
47  
48  /**
49   * Util to work with {@link info.magnolia.cms.core.MetaData}.
50   *
51   * @deprecated since 4.5 as it operates on deprecated Content - use {@link info.magnolia.jcr.util.MetaDataUtil} instead.
52   *
53   *             Hint: since 5.0 you should use {@link info.magnolia.jcr.util.NodeUtil} instead.
54   */
55  @Deprecated
56  public class MetaDataUtil {
57      private static final Logger log = LoggerFactory.getLogger(MetaDataUtil.class);
58  
59      public static String getPropertyValueString(Content content, String propertyName) {
60          return getPropertyValueString(content, propertyName, null);
61      }
62  
63      /**
64       * Returns the representation of the value as a String.
65       */
66      public static String getPropertyValueString(Content content, String propertyName, String dateFormat) {
67          try {
68              if (propertyName.equals(MetaData.CREATION_DATE) || propertyName.equals(MetaData.LAST_MODIFIED) || propertyName.equals(MetaData.LAST_ACTION)) {
69                  final Calendar date;
70                  if (propertyName.equals(MetaData.CREATION_DATE)) {
71                      date = content.getMetaData().getCreationDate();
72                  } else if (propertyName.equals(MetaData.LAST_MODIFIED)) {
73                      date = content.getMetaData().getModificationDate();
74                  } else if (propertyName.equals(MetaData.LAST_ACTION)) {
75                      date = content.getMetaData().getLastActionDate();
76                  } else {
77                      date = content.getMetaData().getDateProperty(propertyName);
78                  }
79  
80                  if (date != null) {
81                      return DateUtil.format(date.getTime(), dateFormat);
82                  }
83              } else if (propertyName.equals(MetaData.ACTIVATED)) {
84                  return Boolean.toString(content.getMetaData().getBooleanProperty(propertyName));
85              } else {
86                  return content.getMetaData().getStringProperty(propertyName);
87              }
88          } catch (Exception e) {
89              log.debug("Exception caught: {}", e.getMessage(), e);
90          }
91          return StringUtils.EMPTY;
92      }
93  
94      /**
95       * @deprecated since 4.5 - use {@link #getActivationStatusIcon(Node)} instead.
96       */
97      public static String getActivationStatusIcon(Content content) {
98          return getActivationStatusIcon(content.getJCRNode());
99      }
100 
101     /**
102      * Return iconFileName for a node.
103      *
104      * @param node node to read status from
105      * @return file name for an icon
106      */
107     public static String getActivationStatusIcon(Node node) {
108 
109         MetaData metaData = info.magnolia.jcr.util.MetaDataUtil.getMetaData(node);
110         String iconFileName;
111         switch (metaData.getActivationStatus()) {
112         case MetaData.ACTIVATION_STATUS_MODIFIED:
113             iconFileName = "indicator_yellow.gif";
114             break;
115         case MetaData.ACTIVATION_STATUS_ACTIVATED:
116             iconFileName = "indicator_green.gif";
117             break;
118         default:
119             iconFileName = "indicator_red.gif";
120         }
121 
122         return iconFileName;
123     }
124 
125 }