This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 152486
Collapse All | Expand All

(-)a/db.core/src/org/netbeans/modules/db/sql/execute/ui/SQLHistoryPanel.java (-3 / +7 lines)
Lines 447-455 Link Here
447
    }
447
    }
448
    
448
    
449
    private void handleSQLHistoryException() {
449
    private void handleSQLHistoryException() {
450
        inputWarningLabel.setText(NbBundle.getMessage(SQLHistoryPanel.class, "LBL_ErrorParsingSQLHistory"));
451
        LOGGER.log(Level.WARNING, NbBundle.getMessage(SQLHistoryPanel.class, "LBL_ErrorParsingSQLHistory"));
450
        LOGGER.log(Level.WARNING, NbBundle.getMessage(SQLHistoryPanel.class, "LBL_ErrorParsingSQLHistory"));
452
        SQLHistoryPersistenceManager.getInstance().removeHistoryFile(historyRoot);
451
        List<SQLHistory> sqlHistoryList = SQLHistoryPersistenceManager.getInstance().retrieve();
452
        view.setCurrentSQLHistoryList(sqlHistoryList);
453
        ((HistoryTableModel) sqlHistoryTable.getModel()).refreshTable(null, sqlHistoryList);
454
        view.updateConnectionUrl();
453
    }
455
    }
454
456
455
    // Variables declaration - do not modify//GEN-BEGIN:variables
457
    // Variables declaration - do not modify//GEN-BEGIN:variables
Lines 482-488 Link Here
482
                this.sqlHistoryList = model.getSQLHistoryList();
484
                this.sqlHistoryList = model.getSQLHistoryList();
483
                this.currentSQLHistoryList = model.getSQLHistoryList();
485
                this.currentSQLHistoryList = model.getSQLHistoryList();
484
            } catch (SQLHistoryException ex) {
486
            } catch (SQLHistoryException ex) {
485
                // ignore for now since this exception will be caught later
487
                LOGGER.log(Level.WARNING, NbBundle.getMessage(SQLHistoryPanel.class, "LBL_ErrorParsingSQLHistory"), ex);
488
                sqlHistoryList = SQLHistoryPersistenceManager.getInstance().retrieve();
489
                setCurrentSQLHistoryList(sqlHistoryList);
486
            }
490
            }
487
        }
491
        }
488
492
(-)a/db.core/src/org/netbeans/modules/db/sql/history/SQLHistoryPersistenceManager.java (-35 / +49 lines)
Lines 49-56 Link Here
49
import java.text.DateFormat;
49
import java.text.DateFormat;
50
import java.text.ParseException;
50
import java.text.ParseException;
51
import java.util.ArrayList;
51
import java.util.ArrayList;
52
import java.util.Calendar;
52
import java.util.Date;
53
import java.util.Date;
53
import java.util.List;
54
import java.util.List;
55
import java.util.Locale;
54
import org.openide.util.Exceptions;
56
import org.openide.util.Exceptions;
55
import org.xml.sax.Attributes;
57
import org.xml.sax.Attributes;
56
import java.util.logging.Level;
58
import java.util.logging.Level;
Lines 95-101 Link Here
95
    private static Document document;
97
    private static Document document;
96
    private List<SQLHistory> sqlHistoryList;
98
    private List<SQLHistory> sqlHistoryList;
97
    private int numElemsToRemove = 0;
99
    private int numElemsToRemove = 0;
98
    
100
99
    private SQLHistoryPersistenceManager() {
101
    private SQLHistoryPersistenceManager() {
100
    }
102
    }
101
103
Lines 105-111 Link Here
105
        }
107
        }
106
        return _instance;
108
        return _instance;
107
    }
109
    }
108
    
110
109
    public void removeHistoryFile(FileObject historyRoot) {
111
    public void removeHistoryFile(FileObject historyRoot) {
110
        try {
112
        try {
111
            FileObject folder = DataFolder.findFolder(historyRoot).getPrimaryFile();
113
            FileObject folder = DataFolder.findFolder(historyRoot).getPrimaryFile();
Lines 128-134 Link Here
128
            throw new SQLHistoryException();
130
            throw new SQLHistoryException();
129
        }
131
        }
130
    }
132
    }
131
    
