// Get a note by Guid
public Note mapNoteFromQuery(NSqlQuery query, boolean loadContent, boolean loadResources, boolean loadRecognition, boolean loadBinary, boolean loadTags) {
DateFormat indfm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
// indfm = new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy");
Note n = new Note();
NoteAttributes na = new NoteAttributes();
n.setAttributes(na);
n.setGuid(query.valueString(0));
n.setUpdateSequenceNum(new Integer(query.valueString(1)));
n.setTitle(query.valueString(2));
try {
n.setCreated(indfm.parse(query.valueString(3)).getTime());
n.setUpdated(indfm.parse(query.valueString(4)).getTime());
n.setDeleted(indfm.parse(query.valueString(5)).getTime());
} catch (ParseException e) {
e.printStackTrace();
}
n.setActive(query.valueBoolean(6,true));
n.setNotebookGuid(query.valueString(7));
try {
String attributeSubjectDate = query.valueString(8);
if (!attributeSubjectDate.equals(""))
na.setSubjectDate(indfm.parse(attributeSubjectDate).getTime());
} catch (ParseException e) {
e.printStackTrace();
}
na.setLatitude(new Float(query.valueString(9)));
na.setLongitude(new Float(query.valueString(10)));
na.setAltitude(new Float(query.valueString(11)));
na.setAuthor(query.valueString(12));
na.setSource(query.valueString(13));
na.setSourceURL(query.valueString(14));
na.setSourceApplication(query.valueString(15));
na.setContentClass(query.valueString(16));
if (loadTags) {
List<String> tagGuids = noteTagsTable.getNoteTags(n.getGuid());
List<String> tagNames = new ArrayList<String>();
TagTable tagTable = db.getTagTable();
for (int i=0; i<tagGuids.size(); i++) {
String currentGuid = tagGuids.get(i);
Tag tag = tagTable.getTag(currentGuid);
if (tag.getName() != null)
tagNames.add(tag.getName());
else
tagNames.add("");
}
n.setTagNames(tagNames);
n.setTagGuids(tagGuids);
}
if (loadContent) {
QTextCodec codec = QTextCodec.codecForLocale();
codec = QTextCodec.codecForName("UTF-8");
String unicode = codec.fromUnicode(query.valueString(17)).toString();
// This is a hack. Basically I need to convert HTML Entities to "normal" text, but if I
// convert the < character to < it will mess up the XML parsing. So, to get around this
// I am "bit stuffing" the < to &< so StringEscapeUtils doesn't unescape it. After
// I'm done I convert it back.
StringBuffer buffer = new StringBuffer(unicode);
if (Global.enableHTMLEntitiesFix && unicode.indexOf("&#") > 0) {
unicode = query.valueString(17);
//System.out.println(unicode);
//unicode = unicode.replace("<", "&_lt;");
//unicode = codec.fromUnicode(StringEscapeUtils.unescapeHtml(unicode)).toString();
//unicode = unicode.replace("&_lt;", "<");
//System.out.println("************************");
int j=1;
for (int i=buffer.indexOf("&#"); i != -1 && buffer.indexOf("&#", i)>0; i=buffer.indexOf("&#",i+1)) {
j = buffer.indexOf(";",i)+1;
if (i<j) {
String entity = buffer.substring(i,j).toString();
int len = entity.length()-1;
String tempEntity = entity.substring(2, len);
try {
Integer.parseInt(tempEntity);
entity = codec.fromUnicode(StringEscapeUtils.unescapeHtml4(entity)).toString();
buffer.delete(i, j);
buffer.insert(i, entity);
} catch (Exception e){ }
}
}
}
n.setContent(unicode);
// n.setContent(query.valueString(16).toString());
String contentHash = query.valueString(18);
if (contentHash != null)
n.setContentHash(contentHash.getBytes());
n.setContentLength(new Integer(query.valueString(19)));
}
if (loadResources)
n.setResources(noteResourceTable.getNoteResources(n.getGuid(), loadBinary));
if (loadRecognition) {
if (n.getResources() == null) {
List<Resource> resources = noteResourceTable.getNoteResourcesRecognition(n.getGuid());
n.setResources(resources);
} else {
// We need to merge the recognition resources with the note resources retrieved earlier
for (int i=0; i<n.getResources().size(); i++) {
Resource r = noteResourceTable.getNoteResourceRecognition(n.getResources().get(i).getGuid());
n.getResources().get(i).setRecognition(r.getRecognition());
}
}
}
n.setContent(fixCarriageReturn(n.getContent()));
return n;
}