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 @ 44455

History | View | Annotate | Download (12.1 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.lang.reflect.Method;
27
import java.util.ArrayList;
28
import java.util.Collections;
29
import java.util.Comparator;
30
import java.util.List;
31
import java.util.Set;
32
import java.util.TreeSet;
33
import java.util.logging.Level;
34

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

    
47
public class PersistenceDevelTool {
48

    
49
    private static Logger logger = LoggerFactory.getLogger(PersistenceDevelTool.class);
50

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

    
57
    public class PersistenceInfo implements Comparable<PersistenceInfo> {
58

    
59
        PersistenceFactory factory;
60
        @SuppressWarnings("rawtypes")
61
        Class theClass;
62
        DynStruct definition;
63

    
64
        PersistenceInfo(PersistenceFactory factory,
65
                @SuppressWarnings("rawtypes") Class theClass,
66
                DynStruct definition) {
67
            this.factory = factory;
68
            this.theClass = theClass;
69
            this.definition = definition;
70
        }
71

    
72
        boolean hasSaveToState() {
73
            try {
74
                Method method = theClass.getMethod("saveToState", PersistentState.class);
75
                if (method == null) {
76
                    return false;
77
                }
78
            } catch (NoSuchMethodException | SecurityException ex) {
79
                return false;
80
            }
81
            return true;
82
        }
83

    
84
        boolean hasLoadFromState() {
85
            try {
86
                Method method = theClass.getMethod("loadFromState", PersistentState.class);
87
                if (method == null) {
88
                    return false;
89
                }
90
            } catch (NoSuchMethodException | SecurityException ex) {
91
                return false;
92
            }
93
            return true;
94
        }
95

    
96
        String getDefinitionName() {
97
            if (definition == null) {
98
                return "";
99
            }
100
            return definition.getFullName();
101
        }
102

    
103
        String getDefinitionDescription() {
104
            if (definition == null) {
105
                return "";
106
            }
107
            return definition.getDescription();
108
        }
109

    
110
        String getClassName() {
111
            if (theClass == null) {
112
                return "";
113
            }
114
            return theClass.getName();
115
        }
116

    
117
        String getFactoryName() {
118
            if (factory == null) {
119
                return "";
120
            }
121
            return factory.getClass().getName();
122
        }
123

    
124
        public int compareTo(PersistenceInfo other) {
125
            int r = this.factory.getClass().getName()
126
                    .compareTo(other.factory.getClass().getName());
127
            if (r == 0) {
128
                return this.theClass.getName().compareTo(
129
                        other.theClass.getName());
130
            }
131
            return r;
132
        }
133
    }
134

    
135
    @SuppressWarnings("rawtypes")
136
    public class ClassComparator implements Comparator {
137

    
138
        public int compare(Object o1, Object o2) {
139
            if (o1 == null || o2 == null || ((Class) o1).getName() == null) {
140
                logger.warn("Esto no deberia estar pasando.");
141
                return 0; // FIXME
142
            }
143
            return ((Class) o1).getName().compareTo(((Class) o2).getName());
144
        }
145

    
146
    }
147

    
148
    @SuppressWarnings({"unchecked", "rawtypes"})
149
    public String getPersistenceFactories() {
150
        int warningsCounter = 0;
151
        List<PersistenceInfo> classes = new ArrayList<PersistenceInfo>();
152
        Set<Class> referencedClasses = new TreeSet<Class>(new ClassComparator());
153

    
154
        PersistenceManager manager = ToolsLocator.getPersistenceManager();
155

    
156
        List<PersistenceFactory> factories = manager.getFactories();
157
        for (PersistenceFactory factory : factories) {
158
            List<Class> theClasses = factory.getManagedClasses();
159
            for (Class theClass : theClasses) {
160

    
161
                DynStruct definition = manager.getDefinition(theClass);
162
                List defs = factory.getDefinitions();
163
                if (definition == null && defs != null && !defs.isEmpty()) {
164
                    for (int i = 0; i < defs.size(); i++) {
165
                        definition = (DynStruct) defs.get(i);
166
                        classes.add(new PersistenceInfo(factory, theClass,
167
                                definition));
168
                    }
169
                } else {
170
                    classes.add(new PersistenceInfo(factory, theClass,
171
                            definition));
172
                }
173
            }
174
        }
175
        Collections.sort(classes);
176

    
177
        StringBuffer buffer = new StringBuffer();
178
        StringBuffer warnings = new StringBuffer();
179

    
180
        buffer.append("<html>\n");
181
        buffer.append("<body>\n");
182
        buffer.append("<h2>Supported persistent classes</h2>\n");
183
        buffer.append("<br>\n");
184

    
185
        buffer.append("<ol>\n");
186
        for (PersistenceInfo classInfo : classes) {
187
            StringBuffer classBuffer = new StringBuffer();
188
            boolean warning = false;
189
            classBuffer.append("  <li>\n    ");
190
            classBuffer.append("Class: <i>");
191
            classBuffer.append(classInfo.getClassName());
192
            classBuffer.append("</i><br>\n    Persistent: <i>");
193

    
194
            if (Persistent.class.isAssignableFrom(classInfo.theClass)) {
195
                classBuffer.append(" yes.");
196
                if (!classInfo.hasSaveToState()) {
197
                    classBuffer.append("<br>\n    <b>This class doesn't implement saveToState</b>");
198
                    warningsCounter++;
199
                    warning = true;
200
                }
201
                if (!classInfo.hasLoadFromState()) {
202
                    classBuffer.append("<br>\n    <b>This class doesn't implement loadFromState</b>");
203
                    warningsCounter++;
204
                    warning = true;
205
                }
206
            } else {
207
                classBuffer.append(" through a factory.");
208
            }
209

    
210
            classBuffer.append("<br>\n    Factory: <i>");
211
            classBuffer.append(classInfo.getFactoryName()).append(
212
                    "</i><br>\n    Definition name: <i>");
213
            classBuffer.append(classInfo.getDefinitionName()).append(
214
                    "</i><br>\n    Description: \n");
215
            classBuffer.append(classInfo.getDefinitionDescription()).append(
216
                    "<br>\n    ");
217
            DataTypesManager dataTypesManager = ToolsLocator
218
                    .getDataTypesManager();
219
            DynStruct definition = classInfo.definition;
220
            if (definition == null) {
221
                classBuffer.append("Definition for ")
222
                        .append(classInfo.getClassName())
223
                        .append(" is null.<br>\n    ");
224
                warningsCounter++;
225
                warning = true;
226
            } else {
227
                DynField[] fields = definition.getDynFields();
228
                for (int i = 0; i < fields.length; i++) {
229
                    DynField field = fields[i];
230
                    if (dataTypesManager.isContainer(field.getType())) {
231
                        if (field.getClassOfItems() == null) {
232
                            classBuffer
233
                                    .append("Field <b>")
234
                                    .append(field.getName())
235
                                    .append("</b> as container (")
236
                                    .append(field.getDataType().getName())
237
                                    .append("),  can't has class for value of items.<br>\n    ");
238
                            warningsCounter++;
239
                            warning = true;
240
                        } else {
241
                            classBuffer.append("Field ")
242
                                    .append(field.getName())
243
                                    .append(" as container (")
244
                                    .append(field.getDataType().getName())
245
                                    .append(") of '")
246
                                    .append(field.getClassOfItems().getName())
247
                                    .append("'.<br>\n    ");
248
                            referencedClasses.add(field.getClassOfItems());
249
                        }
250
                    } else if (dataTypesManager.isObject(field.getType())) {
251
                        if (field.getClassOfValue() == null) {
252
                            classBuffer
253
                                    .append("Field <b>")
254
                                    .append(field.getName())
255
                                    .append(" as object </b> can't has class of value.<br>\n    ");
256
                            warningsCounter++;
257
                            warning = true;
258
                        } else {
259
                            classBuffer.append("Field ")
260
                                    .append(field.getName()).append(" as object '")
261
                                    .append(field.getClassOfValue())
262
                                    .append("'.<br>\n    ");
263
                            referencedClasses.add(field.getClassOfValue());
264
                        }
265
                    } else {
266
                        classBuffer.append("Field ")
267
                                .append(field.getName()).append(" as '")
268
                                .append(field.getDataType().getName())
269
                                .append("'.<br>\n    ");
270
                    }
271
                }
272
            }
273
            classBuffer.append("<br>\n  </li>\n");
274
            buffer.append(classBuffer);
275
            if (warning) {
276
                warnings.append(classBuffer);
277
            }
278
        }
279
        buffer.append("</ol>\n");
280
        buffer.append("<br>\n");
281
        buffer.append("<br>\n");
282

    
283
        buffer.append("<h2>Persistent classes with problems</h2>\n");
284
        buffer.append("<ol>\n");
285
        buffer.append(warnings);
286
        buffer.append("</ol>\n");
287
        buffer.append("<br>\n<p>Total warnigs: ").append(warningsCounter)
288
                .append("</p>");
289

    
290
        buffer.append("<h2>Not persistents used classes</h2>\n");
291
        buffer.append("<ol>\n");
292
        for (Class theClass : referencedClasses) {
293
            // if( manager.canPersist(theClass) ) {
294
            // continue;
295
            // }
296
            if (Persistent.class.isAssignableFrom(theClass)) {
297
                continue;
298
            }
299
            if (manager.getFactories().get(theClass) != null) {
300
                continue;
301
            }
302
            buffer.append("  <li>\n");
303
            buffer.append("    <i>").append(theClass.getName())
304
                    .append("</i><br>\n");
305
            buffer.append("  </li>\n");
306
            warningsCounter++;
307
        }
308
        buffer.append("</ol>\n");
309

    
310
        buffer.append("</body>\n");
311
        buffer.append("</html>\n");
312

    
313
        return buffer.toString();
314
    }
315

    
316
}