001 package org.tynamo.conversations.services;
002
003 public class Conversation {
004 private final String pageName;
005
006 private final String id;
007
008 private final boolean usingCookie;
009
010 private final long maxIdleSeconds;
011
012 private long lastTouched;
013
014 private long requiredEndTimestamp;
015
016 public boolean isUsingCookie() {
017 return usingCookie;
018 }
019
020 protected Conversation(String id, String pageName, Integer maxIdleSeconds, Integer maxConversationLengthSeconds, boolean usingCookie) {
021 if (maxIdleSeconds == null) maxIdleSeconds = 0;
022 if (maxConversationLengthSeconds == null) maxConversationLengthSeconds = 0;
023 this.id = id;
024 this.pageName = pageName;
025 this.usingCookie = usingCookie;
026 this.maxIdleSeconds = maxIdleSeconds;
027 this.requiredEndTimestamp = maxConversationLengthSeconds == 0 ? 0 : System.currentTimeMillis() + maxConversationLengthSeconds * 1000L;
028 touch();
029 }
030
031 public String getId() {
032 return id;
033 }
034
035 public String getPageName() {
036 return pageName;
037 }
038
039 public void touch() {
040 lastTouched = System.currentTimeMillis();
041 }
042
043 public Integer getSecondsBeforeBecomesIdle() {
044 if (maxIdleSeconds <= 0) return null;
045 return (int) ((maxIdleSeconds - (System.currentTimeMillis() - lastTouched) / 1000L));
046 }
047
048 /**
049 * True if conversation has been idle for too long or past its maxConversationLength, otherwise resets the idletime if
050 * resetIdle is true
051 **/
052 public boolean isIdle(boolean resetIdle) {
053 if (requiredEndTimestamp > 0) {
054 if (System.currentTimeMillis() > requiredEndTimestamp) return true;
055 }
056 if (maxIdleSeconds < 1) {
057 if (resetIdle) touch();
058 return false;
059 }
060 if ((System.currentTimeMillis() - lastTouched) / 1000L > maxIdleSeconds) return true;
061 if (resetIdle) touch();
062 return false;
063 }
064
065 }