package com.appspot.gaeforum309.web;
import java.util.List;
import com.appspot.gaeforum309.core.ForumAPI;
import com.appspot.gaeforum309.db.DBCategory;
import com.appspot.gaeforum309.db.DBComment;
import com.appspot.gaeforum309.db.DBConversation;
import com.appspot.gaeforum309.db.DBTopic;
import com.appspot.gaeforum309.db.DBUser;
import com.appspot.gaeforum309.pathing.Path;
import com.appspot.gaeforum309.pathing.PathBuilder.IPathable;
// TODO : Add date formatter. Each user can configure his date format, and time zone.
//TODO : Template must not directly have url or field name (in form). It must call ForumPages of ForumAction for obtains informations.
public class TemplateDefault extends Template {
ForumPages.PageInfo pageInfo;
@Override
public void initTemplate(ForumPages.PageInfo pageInfo) {
this.pageInfo = pageInfo;
addSection(DBCategory.tagName, new Template.ITemplateSection() {
@Override
public void head(StringBuilder response, Path path, IPathable object) {
DBCategory category = (DBCategory)object;
response.append("<H2><a href='"+ ForumPages.Page.getUrl(category) +"'>" + category.getTitre()+"</a></H2><b>"+category.getDescription()+"</b>");
response.append("<table border='1' width='100%'><tr><th width='10px'></th><th>Topic</th></tr>");
}
@Override
public void foot(StringBuilder response, Path path, IPathable object) {
response.append("</table>");
}
@Override
public void body(StringBuilder response, Path path, IPathable object) {
}
});
addSection(DBTopic.tagName, new Template.ITemplateSection() {
@Override
public void head(StringBuilder response, Path path, IPathable object) {
DBTopic topic = (DBTopic)object;
response.append("<H2><a href='"+ ForumPages.Page.getUrl(topic)+"'>" + topic.getTitle() + "</a></H2>");
response.append("<table border='1' width='100%'><tr><th width='10px'></th><th>Subject</th><th>Author</th><th>Last message</th></th>");
}
@Override
public void foot(StringBuilder response, Path path, IPathable object) {
DBTopic topic = (DBTopic)object;
response.append("<H2><a href='"+ForumPages.Page.getUrl(topic)+"'>" + topic.getTitle() + "</a></H2>");
response.append("</table>");
}
@Override
public void body(StringBuilder response, Path path, IPathable object) {
DBTopic topic = (DBTopic)object;
response.append("<tr><td>X</td><td><b><a href='"+ ForumPages.Page.getUrl(topic) +"'>"+topic.getTitle()+"</a>:</b> "+topic.getDescription()+"</b></td></tr>");
}
});
addSection(DBConversation.tagName, new Template.ITemplateSection() {
@Override
public void head(StringBuilder response, Path path, IPathable object) {
response.append("<table border='1' width='100%'><tr><th width='5px'>Author</th><th>Message</th></tr>");
}
@Override
public void foot(StringBuilder response, Path path, IPathable object) {
DBConversation conv = (DBConversation) path.getObjectByTagName(DBConversation.tagName);
response.append("</table>");
if(ForumAPI.loggedUser())
{
response.append("<p>Comment this discussion:</p>" +
"<form action='/forumAction' method='post' >" +
"<input type='hidden' name='action' value='addComment' />" +
"<input type='hidden' name='conversationKey' value='"+ conv.getKeyStr() +"' />" +
"<textarea style='width:100%' rows=7 name='comment'></textarea><br />" +
"<input type='submit' />" +
"</form>");
}
else
{
response.append("<p>Please log you for comment this conversation.</p>");
}
}
@Override
public void body(StringBuilder response, Path path, IPathable object) {
DBConversation conversation = (DBConversation)object;
@SuppressWarnings("unchecked")
List<DBComment> lcomment = (List<DBComment>) conversation.getChilds(0, 1);
DBComment dbcomment = null;
if(lcomment.size() == 0)
return;
dbcomment = lcomment.get(0);
response.append("<tr><td>X</td><td><b><a href='" + ForumPages.Page.getUrl(conversation) + "'>"+ conversation.getTitle() + "</a></b><br />" + conversation.getDescription() + "</td><td>"+ dbcomment.getAuthor().getEmail() +"</td><td>"+ dbcomment.getDateDiscription() +"</td>");
}
});
addSection(DBComment.tagName, new Template.ITemplateSection() {
@Override
public void head(StringBuilder response, Path path, IPathable object) {
}
@Override
public void foot(StringBuilder response, Path path, IPathable object) {
}
@Override
public void body(StringBuilder response, Path path, IPathable object) {
DBComment comment = (DBComment) object;
response.append("<tr><td width='5px'>" + comment.getAuthor().getEmail() + "<br />" + comment.getDateDiscription() + "</td><td><div>"+ comment.getTextComment().replaceAll("\n", "<br />") +"</div></td></tr>");
}
});
}
@Override
public String contentType() {
return "text/html";
}
@Override
public void beginPage(StringBuilder response, Path path)
{
response.append("<html><body>");
response.append("<H1>Forum " + ForumAPI.Config.getForumName() + "</H1>");
if(ForumAPI.loggedUser())
{
DBUser dbuser = ForumAPI.userLogin();
response.append("<p>Welcome Mr " + dbuser.getEmail() + "</p>");
if(ForumAPI.loggedUserAsAdmin())
{
response.append("<p>You are an administrator.</p>");
}
}
else
{
response.append("<p>Please log you with your Google Account to post comment.</p><a href='"+ ForumAPI.userLoginLogoutUrl("/forum") +"'>Log me</a>");
}
response.append("<p>");
for(IPathable p:path.getPath())
{
response.append("/");
response.append(p.label());
}
response.append("</p>");
}
@Override
public void endPage(StringBuilder response, Path path) {
response.append("<p>");
// TODO: Next/previous page
response.append("</body></html>");
}
}