Option for creating a new Legal Database
authorHarishankar <v.harishankar@gmail.com>
Tue, 31 Mar 2020 07:31:39 +0000 (13:01 +0530)
committerHarishankar <v.harishankar@gmail.com>
Tue, 31 Mar 2020 07:31:39 +0000 (13:01 +0530)
Introduced option for creating a new legal database from
the Database Settings dialog.

src/habeas/DatabaseSettingsDialog.form
src/habeas/DatabaseSettingsDialog.java
src/habeas/Habeas.java
src/habeas/Utility.java

index cf74391..4367487 100644 (file)
               <Group type="103" groupAlignment="0" attributes="0">
                   <Group type="102" attributes="0">
                       <Component id="jLabel1" min="-2" pref="139" max="-2" attributes="0"/>
-                      <EmptySpace min="0" pref="0" max="32767" attributes="0"/>
+                      <EmptySpace max="32767" attributes="0"/>
                   </Group>
-                  <Group type="102" alignment="0" attributes="0">
-                      <Group type="103" groupAlignment="1" attributes="0">
-                          <Component id="txtDatabaseURL" min="-2" pref="231" max="-2" attributes="0"/>
-                          <Component id="buttonApply" min="-2" pref="92" max="-2" attributes="0"/>
+                  <Group type="102" attributes="0">
+                      <Component id="txtDatabaseURL" pref="255" max="32767" attributes="0"/>
+                      <EmptySpace max="-2" attributes="0"/>
+                      <Group type="103" groupAlignment="0" max="-2" attributes="0">
+                          <Component id="buttonChooseFile" pref="109" max="32767" attributes="0"/>
+                          <Component id="buttonApply" max="32767" attributes="0"/>
                       </Group>
                       <EmptySpace max="-2" attributes="0"/>
-                      <Group type="103" groupAlignment="0" attributes="0">
+                      <Group type="103" groupAlignment="0" max="-2" attributes="0">
+                          <Component id="jButton1" pref="97" max="32767" attributes="0"/>
                           <Component id="buttonClose" max="32767" attributes="0"/>
-                          <Component id="buttonChooseFile" pref="93" max="32767" attributes="0"/>
                       </Group>
+                      <EmptySpace max="32767" attributes="0"/>
                   </Group>
               </Group>
-              <EmptySpace max="-2" attributes="0"/>
           </Group>
       </Group>
     </DimensionLayout>
               <Group type="103" groupAlignment="3" attributes="0">
                   <Component id="txtDatabaseURL" alignment="3" min="-2" max="-2" attributes="0"/>
                   <Component id="buttonChooseFile" alignment="3" min="-2" max="-2" attributes="0"/>
+                  <Component id="jButton1" alignment="3" min="-2" max="-2" attributes="0"/>
               </Group>
-              <EmptySpace pref="29" max="32767" attributes="0"/>
+              <EmptySpace pref="22" max="32767" attributes="0"/>
               <Group type="103" groupAlignment="3" attributes="0">
-                  <Component id="buttonClose" alignment="3" min="-2" max="-2" attributes="0"/>
                   <Component id="buttonApply" alignment="3" min="-2" max="-2" attributes="0"/>
+                  <Component id="buttonClose" alignment="3" min="-2" max="-2" attributes="0"/>
               </Group>
               <EmptySpace max="-2" attributes="0"/>
           </Group>
@@ -86,6 +89,7 @@
     </Component>
     <Component class="javax.swing.JButton" name="buttonClose">
       <Properties>
+        <Property name="mnemonic" type="int" value="67"/>
         <Property name="text" type="java.lang.String" value="Close"/>
       </Properties>
       <Events>
     </Component>
     <Component class="javax.swing.JButton" name="buttonApply">
       <Properties>
+        <Property name="mnemonic" type="int" value="97"/>
         <Property name="text" type="java.lang.String" value="Apply"/>
       </Properties>
       <Events>
         <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="buttonApplyActionPerformed"/>
       </Events>
     </Component>
+    <Component class="javax.swing.JButton" name="jButton1">
+      <Properties>
+        <Property name="text" type="java.lang.String" value="Create..."/>
+      </Properties>
+      <Events>
+        <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="jButton1ActionPerformed"/>
+      </Events>
+    </Component>
   </SubComponents>
 </Form>
index c28c82d..763361e 100644 (file)
@@ -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
index 73efc94..b8b43d4 100644 (file)
@@ -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());
index 2d2caca..6ca96e5 100644 (file)
@@ -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");
     }