From 6d856b912f3577a91a944b17db29294864b023e8 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Wed, 30 Jul 2008 13:59:32 +0000 Subject: Client: Completed "Add Subscription" form Fixed the Add Subscription form so that it downloads, parses and adds a Subscription if you give it the correct URL. Also synchronized all of the Database-accessing methods in Database.Wrapper because before the database was deadlocking all of the time. --- .../fourisland/instadisc/AddSubscriptionForm.form | 6 +- .../fourisland/instadisc/AddSubscriptionForm.java | 10 +- .../com/fourisland/instadisc/Database/Wrapper.java | 252 +++++++++++---------- .../src/com/fourisland/instadisc/InstaDiscApp.java | 14 +- .../com/fourisland/instadisc/InstaDiscView.java | 3 +- .../instadisc/Item/Categories/Category.java | 4 +- .../src/com/fourisland/instadisc/Item/Item.java | 2 +- .../instadisc/Item/SubscriptionFile.java | 119 ++++++++++ 8 files changed, 280 insertions(+), 130 deletions(-) create mode 100644 client/trunk/src/com/fourisland/instadisc/Item/SubscriptionFile.java diff --git a/client/trunk/src/com/fourisland/instadisc/AddSubscriptionForm.form b/client/trunk/src/com/fourisland/instadisc/AddSubscriptionForm.form index 3435493..91459ed 100644 --- a/client/trunk/src/com/fourisland/instadisc/AddSubscriptionForm.form +++ b/client/trunk/src/com/fourisland/instadisc/AddSubscriptionForm.form @@ -28,11 +28,11 @@ - - + + - + diff --git a/client/trunk/src/com/fourisland/instadisc/AddSubscriptionForm.java b/client/trunk/src/com/fourisland/instadisc/AddSubscriptionForm.java index 149bea1..fb05037 100644 --- a/client/trunk/src/com/fourisland/instadisc/AddSubscriptionForm.java +++ b/client/trunk/src/com/fourisland/instadisc/AddSubscriptionForm.java @@ -6,10 +6,9 @@ package com.fourisland.instadisc; +import com.fourisland.instadisc.Item.SubscriptionFile; import java.net.MalformedURLException; import java.net.URL; -import java.util.logging.Level; -import java.util.logging.Logger; /** * @@ -80,11 +79,11 @@ public class AddSubscriptionForm extends javax.swing.JDialog { .addComponent(jLabel1) .addGroup(layout.createSequentialGroup() .addGap(12, 12, 12) - .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jLabel3) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) - .addComponent(jTextField1)) + .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 214, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(jLabel2))) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) @@ -119,8 +118,9 @@ public class AddSubscriptionForm extends javax.swing.JDialog { private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed try { URL url = new URL(jTextField1.getText());//GEN-LAST:event_jButton2ActionPerformed + SubscriptionFile sf = new SubscriptionFile(url, jLabel4); } catch (MalformedURLException ex) { - jLabel4.setText("Error: "); + jLabel4.setText("Error: Subscription URL is malformed"); } } diff --git a/client/trunk/src/com/fourisland/instadisc/Database/Wrapper.java b/client/trunk/src/com/fourisland/instadisc/Database/Wrapper.java index 13623c0..57ec799 100644 --- a/client/trunk/src/com/fourisland/instadisc/Database/Wrapper.java +++ b/client/trunk/src/com/fourisland/instadisc/Database/Wrapper.java @@ -36,7 +36,7 @@ public class Wrapper { envConfig.setAllowCreate(true); esConfig.setAllowCreate(true); try { - e = new Environment(new File(loc + "db"), envConfig); + e = new Environment(new File(loc), envConfig); es = new EntityStore(e, "EntityStore", esConfig); } catch (DatabaseException ex) { Logger.getLogger(Wrapper.class.getName()).log(Level.SEVERE, null, ex); @@ -54,37 +54,38 @@ public class Wrapper { Logger.getLogger(Wrapper.class.getName()).log(Level.SEVERE, null, ex); } } - - public static String getConfig(String key) - { - try { - return idConfig.get(key).getValue(); - } catch (DatabaseException ex) { - Logger.getLogger(Wrapper.class.getName()).log(Level.SEVERE, null, ex); - return ""; + + public static String getConfig(String key) { + synchronized (idConfig) { + try { + return idConfig.get(key).getValue(); + } catch (DatabaseException ex) { + Logger.getLogger(Wrapper.class.getName()).log(Level.SEVERE, null, ex); + return ""; + } } } - - public static void setConfig(String key, String value) - { - try { - if (idConfig.contains(key)) { - IDConfig temp = idConfig.get(key); - temp.setValue(value); - idConfig.put(temp); - } else { - IDConfig temp = new IDConfig(); - temp.setKey(key); - temp.setValue(value); - idConfig.put(temp); + + public static void setConfig(String key, String value) { + synchronized (idConfig) { + try { + if (idConfig.contains(key)) { + IDConfig temp = idConfig.get(key); + temp.setValue(value); + idConfig.put(temp); + } else { + IDConfig temp = new IDConfig(); + temp.setKey(key); + temp.setValue(value); + idConfig.put(temp); + } + } catch (DatabaseException ex) { + Logger.getLogger(Wrapper.class.getName()).log(Level.SEVERE, null, ex); } - } catch (DatabaseException ex) { - Logger.getLogger(Wrapper.class.getName()).log(Level.SEVERE, null, ex); } } - - public static boolean containsOldVerID(Integer id) - { + + public static boolean containsOldVerID(Integer id) { try { return oldVerID.contains(id); } catch (DatabaseException ex) { @@ -92,117 +93,134 @@ public class Wrapper { return false; } } - - public static int countOldVerID() - { - try { - return (int) oldVerID.count(); - } catch (DatabaseException ex) { - Logger.getLogger(Wrapper.class.getName()).log(Level.SEVERE, null, ex); - return 0; + + public static int countOldVerID() { + synchronized (oldVerID) { + try { + return (int) oldVerID.count(); + } catch (DatabaseException ex) { + Logger.getLogger(Wrapper.class.getName()).log(Level.SEVERE, null, ex); + return 0; + } } } - - public static void emptyOldVerID() - { - try { - EntityCursor ec = oldVerID.entities(); + + public static void emptyOldVerID() { + synchronized (oldVerID) { try { - Iterator i = ec.iterator(); - while (i.hasNext()) { - i.remove(); + EntityCursor ec = oldVerID.entities(); + try { + Iterator i = ec.iterator(); + while (i.hasNext()) { + i.remove(); + } + } finally { + ec.close(); } - } finally { - ec.close(); + } catch (DatabaseException ex) { + Logger.getLogger(Wrapper.class.getName()).log(Level.SEVERE, null, ex); } - } catch (DatabaseException ex) { - Logger.getLogger(Wrapper.class.getName()).log(Level.SEVERE, null, ex); } } - - public static void addOldVerID(Integer id) - { - try { - OldVerID temp = new OldVerID(); - temp.setID(id); - oldVerID.put(temp); - } catch (DatabaseException ex) { - Logger.getLogger(Wrapper.class.getName()).log(Level.SEVERE, null, ex); + + public static void addOldVerID(Integer id) { + synchronized (oldVerID) { + try { + OldVerID temp = new OldVerID(); + temp.setID(id); + oldVerID.put(temp); + } catch (DatabaseException ex) { + Logger.getLogger(Wrapper.class.getName()).log(Level.SEVERE, null, ex); + } } } - - public static void addItem(Item m_item) - { - try { - item.put(m_item); - } catch (DatabaseException ex) { - Logger.getLogger(Wrapper.class.getName()).log(Level.SEVERE, null, ex); + + public static void addItem(Item m_item) { + synchronized (item) { + try { + item.put(m_item); + } catch (DatabaseException ex) { + Logger.getLogger(Wrapper.class.getName()).log(Level.SEVERE, null, ex); + } } } - - public static int countItem() - { - try { - return (int) item.count(); - } catch (DatabaseException ex) { - Logger.getLogger(Wrapper.class.getName()).log(Level.SEVERE, null, ex); - return 0; + + public static int countItem() { + synchronized (item) { + try { + return (int) item.count(); + } catch (DatabaseException ex) { + Logger.getLogger(Wrapper.class.getName()).log(Level.SEVERE, null, ex); + return 0; + } } } - - public static void dropFromTopItem() - { - try { - Integer[] keySet = (Integer[]) item.map().keySet().toArray(); - item.delete(keySet[0]); - } catch (DatabaseException ex) { - Logger.getLogger(Wrapper.class.getName()).log(Level.SEVERE, null, ex); + + public static void dropFromTopItem() { + synchronized (item) { + try { + Integer[] keySet = (Integer[]) item.map().keySet().toArray(); + item.delete(keySet[0]); + } catch (DatabaseException ex) { + Logger.getLogger(Wrapper.class.getName()).log(Level.SEVERE, null, ex); + } } } - - public static Item[] getAllItem() - { - try { - Iterator i = item.entities().iterator(); - Item[] temp = new Item[0]; - int len = 0; - - while (i.hasNext()) - { - Item[] temp2 = new Item[len+1]; - int j=0; - for (j=0;j i = item.entities().iterator(); + Item[] temp = new Item[0]; + int len = 0; + + while (i.hasNext()) { + Item[] temp2 = new Item[len + 1]; + int j = 0; + for (j = 0; j < len; j++) { + temp2[j] = temp[j]; + } + temp2[len] = i.next(); + temp = temp2; } - temp2[len] = i.next(); - temp = temp2; + + return temp; + } catch (DatabaseException ex) { + Logger.getLogger(Wrapper.class.getName()).log(Level.SEVERE, null, ex); + return new Item[0]; } - - return temp; - } catch (DatabaseException ex) { - Logger.getLogger(Wrapper.class.getName()).log(Level.SEVERE, null, ex); - return new Item[0]; } } - - public static Subscription getSubscription(String url) - { - try { - return subscription.get(url); - } catch (DatabaseException ex) { - Logger.getLogger(Wrapper.class.getName()).log(Level.SEVERE, null, ex); - return null; + + public static Subscription getSubscription(String url) { + synchronized (subscription) { + try { + return subscription.get(url); + } catch (DatabaseException ex) { + Logger.getLogger(Wrapper.class.getName()).log(Level.SEVERE, null, ex); + return null; + } } } - - public static boolean existsSubscription(String url) - { - try { - return subscription.contains(url); - } catch (DatabaseException ex) { - Logger.getLogger(Wrapper.class.getName()).log(Level.SEVERE, null, ex); - return false; + + public static boolean existsSubscription(String url) { + synchronized (subscription) { + try { + return subscription.contains(url); + } catch (DatabaseException ex) { + Logger.getLogger(Wrapper.class.getName()).log(Level.SEVERE, null, ex); + return false; + } + } + } + + public static void addSubscription(Subscription s) { + synchronized (subscription) { + try { + subscription.put(s); + } catch (DatabaseException ex) { + Logger.getLogger(Wrapper.class.getName()).log(Level.SEVERE, null, ex); + } } } } diff --git a/client/trunk/src/com/fourisland/instadisc/InstaDiscApp.java b/client/trunk/src/com/fourisland/instadisc/InstaDiscApp.java index dffb663..e891d32 100644 --- a/client/trunk/src/com/fourisland/instadisc/InstaDiscApp.java +++ b/client/trunk/src/com/fourisland/instadisc/InstaDiscApp.java @@ -16,7 +16,7 @@ import org.jdesktop.application.SingleFrameApplication; public class InstaDiscApp extends SingleFrameApplication { public static TrayIcon ti; - + /** * At startup create and show the main frame of the application. */ @@ -52,7 +52,17 @@ public class InstaDiscApp extends SingleFrameApplication { } Wrapper.init(db.getAbsolutePath()); - if (!Wrapper.getConfig("initCheck").equals("done")) { + + boolean notInit = false; + try { + if (!Wrapper.getConfig("initCheck").equals("done")) { + notInit = true; + } + } catch (NullPointerException ex) { + notInit = true; + } + + if (notInit) { Thread th = new Thread(new FirstRunWizard()); th.start(); } else { diff --git a/client/trunk/src/com/fourisland/instadisc/InstaDiscView.java b/client/trunk/src/com/fourisland/instadisc/InstaDiscView.java index d9a6c8a..36b2375 100644 --- a/client/trunk/src/com/fourisland/instadisc/InstaDiscView.java +++ b/client/trunk/src/com/fourisland/instadisc/InstaDiscView.java @@ -285,7 +285,8 @@ public class InstaDiscView extends FrameView { }//GEN-LAST:event_jList1MouseClicked private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed - // TODO add your handling code here: + AddSubscriptionForm asf = new AddSubscriptionForm(new JFrame(), true); + asf.setVisible(true); }//GEN-LAST:event_jMenuItem1ActionPerformed // Variables declaration - do not modify//GEN-BEGIN:variables diff --git a/client/trunk/src/com/fourisland/instadisc/Item/Categories/Category.java b/client/trunk/src/com/fourisland/instadisc/Item/Categories/Category.java index 29914b9..1bb0c23 100644 --- a/client/trunk/src/com/fourisland/instadisc/Item/Categories/Category.java +++ b/client/trunk/src/com/fourisland/instadisc/Item/Categories/Category.java @@ -5,6 +5,7 @@ package com.fourisland.instadisc.Item.Categories; +import com.fourisland.instadisc.Database.Wrapper; import com.fourisland.instadisc.Item.WellFormedItem; import java.util.HashMap; import javax.swing.Icon; @@ -30,7 +31,8 @@ public class Category { public static boolean checkForRequiredSemantics(HashMap headerMap) { boolean good = true; - if (headerMap.get("Category").equals("forum-post")) { + String category = Wrapper.getSubscription(headerMap.get("Subscription")).getCategory(); + if (category.equals("forum-post")) { good = (good ? WellFormedItem.checkForRequiredHeader(headerMap, "forum") : false); } return good; diff --git a/client/trunk/src/com/fourisland/instadisc/Item/Item.java b/client/trunk/src/com/fourisland/instadisc/Item/Item.java index 71e6f06..5ed6fe9 100644 --- a/client/trunk/src/com/fourisland/instadisc/Item/Item.java +++ b/client/trunk/src/com/fourisland/instadisc/Item/Item.java @@ -48,7 +48,7 @@ public class Item { di.setAuthor(headerMap.get("Author")); di.setURL(new URL(headerMap.get("URL")).toString()); - HashMap temp = headerMap; + HashMap temp = (HashMap) headerMap.clone(); temp.remove("ID"); temp.remove("Verification"); temp.remove("Verification-ID"); diff --git a/client/trunk/src/com/fourisland/instadisc/Item/SubscriptionFile.java b/client/trunk/src/com/fourisland/instadisc/Item/SubscriptionFile.java new file mode 100644 index 0000000..adca86e --- /dev/null +++ b/client/trunk/src/com/fourisland/instadisc/Item/SubscriptionFile.java @@ -0,0 +1,119 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package com.fourisland.instadisc.Item; + +import com.fourisland.instadisc.Database.Subscription; +import com.fourisland.instadisc.Database.Wrapper; +import java.io.IOException; +import java.io.InputStream; +import java.net.HttpURLConnection; +import java.net.URL; +import java.util.HashMap; +import java.util.logging.Level; +import java.util.logging.Logger; +import javax.swing.JLabel; + +/** + * + * @author hatkirby + */ +public class SubscriptionFile { + + public HashMap headerMap; + + public SubscriptionFile(URL url, JLabel status) { + status.setText("Checking...."); + try { + HttpURLConnection urc = (HttpURLConnection) url.openConnection(); + Thread th = new Thread(new SubscriptionFileThread(urc, status)); + th.start(); + } catch (IOException ex) { + Logger.getLogger(SubscriptionFile.class.getName()).log(Level.SEVERE, null, ex); + } + } +} + +class SubscriptionFileThread implements Runnable { + + HttpURLConnection urc; + JLabel status; + + public SubscriptionFileThread(HttpURLConnection urc, JLabel status) { + this.urc = urc; + this.status = status; + } + + public void run() { + InputStream is = null; + try { + is = urc.getInputStream(); + int[] buffer = new int[1000]; + int rs = 0; + int i = 0; + + while (rs != -1) { + try { + rs = is.read(); + + if (rs != -1) { + buffer[i] = rs; + } + i++; + } catch (IOException ex) { + Logger.getLogger(SubscriptionFileThread.class.getName()).log(Level.SEVERE, null, ex); + } + } + + StringBuilder result = new StringBuilder(); + int j = 0; + for (j = 0; j < i; j++) { + result.append(Character.toString((char) buffer[j])); + } + + String[] headers = result.toString().split("\n"); + HashMap headerMap = new HashMap(); + i = 0; + while (1 == 1) { + try { + String[] nameVal = headers[i].split(": "); + String name = nameVal[0]; + String value = nameVal[1].trim(); + headerMap.put(name, value); + } catch (Exception ex) { + break; + } + i++; + } + + if (headerMap.containsKey("Subscription")) { + if (headerMap.containsKey("Title")) { + if (headerMap.containsKey("Category")) { + Subscription s = new Subscription(); + s.setURL(headerMap.get("Subscription")); + s.setTitle(headerMap.get("Title")); + s.setCategory(headerMap.get("Category")); + Wrapper.addSubscription(s); + + status.setText("You've sucessfully subscribed to that website"); + } else { + status.setText("Error: Subscription file is not well-formed"); + } + } else { + status.setText("Error: Subscription file is not well-formed"); + } + } else { + status.setText("Error: Subscription file is not well-formed"); + } + } catch (IOException ex) { + Logger.getLogger(SubscriptionFileThread.class.getName()).log(Level.SEVERE, null, ex); + } finally { + try { + is.close(); + } catch (IOException ex) { + Logger.getLogger(SubscriptionFileThread.class.getName()).log(Level.SEVERE, null, ex); + } + } + } +} -- cgit 1.4.1