View Javadoc

1   /**
2    * This file Copyright (c) 2003-2011 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.gui.controlx.list;
35  
36  import info.magnolia.cms.core.Content;
37  import info.magnolia.cms.core.NodeData;
38  
39  import java.util.Calendar;
40  import java.util.Date;
41  
42  import javax.jcr.PropertyType;
43  import javax.jcr.RepositoryException;
44  
45  import org.apache.commons.beanutils.MethodUtils;
46  import org.apache.commons.beanutils.PropertyUtils;
47  import org.apache.commons.lang.StringUtils;
48  import org.slf4j.LoggerFactory;
49  import org.slf4j.Logger;
50  
51  
52  /**
53   * @author Sameer Charles $Id: DefaultValueProvider.java 41137 2011-01-06 18:19:25Z gjoseph $
54   */
55  public class DefaultValueProvider implements ValueProvider {
56      private static final Logger log = LoggerFactory.getLogger(DefaultValueProvider.class);
57  
58      /**
59       * singleton
60       */
61      private static ValueProvider thisInstance = new DefaultValueProvider();
62  
63      /**
64       * Not allowed to be instanciated outside the scope of this class
65       */
66      protected DefaultValueProvider() {
67      }
68  
69      /**
70       * get instance
71       */
72      public static ValueProvider getInstance() {
73          return thisInstance;
74      }
75  
76      /* (non-Javadoc)
77       * @see info.magnolia.cms.gui.controlx.list.util.ValueProvider#getValue(java.lang.String, java.lang.Object)
78       */
79      public Object getValue(String name, Object obj) {
80          Object value = null;
81          try {
82              if (obj instanceof Content) {
83                  Content node = (Content) obj;
84                  if (node.hasNodeData(name)) {
85                      NodeData nd = node.getNodeData(name);
86                      if (nd.getType() == PropertyType.DATE) {
87                          value = nd.getDate();
88                      }
89                      else {
90                          value = nd.getString();
91                      }
92                  }
93  
94                  if (value == null) {
95                      try {
96                          value = PropertyUtils.getProperty(node.getMetaData(), name);
97                      }
98                      catch (NoSuchMethodException e) {
99                          value = node.getMetaData().getStringProperty(name);
100                         if (StringUtils.isEmpty((String) value)) {
101                             value = null;
102                         }
103                     }
104                 }
105             }
106 
107             if (value == null) {
108                 // is this a property of the object
109                 try {
110                     value = PropertyUtils.getProperty(obj, name);
111                 }
112                 catch (NoSuchMethodException e1) {
113                     // check if getter exist for this name
114                     try {
115                         String methodName = "get"
116                             + StringUtils.substring(name, 0, 1).toUpperCase()
117                             + StringUtils.substring(name, 1);
118                         value = MethodUtils.invokeMethod(this, methodName, obj);
119                     }
120                     catch (NoSuchMethodException e2) {
121                         value = StringUtils.EMPTY;
122                     }
123                 }
124             }
125         }
126         catch (Exception e) {
127             log.error("can't get value", e);
128             value = StringUtils.EMPTY;
129         }
130 
131         if (value instanceof Calendar) {
132             value = new Date(((Calendar) value).getTimeInMillis());
133         }
134 
135         return value;
136     }
137 
138     /**
139      * get node type
140      * @return node type
141      */
142     public String getType(Content node) {
143         try {
144             return node.getNodeTypeName();
145         }
146         catch (RepositoryException re) {
147             log.error(re.getMessage(), re);
148         }
149         return StringUtils.EMPTY;
150     }
151 
152     /**
153      * get path
154      * @return handle for the ciurrent object
155      */
156     public String getPath(Content node) {
157         return node.getHandle();
158     }
159 
160 }