From: Harishankar Date: Tue, 31 Mar 2020 07:31:39 +0000 (+0530) Subject: Option for creating a new Legal Database X-Git-Url: https://harishankar.org/repos/?a=commitdiff_plain;h=43efa7c67c7da936d3395345983dde4603253896;p=habeas.git Option for creating a new Legal Database Introduced option for creating a new legal database from the Database Settings dialog. --- diff --git a/src/habeas/DatabaseSettingsDialog.form b/src/habeas/DatabaseSettingsDialog.form index cf74391..4367487 100644 --- a/src/habeas/DatabaseSettingsDialog.form +++ b/src/habeas/DatabaseSettingsDialog.form @@ -30,21 +30,23 @@ - + - - - - + + + + + + - + + - + - @@ -57,11 +59,12 @@ + - + - + @@ -86,6 +89,7 @@ + @@ -94,11 +98,20 @@ + + + + + + + + + diff --git a/src/habeas/DatabaseSettingsDialog.java b/src/habeas/DatabaseSettingsDialog.java index c28c82d..763361e 100644 --- a/src/habeas/DatabaseSettingsDialog.java +++ b/src/habeas/DatabaseSettingsDialog.java @@ -6,6 +6,7 @@ package habeas; import javax.swing.JFileChooser; +import javax.swing.JOptionPane; /** * @@ -36,6 +37,7 @@ public class DatabaseSettingsDialog extends javax.swing.JDialog { buttonChooseFile = new javax.swing.JButton(); buttonClose = new javax.swing.JButton(); buttonApply = new javax.swing.JButton(); + jButton1 = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); setTitle("Connection"); @@ -50,6 +52,7 @@ public class DatabaseSettingsDialog extends javax.swing.JDialog { } }); + buttonClose.setMnemonic('C'); buttonClose.setText("Close"); buttonClose.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { @@ -57,6 +60,7 @@ public class DatabaseSettingsDialog extends javax.swing.JDialog { } }); + buttonApply.setMnemonic('a'); buttonApply.setText("Apply"); buttonApply.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { @@ -64,6 +68,13 @@ public class DatabaseSettingsDialog extends javax.swing.JDialog { } }); + jButton1.setText("Create..."); + jButton1.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + jButton1ActionPerformed(evt); + } + }); + javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( @@ -73,16 +84,18 @@ public class DatabaseSettingsDialog extends javax.swing.JDialog { .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE) - .addGap(0, 0, Short.MAX_VALUE)) + .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGroup(layout.createSequentialGroup() - .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) - .addComponent(txtDatabaseURL, javax.swing.GroupLayout.PREFERRED_SIZE, 231, javax.swing.GroupLayout.PREFERRED_SIZE) - .addComponent(buttonApply, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)) + .addComponent(txtDatabaseURL, javax.swing.GroupLayout.DEFAULT_SIZE, 255, Short.MAX_VALUE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) - .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addComponent(buttonClose, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) - .addComponent(buttonChooseFile, javax.swing.GroupLayout.DEFAULT_SIZE, 93, Short.MAX_VALUE)))) - .addContainerGap()) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) + .addComponent(buttonChooseFile, javax.swing.GroupLayout.DEFAULT_SIZE, 109, Short.MAX_VALUE) + .addComponent(buttonApply, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) + .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 97, Short.MAX_VALUE) + .addComponent(buttonClose, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) + .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) @@ -92,11 +105,12 @@ public class DatabaseSettingsDialog extends javax.swing.JDialog { .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(txtDatabaseURL, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) - .addComponent(buttonChooseFile)) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 29, Short.MAX_VALUE) + .addComponent(buttonChooseFile) + .addComponent(jButton1)) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 22, Short.MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) - .addComponent(buttonClose) - .addComponent(buttonApply)) + .addComponent(buttonApply) + .addComponent(buttonClose)) .addContainerGap()) ); @@ -105,7 +119,7 @@ public class DatabaseSettingsDialog extends javax.swing.JDialog { private void buttonApplyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonApplyActionPerformed // TODO add your handling code here - Utility.setConnectionURL (txtDatabaseURL.getText()); + Utility.saveConnectionURL (txtDatabaseURL.getText()); }//GEN-LAST:event_buttonApplyActionPerformed private void buttonCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonCloseActionPerformed @@ -122,6 +136,22 @@ public class DatabaseSettingsDialog extends javax.swing.JDialog { } }//GEN-LAST:event_buttonChooseFileActionPerformed + private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed + // TODO add your handling code here: + JFileChooser chooser = new JFileChooser (); + int i = chooser.showSaveDialog(this); + if (i == JFileChooser.APPROVE_OPTION) { + String db_path = chooser.getSelectedFile().getAbsolutePath(); + boolean rt = Utility.createDatabase (db_path); + if (rt == false) + JOptionPane.showMessageDialog(this, ERROR_CREATING_TABLE); + else + txtDatabaseURL.setText(db_path); + } + }//GEN-LAST:event_jButton1ActionPerformed + private static final String ERROR_CREATING_TABLE = "Error creating database. " + + "Please check connection settings and driver"; + /** * @param args the command line arguments */ @@ -169,6 +199,7 @@ public class DatabaseSettingsDialog extends javax.swing.JDialog { private javax.swing.JButton buttonApply; private javax.swing.JButton buttonChooseFile; private javax.swing.JButton buttonClose; + private javax.swing.JButton jButton1; private javax.swing.JLabel jLabel1; private javax.swing.JTextField txtDatabaseURL; // End of variables declaration//GEN-END:variables diff --git a/src/habeas/Habeas.java b/src/habeas/Habeas.java index 73efc94..b8b43d4 100644 --- a/src/habeas/Habeas.java +++ b/src/habeas/Habeas.java @@ -21,7 +21,7 @@ public class Habeas { */ public static void main(String[] args) { // TODO code application logic here - Utility.getConnectionURL(); + Utility.retrieveConnectionURL(); try { UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()); diff --git a/src/habeas/Utility.java b/src/habeas/Utility.java index 2d2caca..6ca96e5 100644 --- a/src/habeas/Utility.java +++ b/src/habeas/Utility.java @@ -17,7 +17,7 @@ import java.util.logging.Logger; */ public class Utility { - static void setConnectionURL(String text) { + static void saveConnectionURL(String text) { connectionURL = text; Preferences.userRoot().put("ConnectionURL", text); } @@ -291,6 +291,55 @@ public class Utility { return false; } + } + + static boolean createDatabase(String db_path) { + saveConnectionURL(db_path); + String tblClients = "CREATE TABLE IF NOT EXISTS \"clients\" (\n" + +" \"id\" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,\n" + +" \"ClientName\" VARCHAR(255) NOT NULL UNIQUE,\n" + +" \"ClientAddress\" TEXT,\n" + +" \"ContactPerson\" TEXT,\n" + +" \"MailID\" VARCHAR(255),\n" + +" \"ContactNumber\" VARCHAR(30)\n" + +" );"; + String tblNotices = "CREATE TABLE IF NOT EXISTS \"legalnotices\" (\n" + + " \"id\" INTEGER PRIMARY KEY AUTOINCREMENT,\n" + + " \"ReferenceNumber\" TEXT NOT NULL UNIQUE,\n" + + " \"Description\" TEXT NOT NULL,\n" + + " \"EntrustmentDate\" INTEGER NOT NULL,\n" + + " \"ClientId\" INTEGER,\n" + + " \"DraftCreated\" INTEGER DEFAULT 0,\n" + + " \"DraftApproved\" INTEGER DEFAULT 0 CHECK(DraftApproved<=DraftCreated),\n" + + " \"NoticeSent\" INTEGER DEFAULT 0 CHECK(NoticeSent<=DraftApproved),\n" + + " \"SentDate\" INTEGER CHECK(SentDate>=EntrustmentDate),\n" + + " \"RPADReference\" TEXT,\n" + + " \"NoticeDelivered\" INTEGER DEFAULT 0 CHECK(NoticeDelivered<=NoticeSent),\n" + + " \"DeliveryDate\" INTEGER CHECK(DeliveryDate>=SentDate),\n" + + " \"BillStatus\" TEXT DEFAULT 'PENDING',\n" + + " \"BillAmount\" INTEGER DEFAULT 1000 CHECK(BillAmount>0),\n" + + " \"BillDate\" INTEGER CHECK(BillDate>=DeliveryDate),\n" + + " \"ClarificationPending\" INTEGER DEFAULT 0,\n" + + " \"ClarificationRemarks\" TEXT\n" + + ");"; + String tblSettings = "CREATE TABLE IF NOT EXISTS \"settings\" (\n" + +" \"key\" TEXT UNIQUE,\n" + +" \"value\" TEXT,\n" + +" PRIMARY KEY(\"key\")\n" + + ");"; + try { + Connection conn = DriverManager.getConnection(JDBC + connectionURL); + Statement st1 = conn.createStatement(); + st1.execute(tblSettings); + st1.execute(tblClients); + st1.execute(tblNotices); + conn.close(); + return true; + } catch (SQLException ex) { + Logger.getLogger(Utility.class.getName()).log(Level.SEVERE, null, ex); + return false; + } + } public Utility () { @@ -320,7 +369,7 @@ public class Utility { } - public static void getConnectionURL () { + public static void retrieveConnectionURL () { connectionURL = Preferences.userRoot().get("ConnectionURL", "legaldb"); }