| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
package org.trails.descriptor; |
| 13 |
|
|
| 14 |
|
import java.lang.reflect.InvocationTargetException; |
| 15 |
|
import java.lang.reflect.Method; |
| 16 |
|
import java.util.List; |
| 17 |
|
|
| 18 |
|
import org.apache.commons.beanutils.BeanUtils; |
| 19 |
|
import org.apache.commons.logging.Log; |
| 20 |
|
import org.apache.commons.logging.LogFactory; |
| 21 |
|
import org.trails.TrailsRuntimeException; |
| 22 |
|
|
| 23 |
|
|
| 24 |
1 |
public class CollectionDescriptor extends TrailsPropertyDescriptor |
| 25 |
|
{ |
| 26 |
1 |
protected static final Log LOG = LogFactory.getLog(CollectionDescriptor.class); |
| 27 |
|
|
| 28 |
|
private Class elementType; |
| 29 |
|
|
| 30 |
55 |
private boolean childRelationship = false; |
| 31 |
|
|
| 32 |
55 |
private String inverseProperty = null; |
| 33 |
|
|
| 34 |
55 |
private boolean oneToMany = false; |
| 35 |
|
|
| 36 |
|
public CollectionDescriptor(Class beanType, IPropertyDescriptor descriptor) |
| 37 |
|
{ |
| 38 |
0 |
super(beanType, descriptor); |
| 39 |
0 |
} |
| 40 |
|
|
| 41 |
|
public CollectionDescriptor(Class beanType, CollectionDescriptor collectionDescriptor) |
| 42 |
|
{ |
| 43 |
1 |
super(beanType, collectionDescriptor.getBeanType()); |
| 44 |
1 |
this.copyFrom(collectionDescriptor); |
| 45 |
1 |
} |
| 46 |
|
|
| 47 |
|
public CollectionDescriptor(Class beanType, Class type) |
| 48 |
|
{ |
| 49 |
54 |
super(beanType, type); |
| 50 |
|
|
| 51 |
54 |
} |
| 52 |
|
|
| 53 |
|
public CollectionDescriptor(Class beanType, String name, Class type) |
| 54 |
|
{ |
| 55 |
42 |
this(beanType, type); |
| 56 |
42 |
this.setName(name); |
| 57 |
42 |
} |
| 58 |
|
|
| 59 |
|
|
| 60 |
|
|
| 61 |
|
|
| 62 |
|
public boolean isCollection() |
| 63 |
|
{ |
| 64 |
10 |
return true; |
| 65 |
|
} |
| 66 |
|
|
| 67 |
|
|
| 68 |
|
|
| 69 |
|
|
| 70 |
|
public Class getElementType() |
| 71 |
|
{ |
| 72 |
21 |
return elementType; |
| 73 |
|
} |
| 74 |
|
|
| 75 |
|
|
| 76 |
|
|
| 77 |
|
|
| 78 |
|
public void setElementType(Class elementType) |
| 79 |
|
{ |
| 80 |
30 |
this.elementType = elementType; |
| 81 |
30 |
} |
| 82 |
|
|
| 83 |
|
public String getInverseProperty() |
| 84 |
|
{ |
| 85 |
4 |
return inverseProperty; |
| 86 |
|
} |
| 87 |
|
|
| 88 |
|
public void setInverseProperty(String inverseProperty) |
| 89 |
|
{ |
| 90 |
3 |
this.inverseProperty = inverseProperty; |
| 91 |
3 |
} |
| 92 |
|
|
| 93 |
|
|
| 94 |
|
|
| 95 |
|
|
| 96 |
|
public boolean isOneToMany() |
| 97 |
|
{ |
| 98 |
3 |
return oneToMany; |
| 99 |
|
} |
| 100 |
|
|
| 101 |
|
public void setOneToMany(boolean oneToMany) |
| 102 |
|
{ |
| 103 |
3 |
this.oneToMany = oneToMany; |
| 104 |
3 |
} |
| 105 |
|
|
| 106 |
|
|
| 107 |
|
|
| 108 |
|
|
| 109 |
|
|
| 110 |
|
public boolean isChildRelationship() |
| 111 |
|
{ |
| 112 |
12 |
return childRelationship; |
| 113 |
|
} |
| 114 |
|
|
| 115 |
|
|
| 116 |
|
|
| 117 |
|
|
| 118 |
|
public void setChildRelationship(boolean childRelationship) |
| 119 |
|
{ |
| 120 |
12 |
this.childRelationship = childRelationship; |
| 121 |
12 |
if (this.childRelationship) |
| 122 |
|
{ |
| 123 |
12 |
setSearchable(false); |
| 124 |
|
} |
| 125 |
12 |
} |
| 126 |
|
|
| 127 |
|
public Object clone() |
| 128 |
|
{ |
| 129 |
1 |
return new CollectionDescriptor(getBeanType(), this); |
| 130 |
|
} |
| 131 |
|
|
| 132 |
|
public String findAddExpression() |
| 133 |
|
{ |
| 134 |
|
final String method = "add"; |
| 135 |
|
|
| 136 |
|
|
| 137 |
|
|
| 138 |
|
|
| 139 |
|
|
| 140 |
|
|
| 141 |
|
|
| 142 |
|
|
| 143 |
6 |
if (isChildRelationship() && List.class.isAssignableFrom(getType())) { |
| 144 |
2 |
return findExpression(method, getName() + ".contains(#member) ? " + getName() + ".size() : " + getName() + "." + method); |
| 145 |
|
} else { |
| 146 |
4 |
return findExpression(method); |
| 147 |
|
} |
| 148 |
|
|
| 149 |
|
} |
| 150 |
|
|
| 151 |
|
public String findRemoveExpression() |
| 152 |
|
{ |
| 153 |
3 |
return findExpression("remove"); |
| 154 |
|
} |
| 155 |
|
|
| 156 |
|
|
| 157 |
|
|
| 158 |
|
|
| 159 |
|
|
| 160 |
|
|
| 161 |
|
|
| 162 |
|
|
| 163 |
|
private String findExpression(String method, String defaultValue) |
| 164 |
|
{ |
| 165 |
9 |
Method addMethod = null; |
| 166 |
|
|
| 167 |
|
try |
| 168 |
|
{ |
| 169 |
9 |
addMethod = getBeanType().getMethod(method + getElementType().getSimpleName(), new Class[]{getElementType()}); |
| 170 |
3 |
} catch (NoSuchMethodException ex) |
| 171 |
|
{ |
| 172 |
|
|
| 173 |
3 |
return defaultValue; |
| 174 |
0 |
} catch (Exception e) |
| 175 |
|
{ |
| 176 |
0 |
throw new TrailsRuntimeException(e); |
| 177 |
|
} |
| 178 |
|
|
| 179 |
6 |
return addMethod.getName(); |
| 180 |
|
} |
| 181 |
|
|
| 182 |
|
String findExpression(String method) |
| 183 |
|
{ |
| 184 |
7 |
return findExpression(method, getName() + "." + method); |
| 185 |
|
} |
| 186 |
|
|
| 187 |
|
private void copyFrom(CollectionDescriptor collectionDescriptor) |
| 188 |
|
{ |
| 189 |
1 |
LOG.debug("Cloning CollectionDescriptor"); |
| 190 |
|
try |
| 191 |
|
{ |
| 192 |
1 |
BeanUtils.copyProperties(this, collectionDescriptor); |
| 193 |
0 |
} catch (IllegalAccessException e) |
| 194 |
|
{ |
| 195 |
0 |
LOG.error(e.getMessage()); |
| 196 |
0 |
} catch (InvocationTargetException e) |
| 197 |
|
{ |
| 198 |
0 |
LOG.error(e.getMessage()); |
| 199 |
|
} |
| 200 |
1 |
} |
| 201 |
|
} |