OSDN Git Service

Apache Luceneを使った日本語検索のために、NoteテーブルにcontentTextカラムを追加。
authoryuki <kimaira7@gmail.com>
Fri, 7 Jun 2013 09:37:47 +0000 (18:37 +0900)
committeryuki <kimaira7@gmail.com>
Fri, 7 Jun 2013 09:37:47 +0000 (18:37 +0900)
src/cx/fbn/nevernote/Global.java
src/cx/fbn/nevernote/gui/RensoNoteListItem.java
src/cx/fbn/nevernote/sql/DatabaseConnection.java
src/cx/fbn/nevernote/sql/NoteTable.java

index de2bb02..739c45c 100644 (file)
@@ -2322,5 +2322,15 @@ public class Global {
                settings.endGroup();
                return value;
        }
+       
+       // タグを排除してプレーンテキストを抽出
+       public static String extractPlainText(String sourceText) {
+               String plainText = sourceText.replaceAll("<.+?>", "");
+               plainText = plainText.replaceAll("\\s{2,}", " ");
+               String kaigyo = System.getProperty("line.separator");
+               plainText = plainText.replaceAll(kaigyo, "");
+               
+               return plainText;
+       }
 }
 
index 294495b..e0842f6 100644 (file)
@@ -48,7 +48,7 @@ public class RensoNoteListItem extends QWidget{
        private final int relationPoints;
        private final String noteCreated;
        private final String tagNames;
-       private String noteContent;
+       private final String noteContent;
        private final RensoNoteList parent;
        private final boolean isStared;
        private final int allPointSum;
@@ -81,12 +81,7 @@ public class RensoNoteListItem extends QWidget{
                this.tagNames = new String(sb);
                
                // this.noteContent = new String(note.getContent());
-               this.noteContent = conn.getNoteTable().getNoteContentNoUTFConversion(note.getGuid());
-               this.noteContent = this.noteContent.replaceAll("<.+?>", "");
-               this.noteContent = this.noteContent.replaceAll("\\s{2,}", " ");
-               String kaigyo = System.getProperty("line.separator");
-               this.noteContent = this.noteContent.replaceAll(kaigyo, "");
-               
+               this.noteContent = Global.extractPlainText(conn.getNoteTable().getNoteContentNoUTFConversion(note.getGuid()));
                palette = new QPalette();
                palette.setColor(QPalette.ColorRole.Window, new QColor(255, 255, 255));
                this.setPalette(palette);
index ef314b5..a15330a 100644 (file)
@@ -290,6 +290,24 @@ public class DatabaseConnection {
                        executeSql("alter table note add column attributeContentClass VarChar");
                        executeSql("update note set attributeContentClass = ''");
                }
+               
+               // Apache Luceneを使った日本語検索のためのプレーンテキストノートコンテンツカラムを準備
+               if (!dbTableColumnExists("NOTE", "CONTENTTEXT")) {
+                       executeSql("alter table note add column contentText VarChar");
+                       executeSql("update note set contentText = ''");
+                       NSqlQuery query = new NSqlQuery(conn);
+                       query.exec("Select guid, content from Note where contentText = ''");
+                       while (query.next()) {
+                               String guid = query.valueString(0);
+                               String content = query.valueString(1);
+                               String contentText = Global.extractPlainText(content);
+                               NSqlQuery query2 = new NSqlQuery(conn);
+                               query2.prepare("update note set contentText=:contentText where guid=:guid");
+                               query2.bindValue(":contentText", contentText);
+                               query2.bindValue(":guid", guid);
+                               query2.exec();
+                       }
+               }
        }
        
        public void executeSql(String sql) {
index 0710817..181d222 100644 (file)
@@ -111,13 +111,13 @@ public class NoteTable {
                                +"contentHash, contentLength, created, updated, deleted, active, notebookGuid, "
                                +"attributeSubjectDate, attributeLatitude, attributeLongitude, attributeAltitude, "
                                +"attributeAuthor, attributeSource, attributeSourceUrl, attributeSourceApplication, "
-                               +"indexNeeded, isExpunged, isDirty, titlecolor, thumbnailneeded" 
+                               +"indexNeeded, isExpunged, isDirty, titlecolor, thumbnailneeded, contentText
                                +") Values("
                                +":guid, :updateSequenceNumber, :title, :content, "
                                +":contentHash, :contentLength, :created, :updated, :deleted, :active, :notebookGuid, "
                                +":attributeSubjectDate, :attributeLatitude, :attributeLongitude, :attributeAltitude, "
                                +":attributeAuthor, :attributeSource, :attributeSourceUrl, :attributeSourceApplication, "
-                               +":indexNeeded, :isExpunged, :isDirty, -1, true) ");
+                               +":indexNeeded, :isExpunged, :isDirty, -1, true, :contentText) ");
 
                StringBuilder created = new StringBuilder(simple.format(n.getCreated()));                       
                StringBuilder updated = new StringBuilder(simple.format(n.getUpdated()));                       
@@ -130,10 +130,18 @@ public class NoteTable {
                query.bindValue(":title", n.getTitle());
                if (isDirty) {
                        EnmlConverter enml = new EnmlConverter(logger);
+                       String contentText = Global.extractPlainText(enml.fixEnXMLCrap(enml.fixEnMediaCrap(n.getContent())));
+
                        query.bindValue(":content", enml.fixEnXMLCrap(enml.fixEnMediaCrap(n.getContent())));
+                       query.bindValue(":contentText", contentText);
                }
-               else
+               else {
+                       String contentText = Global.extractPlainText(n.getContent());
+                       
                        query.bindValue(":content", n.getContent());
+                       query.bindValue(":contentText", contentText);
+                       
+               }
                query.bindValue(":contentHash", n.getContentHash());
                query.bindValue(":contentLength", n.getContentLength());
                query.bindValue(":created", created.toString());
@@ -567,7 +575,7 @@ public class NoteTable {
        public void updateNoteContent(String guid, String content) {
                logger.log(logger.HIGH, "Entering NoteTable.updateNoteContent");
                NSqlQuery query = new NSqlQuery(db.getConnection());
-               boolean check = query.prepare("Update Note set content=:content, updated=CURRENT_TIMESTAMP(), isDirty=true, indexNeeded=true, " +
+               boolean check = query.prepare("Update Note set content=:content, contentText=:contentText, updated=CURRENT_TIMESTAMP(), isDirty=true, indexNeeded=true, " +
                                " thumbnailneeded=true where guid=:guid");
                if (!check) {
                        logger.log(logger.EXTREME, "Update note content sql prepare has failed.");
@@ -578,6 +586,7 @@ public class NoteTable {
 //             codec = QTextCodec.codecForName("UTF-8");
 //             query.bindValue(":content", codec.fromUnicode(content).toString());
                query.bindValue(":content", content);
+               query.bindValue(":contentText", Global.extractPlainText(content));
                query.bindValue(":guid", guid);
 
                check = query.exec();
@@ -1594,8 +1603,9 @@ public class NoteTable {
                                                 newSegment +
                                                 n.getContent().substring(endPos);
                                NSqlQuery query = new NSqlQuery(db.getConnection());
-                               query.prepare("update note set isdirty=true, thumbnailneeded=true, content=:content where guid=:guid");
+                               query.prepare("update note set isdirty=true, thumbnailneeded=true, content=:content, contentText=:contentText where guid=:guid");
                                query.bindValue(":content", content);
+                               query.bindValue(":contentText", Global.extractPlainText(content));
                                query.bindValue(":guid", n.getGuid());
                                query.exec();
                        }