PageRenderTime 164ms CodeModel.GetById 60ms app.highlight 45ms RepoModel.GetById 55ms app.codeStats 1ms

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

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