KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > gui > controlx > list > DefaultValueProvider


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2006 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.cms.gui.controlx.list;
14
15 import info.magnolia.cms.core.Content;
16 import info.magnolia.cms.core.NodeData;
17
18 import java.util.Calendar JavaDoc;
19 import java.util.Date JavaDoc;
20
21 import javax.jcr.PropertyType;
22 import javax.jcr.RepositoryException;
23
24 import org.apache.commons.beanutils.MethodUtils;
25 import org.apache.commons.beanutils.PropertyUtils;
26 import org.apache.commons.lang.StringUtils;
27 import org.apache.log4j.Logger;
28
29
30 /**
31  * @author Sameer Charles $Id: DefaultValueProvider.java 6546 2006-10-04 09:53:05Z philipp $
32  */

33 public class DefaultValueProvider implements ValueProvider {
34
35     /**
36      * Logger
37      */

38     private static final Logger log = Logger.getLogger(DefaultValueProvider.class);
39
40     /**
41      * singleton
42      */

43     private static ValueProvider thisInstance = new DefaultValueProvider();
44
45     /**
46      * Not allowed to be instanciated outside the scope of this class
47      */

48     protected DefaultValueProvider() {
49     }
50
51     /**
52      * get instance
53      */

54     public static ValueProvider getInstance() {
55         return thisInstance;
56     }
57
58     /* (non-Javadoc)
59      * @see info.magnolia.cms.gui.controlx.list.util.ValueProvider#getValue(java.lang.String, java.lang.Object)
60      */

61     public Object JavaDoc getValue(String JavaDoc name, Object JavaDoc obj) {
62         Object JavaDoc value = null;
63         try {
64             if (obj instanceof Content) {
65                 Content node = (Content) obj;
66                 if (node.hasNodeData(name)) {
67                     NodeData nd = node.getNodeData(name);
68                     if (nd.getType() == PropertyType.DATE) {
69                         value = nd.getDate();
70                     }
71                     else {
72                         value = nd.getString();
73                     }
74                 }
75
76                 if (value == null) {
77                     try {
78                         value = PropertyUtils.getProperty(node.getMetaData(), name);
79                     }
80                     catch (NoSuchMethodException JavaDoc e) {
81                         value = node.getMetaData().getStringProperty(name);
82                         if (StringUtils.isEmpty((String JavaDoc) value)) {
83                             value = null;
84                         }
85                     }
86                 }
87             }
88
89             if (value == null) {
90                 // is this a property of the object
91
try {
92                     value = PropertyUtils.getProperty(obj, name);
93                 }
94                 catch (NoSuchMethodException JavaDoc e1) {
95                     // check if getter exist for this name
96
try {
97                         String JavaDoc methodName = "get"
98                             + StringUtils.substring(name, 0, 1).toUpperCase()
99                             + StringUtils.substring(name, 1);
100                         value = MethodUtils.invokeMethod(this, methodName, obj);
101                     }
102                     catch (NoSuchMethodException JavaDoc e2) {
103                         value = StringUtils.EMPTY;
104                     }
105                 }
106             }
107         }
108         catch (Exception JavaDoc e) {
109             log.error("can't get value", e);
110             value = StringUtils.EMPTY;
111         }
112
113         if (value instanceof Calendar JavaDoc) {
114             value = new Date JavaDoc(((Calendar JavaDoc) value).getTimeInMillis());
115         }
116
117         return value;
118     }
119
120     /**
121      * get node type
122      * @return node type
123      */

124     public String JavaDoc getType(Content node) {
125         try {
126             return node.getNodeTypeName();
127         }
128         catch (RepositoryException re) {
129             log.error(re.getMessage(), re);
130         }
131         return StringUtils.EMPTY;
132     }
133
134     /**
135      * get path
136      * @return handle for the ciurrent object
137      */

138     public String JavaDoc getPath(Content node) {
139         return node.getHandle();
140     }
141
142 }
143
Popular Tags