From acd07b0d1c7240c5d3870bb2f868c692fca1fc1b Mon Sep 17 00:00:00 2001 From: yuki Date: Sat, 23 Feb 2008 12:45:08 +0000 Subject: [PATCH] =?utf8?q?=E6=B0=B8=E7=B6=9A=E5=8C=96=E5=87=A6=E7=90=86?= =?utf8?q?=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit git-svn-id: http://192.168.11.7/svn/repository/NicoBrowserBranches/jpa/NicoBrowser@30 bdf3b611-c98c-6041-8292-703d9c9adbe7 --- createDDL.jdbc | 3 ++ dropDDL.jdbc | 2 + nbproject/project.properties | 7 ++- src/META-INF/persistence.xml | 14 +++++ src/nicobrowser/NicoContent.java | 89 ++++++++++++++++++++++++++++++- src/nicobrowser/NicoHttpClient.java | 2 +- src/nicobrowser/NicoTableModel.java | 91 -------------------------------- test/nicobrowser/NicoHttpClientTest.java | 30 ++++++++++- 8 files changed, 143 insertions(+), 95 deletions(-) create mode 100644 createDDL.jdbc create mode 100644 dropDDL.jdbc create mode 100644 src/META-INF/persistence.xml delete mode 100644 src/nicobrowser/NicoTableModel.java diff --git a/createDDL.jdbc b/createDDL.jdbc new file mode 100644 index 0000000..33038a9 --- /dev/null +++ b/createDDL.jdbc @@ -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 index 0000000..62fa666 --- /dev/null +++ b/dropDDL.jdbc @@ -0,0 +1,2 @@ +DROP TABLE NICOCONTENT +DELETE FROM SEQUENCE WHERE SEQ_NAME = 'SEQ_GEN' diff --git a/nbproject/project.properties b/nbproject/project.properties index bfc26b1..9e92ad6 100644 --- a/nbproject/project.properties +++ b/nbproject/project.properties @@ -25,7 +25,9 @@ javac.classpath=\ ${libs.HttpClient3.classpath}:\ ${libs.Log4J.classpath}:\ ${libs.Codec.classpath}:\ - ${libs.Rome.classpath} + ${libs.Rome.classpath}:\ + ${libs.H2_DB.classpath}:\ + ${libs.toplink.classpath} # Space-separated list of extra javac options javac.compilerargs= javac.deprecation=false @@ -54,6 +56,9 @@ jnlp.signed=true # Property libs.Codec.classpath is set here just to make sharing of project simpler. # The library definition has always preference over this property. libs.Codec.classpath=../../../java/commons/commons-codec-1.3/commons-codec-1.3.jar +# Property libs.H2_DB.classpath is set here just to make sharing of project simpler. +# The library definition has always preference over this property. +libs.H2_DB.classpath=../../../java/h2-2008-02-02/h2/bin/h2.jar # Property libs.HttpClient3.classpath is set here just to make sharing of project simpler. # The library definition has always preference over this property. libs.HttpClient3.classpath=../../../java/commons/commons-httpclient-3.1/commons-httpclient-3.1.jar diff --git a/src/META-INF/persistence.xml b/src/META-INF/persistence.xml new file mode 100644 index 0000000..a7042ba --- /dev/null +++ b/src/META-INF/persistence.xml @@ -0,0 +1,14 @@ + + + + oracle.toplink.essentials.PersistenceProvider + nicobrowser.NicoContent + + + + + + + + + diff --git a/src/nicobrowser/NicoContent.java b/src/nicobrowser/NicoContent.java index c71d6d2..6b2ca6f 100644 --- a/src/nicobrowser/NicoContent.java +++ b/src/nicobrowser/NicoContent.java @@ -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 diff --git a/src/nicobrowser/NicoHttpClient.java b/src/nicobrowser/NicoHttpClient.java index 9b48916..b598bbf 100644 --- a/src/nicobrowser/NicoHttpClient.java +++ b/src/nicobrowser/NicoHttpClient.java @@ -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 index aa8345c..0000000 --- a/src/nicobrowser/NicoTableModel.java +++ /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; - } -} diff --git a/test/nicobrowser/NicoHttpClientTest.java b/test/nicobrowser/NicoHttpClientTest.java index 294bc94..f241556 100644 --- a/test/nicobrowser/NicoHttpClientTest.java +++ b/test/nicobrowser/NicoHttpClientTest.java @@ -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 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(); + } + } } -- 2.11.0