001: package dalma.container;
002:
003: import dalma.Conversation;
004: import dalma.Engine;
005: import dalma.ConversationState;
006: import dalma.impl.XmlFile;
007:
008: import java.util.Date;
009: import java.util.List;
010: import java.util.Collections;
011: import java.util.Arrays;
012: import java.util.logging.LogRecord;
013: import java.util.logging.Logger;
014: import java.io.Serializable;
015: import java.io.File;
016: import java.io.IOException;
017:
018: /**
019: * {@link Conversation} that represents a completed one.
020: *
021: * <p>
022: * This object is immutable. From outside this object is accessed just as
023: * {@link Conversation}.
024: *
025: * @author Kohsuke Kawaguchi
026: */
027: final class CompletedConversation implements Conversation, Serializable {
028: private final int id;
029: private final String title;
030: private final long startDate;
031: private final long endDate;
032: private final LogRecord[] logs;
033:
034: private transient List<LogRecord> logView;
035:
036: /**
037: * Creates a new {@link CompletedConversation} from another {@link Conversation}.
038: */
039: CompletedConversation(Conversation that) {
040: this .id = that.getId();
041: this .title = that.getTitle();
042: this .startDate = that.getStartDate().getTime();
043: this .endDate = that.getCompletionDate().getTime();
044:
045: List<LogRecord> ll = that.getLog();
046: this .logs = ll.toArray(new LogRecord[ll.size()]);
047: }
048:
049: public int getId() {
050: return id;
051: }
052:
053: public Engine getEngine() {
054: throw uoe();
055: }
056:
057: public ConversationState getState() {
058: return ConversationState.ENDED;
059: }
060:
061: public void remove() {
062: throw uoe();
063: }
064:
065: public void join() {
066: throw uoe();
067: }
068:
069: public String getTitle() {
070: return title;
071: }
072:
073: public List<LogRecord> getLog() {
074: if (logView == null)
075: logView = Collections.unmodifiableList(Arrays.asList(logs));
076: return logView;
077: }
078:
079: public Date getStartDate() {
080: return new Date(startDate);
081: }
082:
083: public Date getCompletionDate() {
084: return new Date(endDate);
085: }
086:
087: private UnsupportedOperationException uoe() {
088: return new UnsupportedOperationException(
089: "This operation is not available on the completed conversation");
090: }
091:
092: /**
093: * Loads a {@link CompletedConversation} from a data file.
094: */
095: public static CompletedConversation load(File file)
096: throws IOException {
097: return (CompletedConversation) new XmlFile(file).read();
098: }
099:
100: /**
101: * Saves the object to a data file.
102: */
103: public void save(File file) throws IOException {
104: new XmlFile(file).write(this);
105: }
106: }
|