| 1 |
64 |
package org.trails.descriptor; |
| 2 |
|
|
| 3 |
|
import java.util.ArrayList; |
| 4 |
|
import java.util.Collections; |
| 5 |
|
import java.util.Comparator; |
| 6 |
|
import java.util.HashMap; |
| 7 |
|
import java.util.List; |
| 8 |
|
import java.util.Map; |
| 9 |
|
|
| 10 |
|
import ognl.OgnlException; |
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
72 |
public class TrailsDescriptorService implements DescriptorService |
| 20 |
|
{ |
| 21 |
|
protected List<Class> types; |
| 22 |
36 |
protected Map<Class, IClassDescriptor> descriptors = new HashMap<Class, IClassDescriptor>(); |
| 23 |
36 |
private List<DescriptorDecorator> decorators = new ArrayList<DescriptorDecorator>(); |
| 24 |
|
private DescriptorFactory descriptorFactory; |
| 25 |
|
|
| 26 |
|
|
| 27 |
|
|
| 28 |
|
|
| 29 |
|
|
| 30 |
|
|
| 31 |
|
|
| 32 |
|
|
| 33 |
|
|
| 34 |
|
public void init() throws OgnlException |
| 35 |
|
{ |
| 36 |
24 |
descriptors.clear(); |
| 37 |
192 |
for (Class type : types) |
| 38 |
|
{ |
| 39 |
144 |
IClassDescriptor descriptor = getDescriptorFactory().buildClassDescriptor(type); |
| 40 |
144 |
descriptor = applyDecorators(descriptor); |
| 41 |
144 |
descriptors.put(type, descriptor); |
| 42 |
|
} |
| 43 |
|
|
| 44 |
192 |
for (IClassDescriptor iClassDescriptor : descriptors.values()) |
| 45 |
|
{ |
| 46 |
144 |
findChildren(iClassDescriptor); |
| 47 |
|
} |
| 48 |
24 |
} |
| 49 |
|
|
| 50 |
|
public List<IClassDescriptor> getAllDescriptors() |
| 51 |
|
{ |
| 52 |
4 |
List<IClassDescriptor> allDescriptors = new ArrayList<IClassDescriptor>(descriptors.values()); |
| 53 |
4 |
Collections.sort(allDescriptors, new Comparator<IClassDescriptor>() |
| 54 |
|
{ |
| 55 |
|
public int compare(IClassDescriptor o1, IClassDescriptor o2) |
| 56 |
|
{ |
| 57 |
60 |
return o1.getDisplayName().compareTo(o2.getDisplayName()); |
| 58 |
|
} |
| 59 |
|
}); |
| 60 |
4 |
return allDescriptors; |
| 61 |
|
} |
| 62 |
|
|
| 63 |
|
|
| 64 |
|
|
| 65 |
|
|
| 66 |
|
public IClassDescriptor getClassDescriptor(Class type) |
| 67 |
|
{ |
| 68 |
24 |
if (type.getName().contains("CGLIB")) |
| 69 |
|
{ |
| 70 |
0 |
return descriptors.get(type.getSuperclass()); |
| 71 |
|
} else |
| 72 |
|
{ |
| 73 |
24 |
return descriptors.get(type); |
| 74 |
|
} |
| 75 |
|
} |
| 76 |
|
|
| 77 |
|
protected void findChildren(IClassDescriptor iClassDescriptor) |
| 78 |
|
{ |
| 79 |
576 |
for (IPropertyDescriptor propertyDescriptor : iClassDescriptor.getPropertyDescriptors()) |
| 80 |
|
{ |
| 81 |
288 |
if (propertyDescriptor.isCollection()) |
| 82 |
|
{ |
| 83 |
0 |
if (((CollectionDescriptor) propertyDescriptor).isChildRelationship()) |
| 84 |
|
{ |
| 85 |
0 |
IClassDescriptor collectionClassDescriptor = getClassDescriptor(((CollectionDescriptor) propertyDescriptor).getElementType()); |
| 86 |
0 |
collectionClassDescriptor.setChild(true); |
| 87 |
|
} |
| 88 |
0 |
if (((CollectionDescriptor) propertyDescriptor).getInverseProperty() != null) |
| 89 |
|
{ |
| 90 |
0 |
iClassDescriptor.setHasCyclicRelationships(true); |
| 91 |
|
} |
| 92 |
|
} |
| 93 |
|
} |
| 94 |
144 |
} |
| 95 |
|
|
| 96 |
|
|
| 97 |
|
|
| 98 |
|
|
| 99 |
|
|
| 100 |
|
|
| 101 |
|
|
| 102 |
|
|
| 103 |
|
protected IClassDescriptor applyDecorators(IClassDescriptor descriptor) |
| 104 |
|
{ |
| 105 |
144 |
IClassDescriptor currDescriptor = descriptor; |
| 106 |
292 |
for (DescriptorDecorator decorator : getDecorators()) |
| 107 |
|
{ |
| 108 |
4 |
currDescriptor = decorator.decorate(currDescriptor); |
| 109 |
|
} |
| 110 |
144 |
return currDescriptor; |
| 111 |
|
} |
| 112 |
|
|
| 113 |
|
|
| 114 |
|
|
| 115 |
|
|
| 116 |
|
|
| 117 |
|
|
| 118 |
|
|
| 119 |
|
public List getTypes() |
| 120 |
|
{ |
| 121 |
0 |
return types; |
| 122 |
|
} |
| 123 |
|
|
| 124 |
|
|
| 125 |
|
|
| 126 |
|
|
| 127 |
|
public void setTypes(List<Class> types) |
| 128 |
|
{ |
| 129 |
20 |
this.types = types; |
| 130 |
20 |
} |
| 131 |
|
|
| 132 |
|
|
| 133 |
|
|
| 134 |
|
|
| 135 |
|
|
| 136 |
|
|
| 137 |
|
|
| 138 |
|
|
| 139 |
|
|
| 140 |
|
public List<DescriptorDecorator> getDecorators() |
| 141 |
|
{ |
| 142 |
148 |
return decorators; |
| 143 |
|
} |
| 144 |
|
|
| 145 |
|
public void setDecorators(List<DescriptorDecorator> decorators) |
| 146 |
|
{ |
| 147 |
0 |
this.decorators = decorators; |
| 148 |
0 |
} |
| 149 |
|
|
| 150 |
|
|
| 151 |
|
|
| 152 |
|
|
| 153 |
|
|
| 154 |
|
|
| 155 |
|
|
| 156 |
|
public DescriptorFactory getDescriptorFactory() |
| 157 |
|
{ |
| 158 |
148 |
return descriptorFactory; |
| 159 |
|
} |
| 160 |
|
|
| 161 |
|
public void setDescriptorFactory(DescriptorFactory descriptorFactory) |
| 162 |
|
{ |
| 163 |
20 |
this.descriptorFactory = descriptorFactory; |
| 164 |
20 |
} |
| 165 |
|
} |