PageRenderTime 729ms CodeModel.GetById 80ms app.highlight 520ms RepoModel.GetById 79ms app.codeStats 1ms

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

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 319 lines | 268 code | 44 blank | 7 comment | 12 complexity | fd75f7ca30acee8c8abd6a7f4ab23581 MD5 | raw file
  1package mpv5.ui.dialogs.subcomponents;
  2
  3import java.awt.Cursor;
  4import java.util.List;
  5import java.util.Vector;
  6import javax.swing.DefaultComboBoxModel;
  7
  8import mpv5.db.common.ConnectionTypeHandler;
  9import mpv5.db.common.Context;
 10import mpv5.db.common.DatabaseConnection;
 11import mpv5.db.common.DatabaseObject;
 12import mpv5.db.common.NodataFoundException;
 13import mpv5.db.common.QueryHandler;
 14import mpv5.db.common.ReturnValue;
 15import mpv5.globals.Messages;
 16import mpv5.logging.Log;
 17import mpv5.ui.dialogs.DialogForFile;
 18import mpv5.ui.dialogs.Popup;
 19import mpv5.ui.dialogs.WizardMaster;
 20import mpv5.ui.dialogs.Wizardable;
 21
 22/**
 23 *
 24 * 
 25 */
 26public class wizard_Yabs1_Import extends javax.swing.JPanel implements Wizardable {
 27
 28    private static final long serialVersionUID = -8347532498124147821L;
 29    private WizardMaster master;
 30    private DatabaseConnection conn;
 31    private QueryHandler qh;
 32    private int imports;
 33
 34    public wizard_Yabs1_Import(WizardMaster w) {
 35        this.master = w;
 36        initComponents();
 37        path.setFilter(DialogForFile.DIRECTORIES);
 38        jComboBox1.setModel(new DefaultComboBoxModel(ConnectionTypeHandler.DRIVERS));
 39        path.setModalityParent(this);
 40    }
 41
 42    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
 43    private void initComponents() {
 44
 45        jPanel1 = new javax.swing.JPanel();
 46        path = new mpv5.ui.beans.LabeledTextChooser();
 47        jLabel1 = new javax.swing.JLabel();
 48        contacts = new javax.swing.JCheckBox();
 49        products = new javax.swing.JCheckBox();
 50        accounts = new javax.swing.JCheckBox();
 51        groups = new javax.swing.JCheckBox();
 52        adresses = new javax.swing.JCheckBox();
 53        users = new javax.swing.JCheckBox();
 54        jComboBox1 = new javax.swing.JComboBox();
 55        jLabel2 = new javax.swing.JLabel();
 56        dbname = new mpv5.ui.beans.LabeledTextField();
 57        dbuser = new mpv5.ui.beans.LabeledTextField();
 58        dbpassword = new mpv5.ui.beans.LabeledTextField();
 59        dbprefix = new mpv5.ui.beans.LabeledTextField();
 60
 61        setBackground(new java.awt.Color(255, 255, 255));
 62        setName("Form"); // NOI18N
 63        setLayout(new java.awt.BorderLayout());
 64
 65        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
 66         java.util.ResourceBundle bundle = mpv5.i18n.LanguageManager.getBundle(); // NOI18N
 67        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(bundle.getString("wizard_Yabs1_Import.jPanel1.border.title"))); // NOI18N
 68        jPanel1.setName("jPanel1"); // NOI18N
 69
 70        path.set_Label(bundle.getString("wizard_Yabs1_Import.path._Label")); // NOI18N
 71        path.setName("path"); // NOI18N
 72
 73        jLabel1.setFont(jLabel1.getFont().deriveFont(jLabel1.getFont().getStyle() & ~java.awt.Font.BOLD));
 74        jLabel1.setText(bundle.getString("wizard_Yabs1_Import.jLabel1.text")); // NOI18N
 75        jLabel1.setName("jLabel1"); // NOI18N
 76
 77        contacts.setBackground(new java.awt.Color(255, 255, 255));
 78        contacts.setSelected(true);
 79        contacts.setText(bundle.getString("wizard_Yabs1_Import.contacts.text")); // NOI18N
 80        contacts.setBorder(javax.swing.BorderFactory.createEtchedBorder());
 81        contacts.setName("contacts"); // NOI18N
 82
 83        products.setBackground(new java.awt.Color(255, 255, 255));
 84        products.setSelected(true);
 85        products.setText(bundle.getString("wizard_Yabs1_Import.products.text")); // NOI18N
 86        products.setBorder(javax.swing.BorderFactory.createEtchedBorder());
 87        products.setName("products"); // NOI18N
 88
 89        accounts.setBackground(new java.awt.Color(255, 255, 255));
 90        accounts.setSelected(true);
 91        accounts.setText(bundle.getString("wizard_Yabs1_Import.accounts.text")); // NOI18N
 92        accounts.setBorder(javax.swing.BorderFactory.createEtchedBorder());
 93        accounts.setName("accounts"); // NOI18N
 94
 95        groups.setBackground(new java.awt.Color(255, 255, 255));
 96        groups.setSelected(true);
 97        groups.setText(bundle.getString("wizard_Yabs1_Import.groups.text")); // NOI18N
 98        groups.setBorder(javax.swing.BorderFactory.createEtchedBorder());
 99        groups.setName("groups"); // NOI18N
100
101        adresses.setBackground(new java.awt.Color(255, 255, 255));
102        adresses.setSelected(true);
103        adresses.setText(bundle.getString("wizard_Yabs1_Import.adresses.text")); // NOI18N
104        adresses.setBorder(javax.swing.BorderFactory.createEtchedBorder());
105        adresses.setName("adresses"); // NOI18N
106
107        users.setBackground(new java.awt.Color(255, 255, 255));
108        users.setSelected(true);
109        users.setText(bundle.getString("wizard_Yabs1_Import.users.text")); // NOI18N
110        users.setBorder(javax.swing.BorderFactory.createEtchedBorder());
111        users.setName("users"); // NOI18N
112
113        jComboBox1.setEditable(true);
114        jComboBox1.setFont(new java.awt.Font("Dialog", 0, 11));
115        jComboBox1.setName("jComboBox1"); // NOI18N
116        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
117            public void actionPerformed(java.awt.event.ActionEvent evt) {
118                jComboBox1ActionPerformed(evt);
119            }
120        });
121
122        jLabel2.setText(bundle.getString("wizard_Yabs1_Import.jLabel2.text")); // NOI18N
123        jLabel2.setName("jLabel2"); // NOI18N
124
125        dbname.set_Label(bundle.getString("wizard_Yabs1_Import.dbname._Label")); // NOI18N
126        dbname.setName("dbname"); // NOI18N
127
128        dbuser.set_Label(bundle.getString("wizard_Yabs1_Import.dbuser._Label")); // NOI18N
129        dbuser.setName("dbuser"); // NOI18N
130
131        dbpassword.set_Label(bundle.getString("wizard_Yabs1_Import.dbpassword._Label")); // NOI18N
132        dbpassword.setName("dbpassword"); // NOI18N
133
134        dbprefix.set_Label(bundle.getString("wizard_Yabs1_Import.dbprefix._Label")); // NOI18N
135        dbprefix.setName("dbprefix"); // NOI18N
136
137        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
138        jPanel1.setLayout(jPanel1Layout);
139        jPanel1Layout.setHorizontalGroup(
140            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
141            .addGroup(jPanel1Layout.createSequentialGroup()
142                .addContainerGap()
143                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
144                    .addGroup(jPanel1Layout.createSequentialGroup()
145                        .addComponent(contacts)
146                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
147                        .addComponent(adresses))
148                    .addComponent(products)
149                    .addComponent(accounts)
150                    .addComponent(groups)
151                    .addComponent(users)
152                    .addComponent(path, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 505, Short.MAX_VALUE)
153                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
154                        .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 70, Short.MAX_VALUE)
155                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
156                        .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 431, javax.swing.GroupLayout.PREFERRED_SIZE))
157                    .addComponent(jLabel1)
158                    .addGroup(jPanel1Layout.createSequentialGroup()
159                        .addComponent(dbuser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
160                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 119, Short.MAX_VALUE)
161                        .addComponent(dbname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
162                    .addGroup(jPanel1Layout.createSequentialGroup()
163                        .addComponent(dbpassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
164                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 119, Short.MAX_VALUE)
165                        .addComponent(dbprefix, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
166                .addContainerGap())
167        );
168        jPanel1Layout.setVerticalGroup(
169            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
170            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
171                .addContainerGap()
172                .addComponent(users)
173                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
174                .addComponent(groups)
175                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
176                .addComponent(accounts)
177                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
178                .addComponent(products)
179                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
180                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
181                    .addComponent(contacts)
182                    .addComponent(adresses))
183                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 77, Short.MAX_VALUE)
184                .addComponent(jLabel1)
185                .addGap(18, 18, 18)
186                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
187                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
188                    .addComponent(jLabel2))
189                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
190                .addComponent(path, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
191                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
192                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
193                    .addGroup(jPanel1Layout.createSequentialGroup()
194                        .addComponent(dbuser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
195                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
196                        .addComponent(dbpassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
197                    .addGroup(jPanel1Layout.createSequentialGroup()
198                        .addComponent(dbname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
199                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
200                        .addComponent(dbprefix, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
201                .addContainerGap())
202        );
203
204        add(jPanel1, java.awt.BorderLayout.CENTER);
205    }// </editor-fold>//GEN-END:initComponents
206
207    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
208    }//GEN-LAST:event_jComboBox1ActionPerformed
209    // Variables declaration - do not modify//GEN-BEGIN:variables
210    private javax.swing.JCheckBox accounts;
211    private javax.swing.JCheckBox adresses;
212    private javax.swing.JCheckBox contacts;
213    private mpv5.ui.beans.LabeledTextField dbname;
214    private mpv5.ui.beans.LabeledTextField dbpassword;
215    private mpv5.ui.beans.LabeledTextField dbprefix;
216    private mpv5.ui.beans.LabeledTextField dbuser;
217    private javax.swing.JCheckBox groups;
218    private javax.swing.JComboBox jComboBox1;
219    private javax.swing.JLabel jLabel1;
220    private javax.swing.JLabel jLabel2;
221    private javax.swing.JPanel jPanel1;
222    private mpv5.ui.beans.LabeledTextChooser path;
223    private javax.swing.JCheckBox products;
224    private javax.swing.JCheckBox users;
225    // End of variables declaration//GEN-END:variables
226
227    public boolean next() {
228        master.setCursor(new Cursor(Cursor.WAIT_CURSOR));
229        if (path.hasText()) {
230
231            imports();
232
233            master.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
234            master.isEnd(true);
235
236            Popup.notice(imports + " " + Messages.IMPORTED.toString());
237            Popup.notice(errors, mpv5.globals.Messages.ERROR_OCCURED);
238            return false;
239        } else {
240            return false;
241        }
242    }
243
244    public boolean back() {
245        return false;
246    }
247
248    public void load() {
249    }
250
251    private void imports() {
252        try {
253            conn = new DatabaseConnection();
254
255            conn.connect(jComboBox1.getSelectedItem().toString(),
256                    dbuser.getText(),
257                    dbpassword.getText(),
258                    path.get_Text(false),
259                    dbname.getText(), dbprefix.getText(),
260                    false);
261            conn.setProgressbar(master.getProgressbar());
262
263            if (conn.getConnection().isValid(1000)) {
264
265                qh = new QueryHandler(conn);
266                if (users.isSelected()) {
267                    importObjects(Context.getUser());
268                }
269
270                if (groups.isSelected()) {
271                    importObjects(Context.getGroup());
272                }
273
274                if (accounts.isSelected()) {
275                    importObjects(Context.getAccounts());
276                }
277
278                if (contacts.isSelected()) {
279                    importObjects(Context.getContact());
280                }
281
282                if (accounts.isSelected()) {
283                    importObjects(Context.getAccounts());
284                }
285
286                if (products.isSelected()) {
287                    importObjects(Context.getProduct());
288                }
289            }
290
291
292        } catch (Exception exception) {
293            Popup.error(exception);
294            Log.Debug(exception);
295        }
296    }
297    List<DatabaseObject> errors = new Vector<DatabaseObject>();
298
299    private void importObjects(Context c) {
300        try {
301            ReturnValue data = qh.clone(c).select(false);
302            DatabaseObject[] objs = DatabaseObject.explode(data, DatabaseObject.getObject(c), false, false);
303            for (int i = 0; i < objs.length; i++) {
304                DatabaseObject databaseObject = objs[i];
305                try {
306                    if (!databaseObject.saveImport()) {
307                        errors.add(databaseObject);
308                    } else {
309                        imports++;
310                    }
311                } catch (Exception e) {
312                    errors.add(databaseObject);
313                }
314            }
315        } catch (NodataFoundException nodataFoundException) {
316            Log.Debug(this, nodataFoundException.getMessage());
317        }
318    }
319}