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