From 1242044c0232dec8fbe41356e8717d4b752b671a Mon Sep 17 00:00:00 2001 From: "balachandar.s" Date: Fri, 17 Mar 2017 23:19:34 +0530 Subject: [PATCH 1/2] Added Abstract Session Handler and Persistence Handler --- .../session/AbstractSessionManager.java | 153 ++++++++++++++++++ .../session/PersistenceHandler.java | 24 +++ 2 files changed, 177 insertions(+) create mode 100755 rivescript-core/src/main/java/com/rivescript/session/AbstractSessionManager.java create mode 100755 rivescript-core/src/main/java/com/rivescript/session/PersistenceHandler.java diff --git a/rivescript-core/src/main/java/com/rivescript/session/AbstractSessionManager.java b/rivescript-core/src/main/java/com/rivescript/session/AbstractSessionManager.java new file mode 100755 index 0000000..c83b389 --- /dev/null +++ b/rivescript-core/src/main/java/com/rivescript/session/AbstractSessionManager.java @@ -0,0 +1,153 @@ +/* + * Copyright (c) 2016 the original author or authors. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package com.rivescript.session; + + +import java.util.Collections; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + + +/** + * Implements the default {@link SessionManager} for RiveScript, based on a {@link ConcurrentHashMap}. + * + * @author Balachandar S + */ +public class AbstractSessionManager implements SessionManager { + private PersistenceHandler handler = null; + public AbstractSessionManager(PersistenceHandler handler) { + this.handler = handler; + } + + @Override + public UserData init(String username) { + if (handler.getUserData(username)==null) { + handler.createUserData(username, defaultSession()); + } + return handler.getUserData(username); + } + + @Override + public void set(String username, String name, String value) { + UserData userData = init(username); + userData.setVariable(name, value); + handler.updateUserData(username, userData); + } + + @Override + public void set(String username, Map vars) { + UserData userData = init(username); + for (Map.Entry var : vars.entrySet()) { + userData.setVariable(var.getKey(), var.getValue()); + } + handler.updateUserData(username, userData); + } + + @Override + public void addHistory(String username, String input, String reply) { + UserData userData = init(username); + Collections.rotate(userData.getHistory().getInput(), 1); // Rotate right. + userData.getHistory().getInput().set(0, input.trim()); // Now set the first item + Collections.rotate(userData.getHistory().getReply(), 1); // Rotate right. + userData.getHistory().getReply().set(0, reply.trim()); // Now set the first item + handler.updateUserData(username, userData); + } + + @Override + public void setLastMatch(String username, String trigger) { + UserData userData = init(username); + userData.setLastMatch(trigger); + handler.updateUserData(username, userData); + } + + @Override + public String get(String username, String name) { + return handler.getUserData(username).getVariable(name); + } + + @Override + public UserData get(String username) { + UserData userData = init(username); + if (userData==null) { + return null; + } + return userData; + } + + @Override + public Map getAll() { + return handler.getAllUserData(); + } + + @Override + public String getLastMatch(String username) { + UserData userData = init(username); + if (userData==null) { + return null; + } + return userData.getLastMatch(); + } + + @Override + public History getHistory(String username) { + UserData userData = init(username); + if (userData==null) { + return null; + } + return userData.getHistory(); + } + + @Override + public void clear(String username) { + handler.deleteUserData(username); + } + + @Override + public void clearAll() { + handler.deleteAllUserData(); + } + + @Override + public void freeze(String username) { + } + + @Override + public void thaw(String username, ThawAction action) { + } + + + /** + * Initializes the default session variables for a user. + *

+ * This mostly just means the topic is set to "random". + * + * @return the user data + */ + private UserData defaultSession() { + UserData userData = new UserData(); + userData.setVariable("topic", "random"); + userData.setLastMatch(""); + return userData; + } + +} diff --git a/rivescript-core/src/main/java/com/rivescript/session/PersistenceHandler.java b/rivescript-core/src/main/java/com/rivescript/session/PersistenceHandler.java new file mode 100755 index 0000000..43d2af9 --- /dev/null +++ b/rivescript-core/src/main/java/com/rivescript/session/PersistenceHandler.java @@ -0,0 +1,24 @@ +package com.rivescript.session; + +import java.util.Map; + +/** + * Provides an interface for persisting the userdata. + * + * @author Balachandar S + */ +public interface PersistenceHandler { + + public void createUserData(String username, UserData userdata); + + public void updateUserData(String username, UserData userdata); + + public UserData getUserData(String username); + + public void deleteUserData(String username); + + public Map getAllUserData(); + + public void deleteAllUserData(); + +} From b1e33c3d44eb28dd73b79c803378a14c1dcb93a1 Mon Sep 17 00:00:00 2001 From: "balachandar.s" Date: Thu, 13 Apr 2017 01:37:34 +0530 Subject: [PATCH 2/2] Modified Persistence Session Manager and Persistence Handler Interface --- .../session/PersistenceHandler.java | 10 ++++ ...er.java => PersistenceSessionManager.java} | 54 ++++++++++++------- 2 files changed, 44 insertions(+), 20 deletions(-) rename rivescript-core/src/main/java/com/rivescript/session/{AbstractSessionManager.java => PersistenceSessionManager.java} (74%) diff --git a/rivescript-core/src/main/java/com/rivescript/session/PersistenceHandler.java b/rivescript-core/src/main/java/com/rivescript/session/PersistenceHandler.java index 43d2af9..113f8da 100755 --- a/rivescript-core/src/main/java/com/rivescript/session/PersistenceHandler.java +++ b/rivescript-core/src/main/java/com/rivescript/session/PersistenceHandler.java @@ -21,4 +21,14 @@ public interface PersistenceHandler { public void deleteAllUserData(); + public void copyUserDataToPrimaryStorage(String username); + + public void moveUserDataToPrimaryStorage(String username); + + public void moveUserDataToSecondaryStorage(String username); + + public void removeUserDataFromSecondaryStorage(String username); + + + } diff --git a/rivescript-core/src/main/java/com/rivescript/session/AbstractSessionManager.java b/rivescript-core/src/main/java/com/rivescript/session/PersistenceSessionManager.java similarity index 74% rename from rivescript-core/src/main/java/com/rivescript/session/AbstractSessionManager.java rename to rivescript-core/src/main/java/com/rivescript/session/PersistenceSessionManager.java index c83b389..7c748d4 100755 --- a/rivescript-core/src/main/java/com/rivescript/session/AbstractSessionManager.java +++ b/rivescript-core/src/main/java/com/rivescript/session/PersistenceSessionManager.java @@ -22,29 +22,32 @@ package com.rivescript.session; +import static com.rivescript.session.ThawAction.DISCARD; +import static com.rivescript.session.ThawAction.KEEP; +import static com.rivescript.session.ThawAction.THAW; import java.util.Collections; import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - /** - * Implements the default {@link SessionManager} for RiveScript, based on a {@link ConcurrentHashMap}. + * Implements the default {@link SessionManager} for RiveScript * * @author Balachandar S */ -public class AbstractSessionManager implements SessionManager { +public class PersistenceSessionManager implements SessionManager { + private PersistenceHandler handler = null; - public AbstractSessionManager(PersistenceHandler handler) { + + public PersistenceSessionManager(PersistenceHandler handler) { this.handler = handler; } - + @Override public UserData init(String username) { - if (handler.getUserData(username)==null) { - handler.createUserData(username, defaultSession()); - } - return handler.getUserData(username); + if (handler.getUserData(username) == null) { + handler.createUserData(username, defaultSession()); + } + return handler.getUserData(username); } @Override @@ -66,10 +69,14 @@ public void set(String username, Map vars) { @Override public void addHistory(String username, String input, String reply) { UserData userData = init(username); - Collections.rotate(userData.getHistory().getInput(), 1); // Rotate right. - userData.getHistory().getInput().set(0, input.trim()); // Now set the first item - Collections.rotate(userData.getHistory().getReply(), 1); // Rotate right. - userData.getHistory().getReply().set(0, reply.trim()); // Now set the first item + Collections.rotate(userData.getHistory().getInput(), 1); // Rotate + // right. + userData.getHistory().getInput().set(0, input.trim()); // Now set the + // first item + Collections.rotate(userData.getHistory().getReply(), 1); // Rotate + // right. + userData.getHistory().getReply().set(0, reply.trim()); // Now set the + // first item handler.updateUserData(username, userData); } @@ -82,13 +89,13 @@ public void setLastMatch(String username, String trigger) { @Override public String get(String username, String name) { - return handler.getUserData(username).getVariable(name); + return handler.getUserData(username).getVariable(name); } @Override public UserData get(String username) { UserData userData = init(username); - if (userData==null) { + if (userData == null) { return null; } return userData; @@ -102,7 +109,7 @@ public Map getAll() { @Override public String getLastMatch(String username) { UserData userData = init(username); - if (userData==null) { + if (userData == null) { return null; } return userData.getLastMatch(); @@ -111,7 +118,7 @@ public String getLastMatch(String username) { @Override public History getHistory(String username) { UserData userData = init(username); - if (userData==null) { + if (userData == null) { return null; } return userData.getHistory(); @@ -129,13 +136,20 @@ public void clearAll() { @Override public void freeze(String username) { + handler.moveUserDataToSecondaryStorage(username); } @Override public void thaw(String username, ThawAction action) { + if (action == THAW) { + handler.copyUserDataToPrimaryStorage(username); + } else if (action == DISCARD) { + handler.removeUserDataFromSecondaryStorage(username); + } else if (action == KEEP) { + handler.moveUserDataToPrimaryStorage(username); + } } - /** * Initializes the default session variables for a user. *

@@ -149,5 +163,5 @@ private UserData defaultSession() { userData.setLastMatch(""); return userData; } - + }