--- /dev/null
+package com.pentila.entSavoie.edx;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import com.liferay.portal.kernel.exception.SystemException;
+import com.liferay.portal.kernel.json.JSONArray;
+import com.liferay.portal.kernel.json.JSONObject;
+import com.liferay.portal.kernel.log.Log;
+import com.liferay.portal.kernel.log.LogFactoryUtil;
+import com.pentila.entSavoie.edx.EdxConstants;
+import com.pentila.entSavoie.edx.model.EdxCourseItem;
+import com.pentila.entSavoie.edx.model.EdxCourseSection;
+import com.pentila.entSavoie.edx.model.EdxCourseSubSection;
+import com.pentila.entSavoie.edx.model.EdxCourseUnit;
+import com.pentila.entSavoie.edx.service.EdxCourseItemLocalServiceUtil;
+import com.pentila.entSavoie.edx.service.EdxCourseSectionLocalServiceUtil;
+import com.pentila.entSavoie.edx.service.EdxCourseSubSectionLocalServiceUtil;
+import com.pentila.entSavoie.edx.service.EdxCourseUnitLocalServiceUtil;
+
+public class EdxSynchronizationUtils {
+
+ private static Log _log = LogFactoryUtil.getLog(EdxSynchronizationUtils.class);
+
+ /**
+ * Synchronize in the DB the whole course with the given sections
+ * @param courseId
+ * @param sections
+ */
+ public static void synchronizeCourseSections(String courseId, JSONArray sections) {
+
+ // Get existing course sections in DB
+ List<EdxCourseSection> existingSectionList = EdxCourseSectionLocalServiceUtil.getSectionsByCourseId(courseId);
+
+ // This list will have all created and already existing section ids
+ List<String> newSectionIds = new ArrayList<String>();
+
+ // Loop over sections to synchronize
+ for (int i = 0 ; i < sections.length() ; i++) {
+
+ JSONObject section = sections.getJSONObject(i);
+ _log.info("section treated in synchronizeCourseSections : "+ section.toString());
+ String sectionId = section.getString(EdxConstants.EDX_API_BLOCKS_ID);
+ newSectionIds.add(sectionId);
+ _log.info("Synchronizing sectionId "+sectionId);
+
+ EdxCourseSection courseSection = null;
+ try {
+ courseSection = EdxCourseSectionLocalServiceUtil.getEdxCourseSection(sectionId);
+ // Update name if it has changed
+ if (!courseSection.getDisplayName().equals(section.getString(EdxConstants.EDX_API_BLOCKS_DISPLAY_NAME))) {
+ courseSection.setDisplayName(section.getString(EdxConstants.EDX_API_BLOCKS_DISPLAY_NAME));
+ EdxCourseSectionLocalServiceUtil.updateEdxCourseSection(courseSection);
+ }
+ } catch (Exception e) {
+ }
+
+ if (courseSection == null) {
+ // Create section if it does not already exist
+ try {
+ courseSection = EdxCourseSectionLocalServiceUtil.createEdxCourseSection(sectionId);
+ courseSection.setBlockId(section.getString(EdxConstants.EDX_API_BLOCKS_BLOCK_ID));
+ courseSection.setCourseId(courseId);
+ courseSection.setDisplayName(section.getString(EdxConstants.EDX_API_BLOCKS_DISPLAY_NAME));
+ courseSection.setStudentViewUrl(section.getString(EdxConstants.EDX_API_BLOCKS_STUDENT_VIEW_URL));
+ courseSection.setLmsWebUrl(section.getString(EdxConstants.EDX_API_BLOCKS_LMS_WEB_URL));
+ EdxCourseSectionLocalServiceUtil.updateEdxCourseSection(courseSection);
+ _log.info("Created new section "+section.getString(EdxConstants.EDX_API_BLOCKS_DISPLAY_NAME)+" for courseId "+courseId);
+ } catch (Exception e) {
+ _log.error("Error when creating edx course section with id "+sectionId, e);
+ }
+ }
+
+ // Synchronize sub-sections
+ synchronizeSubSections(courseId, sectionId, section.getJSONArray("subSections"));
+
+ }
+
+ // Remove obsolete sections
+ for (EdxCourseSection existingSection : existingSectionList) {
+ boolean isObsolete = true;
+ for (String newSectionId : newSectionIds) {
+ if (existingSection.getSectionId().equals(newSectionId)) {
+ isObsolete = false;
+ }
+ }
+ if (isObsolete) {
+ try {
+ EdxCourseSectionLocalServiceUtil.deleteEdxCourseSection(existingSection);
+ } catch (SystemException e) {
+ _log.error("Error when removing sectionId "+existingSection.getSectionId()+" from the courseId "+courseId);
+ }
+ _log.info("SectionId "+existingSection.getSectionId()+" is obsolete => removed");
+ }
+ }
+
+ }
+
+
+ /**
+ * Synchronize in the DB the subSections of a given course
+ * @param courseId
+ * @param sectionId
+ * @param subSections
+ */
+ private static void synchronizeSubSections(String courseId, String sectionId, JSONArray subSections) {
+
+ // Get existing course sub-sections in DB
+ List<EdxCourseSubSection> existingSubSectionList = EdxCourseSubSectionLocalServiceUtil.getSubSectionsBySectionId(sectionId);
+
+ // This list will have all created and already existing subsection ids
+ List<String> newSubSectionIds = new ArrayList<String>();
+
+ // Loop over subsections to synchronize
+ for (int i = 0 ; i < subSections.length() ; i++) {
+
+ JSONObject subSection = subSections.getJSONObject(i);
+ String subSectionId = subSection.getString(EdxConstants.EDX_API_BLOCKS_ID);
+ newSubSectionIds.add(subSectionId);
+ _log.info("Synchronizing subSectionId "+subSectionId);
+
+ // Create subsection if it does not already exist
+ EdxCourseSubSection courseSubSection = null;
+ try {
+ courseSubSection = EdxCourseSubSectionLocalServiceUtil.getEdxCourseSubSection(subSectionId);
+ // Update name if it has changed
+ if (!courseSubSection.getDisplayName().equals(subSection.getString(EdxConstants.EDX_API_BLOCKS_DISPLAY_NAME))) {
+ courseSubSection.setDisplayName(subSection.getString(EdxConstants.EDX_API_BLOCKS_DISPLAY_NAME));
+ EdxCourseSubSectionLocalServiceUtil.updateEdxCourseSubSection(courseSubSection);
+ }
+ } catch (Exception e) {
+ }
+ if (courseSubSection == null) {
+
+ try {
+ courseSubSection = EdxCourseSubSectionLocalServiceUtil.createEdxCourseSubSection(subSectionId);
+ courseSubSection.setBlockId(subSection.getString(EdxConstants.EDX_API_BLOCKS_BLOCK_ID));
+ courseSubSection.setSectionId(sectionId);
+ courseSubSection.setCourseId(courseId);
+ courseSubSection.setDisplayName(subSection.getString(EdxConstants.EDX_API_BLOCKS_DISPLAY_NAME));
+ courseSubSection.setStudentViewUrl(subSection.getString(EdxConstants.EDX_API_BLOCKS_STUDENT_VIEW_URL));
+ courseSubSection.setLmsWebUrl(subSection.getString(EdxConstants.EDX_API_BLOCKS_LMS_WEB_URL));
+ EdxCourseSubSectionLocalServiceUtil.updateEdxCourseSubSection(courseSubSection);
+ _log.info("Created new subsection "+subSection.getString(EdxConstants.EDX_API_BLOCKS_DISPLAY_NAME)+" for sectionId "+sectionId+" and courseId "+courseId);
+ } catch (Exception e) {
+ _log.error("Error when creating edx course subsection with id "+subSectionId, e);
+ }
+ }
+
+ // Synchronize units
+ synchronizeUnits(courseId, subSectionId, subSection.getJSONArray("units"));
+
+ }
+
+ // Remove obsolete subsections
+ for (EdxCourseSubSection existingSubSection : existingSubSectionList) {
+ boolean isObsolete = true;
+ for (String newSubSectionId : newSubSectionIds) {
+ if (existingSubSection.getSubSectionId().equals(newSubSectionId)) {
+ isObsolete = false;
+ }
+ }
+ if (isObsolete) {
+ try {
+ EdxCourseSubSectionLocalServiceUtil.deleteEdxCourseSubSection(existingSubSection);
+ } catch (SystemException e) {
+ _log.error("Error when removing subsectionId "+existingSubSection.getSubSectionId()+" from the sectionId "+sectionId+" and courseId "+courseId);
+ }
+ _log.info("SubsectionId " + existingSubSection.getSubSectionId() + " is obsolete => removed");
+ }
+ }
+ }
+
+
+ /**
+ * Synchronize in the DB the units of a given course
+ * @param courseId
+ * @param subSectionId
+ * @param units
+ */
+ private static void synchronizeUnits(String courseId, String subSectionId, JSONArray units) {
+
+ // Get existing course sub-sections in DB
+ List<EdxCourseUnit> existingUnitList = EdxCourseUnitLocalServiceUtil.getUnitsBySubSectionId(subSectionId);
+
+ // This list will have all created and already existing unit ids
+ List<String> newUnitIds = new ArrayList<String>();
+
+ // Loop over subsections to synchronize
+ for (int i = 0 ; i < units.length() ; i++) {
+
+ JSONObject unit = units.getJSONObject(i);
+ String unitId = unit.getString(EdxConstants.EDX_API_BLOCKS_ID);
+ newUnitIds.add(unitId);
+ _log.info("Synchronizing unitId "+unitId);
+
+ // Create unit if it does not already exist
+ EdxCourseUnit courseUnit = null;
+ try {
+ courseUnit = EdxCourseUnitLocalServiceUtil.getEdxCourseUnit(unitId);
+ // Update name if it has changed
+ if (!courseUnit.getDisplayName().equals(unit.getString(EdxConstants.EDX_API_BLOCKS_DISPLAY_NAME))) {
+ courseUnit.setDisplayName(unit.getString(EdxConstants.EDX_API_BLOCKS_DISPLAY_NAME));
+ EdxCourseUnitLocalServiceUtil.updateEdxCourseUnit(courseUnit);
+ }
+ } catch (Exception e) {
+ }
+ if (courseUnit == null) {
+
+ try {
+ courseUnit = EdxCourseUnitLocalServiceUtil.createEdxCourseUnit(unitId);
+ courseUnit.setBlockId(unit.getString(EdxConstants.EDX_API_BLOCKS_BLOCK_ID));
+ courseUnit.setSubSectionId(subSectionId);
+ courseUnit.setCourseId(courseId);
+ courseUnit.setDisplayName(unit.getString(EdxConstants.EDX_API_BLOCKS_DISPLAY_NAME));
+ courseUnit.setStudentViewUrl(unit.getString(EdxConstants.EDX_API_BLOCKS_STUDENT_VIEW_URL));
+ courseUnit.setLmsWebUrl(unit.getString(EdxConstants.EDX_API_BLOCKS_LMS_WEB_URL));
+ EdxCourseUnitLocalServiceUtil.updateEdxCourseUnit(courseUnit);
+ _log.info("Created new unit "+unit.getString(EdxConstants.EDX_API_BLOCKS_DISPLAY_NAME)+" for subSctionId "+subSectionId+" and courseId "+courseId);
+ } catch (Exception e) {
+ _log.error("Error when creating edx course unit with id "+unitId, e);
+ }
+ }
+
+ // Synchronize items
+ synchronizeItems(courseId, unitId, unit.getJSONArray("items"));
+
+ }
+
+ // Remove obsolete units
+ for (EdxCourseUnit existingUnit : existingUnitList) {
+ boolean isObsolete = true;
+ for (String newUnitId : newUnitIds) {
+ if (existingUnit.getUnitId().equals(newUnitId)) {
+ isObsolete = false;
+ }
+ }
+ if (isObsolete) {
+ try {
+ EdxCourseUnitLocalServiceUtil.deleteEdxCourseUnit(existingUnit);
+ } catch (SystemException e) {
+ _log.error("Error when removing unitId "+existingUnit.getUnitId()+" from the subSctionId "+subSectionId+" and courseId "+courseId);
+ }
+ _log.info("UnitId " + existingUnit.getSubSectionId() + " is obsolete => removed");
+ }
+ }
+
+
+ }
+
+
+ /**
+ * Synchronize in the DB the items of a given course
+ * @param courseId
+ * @param unitId
+ * @param items
+ */
+ private static void synchronizeItems(String courseId, String unitId, JSONArray items) {
+
+ // Get existing course sub-sections in DB
+ List<EdxCourseItem> existingItemList = EdxCourseItemLocalServiceUtil.getItemsByUnitId(unitId);
+
+ // This list will have all created and already existing unit ids
+ List<String> newItemIds = new ArrayList<String>();
+
+ // Loop over subsections to synchronize
+ for (int i = 0 ; i < items.length() ; i++) {
+
+ JSONObject item = items.getJSONObject(i);
+ String itemId = item.getString(EdxConstants.EDX_API_BLOCKS_ID);
+ newItemIds.add(itemId);
+ _log.info("Synchronizing itemId "+itemId);
+
+ // Create item if it does not already exist
+ EdxCourseItem courseItem = null;
+ try {
+ courseItem = EdxCourseItemLocalServiceUtil.getEdxCourseItem(itemId);
+ // Update name if it has changed
+ if (!courseItem.getDisplayName().equals(item.getString(EdxConstants.EDX_API_BLOCKS_DISPLAY_NAME))) {
+ courseItem.setDisplayName(item.getString(EdxConstants.EDX_API_BLOCKS_DISPLAY_NAME));
+ EdxCourseItemLocalServiceUtil.updateEdxCourseItem(courseItem);
+ }
+ } catch (Exception e) {
+ }
+ if (courseItem == null) {
+
+ try {
+ courseItem = EdxCourseItemLocalServiceUtil.createEdxCourseItem(itemId);
+ courseItem.setBlockId(item.getString(EdxConstants.EDX_API_BLOCKS_BLOCK_ID));
+ courseItem.setUnitId(unitId);
+ courseItem.setCourseId(courseId);
+ courseItem.setDisplayName(item.getString(EdxConstants.EDX_API_BLOCKS_DISPLAY_NAME));
+ courseItem.setStudentViewUrl(item.getString(EdxConstants.EDX_API_BLOCKS_STUDENT_VIEW_URL));
+ courseItem.setLmsWebUrl(item.getString(EdxConstants.EDX_API_BLOCKS_LMS_WEB_URL));
+ EdxCourseItemLocalServiceUtil.updateEdxCourseItem(courseItem);
+ _log.info("Created new item "+item.getString(EdxConstants.EDX_API_BLOCKS_DISPLAY_NAME)+" for unitId "+unitId+" and courseId "+courseId);
+ } catch (Exception e) {
+ _log.error("Error when creating edx course item with id "+itemId, e);
+ }
+ }
+ }
+
+ // Remove obsolete items
+ for (EdxCourseItem existingItem : existingItemList) {
+ boolean isObsolete = true;
+ for (String newItemId : newItemIds) {
+ if (existingItem.getItemId().equals(newItemId)) {
+ isObsolete = false;
+ }
+ }
+ if (isObsolete) {
+ try {
+ EdxCourseItemLocalServiceUtil.deleteEdxCourseItem(existingItem);
+ } catch (SystemException e) {
+ _log.error("Error when removing unitId "+existingItem.getItemId()+" from the unitId "+unitId+" and courseId "+courseId);
+ }
+ _log.info("ItemId " + existingItem.getItemId() + " is obsolete => removed");
+ }
+ }
+
+
+ }
+
+
+}