Commit c77e2144b8fc1661fa78b8cdc6b93c0a4cdc559b
1 parent
52fa3280
Grundsatzstruktur eingeführt
Showing
8 changed files
with
282 additions
and
1 deletions
src/main/java/net/ziemers/swxercise/db/BaseEntity.java
0 → 100644
1 | +package net.ziemers.swxercise.db; | |
2 | + | |
3 | +import javax.persistence.GeneratedValue; | |
4 | +import javax.persistence.Id; | |
5 | + | |
6 | +public class BaseEntity { | |
7 | + | |
8 | + @Id | |
9 | + @GeneratedValue | |
10 | + protected Long id; | |
11 | + | |
12 | + public Long getId() { | |
13 | + return id; | |
14 | + } | |
15 | + | |
16 | +} | |
0 | 17 | \ No newline at end of file | ... | ... |
src/main/java/net/ziemers/swxercise/db/dao/GenericDao.java
0 → 100644
1 | +package net.ziemers.swxercise.db.dao; | |
2 | + | |
3 | +import java.util.Collection; | |
4 | + | |
5 | +import javax.ejb.Stateless; | |
6 | +import javax.inject.Inject; | |
7 | +import javax.persistence.EntityManager; | |
8 | +import javax.persistence.criteria.CriteriaBuilder; | |
9 | +import javax.persistence.criteria.CriteriaQuery; | |
10 | +// import javax.persistence.criteria.Predicate; | |
11 | +// import javax.persistence.criteria.Root; | |
12 | + | |
13 | +import net.ziemers.swxercise.db.BaseEntity; | |
14 | + | |
15 | +@Stateless | |
16 | +public class GenericDao { | |
17 | + | |
18 | + @Inject | |
19 | + private EntityManager entityManager; | |
20 | + | |
21 | + /** | |
22 | + * Speichert die gegebene Entität. | |
23 | + * | |
24 | + * @see EntityManager#persist(Object) | |
25 | + * @param entity die zu speichernde Entität | |
26 | + * @param <T> Generic für den Entity-Typ. | |
27 | + * @return die Id der persistierten Entität | |
28 | + */ | |
29 | + public <T extends BaseEntity> Long save(final T entity) { | |
30 | + entityManager.persist(entity); | |
31 | + return entity.getId(); | |
32 | + } | |
33 | + | |
34 | + public <T extends BaseEntity> T saveOrUpdate(final T entity) { | |
35 | + return entityManager.merge(entity); | |
36 | + } | |
37 | + | |
38 | + /** | |
39 | + * @param entityType Entity-Typ. | |
40 | + * @param primaryKey Id | |
41 | + * @param <T> Generic für Entity-Typ | |
42 | + * @return die Entität mit gegebener Id oder <code>null</code>, wenn keine gefunden | |
43 | + */ | |
44 | + public <T extends BaseEntity> T findById(Class<T> entityType, Long primaryKey) { | |
45 | + T entity = entityManager.find(entityType, primaryKey); | |
46 | + return entity; | |
47 | + } | |
48 | + | |
49 | + /** | |
50 | + * @param entityType Entity-Typ. | |
51 | + * @param <T> Generic für den Entity-Typ. | |
52 | + * @return alle (aktiven) Entitäten vom gegebenen Typ | |
53 | + */ | |
54 | + public <T extends BaseEntity> Collection<T> findAll(Class<T> entityType) { | |
55 | + CriteriaBuilder cb = entityManager.getCriteriaBuilder(); | |
56 | + CriteriaQuery<T> query = cb.createQuery(entityType); | |
57 | + // Root<T> root = query.from(entityType); | |
58 | + // Predicate activeStatus = cb.equal(root.get(STATUS), Status.ACTIVE); | |
59 | + // query.where(activeStatus); | |
60 | + return entityManager.createQuery(query).getResultList(); | |
61 | + } | |
62 | + | |
63 | +} | ... | ... |
src/main/java/net/ziemers/swxercise/lg/user/Address.java
0 → 100644
1 | +package net.ziemers.swxercise.lg.user; | |
2 | + | |
3 | +import javax.persistence.Entity; | |
4 | + | |
5 | +import net.ziemers.swxercise.db.BaseEntity; | |
6 | + | |
7 | +@Entity | |
8 | +public class Address extends BaseEntity { | |
9 | + | |
10 | + private String street; | |
11 | + | |
12 | + private String zipcode; | |
13 | + | |
14 | + private String city; | |
15 | + | |
16 | + private String country; | |
17 | + | |
18 | + public String getStreet() { | |
19 | + return street; | |
20 | + } | |
21 | + | |
22 | + public void setStreet(String street) { | |
23 | + this.street = street; | |
24 | + } | |
25 | + | |
26 | + public String getZipcode() { | |
27 | + return zipcode; | |
28 | + } | |
29 | + | |
30 | + public void setZipcode(String zipcode) { | |
31 | + this.zipcode = zipcode; | |
32 | + } | |
33 | + | |
34 | + public String getCity() { | |
35 | + return city; | |
36 | + } | |
37 | + | |
38 | + public void setCity(String city) { | |
39 | + this.city = city; | |
40 | + } | |
41 | + | |
42 | + public String getCountry() { | |
43 | + return country; | |
44 | + } | |
45 | + | |
46 | + public void setCountry(String country) { | |
47 | + this.country = country; | |
48 | + } | |
49 | + | |
50 | +} | ... | ... |
src/main/java/net/ziemers/swxercise/lg/user/Profile.java
0 → 100644
1 | +package net.ziemers.swxercise.lg.user; | |
2 | + | |
3 | +import javax.persistence.Entity; | |
4 | + | |
5 | +import net.ziemers.swxercise.db.BaseEntity; | |
6 | + | |
7 | +@Entity | |
8 | +public class Profile extends BaseEntity { | |
9 | + | |
10 | + private String passwordHash; | |
11 | + | |
12 | + private String mailaddress; | |
13 | + | |
14 | + public String getPasswordHash() { | |
15 | + return passwordHash; | |
16 | + } | |
17 | + | |
18 | + public void setPasswordHash(String passwordHash) { | |
19 | + this.passwordHash = passwordHash; | |
20 | + } | |
21 | + | |
22 | + public String getMailaddress() { | |
23 | + return mailaddress; | |
24 | + } | |
25 | + | |
26 | + public void setMailaddress(String mailaddress) { | |
27 | + this.mailaddress = mailaddress; | |
28 | + } | |
29 | + | |
30 | +} | ... | ... |
src/main/java/net/ziemers/swxercise/lg/user/User.java
0 → 100644
1 | +package net.ziemers.swxercise.lg.user; | |
2 | + | |
3 | +import javax.persistence.Entity; | |
4 | + | |
5 | +import net.ziemers.swxercise.db.BaseEntity; | |
6 | + | |
7 | +@Entity | |
8 | +public class User extends BaseEntity { | |
9 | + | |
10 | + private String firstname; | |
11 | + | |
12 | + private String lastname; | |
13 | + | |
14 | + public String getFirstname() { | |
15 | + return firstname; | |
16 | + } | |
17 | + | |
18 | + public void setFirstname(String firstname) { | |
19 | + this.firstname = firstname; | |
20 | + } | |
21 | + | |
22 | + public String getLastname() { | |
23 | + return lastname; | |
24 | + } | |
25 | + | |
26 | + public void setLastname(String lastname) { | |
27 | + this.lastname = lastname; | |
28 | + } | |
29 | + | |
30 | +} | ... | ... |
src/main/java/net/ziemers/swxercise/lg/user/service/UserService.java
0 → 100644
1 | +package net.ziemers.swxercise.lg.user.service; | |
2 | + | |
3 | +import java.util.Collection; | |
4 | + | |
5 | +import javax.ejb.Stateless; | |
6 | +import javax.inject.Inject; | |
7 | + | |
8 | +import net.ziemers.swxercise.db.BaseEntity; | |
9 | +import net.ziemers.swxercise.db.dao.GenericDao; | |
10 | +import net.ziemers.swxercise.lg.user.User; | |
11 | + | |
12 | +@Stateless | |
13 | +public class UserService { | |
14 | + | |
15 | + @Inject | |
16 | + private GenericDao genericDao; | |
17 | + | |
18 | + public User findUser(final Long id) { | |
19 | + return genericDao.findById(User.class, id); | |
20 | + } | |
21 | + | |
22 | + public Collection<User> findAllUsers() { | |
23 | + return genericDao.findAll(User.class); | |
24 | + } | |
25 | + | |
26 | + public <T extends BaseEntity> Long saveUser(final User user) { | |
27 | + return genericDao.save(user); | |
28 | + } | |
29 | + | |
30 | +} | ... | ... |
src/main/java/net/ziemers/swxercise/ui/RestController.java
1 | 1 | package net.ziemers.swxercise.ui; |
2 | 2 | |
3 | +import java.util.Collection; | |
4 | + | |
3 | 5 | import javax.annotation.PostConstruct; |
4 | 6 | import javax.enterprise.context.ApplicationScoped; |
7 | +import javax.inject.Inject; | |
5 | 8 | import javax.ws.rs.GET; |
6 | 9 | import javax.ws.rs.Path; |
7 | 10 | import javax.ws.rs.PathParam; |
8 | 11 | import javax.ws.rs.Produces; |
9 | 12 | import javax.ws.rs.core.MediaType; |
10 | 13 | |
14 | +import net.ziemers.swxercise.lg.user.User; | |
15 | +import net.ziemers.swxercise.lg.user.service.UserService; | |
11 | 16 | import org.slf4j.Logger; |
12 | 17 | import org.slf4j.LoggerFactory; |
13 | 18 | |
... | ... | @@ -15,10 +20,13 @@ import org.slf4j.LoggerFactory; |
15 | 20 | @Path(RestController.webContextPath) |
16 | 21 | public class RestController { |
17 | 22 | |
18 | - static final String webContextPath = "/misc"; | |
23 | + static final String webContextPath = "/user"; | |
19 | 24 | |
20 | 25 | private Logger logger; |
21 | 26 | |
27 | + @Inject | |
28 | + private UserService userService; | |
29 | + | |
22 | 30 | @PostConstruct |
23 | 31 | public void init() { |
24 | 32 | logger = LoggerFactory.getLogger(RestController.class); |
... | ... | @@ -39,4 +47,11 @@ public class RestController { |
39 | 47 | return String.format("Hello %s", name); |
40 | 48 | } |
41 | 49 | |
50 | + @GET | |
51 | + @Path("/get") | |
52 | + @Produces(MediaType.APPLICATION_JSON) | |
53 | + public Collection<User> getAllUsers() { | |
54 | + return userService.findAllUsers(); | |
55 | + } | |
56 | + | |
42 | 57 | } | ... | ... |
src/main/resources/META-INF/persistence.xml
0 → 100644
1 | +<persistence version="2.0" | |
2 | + xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
3 | + xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"> | |
4 | + <persistence-unit name="swXercisePU" transaction-type="JTA"> | |
5 | + <jta-data-source>java:jboss/datasources/swXerciseDS</jta-data-source> | |
6 | + <properties> | |
7 | + <!-- property name="hibernate.hbm2ddl.auto" value="create-drop" / --> | |
8 | + <property name="hibernate.hbm2ddl.auto" value="update" /> | |
9 | + </properties> | |
10 | + </persistence-unit> | |
11 | + <persistence-unit name="swXerciseTestPU" | |
12 | + transaction-type="RESOURCE_LOCAL"> | |
13 | + <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> | |
14 | + <exclude-unlisted-classes>false</exclude-unlisted-classes> | |
15 | + | |
16 | + <properties> | |
17 | + <!-- Property for WildFly to ignore this --> | |
18 | + <property name="jboss.as.jpa.managed" value="false" /> | |
19 | + <!-- Properties for Hibernate --> | |
20 | + <property name="javax.persistence.jdbc.url" | |
21 | + value="jdbc:mariadb://${abisnext.test.host}:${abisnext.test.port}/${abisnext.test.schema}" /> | |
22 | + <property name="javax.persistence.jdbc.user" value="${abisnext.test.user}" /> | |
23 | + <property name="javax.persistence.jdbc.password" value="${abisnext.test.password}" /> | |
24 | + <property name="javax.persistence.jdbc.driver" value="org.mariadb.jdbc.Driver" /> | |
25 | + <property name="hibernate.show_sql" value="false" /> | |
26 | + <property name="hibernate.format_sql" value="false" /> | |
27 | + <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" /> | |
28 | + <!--<property name="hibernate.hbm2ddl.auto" value="create-drop" /> --> | |
29 | + <property name="hibernate.connection.useUnicode" value="true" /> | |
30 | + <property name="hibernate.connection.characterEncoding" | |
31 | + value="UTF-8" /> | |
32 | + <property name="hibernate.connection.provider_class" | |
33 | + value="org.hibernate.connection.C3P0ConnectionProvider" /> | |
34 | + | |
35 | + <property name="hibernate.c3p0.min_size" value="2" /> | |
36 | + <property name="hibernate.c3p0.max_size" value="100" /> | |
37 | + <property name="hibernate.c3p0.timeout" value="10" /> | |
38 | + <property name="hibernate.c3p0.max_statements" value="100" /> | |
39 | + <property name="hibernate.c3p0.idle_test_period" value="30" /> | |
40 | + | |
41 | + <property name="hibernate.temp.use_jdbc_metadata_defaults" | |
42 | + value="false" /> | |
43 | + <property name="hibernate.ejb.entitymanager_factory_name" | |
44 | + value="abisnext_test" /> | |
45 | + </properties> | |
46 | + </persistence-unit> | |
47 | +</persistence> | |
0 | 48 | \ No newline at end of file | ... | ... |