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
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
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
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
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
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
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
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
\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
public String getErrorMessage() {\r
return errorMessage;\r
}\r
+\r
+ private double doubleValue() {\r
+ return new Double(textValue());\r
+ }\r
}\r