Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.app / org.gvsig.app.mainplugin / src / main / java / org / gvsig / app / extension / develtools / PersistenceDevelTool.java @ 44190

History | View | Annotate | Download (8.85 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.app.extension.develtools;
25

    
26
import java.util.ArrayList;
27
import java.util.Collections;
28
import java.util.Comparator;
29
import java.util.List;
30
import java.util.Set;
31
import java.util.TreeSet;
32

    
33
import org.gvsig.tools.ToolsLocator;
34
import org.gvsig.tools.dataTypes.DataTypesManager;
35
import org.gvsig.tools.dynobject.DynField;
36
import org.gvsig.tools.dynobject.DynStruct;
37
import org.gvsig.tools.persistence.PersistenceFactory;
38
import org.gvsig.tools.persistence.PersistenceManager;
39
import org.gvsig.tools.persistence.Persistent;
40
import org.gvsig.tools.swing.api.windowmanager.WindowManager;
41
import org.slf4j.Logger;
42
import org.slf4j.LoggerFactory;
43

    
44
public class PersistenceDevelTool {
45
        private static Logger logger = LoggerFactory.getLogger(PersistenceDevelTool.class);
46

    
47
        public void showPersistenceFactories() {
48
                String html = this.getPersistenceFactories();
49
                InfoPanel.save2file("persistenceinfo", html);
50
                InfoPanel.showPanel("Persistence factories", WindowManager.MODE.WINDOW, html);
51
        }
52
        
53

    
54
        public class PersistenceInfo implements Comparable<PersistenceInfo> {
55
                PersistenceFactory factory;
56
                @SuppressWarnings("rawtypes")
57
                Class theClass;
58
                DynStruct definition;
59

    
60
                PersistenceInfo(PersistenceFactory factory,
61
                                @SuppressWarnings("rawtypes") Class theClass,
62
                                DynStruct definition) {
63
                        this.factory = factory;
64
                        this.theClass = theClass;
65
                        this.definition = definition;
66
                }
67

    
68
                String getDefinitionName() {
69
                        if (definition == null) {
70
                                return "";
71
                        }
72
                        return definition.getFullName();
73
                }
74

    
75
                String getDefinitionDescription() {
76
                        if (definition == null) {
77
                                return "";
78
                        }
79
                        return definition.getDescription();
80
                }
81

    
82
                String getClassName() {
83
                        if (theClass == null) {
84
                                return "";
85
                        }
86
                        return theClass.getName();
87
                }
88

    
89
                String getFactoryName() {
90
                        if (factory == null) {
91
                                return "";
92
                        }
93
                        return factory.getClass().getName();
94
                }
95

    
96
                public int compareTo(PersistenceInfo other) {
97
                        int r = this.factory.getClass().getName()
98
                                        .compareTo(other.factory.getClass().getName());
99
                        if (r == 0) {
100
                                return this.theClass.getName().compareTo(
101
                                                other.theClass.getName());
102
                        }
103
                        return r;
104
                }
105
        }
106

    
107
        @SuppressWarnings("rawtypes")
108
        public class ClassComparator implements Comparator {
109

    
110
                public int compare(Object o1, Object o2) {
111
                        if (o1 == null || o2 == null || ((Class) o1).getName() == null) {
112
                                logger.warn("Esto no deberia estar pasando.");
113
                                return 0; // FIXME
114
                        }
115
                        return ((Class) o1).getName().compareTo(((Class) o2).getName());
116
                }
117

    
118
        }
119

    
120
        @SuppressWarnings({ "unchecked", "rawtypes" })
121
        public String getPersistenceFactories() {
122
                int warningsCounter = 0;
123
                List<PersistenceInfo> classes = new ArrayList<PersistenceInfo>();
124
                Set<Class> referencedClasses = new TreeSet<Class>(new ClassComparator());
125

    
126
                PersistenceManager manager = ToolsLocator.getPersistenceManager();
127

    
128
                List<PersistenceFactory> factories = manager.getFactories();
129
                for (PersistenceFactory factory : factories) {
130
                        List<Class> theClasses = factory.getManagedClasses();
131
                        for (Class theClass : theClasses) {
132

    
133
                                DynStruct definition = manager.getDefinition(theClass);
134
                                List defs = factory.getDefinitions();
135
                                if (definition == null && defs != null && !defs.isEmpty()) {
136
                                        for (int i = 0; i < defs.size(); i++) {
137
                                                definition = (DynStruct) defs.get(i);
138
                                                classes.add(new PersistenceInfo(factory, theClass,
139
                                                                definition));
140
                                        }
141
                                } else {
142
                                        classes.add(new PersistenceInfo(factory, theClass,
143
                                                        definition));
144
                                }
145
                        }
146
                }
147
                Collections.sort(classes);
148

    
149
                StringBuffer buffer = new StringBuffer();
150
                StringBuffer warnings = new StringBuffer();
151

    
152
                buffer.append("<html>\n");
153
                buffer.append("<body>\n");
154
                buffer.append("<h2>Supported persistent classes</h2>\n");
155
                buffer.append("<br>\n");
156

    
157
                buffer.append("<ol>\n");
158
                for (PersistenceInfo classInfo : classes) {
159
                        StringBuffer classBuffer = new StringBuffer();
160
                        boolean warning = false;
161
                        classBuffer.append("  <li>\n    ");
162
                        classBuffer.append("Class: <i>");
163
                        classBuffer.append(classInfo.getClassName());
164
                        classBuffer.append("</i><br>\n    Persistent: <i>");
165
                        if (Persistent.class.isAssignableFrom(classInfo.theClass)) {
166
                                classBuffer.append(" yes.");
167
                        } else {
168
                                classBuffer.append(" through a factory.");
169
                        }
170
                        classBuffer.append("<br>\n    Factory: <i>");
171
                        classBuffer.append(classInfo.getFactoryName()).append(
172
                                        "</i><br>\n    Definition name: <i>");
173
                        classBuffer.append(classInfo.getDefinitionName()).append(
174
                                        "</i><br>\n    Description: \n");
175
                        classBuffer.append(classInfo.getDefinitionDescription()).append(
176
                                        "<br>\n    ");
177
                        DataTypesManager dataTypesManager = ToolsLocator
178
                                        .getDataTypesManager();
179
                        DynStruct definition = classInfo.definition;
180
                        if (definition == null) {
181
                                classBuffer.append("Definition for ")
182
                                                .append(classInfo.getClassName())
183
                                                .append(" is null.<br>\n    ");
184
                                warningsCounter++;
185
                                warning = true;
186
                        } else {
187
                                DynField[] fields = definition.getDynFields();
188
                                for (int i = 0; i < fields.length; i++) {
189
                                        DynField field = fields[i];
190
                                        if (dataTypesManager.isContainer(field.getType())) {
191
                                                if (field.getClassOfItems() == null) {
192
                                                        classBuffer
193
                                                                        .append("Field <b>")
194
                                                                        .append(field.getName())
195
                                                                        .append("</b> as container (")
196
                                                                        .append(field.getDataType().getName())
197
                                                                        .append("),  can't has class for value of items.<br>\n    ");
198
                                                        warningsCounter++;
199
                                                        warning = true;
200
                                                } else {
201
                                                        classBuffer.append("Field ")
202
                                                                        .append(field.getName())
203
                                                                        .append(" as container (")
204
                                                                        .append(field.getDataType().getName())
205
                                                                        .append(") of '")
206
                                                                        .append(field.getClassOfItems().getName())
207
                                                                        .append("'.<br>\n    ");
208
                                                        referencedClasses.add(field.getClassOfItems());
209
                                                }
210
                                        } else if (dataTypesManager.isObject(field.getType())) {
211
                                                if (field.getClassOfValue() == null) {
212
                                                        classBuffer
213
                                                                        .append("Field <b>")
214
                                                                        .append(field.getName())
215
                                                                        .append(" as object </b> can't has class of value.<br>\n    ");
216
                                                        warningsCounter++;
217
                                                        warning = true;
218
                                                } else {
219
                                                        classBuffer.append("Field ")
220
                                                                        .append(field.getName()).append(" as object '")
221
                                                                        .append(field.getClassOfValue())
222
                                                                        .append("'.<br>\n    ");
223
                                                        referencedClasses.add(field.getClassOfValue());
224
                                                }
225
                                        } else {
226
                                                classBuffer.append("Field ")
227
                                                    .append(field.getName()).append(" as '")
228
                                                    .append(field.getDataType().getName())
229
                                                    .append("'.<br>\n    ");
230
                                        }
231
                                }
232
                        }
233
                        classBuffer.append("<br>\n  </li>\n");
234
                        buffer.append(classBuffer);
235
                        if (warning) {
236
                                warnings.append(classBuffer);
237
                        }
238
                }
239
                buffer.append("</ol>\n");
240
                buffer.append("<br>\n");
241
                buffer.append("<br>\n");
242

    
243
                buffer.append("<h2>Persistent classes with problems</h2>\n");
244
                buffer.append("<ol>\n");
245
                buffer.append(warnings);
246
                buffer.append("</ol>\n");
247
                buffer.append("<br>\n<p>Total warnigs: ").append(warningsCounter)
248
                                .append("</p>");
249

    
250
                buffer.append("<h2>Not persistents used classes</h2>\n");
251
                buffer.append("<ol>\n");
252
                for (Class theClass : referencedClasses) {
253
                        // if( manager.canPersist(theClass) ) {
254
                        // continue;
255
                        // }
256
                        if (Persistent.class.isAssignableFrom(theClass)) {
257
                                continue;
258
                        }
259
                        if (manager.getFactories().get(theClass) != null) {
260
                                continue;
261
                        }
262
                        buffer.append("  <li>\n");
263
                        buffer.append("    <i>").append(theClass.getName())
264
                                        .append("</i><br>\n");
265
                        buffer.append("  </li>\n");
266
                        warningsCounter++;
267
                }
268
                buffer.append("</ol>\n");
269

    
270
                buffer.append("</body>\n");
271
                buffer.append("</html>\n");
272

    
273
                return buffer.toString();
274
        }
275

    
276

    
277
}