View Javadoc
1   /**
2    * This file Copyright (c) 2003-2014 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$
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      @Override
80      public Object getValue(String name, Object obj) {
81          Object value = null;
82          try {
83              if (obj instanceof Content) {
84                  Content node = (Content) obj;
85                  if (node.hasNodeData(name)) {
86                      NodeData nd = node.getNodeData(name);
87                      if (nd.getType() == PropertyType.DATE) {
88                          value = nd.getDate();
89                      }
90                      else {
91                          value = nd.getString();
92                      }
93                  }
94  
95                  if (value == null) {
96                      try {
97                          value = PropertyUtils.getProperty(node.getMetaData(), name);
98                      }
99                      catch (NoSuchMethodException e) {
100                         value = node.getMetaData().getStringProperty(name);
101                         if (StringUtils.isEmpty((String) value)) {
102                             value = null;
103                         }
104                     }
105                 }
106             }
107 
108             if (value == null) {
109                 // is this a property of the object
110                 try {
111                     value = PropertyUtils.getProperty(obj, name);
112                 }
113                 catch (NoSuchMethodException e1) {
114                     // check if getter exist for this name
115                     try {
116                         String methodName = "get"
117                             + StringUtils.substring(name, 0, 1).toUpperCase()
118                             + StringUtils.substring(name, 1);
119                         value = MethodUtils.invokeMethod(this, methodName, obj);
120                     }
121                     catch (NoSuchMethodException e2) {
122                         value = StringUtils.EMPTY;
123                     }
124                 }
125             }
126         }
127         catch (Exception e) {
128             log.error("can't get value", e);
129             value = StringUtils.EMPTY;
130         }
131 
132         if (value instanceof Calendar) {
133             value = new Date(((Calendar) value).getTimeInMillis());
134         }
135 
136         return value;
137     }
138 
139     /**
140      * get node type
141      * @return node type
142      */
143     public String getType(Content node) {
144         try {
145             return node.getNodeTypeName();
146         }
147         catch (RepositoryException re) {
148             log.error(re.getMessage(), re);
149         }
150         return StringUtils.EMPTY;
151     }
152 
153     /**
154      * get path
155      * @return handle for the ciurrent object
156      */
157     public String getPath(Content node) {
158         return node.getHandle();
159     }
160 
161 }