DHXScheduler保存/获取自定义灯箱字段

时间:2014-03-24 13:19:02

标签: java javascript json dhtmlx dhtmlx-scheduler

我在我的JSP应用程序中使用DHXScheduler进行考试时间表。我添加了一个完全自定义的灯箱,我自己的字段包含有关考试的信息。

这是我的EventsManager类,它保存,创建和获取事件。它扩展了类DHXEventsManager。

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<Event> events = new ArrayList<Event>();

    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();
}
}

Event类扩展DHXEvent并包含其他字段的getter和setter。问题是,当我从DHXEv转换为Event时,我将其他字段丢失为null,但saveEvent方法仅接受DHXEv作为参数。

这是events.jsp文件,其中调用EventsManager中的getEvents()方法以获取包含事件的JSON对象。

<%@ page contentType="application/json" 
         import="com.dhtmlx.planner.*,servlets.*,events.EventsManager"
%>

<%= getEvents(request) %>
<%!
    String getEvents(HttpServletRequest request) throws Exception {
        EventsManager evs = new EventsManager(request);
        return evs.run();
    }
%>

这是它返回的样本。如您所见,它只返回DHXEv对象中的字段,即id, start_date, end_date and text

{id:7, text:CIS3087, end_date:03/27/2014 13:00, start_date:03/27/2014 10:00}

如何将这些附加字段添加到JSON对象,即具有studyunit title,department等对象?

这是调度程序的一些相关客户端代码..它的配置以及save_form()和show_lightbox方法:

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");              
};

我猜它只是为了获取JSON对象中的字段并在灯箱中显示它们而进行的一个小修改。

你知道我怎么能解决这个问题吗?谢谢!

1 个答案:

答案 0 :(得分:-1)

请确保Event类中的其他字段是公开的。 DHXEventsManager使用Class.getFields()来获取事件属性。如果属性是私有的,则它在生成的JSON中不可用。