133
132
    public List<SQLHistory> retrieve(String historyFilePath, FileObject historyFileObject) throws ClassNotFoundException, SQLHistoryException {
134
    public List<SQLHistory> retrieve(String historyFilePath, FileObject historyFileObject) throws ClassNotFoundException, SQLHistoryException {
133
        Handler handler = null;
135
        Handler handler = null;
134
        try {
136
        try {
Lines 136-143 Link Here
136
            DataFolder df = DataFolder.findFolder(historyFileObject);
138
            DataFolder df = DataFolder.findFolder(historyFileObject);
137
            AtomicFileAction reader = new AtomicFileAction(df, handler, READ, null);
139
            AtomicFileAction reader = new AtomicFileAction(df, handler, READ, null);
138
            df.getPrimaryFile().getFileSystem().runAtomicAction(reader);
140
            df.getPrimaryFile().getFileSystem().runAtomicAction(reader);
139
            
141
140
        } catch (IOException ex) {
142
        } catch (IOException ex) {
143
            sqlHistoryList = handler.getXmlSqlHistoryList();
141
            throw new SQLHistoryException();
144
            throw new SQLHistoryException();
142
        }
145
        }
143
        if (handler != null) {
146
        if (handler != null) {
Lines 146-160 Link Here
146
            return new ArrayList<SQLHistory>();
149
            return new ArrayList<SQLHistory>();
147
        }
150
        }
148
    }
151
    }
149
    
152
153
    public List<SQLHistory> retrieve() {
154
        return sqlHistoryList;
155
    }
156
150
    public void setNumElemsToRemove(int elemsToRemove) {
157
    public void setNumElemsToRemove(int elemsToRemove) {
151
        numElemsToRemove = elemsToRemove;
158
        numElemsToRemove = elemsToRemove;
152
    }
159
    }
153
    
160
154
    public int getNumElemsToRemove() {
161
    public int getNumElemsToRemove() {
155
        return numElemsToRemove;
162
        return numElemsToRemove;
156
    }
163
    }
157
    
164
158
    public List<SQLHistory> updateSQLSaved(int limit, FileObject historyFileObject) throws SQLHistoryException {
165
    public List<SQLHistory> updateSQLSaved(int limit, FileObject historyFileObject) throws SQLHistoryException {
159
        String historyFilePath = null;
166
        String historyFilePath = null;
160
        List<SQLHistory> updatedSQLHistoryList = null;
167
        List<SQLHistory> updatedSQLHistoryList = null;
Lines 163-169 Link Here
163
            if (historyFileObject == null) {
170
            if (historyFileObject == null) {
164
                return new ArrayList<SQLHistory>();
171
                return new ArrayList<SQLHistory>();
165
            }
172
            }
166
            updatedSQLHistoryList = retrieve(historyFilePath, historyFileObject);  
173
            updatedSQLHistoryList = retrieve(historyFilePath, historyFileObject);
167
            // Remove elements from list based on the number of statements to save that is set in the SQL History dialog
174
            // Remove elements from list based on the number of statements to save that is set in the SQL History dialog
168
            if (limit < updatedSQLHistoryList.size()) {
175
            if (limit < updatedSQLHistoryList.size()) {
169
                numElemsToRemove = updatedSQLHistoryList.size() - limit;
176
                numElemsToRemove = updatedSQLHistoryList.size() - limit;
Lines 175-181 Link Here
175
                    df.getPrimaryFile().getFileSystem().runAtomicAction(modifier);
182
                    df.getPrimaryFile().getFileSystem().runAtomicAction(modifier);
176
                }
183
                }
177
            }
184
            }
178
            updatedSQLHistoryList = retrieve(historyFilePath, historyFileObject); 
185
            updatedSQLHistoryList = retrieve(historyFilePath, historyFileObject);
179
        } catch (ClassNotFoundException ex) {
186
        } catch (ClassNotFoundException ex) {
180
            throw new SQLHistoryException();
187
            throw new SQLHistoryException();
181
        } catch (IOException ex) {
188
        } catch (IOException ex) {
Lines 183-189 Link Here
183
        }
190
        }
184
        return updatedSQLHistoryList;
191
        return updatedSQLHistoryList;
185
    }
192
    }
186
    
