Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@

public class MyAuthorizationCodeAccessTokenProvider extends AuthorizationCodeAccessTokenProvider implements Serializable {

/**
*
*/
private static final long serialVersionUID = 3822611002661972274L;

private StateKeyGenerator stateKeyGenerator = new DefaultStateKeyGenerator();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package org.baeldung.config;

import java.util.Properties;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableTransactionManagement
@PropertySource({ "classpath:persistence.properties" })
@ComponentScan({ "org.baeldung.persistence" })
@EnableJpaRepositories(basePackages = "org.baeldung.persistence.dao")
public class PersistenceJPAConfig {

@Autowired
private Environment env;

public PersistenceJPAConfig() {
super();
}

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource());
em.setPackagesToScan(new String[] { "org.baeldung.persistence.model" });
final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
em.setJpaProperties(additionalProperties());
return em;
}

@Bean
public DataSource dataSource() {
final DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(env.getProperty("jdbc.driverClassName"));
dataSource.setUrl(env.getProperty("jdbc.url"));
dataSource.setUsername(env.getProperty("jdbc.user"));
dataSource.setPassword(env.getProperty("jdbc.pass"));
return dataSource;
}

@Bean
public JpaTransactionManager transactionManager() {
final JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
return transactionManager;
}

@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
return new PersistenceExceptionTranslationPostProcessor();
}

final Properties additionalProperties() {
final Properties hibernateProperties = new Properties();
hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
hibernateProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
return hibernateProperties;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class ServletInitializer extends AbstractDispatcherServletInitializer {
@Override
protected WebApplicationContext createServletApplicationContext() {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.register(WebConfig.class);
context.register(PersistenceJPAConfig.class, WebConfig.class);
return context;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

import java.util.Arrays;

import org.baeldung.web.schedule.ScheduledTasks;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.security.oauth2.client.OAuth2ClientContext;
import org.springframework.security.oauth2.client.OAuth2RestTemplate;
import org.springframework.security.oauth2.client.resource.OAuth2ProtectedResourceDetails;
Expand All @@ -27,7 +30,9 @@

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = { "org.baeldung.web" })
@EnableScheduling
@EnableAsync
@ComponentScan({ "org.baeldung.web" })
public class WebConfig extends WebMvcConfigurerAdapter {

@Bean
Expand All @@ -48,6 +53,25 @@ public void configureDefaultServletHandling(DefaultServletHandlerConfigurer conf
configurer.enable();
}

// @Bean
// public RedditController redditController(OAuth2RestTemplate redditRestTemplate) {
// RedditController controller = new RedditController();
// controller.setRedditRestTemplate(redditRestTemplate);
// return controller;
// }
//
// @Bean
// public RestExceptionHandler restExceptionHandler() {
// return new RestExceptionHandler();
// }
//
@Bean
public ScheduledTasks scheduledTasks(OAuth2ProtectedResourceDetails reddit) {
ScheduledTasks s = new ScheduledTasks();
s.setRedditRestTemplate(new OAuth2RestTemplate(reddit));
return s;
}

public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.baeldung.persistence.dao;

import java.util.Date;
import java.util.List;

import org.baeldung.persistence.model.Post;
import org.baeldung.persistence.model.User;
import org.springframework.data.jpa.repository.JpaRepository;

public interface PostRepository extends JpaRepository<Post, Long> {

public List<Post> findBySubmissionDateBefore(Date date);

public List<Post> findByUser(User user);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.baeldung.persistence.dao;

import org.baeldung.persistence.model.User;
import org.springframework.data.jpa.repository.JpaRepository;

public interface UserRepository extends JpaRepository<User, Long> {
public User findByUsername(String username);

public User findByAccessToken(String token);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package org.baeldung.persistence.model;

import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

@Entity
public class Post {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

private String title;

private String subreddit;

private String url;

private Date submissionDate;

private boolean isSent;

private String submissionResponse;

@ManyToOne
@JoinColumn(name = "user_id")
private User user;

public Post() {
super();
}

public Long getId() {
return id;
}

public void setId(final Long id) {
this.id = id;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getSubreddit() {
return subreddit;
}

public void setSubreddit(String subreddit) {
this.subreddit = subreddit;
}

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}

public Date getSubmissionDate() {
return submissionDate;
}

public void setSubmissionDate(Date submissionDate) {
this.submissionDate = submissionDate;
}

public User getUser() {
return user;
}

public void setUser(User user) {
this.user = user;
}

public boolean isSent() {
return isSent;
}

public void setSent(boolean isSent) {
this.isSent = isSent;
}

public String getSubmissionResponse() {
return submissionResponse;
}

public void setSubmissionResponse(String submissionResponse) {
this.submissionResponse = submissionResponse;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package org.baeldung.persistence.model;

import java.util.Date;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;

@Entity
public class User {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

private String username;

private String accessToken;

private String refreshToken;

private Date tokenExpiration;

@OneToMany(mappedBy = "user")
private List<Post> posts;

public User() {
super();
}

public Long getId() {
return id;
}

public void setId(final Long id) {
this.id = id;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getAccessToken() {
return accessToken;
}

public void setAccessToken(String accessToken) {
this.accessToken = accessToken;
}

public String getRefreshToken() {
return refreshToken;
}

public void setRefreshToken(String refreshToken) {
this.refreshToken = refreshToken;
}

public Date getTokenExpiration() {
return tokenExpiration;
}

public void setTokenExpiration(Date tokenExpiration) {
this.tokenExpiration = tokenExpiration;
}

public List<Post> getPosts() {
return posts;
}

public void setPosts(List<Post> posts) {
this.posts = posts;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((username == null) ? 0 : username.hashCode());
return result;
}

@Override
public boolean equals(final Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final User user = (User) obj;
if (!username.equals(user.username))
return false;
return true;
}

}
Loading