root / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / app / extension / develtools / PersistenceDevelTool.java @ 38608
History | View | Annotate | Download (7.12 KB)
1 | 38564 | jjdelcerro | package org.gvsig.app.extension.develtools; |
---|---|---|---|
2 | |||
3 | import java.util.ArrayList; |
||
4 | import java.util.Collections; |
||
5 | import java.util.Comparator; |
||
6 | import java.util.List; |
||
7 | import java.util.Set; |
||
8 | import java.util.TreeSet; |
||
9 | |||
10 | import org.gvsig.tools.ToolsLocator; |
||
11 | import org.gvsig.tools.dataTypes.DataTypesManager; |
||
12 | import org.gvsig.tools.dynobject.DynField; |
||
13 | import org.gvsig.tools.dynobject.DynStruct; |
||
14 | import org.gvsig.tools.persistence.PersistenceFactory; |
||
15 | import org.gvsig.tools.persistence.PersistenceManager; |
||
16 | import org.gvsig.tools.persistence.Persistent; |
||
17 | import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
||
18 | import org.slf4j.Logger; |
||
19 | import org.slf4j.LoggerFactory; |
||
20 | |||
21 | public class PersistenceDevelTool { |
||
22 | private static Logger logger = LoggerFactory.getLogger(PersistenceDevelTool.class); |
||
23 | |||
24 | public void showPersistenceFactories() { |
||
25 | String html = this.getPersistenceFactories(); |
||
26 | InfoPanel.save2file("persistenceinfo", html);
|
||
27 | InfoPanel.showPanel("Persistence factories", WindowManager.MODE.WINDOW, html);
|
||
28 | } |
||
29 | |||
30 | |||
31 | public class PersistenceInfo implements Comparable<PersistenceInfo> { |
||
32 | PersistenceFactory factory; |
||
33 | @SuppressWarnings("rawtypes") |
||
34 | Class theClass;
|
||
35 | DynStruct definition; |
||
36 | |||
37 | PersistenceInfo(PersistenceFactory factory, |
||
38 | @SuppressWarnings("rawtypes") Class theClass, |
||
39 | DynStruct definition) { |
||
40 | this.factory = factory;
|
||
41 | this.theClass = theClass;
|
||
42 | this.definition = definition;
|
||
43 | } |
||
44 | |||
45 | String getDefinitionName() {
|
||
46 | if (definition == null) { |
||
47 | return ""; |
||
48 | } |
||
49 | return definition.getFullName();
|
||
50 | } |
||
51 | |||
52 | String getDefinitionDescription() {
|
||
53 | if (definition == null) { |
||
54 | return ""; |
||
55 | } |
||
56 | return definition.getDescription();
|
||
57 | } |
||
58 | |||
59 | String getClassName() {
|
||
60 | if (theClass == null) { |
||
61 | return ""; |
||
62 | } |
||
63 | return theClass.getName();
|
||
64 | } |
||
65 | |||
66 | String getFactoryName() {
|
||
67 | if (factory == null) { |
||
68 | return ""; |
||
69 | } |
||
70 | return factory.getClass().getName();
|
||
71 | } |
||
72 | |||
73 | public int compareTo(PersistenceInfo other) { |
||
74 | int r = this.factory.getClass().getName() |
||
75 | .compareTo(other.factory.getClass().getName()); |
||
76 | if (r == 0) { |
||
77 | return this.theClass.getName().compareTo( |
||
78 | other.theClass.getName()); |
||
79 | } |
||
80 | return r;
|
||
81 | } |
||
82 | } |
||
83 | |||
84 | @SuppressWarnings("rawtypes") |
||
85 | public class ClassComparator implements Comparator { |
||
86 | |||
87 | public int compare(Object o1, Object o2) { |
||
88 | if (o1 == null || o2 == null || ((Class) o1).getName() == null) { |
||
89 | logger.warn("Esto no deberia estar pasando.");
|
||
90 | return 0; // FIXME |
||
91 | } |
||
92 | return ((Class) o1).getName().compareTo(((Class) o2).getName()); |
||
93 | } |
||
94 | |||
95 | } |
||
96 | |||
97 | @SuppressWarnings({ "unchecked", "rawtypes" }) |
||
98 | public String getPersistenceFactories() { |
||
99 | int warningsCounter = 0; |
||
100 | List<PersistenceInfo> classes = new ArrayList<PersistenceInfo>(); |
||
101 | Set<Class> referencedClasses = new TreeSet<Class>(new ClassComparator()); |
||
102 | |||
103 | PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
||
104 | |||
105 | List<PersistenceFactory> factories = manager.getFactories();
|
||
106 | for (PersistenceFactory factory : factories) {
|
||
107 | List<Class> theClasses = factory.getManagedClasses(); |
||
108 | for (Class theClass : theClasses) { |
||
109 | |||
110 | DynStruct definition = manager.getDefinition(theClass); |
||
111 | List defs = factory.getDefinitions();
|
||
112 | if (definition == null && defs != null && !defs.isEmpty()) { |
||
113 | for (int i = 0; i < defs.size(); i++) { |
||
114 | definition = (DynStruct) defs.get(i); |
||
115 | classes.add(new PersistenceInfo(factory, theClass,
|
||
116 | definition)); |
||
117 | } |
||
118 | } else {
|
||
119 | classes.add(new PersistenceInfo(factory, theClass,
|
||
120 | definition)); |
||
121 | } |
||
122 | } |
||
123 | } |
||
124 | Collections.sort(classes);
|
||
125 | |||
126 | StringBuffer buffer = new StringBuffer(); |
||
127 | StringBuffer warnings = new StringBuffer(); |
||
128 | |||
129 | buffer.append("<html>\n");
|
||
130 | buffer.append("<body>\n");
|
||
131 | buffer.append("<h2>Supported persistent classes</h2>\n");
|
||
132 | buffer.append("<br>\n");
|
||
133 | |||
134 | buffer.append("<ol>\n");
|
||
135 | for (PersistenceInfo classInfo : classes) {
|
||
136 | StringBuffer classBuffer = new StringBuffer(); |
||
137 | boolean warning = false; |
||
138 | classBuffer.append(" <li>\n ");
|
||
139 | classBuffer.append("Class: <i>");
|
||
140 | classBuffer.append(classInfo.getClassName()); |
||
141 | classBuffer.append("</i><br>\n Persistent: <i>");
|
||
142 | if (Persistent.class.isAssignableFrom(classInfo.theClass)) {
|
||
143 | classBuffer.append(" yes.");
|
||
144 | } else {
|
||
145 | classBuffer.append(" through a factory.");
|
||
146 | } |
||
147 | classBuffer.append("<br>\n Factory: <i>");
|
||
148 | classBuffer.append(classInfo.getFactoryName()).append( |
||
149 | "</i><br>\n Definition name: <i>");
|
||
150 | classBuffer.append(classInfo.getDefinitionName()).append( |
||
151 | "</i><br>\n Description: \n");
|
||
152 | classBuffer.append(classInfo.getDefinitionDescription()).append( |
||
153 | "<br>\n ");
|
||
154 | DataTypesManager dataTypesManager = ToolsLocator |
||
155 | .getDataTypesManager(); |
||
156 | DynStruct definition = classInfo.definition; |
||
157 | if (definition == null) { |
||
158 | classBuffer.append("Definition for ")
|
||
159 | .append(classInfo.getClassName()) |
||
160 | .append(" is null.<br>\n ");
|
||
161 | warningsCounter++; |
||
162 | warning = true;
|
||
163 | } else {
|
||
164 | DynField[] fields = definition.getDynFields();
|
||
165 | for (int i = 0; i < fields.length; i++) { |
||
166 | DynField field = fields[i]; |
||
167 | if (dataTypesManager.isContainer(field.getType())) {
|
||
168 | if (field.getClassOfItems() == null) { |
||
169 | classBuffer |
||
170 | .append("Field <b>")
|
||
171 | .append(field.getName()) |
||
172 | .append("</b>, container, can't has class for value of items.<br>\n ");
|
||
173 | warningsCounter++; |
||
174 | warning = true;
|
||
175 | } else {
|
||
176 | classBuffer.append("Field ")
|
||
177 | .append(field.getName()) |
||
178 | .append(", container of '")
|
||
179 | .append(field.getClassOfItems().getName()) |
||
180 | .append("'.<br>\n ");
|
||
181 | referencedClasses.add(field.getClassOfItems()); |
||
182 | } |
||
183 | } else if (dataTypesManager.isObject(field.getType())) { |
||
184 | if (field.getClassOfValue() == null) { |
||
185 | classBuffer |
||
186 | .append("Field <b>")
|
||
187 | .append(field.getName()) |
||
188 | .append("</b> can't has class of value.<br>\n ");
|
||
189 | warningsCounter++; |
||
190 | warning = true;
|
||
191 | } else {
|
||
192 | classBuffer.append("Field ")
|
||
193 | .append(field.getName()).append(" of '")
|
||
194 | .append(field.getClassOfValue()) |
||
195 | .append("'.<br>\n ");
|
||
196 | referencedClasses.add(field.getClassOfValue()); |
||
197 | } |
||
198 | } |
||
199 | } |
||
200 | } |
||
201 | classBuffer.append("<br>\n </li>\n");
|
||
202 | buffer.append(classBuffer); |
||
203 | if (warning) {
|
||
204 | warnings.append(classBuffer); |
||
205 | } |
||
206 | } |
||
207 | buffer.append("</ol>\n");
|
||
208 | buffer.append("<br>\n");
|
||
209 | buffer.append("<br>\n");
|
||
210 | |||
211 | buffer.append("<h2>Persistent classes with problems</h2>\n");
|
||
212 | buffer.append("<ol>\n");
|
||
213 | buffer.append(warnings); |
||
214 | buffer.append("</ol>\n");
|
||
215 | buffer.append("<br>\n<p>Total warnigs: ").append(warningsCounter)
|
||
216 | .append("</p>");
|
||
217 | |||
218 | buffer.append("<h2>Not persistents used classes</h2>\n");
|
||
219 | buffer.append("<ol>\n");
|
||
220 | for (Class theClass : referencedClasses) { |
||
221 | // if( manager.canPersist(theClass) ) {
|
||
222 | // continue;
|
||
223 | // }
|
||
224 | if (Persistent.class.isAssignableFrom(theClass)) {
|
||
225 | continue;
|
||
226 | } |
||
227 | if (manager.getFactories().get(theClass) != null) { |
||
228 | continue;
|
||
229 | } |
||
230 | buffer.append(" <li>\n");
|
||
231 | buffer.append(" <i>").append(theClass.getName())
|
||
232 | .append("</i><br>\n");
|
||
233 | buffer.append(" </li>\n");
|
||
234 | warningsCounter++; |
||
235 | } |
||
236 | buffer.append("</ol>\n");
|
||
237 | |||
238 | buffer.append("</body>\n");
|
||
239 | buffer.append("</html>\n");
|
||
240 | |||
241 | return buffer.toString();
|
||
242 | } |
||
243 | |||
244 | |||
245 | } |