1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
54
55 public class DefaultValueProvider implements ValueProvider {
56 private static final Logger log = LoggerFactory.getLogger(DefaultValueProvider.class);
57
58
59
60
61 private static ValueProvider thisInstance = new DefaultValueProvider();
62
63
64
65
66 protected DefaultValueProvider() {
67 }
68
69
70
71
72 public static ValueProvider getInstance() {
73 return thisInstance;
74 }
75
76
77
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
109 try {
110 value = PropertyUtils.getProperty(obj, name);
111 }
112 catch (NoSuchMethodException e1) {
113
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
140
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
154
155
156 public String getPath(Content node) {
157 return node.getHandle();
158 }
159
160 }