Index: lams_bb_integration/src/org/lamsfoundation/ld/integration/blackboard/CloneLessonsServlet.java =================================================================== diff -u -radc0d80ed42274e4f08b4dbd588f50eb1715cd39 -r1e67eface854ed42fb8afdc60fddc2e7c78d9075 --- lams_bb_integration/src/org/lamsfoundation/ld/integration/blackboard/CloneLessonsServlet.java (.../CloneLessonsServlet.java) (revision adc0d80ed42274e4f08b4dbd588f50eb1715cd39) +++ lams_bb_integration/src/org/lamsfoundation/ld/integration/blackboard/CloneLessonsServlet.java (.../CloneLessonsServlet.java) (revision 1e67eface854ed42fb8afdc60fddc2e7c78d9075) @@ -20,7 +20,6 @@ * **************************************************************** */ - package org.lamsfoundation.ld.integration.blackboard; import java.io.IOException; @@ -37,15 +36,14 @@ import org.lamsfoundation.ld.integration.util.LamsSecurityUtil; import org.lamsfoundation.ld.integration.util.LineitemUtil; +import blackboard.data.ValidationException; import blackboard.data.content.Content; import blackboard.data.course.Course; import blackboard.data.user.User; +import blackboard.persist.PersistenceException; import blackboard.persist.PkId; import blackboard.persist.content.ContentDbPersister; import blackboard.persist.course.CourseDbLoader; -import blackboard.platform.BbServiceManager; -import blackboard.platform.context.Context; -import blackboard.platform.context.ContextManager; import blackboard.util.StringUtil; /** @@ -58,101 +56,119 @@ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - String courseIdParam = request.getParameter("courseId"); + String courseIdParam = request.getParameter("courseId"); if (StringUtil.isEmpty(courseIdParam)) { throw new RuntimeException("Required parameters are missing. courseId: " + courseIdParam); } String newLessonIds = ""; try { - // get Blackboard context - ContextManager ctxMgr = (ContextManager) BbServiceManager.lookupService(ContextManager.class); - Context ctx = ctxMgr.setContext(request); - ContentDbPersister persister =ContentDbPersister.Default.getInstance(); - - CourseDbLoader courseLoader = CourseDbLoader.Default.getInstance(); - Course course = courseLoader.loadByCourseId(courseIdParam); - PkId courseId = (PkId) course.getId(); - String _course_id = "_" + courseId.getPk1() + "_" + courseId.getPk2(); - logger.debug("Starting clonning course lessons (courseId=" + courseId + ")."); - - // find a teacher that will be assigned as lesson's author on LAMS side - User teacher = BlackboardUtil.getCourseTeacher(courseId); - - //find all lessons that should be updated - List lamsContents = BlackboardUtil.getLamsLessonsByCourse(courseId); - for (Content content : lamsContents) { - - PkId contentId = (PkId) content.getId(); - String _content_id = "_" + contentId.getPk1() + "_" + contentId.getPk2(); - - String url = content.getUrl(); - String urlLessonId = getParameterValue(url, "lsid"); - String urlCourseId = getParameterValue(url, "course_id"); - String urlContentId = getParameterValue(url, "content_id"); - - //in case when both courseId and contentId don't coincide with the ones from URL - means lesson needs to be cloned - if (!urlCourseId.equals(_course_id) && !urlContentId.equals(_content_id)) { - - final Long newLessonId = LamsSecurityUtil.cloneLesson(teacher, courseIdParam, urlLessonId); - - // update lesson id - content.setLinkRef(Long.toString(newLessonId)); - - // update URL - url = replaceParameterValue(url, "lsid", Long.toString(newLessonId)); - url = replaceParameterValue(url, "course_id", _course_id); - url = replaceParameterValue(url, "content_id", _content_id); - content.setUrl(url); - - // persist updated content - persister.persist(content); - - //update lineitem details - LineitemUtil.updateLineitemLessonId(content, _course_id, newLessonId, ctx, teacher.getUserName()); - - logger.debug("Lesson (lessonId=" + urlLessonId + ") was successfully cloned to the one (lessonId=" - + newLessonId + ")."); - - newLessonIds += newLessonId + ", "; - } - - } - + newLessonIds = recreateLessonsAfterCourseCopy(courseIdParam); } catch (IllegalStateException e) { throw new ServletException( "LAMS Server timeout, did not get a response from the LAMS server. Please contact your systems administrator", e); } catch (Exception e) { throw new ServletException(e); } - + //prepare string to write out int newLessonsCounts = newLessonIds.length() - newLessonIds.replace(",", "").length(); String resultStr = "Complete! " + newLessonsCounts + " lessons have been cloned."; //add all lessonIds (without the last comma) if (newLessonsCounts > 0) { - resultStr += " Their updated lessonIds: " + newLessonIds.substring(0, newLessonIds.length()-2); + resultStr += " Their updated lessonIds: " + newLessonIds.substring(0, newLessonIds.length() - 2); } logger.debug(resultStr); - + response.setContentType("text/html"); PrintWriter out = response.getWriter(); - out.write(resultStr); - out.flush(); - out.close(); + out.write(resultStr); + out.flush(); + out.close(); } - + @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } - - /* + + /** + * Recreates lessons after course has been copied. I.e. asks LAMS server to clone old lesson and then updates BB + * link with the newly created lesson Id. + * + * @param courseIdParam + * id of the course that has been copied + * @return + * @throws PersistenceException + * @throws ValidationException + * @throws IOException + * @throws ServletException + */ + public static String recreateLessonsAfterCourseCopy(String courseIdParam) + throws PersistenceException, ValidationException, ServletException, IOException { + String newLessonIds = ""; + + ContentDbPersister persister = ContentDbPersister.Default.getInstance(); + CourseDbLoader courseLoader = CourseDbLoader.Default.getInstance(); + Course course = courseLoader.loadByCourseId(courseIdParam); + PkId courseId = (PkId) course.getId(); + String _course_id = "_" + courseId.getPk1() + "_" + courseId.getPk2(); + logger.debug("Starting clonning course lessons (courseId=" + courseId + ")."); + + // find a teacher that will be assigned as lesson's author on LAMS side + User teacher = BlackboardUtil.getCourseTeacher(courseId); + + //find all lessons that should be updated + List lamsContents = BlackboardUtil.getLamsLessonsByCourse(courseId); + for (Content content : lamsContents) { + + PkId contentId = (PkId) content.getId(); + String _content_id = "_" + contentId.getPk1() + "_" + contentId.getPk2(); + + String url = content.getUrl(); + String urlLessonId = getParameterValue(url, "lsid"); + String urlCourseId = getParameterValue(url, "course_id"); + String urlContentId = getParameterValue(url, "content_id"); + + //in case when both courseId and contentId don't coincide with the ones from URL - means lesson needs to be cloned + if (!urlCourseId.equals(_course_id) && !urlContentId.equals(_content_id)) { + + final Long newLessonId = LamsSecurityUtil.cloneLesson(teacher, courseIdParam, urlLessonId); + + // update lesson id + content.setLinkRef(Long.toString(newLessonId)); + + // update URL + url = replaceParameterValue(url, "lsid", Long.toString(newLessonId)); + url = replaceParameterValue(url, "course_id", _course_id); + url = replaceParameterValue(url, "content_id", _content_id); + content.setUrl(url); + + // persist updated content + persister.persist(content); + + //update lineitem details + LineitemUtil.updateLineitemLessonId(content, _course_id, newLessonId, teacher.getUserName()); + + logger.debug("Lesson (lessonId=" + urlLessonId + ") was successfully cloned to the one (lessonId=" + + newLessonId + ")."); + + newLessonIds += newLessonId + ", "; + } + + } + + return newLessonIds; + } + + /** * Returns param value, and empty string in case of there is no such param available * * @param url + * * @param paramName + * * @return */ private static String getParameterValue(String url, String paramName) { @@ -171,7 +187,7 @@ return paramValue; } - + private static String replaceParameterValue(String url, String paramName, String newParamValue) { String oldParamValue = ""; Index: lams_bb_integration/src/org/lamsfoundation/ld/integration/blackboard/CorrectLineitemsServlet.java =================================================================== diff -u -radc0d80ed42274e4f08b4dbd588f50eb1715cd39 -r1e67eface854ed42fb8afdc60fddc2e7c78d9075 --- lams_bb_integration/src/org/lamsfoundation/ld/integration/blackboard/CorrectLineitemsServlet.java (.../CorrectLineitemsServlet.java) (revision adc0d80ed42274e4f08b4dbd588f50eb1715cd39) +++ lams_bb_integration/src/org/lamsfoundation/ld/integration/blackboard/CorrectLineitemsServlet.java (.../CorrectLineitemsServlet.java) (revision 1e67eface854ed42fb8afdc60fddc2e7c78d9075) @@ -41,9 +41,6 @@ import blackboard.data.user.User; import blackboard.persist.PkId; import blackboard.persist.course.CourseDbLoader; -import blackboard.platform.BbServiceManager; -import blackboard.platform.context.Context; -import blackboard.platform.context.ContextManager; import blackboard.util.StringUtil; /** @@ -62,10 +59,6 @@ } try { - // get Blackboard context - ContextManager ctxMgr = (ContextManager) BbServiceManager.lookupService(ContextManager.class); - Context ctx = ctxMgr.setContext(request); - CourseDbLoader courseLoader = CourseDbLoader.Default.getInstance(); Course course = courseLoader.loadByCourseId(courseIdParam); PkId courseId = (PkId) course.getId(); @@ -83,7 +76,7 @@ String lessonId = content.getLinkRef(); //update lineitem details - LineitemUtil.updateLineitemLessonId(content, _course_id, Long.parseLong(lessonId), ctx, + LineitemUtil.updateLineitemLessonId(content, _course_id, Long.parseLong(lessonId), teacher.getUserName()); } Index: lams_bb_integration/src/org/lamsfoundation/ld/integration/blackboard/ImportLessonsServlet.java =================================================================== diff -u -r4cbb8f0a143a5ceed24a3de664315b501a8cbbdf -r1e67eface854ed42fb8afdc60fddc2e7c78d9075 --- lams_bb_integration/src/org/lamsfoundation/ld/integration/blackboard/ImportLessonsServlet.java (.../ImportLessonsServlet.java) (revision 4cbb8f0a143a5ceed24a3de664315b501a8cbbdf) +++ lams_bb_integration/src/org/lamsfoundation/ld/integration/blackboard/ImportLessonsServlet.java (.../ImportLessonsServlet.java) (revision 1e67eface854ed42fb8afdc60fddc2e7c78d9075) @@ -40,17 +40,12 @@ import org.lamsfoundation.ld.integration.util.LineitemUtil; import blackboard.base.FormattedText; -import blackboard.base.InitializationException; import blackboard.data.content.Content; import blackboard.data.course.Course; import blackboard.data.user.User; import blackboard.persist.PkId; import blackboard.persist.content.ContentDbPersister; import blackboard.persist.course.CourseDbLoader; -import blackboard.platform.BbServiceException; -import blackboard.platform.BbServiceManager; -import blackboard.platform.context.Context; -import blackboard.platform.context.ContextManager; import blackboard.util.StringUtil; /** @@ -70,11 +65,7 @@ String newLessonIds = ""; try { - // get Blackboard context - ContextManager ctxMgr = (ContextManager) BbServiceManager.lookupService(ContextManager.class); - Context ctx = ctxMgr.setContext(request); ContentDbPersister persister =ContentDbPersister.Default.getInstance(); - CourseDbLoader courseLoader = CourseDbLoader.Default.getInstance(); Course course = courseLoader.loadByCourseId(courseIdParam); PkId courseId = (PkId) course.getId(); @@ -126,7 +117,7 @@ persister.persist(content); //update lineitem details - LineitemUtil.updateLineitemLessonId(content, _course_id, newLessonId, ctx, teacher.getUserName()); + LineitemUtil.updateLineitemLessonId(content, _course_id, newLessonId, teacher.getUserName()); logger.debug("Lesson (lessonId=" + urlLessonId + ") was successfully imported to the one (lessonId=" + newLessonId + ")."); @@ -147,10 +138,6 @@ throw new ServletException( "LAMS Server timeout, did not get a response from the LAMS server. Please contact your systems administrator", e); - } catch (InitializationException e) { - throw new ServletException(e); - } catch (BbServiceException e) { - throw new ServletException(e); } catch (Exception e) { throw new ServletException(e); } Index: lams_bb_integration/src/org/lamsfoundation/ld/integration/util/BlackboardUtil.java =================================================================== diff -u -r4cbb8f0a143a5ceed24a3de664315b501a8cbbdf -r1e67eface854ed42fb8afdc60fddc2e7c78d9075 --- lams_bb_integration/src/org/lamsfoundation/ld/integration/util/BlackboardUtil.java (.../BlackboardUtil.java) (revision 4cbb8f0a143a5ceed24a3de664315b501a8cbbdf) +++ lams_bb_integration/src/org/lamsfoundation/ld/integration/util/BlackboardUtil.java (.../BlackboardUtil.java) (revision 1e67eface854ed42fb8afdc60fddc2e7c78d9075) @@ -7,9 +7,12 @@ import java.util.Calendar; import java.util.List; +import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import org.apache.log4j.Logger; + import blackboard.base.BbList; import blackboard.base.FormattedText; import blackboard.data.ValidationException; @@ -26,6 +29,7 @@ import blackboard.persist.PkId; import blackboard.persist.content.ContentDbLoader; import blackboard.persist.content.ContentDbPersister; +import blackboard.persist.course.CourseDbLoader; import blackboard.persist.course.CourseMembershipDbLoader; import blackboard.persist.navigation.CourseTocDbLoader; import blackboard.persist.user.UserDbLoader; Index: lams_bb_integration/src/org/lamsfoundation/ld/integration/util/LineitemUtil.java =================================================================== diff -u -r4cbb8f0a143a5ceed24a3de664315b501a8cbbdf -r1e67eface854ed42fb8afdc60fddc2e7c78d9075 --- lams_bb_integration/src/org/lamsfoundation/ld/integration/util/LineitemUtil.java (.../LineitemUtil.java) (revision 4cbb8f0a143a5ceed24a3de664315b501a8cbbdf) +++ lams_bb_integration/src/org/lamsfoundation/ld/integration/util/LineitemUtil.java (.../LineitemUtil.java) (revision 1e67eface854ed42fb8afdc60fddc2e7c78d9075) @@ -135,9 +135,9 @@ */ private static boolean hasLessonScoreOutputs(Content bbContent, String ldId, String username) throws IOException { - //sequence_id parameter is null in case we come from modify_proc + //sequence_id parameter is null in case we come from modify_proc or CorrectLineItemServlet/CloneLessonsServlet/ImportLessonsServlet if (ldId == null) { - //get sequence_id from bbcontent URL set in start_lesson_proc + //get sequence_id from bbcontent URL, set in start_lesson_proc String bbContentUrl = bbContent.getUrl(); String[] params = bbContentUrl.split("&"); for (String param : params) { @@ -236,7 +236,7 @@ * @throws ValidationException * @throws IOException */ - public static void updateLineitemLessonId(Content bbContent, String courseIdStr, Long newLamsLessonId, Context ctx, String userName) + public static void updateLineitemLessonId(Content bbContent, String courseIdStr, Long newLamsLessonId, String userName) throws PersistenceException, ServletException, ValidationException, IOException { LineitemDbLoader lineitemLoader = LineitemDbLoader.Default.getInstance(); LineitemDbPersister linePersister = LineitemDbPersister.Default.getInstance(); @@ -250,8 +250,7 @@ Id lineitemId = getLineitem(_content_id, courseIdStr, false); //in case admin forgot to check "Grade Center Columns and Settings" option on doing course copy/import if (lineitemId == null) { - String ldId = ctx.getRequestParameter("sequence_id"); - createLineitem(bbContent, ldId, userName); + createLineitem(bbContent, null, userName); //in case he checked it and BB created Lineitem object, then just need to update it } else {