Skip to content

Commit 1aad210

Browse files
committed
add schedule post to reddit
1 parent 4963e40 commit 1aad210

18 files changed

Lines changed: 2512 additions & 43 deletions
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package org.baeldung.config;
2+
3+
import java.util.Properties;
4+
5+
import javax.sql.DataSource;
6+
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.context.annotation.Bean;
9+
import org.springframework.context.annotation.ComponentScan;
10+
import org.springframework.context.annotation.Configuration;
11+
import org.springframework.context.annotation.PropertySource;
12+
import org.springframework.core.env.Environment;
13+
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
14+
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
15+
import org.springframework.jdbc.datasource.DriverManagerDataSource;
16+
import org.springframework.orm.jpa.JpaTransactionManager;
17+
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
18+
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
19+
import org.springframework.transaction.annotation.EnableTransactionManagement;
20+
21+
@Configuration
22+
@EnableTransactionManagement
23+
@PropertySource({ "classpath:persistence.properties" })
24+
@ComponentScan({ "org.baeldung.persistence" })
25+
@EnableJpaRepositories(basePackages = "org.baeldung.persistence.dao")
26+
public class PersistenceJPAConfig {
27+
28+
@Autowired
29+
private Environment env;
30+
31+
public PersistenceJPAConfig() {
32+
super();
33+
}
34+
35+
@Bean
36+
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
37+
final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
38+
em.setDataSource(dataSource());
39+
em.setPackagesToScan(new String[] { "org.baeldung.persistence.model" });
40+
final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
41+
em.setJpaVendorAdapter(vendorAdapter);
42+
em.setJpaProperties(additionalProperties());
43+
return em;
44+
}
45+
46+
@Bean
47+
public DataSource dataSource() {
48+
final DriverManagerDataSource dataSource = new DriverManagerDataSource();
49+
dataSource.setDriverClassName(env.getProperty("jdbc.driverClassName"));
50+
dataSource.setUrl(env.getProperty("jdbc.url"));
51+
dataSource.setUsername(env.getProperty("jdbc.user"));
52+
dataSource.setPassword(env.getProperty("jdbc.pass"));
53+
return dataSource;
54+
}
55+
56+
@Bean
57+
public JpaTransactionManager transactionManager() {
58+
final JpaTransactionManager transactionManager = new JpaTransactionManager();
59+
transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
60+
return transactionManager;
61+
}
62+
63+
@Bean
64+
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
65+
return new PersistenceExceptionTranslationPostProcessor();
66+
}
67+
68+
final Properties additionalProperties() {
69+
final Properties hibernateProperties = new Properties();
70+
hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
71+
hibernateProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
72+
return hibernateProperties;
73+
}
74+
75+
}

