Revision 47336 trunk/org.gvsig.desktop/org.gvsig.desktop.plugin/org.gvsig.xml2db/org.gvsig.xml2db.lib/org.gvsig.xml2db.lib.impl/src/main/java/org/gvsig/xml2db/lib/impl/CopyXML2dbImpl.java

View differences:

CopyXML2dbImpl.java
27 27
import org.gvsig.fmap.dal.store.jdbc2.JDBCServerExplorer;
28 28
import org.gvsig.tools.dispose.DisposeUtils;
29 29
import org.gvsig.tools.task.SimpleTaskStatus;
30
import org.gvsig.xml2db.lib.impl.xmlinfo.XMLTableInfoImpl;
30 31
import org.slf4j.Logger;
31 32
import org.slf4j.LoggerFactory;
32 33
import org.xml.sax.Attributes;
......
52 53
        private EditableFeature current_row;
53 54
        private int pkcounter;
54 55
        private EditableFeature last_row;
56
        private FeatureType featureType;
55 57
        
56 58
        
57 59
        private TableInfo(Map<String,TableInfo> tables, JDBCStoreParameters openStoreParams) {
......
59 61
            this.openStoreParams = openStoreParams;
60 62
            this.tableName = openStoreParams.getTable();
61 63
            this.pkcounter = 1;
64

  
65
            FeatureStore theStore = null;
66
            try {
67
                DataManager dataManager = DALLocator.getDataManager();
68
                theStore = (FeatureStore) dataManager.openStore(
69
                        this.openStoreParams.getProviderName(),
70
                        openStoreParams
71
                );
72
                this.featureType = theStore.getDefaultFeatureTypeQuietly();
73
            } catch (Throwable t) {
74
                throw new RuntimeException("Can't get path from store '" + this.getTableName() + "'.", t);
75
            } finally {
76
                DisposeUtils.disposeQuietly(theStore);
77
            }
78

  
62 79
        }
63 80
        
64 81
        public void close() {
65 82
            try {
66 83
                this.insert();
67 84
                if( this.store!=null ) {
68
                    System.out.println("###: close("+this.tableName+")");
85
//                    System.out.println("###: close("+this.tableName+")");
69 86
                    this.store.finishEditing();
70 87
                    DisposeUtils.dispose(this.store);
71 88
                }
......
81 98
        public FeatureStore getStore() {
82 99
            if( this.store == null ) {
83 100
                try {
84
                    System.out.println("###: create store("+this.tableName+")");
101
//                    System.out.println("###: create store("+this.tableName+")");
85 102
                    DataManager dataManager = DALLocator.getDataManager();                
86 103
                    this.store = (FeatureStore) dataManager.openStore(
87 104
                            this.openStoreParams.getProviderName(), 
......
126 143
            if( this.current_row != null ) {
127 144
//                System.out.println("###: set("+this.tableName+", "+name+", "+Objects.toString(value)+")");
128 145
                try {
129
                    if( StringUtils.equalsIgnoreCase("CONDICION_METEO", name) ) {
130
                        System.out.println("CONDICION_METEO...");
146
                    if( this.current_row.getType().get(name) == null ) {
147
                        LOGGER.info("Not exists attribute '"+name+"' in "+ this.getStore().getName());
148
                        throw new IllegalArgumentException("Not exists attribute '"+name+"' in "+ this.getStore().getName());
131 149
                    }
132 150
                    this.current_row.set(name, value);
133 151
                } catch(RuntimeException ex) {
......
170 188
            FeatureAttributeDescriptor[] attrdescs = this.last_row.getType().getPrimaryKey();
171 189
            return this.last_row.get(attrdescs[0].getName());
172 190
        }
191
        
192
        public String getPath() {
193
            return this.featureType.getTags().getString("xml2db.path", null);
194
        }
195

  
196
        public boolean containsAttr(String attrname) {
197
            return this.featureType.get(attrname) != null;
198
        }
173 199
    }
174 200
    
175 201
    public void copyData(InputSource is, JDBCServerExplorerParameters dbparams, long countLines, SimpleTaskStatus taskStatus) {
......
186 212
        MutableInt line = new MutableInt(0);
187 213
        MutableInt column = new MutableInt(0);
188 214
        try {
189
            taskStatus.message("Coping...");
215
            taskStatus.message("Copying...");
190 216
            taskStatus.setRangeOfValues(0, countLines);
191 217
            
192 218
            SAXParserFactory spf = SAXParserFactory.newInstance();
......
219 245
                    try {
220 246
                        path.add(localName);
221 247
                        String path_s = StringUtils.join(path, "/");
222
                        String tableName1 = FilenameUtils.getBaseName(path_s).toLowerCase();
223
                        TableInfo table1 = tables.get(tableName1);
248
                        TableInfo table1 = getTableByPath(tables, path_s);
224 249
                        if( table1 != null ) {
225 250
                            table1.createRow();
226 251
                            for (int i = 0; i < attributes.getLength(); i++) {
......
243 268
                    taskStatus.setCurValue(line.intValue());
244 269
                    try {
245 270
                        String path_s = StringUtils.join(path, "/");
246
                        String tableName1 = FilenameUtils.getBaseName(path_s).toLowerCase();
247
                        TableInfo table1 = tables.get(tableName1);
248
                        if( table1 != null ) {
271
                        TableInfo table1 = getTableByPath(tables, path_s);
272
                        if(table1 != null) {
273
                            if (this.valueBuilder != null) {
274
                                String value = StringUtils.trimToNull(this.valueBuilder.toString());
275
                                if (value != null) {
276
                                    table1.set(localName + "$v", value);
277
                                }
278
                            }
279
                            
249 280
                            table1.insert();
250 281
                        } else if( path.size()>1 ) {
251 282
                            List<String> parentpath = path.subList(0, path.size()-1);
252 283
                            String parentpath_s = StringUtils.join(parentpath, "/");
253 284
                            String attrname = FilenameUtils.getBaseName(path_s);
254
//                            if( StringUtils.equalsIgnoreCase("CONDICION_FIRME", attrname) ) {
255
//                                System.out.println("CONDICION_FIRME...");
256
//                            }
257
                            String tableName2 = FilenameUtils.getBaseName(parentpath_s).toLowerCase();
258
                            TableInfo table2 = tables.get(tableName2);
285

  
286
                            TableInfo table2 = getTableByPath(tables, parentpath_s);
259 287
                            if( table2!=null ) {
260 288
                                if( this.valueBuilder!=null ) {
261 289
                                    String value = StringUtils.trimToNull(this.valueBuilder.toString());  
......
290 318
            throw new RuntimeException("Can't copy data (line "+line+", column "+column+").", ex);
291 319
        }
292 320
    }
321

  
322
    private TableInfo getTableByPath(Map<String,TableInfo> tables, String path) {
323
        for (TableInfo table : tables.values()) {
324
            if(StringUtils.equalsIgnoreCase(path, table.getPath())){
325
                return table;
326
            }
327
        }
328
        return null;
329
    }
293 330
    
294 331
    public void copyData(File xml, Charset charset, JDBCServerExplorerParameters dbparams, SimpleTaskStatus taskStatus) {
295 332
        long count = Xml2dbCommons.countLines(xml, charset, taskStatus);

Also available in: Unified diff