From dc58d2a8015f063f0801d0847d2466f694b92e44 Mon Sep 17 00:00:00 2001 From: DOHA Date: Sun, 8 Mar 2015 21:59:07 +0200 Subject: [PATCH 1/2] modify schedule reddit --- .../org/baeldung/persistence/model/Post.java | 10 +++ .../org/baeldung/web/RedditController.java | 56 +++++++++---- .../baeldung/web/schedule/ScheduledTasks.java | 4 +- .../main/webapp/WEB-INF/jsp/editPostForm.jsp | 79 +++++++++++++++++++ .../main/webapp/WEB-INF/jsp/postListView.jsp | 21 ++++- .../src/main/webapp/WEB-INF/jsp/reddit.jsp | 4 +- .../webapp/WEB-INF/jsp/schedulePostForm.jsp | 15 ++-- .../webapp/WEB-INF/jsp/submissionForm.jsp | 14 ++-- .../webapp/WEB-INF/jsp/submissionResponse.jsp | 4 +- .../src/main/webapp/index.jsp | 6 +- 10 files changed, 179 insertions(+), 34 deletions(-) create mode 100755 spring-security-oauth/src/main/webapp/WEB-INF/jsp/editPostForm.jsp diff --git a/spring-security-oauth/src/main/java/org/baeldung/persistence/model/Post.java b/spring-security-oauth/src/main/java/org/baeldung/persistence/model/Post.java index 2ddfc9b649..767c6fbe78 100644 --- a/spring-security-oauth/src/main/java/org/baeldung/persistence/model/Post.java +++ b/spring-security-oauth/src/main/java/org/baeldung/persistence/model/Post.java @@ -22,6 +22,8 @@ public class Post { private String url; + private boolean sendReplies; + private Date submissionDate; private boolean isSent; @@ -68,6 +70,14 @@ public class Post { this.url = url; } + public boolean isSendReplies() { + return sendReplies; + } + + public void setSendReplies(boolean sendReplies) { + this.sendReplies = sendReplies; + } + public Date getSubmissionDate() { return submissionDate; } diff --git a/spring-security-oauth/src/main/java/org/baeldung/web/RedditController.java b/spring-security-oauth/src/main/java/org/baeldung/web/RedditController.java index a8f48684cf..ee53d8257a 100644 --- a/spring-security-oauth/src/main/java/org/baeldung/web/RedditController.java +++ b/spring-security-oauth/src/main/java/org/baeldung/web/RedditController.java @@ -17,15 +17,18 @@ import org.baeldung.reddit.util.RedditApiConstants; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; import org.springframework.security.oauth2.client.OAuth2RestTemplate; import org.springframework.security.oauth2.common.OAuth2AccessToken; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; +import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.ResponseStatus; import com.fasterxml.jackson.databind.JsonNode; @@ -96,6 +99,9 @@ public class RedditController { post.setTitle(formParams.get("title")); post.setSubreddit(formParams.get("sr")); post.setUrl(formParams.get("url")); + if (formParams.containsKey("sendreplies")) { + post.setSendReplies(true); + } post.setSubmissionDate(dateFormat.parse(formParams.get("date"))); post.setSubmissionResponse("Not sent yet"); if (post.getSubmissionDate().before(new Date())) { @@ -116,6 +122,42 @@ public class RedditController { return "postListView"; } + // === post actions + + @RequestMapping(value = "/deletePost/{id}", method = RequestMethod.DELETE) + @ResponseStatus(HttpStatus.OK) + public void deletePost(@PathVariable("id") final Long id) { + postReopsitory.delete(id); + } + + @RequestMapping(value = "/editPost/{id}", method = RequestMethod.GET) + public String showEditPostForm(final Model model, @PathVariable Long id) { + final Post post = postReopsitory.findOne(id); + model.addAttribute("post", post); + model.addAttribute("dateValue", dateFormat.format(post.getSubmissionDate())); + return "editPostForm"; + } + + @RequestMapping(value = "/updatePost/{id}", method = RequestMethod.POST) + public String updatePost(Model model, @PathVariable("id") final Long id, @RequestParam final Map formParams) throws ParseException { + final Post post = postReopsitory.findOne(id); + post.setTitle(formParams.get("title")); + post.setSubreddit(formParams.get("sr")); + post.setUrl(formParams.get("url")); + if (formParams.containsKey("sendreplies")) { + post.setSendReplies(true); + } else { + post.setSendReplies(false); + } + post.setSubmissionDate(dateFormat.parse(formParams.get("date"))); + if (post.getSubmissionDate().before(new Date())) { + model.addAttribute("msg", "Invalid date"); + return "submissionResponse"; + } + postReopsitory.save(post); + return "redirect:/posts"; + } + // === private private final MultiValueMap constructParams(final Map formParams) { @@ -123,7 +165,6 @@ public class RedditController { param.add(RedditApiConstants.API_TYPE, "json"); param.add(RedditApiConstants.KIND, "link"); param.add(RedditApiConstants.RESUBMIT, "true"); - param.add(RedditApiConstants.SENDREPLIES, "false"); param.add(RedditApiConstants.THEN, "comments"); for (final Map.Entry entry : formParams.entrySet()) { param.add(entry.getKey(), entry.getValue()); @@ -131,19 +172,6 @@ public class RedditController { return param; } - private final Map constructParams2(final Map formParams) { - final Map param = new HashMap(); - param.put(RedditApiConstants.API_TYPE, "json"); - param.put(RedditApiConstants.KIND, "link"); - param.put(RedditApiConstants.RESUBMIT, "true"); - param.put(RedditApiConstants.SENDREPLIES, "false"); - param.put(RedditApiConstants.THEN, "comments"); - for (final Map.Entry entry : formParams.entrySet()) { - param.put(entry.getKey(), entry.getValue()); - } - return param; - } - private final String needsCaptcha() { final String result = redditRestTemplate.getForObject("https://oauth.reddit.com/api/needs_captcha.json", String.class); return result; diff --git a/spring-security-oauth/src/main/java/org/baeldung/web/schedule/ScheduledTasks.java b/spring-security-oauth/src/main/java/org/baeldung/web/schedule/ScheduledTasks.java index bfdfb3c5ff..9463086e52 100644 --- a/spring-security-oauth/src/main/java/org/baeldung/web/schedule/ScheduledTasks.java +++ b/spring-security-oauth/src/main/java/org/baeldung/web/schedule/ScheduledTasks.java @@ -60,8 +60,10 @@ public class ScheduledTasks { param.add(RedditApiConstants.API_TYPE, "json"); param.add(RedditApiConstants.KIND, "link"); param.add(RedditApiConstants.RESUBMIT, "true"); - param.add(RedditApiConstants.SENDREPLIES, "false"); param.add(RedditApiConstants.THEN, "comments"); + if (post.isSendReplies()) { + param.add(RedditApiConstants.SENDREPLIES, "true"); + } logger.info("Submit link with these parameters: " + param.entrySet()); final JsonNode node = redditRestTemplate.postForObject("https://oauth.reddit.com/api/submit", param, JsonNode.class); diff --git a/spring-security-oauth/src/main/webapp/WEB-INF/jsp/editPostForm.jsp b/spring-security-oauth/src/main/webapp/WEB-INF/jsp/editPostForm.jsp new file mode 100755 index 0000000000..0689f9e45f --- /dev/null +++ b/spring-security-oauth/src/main/webapp/WEB-INF/jsp/editPostForm.jsp @@ -0,0 +1,79 @@ +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> + + + +Schedule to Reddit + +"> + + + + + + +
+

Edit Scheduled Post

+
" method="post"> +
+ +
+ + +
+

+
+ + +
+

+
+ + +
+

+
+ checked /> Send replies to my inbox +
+

+ + + + + +

+ + +
+
+
+ + \ No newline at end of file diff --git a/spring-security-oauth/src/main/webapp/WEB-INF/jsp/postListView.jsp b/spring-security-oauth/src/main/webapp/WEB-INF/jsp/postListView.jsp index dc32612c8e..6c56adcc7a 100755 --- a/spring-security-oauth/src/main/webapp/WEB-INF/jsp/postListView.jsp +++ b/spring-security-oauth/src/main/webapp/WEB-INF/jsp/postListView.jsp @@ -1,10 +1,10 @@ <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> - + -Spring Security OAuth +Schedule to Reddit @@ -43,6 +43,7 @@ Post title Submission Date Status +Actions @@ -50,9 +51,25 @@ + + Edit + Delete + + + \ No newline at end of file diff --git a/spring-security-oauth/src/main/webapp/WEB-INF/jsp/reddit.jsp b/spring-security-oauth/src/main/webapp/WEB-INF/jsp/reddit.jsp index 1da8e5a580..15a3648429 100755 --- a/spring-security-oauth/src/main/webapp/WEB-INF/jsp/reddit.jsp +++ b/spring-security-oauth/src/main/webapp/WEB-INF/jsp/reddit.jsp @@ -1,8 +1,8 @@ <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> - + -Spring Security OAuth +Schedule to Reddit diff --git a/spring-security-oauth/src/main/webapp/WEB-INF/jsp/schedulePostForm.jsp b/spring-security-oauth/src/main/webapp/WEB-INF/jsp/schedulePostForm.jsp index 976354976c..7c74a614de 100755 --- a/spring-security-oauth/src/main/webapp/WEB-INF/jsp/schedulePostForm.jsp +++ b/spring-security-oauth/src/main/webapp/WEB-INF/jsp/schedulePostForm.jsp @@ -1,8 +1,8 @@ <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> - + -Spring Security OAuth +Schedule to Reddit "> @@ -42,17 +42,21 @@
- +


- +


- + +
+

+
+ Send replies to my inbox


@@ -65,6 +69,7 @@

+
diff --git a/spring-security-oauth/src/main/webapp/WEB-INF/jsp/submissionForm.jsp b/spring-security-oauth/src/main/webapp/WEB-INF/jsp/submissionForm.jsp index 17d8c3680c..644bb8a2e7 100755 --- a/spring-security-oauth/src/main/webapp/WEB-INF/jsp/submissionForm.jsp +++ b/spring-security-oauth/src/main/webapp/WEB-INF/jsp/submissionForm.jsp @@ -1,8 +1,8 @@ <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> - + -Spring Security OAuth +Schedule to Reddit @@ -39,17 +39,21 @@
- +


- +


- + +
+

+
+ Send replies to my inbox


diff --git a/spring-security-oauth/src/main/webapp/WEB-INF/jsp/submissionResponse.jsp b/spring-security-oauth/src/main/webapp/WEB-INF/jsp/submissionResponse.jsp index 3f35c396d2..df18f6b346 100755 --- a/spring-security-oauth/src/main/webapp/WEB-INF/jsp/submissionResponse.jsp +++ b/spring-security-oauth/src/main/webapp/WEB-INF/jsp/submissionResponse.jsp @@ -1,8 +1,8 @@ <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> - + -Spring Security OAuth +Schedule to Reddit diff --git a/spring-security-oauth/src/main/webapp/index.jsp b/spring-security-oauth/src/main/webapp/index.jsp index a3e546464a..ec8aeda34e 100755 --- a/spring-security-oauth/src/main/webapp/index.jsp +++ b/spring-security-oauth/src/main/webapp/index.jsp @@ -4,13 +4,13 @@ -Spring Security OAuth +Schedule to Reddit
-

Welcome to Spring Security OAuth

+

Schedule to Reddit

Login with Reddit - +
\ No newline at end of file From 9054203652382dbad7b6b75a5a397f1be71e4564 Mon Sep 17 00:00:00 2001 From: DOHA Date: Sun, 8 Mar 2015 22:13:25 +0200 Subject: [PATCH 2/2] fix home url --- .../src/main/webapp/WEB-INF/jsp/editPostForm.jsp | 2 +- .../src/main/webapp/WEB-INF/jsp/postListView.jsp | 2 +- .../src/main/webapp/WEB-INF/jsp/schedulePostForm.jsp | 2 +- .../src/main/webapp/WEB-INF/jsp/submissionForm.jsp | 2 +- .../src/main/webapp/WEB-INF/jsp/submissionResponse.jsp | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/spring-security-oauth/src/main/webapp/WEB-INF/jsp/editPostForm.jsp b/spring-security-oauth/src/main/webapp/WEB-INF/jsp/editPostForm.jsp index 0689f9e45f..6912cefbe1 100755 --- a/spring-security-oauth/src/main/webapp/WEB-INF/jsp/editPostForm.jsp +++ b/spring-security-oauth/src/main/webapp/WEB-INF/jsp/editPostForm.jsp @@ -20,7 +20,7 @@ - Schedule to Reddit + Schedule to Reddit
diff --git a/spring-security-oauth/src/main/webapp/WEB-INF/jsp/postListView.jsp b/spring-security-oauth/src/main/webapp/WEB-INF/jsp/postListView.jsp index 6c56adcc7a..63a70c1f27 100755 --- a/spring-security-oauth/src/main/webapp/WEB-INF/jsp/postListView.jsp +++ b/spring-security-oauth/src/main/webapp/WEB-INF/jsp/postListView.jsp @@ -19,7 +19,7 @@ - Schedule to Reddit + Schedule to Reddit diff --git a/spring-security-oauth/src/main/webapp/WEB-INF/jsp/schedulePostForm.jsp b/spring-security-oauth/src/main/webapp/WEB-INF/jsp/schedulePostForm.jsp index 7c74a614de..0193422ea3 100755 --- a/spring-security-oauth/src/main/webapp/WEB-INF/jsp/schedulePostForm.jsp +++ b/spring-security-oauth/src/main/webapp/WEB-INF/jsp/schedulePostForm.jsp @@ -20,7 +20,7 @@ - Schedule to Reddit + Schedule to Reddit diff --git a/spring-security-oauth/src/main/webapp/WEB-INF/jsp/submissionForm.jsp b/spring-security-oauth/src/main/webapp/WEB-INF/jsp/submissionForm.jsp index 644bb8a2e7..498b11b7ee 100755 --- a/spring-security-oauth/src/main/webapp/WEB-INF/jsp/submissionForm.jsp +++ b/spring-security-oauth/src/main/webapp/WEB-INF/jsp/submissionForm.jsp @@ -17,7 +17,7 @@ - Schedule to Reddit + Schedule to Reddit diff --git a/spring-security-oauth/src/main/webapp/WEB-INF/jsp/submissionResponse.jsp b/spring-security-oauth/src/main/webapp/WEB-INF/jsp/submissionResponse.jsp index df18f6b346..72f4a5187d 100755 --- a/spring-security-oauth/src/main/webapp/WEB-INF/jsp/submissionResponse.jsp +++ b/spring-security-oauth/src/main/webapp/WEB-INF/jsp/submissionResponse.jsp @@ -17,7 +17,7 @@ - Schedule to Reddit + Schedule to Reddit