title: DHXScheduler Save/Get Custom Lightbox Fields
body: <p>I am using DHXScheduler in my JSP application for exam timetabling. I added a fully custom lightbox with my own fields with information about exams. </p>
  
  <p>This is my EventsManager class which saves, creates and gets events. It extends the class DHXEventsManager. </p>
  
  <pre><code>public class EventsManager extends DHXEventsManager {
  
  public EventsManager(HttpServletRequest request) {          
      super(request);
  }
  
  public Iterable getEvents() {
  
      Connection conn = null;
      DHXEventsManager.date_format = "yyyy-MM-dd HH:mm:ss";
      List&lt;Event&gt; events = new ArrayList&lt;Event&gt;();
  
      try {
  
          conn = SQLHelper.getConnection();
          Statement statement = conn.createStatement();
  
          StringBuffer query = new StringBuffer();
          query.append("SELECT * FROM ");
          query.append(TimetableEvent.TBL_EVENTS);
          query.append(" e JOIN ");
          query.append(StudyUnit.TBL_STUDYUNITS);
          query.append(" s ON e.");
          query.append(TimetableEvent.FLD_UNITCODE);
          query.append(" = s.");
          query.append(StudyUnit.FLD_UNITCODE);
  
          ResultSet rs = statement.executeQuery(query.toString());
  
          while (rs.next()) {
  
              Event e = new Event(); // extends DHXEvent and contains additional fields
  
              e.setId(Integer.parseInt(rs.getString(TimetableEvent.FLD_ID)));
              e.setText(rs.getString(TimetableEvent.FLD_UNITCODE));
              e.setStart_date(rs.getString(TimetableEvent.FLD_STARTDATE));
              e.setEnd_date(rs.getString(TimetableEvent.FLD_ENDDATE));
              e.setUnitCode(rs.getString(StudyUnit.FLD_UNITCODE));
              e.setTitle(rs.getString(StudyUnit.FLD_TITLE));
              e.setYear(rs.getString(StudyUnit.FLD_YEAR));
              e.setSemester(rs.getShort(StudyUnit.FLD_SEMESTER));
              e.setExamLength(rs.getFloat(StudyUnit.FLD_EXAMLENGTH));
              e.setNoOfStudents(rs.getShort(StudyUnit.FLD_NOOFSTUDENTS));
              e.setDepartment(rs.getString(StudyUnit.FLD_DEPARTMENT));
              e.setCredits(rs.getShort(StudyUnit.FLD_CREDITS));
              e.setEvening(rs.getBoolean(StudyUnit.FLD_EVENING));
  
              events.add(e);
              }
  
          } catch (SQLException e) {
              System.out.println("[EventsManager.getEvents()]: " + e.getMessage());
  
          } finally {
              SQLHelper.closeConnection(conn);
          }
  
          DHXEventsManager.date_format = "MM/dd/yyyy HH:mm";
          return events;
      }
  
      @Override
      public DHXStatus saveEvent(DHXEv event, DHXStatus status) {
      Connection conn = SQLHelper.getConnection();
      PreparedStatement pstmt = null;
      ResultSet rs = null;
  
      Event ev = (Event) event;
  
      try {
  
          if (status == DHXStatus.UPDATE) {
              pstmt = TimetableEvent.updateEvent(conn, ev);
  
          } else if (status == DHXStatus.INSERT) {
              System.out.println("SAVE");
              pstmt = TimetableEvent.insertEvent(conn, ev);
  
          } else if (status == DHXStatus.DELETE) {
              pstmt = TimetableEvent.deleteEvent(conn, event);
          }
  
          if (pstmt != null) {
              pstmt.executeUpdate();
              rs = pstmt.getGeneratedKeys();
  
              if (rs.next()) {
                  event.setId(rs.getInt(1));
                  ev.setId(rs.getInt(1));
              }
          }
  
      } catch (SQLException e) {
          System.out.println("[EventsManager.saveEvent() - " + status.name() + "]: " + e.getMessage());
          e.printStackTrace();
  
      } finally {
  
          if (rs != null) SQLHelper.closeResultSet(rs);
          if (pstmt != null) SQLHelper.closePreparedStatement(pstmt);
          if (conn != null) SQLHelper.closeConnection(conn);
      }
  
      return status;
  }
  
      @Override
  public DHXEv createEvent(String id, DHXStatus status) {
      return new Event();
  }
  }
  </code></pre>
  
  <p>The <code>Event</code> class extends <code>DHXEvent</code> and contains getters and setters for the additional fields. The problem is that when I am casting from <code>DHXEv</code> to <code>Event</code>, I am losing the additional fields to null but the saveEvent method only accepts DHXEv as a parameter.</p>
  
  <p>This is <code>events.jsp</code> file where the getEvents() method from EventsManager is called to get JSON objects with events.</p>
  
  <pre><code>&lt;%@ page contentType="application/json" 
           import="com.dhtmlx.planner.*,servlets.*,events.EventsManager"
  %&gt;
  
  &lt;%= getEvents(request) %&gt;
  &lt;%!
      String getEvents(HttpServletRequest request) throws Exception {
          EventsManager evs = new EventsManager(request);
          return evs.run();
      }
  %&gt;
  </code></pre>
  
  <p>This is a sample of what its returning. As you can see it only returns fields from <code>DHXEv</code> object i.e. <code>id, start_date, end_date and text</code>.</p>
  
  <pre><code>{id:7, text:CIS3087, end_date:03/27/2014 13:00, start_date:03/27/2014 10:00}
  </code></pre>
  
  <p>How can I add those additional fields to the JSON object i.e. have an object with studyunit title, department, etc?</p>
  
  <p>This is the some relevant client-side code for the scheduler.. its configuration and the save_form() and show_lightbox methods:</p>
  
  <pre><code>scheduler.config.api_date = "%Y-%m-%d %H:%i";
  scheduler.config.details_on_dblclick = true;
  scheduler.config.details_on_create = true;
  scheduler.config.first_hour = "8";  
  scheduler.config.last_hour = "22";
  scheduler.config.drag_lightbox = true;
  scheduler.config.show_loading = true;
  scheduler.config.mark_now = true;
  
  scheduler.init('scheduler_here', new Date(), "week");
  scheduler.load("events.jsp", "json");
  
  var dp = new dataProcessor("events.jsp");
  dp.init(scheduler);
  
  function save_form() {
        var ev = scheduler.getEvent(scheduler.getState().lightbox_id);
  
        ev.text = html("studyunit_code").value;
        ev.start_date = setDate("start_date", "starttime");
        ev.end_date = setDate("start_date", "endtime");
  
        scheduler.endLightbox(true, html("custom_form"));
  
        $("#form1").submit(); // submits all the other fields and saves them to a db table
  }
  
  scheduler.showLightbox = function(id) {
  
      var ev = scheduler.getEvent(id);
      scheduler.startLightbox(id, html("custom_form"));
  
      html("studyunit_code").value = ev.text;
      html("studyunit_code").focus();
      html("studyunit_title").value = ev.studyunit_title;
  
      var startDate = ev.start_date;
      getDate(startDate, "start_date", "starttime");
  
      var endDate = ev.end_date;
      getDate(endDate, "start_date", "endtime");              
  };
  </code></pre>
  
  <p>I'm guessing its just a small modification that I have to make to get the fields in the JSON object and show them in the lightbox.</p>
  
  <p>Do you have any idea of how I can solve this? Thanks!</p>