Skip to content

Commit 8062092

Browse files
committed
Added code comments.
1 parent 27ff01d commit 8062092

File tree

16 files changed

+132
-2
lines changed

16 files changed

+132
-2
lines changed

service-layer/src/main/java/com/iluwatar/app/App.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.iluwatar.app;
22

3+
import java.util.List;
4+
35
import com.iluwatar.magic.MagicService;
46
import com.iluwatar.magic.MagicServiceImpl;
57
import com.iluwatar.spell.Spell;
@@ -14,18 +16,34 @@
1416

1517

1618
/**
19+
* Service layer defines an application's boundary with a layer of services that establishes
20+
* a set of available operations and coordinates the application's response in each operation.
1721
*
22+
* Enterprise applications typically require different kinds of interfaces to the data
23+
* they store and the logic they implement: data loaders, user interfaces, integration gateways,
24+
* and others. Despite their different purposes, these interfaces often need common interactions
25+
* with the application to access and manipulate its data and invoke its business logic. The
26+
* interactions may be complex, involving transactions across multiple resources and the
27+
* coordination of several responses to an action. Encoding the logic of the interactions
28+
* separately in each interface causes a lot of duplication.
1829
*
30+
* The example application demonstrates interactions between a client (App) and a service
31+
* (MagicService). The service is implemented with 3-layer architecture (entity, dao, service).
32+
* For persistence the example uses in-memory H2 database which is populated on each application
33+
* startup.
1934
*
2035
*/
2136
public class App {
2237

2338
public static void main( String[] args ) {
39+
// populate the in-memory database
2440
initData();
41+
// query the data using the service
2542
queryData();
2643
}
2744

2845
public static void initData() {
46+
// spells
2947
Spell spell1 = new Spell("Ice dart");
3048
Spell spell2 = new Spell("Invisibility");
3149
Spell spell3 = new Spell("Stun bolt");
@@ -62,6 +80,7 @@ public static void initData() {
6280
spellDao.persist(spell16);
6381
spellDao.persist(spell17);
6482

83+
// spellbooks
6584
SpellbookDao spellbookDao = new SpellbookDaoImpl();
6685
Spellbook spellbook1 = new Spellbook("Book of Orgymon");
6786
spellbookDao.persist(spellbook1);
@@ -102,6 +121,7 @@ public static void initData() {
102121
spellbook7.addSpell(spell17);
103122
spellbookDao.merge(spellbook7);
104123

124+
// wizards
105125
WizardDao wizardDao = new WizardDaoImpl();
106126
Wizard wizard1 = new Wizard("Aderlard Boud");
107127
wizardDao.persist(wizard1);
@@ -138,5 +158,15 @@ public static void queryData() {
138158
for (Spell s: service.findAllSpells()) {
139159
System.out.println(s.getName());
140160
}
161+
System.out.println("Find wizards with spellbook 'Book of Idores'");
162+
List<Wizard> wizardsWithSpellbook = service.findWizardsWithSpellbook("Book of Idores");
163+
for (Wizard w: wizardsWithSpellbook) {
164+
System.out.println(String.format("%s has 'Book of Idores'", w.getName()));
165+
}
166+
System.out.println("Find wizards with spell 'Fireball'");
167+
List<Wizard> wizardsWithSpell = service.findWizardsWithSpell("Fireball");
168+
for (Wizard w: wizardsWithSpell) {
169+
System.out.println(String.format("%s has 'Fireball'", w.getName()));
170+
}
141171
}
142172
}

service-layer/src/main/java/com/iluwatar/common/BaseEntity.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
import javax.persistence.MappedSuperclass;
66
import javax.persistence.Version;
77

8+
/**
9+
*
10+
* Base class for entities.
11+
*
12+
*/
813
@MappedSuperclass
914
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
1015
public class BaseEntity {

service-layer/src/main/java/com/iluwatar/common/Dao.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
import java.util.List;
44

5+
/**
6+
*
7+
* Dao interface.
8+
*
9+
* @param <E>
10+
*
11+
*/
512
public interface Dao<E extends BaseEntity> {
613

714
E find(Long id);

service-layer/src/main/java/com/iluwatar/common/DaoBaseImpl.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@
1010

1111
import com.iluwatar.hibernate.HibernateUtil;
1212

13+
/**
14+
*
15+
* Base class for Dao implementations.
16+
*
17+
* @param <E>
18+
*
19+
*/
1320
public abstract class DaoBaseImpl<E extends BaseEntity> implements Dao<E> {
1421

1522
@SuppressWarnings("unchecked")

service-layer/src/main/java/com/iluwatar/hibernate/HibernateUtil.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
import com.iluwatar.spellbook.Spellbook;
88
import com.iluwatar.wizard.Wizard;
99

10+
/**
11+
*
12+
* Produces the Hibernate SessionFactory.
13+
*
14+
*/
1015
public class HibernateUtil {
1116

1217
private static final SessionFactory sessionFactory;

service-layer/src/main/java/com/iluwatar/magic/MagicService.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,20 @@
77
import com.iluwatar.wizard.Wizard;
88

99

10+
/**
11+
*
12+
* Service interface.
13+
*
14+
*/
1015
public interface MagicService {
1116

1217
List<Wizard> findAllWizards();
1318

1419
List<Spellbook> findAllSpellbooks();
1520

1621
List<Spell> findAllSpells();
17-
22+
23+
List<Wizard> findWizardsWithSpellbook(String name);
24+
25+
List<Wizard> findWizardsWithSpell(String name);
1826
}

service-layer/src/main/java/com/iluwatar/magic/MagicServiceImpl.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.iluwatar.magic;
22

3+
import java.util.ArrayList;
34
import java.util.List;
45

56
import com.iluwatar.spell.Spell;
@@ -9,6 +10,11 @@
910
import com.iluwatar.wizard.Wizard;
1011
import com.iluwatar.wizard.WizardDao;
1112

13+
/**
14+
*
15+
* Service implementation.
16+
*
17+
*/
1218
public class MagicServiceImpl implements MagicService {
1319

1420
private WizardDao wizardDao;
@@ -35,4 +41,17 @@ public List<Spellbook> findAllSpellbooks() {
3541
public List<Spell> findAllSpells() {
3642
return spellDao.findAll();
3743
}
44+
45+
@Override
46+
public List<Wizard> findWizardsWithSpellbook(String name) {
47+
Spellbook spellbook = spellbookDao.findByName(name);
48+
return new ArrayList<Wizard>(spellbook.getWizards());
49+
}
50+
51+
@Override
52+
public List<Wizard> findWizardsWithSpell(String name) {
53+
Spell spell = spellDao.findByName(name);
54+
Spellbook spellbook = spell.getSpellbook();
55+
return new ArrayList<Wizard>(spellbook.getWizards());
56+
}
3857
}

service-layer/src/main/java/com/iluwatar/spell/Spell.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
import com.iluwatar.common.BaseEntity;
1212
import com.iluwatar.spellbook.Spellbook;
1313

14+
/**
15+
*
16+
* Spell entity.
17+
*
18+
*/
1419
@Entity
1520
@Table(name="SPELL")
1621
public class Spell extends BaseEntity {

service-layer/src/main/java/com/iluwatar/spell/SpellDao.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
import com.iluwatar.common.Dao;
44

5+
/**
6+
*
7+
* SpellDao interface.
8+
*
9+
*/
510
public interface SpellDao extends Dao<Spell> {
611

712
Spell findByName(String name);

service-layer/src/main/java/com/iluwatar/spell/SpellDaoImpl.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77

88
import com.iluwatar.common.DaoBaseImpl;
99

10+
/**
11+
*
12+
* SpellDao implementation.
13+
*
14+
*/
1015
public class SpellDaoImpl extends DaoBaseImpl<Spell> implements SpellDao {
1116

1217
@Override
@@ -19,6 +24,7 @@ public Spell findByName(String name) {
1924
Criteria criteria = session.createCriteria(persistentClass);
2025
criteria.add(Expression.eq("name", name));
2126
result = (Spell) criteria.uniqueResult();
27+
result.getSpellbook().getWizards().size();
2228
tx.commit();
2329
}
2430
catch (Exception e) {

0 commit comments

Comments
 (0)