PageRenderTime 145ms CodeModel.GetById 61ms app.highlight 45ms RepoModel.GetById 35ms app.codeStats 0ms

/src/mpv5/ui/dialogs/subcomponents/wizard_AccountXMLImport_1.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 247 lines | 204 code | 31 blank | 12 comment | 10 complexity | 80a117f30e41a17f3e4f60627f22cdc8 MD5 | raw file
  1package mpv5.ui.dialogs.subcomponents;
  2
  3import java.awt.Cursor;
  4import java.io.File;
  5import java.util.ArrayList;
  6
  7
  8import java.util.Arrays;
  9import java.util.HashMap;
 10import java.util.List;
 11import java.util.Vector;
 12import java.util.concurrent.ExecutionException;
 13import java.util.logging.Level;
 14import java.util.logging.Logger;
 15import javax.swing.SwingUtilities;
 16import javax.swing.SwingWorker;
 17import javax.swing.text.TableView.TableRow;
 18import mpv5.db.common.DatabaseObject;
 19import mpv5.db.objects.Account;
 20import mpv5.globals.Messages;
 21import mpv5.logging.Log;
 22import mpv5.ui.dialogs.Popup;
 23import mpv5.ui.dialogs.WizardMaster;
 24import mpv5.ui.dialogs.Wizardable;
 25import mpv5.utils.models.ImportTableModel;
 26import mpv5.utils.tables.TableFormat;
 27import mpv5.utils.xml.XMLReader;
 28
 29/**
 30 *
 31 * 
 32 */
 33public class wizard_AccountXMLImport_1 extends javax.swing.JPanel implements Wizardable {
 34
 35    private static final long serialVersionUID = -8347532498124147821L;
 36    private WizardMaster master;
 37    private boolean isConsumed = false;
 38
 39    public wizard_AccountXMLImport_1(WizardMaster w) {
 40        this.master = w;
 41        initComponents();
 42
 43    }
 44
 45    private void importXML() {
 46        master.setCursor(new Cursor(Cursor.WAIT_CURSOR));
 47        isConsumed = true;
 48        XMLReader x;
 49        ArrayList<ArrayList<DatabaseObject>> objs = null;
 50
 51        if (master.getStore().hasProperty("file")) {
 52            x = new XMLReader();
 53            x.setOverwriteExisting(true);
 54            try {
 55                x.newDoc(new File(master.getStore().getProperty("file")), false);
 56                objs = x.getObjects();
 57                jTable1.setModel(ImportTableModel.getModel(objs, false));
 58                jLabel2.setText(jLabel2.getText() + " " + master.getStore().getProperty("file") + " (" + jTable1.getRowCount() + ")");
 59                TableFormat.format(jTable1, 0, 0);
 60                TableFormat.format(jTable1, 1, 0);
 61                TableFormat.format(jTable1, 2, 100);
 62            } catch (Exception ex) {
 63                Popup.error(ex);
 64                Log.Debug(ex);
 65            } finally {
 66                master.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
 67            }
 68        }
 69    }
 70
 71    private void imports() {
 72        jProgressBar1.setMinimum(0);
 73        jProgressBar1.setMaximum(jTable1.getRowCount());
 74
 75        final List<Account> accs = new Vector<Account>();
 76        final HashMap<Integer, Account> daccs = new HashMap<Integer, Account>();
 77        Runnable runnable = new Runnable() {
 78
 79            public void run() {
 80                int imp = 0;
 81                String val = null;
 82                for (int i = 0; i < jTable1.getRowCount(); i++) {
 83                    master.setCursor(new Cursor(Cursor.WAIT_CURSOR));
 84                    final int p = i;
 85                    try {
 86                        DatabaseObject dog = ((DatabaseObject) jTable1.getValueAt(p, 0));
 87                        accs.add((Account) dog);
 88
 89                        val = "<html><p><font color =green>Analysed ID: " + dog.__getIDS();
 90                        imp++;
 91                    } catch (Exception e) {
 92                        val = "<html><p><font color =red>" + Messages.ERROR_OCCURED + ": " + e.getMessage();
 93                    } finally {
 94                        jTable1.setValueAt(val, p, 4);
 95                        jProgressBar1.setValue(p);
 96                        master.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
 97                    }
 98                }
 99
100                jProgressBar1.setValue(0);
101                 master.setCursor(new Cursor(Cursor.WAIT_CURSOR));
102                //find lowest parents (children of all accounts)
103                for (int j = 0; j < accs.size(); j++) {
104                    Account account = accs.get(j);
105                    daccs.put(account.__getIDS(), account);
106                    account.setIDS(-1);
107                }
108
109                for (int i = 0; i < accs.size(); i++) {
110                    Account account = accs.get(i);
111                    account.setIntparentaccount(findParentAccount(account));
112                    account.saveImport();
113                    try {
114                        val = "<html><p><font color =green>Saved account " + account + " [" + account.__getIDS() +"]";
115                    } catch (Exception e) {
116                        val = "<html><p><font color =red>" + Messages.ERROR_OCCURED + ": " + e.getMessage();
117                    } finally {
118                        jTable1.setValueAt(val, i, 4);
119                        jProgressBar1.setValue(i);
120                        master.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
121                    }
122                }
123
124                jProgressBar1.setString(imp + " " + Messages.IMPORTED);
125            }
126
127            //recursion is a powerful, but slow beast..
128            private int findParentAccount(Account account) {
129                Account pa = daccs.get(account.__getIntparentaccount());
130                if (pa == null) {
131                    return 1;
132                } else if (pa.isExisting()) {
133                    return pa.__getIDS();
134                } else {
135                    pa.setIntparentaccount(findParentAccount(pa));
136                    pa.saveImport();
137                    return pa.__getIDS();
138                }
139            }
140        };
141        new Thread(runnable).start();
142
143        master.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
144        master.isEnd(true);
145    }
146
147    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
148    private void initComponents() {
149
150        jPanel1 = new javax.swing.JPanel();
151        jLabel2 = new javax.swing.JLabel();
152        jScrollPane1 = new javax.swing.JScrollPane();
153        jTable1 = new javax.swing.JTable();
154        jProgressBar1 = new javax.swing.JProgressBar();
155
156        setBackground(new java.awt.Color(255, 255, 255));
157        setName("Form"); // NOI18N
158        setLayout(new java.awt.BorderLayout());
159
160        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
161        java.util.ResourceBundle bundle = mpv5.i18n.LanguageManager.getBundle(); 
162        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(bundle.getString("wizard_AccountXMLImport_1.jPanel1.border.title"))); // NOI18N
163        jPanel1.setName("jPanel1"); // NOI18N
164
165        jLabel2.setText(bundle.getString("wizard_AccountXMLImport_1.jLabel2.text")); // NOI18N
166        jLabel2.setName("jLabel2"); // NOI18N
167
168        jScrollPane1.setName("jScrollPane1"); // NOI18N
169
170        jTable1.setAutoCreateRowSorter(true);
171        jTable1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
172        jTable1.setModel(new javax.swing.table.DefaultTableModel(
173            new Object [][] {
174                {null}
175            },
176            new String [] {
177                "Title 1"
178            }
179        ));
180        jTable1.setCellSelectionEnabled(true);
181        jTable1.setDoubleBuffered(true);
182        jTable1.setFillsViewportHeight(true);
183        jTable1.setName("jTable1"); // NOI18N
184        jScrollPane1.setViewportView(jTable1);
185
186        jProgressBar1.setName("jProgressBar1"); // NOI18N
187        jProgressBar1.setStringPainted(true);
188
189        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
190        jPanel1.setLayout(jPanel1Layout);
191        jPanel1Layout.setHorizontalGroup(
192            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
193            .addGroup(jPanel1Layout.createSequentialGroup()
194                .addContainerGap()
195                .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 399, Short.MAX_VALUE)
196                .addContainerGap())
197            .addComponent(jProgressBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 423, Short.MAX_VALUE)
198            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
199                .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 423, Short.MAX_VALUE))
200        );
201        jPanel1Layout.setVerticalGroup(
202            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
203            .addGroup(jPanel1Layout.createSequentialGroup()
204                .addComponent(jLabel2)
205                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 343, Short.MAX_VALUE)
206                .addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
207            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
208                .addGroup(jPanel1Layout.createSequentialGroup()
209                    .addGap(20, 20, 20)
210                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 337, Short.MAX_VALUE)
211                    .addGap(21, 21, 21)))
212        );
213
214        add(jPanel1, java.awt.BorderLayout.CENTER);
215    }// </editor-fold>//GEN-END:initComponents
216    // Variables declaration - do not modify//GEN-BEGIN:variables
217    private javax.swing.JLabel jLabel2;
218    private javax.swing.JPanel jPanel1;
219    private javax.swing.JProgressBar jProgressBar1;
220    private javax.swing.JScrollPane jScrollPane1;
221    private javax.swing.JTable jTable1;
222    // End of variables declaration//GEN-END:variables
223
224    public boolean next() {
225        try {
226            imports();
227        } catch (Exception e) {
228            Log.Debug(e);
229            master.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
230        }
231        return false;
232    }
233
234    public boolean back() {
235        return true;
236    }
237
238    /**
239     *
240     */
241    public void load() {
242        if (!isConsumed) {
243            Log.Debug(this, "Overwrite is set to " + master.getStore().getProperty("overwrite", false));
244            importXML();
245        }
246    }
247}