public static Map encode(Note n) { if (n.getChapter() == null) { throw new IllegalArgumentException("No chapter for note: " + n); } if ((n.getDescription() == null) || (n.getDescription().trim().length() == 0)) { throw new IllegalArgumentException("No text for note: " + n); } Map cdata = new HashMap(); cdata.put(MessageFieldNames.commentid, n.getId()); cdata.put(MessageFieldNames.chapterid, n.getChapter().getId()); cdata.put(MessageFieldNames.chapterversion, n.getChapter().getVersion()); cdata.put(MessageFieldNames.chaptername, n.getChapter().getName()); cdata.put(MessageFieldNames.text, n.getDescription()); cdata.put(MessageFieldNames.start, n.getStartPosition()); if (n.getDealtWith() != null) { cdata.put(MessageFieldNames.date, n.getDealtWith().getTime()); } if ((n.getEndPosition() > 0) && (n.getEndPosition() > n.getStartPosition())) { cdata.put(MessageFieldNames.end, n.getEndPosition()); } return cdata; }
// Not the right place for this, create a better structure later. public static Map encode(Chapter c) { Map cdata = new HashMap(); cdata.put(MessageFieldNames.chapterid, c.getId()); cdata.put(MessageFieldNames.name, c.getName()); cdata.put(MessageFieldNames.text, c.getText()); cdata.put(MessageFieldNames.version, c.getVersion()); if (c.getMarkup() != null) { cdata.put(MessageFieldNames.markup, c.getMarkup()); } return cdata; }
public static Object checkTypeAndNotNull(String field, Map data, Class expect) throws GeneralException { Object o = data.get(field); if (o == null) { throw new GeneralException("Expected a value for: " + field); } if (expect != null) { if (!expect.isAssignableFrom(o.getClass())) { throw new GeneralException( "Expected type for: " + field + " to be: " + expect.getClass().getName() + ", is: " + o.getClass().getName()); } } return o; }
public static boolean getBoolean(String field, Map data, boolean required) throws GeneralException { Object o = null; if (required) { o = TypeEncoder.checkTypeAndNotNull(field, data, Boolean.class); } else { o = data.get(field); if (o == null) { return false; } if (!Boolean.class.isAssignableFrom(o.getClass())) { throw new GeneralException( "Expected type for: " + field + " to be: " + Number.class.getName() + ", is: " + o.getClass().getName()); } } return (Boolean) o; }
public static Date getDate(String field, Map data, boolean required) throws GeneralException { Object o = null; if (required) { o = TypeEncoder.checkTypeAndNotNull(field, data, Number.class); } else { o = data.get(field); if (o == null) { return null; } if (!Number.class.isAssignableFrom(o.getClass())) { throw new GeneralException( "Expected type for: " + field + " to be: " + Number.class.getName() + ", is: " + o.getClass().getName()); } } return new Date(((Number) o).longValue()); }