@Override public int createEvent(final Event event) { if (event == null) { throw new IllegalArgumentException("event cannot be null"); } if (event.getId() != null) { throw new IllegalArgumentException("event.getId() must be null when creating a new Message"); } final CalendarUser owner = event.getOwner(); if (owner == null) { throw new IllegalArgumentException("event.getOwner() cannot be null"); } final CalendarUser attendee = event.getAttendee(); if (attendee == null) { throw new IllegalArgumentException("attendee.getOwner() cannot be null"); } final Calendar when = event.getWhen(); if (when == null) { throw new IllegalArgumentException("event.getWhen() cannot be null"); } KeyHolder keyHolder = new GeneratedKeyHolder(); this.jdbcOperations.update( new PreparedStatementCreator() { public PreparedStatement createPreparedStatement(Connection connection) throws SQLException { PreparedStatement ps = connection.prepareStatement( "insert into events (when,summary,description,owner,attendee) values (?, ?, ?, ?, ?)", new String[] {"id"}); ps.setDate(1, new java.sql.Date(when.getTimeInMillis())); ps.setString(2, event.getSummary()); ps.setString(3, event.getDescription()); ps.setInt(4, owner.getId()); ps.setObject(5, attendee == null ? null : attendee.getId()); return ps; } }, keyHolder); return keyHolder.getKey().intValue(); }
public Event mapRow(ResultSet rs, int rowNum) throws SQLException { CalendarUser attendee = ATTENDEE_ROW_MAPPER.mapRow(rs, rowNum); CalendarUser owner = OWNER_ROW_MAPPER.mapRow(rs, rowNum); Event event = new Event(); event.setId(rs.getInt("events.id")); event.setSummary(rs.getString("events.summary")); event.setDescription(rs.getString("events.description")); Calendar when = Calendar.getInstance(); when.setTime(rs.getDate("events.when")); event.setWhen(when); event.setAttendee(attendee); event.setOwner(owner); return event; }