1616
1717package software .xdev .spring .data .eclipse .store .demo .complex ;
1818
19- import java .time .LocalDate ;
20-
21- import org .slf4j .Logger ;
22- import org .slf4j .LoggerFactory ;
2319import org .springframework .beans .factory .annotation .Autowired ;
2420import org .springframework .boot .CommandLineRunner ;
2521import org .springframework .boot .SpringApplication ;
2622import org .springframework .boot .autoconfigure .SpringBootApplication ;
2723import org .springframework .context .ConfigurableApplicationContext ;
28- import org .springframework .data .domain .Pageable ;
29-
30- import software .xdev .spring .data .eclipse .store .demo .complex .owner .Owner ;
31- import software .xdev .spring .data .eclipse .store .demo .complex .owner .OwnerRepository ;
32- import software .xdev .spring .data .eclipse .store .demo .complex .owner .Pet ;
33- import software .xdev .spring .data .eclipse .store .demo .complex .owner .PetType ;
34- import software .xdev .spring .data .eclipse .store .demo .complex .owner .Visit ;
3524
3625
3726@ SpringBootApplication
3827public class ComplexDemoApplication implements CommandLineRunner
3928{
40- private static final Logger LOG = LoggerFactory .getLogger (ComplexDemoApplication .class );
41- private final OwnerRepository ownerRepository ;
29+ private final OwnerService ownerService ;
4230 private final VetService vetService ;
4331
4432 @ Autowired
4533 public ComplexDemoApplication (
46- final OwnerRepository ownerRepository ,
34+ final OwnerService ownerService ,
4735 final VetService vetService
4836 )
4937 {
50- this .ownerRepository = ownerRepository ;
38+ this .ownerService = ownerService ;
5139 this .vetService = vetService ;
5240 }
5341
@@ -60,89 +48,31 @@ public static void main(final String[] args)
6048 @ Override
6149 public void run (final String ... args )
6250 {
63- this .transactionalVetCalls ();
64-
51+ this .vetCalls ();
6552 this .ownerCalls ();
6653 }
6754
55+ /**
56+ * Some calls are transactional (delete and create) and some are not (log).
57+ */
6858 private void ownerCalls ()
6959 {
70- LOG .info ("----Owner-BeforeDeleteAll----" );
71- this .ownerRepository .findAll (Pageable .unpaged ()).forEach (i -> LOG .info (i .toString ()));
72- this .ownerRepository .deleteAll ();
73-
74- LOG .info ("----Owner-AfterDeleteAll----" );
75- this .ownerRepository .findAll (Pageable .unpaged ()).forEach (i -> LOG .info (i .toString ()));
76-
77- final Owner owner = createOwner ();
78- this .ownerRepository .save (owner );
79-
80- LOG .info ("----Owner-AfterSave----" );
81- this .ownerRepository .findAll (Pageable .unpaged ()).forEach (i -> LOG .info (i .toString ()));
82-
83- final Visit visit = createVisit ();
84- owner .addVisit ("Peter" , visit );
85- this .ownerRepository .save (owner );
86-
87- LOG .info ("----Owner-AfterVisit----" );
88- this .ownerRepository
89- .findByLastName ("Nicks" , Pageable .unpaged ())
90- .forEach (i ->
91- {
92- LOG .info (i .toString ());
93- i .getPets ().forEach (p -> {
94- LOG .info (p .toString ());
95- p .getVisits ().forEach (v -> LOG .info (v .toString ()));
96- }
97- );
98- }
99- );
100-
101- LOG .info ("----Owner-Lazy Pet loading----" );
102- this .ownerRepository .findAll ().forEach (
103- o -> o .getPets ().forEach (
104- pet -> LOG .info (String .format (
105- "Pet %s has owner %s %s" ,
106- pet .getName (),
107- o .getFirstName (),
108- o .getLastName ()))
109- )
110- );
60+ this .ownerService .logOwners ();
61+ this .ownerService .deleteAll ();
62+ this .ownerService .logOwners ();
63+ this .ownerService .createNewOwnerAndVisit ();
64+ this .ownerService .logOwnersAndVisits ();
11165 }
11266
11367 /**
11468 * Each of these calls are one transaction.
11569 */
116- private void transactionalVetCalls ()
70+ private void vetCalls ()
11771 {
11872 this .vetService .logVetEntries ();
11973 this .vetService .deleteAll ();
12074 this .vetService .logVetEntries ();
12175 this .vetService .saveNewEntries ();
12276 this .vetService .logVetEntries ();
12377 }
124-
125- private static Visit createVisit ()
126- {
127- final Visit visit = new Visit ();
128- visit .setDate (LocalDate .now ());
129- visit .setDescription ("Peter got his first parvovirus vaccine" );
130- return visit ;
131- }
132-
133- @ SuppressWarnings ("checkstyle:MagicNumber" )
134- private static Owner createOwner ()
135- {
136- final Owner owner = new Owner ();
137- owner .setFirstName ("Stevie" );
138- owner .setLastName ("Nicks" );
139- final Pet pet = new Pet ();
140- pet .setBirthDate (LocalDate .now ().minusWeeks (6 ));
141- pet .setName ("Peter" );
142- final PetType petType = new PetType ();
143- petType .setName ("Dog" );
144- pet .setType (petType );
145- owner .addPet (pet );
146- return owner ;
147- }
14878}
0 commit comments