Skip to content

Commit dfa6eb9

Browse files
committed
Added first project version.
Angular paginated list with Java EE 7 Rest backend.
0 parents  commit dfa6eb9

18 files changed

Lines changed: 10189 additions & 0 deletions

File tree

pom.xml

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>com.cortez.samples</groupId>
6+
<artifactId>javaee7-angular</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
<packaging>war</packaging>
9+
10+
<name>nextprev</name>
11+
12+
<prerequisites>
13+
<maven>3.1.0</maven>
14+
</prerequisites>
15+
16+
<properties>
17+
<!-- Project -->
18+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
19+
<java.version>1.7</java.version>
20+
21+
<!-- Plugins -->
22+
<version.plugin.compiler>3.1</version.plugin.compiler>
23+
<version.plugin.enforcer>1.3.1</version.plugin.enforcer>
24+
25+
<!-- Dependencies -->
26+
<wildfly.version>8.0.0.CR1</wildfly.version>
27+
</properties>
28+
29+
<build>
30+
<plugins>
31+
<plugin>
32+
<groupId>org.apache.maven.plugins</groupId>
33+
<artifactId>maven-compiler-plugin</artifactId>
34+
<version>${version.plugin.compiler}</version>
35+
<configuration>
36+
<source>${java.version}</source>
37+
<target>${java.version}</target>
38+
<debug>true</debug>
39+
<optimize>true</optimize>
40+
</configuration>
41+
</plugin>
42+
43+
<plugin>
44+
<groupId>org.apache.maven.plugins</groupId>
45+
<artifactId>maven-enforcer-plugin</artifactId>
46+
<version>${version.plugin.enforcer}</version>
47+
<executions>
48+
<execution>
49+
<id>enforce-maven</id>
50+
<goals>
51+
<goal>enforce</goal>
52+
</goals>
53+
<configuration>
54+
<rules>
55+
<requireJavaVersion>
56+
<version>[${java.version},1.8)</version>
57+
</requireJavaVersion>
58+
</rules>
59+
</configuration>
60+
</execution>
61+
</executions>
62+
</plugin>
63+
64+
<plugin>
65+
<groupId>org.apache.maven.plugins</groupId>
66+
<artifactId>maven-war-plugin</artifactId>
67+
<version>2.4</version>
68+
<configuration>
69+
<failOnMissingWebXml>false</failOnMissingWebXml>
70+
</configuration>
71+
</plugin>
72+
73+
<plugin>
74+
<groupId>org.zeroturnaround</groupId>
75+
<artifactId>jrebel-maven-plugin</artifactId>
76+
<executions>
77+
<execution>
78+
<id>generate-rebel-xml</id>
79+
<phase>process-resources</phase>
80+
<goals>
81+
<goal>generate</goal>
82+
</goals>
83+
</execution>
84+
</executions>
85+
</plugin>
86+
</plugins>
87+
</build>
88+
89+
<dependencies>
90+
<dependency>
91+
<groupId>javax</groupId>
92+
<artifactId>javaee-api</artifactId>
93+
<version>7.0</version>
94+
<scope>provided</scope>
95+
</dependency>
96+
</dependencies>
97+
</project>
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.cortez.samples.javaee7angular.data;
2+
3+
import javax.persistence.Entity;
4+
import javax.persistence.Id;
5+
6+
/**
7+
* @author Roberto Cortez
8+
*/
9+
@Entity
10+
public class Person {
11+
@Id
12+
private Long id;
13+
14+
private String name;
15+
16+
private String description;
17+
18+
public Long getId() {
19+
return id;
20+
}
21+
22+
public void setId(Long id) {
23+
this.id = id;
24+
}
25+
26+
public String getName() {
27+
return name;
28+
}
29+
30+
public void setName(String name) {
31+
this.name = name;
32+
}
33+
34+
public String getDescription() {
35+
return description;
36+
}
37+
38+
public void setDescription(String description) {
39+
this.description = description;
40+
}
41+
42+
@Override
43+
public boolean equals(Object o) {
44+
if (this == o) { return true; }
45+
if (o == null || getClass() != o.getClass()) { return false; }
46+
47+
Person person = (Person) o;
48+
49+
return id.equals(person.id);
50+
}
51+
52+
@Override
53+
public int hashCode() {
54+
return id.hashCode();
55+
}
56+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.cortez.samples.javaee7angular.pagination;
2+
3+
import java.util.List;
4+
5+
/**
6+
* @author Roberto Cortez
7+
*/
8+
public class PaginatedListWrapper<T> {
9+
private Integer currentPage;
10+
private Integer pageSize;
11+
private Integer totalResults;
12+
13+
private String sortFields;
14+
private String sortDirections;
15+
private List<T> list;
16+
17+
public Integer getCurrentPage() {
18+
return currentPage;
19+
}
20+
21+
public void setCurrentPage(Integer currentPage) {
22+
this.currentPage = currentPage;
23+
}
24+
25+
public Integer getPageSize() {
26+
return pageSize;
27+
}
28+
29+
public void setPageSize(Integer pageSize) {
30+
this.pageSize = pageSize;
31+
}
32+
33+
public Integer getTotalResults() {
34+
return totalResults;
35+
}
36+
37+
public void setTotalResults(Integer totalResults) {
38+
this.totalResults = totalResults;
39+
}
40+
41+
public String getSortFields() {
42+
return sortFields;
43+
}
44+
45+
public void setSortFields(String sortFields) {
46+
this.sortFields = sortFields;
47+
}
48+
49+
public String getSortDirections() {
50+
return sortDirections;
51+
}
52+
53+
public void setSortDirections(String sortDirections) {
54+
this.sortDirections = sortDirections;
55+
}
56+
57+
public List<T> getList() {
58+
return list;
59+
}
60+
61+
public void setList(List<T> list) {
62+
this.list = list;
63+
}
64+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.cortez.samples.javaee7angular.rest;
2+
3+
import javax.ws.rs.ApplicationPath;
4+
import javax.ws.rs.core.Application;
5+
6+
/**
7+
* @author Roberto Cortez
8+
*/
9+
@ApplicationPath("/resources")
10+
public class PersonApplication extends Application {
11+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.cortez.samples.javaee7angular.rest;
2+
3+
import com.cortez.samples.javaee7angular.data.Person;
4+
import com.cortez.samples.javaee7angular.pagination.PaginatedListWrapper;
5+
6+
import javax.ejb.Stateless;
7+
import javax.persistence.EntityManager;
8+
import javax.persistence.PersistenceContext;
9+
import javax.persistence.Query;
10+
import javax.ws.rs.*;
11+
import javax.ws.rs.core.MediaType;
12+
import java.util.List;
13+
14+
/**
15+
* @author Roberto Cortez
16+
*/
17+
@Stateless
18+
@Path("persons")
19+
public class PersonResource {
20+
@PersistenceContext
21+
private EntityManager entityManager;
22+
23+
private Integer countPersons() {
24+
Query query = entityManager.createQuery("SELECT COUNT(p.id) FROM Person p");
25+
return ((Long) query.getSingleResult()).intValue();
26+
}
27+
28+
@SuppressWarnings("unchecked")
29+
private List<Person> findPersons(int startPosition, int maxResults, String sortFields, String sortDirections) {
30+
Query query = entityManager.createQuery("SELECT p FROM Person p ORDER BY " + sortFields + " " + sortDirections);
31+
query.setFirstResult(startPosition);
32+
query.setMaxResults(maxResults);
33+
return query.getResultList();
34+
}
35+
36+
public PaginatedListWrapper<Person> findPersons(PaginatedListWrapper<Person> wrapper) {
37+
wrapper.setTotalResults(countPersons());
38+
int start = (wrapper.getCurrentPage() - 1) * wrapper.getPageSize();
39+
wrapper.setList(findPersons(start,
40+
wrapper.getPageSize(),
41+
wrapper.getSortFields(),
42+
wrapper.getSortDirections()));
43+
return wrapper;
44+
}
45+
46+
@GET
47+
@Produces(MediaType.APPLICATION_JSON)
48+
public PaginatedListWrapper<Person> listPersons(@DefaultValue("1")
49+
@QueryParam("page")
50+
Integer page,
51+
@DefaultValue("id")
52+
@QueryParam("sortFields")
53+
String sortFields,
54+
@DefaultValue("asc")
55+
@QueryParam("sortDirections")
56+
String sortDirections) {
57+
PaginatedListWrapper<Person> paginatedListWrapper = new PaginatedListWrapper<>();
58+
paginatedListWrapper.setCurrentPage(page);
59+
paginatedListWrapper.setSortFields(sortFields);
60+
paginatedListWrapper.setSortDirections(sortDirections);
61+
paginatedListWrapper.setPageSize(5);
62+
return findPersons(paginatedListWrapper);
63+
}
64+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<persistence version="2.1"
3+
xmlns="http://xmlns.jcp.org/xml/ns/persistence"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
6+
<persistence-unit name="myPU" transaction-type="JTA">
7+
<properties>
8+
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
9+
<property name="javax.persistence.schema-generation.create-source" value="script"/>
10+
<property name="javax.persistence.schema-generation.drop-source" value="script"/>
11+
<property name="javax.persistence.schema-generation.create-script-source" value="sql/create.sql"/>
12+
<property name="javax.persistence.schema-generation.drop-script-source" value="sql/drop.sql"/>
13+
<property name="javax.persistence.sql-load-script-source" value="sql/load.sql"/>
14+
</properties>
15+
</persistence-unit>
16+
</persistence>

src/main/resources/sql/create.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CREATE TABLE PERSON("ID" INTEGER not null primary key, "NAME" VARCHAR(50), "DESCRIPTION" VARCHAR(100))

src/main/resources/sql/drop.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DROP TABLE PERSON;

src/main/resources/sql/load.sql

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
INSERT INTO PERSON("ID", "NAME", "DESCRIPTION") VALUES (1, 'Uzumaki Naruto', 'Konoha')
2+
INSERT INTO PERSON("ID", "NAME", "DESCRIPTION") VALUES (2, 'Hatake Kakashi', 'Konoha')
3+
INSERT INTO PERSON("ID", "NAME", "DESCRIPTION") VALUES (3, 'Haruno Sakura', 'Konoha')
4+
INSERT INTO PERSON("ID", "NAME", "DESCRIPTION") VALUES (4, 'Uchiha Sasuke', 'Missing-nin')
5+
INSERT INTO PERSON("ID", "NAME", "DESCRIPTION") VALUES (5, 'Gaara', 'Sunagakure')
6+
INSERT INTO PERSON("ID", "NAME", "DESCRIPTION") VALUES (6, 'Killer Bee', 'Kumogakure')
7+
INSERT INTO PERSON("ID", "NAME", "DESCRIPTION") VALUES (7, 'Jiraya', 'Konoha')
8+
INSERT INTO PERSON("ID", "NAME", "DESCRIPTION") VALUES (8, 'Namikaze Minato', 'Konoha')
9+
INSERT INTO PERSON("ID", "NAME", "DESCRIPTION") VALUES (9, 'Uchiha Madara', 'Missing-nin')
10+
INSERT INTO PERSON("ID", "NAME", "DESCRIPTION") VALUES (10, 'Senju Hashirama', 'Konoha')
11+
INSERT INTO PERSON("ID", "NAME", "DESCRIPTION") VALUES (11, 'Might Guy', 'Konoha')
12+
INSERT INTO PERSON("ID", "NAME", "DESCRIPTION") VALUES (12, 'Hyuga Neji', 'Konoha')
13+
INSERT INTO PERSON("ID", "NAME", "DESCRIPTION") VALUES (13, 'Rock Lee', 'Konoha')
14+
INSERT INTO PERSON("ID", "NAME", "DESCRIPTION") VALUES (14, 'Uchiha Obito', 'Missing-nin')
15+
INSERT INTO PERSON("ID", "NAME", "DESCRIPTION") VALUES (15, 'Kurama', 'Tailed Beast')
16+
INSERT INTO PERSON("ID", "NAME", "DESCRIPTION") VALUES (16, 'Uzumaki Kushina', 'Konoha')
17+
INSERT INTO PERSON("ID", "NAME", "DESCRIPTION") VALUES (17, 'Nara Shikamaru', 'Konoha')
18+
INSERT INTO PERSON("ID", "NAME", "DESCRIPTION") VALUES (18, 'Sarutobi Hiruzen', 'Konoha')
19+
INSERT INTO PERSON("ID", "NAME", "DESCRIPTION") VALUES (19, 'Tsunade', 'Konoha')
20+
INSERT INTO PERSON("ID", "NAME", "DESCRIPTION") VALUES (20, 'Orochimaru', 'Missing-nin')
21+
INSERT INTO PERSON("ID", "NAME", "DESCRIPTION") VALUES (21, 'Uchicha Itachi', 'Missing-nin')

src/main/webapp/css/style.css

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
body {
2+
width: 50%;
3+
margin-left: auto;
4+
margin-right: auto;
5+
}
6+
7+
.grid {
8+
text-align: center;
9+
}
10+
11+
.gridStyle {
12+
border: 1px solid rgb(212, 212, 212);
13+
height: 180px;
14+
}

0 commit comments

Comments
 (0)