* some refactoring to Blog

* changed default sort order to reverse (newest first)

git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@4475 6c8d7289-2bf4-0310-a012-ef5d649a1542
pull/1/head
lulabad 17 years ago
parent 766a04bc06
commit c1b9a03304

@ -140,12 +140,12 @@ public class Blog {
} }
if(hasRights && post.containsKey("delete") && post.get("delete").equals("sure")) { if(hasRights && post.containsKey("delete") && post.get("delete").equals("sure")) {
page = switchboard.blogDB.read(pagename); page = switchboard.blogDB.readBlogEntry(pagename);
final Iterator<String> i = page.comments().iterator(); final Iterator<String> i = page.getComments().iterator();
while(i.hasNext()) { while(i.hasNext()) {
switchboard.blogCommentDB.delete(i.next()); switchboard.blogCommentDB.delete(i.next());
} }
switchboard.blogDB.delete(pagename); switchboard.blogDB.deleteBlogEntry(pagename);
pagename = DEFAULT_PAGE; pagename = DEFAULT_PAGE;
} }
@ -169,9 +169,9 @@ public class Blog {
if(pagename.equals(DEFAULT_PAGE)) { if(pagename.equals(DEFAULT_PAGE)) {
pagename = String.valueOf(System.currentTimeMillis()); pagename = String.valueOf(System.currentTimeMillis());
} else { } else {
page = switchboard.blogDB.read(pagename); page = switchboard.blogDB.readBlogEntry(pagename);
comments = page.comments(); comments = page.getComments();
date = page.date(); date = page.getDate();
} }
final String commentMode = post.get("commentMode", "1"); final String commentMode = post.get("commentMode", "1");
final String StrSubject = post.get("subject", ""); final String StrSubject = post.get("subject", "");
@ -182,7 +182,7 @@ public class Blog {
subject = StrSubject.getBytes(); subject = StrSubject.getBytes();
} }
switchboard.blogDB.write(switchboard.blogDB.newEntry(pagename, subject, author, ip, date, content, comments, commentMode)); switchboard.blogDB.writeBlogEntry(switchboard.blogDB.newEntry(pagename, subject, author, ip, date, content, comments, commentMode));
// create a news message // create a news message
final HashMap<String, String> map = new HashMap<String, String>(); final HashMap<String, String> map = new HashMap<String, String>();
@ -192,7 +192,7 @@ public class Blog {
yacyCore.newsPool.publishMyNews(yacyNewsRecord.newRecord(yacyNewsPool.CATEGORY_BLOG_ADD, map)); yacyCore.newsPool.publishMyNews(yacyNewsRecord.newRecord(yacyNewsPool.CATEGORY_BLOG_ADD, map));
} }
page = switchboard.blogDB.read(pagename); //maybe "if(page == null)" page = switchboard.blogDB.readBlogEntry(pagename); //maybe "if(page == null)"
if (post.containsKey("edit")) { if (post.containsKey("edit")) {
//edit an entry //edit an entry
@ -200,10 +200,10 @@ public class Blog {
try { try {
prop.put("mode", "1"); //edit prop.put("mode", "1"); //edit
prop.put("mode_commentMode", page.getCommentMode()); prop.put("mode_commentMode", page.getCommentMode());
prop.putHTML("mode_author", new String(page.author(),"UTF-8"), xml); prop.putHTML("mode_author", new String(page.getAuthor(),"UTF-8"), xml);
prop.put("mode_pageid", page.key()); prop.put("mode_pageid", page.getKey());
prop.putHTML("mode_subject", new String(page.subject(), "UTF-8"), xml); prop.putHTML("mode_subject", new String(page.getSubject(), "UTF-8"), xml);
prop.put("mode_page-code", new String(page.page(), "UTF-8")); prop.put("mode_page-code", new String(page.getPage(), "UTF-8"));
} catch (UnsupportedEncodingException e) {} } catch (UnsupportedEncodingException e) {}
} }
else { else {
@ -235,14 +235,14 @@ public class Blog {
prop.put("mode", "4"); prop.put("mode", "4");
prop.put("mode_pageid", pagename); prop.put("mode_pageid", pagename);
try { try {
prop.putHTML("mode_author",new String(page.author(), "UTF-8"), xml); prop.putHTML("mode_author",new String(page.getAuthor(), "UTF-8"), xml);
} catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e) {
prop.putHTML("mode_author",new String(page.author()), xml); prop.putHTML("mode_author",new String(page.getAuthor()), xml);
} }
try { try {
prop.putHTML("mode_subject",new String(page.subject(),"UTF-8"), xml); prop.putHTML("mode_subject",new String(page.getSubject(),"UTF-8"), xml);
} catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e) {
prop.putHTML("mode_subject",new String(page.subject()), xml); prop.putHTML("mode_subject",new String(page.getSubject()), xml);
} }
} }
else prop.put("mode", "3"); //access denied (no rights) else prop.put("mode", "3"); //access denied (no rights)
@ -305,7 +305,7 @@ public class Blog {
if(0 < start--) continue; if(0 < start--) continue;
putBlogEntry( putBlogEntry(
prop, prop,
switchboard.blogDB.read(pageid), switchboard.blogDB.readBlogEntry(pageid),
address, address,
count++, count++,
hasRights, hasRights,
@ -342,16 +342,16 @@ public class Blog {
{ {
// subject // subject
try { try {
prop.putHTML("mode_entries_" + number + "_subject", new String(entry.subject(),"UTF-8"), xml); prop.putHTML("mode_entries_" + number + "_subject", new String(entry.getSubject(),"UTF-8"), xml);
} catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e) {
prop.putHTML("mode_entries_" + number + "_subject", new String(entry.subject()), xml); prop.putHTML("mode_entries_" + number + "_subject", new String(entry.getSubject()), xml);
} }
// author // author
try { try {
prop.putHTML("mode_entries_" + number + "_author", new String(entry.author(),"UTF-8"), xml); prop.putHTML("mode_entries_" + number + "_author", new String(entry.getAuthor(),"UTF-8"), xml);
} catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e) {
prop.putHTML("mode_entries_" + number + "_author", new String(entry.author()), xml); prop.putHTML("mode_entries_" + number + "_author", new String(entry.getAuthor()), xml);
} }
// comments // comments
@ -359,27 +359,27 @@ public class Blog {
prop.put("mode_entries_" + number + "_commentsactive", "0"); prop.put("mode_entries_" + number + "_commentsactive", "0");
} else { } else {
prop.put("mode_entries_" + number + "_commentsactive", "1"); prop.put("mode_entries_" + number + "_commentsactive", "1");
prop.put("mode_entries_" + number + "_commentsactive_pageid", entry.key()); prop.put("mode_entries_" + number + "_commentsactive_pageid", entry.getKey());
prop.put("mode_entries_" + number + "_commentsactive_address", address); prop.put("mode_entries_" + number + "_commentsactive_address", address);
prop.put("mode_entries_" + number + "_commentsactive_comments", entry.commentsSize()); prop.put("mode_entries_" + number + "_commentsactive_comments", entry.getCommentsSize());
} }
prop.put("mode_entries_" + number + "_date", dateString(entry.date())); prop.put("mode_entries_" + number + "_date", dateString(entry.getDate()));
prop.put("mode_entries_" + number + "_rfc822date", httpc.dateString(entry.date())); prop.put("mode_entries_" + number + "_rfc822date", httpc.dateString(entry.getDate()));
prop.put("mode_entries_" + number + "_pageid", entry.key()); prop.put("mode_entries_" + number + "_pageid", entry.getKey());
prop.put("mode_entries_" + number + "_address", address); prop.put("mode_entries_" + number + "_address", address);
prop.put("mode_entries_" + number + "_ip", entry.ip()); prop.put("mode_entries_" + number + "_ip", entry.getIp());
if(xml) { if(xml) {
prop.put("mode_entries_" + number + "_page", entry.page()); prop.put("mode_entries_" + number + "_page", entry.getPage());
prop.put("mode_entries_" + number + "_timestamp", entry.timestamp()); prop.put("mode_entries_" + number + "_timestamp", entry.getTimestamp());
} else { } else {
prop.putWiki("mode_entries_" + number + "_page", entry.page()); prop.putWiki("mode_entries_" + number + "_page", entry.getPage());
} }
if(hasRights) { if(hasRights) {
prop.put("mode_entries_" + number + "_admin", "1"); prop.put("mode_entries_" + number + "_admin", "1");
prop.put("mode_entries_" + number + "_admin_pageid",entry.key()); prop.put("mode_entries_" + number + "_admin_pageid",entry.getKey());
} else { } else {
prop.put("mode_entries_" + number + "_admin", "0"); prop.put("mode_entries_" + number + "_admin", "0");
} }

@ -126,7 +126,7 @@ public class BlogComments {
author = StrAuthor.getBytes(); author = StrAuthor.getBytes();
} }
page = switchboard.blogDB.read(pagename); //maybe "if(page == null)" page = switchboard.blogDB.readBlogEntry(pagename); //maybe "if(page == null)"
// comments not allowed // comments not allowed
if (page.getCommentMode() == 0) { if (page.getCommentMode() == 0) {
@ -158,9 +158,9 @@ public class BlogComments {
subject = StrSubject.getBytes(); subject = StrSubject.getBytes();
} }
String commentID = String.valueOf(System.currentTimeMillis()); String commentID = String.valueOf(System.currentTimeMillis());
BlogEntry blogEntry = switchboard.blogDB.read(pagename); BlogEntry blogEntry = switchboard.blogDB.readBlogEntry(pagename);
blogEntry.addComment(commentID); blogEntry.addComment(commentID);
switchboard.blogDB.write(blogEntry); switchboard.blogDB.writeBlogEntry(blogEntry);
switchboard.blogCommentDB.write(switchboard.blogCommentDB.newEntry(commentID, subject, author, ip, date, content)); switchboard.blogCommentDB.write(switchboard.blogCommentDB.newEntry(commentID, subject, author, ip, date, content));
prop.put("LOCATION","BlogComments.html?page=" + pagename); prop.put("LOCATION","BlogComments.html?page=" + pagename);
@ -171,14 +171,14 @@ public class BlogComments {
StrAuthor, StrAuthor,
yacyCore.seedDB.mySeed().hash, yacyCore.seedDB.mySeed().hash,
yacyCore.seedDB.mySeed().getName(), yacyCore.seedDB.mySeed().hash, yacyCore.seedDB.mySeed().getName(), yacyCore.seedDB.mySeed().hash,
"new blog comment: " + new String(blogEntry.subject(),"UTF-8"), content)); "new blog comment: " + new String(blogEntry.getSubject(),"UTF-8"), content));
} catch (UnsupportedEncodingException e1) { } catch (UnsupportedEncodingException e1) {
switchboard.messageDB.write(msgEntry = switchboard.messageDB.newEntry( switchboard.messageDB.write(msgEntry = switchboard.messageDB.newEntry(
"blogComment", "blogComment",
StrAuthor, StrAuthor,
yacyCore.seedDB.mySeed().hash, yacyCore.seedDB.mySeed().hash,
yacyCore.seedDB.mySeed().getName(), yacyCore.seedDB.mySeed().hash, yacyCore.seedDB.mySeed().getName(), yacyCore.seedDB.mySeed().hash,
"new blog comment: " + new String(blogEntry.subject()), content)); "new blog comment: " + new String(blogEntry.getSubject()), content));
} }
messageForwardingViaEmail(env, msgEntry); messageForwardingViaEmail(env, msgEntry);
@ -232,30 +232,30 @@ public class BlogComments {
} }
else { else {
//show 1 blog entry //show 1 blog entry
prop.put("mode_pageid", page.key()); prop.put("mode_pageid", page.getKey());
prop.put("mode_allow_pageid", pagename); prop.put("mode_allow_pageid", pagename);
try { try {
prop.putHTML("mode_subject", new String(page.subject(),"UTF-8")); prop.putHTML("mode_subject", new String(page.getSubject(),"UTF-8"));
} catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e) {
prop.putHTML("mode_subject", new String(page.subject())); prop.putHTML("mode_subject", new String(page.getSubject()));
} }
try { try {
prop.putHTML("mode_author", new String(page.author(),"UTF-8")); prop.putHTML("mode_author", new String(page.getAuthor(),"UTF-8"));
prop.putHTML("mode_allow_author", new String(author, "UTF-8")); prop.putHTML("mode_allow_author", new String(author, "UTF-8"));
} catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e) {
prop.putHTML("mode_author", new String(page.author())); prop.putHTML("mode_author", new String(page.getAuthor()));
prop.putHTML("mode_allow_author", new String(author)); prop.putHTML("mode_allow_author", new String(author));
} }
prop.put("mode_comments", page.commentsSize()); prop.put("mode_comments", page.getCommentsSize());
prop.put("mode_date", dateString(page.date())); prop.put("mode_date", dateString(page.getDate()));
prop.putWiki("mode_page", page.page()); prop.putWiki("mode_page", page.getPage());
if(hasRights) { if(hasRights) {
prop.put("mode_admin", "1"); prop.put("mode_admin", "1");
prop.put("mode_admin_pageid", page.key()); prop.put("mode_admin_pageid", page.getKey());
} }
//show all commments //show all commments
try { try {
Iterator<String> i = page.comments().iterator(); Iterator<String> i = page.getComments().iterator();
int commentMode = page.getCommentMode(); int commentMode = page.getCommentMode();
String pageid; String pageid;
blogBoardComments.CommentEntry entry; blogBoardComments.CommentEntry entry;
@ -303,11 +303,11 @@ public class BlogComments {
prop.put("mode_entries_"+count+"_ip", entry.ip()); prop.put("mode_entries_"+count+"_ip", entry.ip());
if(hasRights) { if(hasRights) {
prop.put("mode_entries_"+count+"_admin", "1"); prop.put("mode_entries_"+count+"_admin", "1");
prop.put("mode_entries_"+count+"_admin_pageid", page.key()); prop.put("mode_entries_"+count+"_admin_pageid", page.getKey());
prop.put("mode_entries_"+count+"_admin_commentid", pageid); prop.put("mode_entries_"+count+"_admin_commentid", pageid);
if(!entry.isAllowed()) { if(!entry.isAllowed()) {
prop.put("mode_entries_"+count+"_admin_moderate", "1"); prop.put("mode_entries_"+count+"_admin_moderate", "1");
prop.put("mode_entries_"+count+"_admin_moderate_pageid", page.key()); prop.put("mode_entries_"+count+"_admin_moderate_pageid", page.getKey());
prop.put("mode_entries_"+count+"_admin_moderate_commentid", pageid); prop.put("mode_entries_"+count+"_admin_moderate_commentid", pageid);
} }
@ -320,7 +320,7 @@ public class BlogComments {
prop.put("mode_moreentries", "1"); //more entries are availible prop.put("mode_moreentries", "1"); //more entries are availible
prop.put("mode_moreentries_start", nextstart); prop.put("mode_moreentries_start", nextstart);
prop.put("mode_moreentries_num", num); prop.put("mode_moreentries_num", num);
prop.put("mode_moreentries_pageid", page.key()); prop.put("mode_moreentries_pageid", page.getKey());
} }
else prop.put("mode_moreentries", "0"); else prop.put("mode_moreentries", "0");
if(prev) { if(prev) {
@ -328,7 +328,7 @@ public class BlogComments {
if (prevstart < 0) prevstart = 0; if (prevstart < 0) prevstart = 0;
prop.put("mode_preventries_start", prevstart); prop.put("mode_preventries_start", prevstart);
prop.put("mode_preventries_num", num); prop.put("mode_preventries_num", num);
prop.put("mode_preventries_pageid", page.key()); prop.put("mode_preventries_pageid", page.getKey());
} else prop.put("mode_preventries", "0"); } else prop.put("mode_preventries", "0");
} catch (IOException e) { } catch (IOException e) {

@ -78,28 +78,24 @@ public class blogBoard {
public static final int keyLength = 64; public static final int keyLength = 64;
private static final int recordSize = 512; private static final int recordSize = 512;
kelondroMapObjects datbase = null; kelondroMapObjects database = null;
public blogBoard(File actpath, long preloadTime) { public blogBoard(File actpath, long preloadTime) {
new File(actpath.getParent()).mkdir(); new File(actpath.getParent()).mkdir();
if (datbase == null) { if (database == null) {
datbase = new kelondroMapObjects(new kelondroDyn(actpath, true, true, preloadTime, keyLength, recordSize, '_', kelondroNaturalOrder.naturalOrder, true, false, false), 500); database = new kelondroMapObjects(new kelondroDyn(actpath, true, true, preloadTime, keyLength, recordSize, '_', kelondroNaturalOrder.naturalOrder, true, false, false), 500);
} }
} }
public int size() { public int size() {
return datbase.size(); return database.size();
} }
public void close() { public void close() {
datbase.close(); database.close();
} }
private static String normalize(String key) { private static String normalize(String key) {
if (key == null) return "null"; if (key == null) return "null";
return key.trim().toLowerCase(); return key.trim().toLowerCase();
} }
public static String webalize(String key) { public static String webalize(String key) {
if (key == null) return "null"; if (key == null) return "null";
key = key.trim().toLowerCase(); key = key.trim().toLowerCase();
@ -108,11 +104,9 @@ public class blogBoard {
key = key.substring(0, p) + "%20" + key.substring(p +1); key = key.substring(0, p) + "%20" + key.substring(p +1);
return key; return key;
} }
public String guessAuthor(String ip) { public String guessAuthor(String ip) {
return wikiBoard.guessAuthor(ip); return wikiBoard.guessAuthor(ip);
} }
public BlogEntry newEntry(String key, byte[] subject, byte[] author, String ip, Date date, byte[] page, ArrayList<String> comments, String commentMode) { public BlogEntry newEntry(String key, byte[] subject, byte[] author, String ip, Date date, byte[] page, ArrayList<String> comments, String commentMode) {
return new BlogEntry(normalize(key), subject, author, ip, date, page, comments, commentMode); return new BlogEntry(normalize(key), subject, author, ip, date, page, comments, commentMode);
} }
@ -124,152 +118,179 @@ public class blogBoard {
public BlogEntry(String nkey, byte[] subject, byte[] author, String ip, Date date, byte[] page, ArrayList<String> comments, String commentMode) { public BlogEntry(String nkey, byte[] subject, byte[] author, String ip, Date date, byte[] page, ArrayList<String> comments, String commentMode) {
record = new HashMap<String, String>(); record = new HashMap<String, String>();
key = nkey; setKey(nkey);
if (key.length() > keyLength) key = key.substring(0, keyLength); setDate(date);
if(date == null) date = new Date(); setSubject(subject);
record.put("date", serverDate.formatShortSecond(date)); setAuthor(author);
if (subject == null) record.put("subject",""); setIp(ip);
else record.put("subject", kelondroBase64Order.enhancedCoder.encode(subject)); setPage(page);
if (author == null) record.put("author",""); setComments(comments);
else record.put("author", kelondroBase64Order.enhancedCoder.encode(author)); setCommentMode(commentMode);
if ((ip == null) || (ip.length() == 0)) ip = "";
record.put("ip", ip);
if (page == null) record.put("page", "");
else record.put("page", kelondroBase64Order.enhancedCoder.encode(page));
if (comments == null) record.put("comments", listManager.collection2string(new ArrayList<String>()));
else record.put("comments", listManager.collection2string(comments));
if (commentMode == null) record.put("commentMode", "1");
else record.put("commentMode", commentMode);
// TODO: implement this function // TODO: implement this function
record.put("privacy", "public"); record.put("privacy", "public");
wikiBoard.setAuthor(ip, new String(author)); wikiBoard.setAuthor(ip, new String(author));
} }
private BlogEntry(String key, HashMap<String, String> record) { private BlogEntry(String key, HashMap<String, String> record) {
this.key = key; this.key = key;
this.record = record; this.record = record;
if (this.record.get("comments")==null) this.record.put("comments", listManager.collection2string(new ArrayList<String>())); if (this.record.get("comments")==null) this.record.put("comments", listManager.collection2string(new ArrayList<String>()));
if (this.record.get("commentMode")==null || this.record.get("commentMode").equals("")) this.record.put("commentMode", "1"); if (this.record.get("commentMode")==null || this.record.get("commentMode").equals("")) this.record.put("commentMode", "1");
} }
private void setKey(String key) {
public String key() { if (key.length() > keyLength)
this.key = key.substring(0, keyLength);
this.key = key;
}
public String getKey() {
return key; return key;
} }
public byte[] getSubject() {
public byte[] subject() {
String m = record.get("subject"); String m = record.get("subject");
if (m == null) return new byte[0]; if (m == null) return new byte[0];
byte[] b = kelondroBase64Order.enhancedCoder.decode(m, "de.anomic.data.blogBoard.subject()"); byte[] b = kelondroBase64Order.enhancedCoder.decode(m, "de.anomic.data.blogBoard.subject()");
if (b == null) return "".getBytes(); if (b == null) return "".getBytes();
return b; return b;
} }
private void setSubject(byte[] subject) {
public Date date() { if (subject == null)
record.put("subject","");
else
record.put("subject", kelondroBase64Order.enhancedCoder.encode(subject));
}
public Date getDate() {
try { try {
String c = record.get("date"); String date = record.get("date");
if (c == null) { if (date == null) {
serverLog.logFinest("Blog", "ERROR: date field missing in blogBoard"); serverLog.logFinest("Blog", "ERROR: date field missing in blogBoard");
//System.out.println("DEBUG - ERROR: date field missing in blogBoard"); return new Date();
return new Date(); }
} return serverDate.parseShortSecond(date);
return serverDate.parseShortSecond(c);
} catch (ParseException e) { } catch (ParseException e) {
return new Date(); return new Date();
} }
} }
private void setDate(Date date) {
public String timestamp() { if(date == null)
String c = record.get("date"); date = new Date();
if (c == null) { record.put("date", serverDate.formatShortSecond(date));
}
public String getTimestamp() {
String timestamp = record.get("date");
if (timestamp == null) {
serverLog.logFinest("Blog", "ERROR: date field missing in blogBoard"); serverLog.logFinest("Blog", "ERROR: date field missing in blogBoard");
//System.out.println("DEBUG - ERROR: date field missing in blogBoard");
return serverDate.formatShortSecond(); return serverDate.formatShortSecond();
} }
return c; return timestamp;
} }
public byte[] getAuthor() {
public byte[] author() { String author = record.get("author");
String m = record.get("author"); if (author == null) return new byte[0];
if (m == null) return new byte[0]; byte[] b = kelondroBase64Order.enhancedCoder.decode(author, "de.anomic.data.blogBoard.author()");
byte[] b = kelondroBase64Order.enhancedCoder.decode(m, "de.anomic.data.blogBoard.author()");
if (b == null) return "".getBytes(); if (b == null) return "".getBytes();
return b; return b;
} }
private void setAuthor(byte[] author) {
public int commentsSize() { if (author == null)
record.put("author","");
else
record.put("author", kelondroBase64Order.enhancedCoder.encode(author));
}
public int getCommentsSize() {
// This ist a Bugfix for Version older than 4443. // This ist a Bugfix for Version older than 4443.
if(record.get("comments").startsWith(",")) { if(record.get("comments").startsWith(",")) {
record.put("comments", record.get("comments").substring(1)); record.put("comments", record.get("comments").substring(1));
write(this); writeBlogEntry(this);
} }
ArrayList<String> m = listManager.string2arraylist(record.get("comments")); ArrayList<String> commentsize = listManager.string2arraylist(record.get("comments"));
return m.size(); return commentsize.size();
} }
public ArrayList<String> getComments() {
public ArrayList<String> comments() { ArrayList<String> comments = listManager.string2arraylist(record.get("comments"));
ArrayList<String> m = listManager.string2arraylist(record.get("comments")); return comments;
return m;
} }
private void setComments(ArrayList<String> comments) {
public String ip() { if (comments == null)
String a = record.get("ip"); record.put("comments", listManager.collection2string(new ArrayList<String>()));
if (a == null) return "127.0.0.1"; else
return a; record.put("comments", listManager.collection2string(comments));
}
public String getIp() {
String ip = record.get("ip");
if (ip == null) return "127.0.0.1";
return ip;
} }
private void setIp(String ip) {
public byte[] page() { if ((ip == null) || (ip.length() == 0))
String m = record.get("page"); ip = "";
if (m == null) return new byte[0]; record.put("ip", ip);
byte[] b = kelondroBase64Order.enhancedCoder.decode(m, "de.anomic.data.blogBoard.page()"); }
if (b == null) return "".getBytes(); public byte[] getPage() {
return b; String page = record.get("page");
if (page == null) return new byte[0];
byte[] page_as_byte = kelondroBase64Order.enhancedCoder.decode(page, "de.anomic.data.blogBoard.page()");
if (page_as_byte == null) return "".getBytes();
return page_as_byte;
} }
private void setPage(byte[] page) {
public void addComment(String commentID) { if (page == null)
record.put("page", "");
else
record.put("page", kelondroBase64Order.enhancedCoder.encode(page));
}
public void addComment(String commentID) {
ArrayList<String> comments = listManager.string2arraylist(record.get("comments")); ArrayList<String> comments = listManager.string2arraylist(record.get("comments"));
comments.add(commentID); comments.add(commentID);
record.put("comments", listManager.collection2string(comments)); record.put("comments", listManager.collection2string(comments));
} }
public boolean removeComment(String commentID) { public boolean removeComment(String commentID) {
ArrayList<String> comments = listManager.string2arraylist(record.get("comments")); ArrayList<String> comments = listManager.string2arraylist(record.get("comments"));
boolean success = comments.remove(commentID); boolean success = comments.remove(commentID);
record.put("comments", listManager.collection2string(comments)); record.put("comments", listManager.collection2string(comments));
return success; return success;
} }
public int getCommentMode(){ public int getCommentMode(){
return Integer.parseInt(record.get("commentMode")); return Integer.parseInt(record.get("commentMode"));
} }
private void setCommentMode(String mode) {
if (mode == null)
record.put("commentMode", "1");
else
record.put("commentMode", mode);
}
public boolean isPublic() { public boolean isPublic() {
return true; String privacy = record.get("privacy");
if (privacy == null)
return true;
if(privacy.equalsIgnoreCase("public"))
return true;
return false;
} }
} }
/*
public String write(BlogEntry page) { * writes a new page and return the key
// writes a new page and returns key */
public String writeBlogEntry(BlogEntry page) {
try { try {
datbase.set(page.key, page.record); database.set(page.key, page.record);
return page.key; return page.key;
} catch (IOException e) { } catch (IOException e) {
return null; return null;
} }
} }
public BlogEntry readBlogEntry(String key) {
public BlogEntry read(String key) { return readBlogEntry(key, database);
return read(key, datbase);
} }
private BlogEntry readBlogEntry(String key, kelondroMapObjects base) {
private BlogEntry read(String key, kelondroMapObjects base) {
key = normalize(key); key = normalize(key);
if (key.length() > keyLength) key = key.substring(0, keyLength); if (key.length() > keyLength)
key = key.substring(0, keyLength);
HashMap<String, String> record = base.getMap(key); HashMap<String, String> record = base.getMap(key);
if (record == null) return newEntry(key, "".getBytes(), "anonymous".getBytes(), "127.0.0.1", new Date(), "".getBytes(), null, null); if (record == null)
return newEntry(key, "".getBytes(), "anonymous".getBytes(), "127.0.0.1", new Date(), "".getBytes(), null, null);
return new BlogEntry(key, record); return new BlogEntry(key, record);
} }
public boolean importXML(String input) { public boolean importXML(String input) {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try { try {
@ -282,7 +303,6 @@ public class blogBoard {
return false; return false;
} }
private boolean parseXMLimport(Document doc) { private boolean parseXMLimport(Document doc) {
if(!doc.getDocumentElement().getTagName().equals("blog")) if(!doc.getDocumentElement().getTagName().equals("blog"))
return false; return false;
@ -342,20 +362,18 @@ public class blogBoard {
page = StrPage.getBytes(); page = StrPage.getBytes();
} }
write (newEntry(key, subject, author, ip, date, page, null, null)); writeBlogEntry (newEntry(key, subject, author, ip, date, page, null, null));
} }
return true; return true;
} }
public void deleteBlogEntry(String key) {
public void delete(String key) {
key = normalize(key); key = normalize(key);
try { try {
datbase.remove(key); database.remove(key);
} catch (IOException e) { } } catch (IOException e) { }
} }
public Iterator<String> keys(boolean up) throws IOException { public Iterator<String> keys(boolean up) throws IOException {
return datbase.keys(up, false); return database.keys(up, false);
} }
/** /**
* Comparator to sort objects of type Blog according to their timestamps * Comparator to sort objects of type Blog according to their timestamps
@ -368,30 +386,32 @@ public class blogBoard {
* @param newestFirst newest first, or oldest first? * @param newestFirst newest first, or oldest first?
*/ */
public BlogComparator(boolean newestFirst){ public BlogComparator(boolean newestFirst){
this.newestFirst=newestFirst; this.newestFirst = newestFirst;
} }
public int compare(String obj1, String obj2) { public int compare(String obj1, String obj2) {
BlogEntry bm1=read(obj1); BlogEntry blogEntry1=readBlogEntry(obj1);
BlogEntry bm2=read(obj2); BlogEntry blogEntry2=readBlogEntry(obj2);
if(bm1==null || bm2==null) if(blogEntry1 == null || blogEntry2 == null)
return 0; //XXX: i think this should not happen? maybe this needs further tracing of the bug return 0;
if(this.newestFirst){ if(this.newestFirst){
if(Long.valueOf(bm2.timestamp()) - Long.valueOf(bm1.timestamp()) >0) return 1; if(Long.valueOf(blogEntry2.getTimestamp()) - Long.valueOf(blogEntry1.getTimestamp()) >0)
return 1;
return -1; return -1;
} }
if(Long.valueOf(bm1.timestamp()) - Long.valueOf(bm2.timestamp()) >0) return 1; if(Long.valueOf(blogEntry1.getTimestamp()) - Long.valueOf(blogEntry2.getTimestamp()) >0)
return 1;
return -1; return -1;
} }
} }
public Iterator<String> getBlogIterator(boolean priv){ public Iterator<String> getBlogIterator(boolean priv){
TreeSet<String> set=new TreeSet<String>(new BlogComparator(false)); TreeSet<String> set = new TreeSet<String>(new BlogComparator(true));
Iterator<BlogEntry> it=blogIterator(true); Iterator<BlogEntry> iterator = blogIterator(priv);
BlogEntry bm; BlogEntry blogEntry;
while(it.hasNext()){ while(iterator.hasNext()){
bm=(BlogEntry)it.next(); blogEntry=(BlogEntry)iterator.next();
if(priv || bm.isPublic()){ if(priv || blogEntry.isPublic()){
set.add(bm.key()); set.add(blogEntry.getKey());
} }
} }
return set.iterator(); return set.iterator();
@ -411,7 +431,7 @@ public class blogBoard {
blogBoard.BlogEntry nextEntry; blogBoard.BlogEntry nextEntry;
public BlogIterator(boolean up) throws IOException { public BlogIterator(boolean up) throws IOException {
//flushBookmarkCache(); //XXX: this will cost performance //flushBookmarkCache(); //XXX: this will cost performance
this.blogIter = blogBoard.this.datbase.keys(up, false); this.blogIter = blogBoard.this.database.keys(up, false);
this.nextEntry = null; this.nextEntry = null;
} }
@ -426,7 +446,7 @@ public class blogBoard {
public BlogEntry next() { public BlogEntry next() {
try { try {
return read((String) this.blogIter.next()); return readBlogEntry((String) this.blogIter.next());
} catch (kelondroException e) { } catch (kelondroException e) {
//resetDatabase(); //resetDatabase();
return null; return null;
@ -436,8 +456,8 @@ public class blogBoard {
public void remove() { public void remove() {
if (this.nextEntry != null) { if (this.nextEntry != null) {
try { try {
Object blogKey = this.nextEntry.key(); Object blogKey = this.nextEntry.getKey();
if (blogKey != null) delete((String) blogKey); if (blogKey != null) deleteBlogEntry((String) blogKey);
} catch (kelondroException e) { } catch (kelondroException e) {
//resetDatabase(); //resetDatabase();
} }

Loading…
Cancel
Save