193
187
    private static final class AtomicFileAction implements FileSystem.AtomicAction {
194
    private static final class AtomicFileAction implements FileSystem.AtomicAction {
188
        List<SQLHistory> sqlHistoryList;
195
        List<SQLHistory> sqlHistoryList;
189
        DataFolder parent;
196
        DataFolder parent;
Lines 191-204 Link Here
191
        FileObject data;
198
        FileObject data;
192
        Handler handler;
199
        Handler handler;
193
        int actionType;
200
        int actionType;
194
        
201
195
        AtomicFileAction(DataFolder parent, Handler handler, int actionType, List<SQLHistory> sqlHistoryList) {
202
        AtomicFileAction(DataFolder parent, Handler handler, int actionType, List<SQLHistory> sqlHistoryList) {
196
            this.parent = parent;
203
            this.parent = parent;
197
            this.handler = handler;
204
            this.handler = handler;
198
            this.sqlHistoryList = sqlHistoryList;
205
            this.sqlHistoryList = sqlHistoryList;
199
            this.actionType = actionType;
206
            this.actionType = actionType;
200
        }
207
        }
201
        
208
202
        public void run() throws IOException {
209
        public void run() throws IOException {
203
            FileLock lck = null;
210
            FileLock lck = null;
204
            OutputStream ostm = null;
211
            OutputStream ostm = null;
Lines 246-255 Link Here
246
                        xmlWriter = new XmlWriter(data, sqlHistoryList, writer);
253
                        xmlWriter = new XmlWriter(data, sqlHistoryList, writer);
247
                        xmlWriter.write(xmlWriter.createElements(document), ""); // NOI18N
254
                        xmlWriter.write(xmlWriter.createElements(document), ""); // NOI18N
248
                        break;
255
                        break;
249
                    case MODIFY:                                                       
256
                    case MODIFY:
250
                        factory = DocumentBuilderFactory.newInstance();
257
                        factory = DocumentBuilderFactory.newInstance();
251
                        builder = factory.newDocumentBuilder();                                                                                               
258
                        builder = factory.newDocumentBuilder();
252
                            if (folder.getChildren().length > 0) {
259
                        if (folder.getChildren().length > 0) {
253
                            data = FileUtil.toFileObject(FileUtil.normalizeFile(new File(fn)));
260
                            data = FileUtil.toFileObject(FileUtil.normalizeFile(new File(fn)));
254
                            InputStream inputStream = data.getInputStream();
261
                            InputStream inputStream = data.getInputStream();
255
                            document = builder.parse(inputStream);
262
                            document = builder.parse(inputStream);
Lines 284-290 Link Here
284
                if (lck != null) {
291
                if (lck != null) {
285
                    lck.releaseLock();
292
                    lck.releaseLock();
286
                }
293
                }
287
            }  
294
            }
288
        }
295
        }
289
    }
296
    }
290
297
Lines 312-318 Link Here
312
                    nameNode = document.createElement("sql");  // NOI18N
319
                    nameNode = document.createElement("sql");  // NOI18N
313
                    nameNode.appendChild(document.createTextNode(sqlHistory.getSql()));
320
                    nameNode.appendChild(document.createTextNode(sqlHistory.getSql()));
314
                    nameNode.setAttribute("url", sqlHistory.getUrl());  // NOI18N
321
                    nameNode.setAttribute("url", sqlHistory.getUrl());  // NOI18N
315
                    nameNode.setAttribute("date", DateFormat.getInstance().format(sqlHistory.getDate()));  // NOI18N
322
                    nameNode.setAttribute("date", new Long(sqlHistory.getDate().getTime()).toString());  // NOI18N
316
                    newNode.appendChild(nameNode);
323
                    newNode.appendChild(nameNode);
317
                }
324
                }
318
                document.adoptNode(newNode);
325
                document.adoptNode(newNode);
Lines 322-334 Link Here
322
                    nameNode = document.createElement("sql");  // NOI18N
329
                    nameNode = document.createElement("sql");  // NOI18N
323
                    nameNode.appendChild(document.createTextNode(sqlHistory.getSql()));
330
                    nameNode.appendChild(document.createTextNode(sqlHistory.getSql()));
324
                    nameNode.setAttribute("url", sqlHistory.getUrl());  // NOI18N
331
                    nameNode.setAttribute("url", sqlHistory.getUrl());  // NOI18N
325
                    nameNode.setAttribute("date", DateFormat.getInstance().format(sqlHistory.getDate()));  // NOI18N
332
                    nameNode.setAttribute("date", new Long(sqlHistory.getDate().getTime()).toString());  // NOI18N
326
                    newNode.insertBefore(nameNode, newNode.getFirstChild());
333
                    newNode.insertBefore(nameNode, newNode.getFirstChild());
327
                }
334
                }
328
            }
335
            }
329
            return newNode;
336
            return newNode;
330
        }
337
        }
331
        
338
332
        /**
339
        /**
333
         * 
340
         * 
334
         * remove XML elements when the number of statements to save is reduced in the SQL History dialog   
341
         * remove XML elements when the number of statements to save is reduced in the SQL History dialog   
Lines 350-362 Link Here
350
                // Remove elements from the DOM
357
                // Remove elements from the DOM
351
                for (int i = 0; i < elemsToRemove; i++) {
358
                for (int i = 0; i < elemsToRemove; i++) {
352
                    if (nodes.item(0) != null) {
359
                    if (nodes.item(0) != null) {
353
                        history.removeChild(nodes.item(nodes.getLength()-1));
360
                        history.removeChild(nodes.item(nodes.getLength() - 1));
354
                    }
361
                    }
355
                }
362
                }
356
            }
363
            }
357
            return history;
364
            return history;
358
        }
365
        }
359
        
366
360
        private void write() {
367
        private void write() {
361
            pw.println("<?xml version='1.0' encoding='UTF-8' ?>");
368
            pw.println("<?xml version='1.0' encoding='UTF-8' ?>");
362
        }
369
        }
Lines 410-416 Link Here
410
            }
417
            }
411
418
412
        }
419
        }
413
        
420
414
        private String fixup(String s) {
421
        private String fixup(String s) {
415
            StringBuffer sb = new StringBuffer();
422
            StringBuffer sb = new StringBuffer();
416
            int len = s.length();
423
            int len = s.length();
Lines 440-446 Link Here
440
            return sb.toString();
447
            return sb.toString();
441
        }
448
        }
442
    }
449
    }
443
    
450
444
    /**
451
    /**
445
     * SAX handler for reading the XML file.
452
     * SAX handler for reading the XML file.
446
     */
