OSDN Git Service

永続化処理を追加 tags/release_20090111@30 tags/release_20090524@30
authoryuki <yuki@bdf3b611-c98c-6041-8292-703d9c9adbe7>
Sat, 23 Feb 2008 12:45:08 +0000 (12:45 +0000)
committeryuki <yuki@bdf3b611-c98c-6041-8292-703d9c9adbe7>
Sat, 23 Feb 2008 12:45:08 +0000 (12:45 +0000)
git-svn-id: http://192.168.11.7/svn/repository/NicoBrowserBranches/jpa/NicoBrowser@30 bdf3b611-c98c-6041-8292-703d9c9adbe7

createDDL.jdbc [new file with mode: 0644]
dropDDL.jdbc [new file with mode: 0644]
nbproject/project.properties
src/META-INF/persistence.xml [new file with mode: 0644]
src/nicobrowser/NicoContent.java
src/nicobrowser/NicoHttpClient.java
src/nicobrowser/NicoTableModel.java [deleted file]
test/nicobrowser/NicoHttpClientTest.java

diff --git a/createDDL.jdbc b/createDDL.jdbc
new file mode 100644 (file)
index 0000000..33038a9
--- /dev/null
@@ -0,0 +1,3 @@
+CREATE TABLE NICOCONTENT (ID NUMBER(19) NOT NULL, FILENAME VARCHAR(255), PAGELINK VARCHAR(255), TITLE VARCHAR(255), NICOID VARCHAR(255), VERSION NUMBER(19), PRIMARY KEY (ID))
+CREATE TABLE SEQUENCE (SEQ_NAME VARCHAR(50) NOT NULL, SEQ_COUNT NUMBER(19), PRIMARY KEY (SEQ_NAME))
+INSERT INTO SEQUENCE(SEQ_NAME, SEQ_COUNT) values ('SEQ_GEN', 1)
diff --git a/dropDDL.jdbc b/dropDDL.jdbc
new file mode 100644 (file)
index 0000000..62fa666
--- /dev/null
@@ -0,0 +1,2 @@
+DROP TABLE NICOCONTENT
+DELETE FROM SEQUENCE WHERE SEQ_NAME = 'SEQ_GEN'
index bfc26b1..9e92ad6 100644 (file)
@@ -25,7 +25,9 @@ javac.classpath=\
     ${libs.HttpClient3.classpath}:\\r
     ${libs.Log4J.classpath}:\\r
     ${libs.Codec.classpath}:\\r
-    ${libs.Rome.classpath}\r
+    ${libs.Rome.classpath}:\\r
+    ${libs.H2_DB.classpath}:\\r
+    ${libs.toplink.classpath}\r
 # Space-separated list of extra javac options\r
 javac.compilerargs=\r
 javac.deprecation=false\r
@@ -54,6 +56,9 @@ jnlp.signed=true
 # Property libs.Codec.classpath is set here just to make sharing of project simpler.\r
 # The library definition has always preference over this property.\r
 libs.Codec.classpath=../../../java/commons/commons-codec-1.3/commons-codec-1.3.jar\r
+# Property libs.H2_DB.classpath is set here just to make sharing of project simpler.\r
+# The library definition has always preference over this property.\r
+libs.H2_DB.classpath=../../../java/h2-2008-02-02/h2/bin/h2.jar\r
 # Property libs.HttpClient3.classpath is set here just to make sharing of project simpler.\r
 # The library definition has always preference over this property.\r
 libs.HttpClient3.classpath=../../../java/commons/commons-httpclient-3.1/commons-httpclient-3.1.jar\r
