コード例 #1
0
  public String getBookDetailsAsString(String id) {
    Book book = hBookDetails.get(id);
    String json = "";
    ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
    JSONObject ret = new JSONObject();

    ret.put("id", book.getId());
    ret.put("author", book.getAuthor());
    ret.put("name", book.getName());
    ret.put("description", book.getDescription());
    ret.put("category", book.getCategory());
    ret.put("price", book.getPrice());
    ret.put("isNew", book.getIsNew());

    try {

      json = ow.writeValueAsString(book.getComments());
      ret.put("comments", new JSONArray(json));

    } catch (IOException e) {
      e.printStackTrace();
    } catch (Exception e) {
      e.printStackTrace();
    }

    return ret.toString();
  }
コード例 #2
0
  public String getInitialization() {
    Locale locale = threadLocale.getLocale();

    JSONObject spec = new JSONObject();

    DateFormatSymbols symbols = new DateFormatSymbols(locale);

    spec.put("months", new JSONArray((Object[]) symbols.getMonths()));

    StringBuilder days = new StringBuilder();

    String[] weekdays = symbols.getWeekdays();

    Calendar c = Calendar.getInstance(locale);

    int firstDay = c.getFirstDayOfWeek();

    // DatePicker needs them in order from monday to sunday.

    for (int i = Calendar.MONDAY; i <= Calendar.SATURDAY; i++) {
      days.append(weekdays[i].substring(0, 1));
    }

    days.append(weekdays[Calendar.SUNDAY].substring(0, 1));

    spec.put("days", days.toString().toLowerCase(locale));

    // jQuery DatePicker widget expects 0 to be sunday. Calendar defines SUNDAY as 1, MONDAY as 2,
    // etc.
    spec.put("firstDay", firstDay - 1);

    // set language
    spec.put("language", locale.getLanguage());

    // TODO: Skip localization if locale is English?

    return String.format("Tapestry.DateField.initLocalization(%s);", spec.toString(compactJSON));
  }