453
     */
Lines 454-460 Link Here
454
        private static StringBuilder sql;
461
        private static StringBuilder sql;
455
        private static Date date;
462
        private static Date date;
456
        boolean matchingUrl = false;
463
        boolean matchingUrl = false;
457
        private  List<SQLHistory> xmlSqlHistoryList = new ArrayList<SQLHistory>();
464
        private List<SQLHistory> xmlSqlHistoryList = new ArrayList<SQLHistory>();
458
        static boolean isSql = false;
465
        static boolean isSql = false;
459
        private int limit = 10000;
466
        private int limit = 10000;
460
467
Lines 463-483 Link Here
463
        }
470
        }
464
471
465
        @Override
472
        @Override
466
        public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException {                                              
473
        public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException {
467
            if (ELEMENT_SQL.equals(qName)) {
474
            if (ELEMENT_SQL.equals(qName)) {
468
                isSql = true;
475
                isSql = true;
469
                
476
                url = attrs.getValue(ATTR_URL_PROPERTY_VALUE);
470
                try {
477
                // #152486 SQL History:  if running NB in multiple locales the history file cannot be parsed
471
                    url = attrs.getValue(ATTR_URL_PROPERTY_VALUE);
478
                if (attrs.getValue(ATTR_DATE_PROPERTY_VALUE).indexOf("/") != -1) { // NOI18N
472
                    date = DateFormat.getInstance().parse(attrs.getValue(ATTR_DATE_PROPERTY_VALUE));
479
                    try {
473
                } catch (ParseException ex) {
480
                        DateFormat df = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
474
                    throw new SAXException();
481
                        date = df.parse(attrs.getValue(ATTR_DATE_PROPERTY_VALUE));
482
                    } catch (ParseException ex) {
483
                        throw new SAXException();
484
                    }
485
                } else {
486
                    Calendar calendar = Calendar.getInstance();
487
                    calendar.setTimeInMillis(new Long(attrs.getValue(ATTR_DATE_PROPERTY_VALUE)).longValue());
488
                    date = calendar.getTime();
475
                }
489
                }
476
            } else {
490
            } else {
477
                isSql = false;
491
                isSql = false;
478
            }
492
            }
479
        }
493
        }
480
        
494
481
        @Override
495
        @Override
482
        public void endElement(String uri, String localName, String qName) {
496
        public void endElement(String uri, String localName, String qName) {
483
            if (ELEMENT_SQL.equals(qName)) {
497
            if (ELEMENT_SQL.equals(qName)) {
Lines 494-500 Link Here
494
            date = null;
508
            date = null;
495
            sql = null;
509
            sql = null;
496
        }
510
        }
497
        
511
498
        private void addHistory(String url, String sql, Date date) {
512
        private void addHistory(String url, String sql, Date date) {
499
            String sqlSetting = NbPreferences.forModule(SQLHistoryPersistenceManager.class).get("SQL_STATEMENTS_SAVED_FOR_HISTORY", "");
513
            String sqlSetting = NbPreferences.forModule(SQLHistoryPersistenceManager.class).get("SQL_STATEMENTS_SAVED_FOR_HISTORY", "");
500
            if (!sqlSetting.equals("")) { // NOI18N
514
            if (!sqlSetting.equals("")) { // NOI18N
Lines 508-514 Link Here
508
                xmlSqlHistoryList.remove(xmlSqlHistoryList.size() - 1);
522
                xmlSqlHistoryList.remove(xmlSqlHistoryList.size() - 1);
509
            }
523
            }
510
        }
524
        }
511
 
525
512
        @Override
526
        @Override
513
        public void characters(char buf[], int offset, int length) {
527
        public void characters(char buf[], int offset, int length) {
514
            if (isSql) {
528
            if (isSql) {
Lines 521-527 Link Here
521
                }
535
                }
522
            }
536
            }
523
        }
537
        }
524
        
538
525
        public void setXmlSqlHistoryList(List<SQLHistory> sqlHistoryList) {
539
        public void setXmlSqlHistoryList(List<SQLHistory> sqlHistoryList) {
526
            xmlSqlHistoryList = sqlHistoryList;
540
            xmlSqlHistoryList = sqlHistoryList;
527
        }
541
        }

Return to bug 152486