Index: lams_tool_doku/src/java/org/lamsfoundation/lams/tool/dokumaran/service/DokumaranService.java =================================================================== diff -u -r10a1136c72ffe94801498ff7fb718dc9b582c521 -r0ba6a6fd46e33bec7edc04b520ad8fd529ecd634 --- lams_tool_doku/src/java/org/lamsfoundation/lams/tool/dokumaran/service/DokumaranService.java (.../DokumaranService.java) (revision 10a1136c72ffe94801498ff7fb718dc9b582c521) +++ lams_tool_doku/src/java/org/lamsfoundation/lams/tool/dokumaran/service/DokumaranService.java (.../DokumaranService.java) (revision 0ba6a6fd46e33bec7edc04b520ad8fd529ecd634) @@ -354,7 +354,7 @@ break; } } - } catch (DokumaranConfigurationException e1) { + } catch (Exception e1) { log.debug(e1.getMessage()); } @@ -775,33 +775,26 @@ @Override public Cookie createEtherpadCookieForLearner(DokumaranUser user, DokumaranSession session) - throws DokumaranConfigurationException, URISyntaxException { + throws DokumaranConfigurationException, URISyntaxException, DokumaranApplicationException { + String groupId = session.getEtherpadGroupId(); + + //don't allow sessions that has had problems with pad initializations. they could be fixed in monitoring by a teacher + if (StringUtils.isEmpty(session.getEtherpadReadOnlyId()) || StringUtils.isEmpty(groupId)) { + throw new DokumaranApplicationException( + "This session has had problems with initialization. Please seek help from your teacher."); + } EPLiteClient client = initializeEPLiteClient(); - String groupId = session.getEtherpadGroupId(); String userName = user.getFirstName() + " " + user.getLastName(); Map map = client.createAuthorIfNotExistsFor(user.getUserId().toString(), userName); String authorId = map.get("authorID"); // search for already existing user's session at Etherpad server - Map sessionsMap = client.listSessionsOfAuthor(authorId); - String userSessionId = null; - for (String sessionId : (Set) sessionsMap.keySet()) { - Map sessessionAttributes = (Map) sessionsMap.get(sessionId); - String groupIdIter = sessessionAttributes.get("groupID"); - if (groupIdIter.equals(groupId)) { - userSessionId = sessionId; - } - } + Map etherpadSessions = client.listSessionsOfAuthor(authorId); + String etherpadSessionId = getEtherpadSession(authorId, groupId, etherpadSessions); - // if session doesn't exist yet - create it - if (userSessionId == null) { - Map map2 = client.createSession(groupId, authorId, 24); - userSessionId = (String) map2.get("sessionID"); - } - - return createEtherpadCookie(userSessionId); + return createEtherpadCookie(etherpadSessionId); } @Override @@ -814,8 +807,6 @@ Map map = client.createAuthorIfNotExistsFor(user.getUserID().toString(), userName); String authorId = map.get("authorID"); - Map etherpadSessions = client.listSessionsOfAuthor(authorId); - List sessionList = dokumaranSessionDao.getByContentId(contentId); if (sessionList.isEmpty()) { return null; @@ -829,6 +820,7 @@ // find according session String etherpadSessionIds = ""; + Map etherpadSessions = client.listSessionsOfAuthor(authorId); for (DokumaranSession session : sessionList) { String groupId = session.getEtherpadGroupId(); @@ -837,27 +829,52 @@ continue; } - // search for already existing user's session - String userSessionId = null; - for (String etherpadSessionId : (Set) etherpadSessions.keySet()) { - Map sessessionAttributes = (Map) etherpadSessions - .get(etherpadSessionId); - String groupIdIter = sessessionAttributes.get("groupID"); - if (groupIdIter.equals(groupId)) { - userSessionId = etherpadSessionId; + String etherpadSessionId = getEtherpadSession(authorId, groupId, etherpadSessions); + etherpadSessionIds += StringUtils.isEmpty(etherpadSessionIds) ? etherpadSessionId : "," + etherpadSessionId; + } + + return createEtherpadCookie(etherpadSessionIds); + } + + /** + * Returns valid Etherpad session. Returns existing one if finds such one and creates the new one otherwise + */ + private String getEtherpadSession(String authorId, String etherpadGroupId, Map etherpadSessions) { + String etherpadSessionId = null; + + // search for already existing user's session + boolean isValidForMoreThan1Hour = false; + for (String etherpadSessionIdIter : (Set) etherpadSessions.keySet()) { + Map sessessionAttributes = (Map) etherpadSessions + .get(etherpadSessionIdIter); + String groupIdIter = (String) sessessionAttributes.get("groupID"); + if (groupIdIter.equals(etherpadGroupId)) { + + // check session expiration date + long validUntil = (Long) sessessionAttributes.get("validUntil") * 1000; + long now = System.currentTimeMillis(); + isValidForMoreThan1Hour = ((validUntil - now) > 0) && ((validUntil - now) >= 60 * 60 * 1000); + + //use existing session if it's valid for more than 1 hour + if (isValidForMoreThan1Hour) { + etherpadSessionId = etherpadSessionIdIter; + break; + + } else { + // can't delete expired sessions as Etherpad throws an exception. Nonetheless it returns expired + // ones when client.listSessionsOfAuthor(authorId) is requested } } - // if session doesn't exist yet - create it - if (userSessionId == null) { - Map map2 = client.createSession(groupId, authorId, 24); - userSessionId = (String) map2.get("sessionID"); - } + } - etherpadSessionIds += StringUtils.isEmpty(etherpadSessionIds) ? userSessionId : "," + userSessionId; + // if session with validity of more than 1 hour doesn't exist yet - create it + if (etherpadSessionId == null) { + Map map2 = client.createSession(etherpadGroupId, authorId, 24); + etherpadSessionId = (String) map2.get("sessionID"); } - return createEtherpadCookie(etherpadSessionIds); + return etherpadSessionId; } /** Index: lams_tool_doku/src/java/org/lamsfoundation/lams/tool/dokumaran/service/IDokumaranService.java =================================================================== diff -u -r10a1136c72ffe94801498ff7fb718dc9b582c521 -r0ba6a6fd46e33bec7edc04b520ad8fd529ecd634 --- lams_tool_doku/src/java/org/lamsfoundation/lams/tool/dokumaran/service/IDokumaranService.java (.../IDokumaranService.java) (revision 10a1136c72ffe94801498ff7fb718dc9b582c521) +++ lams_tool_doku/src/java/org/lamsfoundation/lams/tool/dokumaran/service/IDokumaranService.java (.../IDokumaranService.java) (revision 0ba6a6fd46e33bec7edc04b520ad8fd529ecd634) @@ -103,7 +103,7 @@ */ boolean isLeaderResponseFinalized(Long toolSessionId); - Cookie createEtherpadCookieForLearner(DokumaranUser user, DokumaranSession session) throws DokumaranConfigurationException, URISyntaxException; + Cookie createEtherpadCookieForLearner(DokumaranUser user, DokumaranSession session) throws DokumaranConfigurationException, URISyntaxException, DokumaranApplicationException; Cookie createEtherpadCookieForMonitor(UserDTO user, Long contentId) throws DokumaranConfigurationException, URISyntaxException; Index: lams_tool_doku/src/java/org/lamsfoundation/lams/tool/dokumaran/web/action/LearningAction.java =================================================================== diff -u -reeb7734f1d37604e1b9e14b4df3214d5cdd0338b -r0ba6a6fd46e33bec7edc04b520ad8fd529ecd634 --- lams_tool_doku/src/java/org/lamsfoundation/lams/tool/dokumaran/web/action/LearningAction.java (.../LearningAction.java) (revision eeb7734f1d37604e1b9e14b4df3214d5cdd0338b) +++ lams_tool_doku/src/java/org/lamsfoundation/lams/tool/dokumaran/web/action/LearningAction.java (.../LearningAction.java) (revision 0ba6a6fd46e33bec7edc04b520ad8fd529ecd634) @@ -78,7 +78,7 @@ @Override public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, - HttpServletResponse response) throws IOException, ServletException, JSONException, DokumaranConfigurationException, URISyntaxException { + HttpServletResponse response) throws IOException, ServletException, JSONException, DokumaranConfigurationException, URISyntaxException, DokumaranApplicationException { String param = mapping.getParameter(); // -----------------------Dokumaran Learner function --------------------------- @@ -113,7 +113,7 @@ * */ private ActionForward start(ActionMapping mapping, ActionForm form, HttpServletRequest request, - HttpServletResponse response) throws DokumaranConfigurationException, URISyntaxException { + HttpServletResponse response) throws DokumaranConfigurationException, DokumaranApplicationException, URISyntaxException { // initial Session Map SessionMap sessionMap = new SessionMap();