Skip to content

Master #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 43 commits into
base: 01
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
965a0ff
Initial commit
joshuawhite929 Feb 26, 2012
7b55af9
added resources directory and moved dataSource.properties
joshuawhite929 Feb 26, 2012
07609de
Initial commit for branch 03
joshuawhite929 Feb 26, 2012
30e3bae
Changes required for branch 04
joshuawhite929 Feb 26, 2012
674d181
Added ConsoleApp.java to run branch 04 example
joshuawhite929 Feb 27, 2012
709e424
Branch 05 changes
joshuawhite929 Feb 27, 2012
b4592bb
changed value to ref
joshuawhite929 Feb 27, 2012
572a369
updated spring schema reference
joshuawhite929 Feb 27, 2012
9162dba
updated spring schema version
joshuawhite929 Feb 27, 2012
a9b333b
Changes to support branch 06
joshuawhite929 Feb 27, 2012
72b2423
Branch 07 changes
joshuawhite929 Feb 27, 2012
43d2fe8
Changes to support branch 08
joshuawhite929 Feb 27, 2012
e73b9bb
formatting changes
joshuawhite929 Mar 11, 2012
81295fd
formatting changes
joshuawhite929 Mar 11, 2012
0d50d66
formatting changes
joshuawhite929 Mar 11, 2012
c145ff8
formatting changes
joshuawhite929 Mar 11, 2012
13bb398
formatting changes
joshuawhite929 Mar 11, 2012
3d62163
formatting changes
joshuawhite929 Mar 11, 2012
4039547
formatting changes
joshuawhite929 Mar 11, 2012
f9a2652
Merge branch '01' into 02
williewheeler May 1, 2013
bdd30a7
Merge branch '01' into 02
williewheeler May 1, 2013
2691537
Merge branch '02' into 03
williewheeler May 1, 2013
54fe626
Merge branch '03' into 04
williewheeler May 1, 2013
27c8dfe
Merge branch '04' into 05
williewheeler May 1, 2013
9409c8e
Merge branch '05' into 06
williewheeler May 1, 2013
4c5c586
Merge branch '06' into 07
williewheeler May 1, 2013
2f011a3
Merge branch '07' into 08
williewheeler May 1, 2013
42a590a
Merge branch '01' into 02
williewheeler May 1, 2013
239175c
Merged 01
williewheeler May 1, 2013
149e5eb
Merged 02
williewheeler May 1, 2013
81a0846
Merged
williewheeler May 1, 2013
a37e496
Added missing boilerplate
williewheeler May 1, 2013
6e092d4
Merged
williewheeler May 1, 2013
31be968
Merged
williewheeler May 1, 2013
1294947
Added boilerplate
williewheeler May 1, 2013
07ddda6
Merge branch '04' into 05
williewheeler May 1, 2013
fc0da10
Merge branch '05' into 06
williewheeler May 1, 2013
3b707f1
Fixed recipe number
williewheeler May 1, 2013
485bfae
Merge branch '05' into 06
williewheeler May 1, 2013
61a8866
Fixed recipe number
williewheeler May 1, 2013
aab5f42
Merged
williewheeler May 1, 2013
7475add
Merge branch '07' into 08
williewheeler May 1, 2013
49295f5
Fixed recipe number
williewheeler May 1, 2013
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
2 changes: 1 addition & 1 deletion README
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
========================================================================================================================
README - Spring in Practice Recipe 1.1
README - Spring in Practice Recipe 1.8

Copyright (c) 2013 Manning Publications Co.

Expand Down
43 changes: 24 additions & 19 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
========================================================================================================================
pom.xml
Copyright (c) 2013 Manning Publications Co.
Joshua White
Willie Wheeler (willie.wheeler@gmail.com)
Book: http://manning.com/wheeler/
Blog: http://springinpractice.com/
Code: https://github.com/springinpractice
========================================================================================================================
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<?xml version="1.0" encoding="UTF-8"?>

<!--
========================================================================================================================
pom.xml

Copyright (c) 2013 Manning Publications Co.

Joshua White
Willie Wheeler (willie.wheeler@gmail.com)

Book: http://manning.com/wheeler/
Blog: http://springinpractice.com/
Code: https://github.com/springinpractice
========================================================================================================================
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

Expand All @@ -33,6 +33,11 @@ Code: https://github.com/springinpractice
</properties>

<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/com/springinpractice/ch01/ConsoleApp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (c) 2013 Manning Publications Co.
*
* Book: http://manning.com/wheeler/
* Blog: http://springinpractice.com/
* Code: https://github.com/springinpractice
*/
package com.springinpractice.ch01;

import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.springinpractice.ch01.model.Account;
import com.springinpractice.ch01.service.AccountService;

public class ConsoleApp {
public static void main(String[] args) throws Exception {
ApplicationContext appCtx =
new ClassPathXmlApplicationContext("applicationContext.xml");
AccountService accountService =
(AccountService)appCtx.getBean("accountService");
List<Account> delinquentAccounts = accountService
.findDeliquentAccounts();

for (Account a : delinquentAccounts) {
System.out.println(a.getAccountNo());
}
}
}
6 changes: 6 additions & 0 deletions src/main/java/com/springinpractice/ch01/dao/AccountDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,11 @@
*/
package com.springinpractice.ch01.dao;

import java.util.List;

import com.springinpractice.ch01.model.Account;