‎spring-security-oauth/src/main/java/org/baeldung/config/ServletInitializer.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class ServletInitializer extends AbstractDispatcherServletInitializer {
1313
@Override
1414
protected WebApplicationContext createServletApplicationContext() {
1515
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
16-
context.register(WebConfig.class);
16+
context.register(PersistenceJPAConfig.class, WebConfig.class);
1717
return context;
1818
}
1919

‎spring-security-oauth/src/main/java/org/baeldung/config/WebConfig.java‎

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
import java.util.Arrays;
44

5-
import org.baeldung.web.RedditController;
6-
import org.baeldung.web.RestExceptionHandler;
5+
import org.baeldung.web.schedule.ScheduledTasks;
76
import org.springframework.beans.factory.annotation.Value;
87
import org.springframework.context.annotation.Bean;
8+
import org.springframework.context.annotation.ComponentScan;
99
import org.springframework.context.annotation.Configuration;
1010
import org.springframework.context.annotation.PropertySource;
1111
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
12+
import org.springframework.scheduling.annotation.EnableAsync;
13+
import org.springframework.scheduling.annotation.EnableScheduling;
1214
import org.springframework.security.oauth2.client.OAuth2ClientContext;
1315
import org.springframework.security.oauth2.client.OAuth2RestTemplate;
1416
import org.springframework.security.oauth2.client.resource.OAuth2ProtectedResourceDetails;
@@ -28,6 +30,9 @@
2830

2931
@Configuration
3032
@EnableWebMvc
33+
@EnableScheduling
34+
@EnableAsync
35+
@ComponentScan({ "org.baeldung.web" })
3136
public class WebConfig extends WebMvcConfigurerAdapter {
3237

3338
@Bean
@@ -48,16 +53,23 @@ public void configureDefaultServletHandling(DefaultServletHandlerConfigurer conf
4853
configurer.enable();
4954
}
5055

56+
// @Bean
57+
// public RedditController redditController(OAuth2RestTemplate redditRestTemplate) {
58+
// RedditController controller = new RedditController();
59+
// controller.setRedditRestTemplate(redditRestTemplate);
60+
// return controller;
61+
// }
62+
//
63+
// @Bean
64+
// public RestExceptionHandler restExceptionHandler() {
65+
// return new RestExceptionHandler();
66+
// }
67+
//
5168
@Bean
52-
public RedditController redditController(OAuth2RestTemplate redditRestTemplate) {
53-
RedditController controller = new RedditController();
54-
controller.setRedditRestTemplate(redditRestTemplate);
55-
return controller;
56-
}
57-
58-
@Bean
59-
public RestExceptionHandler restExceptionHandler() {
60-
return new RestExceptionHandler();
69+
public ScheduledTasks scheduledTasks(OAuth2ProtectedResourceDetails reddit) {
70+
ScheduledTasks s = new ScheduledTasks();
71+
s.setRedditRestTemplate(new OAuth2RestTemplate(reddit));
72+
return s;
6173
}
6274

6375
public void addResourceHandlers(ResourceHandlerRegistry registry) {
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package org.baeldung.persistence.dao;
2+
3+
import java.util.Date;
4+
import java.util.List;
5+
6+
import org.baeldung.persistence.model.Post;
7+
import org.baeldung.persistence.model.User;
8+
import org.springframework.data.jpa.repository.JpaRepository;
9+
10+
public interface PostRepository extends JpaRepository<Post, Long> {
11+
12+
public List<Post> findBySubmissionDateBefore(Date date);
13+
14+
public List<Post> findByUser(User user);
15+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.baeldung.persistence.dao;
2+
3+
import org.baeldung.persistence.model.User;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
6+
public interface UserRepository extends JpaRepository<User, Long> {
7+
public User findByUsername(String username);
8+
9+
public User findByAccessToken(String token);
10+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package org.baeldung.persistence.model;
2+
3+
import java.util.Date;
4+
5+
import javax.persistence.Entity;
6+
import javax.persistence.GeneratedValue;
7+
import javax.persistence.GenerationType;
8+
import javax.persistence.Id;
9+
import javax.persistence.JoinColumn;
10+
import javax.persistence.ManyToOne;
11+
12+
@Entity
13+
public class Post {
14+
15+
@Id
16+
@GeneratedValue(strategy = GenerationType.AUTO)
17+
private Long id;
18+
19+
private String title;
20+
21+
private String subreddit;
22+
23+
private String url;
24+
25+
private Date submissionDate;
26+
27+
private boolean isSent;
28+
29+
private String submissionResponse;
30+
31+
@ManyToOne
32+
@JoinColumn(name = "user_id")
33+
private User user;
34+
35+
public Post() {
36+
super();
37+
}
38+
39+
public Long getId() {
40+
return id;
41+
}
42+
43+
public void setId(final Long id) {
44+
this.id = id;
45+
}
46+
47+
public String getTitle() {
48+
return title;
49+
}
50+
51+
public void setTitle(String title) {
52+
this.title = title;
53+
}
54+
55+
public String getSubreddit() {
56+
return subreddit;
57+
}
58+
59+
public void setSubreddit(String subreddit) {
60+
this.subreddit = subreddit;
61+
}
62+
63+
public String getUrl() {
64+
return url;
65+
}
66+
67+
public void setUrl(String url) {
68+
this.url = url;
69+
}
70+
71+
public Date getSubmissionDate() {
72+
return submissionDate;
73+
}
74+
75+
public void setSubmissionDate(Date submissionDate) {
76+
this.submissionDate = submissionDate;
77+
}
78+
79+
public User getUser() {
80+
return user;
81+
}
82+
83+
public void setUser(User user) {
84+
this.user = user;
85+
}
86+
87+
public boolean isSent() {
88+
return isSent;
89+
}
90+
91+
public void setSent(boolean isSent) {
92+
this.isSent = isSent;
93+
}
94+
95+
public String getSubmissionResponse() {
96+
return submissionResponse;
97+
}
98+
99+
public void setSubmissionResponse(String submissionResponse) {
100+
this.submissionResponse = submissionResponse;
101+
}
102+
103+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package org.baeldung.persistence.model;
2+
3+
import java.util.Date;
4+
import java.util.List;
5+
6+
import javax.persistence.Entity;
7+
import javax.persistence.GeneratedValue;
8+
import javax.persistence.GenerationType;
9+
import javax.persistence.Id;
10+
import javax.persistence.OneToMany;
11+
12+
@Entity
13+
public class User {
14+
15+
@Id
16+
@GeneratedValue(strategy = GenerationType.AUTO)
17+
private Long id;
18+
19+
private String username;
20+
21+
private String accessToken;
22+
23+
private String refreshToken;
24+
25+
private Date tokenExpiration;
26+
27+
@OneToMany(mappedBy = "user")
28+
private List<Post> posts;
29+
30+
public User() {
31+
super();
32+
}
33+
34+
public Long getId() {
35+
return id;
36+
}
37+
38+
public void setId(final Long id) {
39+
this.id = id;
40+
}
41+
42+
public String getUsername() {
43+
return username;
44+
}
45+
46+
public void setUsername(String username) {
47+
this.username = username;
48+
}
49+
50+
public String getAccessToken() {
51+
return accessToken;
52+
}
53+
54+
public void setAccessToken(String accessToken) {
55+
this.accessToken = accessToken;
56+
}
57+
58+
public String getRefreshToken() {
59+
return refreshToken;
60+
}
61+
62+
public void setRefreshToken(String refreshToken) {
63+
this.refreshToken = refreshToken;
64+
}
65+
66+
public Date getTokenExpiration() {
67+
return tokenExpiration;
68+
}
69+
70+
public void setTokenExpiration(Date tokenExpiration) {
71+
this.tokenExpiration = tokenExpiration;
72+
}
73+
74+
public List<Post> getPosts() {
75+
return posts;
76+
}
77+
78+
public void setPosts(List<Post> posts) {
79+
this.posts = posts;
80+
}
81+
82+
@Override
83+
public int hashCode() {
84+
final int prime = 31;
85+
int result = 1;
86+
result = prime * result + ((username == null) ? 0 : username.hashCode());
87+
return result;
88+
}
89+
90+
@Override
91+
public boolean equals(final Object obj) {
92+
if (this == obj)
93+
return true;
94+
if (obj == null)
95+
return false;
96+
if (getClass() != obj.getClass())
97+
return false;
98+
final User user = (User) obj;
99+
if (!username.equals(user.username))
100+
return false;
101+
return true;
102+
}
103+
104+
}

0 commit comments

Comments
 (0)