/**
   * Make a copy of the selected module
   *
   * @return list_auth_modules
   */
  public String duplicateAction() {
    FacesContext ctx = FacesContext.getCurrentInstance();
    ResourceLoader bundle = new ResourceLoader("org.etudes.tool.melete.bundle.Messages");
    if (!saveModuleDates()) return "list_auth_modules";

    try {
      resetSelectedLists();
      UIViewRoot root = ctx.getViewRoot();
      UIData table = (UIData) root.findComponent("listauthmodulesform").findComponent("table");
      ModuleDateBean mdbean = (ModuleDateBean) table.getRowData();
      logger.debug("calling copy for " + mdbean.getModule().getTitle());
      moduleService.copyModule((ModuleObjService) mdbean.getModule(), courseId, userId);
    } catch (MeleteException me) {
      logger.debug(me.toString());
      String msg = bundle.getString("copy_fail");
      addMessage(ctx, "Error Message", msg, FacesMessage.SEVERITY_ERROR);
    }
    resetValues();
    return "list_auth_modules";
  }
  /**
   * Bring section one level up in indentation
   *
   * @return list_auth_modules
   */
  public String BringSubSectionLevelUpAction() {
    FacesContext ctx = FacesContext.getCurrentInstance();
    ResourceLoader bundle = new ResourceLoader("org.etudes.tool.melete.bundle.Messages");
    if (!saveModuleDates()) return "list_auth_modules";
    if ((moduleDateBeans == null) || (moduleDateBeans.size() == 0)) return "list_auth_modules";

    if (count == 0) {
      String msg = bundle.getString("list_select_one_indent");
      addMessage(ctx, "Error Message", msg, FacesMessage.SEVERITY_ERROR);
      resetSubSectionValues();
      return "list_auth_modules";
    }

    // if module is selected then throw message
    if (moduleSelected) {
      String msg = bundle.getString("list_select_indent");
      addMessage(ctx, "Error Message", msg, FacesMessage.SEVERITY_ERROR);
      resetSubSectionValues();
      return "list_auth_modules";
    }

    // section selected
    /*
     * if(sectionSelected && count != 1) { String msg =
     * bundle.getString("list_select_one_indent"); addMessage(ctx,"Error
     * Message
     * ",msg,FacesMessage.SEVERITY_ERROR); resetSubSectionValues(); return "
     * list_auth_modules"; }
     */

    if (sectionSelected == true) {
      if ((secObjMap == null) || (secObjMap.size() == 0)) return "list_auth_modules";
      SecModObj smObj = null;
      ModuleDateBean mdbean = null;
      SectionBeanService secBean = null;
      List indentSecBeans = null;
      if (indentSecBeans == null) {
        indentSecBeans = new ArrayList();
      }

      if (selectedSecIds == null) {
        resetSubSectionValues();
        return "list_auth_modules";
      }
      // If one section is selected, we check if its the top level section
      // If multiple sections are selected, we indent those that we can
      // and leave the others alone
      if (selectedSecIds.size() == 1) {
        smObj = (SecModObj) secObjMap.get(selectedSecIds.get(0));
        if ((smObj != null) && (smObj.getMdBean() != null) && (smObj.getSecBean() != null)) {
          mdbean = (ModuleDateBean) smObj.getMdBean();
          secBean = (SectionBeanService) smObj.getSecBean();
          if (checkTopLevelSection(secBean.getDisplaySequence())) {
            logger.debug("Top level section can't indent left more");
            resetSubSectionValues();
            return "list_auth_modules";
          }
          try {
            indentSecBeans.add(secBean);
            moduleService.bringOneLevelUp(mdbean.getModule(), indentSecBeans);
          } catch (MeleteException me) {
            logger.debug(me.toString());
            String msg = bundle.getString("indent_left_fail");
            addMessage(ctx, "Error Message", msg, FacesMessage.SEVERITY_ERROR);
          }
        }
      } else {
        // Multiple indent code
        boolean res = checkDifModules(selectedSecIds);
        if (res == true) {
          String msg = bundle.getString("list_select_in_onemodule");
          addMessage(ctx, "Select Indent", msg, FacesMessage.SEVERITY_ERROR);
          resetSubSectionValues();
          return "list_auth_modules";
        } else {
          mdbean = null;
          secBean = null;

          for (ListIterator i = selectedSecIds.listIterator(); i.hasNext(); ) {
            Integer sectionId = (Integer) i.next();
            smObj = (SecModObj) secObjMap.get(sectionId);
            if ((smObj != null) && (smObj.getMdBean() != null) && (smObj.getSecBean() != null)) {
              mdbean = smObj.getMdBean();
              secBean = smObj.getSecBean();
              indentSecBeans.add(secBean);
            }
          }
          if ((indentSecBeans != null) && (indentSecBeans.size() != 0)) {
            try {
              moduleService.bringOneLevelUp(mdbean.getModule(), indentSecBeans);
            } catch (MeleteException me) {
              logger.debug(me.toString());
              String msg = bundle.getString("indent_left_fail");
              addMessage(ctx, "Error Message", msg, FacesMessage.SEVERITY_ERROR);
            }
          }
        }
      }
    }

    resetSubSectionValues();
    int saveShowId = showModuleId;
    resetValues();
    setShowModuleId(saveShowId);
    return "list_auth_modules";
  }
  /**
   * Indent this section by first checking if indentation is less than 10 levels
   *
   * @return list_auth_modules
   */
  public String CreateSubSectionAction() {
    FacesContext ctx = FacesContext.getCurrentInstance();
    ResourceLoader bundle = new ResourceLoader("org.etudes.tool.melete.bundle.Messages");
    if (!saveModuleDates()) return "list_auth_modules";
    if ((moduleDateBeans == null) || (moduleDateBeans.size() == 0)) return "list_auth_modules";

    // if module is selected then throw message
    if (moduleSelected) {
      String msg = bundle.getString("list_select_indent");
      addMessage(ctx, "Select  Indent", msg, FacesMessage.SEVERITY_ERROR);
      resetSubSectionValues();
      return "list_auth_modules";
    }

    if ((moduleSelected == false) && (sectionSelected == false)) {
      String msg = bundle.getString("list_select_one_indent");
      addMessage(ctx, "Select Indent", msg, FacesMessage.SEVERITY_ERROR);
      resetSubSectionValues();
      return "list_auth_modules";
    }
    if (sectionSelected == true) {
      if ((secObjMap == null) || (secObjMap.size() == 0)) {
        resetSubSectionValues();
        return "list_auth_modules";
      }
      SecModObj smObj = null;
      ModuleDateBean mdbean = null;
      SectionBeanService secBean = null;
      List indentSecBeans = null;
      if (indentSecBeans == null) {
        indentSecBeans = new ArrayList();
      }

      if (selectedSecIds == null) {
        resetSubSectionValues();
        return "list_auth_modules";
      }
      // If one section is selected, we check if its the first section
      // or if it is too deep to indent
      // If multiple sections are selected, we indent those that we can
      // and leave the others alone
      if (selectedSecIds.size() == 1) {
        smObj = (SecModObj) secObjMap.get(selectedSecIds.get(0));
        if ((smObj != null) && (smObj.getMdBean() != null) && (smObj.getSecBean() != null)) {
          mdbean = smObj.getMdBean();
          if (mdbean.getSectionBeans().size() < 2) {
            logger.debug("First section can't be change to subsection");
            resetSubSectionValues();
            return "list_auth_modules";
          } else {
            secBean = smObj.getSecBean();

            boolean indentOk = findIndent(secBean, mdbean.getSectionBeans());
            // Only allow indent upto 10 levels
            if (indentOk) {
              try {
                indentSecBeans.add(secBean);
                moduleService.createSubSection(mdbean.getModule(), indentSecBeans);
              } catch (MeleteException me) {
                logger.debug(me.toString());
                String msg = bundle.getString("indent_right_fail");
                addMessage(ctx, "Error Message", msg, FacesMessage.SEVERITY_ERROR);
              }
            } else {
              String msg = bundle.getString("indent_right_toodeep");
              addMessage(ctx, "Error Message", msg, FacesMessage.SEVERITY_ERROR);
            }
          }
        } else {
          resetSubSectionValues();
          return "list_auth_modules";
        }
      } else {
        // Multiple indent code
        boolean res = checkDifModules(selectedSecIds);
        if (res == true) {
          String msg = bundle.getString("list_select_in_onemodule");
          addMessage(ctx, "Select Indent", msg, FacesMessage.SEVERITY_ERROR);
          resetSubSectionValues();
          return "list_auth_modules";
        } else {
          mdbean = null;
          secBean = null;

          for (ListIterator i = selectedSecIds.listIterator(); i.hasNext(); ) {
            Integer sectionId = (Integer) i.next();
            smObj = (SecModObj) secObjMap.get(sectionId);
            if ((smObj != null) && (smObj.getMdBean() != null) && (smObj.getSecBean() != null)) {
              mdbean = smObj.getMdBean();
              secBean = smObj.getSecBean();
              boolean indentOk = findIndent(secBean, mdbean.getSectionBeans());
              if (indentOk) {
                indentSecBeans.add(secBean);
              }
            }
          }
          try {
            moduleService.createSubSection(mdbean.getModule(), indentSecBeans);
          } catch (MeleteException me) {
            logger.debug(me.toString());
            String msg = bundle.getString("indent_right_fail");
            addMessage(ctx, "Error Message", msg, FacesMessage.SEVERITY_ERROR);
          }
        }
      }
    }
    resetSubSectionValues();
    int saveShowId = showModuleId;
    resetValues();
    setShowModuleId(saveShowId);
    return "list_auth_modules";
  }