@Override
 @PreAuthorize("checkPermission('StudentSchedulingStatusTypeEdit')")
 public void save(SimpleEditInterface data, SessionContext context, Session hibSession) {
   for (StudentSectioningStatus status : StudentSectioningStatusDAO.getInstance().findAll()) {
     Record r = data.getRecord(status.getUniqueId());
     if (r == null) delete(status, context, hibSession);
     else update(status, r, context, hibSession);
   }
   for (Record r : data.getNewRecords()) save(r, context, hibSession);
 }
 @Override
 @PreAuthorize("checkPermission('StudentSchedulingStatusTypeEdit')")
 public void save(Record record, SessionContext context, Session hibSession) {
   StudentSectioningStatus status = new StudentSectioningStatus();
   int value = 0;
   for (int i = 0; i < StatusOption.values().length; i++)
     if ("true".equals(record.getField(2 + i)))
       value += StatusOption.values()[i].getOption().toggle();
   status.setTypes(new HashSet<CourseType>());
   List<CourseType> courseTypes = CourseTypeDAO.getInstance().findAll(Order.asc("reference"));
   if (!courseTypes.isEmpty()) {
     for (int i = 0; i < courseTypes.size(); i++)
       if ("true".equals(record.getField(3 + StatusOption.values().length + i)))
         status.getTypes().add(courseTypes.get(i));
     if (!"true".equals(record.getField(3 + StatusOption.values().length + courseTypes.size())))
       value += StudentSectioningStatus.Option.notype.toggle();
   }
   status.setReference(record.getField(0));
   status.setLabel(record.getField(1));
   status.setStatus(value);
   status.setMessage(record.getField(2 + StatusOption.values().length));
   record.setUniqueId((Long) hibSession.save(status));
   ChangeLog.addChange(
       hibSession,
       context,
       status,
       status.getReference() + " " + status.getLabel(),
       Source.SIMPLE_EDIT,
       Operation.CREATE,
       null,
       null);
 }
  @Override
  public Boolean execute(OnlineSectioningServer server, OnlineSectioningHelper helper) {
    StudentSectioningStatus status =
        (hasStatus()
            ? (StudentSectioningStatus)
                helper
                    .getHibSession()
                    .createQuery("from StudentSectioningStatus where reference = :ref")
                    .setString("ref", getStatus())
                    .uniqueResult()
            : null);
    for (Long studentId : getStudentIds()) {
      Lock lock = server.lockStudent(studentId, null, name());
      try {
        XStudent student = server.getStudent(studentId);
        helper.beginTransaction();
        try {
          Student dbStudent = StudentDAO.getInstance().get(studentId, helper.getHibSession());
          if (student != null && dbStudent != null) {

            OnlineSectioningLog.Action.Builder action =
                helper.addAction(this, server.getAcademicSession());
            action.setStudent(
                OnlineSectioningLog.Entity.newBuilder()
                    .setUniqueId(student.getStudentId())
                    .setExternalId(student.getExternalId())
                    .setName(student.getName()));
            if (status != null) {
              action.addOther(
                  OnlineSectioningLog.Entity.newBuilder()
                      .setUniqueId(status.getUniqueId())
                      .setName(status.getLabel())
                      .setExternalId(status.getReference())
                      .setType(OnlineSectioningLog.Entity.EntityType.OTHER));
            }

            student.setStatus(getStatus());
            dbStudent.setSectioningStatus(status);
            helper.getHibSession().saveOrUpdate(dbStudent);
            server.update(student, false);
          }
          helper.commitTransaction();
        } catch (Exception e) {
          helper.rollbackTransaction();
          if (e instanceof SectioningException) throw (SectioningException) e;
          throw new SectioningException(MSG.exceptionUnknown(e.getMessage()), e);
        }

      } finally {
        lock.release();
      }
    }
    return true;
  }
 protected void delete(
     StudentSectioningStatus status, SessionContext context, Session hibSession) {
   if (status == null) return;
   ChangeLog.addChange(
       hibSession,
       context,
       status,
       status.getReference() + " " + status.getLabel(),
       Source.SIMPLE_EDIT,
       Operation.DELETE,
       null,
       null);
   hibSession.delete(status);
 }
 @Override
 @PreAuthorize("checkPermission('StudentSchedulingStatusTypes')")
 public SimpleEditInterface load(SessionContext context, Session hibSession) {
   List<CourseType> courseTypes = CourseTypeDAO.getInstance().findAll(Order.asc("reference"));
   SimpleEditInterface.Field[] fields =
       new SimpleEditInterface.Field
           [courseTypes.isEmpty()
               ? 3 + StatusOption.values().length
               : 4 + StatusOption.values().length + courseTypes.size()];
   int idx = 0;
   fields[idx++] = new Field(MESSAGES.fieldAbbreviation(), FieldType.text, 160, 20, Flag.UNIQUE);
   fields[idx++] = new Field(MESSAGES.fieldName(), FieldType.text, 300, 60, Flag.UNIQUE);
   for (StatusOption t : StatusOption.values())
     fields[idx++] = new Field(t.getLabel(), FieldType.toggle, 40);
   fields[idx++] = new Field(MESSAGES.fieldMessage(), FieldType.text, 400, 200);
   if (!courseTypes.isEmpty()) {
     for (int i = 0; i < courseTypes.size(); i++)
       fields[idx++] = new Field(courseTypes.get(i).getReference(), FieldType.toggle, 40);
     fields[idx++] = new Field(MESSAGES.toggleNoCourseType(), FieldType.toggle, 40);
   }
   SimpleEditInterface data = new SimpleEditInterface(fields);
   data.setSortBy(0, 1);
   for (StudentSectioningStatus status : StudentSectioningStatusDAO.getInstance().findAll()) {
     Record r = data.addRecord(status.getUniqueId());
     idx = 0;
     r.setField(idx++, status.getReference());
     r.setField(idx++, status.getLabel());
     for (StatusOption t : StatusOption.values())
       r.setField(idx++, status.hasOption(t.getOption()) ? "true" : "false");
     r.setField(idx++, status.getMessage());
     if (!courseTypes.isEmpty()) {
       for (int i = 0; i < courseTypes.size(); i++)
         r.setField(idx++, status.getTypes().contains(courseTypes.get(i)) ? "true" : "false");
       r.setField(
           idx++, status.hasOption(StudentSectioningStatus.Option.notype) ? "false" : "true");
     }
   }
   data.setEditable(context.hasPermission(Right.StudentSchedulingStatusTypeEdit));
   return data;
 }
 protected void update(
     StudentSectioningStatus status, Record record, SessionContext context, Session hibSession) {
   if (status == null) return;
   int value = 0;
   for (int i = 0; i < StatusOption.values().length; i++)
     if ("true".equals(record.getField(2 + i)))
       value += StatusOption.values()[i].getOption().toggle();
   Set<CourseType> types = new HashSet<CourseType>();
   List<CourseType> courseTypes = CourseTypeDAO.getInstance().findAll(Order.asc("reference"));
   if (!courseTypes.isEmpty()) {
     for (int i = 0; i < courseTypes.size(); i++)
       if ("true".equals(record.getField(3 + StatusOption.values().length + i)))
         types.add(courseTypes.get(i));
     if (!"true".equals(record.getField(3 + StatusOption.values().length + courseTypes.size())))
       value += StudentSectioningStatus.Option.notype.toggle();
   }
   boolean changed =
       !ToolBox.equals(status.getReference(), record.getField(0))
           || !ToolBox.equals(status.getLabel(), record.getField(1))
           || !ToolBox.equals(status.getStatus(), value)
           || !ToolBox.equals(status.getTypes(), types)
           || !ToolBox.equals(
               status.getMessage(), record.getField(2 + StatusOption.values().length));
   status.setReference(record.getField(0));
   status.setLabel(record.getField(1));
   status.setStatus(value);
   status.setTypes(types);
   status.setMessage(record.getField(2 + StatusOption.values().length));
   hibSession.saveOrUpdate(status);
   if (changed)
     ChangeLog.addChange(
         hibSession,
         context,
         status,
         status.getReference() + " " + status.getLabel(),
         Source.SIMPLE_EDIT,
         Operation.UPDATE,
         null,
         null);
 }