Home > Articles > Programming > Java

  • Print
  • + Share This
From the author of

Message

The Message class represents a message being viewed or added to the message board. Every message contains the conventional name, email, subject, and body information. In addition to this data (provided by the user), the system provides a unique ID, a parent ID, which is the ID of the parent message to which the post is a reply, and a posted date. The class is as shown in Listing 2.

Listing 2 Message.java—Class Containing the Different Elements of a Message

import java.text.DateFormat;
import java.util.Date;
import org.apache.struts.action.ValidatingActionForm;
import org.apache.struts.util.ErrorMessages;

public class Message implements ValidatingActionForm {
    public final static String SPACE = " ";
    protected static DateFormat dformatter =
        DateFormat.getDateInstance(DateFormat.MEDIUM);
    protected static DateFormat tformatter =
        DateFormat.getTimeInstance(DateFormat.SHORT);

    protected String parentId;
    protected String id;
    protected String email;
    protected String name;
    protected String subject;
    protected String body;
    protected String timestamp;

    public Message () {
    }

    public Message (Message els) {
        inject(els);
    }

    public Message(String name, String email, String subject, String body) {
        setName(name);
        setEmail(email);
        setSubject(subject);
        setBody(body);
    }

    public void inject(Message els) {
        setParentId(els.getParentId());
        setId(els.getId());
        setEmail(els.getEmail());
        setName(els.getName());
        setSubject(els.getSubject());
        setBody(els.getBody());
        setTimestamp(els.getTimestamp());
    }

    public void setCurrentTimestamp() {
        Date current = new Date();
        setTimestamp(
          dformatter.format(current) + SPACE +
          tformatter.format(current));
    }

// Provide setter and getter methods for the different properties
…

    public String toString() {
        StringBuffer s = new StringBuffer();
        s.append("id[" + getId() + "], name[" + getName() + "], ");
        s.append("email[" + getEmail() + "], subject[" + getSubject() + "], ");
        s.append("body[" + getBody() + "], parentId[" + getParentId() +"]");
        return s.toString();
    }
}

Because this model class implements the ValidatingActionForm class, you will need to implement the validate() method that validates the different properties of the class. A valid message is identified by a non-null name, an email, and a body. The method returns an array of Strings that describe the encountered errors. You can add additional checks to determine whether the email has the format of name@domain.

To internationalize the error messages, you can use the ErrorMessages class provided in the Struts framework. You will need to specify error key-message entries in the application resources bundle for a specific locale, as described in Part 4 of this series: "Developing the Controller for the Application." To add an error message, invoke addError on the ErrorMessages instance, passing only the key. I have added an extra method _validate() that returns an instance of ErrorMessages to aggregate the errors encountered with the form validation with the ones that were encountered while invoking the action.

Listing 3 Message.java—Implementing the validate() Method of the ValidatingActionForm Interface

    public ErrorMessages _validate() {
        ErrorMessages errors = new ErrorMessages();
        if (getName() == null || getName().length() == 0) {
            errors.addError("error.name.missing");
        }
        if (getEmail() == null || getEmail().length() == 0) {
            errors.addError("error.email.missing");
        }
        if (getBody() == null || getBody().length() == 0) {
            errors.addError("error.body.missing");
        }
        return errors;
    }

    /**
     * Validate the properties of this form bean, and return an array of
     * message keys for any errors we encounter.
     */
    public String[] validate() {
        return _validate().getErrors();
    }

In addition to this rudimentary class that contains the different elements of the message, we will also create another object-oriented avatar of the class: OOMessage. As shown in Listing 4, this class provides relationships to the parent message and replies.

Listing 4 OOMessage.java—The OO Avatar of Message

import java.util.Vector;

public class OOMessage extends Message {
    protected Message parent = null;
    protected Vector replies = new Vector();

    public OOMessage() {
    }

     public OOMessage(Message els) {
        super(els);
    }

    public void setParent(OOMessage parMsg) {
        parentId = parMsg.getId();
        parent = parMsg;
        parMsg.addReply(this);
    }

    public OOMessage getReplyAt(int index) {
        return (OOMessage) replies.elementAt(index);
    }

    public int getReplyCount() {
        return replies.size();
    }

    public OOMessage[] getReplies() {
        OOMessage[] array= new OOMessage[replies.size()];
        replies.copyInto(array);
        return array;
    }

    public void addReply(OOMessage reply) {
        replies.addElement(reply);
    }

    public void setReplies(OOMessage[] children) {
        replies.removeAllElements();
        for (int i = 0; i < children.length; i++) {
            replies.addElement(children[i]);
        }
    }

    public void setReply(int index, OOMessage reply) {
        replies.insertElementAt(reply, index);
    }
  • + Share This
  • 🔖 Save To Your Account