public interface AccountDao {

List<Account> findAll() throws Exception;
}
50 changes: 50 additions & 0 deletions src/main/java/com/springinpractice/ch01/dao/csv/CsvAccountDao.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright (c) 2013 Manning Publications Co.
*
* Book: http://manning.com/wheeler/
* Blog: http://springinpractice.com/
* Code: https://github.com/springinpractice
*/
package com.springinpractice.ch01.dao.csv;

import java.io.BufferedReader;
import java.io.FileReader;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.core.io.Resource;
import com.springinpractice.ch01.dao.AccountDao;
import com.springinpractice.ch01.model.Account;

public class CsvAccountDao implements AccountDao {

private Resource csvResource;

public void setCsvResource(Resource csvFile) {
this.csvResource = csvFile;
}

public List<Account> findAll() throws Exception {
List<Account> results = new ArrayList<Account>();

DateFormat fmt = new SimpleDateFormat("MMddyyyy");
BufferedReader br = new BufferedReader(
new FileReader(csvResource.getFile()));
String line;
while ((line = br.readLine()) != null) {
String[] fields = line.split(",");

String accountNo = fields[0];
BigDecimal balance = new BigDecimal(fields[1]);
Date lastPaidOn = fmt.parse(fields[2]);
Account account =
new Account(accountNo, balance, lastPaidOn);
results.add(account);
}
br.close();
return results;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,30 @@
*/
package com.springinpractice.ch01.dao.jdbc;

import org.apache.commons.dbcp.BasicDataSource;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.springinpractice.ch01.dao.AccountDao;
import com.springinpractice.ch01.model.Account;

@Component
public class JdbcAccountDao implements AccountDao {
private BasicDataSource dataSource;

public JdbcAccountDao() {
dataSource = new BasicDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/springbook" +
"?autoReconnect=true");
dataSource.setUsername("root");
dataSource.setPassword("");
@Autowired
private DataSource dataSource;

public JdbcAccountDao() {}

public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}

public List<Account> findAll() throws Exception {
throw new UnsupportedOperationException("This method has not been implemented");
}

}
36 changes: 36 additions & 0 deletions src/main/java/com/springinpractice/ch01/model/Account.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (c) 2013 Manning Publications Co.
*
* Book: http://manning.com/wheeler/
* Blog: http://springinpractice.com/
* Code: https://github.com/springinpractice
*/
package com.springinpractice.ch01.model;

import java.math.BigDecimal;
import java.util.Date;

public class Account {

private String accountNo;
private BigDecimal balance;
private Date lastPaidOn;

public Account(String accountNo, BigDecimal balance, Date lastPaidOn) {
this.accountNo = accountNo;
this.balance = balance;
this.lastPaidOn = lastPaidOn;
}

public String getAccountNo() {
return accountNo;
}

public BigDecimal getBalance() {
return balance;
}

public Date getLastPaidOn() {
return lastPaidOn;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright (c) 2013 Manning Publications Co.
*
* Book: http://manning.com/wheeler/
* Blog: http://springinpractice.com/
* Code: https://github.com/springinpractice
*/
package com.springinpractice.ch01.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.springinpractice.ch01.dao.AccountDao;
import com.springinpractice.ch01.model.Account;

@Component
public class AccountService {

@Autowired
private AccountDao accountDao;

public AccountService() {}

public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}

public List<Account> findDeliquentAccounts() throws Exception {
List<Account> delinquentAccounts = new ArrayList<Account>();
List<Account> accounts = accountDao.findAll();

Date thirtyDaysAgo = daysAgo(30);
for (Account account : accounts) {
boolean owesMoney = account.getBalance()
.compareTo(BigDecimal.ZERO) > 0;
boolean thirtyDaysLate = account.getLastPaidOn()
.compareTo(thirtyDaysAgo) <= 0;

if (owesMoney && thirtyDaysLate) {
delinquentAccounts.add(account);
}
}
return delinquentAccounts;
}

private static Date daysAgo(int days) {
GregorianCalendar gc = new GregorianCalendar();
gc.add(Calendar.DATE, -days);
return gc.getTime();
}

}
3 changes: 3 additions & 0 deletions src/main/resources/accounts.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
100,0,09012008
200,100,08012008
300,-100,09012008
28 changes: 28 additions & 0 deletions src/main/resources/applicationContext.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Source project: sip01, branch: 08 (Maven Project) -->

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">

<context:component-scan base-package="com.springinpractice.ch01"/>

<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="springbook.properties"/>

<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close"
p:driverClassName="${dataSource.driverClassName}"
p:url="${dataSource.url}"
p:username="${dataSource.username}"
p:password="${dataSource.password}"/>

</beans>
4 changes: 4 additions & 0 deletions src/main/resources/dataSource.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
driverClassName=com.mysql.jdbc.Driver
url=:mysql://localhost:3306/springbook?autoReconnect=true
username=someusername
password=somepassword
8 changes: 8 additions & 0 deletions src/main/resources/springbook.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Change springbook.properties to reflect your environment
dataSource.driverClassName=com.mysql.jdbc.Driver
dataSource.url=jdbc:mysql://localhost:3306/springbook?autoReconnect=true
dataSource.username=root
dataSource.password=
mailSender.host=mail.kattare.com
recaptcha.publicKey=get_one_from_recaptcha_website
recaptcha.privateKey=get_one_from_recaptcha_website