1 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 ; 19 import java.util.Date ; 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 33 public class DefaultValueProvider implements ValueProvider { 34 35 38 private static final Logger log = Logger.getLogger(DefaultValueProvider.class); 39 40 43 private static ValueProvider thisInstance = new DefaultValueProvider(); 44 45 48 protected DefaultValueProvider() { 49 } 50 51 54 public static ValueProvider getInstance() { 55 return thisInstance; 56 } 57 58 61 public Object getValue(String name, Object obj) { 62 Object 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 e) { 81 value = node.getMetaData().getStringProperty(name); 82 if (StringUtils.isEmpty((String ) value)) { 83 value = null; 84 } 85 } 86 } 87 } 88 89 if (value == null) { 90 try { 92 value = PropertyUtils.getProperty(obj, name); 93 } 94 catch (NoSuchMethodException e1) { 95 try { 97 String 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 e2) { 103 value = StringUtils.EMPTY; 104 } 105 } 106 } 107 } 108 catch (Exception e) { 109 log.error("can't get value", e); 110 value = StringUtils.EMPTY; 111 } 112 113 if (value instanceof Calendar ) { 114 value = new Date (((Calendar ) value).getTimeInMillis()); 115 } 116 117 return value; 118 } 119 120 124 public String 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 138 public String getPath(Content node) { 139 return node.getHandle(); 140 } 141 142 } 143 | Popular Tags |