diff --git a/src/META-INF/persistence.xml b/src/META-INF/persistence.xml
new file mode 100644 (file)
index 0000000..a7042ba
--- /dev/null
@@ -0,0 +1,14 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
+  <persistence-unit name="NicoBrowserPU" transaction-type="RESOURCE_LOCAL">
+    <provider>oracle.toplink.essentials.PersistenceProvider</provider>
+    <class>nicobrowser.NicoContent</class>
+    <properties>
+      <property name="toplink.jdbc.user" value="sa"/>
+      <property name="toplink.jdbc.password" value=""/>
+      <property name="toplink.jdbc.url" value="jdbc:h2:db/NicoDB"/>
+      <property name="toplink.jdbc.driver" value="org.h2.Driver"/>
+      <property name="toplink.ddl-generation" value="drop-and-create-tables"/>
+    </properties>
+  </persistence-unit>
+</persistence>
index c71d6d2..6b2ca6f 100644 (file)
@@ -1,13 +1,52 @@
 /*$Id$*/
 package nicobrowser;
 
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+import javax.persistence.Version;
+
 /**
  * ニコニコ動画コンテンツ情報.
  */
-public class NicoContent {
+@Entity
+public class NicoContent implements java.io.Serializable {
 
+    private static final long serialVersionUID = 1L;
+    private Long id;
+    private Long version;
+    private String nicoId;
     private String pageLink;
     private String title;
+    private String fileName;
+
+    @Id
+    @GeneratedValue(strategy = GenerationType.AUTO)
+    public Long getId() {
+        return id;
+    }
+
+    public void setId(Long id) {
+        this.id = id;
+    }
+
+    @Version
+    public Long getVersion() {
+        return version;
+    }
+
+    public void setVersion(Long version) {
+        this.version = version;
+    }
+
+    public String getNicoId() {
+        return nicoId;
+    }
+
+    protected void setNicoId(String nicoId) {
+        this.nicoId = nicoId;
+    }
 
     public String getPageLink() {
         return pageLink;
@@ -15,6 +54,8 @@ public class NicoContent {
 
     public void setPageLink(String pageLink) {
         this.pageLink = pageLink;
+        String[] elm = pageLink.split("/");
+        setNicoId(elm[elm.length - 1]);
     }
 
     public String getTitle() {
@@ -23,6 +64,52 @@ public class NicoContent {
 
     public void setTitle(String title) {
         this.title = title;
+        setFileName(title);
+    }
+
+    public String getFileName() {
+        return fileName;
+    }
+
+    protected void setFileName(String fileName) {
+        if (fileName == null) {
+            this.fileName = null;
+            return;
+        }
+
+        StringBuilder str = new StringBuilder();
+        try {
+            for (int i = 0; i < fileName.length(); i++) {
+                char c = fileName.charAt(i);
+                if (c == '\\' || c == '/' || c == ':' || c == '*' || c == '?' || c == '"' || c == '<' || c == '>' || c == '|') {
+                    c = '_';
+                }
+                str.append(c);
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        this.fileName = str.toString();
+    }
+
+    @Override
+    public int hashCode() {
+        int hash = 0;
+        hash += (id != null ? id.hashCode() : 0);
+        return hash;
+    }
+
+    @Override
+    public boolean equals(Object object) {
+        // TODO: Warning - this method won't work in the case the id fields are not set
+        if (!(object instanceof NicoContent)) {
+            return false;
+        }
+        NicoContent other = (NicoContent) object;
+        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
+            return false;
+        }
+        return true;
     }
 
     @Override
index 9b48916..b598bbf 100644 (file)
@@ -161,7 +161,7 @@ public class NicoHttpClient extends HttpClient {
             Reader reader = new BufferedReader(new InputStreamReader(get.getResponseBodyAsStream(), "UTF-8"));
             contList = getNicoContents(reader);
         } catch (FeedException ex) {
-            Logger.getLogger(NicoHttpClient.class.getName()).log(Level.SEVERE, null, ex);
+            Logger.getLogger(NicoHttpClient.class.getName()).log(Level.SEVERE, null, "FeedExceitpion" + listNo);
         } catch (IOException ex) {
             Logger.getLogger(NicoHttpClient.class.getName()).log(Level.SEVERE, null, ex);
         } finally {
diff --git a/src/nicobrowser/NicoTableModel.java b/src/nicobrowser/NicoTableModel.java
deleted file mode 100644 (file)
index aa8345c..0000000
+++ /dev/null
@@ -1,91 +0,0 @@
-/*$Id$*/
-package nicobrowser;
-
-import java.awt.Component;
-import java.util.logging.Level;
-import java.util.logging.Logger;
-import javax.swing.ImageIcon;
-import javax.swing.JButton;
-import javax.swing.JTable;
-import javax.swing.JTextArea;
-import javax.swing.table.DefaultTableCellRenderer;
-import javax.swing.table.DefaultTableModel;
-import javax.swing.table.TableColumn;
-
-/**
- *
- * @author yuki
- */
-public class NicoTableModel extends DefaultTableModel {
-
-    private static final ColumnContext[] COLUMN_CONTEXT = {
-        new ColumnContext("サムネイル", ImageIcon.class),
-        new ColumnContext("タイトル", String.class),
-        new ColumnContext("説明", JTextArea.class),
-        new ColumnContext("リンク", JButton.class)
-    };
-
-    private static class ColumnContext {
-
-        private String columnName;
-        private Class renderer;
-
-        private ColumnContext(String columnName, Class renderer) {
-            this.columnName = columnName;
-            this.renderer = renderer;
-        }
-    }
-
-    static class MyCellRender extends DefaultTableCellRenderer {
-
-        @Override
-        public Component getTableCellRendererComponent(
-                JTable table, Object value,
-                boolean isSelected, boolean hasFocus, int row, int column) {
-            if (isSelected) {
-                table.setForeground(table.getSelectionForeground());
-                table.setBackground(table.getSelectionBackground());
-            } else {
-                table.setForeground(table.getForeground());
-                table.setBackground(table.getBackground());
-            }
-
-            //setFont(table.getFont());
-
-            try {
-                Object obj = COLUMN_CONTEXT[column].renderer.newInstance();
-
-
-                return (Component) obj;
-            } catch (InstantiationException ex) {
-                Logger.getLogger(NicoTableModel.class.getName()).log(Level.SEVERE, null, ex);
-            } catch (IllegalAccessException ex) {
-                Logger.getLogger(NicoTableModel.class.getName()).log(Level.SEVERE, null, ex);
-            }
-            return null;
-
-
-        }
-    }
-
-    public NicoTableModel() {
-        super();
-        for (ColumnContext c : COLUMN_CONTEXT) {
-            addColumn(c.columnName);
-        }
-        TableColumn column;
-
-
-    }
-
-    @Override
-    public Class<?> getColumnClass(int columnIndex) {
-        return COLUMN_CONTEXT[columnIndex].renderer;
-
-    }
-
-    @Override
-    public boolean isCellEditable(int rowIndex, int columnIndex) {
-        return false;
-    }
-}
index 294bc94..f241556 100644 (file)
@@ -2,6 +2,10 @@
 package nicobrowser;
 
 import java.util.List;
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.EntityTransaction;
+import javax.persistence.Persistence;
 import org.junit.After;
 import org.junit.AfterClass;
 import org.junit.Before;
@@ -108,7 +112,6 @@ public class NicoHttpClientTest {
     public void loadMyListDaily() {
         System.out.println("loadMyListDaily");
         NicoHttpClient instance = NicoHttpClient.getInstance();
-        instance.login(OK_MAIL, OK_PASS);
         List<NicoContent> list = instance.loadMyListDaily();
         assertNotNull(list);
         assertNotSame(0, list.size());
@@ -116,5 +119,30 @@ public class NicoHttpClientTest {
 //        for (Object o : list) {
 //            System.out.println(o.toString());
 //        }
+
+        EntityManagerFactory factory;
+        EntityManager manager;
+
+        factory = Persistence.createEntityManagerFactory("NicoBrowserPU");
+        manager = factory.createEntityManager();
+
+        EntityTransaction transaction = manager.getTransaction();
+
+        transaction.begin();
+        try {
+            for (NicoContent c : list) {
+                manager.persist(c);
+            }
+
+            transaction.commit();
+            System.out.println("2つのPersonオブジェクトを永続化しました.");
+        } catch (Exception ex) {
+            ex.printStackTrace();
+            transaction.rollback();
+        } finally {
+            manager.close();
+            factory.close();
+        }
+
     }
 }