package net.devrieze.chatterbox.server;

import java.util.Calendar;
import java.util.GregorianCalendar;

import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

@PersistenceCapable
public class Message {
  
  @SuppressWarnings("unused")
  @Persistent
  @PrimaryKey
  private Key key;
  
  @Persistent
  private Long index;
  
  @Persistent
  private String message;
  
  @Persistent
  private Long msgTime;

  public Message(long index, String message) {
    this.index = index;
    this.key = KeyFactory.createKey(Message.class.getSimpleName(), index);
    this.message = message;
    msgTime = Calendar.getInstance().getTimeInMillis();
  }

  public long getIndex() {
    return index;
  }
  
  public String getMessage() {
    return message;
  }

  public CharSequence toXML() {
    // Capacity estimated to 40 characters plus message length
    return new StringBuilder(40+message.length()).append("<message index=\"").append(getIndex()).append("\" epoch=\"")
          .append(msgTime).append("\">")
          .append(message).append("</message>");
  }

  @Override
  public String toString() {
    return new StringBuilder(12+message.length()).append(index).append(": ").append(message).toString();
  }
  
  public long getMsgTime() {
    return msgTime;
  }
  
  public GregorianCalendar getMsgDate() {
    // Explicit time so as to not to incur cost of getting current time
    GregorianCalendar calendar = new GregorianCalendar(1970, 1, 1);
    calendar.setTimeInMillis(msgTime);
    return calendar;
  }
  
}

@@ -330,16 +330,74 @@
 Factory;
+%0Aimport com.google.appengine.api.users.UserServiceFactory;
 %0A%0A@Persi
@@ -637,16 +637,56 @@
 gTime;%0A%0A
+  @Persistent%0A  private String source;%0A%0A
   public
@@ -851,16 +851,99 @@
 essage;%0A
+    this.source = UserServiceFactory.getUserService().getCurrentUser().getEmail();%0A
     msgT
@@ -1317,16 +1317,16 @@
 ch=%5C%22%22)%0A
-
         
@@ -1354,16 +1354,80 @@
 pend(%22%5C%22
+ from=%5C%22%22)%0A          .append(Util.encodeHtml(source)).append(%22%5C%22
 %3E%22)%0A    

Add a source to messages