commit
2003c72124
|
@ -0,0 +1,37 @@
|
|||
package com.baeldung.repositoryvsdaopattern;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
public class Tweet {
|
||||
|
||||
private String email;
|
||||
|
||||
private String tweetText;
|
||||
|
||||
private Date dateCreated;
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
public void setEmail(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public String getTweetText() {
|
||||
return tweetText;
|
||||
}
|
||||
|
||||
public void setTweetText(String tweetText) {
|
||||
this.tweetText = tweetText;
|
||||
}
|
||||
|
||||
public Date getDateCreated() {
|
||||
return dateCreated;
|
||||
}
|
||||
|
||||
public void setDateCreated(Date dateCreated) {
|
||||
this.dateCreated = dateCreated;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
package com.baeldung.repositoryvsdaopattern;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface TweetDao {
|
||||
|
||||
List<Tweet> fetchTweets(String email);
|
||||
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
package com.baeldung.repositoryvsdaopattern;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class TweetDaoImpl implements TweetDao {
|
||||
|
||||
@Override
|
||||
public List<Tweet> fetchTweets(String email) {
|
||||
List<Tweet> tweets = new ArrayList<Tweet>();
|
||||
|
||||
//call Twitter API and prepare Tweet object
|
||||
|
||||
return tweets;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,51 @@
|
|||
package com.baeldung.repositoryvsdaopattern;
|
||||
|
||||
public class User {
|
||||
|
||||
private Long id;
|
||||
private String userName;
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
private String email;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getUserName() {
|
||||
return userName;
|
||||
}
|
||||
|
||||
public void setUserName(String userName) {
|
||||
this.userName = userName;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
public void setEmail(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package com.baeldung.repositoryvsdaopattern;
|
||||
|
||||
public interface UserDao {
|
||||
|
||||
void create(User user);
|
||||
|
||||
User read(Long id);
|
||||
|
||||
void update(User user);
|
||||
|
||||
void delete(String userName);
|
||||
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
package com.baeldung.repositoryvsdaopattern;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
|
||||
public class UserDaoImpl implements UserDao {
|
||||
|
||||
private final EntityManager entityManager;
|
||||
|
||||
public UserDaoImpl(EntityManager entityManager) {
|
||||
this.entityManager = entityManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void create(User user) {
|
||||
entityManager.persist(user);
|
||||
}
|
||||
|
||||
@Override
|
||||
public User read(Long id) {
|
||||
return entityManager.find(User.class, id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(User user) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(String userName) {
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
package com.baeldung.repositoryvsdaopattern;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface UserRepository {
|
||||
|
||||
User get(Long id);
|
||||
|
||||
void add(User user);
|
||||
|
||||
void update(User user);
|
||||
|
||||
void remove(User user);
|
||||
|
||||
User findByUserName(String userName);
|
||||
|
||||
User findByEmail(String email);
|
||||
|
||||
List<Tweet> fetchTweets(User user);
|
||||
|
||||
}
|
|
@ -0,0 +1,51 @@
|
|||
package com.baeldung.repositoryvsdaopattern;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class UserRepositoryImpl implements UserRepository {
|
||||
|
||||
private UserDaoImpl userDaoImpl;
|
||||
private TweetDaoImpl tweetDaoImpl;
|
||||
|
||||
@Override
|
||||
public User get(Long id) {
|
||||
UserSocialMedia user = (UserSocialMedia) userDaoImpl.read(id);
|
||||
|
||||
List<Tweet> tweets = tweetDaoImpl.fetchTweets(user.getEmail());
|
||||
user.setTweets(tweets);
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(User user) {
|
||||
userDaoImpl.create(user);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove(User user) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(User user) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Tweet> fetchTweets(User user) {
|
||||
return tweetDaoImpl.fetchTweets(user.getEmail());
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public User findByUserName(String userName) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public User findByEmail(String email) {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
package com.baeldung.repositoryvsdaopattern;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class UserSocialMedia extends User {
|
||||
|
||||
private List<Tweet> tweets;
|
||||
|
||||
public List<Tweet> getTweets() {
|
||||
return tweets;
|
||||
}
|
||||
|
||||
public void setTweets(List<Tweet> tweets) {
|
||||
this.tweets = tweets;
|
||||
}
|
||||
|
||||
}
|
|
@ -10,7 +10,7 @@ import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
|
|||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan("com.baeldung.spring.jdbc")
|
||||
@ComponentScan("com.baeldung.spring.jdbc.template.guide")
|
||||
public class SpringJdbcConfig {
|
||||
|
||||
@Bean
|
||||
|
|
|
@ -7,7 +7,7 @@ import restx.tests.RestxSpecTestsRunner;
|
|||
|
||||
@RunWith(RestxSpecTestsRunner.class)
|
||||
@FindSpecsIn("specs/hello")
|
||||
public class HelloResourceSpecUnitTest {
|
||||
public class HelloResourceSpecIntegrationTest {
|
||||
|
||||
/**
|
||||
* Useless, thanks to both @RunWith(RestxSpecTestsRunner.class) & @FindSpecsIn()
|
|
@ -9,9 +9,9 @@
|
|||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-spring-4</artifactId>
|
||||
<artifactId>parent-spring-5</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-spring-4</relativePath>
|
||||
<relativePath>../../parent-spring-5</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -21,17 +21,17 @@
|
|||
<dependency>
|
||||
<groupId>org.springframework.security</groupId>
|
||||
<artifactId>spring-security-web</artifactId>
|
||||
<version>${org.springframework.security.version}</version>
|
||||
<version>${spring-security.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.security</groupId>
|
||||
<artifactId>spring-security-config</artifactId>
|
||||
<version>${org.springframework.security.version}</version>
|
||||
<version>${spring-security.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.security</groupId>
|
||||
<artifactId>spring-security-taglibs</artifactId>
|
||||
<version>${org.springframework.security.version}</version>
|
||||
<version>${spring-security.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Spring -->
|
||||
|
@ -39,33 +39,33 @@
|
|||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-core</artifactId>
|
||||
<version>${org.springframework.version}</version>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
<version>${org.springframework.version}</version>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-beans</artifactId>
|
||||
<version>${org.springframework.version}</version>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-aop</artifactId>
|
||||
<version>${org.springframework.version}</version>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-web</artifactId>
|
||||
<version>${org.springframework.version}</version>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-webmvc</artifactId>
|
||||
<version>${org.springframework.version}</version>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- web -->
|
||||
|
@ -163,11 +163,64 @@
|
|||
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<!-- Spring -->
|
||||
<org.springframework.version>4.3.6.RELEASE</org.springframework.version>
|
||||
<org.springframework.security.version>4.2.1.RELEASE</org.springframework.security.version>
|
||||
<profiles>
|
||||
<profile>
|
||||
<id>default-first</id>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>com.github.eirslett</groupId>
|
||||
<artifactId>frontend-maven-plugin</artifactId>
|
||||
|
||||
<executions>
|
||||
<execution>
|
||||
<id>install node and npm</id>
|
||||
<phase>none</phase>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>npm install</id>
|
||||
<phase>none</phase>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>npm run build</id>
|
||||
<phase>none</phase>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</profile>
|
||||
<profile>
|
||||
<id>default-second</id>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>com.github.eirslett</groupId>
|
||||
<artifactId>frontend-maven-plugin</artifactId>
|
||||
|
||||
<executions>
|
||||
<execution>
|
||||
<id>install node and npm</id>
|
||||
<phase>none</phase>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>npm install</id>
|
||||
<phase>none</phase>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>npm run build</id>
|
||||
<phase>none</phase>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</profile>
|
||||
</profiles>
|
||||
|
||||
<properties>
|
||||
<!-- util -->
|
||||
<guava.version>19.0</guava.version>
|
||||
|
||||
|
@ -179,7 +232,5 @@
|
|||
<!-- frontend -->
|
||||
<node.version>v8.11.3</node.version>
|
||||
<npm.version>6.1.0</npm.version>
|
||||
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -10,9 +10,9 @@
|
|||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-spring-4</artifactId>
|
||||
<artifactId>parent-spring-5</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-spring-4</relativePath>
|
||||
<relativePath>../../parent-spring-5</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -182,7 +182,6 @@
|
|||
|
||||
<properties>
|
||||
<hibernate-core.version>5.2.10.Final</hibernate-core.version>
|
||||
<spring-security.version>4.2.3.RELEASE</spring-security.version>
|
||||
<spring-data-jpa.version>1.11.3.RELEASE</spring-data-jpa.version>
|
||||
<logback-classic.version>1.2.3</logback-classic.version>
|
||||
<spring-boot-starter-test.version>1.5.10.RELEASE</spring-boot-starter-test.version>
|
||||
|
|
|
@ -2,63 +2,63 @@
|
|||
<head>
|
||||
<title>Chat WebSocket</title>
|
||||
|
||||
<script src="./js/sockjs-0.3.4.js"></script>
|
||||
<script src="./js/stomp.js"></script>
|
||||
<script src="resources/js/sockjs-0.3.4.js"></script>
|
||||
<script src="resources/js/stomp.js"></script>
|
||||
|
||||
<script type="text/javascript">
|
||||
|
||||
|
||||
var stompClient = null;
|
||||
|
||||
|
||||
function setConnected(connected) {
|
||||
|
||||
|
||||
document.getElementById('connect').disabled = connected;
|
||||
document.getElementById('disconnect').disabled = !connected;
|
||||
document.getElementById('conversationDiv').style.visibility = connected ? 'visible' : 'hidden';
|
||||
document.getElementById('response').innerHTML = '';
|
||||
}
|
||||
|
||||
|
||||
function connect() {
|
||||
|
||||
var socket = new SockJS('/spring-mvc-java/chat');
|
||||
stompClient = Stomp.over(socket);
|
||||
|
||||
|
||||
var socket = new SockJS('/chat');
|
||||
stompClient = Stomp.over(socket);
|
||||
|
||||
stompClient.connect({}, function(frame) {
|
||||
|
||||
|
||||
setConnected(true);
|
||||
console.log('Connected: ' + frame);
|
||||
stompClient.subscribe('/topic/messages', function(messageOutput) {
|
||||
|
||||
|
||||
showMessageOutput(JSON.parse(messageOutput.body));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
function disconnect() {
|
||||
|
||||
|
||||
if(stompClient != null) {
|
||||
stompClient.disconnect();
|
||||
}
|
||||
|
||||
|
||||
setConnected(false);
|
||||
console.log("Disconnected");
|
||||
}
|
||||
|
||||
|
||||
function sendMessage() {
|
||||
|
||||
|
||||
var from = document.getElementById('from').value;
|
||||
var text = document.getElementById('text').value;
|
||||
stompClient.send("/app/chat", {}, JSON.stringify({'from':from, 'text':text}));
|
||||
}
|
||||
|
||||
|
||||
function showMessageOutput(messageOutput) {
|
||||
|
||||
|
||||
var response = document.getElementById('response');
|
||||
var p = document.createElement('p');
|
||||
p.style.wordWrap = 'break-word';
|
||||
p.appendChild(document.createTextNode(messageOutput.from + ": " + messageOutput.text + " (" + messageOutput.time + ")"));
|
||||
response.appendChild(p);
|
||||
}
|
||||
|
||||
|
||||
</script>
|
||||
|
||||
</head>
|
Loading…
Reference in New Issue