Skip to contentMethod: static {...}
1: /*
2: * *************************************************************************************************************************************************************
3: *
4: * TheseFoolishThings: Miscellaneous utilities
5: * http://tidalwave.it/projects/thesefoolishthings
6: *
7: * Copyright (C) 2009 - 2024 by Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *************************************************************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
12: * You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
17: * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
18: *
19: * *************************************************************************************************************************************************************
20: *
21: * git clone https://bitbucket.org/tidalwave/thesefoolishthings-src
22: * git clone https://github.com/tidalwave-it/thesefoolishthings-src
23: *
24: * *************************************************************************************************************************************************************
25: */
26: package it.tidalwave.thesefoolishthings.examples.dci.persistable.jpa;
27:
28: import javax.annotation.Nonnull;
29: import java.util.List;
30: import java.util.Optional;
31: import jakarta.transaction.Transactional;
32: import it.tidalwave.util.As;
33: import it.tidalwave.util.AsExtensions;
34: import it.tidalwave.util.Id;
35: import it.tidalwave.thesefoolishthings.examples.dci.persistable.jpa.role.Findable;
36: import it.tidalwave.thesefoolishthings.examples.person.Person;
37: import lombok.experimental.ExtensionMethod;
38: import static it.tidalwave.role.io.Persistable._Persistable_;
39:
40: /***************************************************************************************************************************************************************
41: *
42: * @author Fabrizio Giudici
43: *
44: **************************************************************************************************************************************************************/
45: @ExtensionMethod(AsExtensions.class)
46: public class TransactionalProcessor
47: {
48: private static final As.Type<Findable<Person>> _FindableOfPersons_ = As.type(Findable.class);
49:
50: /**
51: * Inserts a bunch of people into the database.
52: * @param persons the entities to insert
53: * */
54: @Transactional
55: public void persistPeople (@Nonnull final Iterable<Person> persons)
56: {
57: persons.forEach(person -> person.as(_Persistable_).persist());
58: }
59:
60: // @Transactional
61: // public void removePeople (@Nonnull final Iterable<Person> persons)
62: // throws Exception
63: // {
64: // persons.forEach(_r(person -> person.as(_Removable_).remove()));
65: // }
66:
67: /** {@return The persons in the database. */
68: @Transactional @Nonnull
69: public List<Person> retrievePeople()
70: {
71: return Person.prototype().as(_FindableOfPersons_).findAll();
72: }
73:
74: /**
75: * {@return a specific person} in the database
76: * @param id the id of the person to retrieve
77: */
78: @Transactional @Nonnull
79: public Optional<Person> retrievePerson (@Nonnull final Id id)
80: {
81: return Person.prototype().as(_FindableOfPersons_).findById(id);
82: }
83: }