public void insertParent(Session session, int menuId, int usertypes) {
    Transaction transaction = null;
    try {
      Criteria menuCrit = session.createCriteria(Menumaster.class);
      menuCrit.add(Restrictions.sqlRestriction("code=" + menuId));
      List menuList = menuCrit.list();
      if (menuList.size() > 0) {
        Menumaster menuObj = (Menumaster) menuList.get(0);

        Criteria menuCrit1 = session.createCriteria(Menuprivilages.class);
        menuCrit1.add(Restrictions.sqlRestriction("usertype=" + usertypes));
        menuCrit1.add(Restrictions.sqlRestriction("menumaster=" + menuObj.getParentcode()));
        List menuList1 = menuCrit1.list();
        if (menuList1.size() <= 0) {
          transaction = session.beginTransaction();

          Usertype typeobj = new Usertype();
          typeobj.setId(usertypes);

          Menumaster menuMasterObj = new Menumaster();
          menuMasterObj.setCode(menuObj.getParentcode());

          Menuprivilages menuObj2 = new Menuprivilages();
          menuObj2.setId(String.valueOf(getSequenceNumber(session)));
          menuObj2.setUsertype(typeobj);
          menuObj2.setMenumaster(menuMasterObj);
          menuObj2.setAccessright(Boolean.TRUE);
          session.save(menuObj2);
          transaction.commit();

          this.insertParent(session, menuObj.getParentcode(), usertypes);
        }
      }
    } catch (Exception e) {
      if (transaction != null) {
        transaction.rollback();
      }
      e.printStackTrace();
    }
  }
  public void updateParent(Session session, int menuId, int usertypes) {
    Transaction transaction = null;
    try {
      Criteria menuCrit3 = session.createCriteria(Menuprivilages.class);
      menuCrit3.add(Restrictions.sqlRestriction("usertype=" + usertypes));
      menuCrit3.add(Restrictions.sqlRestriction("menumaster=" + menuId));
      List menuList3 = menuCrit3.list();
      if (menuList3.size() > 0) {
        transaction = session.beginTransaction();
        Menuprivilages menuObj3 = (Menuprivilages) menuList3.get(0);
        menuObj3.setAccessright(Boolean.TRUE);
        session.update(menuObj3);
        transaction.commit();

        this.updateParent(session, menuObj3.getMenumaster().getParentcode(), usertypes);
      }
    } catch (Exception e) {
      if (transaction != null) {
        transaction.rollback();
      }
      e.printStackTrace();
    }
  }
  @GlobalDBOpenCloseAndUserPrivilages
  public Map saveMenuAssign(
      Session session,
      HttpServletRequest request,
      HttpServletResponse response,
      String LoggedInRegion,
      String LoggedInUser,
      String usertypes,
      String menucodes) {
    Map resultMap = new HashMap();
    String[] strArray = null;
    String delimiter = "-";
    Transaction transaction = null;
    try {

      Criteria menuCrit = session.createCriteria(Menuprivilages.class);
      menuCrit.add(Restrictions.sqlRestriction("usertype=" + Integer.parseInt(usertypes)));
      List menuList = menuCrit.list();
      if (menuList.size() > 0) {
        for (int i = 0; i < menuList.size(); i++) {
          Menuprivilages menuObj = (Menuprivilages) menuList.get(i);
          transaction = session.beginTransaction();
          menuObj.setAccessright(Boolean.FALSE);
          session.update(menuObj);
          transaction.commit();
        }

        if (!menucodes.equalsIgnoreCase("") && menucodes != null) {
          strArray = menucodes.split(java.util.regex.Pattern.quote(delimiter));
          for (String menuId : strArray) {
            Criteria menuCrit1 = session.createCriteria(Menuprivilages.class);
            menuCrit1.add(Restrictions.sqlRestriction("usertype=" + Integer.parseInt(usertypes)));
            menuCrit1.add(Restrictions.sqlRestriction("menumaster=" + Integer.parseInt(menuId)));
            List menuList1 = menuCrit1.list();
            if (menuList1.size() > 0) {
              transaction = session.beginTransaction();
              Menuprivilages menuObj = (Menuprivilages) menuList1.get(0);
              menuObj.setAccessright(Boolean.TRUE);
              session.update(menuObj);
              transaction.commit();

              this.updateParent(
                  session, menuObj.getMenumaster().getParentcode(), menuObj.getUsertype().getId());

            } else {
              transaction = session.beginTransaction();

              Usertype typeobj = new Usertype();
              typeobj.setId(Integer.parseInt(usertypes));

              Menumaster menuMasterObj = new Menumaster();
              menuMasterObj.setCode(Integer.parseInt(menuId));

              Menuprivilages menuObj = new Menuprivilages();
              menuObj.setId(String.valueOf(getSequenceNumber(session)));
              menuObj.setUsertype(typeobj);
              menuObj.setMenumaster(menuMasterObj);
              menuObj.setAccessright(Boolean.TRUE);
              session.saveOrUpdate(menuObj);
              transaction.commit();

              this.insertParent(session, Integer.parseInt(menuId), Integer.parseInt(usertypes));
            }
          }
        }

      } else {
        Criteria criteria = session.createCriteria(Menumaster.class);
        //                criteria.add(Restrictions.ne("parentcode", 0));
        criteria.addOrder(Order.asc("menuorder"));
        List menumasterlist = criteria.list();
        if (menumasterlist.size() > 0) {
          for (int i = 0; i < menumasterlist.size(); i++) {
            Menumaster menuMasterObj = (Menumaster) menumasterlist.get(i);

            transaction = session.beginTransaction();

            Usertype typeobj = new Usertype();
            typeobj.setId(Integer.parseInt(usertypes));

            Menuprivilages menuObj = new Menuprivilages();
            menuObj.setId(String.valueOf(getSequenceNumber(session)));
            menuObj.setUsertype(typeobj);
            menuObj.setMenumaster(menuMasterObj);
            menuObj.setAccessright(Boolean.FALSE);
            session.save(menuObj);
            transaction.commit();
          }
        }

        if (!menucodes.equalsIgnoreCase("") && menucodes != null) {
          strArray = menucodes.split(java.util.regex.Pattern.quote(delimiter));
          for (String menuId : strArray) {
            Criteria menuCrit1 = session.createCriteria(Menuprivilages.class);
            menuCrit1.add(Restrictions.sqlRestriction("usertype=" + Integer.parseInt(usertypes)));
            menuCrit1.add(Restrictions.sqlRestriction("menumaster=" + Integer.parseInt(menuId)));
            List menuList1 = menuCrit1.list();
            if (menuList1.size() > 0) {
              transaction = session.beginTransaction();
              Menuprivilages menuObj = (Menuprivilages) menuList1.get(0);
              menuObj.setAccessright(Boolean.TRUE);
              session.update(menuObj);
              transaction.commit();

              this.updateParent(
                  session, menuObj.getMenumaster().getParentcode(), menuObj.getUsertype().getId());

            } else {
              transaction = session.beginTransaction();

              Usertype typeobj = new Usertype();
              typeobj.setId(Integer.parseInt(usertypes));

              Menumaster menuMasterObj = new Menumaster();
              menuMasterObj.setCode(Integer.parseInt(menuId));

              Menuprivilages menuObj = new Menuprivilages();
              menuObj.setId(String.valueOf(getSequenceNumber(session)));
              menuObj.setUsertype(typeobj);
              menuObj.setMenumaster(menuMasterObj);
              menuObj.setAccessright(Boolean.TRUE);
              session.save(menuObj);
              transaction.commit();
              this.insertParent(session, Integer.parseInt(menuId), Integer.parseInt(usertypes));
            }
          }
        }
      }

      resultMap.put("success", "Successfully Menus Assigned");
    } catch (Exception e) {
      e.printStackTrace();
      if (transaction != null) {
        transaction.rollback();
      }
      resultMap.put("ERROR", "Menus Assigning Faild");
    }
    return resultMap;
  }