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 @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
110 try {
111 value = PropertyUtils.getProperty(obj, name);
112 }
113 catch (NoSuchMethodException e1) {
114
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
141
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
155
156
157 public String getPath(Content node) {
158 return node.getHandle();
159 }
160
161 }