Skip to content

Commit 244d6a4

Browse files
committed
Add exercise15
1 parent b82653f commit 244d6a4

38 files changed

Lines changed: 1048 additions & 0 deletions
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
apply plugin: 'tomcat'
2+
3+
buildscript {
4+
repositories {
5+
jcenter()
6+
}
7+
8+
dependencies {
9+
classpath 'org.gradle.api.plugins:gradle-tomcat-plugin:1.0'
10+
}
11+
}
12+
13+
repositories {
14+
mavenCentral()
15+
}
16+
17+
dependencies {
18+
providedCompile 'javax.servlet:javax.servlet-api:3.1.0'
19+
compile 'org.springframework:spring-webmvc:4.0.0.RELEASE'
20+
compile 'org.springframework:spring-jdbc:4.0.0.RELEASE'
21+
compile 'org.springframework:spring-orm:4.0.0.RELEASE'
22+
compile 'org.hsqldb:hsqldb:2.3.1'
23+
runtime 'jstl:jstl:1.2'
24+
25+
compile 'org.hibernate:hibernate-core:4.3.0.Final'
26+
compile group: 'com.google.guava', name: 'guava', version: '15.0'
27+
28+
def tomcatVersion = '7.0.11'
29+
tomcat "org.apache.tomcat.embed:tomcat-embed-core:${tomcatVersion}",
30+
"org.apache.tomcat.embed:tomcat-embed-logging-juli:${tomcatVersion}"
31+
tomcat("org.apache.tomcat.embed:tomcat-embed-jasper:${tomcatVersion}") {
32+
exclude group: 'org.eclipse.jdt.core.compiler', module: 'ecj'
33+
}
34+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
org.gradle.jvmargs=-XX:MaxPermSize=256m
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package tw.codedata;
2+
3+
import javax.persistence.*;
4+
5+
@Entity
6+
@Table(name="directors")
7+
public class Director {
8+
@Id
9+
@GeneratedValue(strategy = GenerationType.AUTO)
10+
private Long id;
11+
private String name;
12+
13+
public Director() {}
14+
15+
public Director(String name) {
16+
this.name = name;
17+
}
18+
19+
public Long getId() {
20+
return id;
21+
}
22+
23+
public void setId(Long id) {
24+
this.id = id;
25+
}
26+
27+
public String getName() {
28+
return name;
29+
}
30+
31+
public void setName(String name) {
32+
this.name = name;
33+
}
34+
35+
@Override
36+
public String toString() {
37+
return "Director{" + "id=" + id + ", name=" + name + '}';
38+
}
39+
40+
41+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package tw.codedata;
2+
3+
import com.google.common.base.Optional;
4+
5+
public interface DirectorDao {
6+
void saveDirector(Director director);
7+
Optional<Director> maybeFromName(String name);
8+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package tw.codedata;
2+
3+
import com.google.common.base.Optional;
4+
import java.util.List;
5+
import org.hibernate.*;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.stereotype.Service;
8+
9+
@Service
10+
public class DirectorDaoHibernateImpl implements DirectorDao {
11+
private SessionFactory sessionFactory;
12+
13+
@Autowired
14+
public DirectorDaoHibernateImpl(SessionFactory sessionFactory) {
15+
this.sessionFactory = sessionFactory;
16+
}
17+
18+
@Override
19+
public void saveDirector(Director director) {
20+
Session session = sessionFactory.openSession();
21+
session.beginTransaction();
22+
session.save(director);
23+
session.getTransaction().commit();
24+
session.close();
25+
}
26+
27+
@Override
28+
public Optional<Director> maybeFromName(String name) {
29+
Session session = sessionFactory.openSession();
30+
session.beginTransaction();
31+
32+
List<Director> directors =
33+
session
34+
.createQuery("from Director as d where d.name = :name")
35+
.setString("name", name).list();
36+
37+
session.getTransaction().commit();
38+
session.close();
39+
40+
return directors.isEmpty() ? Optional.<Director>absent() : Optional.of(directors.get(0));
41+
42+
}
43+
44+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package tw.codedata;
2+
3+
import javax.persistence.*;
4+
5+
@Entity
6+
@Table(name="dvds")
7+
public class Dvd {
8+
@Id
9+
@GeneratedValue(strategy = GenerationType.AUTO)
10+
private Long id;
11+
private String title;
12+
private Integer year;
13+
private Integer duration;
14+
15+
@ManyToOne(cascade=CascadeType.ALL)
16+
@JoinColumn(name="director_id")
17+
private Director director;
18+
19+
public Dvd() {}
20+
21+
public Dvd(String title, Integer year, Integer duration, Director director) {
22+
this.title = title;
23+
this.year = year;
24+
this.duration = duration;
25+
this.director = director;
26+
}
27+
28+
public Long getId() {
29+
return id;
30+
}
31+
32+
public void setId(Long id) {
33+
this.id = id;
34+
}
35+
36+
public String getTitle() {
37+
return title;
38+
}
39+
40+
public void setTitle(String title) {
41+
this.title = title;
42+
}
43+
44+
public Integer getYear() {
45+
return year;
46+
}
47+
48+
public void setYear(Integer year) {
49+
this.year = year;
50+
}
51+
52+
public Integer getDuration() {
53+
return duration;
54+
}
55+
56+
public void setDuration(Integer duration) {
57+
this.duration = duration;
58+
}
59+
60+
public Director getDirector() {
61+
return director;
62+
}
63+
64+
public void setDirector(Director director) {
65+
this.director = director;
66+
}
67+
68+
@Override
69+
public String toString() {
70+
return "Dvd{" +
71+
"id=" + id +
72+
", title=" + title +
73+
", year=" + year +
74+
", duration=" + duration +
75+
", director=" + director +
76+
'}';
77+
}
78+
79+
80+
81+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package tw.codedata;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.stereotype.Controller;
5+
import org.springframework.ui.Model;
6+
import org.springframework.web.bind.annotation.*;
7+
8+
@Controller
9+
public class DvdController {
10+
private DvdLibraryService dvdLibraryService;
11+
12+
@Autowired
13+
public void setDvdLibraryService(DvdLibraryService dvdLibraryService) {
14+
this.dvdLibraryService = dvdLibraryService;
15+
}
16+
17+
public DvdLibraryService getDvdLibraryService() {
18+
return dvdLibraryService;
19+
}
20+
21+
@RequestMapping("list")
22+
public String list(Model m) {
23+
m.addAttribute("dvds", getDvdLibraryService().allDvds());
24+
return "list";
25+
}
26+
27+
@RequestMapping("add")
28+
public String add(
29+
@RequestParam("title") String title,
30+
@RequestParam("year") Integer year,
31+
@RequestParam("duration") Integer duration,
32+
@RequestParam("director") String director,
33+
Model m) {
34+
Dvd dvd = getDvdLibraryService().addDvd(title, year, duration, director);
35+
m.addAttribute("dvd", dvd);
36+
return "success";
37+
}
38+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package tw.codedata;
2+
3+
import java.util.List;
4+
5+
public interface DvdDao {
6+
void saveDvd(Dvd dvd);
7+
List<Dvd> allDvds();
8+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package tw.codedata;
2+
3+
import java.util.List;
4+
import org.hibernate.Session;
5+
import org.hibernate.SessionFactory;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.stereotype.Service;
8+
9+
@Service
10+
public class DvdDaoHibernateImpl implements DvdDao {
11+
private SessionFactory sessionFactory;
12+
13+
@Autowired
14+
public DvdDaoHibernateImpl(SessionFactory sessionFactory) {
15+
this.sessionFactory = sessionFactory;
16+
}
17+
18+
@Override
19+
public void saveDvd(Dvd dvd) {
20+
Session session = sessionFactory.openSession();
21+
session.beginTransaction();
22+
session.save(dvd);
23+
session.getTransaction().commit();
24+
session.close();
25+
}
26+
27+
@Override
28+
public List<Dvd> allDvds() {
29+
Session session = sessionFactory.openSession();
30+
session.beginTransaction();
31+
32+
List<Dvd> dvds = session.createQuery("from Dvd").list();
33+
34+
session.getTransaction().commit();
35+
session.close();
36+
37+
return dvds;
38+
}
39+
40+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package tw.codedata;
2+
3+
import java.util.List;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.stereotype.Service;
6+
7+
@Service
8+
public class DvdLibraryService {
9+
private DirectorDao directorDao;
10+
private DvdDao dvdDao;
11+
12+
@Autowired
13+
public void setDirectorDao(DirectorDao directorDao) {
14+
this.directorDao = directorDao;
15+
}
16+
17+
@Autowired
18+
public void setDvdDao(DvdDao dvdDao) {
19+
this.dvdDao = dvdDao;
20+
}
21+
22+
public DirectorDao getDirectorDao() {
23+
return directorDao;
24+
}
25+
26+
public DvdDao getDvdDao() {
27+
return dvdDao;
28+
}
29+
30+
public List<Dvd> allDvds() {
31+
return getDvdDao().allDvds();
32+
}
33+
34+
public Dvd addDvd(String title, Integer year, Integer duration, String directorName) {
35+
Director director = new Director(directorName);
36+
getDirectorDao().saveDirector(director);
37+
Dvd dvd = new Dvd(title, year, duration, director);
38+
getDvdDao().saveDvd(dvd);
39+
return dvd;
40+
}
41+
}

0 commit comments

Comments
 (0)