Example #1
0
  private void updateEvent(PartakeConnection con, IPartakeDAOs daos)
      throws PartakeException, DAOException {
    if (params.containsKey("title")) {
      String title = getString("title");
      if (StringUtils.isBlank(title) || title.length() > 100)
        throw new PartakeException(
            UserErrorCode.INVALID_PARAMETERS, "title", "タイトルは 100 文字以下で必ず入力してください。");
      else event.setTitle(title);
    }

    if (params.containsKey("summary")) {
      String summary = getString("summary");
      if (summary != null && summary.length() > 100)
        throw new PartakeException(
            UserErrorCode.INVALID_PARAMETERS, "summary", "概要は 100 文字以下で入力してください。");
      else event.setSummary(summary);
    }

    if (params.containsKey("category")) {
      String category = getString("category");
      if (category == null || !EventCategory.isValidCategoryName(category))
        throw new PartakeException(
            UserErrorCode.INVALID_PARAMETERS, "category", "カテゴリーは正しいものを必ず入力してください。");
      else event.setCategory(category);
    }

    if (params.containsKey("beginDate")) {
      DateTime beginDate = getDateTime("beginDate");
      if (beginDate == null)
        throw new PartakeException(
            UserErrorCode.INVALID_PARAMETERS, "beginDate", "開始日時は必ず入力してください。");

      Calendar beginCalendar = TimeUtil.calendar(beginDate.toDate());
      if (beginCalendar.get(Calendar.YEAR) < 2000 || 2100 < beginCalendar.get(Calendar.YEAR))
        throw new PartakeException(UserErrorCode.INVALID_PARAMETERS, "beginDate", "開始日時の範囲が不正です。");
      else event.setBeginDate(beginDate);

      json.put("eventDuration", Helper.readableDuration(event.getBeginDate(), event.getEndDate()));
    }

    // TODO: What happend if beginDate is set after endDate is set and endDate <= beginDate. It
    // should be an error.
    if (params.containsKey("endDate")) {
      String endDateStr = getString("endDate");
      if (StringUtils.isBlank(endDateStr)) event.setEndDate(null);
      else {
        DateTime endDate = TimeUtil.parseForEvent(endDateStr);
        if (endDate == null)
          throw new PartakeException(UserErrorCode.INVALID_PARAMETERS, "endDate", "終了日時が不正です。");
        else {
          Calendar endCalendar = TimeUtil.calendar(endDate.toDate());
          if (endCalendar.get(Calendar.YEAR) < 2000 || 2100 < endCalendar.get(Calendar.YEAR))
            throw new PartakeException(
                UserErrorCode.INVALID_PARAMETERS, "endDate", "終了日時の範囲が不正です。");
          else if (event.getBeginDate() != null && endDate.isBefore(event.getBeginDate()))
            throw new PartakeException(
                UserErrorCode.INVALID_PARAMETERS, "endDate", "終了日時が開始日時より前になっています。");
          else event.setEndDate(endDate);
        }
      }
      json.put("eventDuration", Helper.readableDuration(event.getBeginDate(), event.getEndDate()));
    }

    if (params.containsKey("url")) {
      String urlStr = getString("url");
      if (StringUtils.isBlank(urlStr)) event.setUrl(urlStr);
      else if (3000 < urlStr.length())
        throw new PartakeException(UserErrorCode.INVALID_PARAMETERS, "url", "URL が長すぎます。");
      else if (!urlStr.startsWith("http://") && !urlStr.startsWith("https://"))
        throw new PartakeException(UserErrorCode.INVALID_PARAMETERS, "url", "URL が不正です。");
      else {
        try {
          new URL(urlStr); // Confirms URL is not malformed.
          event.setUrl(urlStr);
        } catch (MalformedURLException e) {
          throw new PartakeException(UserErrorCode.INVALID_PARAMETERS, "url", "URL が不正です。");
        }
      }
    }

    if (params.containsKey("place")) {
      String place = getString("place");
      if (place != null && 300 < place.length())
        throw new PartakeException(UserErrorCode.INVALID_PARAMETERS, "place", "場所が長すぎます");
      else event.setPlace(place);
    }

    if (params.containsKey("address")) {
      String address = getString("address");
      if (address != null && 300 < address.length())
        throw new PartakeException(UserErrorCode.INVALID_PARAMETERS, "address", "住所が長すぎます。");
      else event.setAddress(address);
    }

    if (params.containsKey("description")) {
      String description = getString("description");
      if (description != null && 1000000 < description.length())
        throw new PartakeException(
            UserErrorCode.INVALID_PARAMETERS, "description", "説明は 1000000 文字以下で入力してください。");
      else event.setDescription(description);
    }

    if (params.containsKey("hashTag")) {
      String hashTag = getString("hashTag");
      if (StringUtils.isBlank(hashTag)) event.setHashTag(null);
      else if (100 < hashTag.length())
        throw new PartakeException(
            UserErrorCode.INVALID_PARAMETERS, "hashTag", "ハッシュタグは100文字以内で記述してください。");
      else if (!Util.isValidHashtag(hashTag))
        throw new PartakeException(
            UserErrorCode.INVALID_PARAMETERS, "hashTag", "ハッシュタグは # から始まる英数字や日本語が指定できます。記号は使えません。");
      else event.setHashTag(hashTag);
    }

    if (params.containsKey("passcode")) {
      String passcode = getString("passcode");
      if (StringUtils.isBlank(passcode)) event.setPasscode(null);
      else if (20 < passcode.length())
        throw new PartakeException(
            UserErrorCode.INVALID_PARAMETERS, "passcode", "パスコードは20文字以下で記入してください。");
      else event.setPasscode(passcode);
    }

    if (params.containsKey("foreImageId")) {
      String foreImageId = getString("foreImageId");
      if (StringUtils.isBlank(foreImageId) || "null".equals(foreImageId))
        event.setForeImageId(null);
      else if (!Util.isUUID(foreImageId))
        throw new PartakeException(UserErrorCode.INVALID_PARAMETERS, "foreImageId", "画像IDが不正です。");
      else {
        // Check foreImageId is owned by the owner.
        UserImage image = daos.getImageAccess().find(con, foreImageId);
        if (image == null)
          throw new PartakeException(UserErrorCode.INVALID_PARAMETERS, "foreImageId", "画像IDが不正です。");
        if (!user.getId().equals(image.getUserId()))
          throw new PartakeException(
              UserErrorCode.INVALID_PARAMETERS, "foreImageId", "あなたが所持していない画像の ID が指定されています。");

        // OK.
        event.setForeImageId(foreImageId);
      }
    }

    if (params.containsKey("backImageId")) {
      String backImageId = getString("backImageId");
      if (StringUtils.isBlank(backImageId) || "null".equals(backImageId))
        event.setBackImageId(null);
      else if (!Util.isUUID(backImageId))
        throw new PartakeException(UserErrorCode.INVALID_PARAMETERS, "backImageId", "画像IDが不正です。");
      else {
        // Check foreImageId is owned by the owner.
        UserImage image = daos.getImageAccess().find(con, backImageId);
        if (image == null)
          throw new PartakeException(UserErrorCode.INVALID_PARAMETERS, "backImageId", "画像IDが不正です。");
        if (!user.getId().equals(image.getUserId()))
          throw new PartakeException(
              UserErrorCode.INVALID_PARAMETERS, "backImageId", "あなたが所持していない画像の ID が指定されています。");

        // OK.
        event.setBackImageId(backImageId);
      }
    }

    if (params.containsKey("relatedEventIds[]")) {
      Set<String> visitedIds = new HashSet<String>();
      String[] relatedEventIds = getStrings("relatedEventIds[]");

      List<String> eventIds = new ArrayList<String>();
      JSONArray array = new JSONArray();
      for (String relatedEventId : relatedEventIds) {
        if (!Util.isUUID(relatedEventId)) continue;

        if (eventId.equals(relatedEventId)) continue;

        if (visitedIds.contains(relatedEventId)) continue;

        visitedIds.add(relatedEventId);

        Event relatedEvent = daos.getEventAccess().find(con, relatedEventId);
        if (relatedEvent == null) continue;

        eventIds.add(relatedEventId);

        JSONObject obj = new JSONObject();
        obj.put("id", relatedEvent.getId());
        obj.put("title", relatedEvent.getTitle());
        array.add(obj);
      }
      event.setRelatedEventIds(eventIds);

      // OK. We want to return event.id and event.title.
      json.put("relatedEvents", array);
    }

    if (params.containsKey("editorIds[]")) {
      JSONArray array = new JSONArray();
      List<String> editorIds = new ArrayList<String>();
      Set<String> visitedIds = new HashSet<String>();
      for (String editorId : getStrings("editorIds[]")) {
        // Skips invalid users here.
        if (!Util.isUUID(editorId)) continue;

        if (visitedIds.contains(editorId)) continue;
        visitedIds.add(editorId);

        UserEx editor = UserDAOFacade.getUserEx(con, daos, editorId);
        if (editor == null) continue;

        // OK.
        editorIds.add(editor.getId());
        array.add(editor.toSafeJSON());
      }

      event.setEditorIds(editorIds);
      json.put("editors", array);
    }
  }