PageRenderTime 24ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

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