OSDN Git Service

Correct issues with importing notes that contain resources.
authorRandy Baumgarte <randy@fbn.cx>
Tue, 20 Mar 2012 13:56:23 +0000 (09:56 -0400)
committerRandy Baumgarte <randy@fbn.cx>
Tue, 20 Mar 2012 13:56:23 +0000 (09:56 -0400)
src/cx/fbn/nevernote/NeverNote.java
src/cx/fbn/nevernote/sql/NoteResourceTable.java
src/cx/fbn/nevernote/sql/NoteTable.java
src/cx/fbn/nevernote/xml/ImportData.java
src/cx/fbn/nevernote/xml/ImportEnex.java

index 6e48319..f75a985 100644 (file)
@@ -5902,6 +5902,15 @@ public class NeverNote extends QMainWindow{
                        else
                                noteReader.setNotebookGuid(listManager.getNotebookIndex().get(0).getGuid());
   
+                       waitCursor(false);
+                       if (QMessageBox.question(this, tr("Confirmation"), 
+                                       tr("Create new tags from import?"),
+                                       QMessageBox.StandardButton.Yes, 
+                                       QMessageBox.StandardButton.No) == StandardButton.Yes.value()) {
+                                                               noteReader.createNewTags = true;
+                       } else
+                               noteReader.createNewTags = false;
+                       waitCursor(true);
                        noteReader.importData(fileName);
        
                        if (noteReader.lastError != 0) {
index e22bda8..f32cab7 100644 (file)
@@ -188,9 +188,8 @@ public class NoteResourceTable  {
                        query.bindValue(":guid", r.getGuid());\r
                        query.bindValue(":noteGuid", r.getNoteGuid());\r
                        if (r.getData() != null) {\r
-//                             query.bindValue(":dataHash", new QByteArray(r.getData().getBodyHash()).toHex());\r
-//                             query.bindValue(":dataHash", "");\r
                                query.bindValue(":dataHash", byteArrayToHexString(r.getData().getBodyHash()));\r
+//                             query.bindValue(":dataHash", "c0369123fe9871d675ae456fd056ba33");\r
                                query.bindValue(":dataSize", r.getData().getSize());\r
                                query.bindBlob(":dataBody", r.getData().getBody());\r
                        }\r
index 92cc548..4476c6f 100644 (file)
@@ -151,6 +151,16 @@ public class NoteTable {
                        query.bindValue(":attributeSource", n.getAttributes().getSource());\r
                        query.bindValue(":attributeSourceUrl", n.getAttributes().getSourceURL());\r
                        query.bindValue(":attributeSourceApplication", n.getAttributes().getSourceApplication());\r
+               } else {\r
+                       created = new StringBuilder(simple.format(n.getCreated()));     \r
+                       query.bindValue(":attributeSubjectDate", created.toString());\r
+                       query.bindValue(":attributeLatitude", 0.0);\r
+                       query.bindValue(":attributeLongitude", 0.0);\r
+                       query.bindValue(":attributeAltitude", 0.0);\r
+                       query.bindValue(":attributeAuthor", "");\r
+                       query.bindValue(":attributeSource", "");\r
+                       query.bindValue(":attributeSourceUrl", "");\r
+                       query.bindValue(":attributeSourceApplication", "");\r
                }\r
                query.bindValue(":indexNeeded", true);\r
                query.bindValue(":isExpunged", false);\r
index 99d5393..ecb912c 100644 (file)
@@ -302,7 +302,7 @@ public class ImportData {
                                if (reader.name().equalsIgnoreCase("AlternateData")) \r
                                        resource.setAlternateData(processData("AlternateData"));\r
                                if (reader.name().equalsIgnoreCase("RecognitionData")) \r
-                                       resource.setRecognition(processData("NoteResourceAttribute"));\r
+                                       resource.setRecognition(processData("RecognitionData"));\r
                                if (reader.name().equalsIgnoreCase("NoteResourceAttribute")) \r
                                        resource.setAttributes(processResourceAttributes());\r
                        }\r
index 3a9627d..5736545 100644 (file)
@@ -26,13 +26,13 @@ import java.text.ParseException;
 import java.text.SimpleDateFormat;\r
 import java.util.ArrayList;\r
 import java.util.Date;\r
+import java.util.List;\r
+\r
+import javax.xml.bind.DatatypeConverter;\r
 \r
 import com.evernote.edam.type.Data;\r
 import com.evernote.edam.type.Note;\r
 import com.evernote.edam.type.NoteAttributes;\r
-import com.evernote.edam.type.NoteSortOrder;\r
-import com.evernote.edam.type.Notebook;\r
-import com.evernote.edam.type.Publishing;\r
 import com.evernote.edam.type.Resource;\r
 import com.evernote.edam.type.ResourceAttributes;\r
 import com.evernote.edam.type.Tag;\r
@@ -40,9 +40,6 @@ import com.trolltech.qt.core.QByteArray;
 import com.trolltech.qt.core.QFile;\r
 import com.trolltech.qt.core.QIODevice;\r
 import com.trolltech.qt.core.QUuid;\r
-import com.trolltech.qt.gui.QIcon;\r
-import com.trolltech.qt.gui.QImage;\r
-import com.trolltech.qt.gui.QPixmap;\r
 import com.trolltech.qt.xml.QXmlStreamReader;\r
 \r
 import cx.fbn.nevernote.sql.DatabaseConnection;\r
@@ -56,26 +53,21 @@ public class ImportEnex {
        DatabaseConnection                                      conn;\r
        QXmlStreamReader                                        reader;\r
        private Note                                            note;\r
-       private Notebook                                        notebook;\r
-       private boolean                                         notebookIsDirty;\r
-       private boolean                                         notebookIsLocal;\r
-       private boolean                                         notebookIsReadOnly;\r
-       private QIcon                                           notebookIcon;\r
-       private Tag                                                     tag;\r
-       private boolean                                         tagIsDirty;\r
        public int                                                      highUpdateSequenceNumber;\r
        public long                                                     lastSequenceDate;\r
        private final ApplicationLogger         logger;\r
-       private final boolean                           backup;\r
        private String                                          notebookGuid;\r
        public final boolean                            importTags = false;\r
        public final boolean                            importNotebooks = false;\r
        private String newGuid;\r
+       List<Tag> tags;\r
+       public boolean createNewTags;\r
        \r
        public ImportEnex(DatabaseConnection c, boolean full) {\r
                logger = new ApplicationLogger("import.log");\r
-               backup = full;\r
                conn = c;\r
+               tags = conn.getTagTable().getAll();\r
+               createNewTags = true;\r
        }\r
        \r
        public void importData(String f) {\r
@@ -108,30 +100,11 @@ public class ImportEnex {
                                        note.getResources().get(i).setUpdateSequenceNum(0);\r
                                }\r
                                note.setActive(true);\r
+                               if (note.getUpdated() == 0) {\r
+                                       note.setUpdated(note.getCreated());\r
+                               }\r
                                conn.getNoteTable().addNote(note, true);\r
                        }\r
-                       if (reader.name().equalsIgnoreCase("notebook") && reader.isStartElement() && (backup || importNotebooks)) {\r
-                               processNotebookNode();\r
-                       String existingGuid = conn.getNotebookTable().findNotebookByName(notebook.getName());\r
-                       if (existingGuid == null) {\r
-                               conn.getNotebookTable().addNotebook(notebook, notebookIsDirty, notebookIsLocal);\r
-                       } else {\r
-                               conn.getNotebookTable().updateNotebookGuid(existingGuid, notebook.getGuid());\r
-                               conn.getNotebookTable().updateNotebook(notebook, notebookIsDirty);\r
-                       }\r
-                       conn.getNotebookTable().setIcon(notebook.getGuid(), notebookIcon, "PNG");\r
-                       conn.getNotebookTable().setReadOnly(notebook.getGuid(), notebookIsReadOnly);\r
-                       }\r
-                       if (reader.name().equalsIgnoreCase("tag") && reader.isStartElement() && (backup || importTags)) {\r
-                               processTagNode();\r
-                               String testGuid = conn.getTagTable().findTagByName(tag.getName());\r
-                       if (testGuid == null)\r
-                               conn.getTagTable().addTag(tag, tagIsDirty);\r
-                       else {\r
-                               conn.getTagTable().updateTagGuid(testGuid, tag.getGuid());\r
-                               conn.getTagTable().updateTag(tag,tagIsDirty);\r
-                       }\r
-                       }\r
                }\r
                xmlFile.close();\r
        }\r
@@ -149,8 +122,34 @@ public class ImportEnex {
                                note.setTitle(textValue());\r
                        if (reader.name().equalsIgnoreCase("Created")) \r
                                note.setCreated(datetimeValue());\r
+                       if (reader.name().equalsIgnoreCase("updated")) \r
+                               note.setCreated(datetimeValue());\r
                        if (reader.name().equalsIgnoreCase("Content")) \r
                                note.setContent(textValue());\r
+                       if (reader.name().equalsIgnoreCase("tag") && createNewTags) {\r
+                               String tag = textValue();\r
+                               Tag noteTag = null;\r
+                               boolean found=false;\r
+                               for (int i=0; i<tags.size(); i++) {\r
+                                       if (tags.get(i).getName().equalsIgnoreCase(tag)) {\r
+                                               found=true;\r
+                                               noteTag = tags.get(i);\r
+                                               i=tags.size();\r
+                                       }\r
+                               }\r
+                               \r
+                               if (!found) {\r
+                                       noteTag = new Tag();\r
+                                       noteTag.setName(tag);\r
+                                       String tagGuid = QUuid.createUuid().toString().replace("{", "").replace("}", "");\r
+                                       noteTag.setGuid(tagGuid);\r
+                                       noteTag.setName(tag);\r
+                                       tags.add(noteTag);\r
+                                       conn.getTagTable().addTag(noteTag, true);\r
+                               }\r
+                               note.addToTagNames(noteTag.getName());\r
+                               note.addToTagGuids(noteTag.getGuid());\r
+                       }\r
                        if (reader.name().equalsIgnoreCase("note-attributes")) \r
                                note.setAttributes(processNoteAttributes());\r
                        if (reader.name().equalsIgnoreCase("resource")) {\r
@@ -166,7 +165,7 @@ public class ImportEnex {
                Resource resource = new Resource();\r
                boolean atEnd = false;\r
                while(!atEnd) {\r
-                       if (reader.isStartElement()) {\r
+                       if (reader.isStartElement() && reader.name().equalsIgnoreCase("resource")) {\r
                                String newResGuid = QUuid.createUuid().toString().replace("{", "").replace("}", "");\r
                                resource.setGuid(newResGuid);\r
                                resource.setNoteGuid(this.newGuid);\r
@@ -179,17 +178,15 @@ public class ImportEnex {
                                resource.setWidth(shortValue());\r
                        if (reader.name().equalsIgnoreCase("data")) \r
                                resource.setData(processData("data"));\r
-//                             if (reader.name().equalsIgnoreCase("recognition")) \r
-//                                     resource.setRecognition(processData("NoteResourceAttribute"));\r
+                       if (reader.name().equalsIgnoreCase("resource-attributes")) \r
+                               resource.setAttributes(processResourceAttributes());\r
+                       if (reader.name().equalsIgnoreCase("recognition")) \r
+                               resource.setRecognition(processRecognition());\r
                        reader.readNext();\r
                        if (reader.name().equalsIgnoreCase("resource") && reader.isEndElement())\r
                                atEnd = true;\r
                }\r
-               resource.setAttributes(new ResourceAttributes());\r
-               resource.getAttributes().setSourceURL("");\r
-               conn.getNoteTable().noteResourceTable.saveNoteResource(resource, true);\r
-\r
-               \r
+               conn.getNoteTable().noteResourceTable.updateNoteResource(resource, true);\r
                return resource;\r
        }\r
        \r
@@ -198,21 +195,24 @@ public class ImportEnex {
                boolean atEnd = false;\r
                while(!atEnd) {\r
                        if (reader.isStartElement()) {\r
+                               try {\r
                                byte[] b = textValue().getBytes();   // data binary\r
                                if (b.length > 0) {\r
                                        QByteArray hexData = new QByteArray(b);\r
-                                       QByteArray binData = new QByteArray(QByteArray.fromHex(hexData));\r
-                                       data.setBody(binData.toByteArray());\r
+                                       String hexString = hexData.toString();\r
+                                       data.setBody(DatatypeConverter.parseBase64Binary(hexString));\r
                                        MessageDigest md;\r
                                        try {\r
                                                md = MessageDigest.getInstance("MD5");\r
-                                               md.update(b);\r
+                                               md.update(data.getBody());\r
                                                data.setBodyHash(md.digest());\r
                                        } catch (NoSuchAlgorithmException e) {\r
                                                // TODO Auto-generated catch block\r
                                                e.printStackTrace();\r
                                        }\r
-                               }\r
+                                       \r
+                               }}\r
+                               catch (Exception e) {};\r
                        }\r
                        if (reader.name().equalsIgnoreCase(nodeName) && reader.isEndElement())\r
                                atEnd = true;\r
@@ -224,138 +224,94 @@ public class ImportEnex {
 \r
        \r
        private NoteAttributes processNoteAttributes() {\r
-       NoteAttributes attributes = new NoteAttributes();\r
+               NoteAttributes attributes = new NoteAttributes();\r
        \r
-       boolean atEnd = false;\r
-       while(!atEnd) {\r
-               if (reader.isStartElement()) {\r
-                       if (reader.name().equalsIgnoreCase("source-url")) \r
-                               attributes.setSourceURL(textValue());\r
-                       if (reader.name().equalsIgnoreCase("source")) \r
-                               attributes.setSource(textValue());\r
-               }\r
-               reader.readNext();\r
-               if (reader.name().equalsIgnoreCase("note-attributes") && reader.isEndElement())\r
-                       atEnd = true;\r
-       }\r
-       \r
-       return attributes;\r
-}\r
-\r
-               \r
-       \r
-\r
-       \r
-\r
-       \r
-       private void processNotebookNode() {\r
-               notebook = new Notebook();\r
-               Publishing p = new Publishing();\r
-               notebook.setPublishing(p);\r
-               notebookIsDirty = false;\r
-               notebookIsLocal = false;\r
-               notebookIsReadOnly = false;\r
-               notebookIcon = null;\r
                boolean atEnd = false;\r
                while(!atEnd) {\r
                        if (reader.isStartElement()) {\r
-                               if (reader.name().equalsIgnoreCase("Guid")) \r
-                                       notebook.setGuid(textValue());\r
-                               if (reader.name().equalsIgnoreCase("Name")) \r
-                                       notebook.setName(textValue());\r
-                               if (reader.name().equalsIgnoreCase("UpdateSequenceNumber")) \r
-                                       notebook.setUpdateSequenceNum(intValue());\r
-                               if (reader.name().equalsIgnoreCase("ServiceCreated")) \r
-                                       notebook.setServiceCreated(longValue());\r
-                               if (reader.name().equalsIgnoreCase("ServiceUpdated")) \r
-                                       notebook.setServiceUpdated(longValue());\r
-                               if (reader.name().equalsIgnoreCase("DefaultNotebook")) {\r
-                                       notebook.setDefaultNotebook(booleanValue());\r
-                               }\r
-                               if (reader.name().equalsIgnoreCase("Dirty")) {\r
-                                       if (booleanValue())\r
-                                               notebookIsDirty = true;\r
-                               }\r
-                               if (reader.name().equalsIgnoreCase("LocalNotebook")) {\r
-                                       if (booleanValue())\r
-                                               notebookIsLocal = true;\r
-                               }\r
-                               if (reader.name().equalsIgnoreCase("ReadOnly")) {\r
-                                       if (booleanValue())\r
-                                               notebookIsReadOnly = true;\r
-                               }\r
-                               if (reader.name().equalsIgnoreCase("PublishingPublicDescription")) {\r
-                                       notebook.getPublishing().setPublicDescription(textValue());\r
-                               }\r
-                               if (reader.name().equalsIgnoreCase("PublishingUri")) {\r
-                                       notebook.getPublishing().setUri(textValue());\r
-                               }\r
-                               if (reader.name().equalsIgnoreCase("PublishingOrder")) {\r
-                                       notebook.getPublishing().setOrder(NoteSortOrder.findByValue(intValue()));\r
-                               }\r
-                               if (reader.name().equalsIgnoreCase("ReadOnly")) {\r
-                                       if (booleanValue())\r
-                                               notebookIsReadOnly = true;\r
-                               }\r
-                               if (reader.name().equalsIgnoreCase("PublishingAscending")) {\r
-                                       if (booleanValue())\r
-                                               notebook.getPublishing().setAscending(true);\r
-                                       else\r
-                                               notebook.getPublishing().setAscending(false);\r
-                               }               \r
-                               if (reader.name().equalsIgnoreCase("Icon")) {\r
-                                       byte[] b = textValue().getBytes();   // data binary\r
-                                       QByteArray hexData = new QByteArray(b);\r
-                                       QByteArray binData = new QByteArray(QByteArray.fromHex(hexData));\r
-                                       notebookIcon = new QIcon(QPixmap.fromImage(QImage.fromData(binData)));\r
-                               }\r
-                               if (reader.name().equalsIgnoreCase("Stack"))\r
-                                       notebook.setStack(textValue());\r
+                               if (reader.name().equalsIgnoreCase("source-url")) \r
+                                       attributes.setSourceURL(textValue());\r
+                               if (reader.name().equalsIgnoreCase("source")) \r
+                                       attributes.setSource(textValue());\r
+                               if (reader.name().equalsIgnoreCase("longitude")) \r
+                                       attributes.setLongitude(doubleValue());\r
+                               if (reader.name().equalsIgnoreCase("latitude")) \r
+                                       attributes.setLatitude(doubleValue());\r
+                               if (reader.name().equalsIgnoreCase("altitude")) \r
+                                       attributes.setAltitude(doubleValue());\r
+                               if (reader.name().equalsIgnoreCase("author")) \r
+                                       attributes.setAuthor(textValue());\r
+                               if (reader.name().equalsIgnoreCase("subject-date")) \r
+                                       attributes.setSubjectDate(datetimeValue());\r
                        }\r
                        reader.readNext();\r
-                       if (reader.name().equalsIgnoreCase("notebook") && reader.isEndElement())\r
-                               atEnd = true;\r
+                       if (reader.name().equalsIgnoreCase("note-attributes") && reader.isEndElement())\r
+                       atEnd = true;\r
                }\r
-               return;\r
+       \r
+               return attributes;\r
        }\r
 \r
+               \r
        \r
+       private Data processRecognition() {\r
+               Data reco = new Data();\r
+               reco.setBody(textValue().getBytes());\r
+               MessageDigest md;\r
+               try {\r
+                       md = MessageDigest.getInstance("MD5");\r
+                       md.update(reco.getBody());\r
+                       reco.setBodyHash(md.digest());\r
+                       reco.setSize(reco.getBody().length);\r
+               } catch (NoSuchAlgorithmException e) {\r
+                       // TODO Auto-generated catch block\r
+                       e.printStackTrace();\r
+               }\r
+               return reco;\r
+       }\r
        \r
-       private void processTagNode() {\r
-               tag = new Tag();\r
-               tagIsDirty = false;\r
+       private ResourceAttributes processResourceAttributes() {\r
+               ResourceAttributes attributes = new ResourceAttributes();\r
                boolean atEnd = false;\r
                while(!atEnd) {\r
-                       if (reader.isStartElement()) {                  \r
-                               if (reader.name().equalsIgnoreCase("Guid")) \r
-                                       tag.setGuid(textValue());\r
-                               if (reader.name().equalsIgnoreCase("Name")) \r
-                                       tag.setName(textValue());\r
-                               if (reader.name().equalsIgnoreCase("UpdateSequenceNumber")) \r
-                                       tag.setUpdateSequenceNum(intValue());\r
-                               if (reader.name().equalsIgnoreCase("ParentGuid")) \r
-                                       tag.setParentGuid(textValue());\r
-                               if (reader.name().equalsIgnoreCase("Dirty")) {\r
-                                       if (booleanValue())\r
-                                               tagIsDirty = true;\r
-                               }\r
+                       if (reader.isStartElement()) {\r
+                               if (reader.name().equalsIgnoreCase("CameraMake")) \r
+                                       attributes.setCameraMake(textValue());          \r
+                               if (reader.name().equalsIgnoreCase("CameraModel")) \r
+                                       attributes.setCameraModel(textValue());         \r
+                               if (reader.name().equalsIgnoreCase("FileName")) \r
+                                       attributes.setFileName(textValue());            \r
+                               if (reader.name().equalsIgnoreCase("RecoType")) \r
+                                       attributes.setRecoType(textValue());            \r
+                               if (reader.name().equalsIgnoreCase("CameraModel")) \r
+                                       attributes.setCameraMake(textValue());          \r
+                               if (reader.name().equalsIgnoreCase("SourceURL")) \r
+                                       attributes.setSourceURL(textValue());           \r
+                               if (reader.name().equalsIgnoreCase("Altitude")) \r
+                                       attributes.setAltitude(doubleValue());          \r
+                               if (reader.name().equalsIgnoreCase("Longitude")) \r
+                                       attributes.setLongitude(doubleValue());         \r
+                               if (reader.name().equalsIgnoreCase("Latitude")) \r
+                                       attributes.setLatitude(doubleValue());          \r
+                               if (reader.name().equalsIgnoreCase("Timestamp")) \r
+                                       attributes.setTimestamp(longValue());           \r
+                               if (reader.name().equalsIgnoreCase("Attachment")) \r
+                                       attributes.setAttachment(booleanValue());               \r
+                               if (reader.name().equalsIgnoreCase("ClientWillIndex")) \r
+                                       attributes.setClientWillIndex(booleanValue());          \r
                        }\r
                        reader.readNext();\r
-                       if (reader.name().equalsIgnoreCase("tag") && reader.isEndElement())\r
+                       if (reader.name().equalsIgnoreCase("resource-attributes") && reader.isEndElement())\r
                                atEnd = true;\r
                }\r
-               return;\r
+               \r
+               return attributes;\r
        }\r
        \r
        \r
-       \r
-       \r
        private String textValue() {\r
                return reader.readElementText();\r
        }\r
-       private int intValue() {\r
-               return new Integer(textValue());\r
-       }\r
        private long longValue() {\r
                return new Long(textValue());\r
        }\r
@@ -397,4 +353,8 @@ public class ImportEnex {
        public String getErrorMessage() {\r
                return errorMessage;\r
        }\r
+\r
+       private double doubleValue() {\r
+               return new Double(textValue());\r
+       }\r
 }\r