/*
 * ICESOFT COMMERCIAL SOURCE CODE LICENSE V 1.1
 *
 * The contents of this file are subject to the ICEsoft Commercial Source
 * Code License Agreement V1.1 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of the
 * License at
 * http://www.icesoft.com/license/commercial-source-v1.1.html
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations under
 * the License.
 *
 * Copyright 2009-2015 ICEsoft Technologies Canada, Corp. All Rights Reserved.
 */
package com.icesoft.net.messaging;

import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.ServletContext;

public class TextMessage
extends AbstractMessage
implements Message {
    private static final Logger LOGGER = Logger.getLogger(TextMessage.class.getName());

    private String text;

    public TextMessage(final ServletContext servletContext) {
        super(servletContext);
    }

    public TextMessage(final String text, final ServletContext servletContext) {
        super(servletContext);
        setText(text);
    }

    public void append(final Message message) {
        if (message instanceof TextMessage) {
            TextMessage _textMessage = (TextMessage)message;
            String _text = _textMessage.getText();
            String _messageLengths;
            if (propertyExists(Message.MESSAGE_LENGTHS)) {
                _messageLengths = getStringProperty(Message.MESSAGE_LENGTHS) + "," + _text.length();
            } else {
                _messageLengths = text.length() + "," + _text.length();
            }
            setStringProperty(Message.MESSAGE_LENGTHS, _messageLengths);
            text = text + _text;
        }
    }

    public void clearBody() {
        text = null;
    }

    public int getLength() {
        return text.length();
    }

    public String getText() {
        return text;
    }

    public void setText(final String text) {
        this.text = text;
    }

    public String toString() {
        return new StringBuffer(super.toString()).append(getText()).append("\r\n").toString();
    }
}
