From aaf094af7ecf98e07e31a231fdab871b25961bd1 Mon Sep 17 00:00:00 2001 From: Ravi Srinivasan <rsriniva@redhat.com> Date: Mon, 10 Sep 2018 13:56:10 +0200 Subject: [PATCH] Chap 4 labs for edX --- labs/create-queries/src/main/webapp/WEB-INF/templates/default.xhtml | 34 labs/create-queries/src/main/java/com/redhat/training/model/Person.java | 58 + solutions/persist-entity/src/main/java/com/redhat/training/services/PersonService.java | 62 + solutions/create-queries/src/main/java/com/redhat/training/model/Person.java | 60 + labs/create-queries/src/main/resources/META-INF/persistence.xml | 15 solutions/create-queries/.gitignore | 5 labs/create-queries/src/main/webapp/WEB-INF/beans.xml | 25 solutions/persist-entity/src/main/webapp/resources/css/screen.css | 79 + labs/persist-entity/src/main/webapp/viewPersons.xhtml | 32 solutions/create-queries/src/main/java/com/redhat/training/servlet/PingServlet.java | 60 + solutions/persist-entity/src/main/webapp/resources/images/redhat.png | 0 labs/persist-entity/src/main/java/com/redhat/training/servlet/PingServlet.java | 60 + labs/create-queries/src/main/webapp/viewPerson.xhtml | 43 labs/persist-entity/README.md | 12 labs/create-queries/src/main/webapp/WEB-INF/faces-config.xml | 30 labs/persist-entity/src/main/java/com/redhat/training/services/PersonService.java | 55 + solutions/persist-entity/src/main/webapp/viewPersons.xhtml | 32 labs/create-queries/src/main/java/com/redhat/training/ui/Hello.java | 60 + solutions/create-queries/src/main/webapp/WEB-INF/faces-config.xml | 30 solutions/persist-entity/.gitignore | 5 solutions/create-queries/src/main/webapp/resources/images/redhat.png | 0 labs/persist-entity/src/main/java/com/redhat/training/ui/Hello.java | 94 + labs/persist-entity/src/main/resources/META-INF/persistence.xml | 15 solutions/persist-entity/src/main/java/com/redhat/training/model/Person.java | 66 + solutions/create-queries/README.md | 12 labs/create-queries/mysql/hellodb.sql | 14 solutions/create-queries/mysql/hellodb.sql | 14 solutions/persist-entity/pom.xml | 148 +++ labs/persist-entity/src/main/webapp/resources/css/screen.css | 79 + solutions/create-queries/src/main/webapp/viewPerson.xhtml | 43 labs/create-queries/src/main/java/com/redhat/training/services/PersonService.java | 24 solutions/create-queries/src/main/webapp/viewPersons.xhtml | 32 solutions/persist-entity/src/main/java/com/redhat/training/ui/Hello.java | 96 ++ labs/create-queries/src/main/webapp/index.html | 23 solutions/persist-entity/src/main/java/com/redhat/training/servlet/PingServlet.java | 60 + labs/create-queries/src/main/java/com/redhat/training/servlet/PingServlet.java | 60 + solutions/create-queries/pom.xml | 148 +++ labs/persist-entity/src/main/webapp/resources/images/redhat.png | 0 solutions/persist-entity/src/main/resources/META-INF/persistence.xml | 15 labs/create-queries/src/main/webapp/index.xhtml | 26 labs/create-queries/README.md | 12 labs/create-queries/src/main/webapp/resources/images/redhat.png | 0 labs/persist-entity/src/main/webapp/viewPerson.xhtml | 33 labs/create-queries/pom.xml | 148 +++ solutions/create-queries/src/main/webapp/WEB-INF/templates/default.xhtml | 34 labs/persist-entity/src/main/webapp/WEB-INF/beans.xml | 25 solutions/create-queries/src/main/webapp/WEB-INF/beans.xml | 25 solutions/persist-entity/src/main/webapp/WEB-INF/faces-config.xml | 30 solutions/create-queries/src/main/webapp/index.xhtml | 26 labs/persist-entity/src/main/webapp/WEB-INF/faces-config.xml | 30 solutions/create-queries/src/main/java/com/redhat/training/ui/Hello.java | 59 + solutions/create-queries/src/main/java/com/redhat/training/services/PersonService.java | 44 solutions/create-queries/src/main/webapp/index.html | 23 solutions/persist-entity/README.md | 12 solutions/persist-entity/src/main/webapp/index.html | 23 labs/persist-entity/pom.xml | 148 +++ solutions/persist-entity/src/main/webapp/WEB-INF/beans.xml | 25 solutions/create-queries/src/main/resources/META-INF/persistence.xml | 15 solutions/create-queries/src/main/webapp/resources/css/screen.css | 79 + labs/persist-entity/src/main/java/com/redhat/training/model/Person.java | 55 + solutions/persist-entity/src/main/webapp/index.xhtml | 34 solutions/persist-entity/src/main/webapp/viewPerson.xhtml | 33 solutions/persist-entity/src/main/webapp/WEB-INF/templates/default.xhtml | 34 labs/create-queries/src/main/webapp/resources/css/screen.css | 79 + labs/persist-entity/src/main/webapp/index.html | 23 labs/create-queries/.gitignore | 5 labs/create-queries/src/main/webapp/viewPersons.xhtml | 32 labs/persist-entity/src/main/webapp/WEB-INF/templates/default.xhtml | 34 labs/persist-entity/src/main/webapp/index.xhtml | 34 69 files changed, 2,880 insertions(+), 0 deletions(-) diff --git a/labs/create-queries/.gitignore b/labs/create-queries/.gitignore new file mode 100644 index 0000000..d0152b8 --- /dev/null +++ b/labs/create-queries/.gitignore @@ -0,0 +1,5 @@ +.classpath +.project +.tern-project +.settings +target diff --git a/labs/create-queries/README.md b/labs/create-queries/README.md new file mode 100644 index 0000000..56cae66 --- /dev/null +++ b/labs/create-queries/README.md @@ -0,0 +1,12 @@ +# JB125 Java EE 7 Hello World web App + +##This app is composed of the following architecture: + +* Maven Project - builds a WAR +* Using JSF 2.2 for web interface, HTML5 facelets page, JSF backing beans, CDI scopes on beans, @Named +* Statelss EJB for services - uses JPA entity beans - exposes appropriate method as REST API using JAX-RS annotations +* JAX-RS Application class +* JPA entity bean to store names - uses H2 in-memory database +* Use EJB Singleton to lookup messaging connection and queue destination and inject into stateless EJB - lookup should only occur once on startup +* Servlet that returns "OK" - this is a health check - path should be [context]/health +* H2 (embedded in-memory DB) backend diff --git a/labs/create-queries/mysql/hellodb.sql b/labs/create-queries/mysql/hellodb.sql new file mode 100644 index 0000000..771d7c3 --- /dev/null +++ b/labs/create-queries/mysql/hellodb.sql @@ -0,0 +1,14 @@ +USE todo; +DROP TABLE IF EXISTS `Person`; +CREATE TABLE `Person` (`id` BIGINT not null auto_increment primary key, `name` VARCHAR(40)); +INSERT INTO `Person` (`id`,`name`) VALUES (1,'John'); +INSERT INTO `Person` (`id`,`name`) VALUES (2,'Steve'); +INSERT INTO `Person` (`id`,`name`) VALUES (3,'Mark'); +INSERT INTO `Person` (`id`,`name`) VALUES (4,'Nick'); +INSERT INTO `Person` (`id`,`name`) VALUES (5,'Nita'); +INSERT INTO `Person` (`id`,`name`) VALUES (6,'John'); +INSERT INTO `Person` (`id`,`name`) VALUES (7,'Julia'); +INSERT INTO `Person` (`id`,`name`) VALUES (8,'Joe'); +INSERT INTO `Person` (`id`,`name`) VALUES (9,'Nita'); +INSERT INTO `Person` (`id`,`name`) VALUES (10,'Branden'); + diff --git a/labs/create-queries/pom.xml b/labs/create-queries/pom.xml new file mode 100644 index 0000000..c6edfb5 --- /dev/null +++ b/labs/create-queries/pom.xml @@ -0,0 +1,148 @@ +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + <artifactId>create-queries</artifactId> + <packaging>war</packaging> + <name>Create Queries web app Project</name> + <description>This is the create-queries project</description> + <parent> + <groupId>com.redhat.training</groupId> + <artifactId>parent-pom</artifactId> + <version>1.0</version> + <relativePath>../pom.xml</relativePath> + </parent> + + <properties> + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> + </properties> + + <dependencies> + + <!-- First declare the APIs we depend on and need for compilation. All + of them are provided by JBoss EAP --> + + <!-- Import the CDI API, we use provided scope as the API is included in + JBoss EAP --> + <dependency> + <groupId>javax.enterprise</groupId> + <artifactId>cdi-api</artifactId> + <scope>provided</scope> + </dependency> + + <!-- Import the Common Annotations API (JSR-250), we use provided scope + as the API is included in JBoss EAP --> + <dependency> + <groupId>org.jboss.spec.javax.annotation</groupId> + <artifactId>jboss-annotations-api_1.2_spec</artifactId> + <scope>provided</scope> + </dependency> + + <!-- Import the JAX-RS API, we use provided scope as the API is included + in JBoss EAP --> + <dependency> + <groupId>org.jboss.spec.javax.ws.rs</groupId> + <artifactId>jboss-jaxrs-api_2.0_spec</artifactId> + <scope>provided</scope> + </dependency> + + <!-- Import the JPA API, we use provided scope as the API is included in + JBoss EAP --> + <dependency> + <groupId>org.hibernate.javax.persistence</groupId> + <artifactId>hibernate-jpa-2.1-api</artifactId> + <scope>provided</scope> + </dependency> + + <!-- Import the EJB API, we use provided scope as the API is included in + JBoss EAP --> + <dependency> + <groupId>org.jboss.spec.javax.ejb</groupId> + <artifactId>jboss-ejb-api_3.2_spec</artifactId> + <scope>provided</scope> + </dependency> + + <dependency> + <groupId>org.jboss.spec.javax.jms</groupId> + <artifactId>jboss-jms-api_2.0_spec</artifactId> + <scope>provided</scope> + </dependency> + + <!-- Bean Validation Implementation --> + <!-- Provides portable constraints such as @Email --> + <!-- Hibernate Validator is shipped in JBoss EAP --> + <dependency> + <groupId>org.hibernate</groupId> + <artifactId>hibernate-validator</artifactId> + <scope>provided</scope> + <exclusions> + <exclusion> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-api</artifactId> + </exclusion> + </exclusions> + </dependency> + + <dependency> + <groupId>org.hibernate</groupId> + <artifactId>hibernate-search-orm</artifactId> + <scope>provided</scope> + </dependency> + + <dependency> + <groupId>org.hibernate</groupId> + <artifactId>hibernate-entitymanager</artifactId> + <scope>provided</scope> + </dependency> + + <!-- Import the JSF API, we use provided scope as the API is included in + JBoss EAP --> + <dependency> + <groupId>org.jboss.spec.javax.faces</groupId> + <artifactId>jboss-jsf-api_2.2_spec</artifactId> + <scope>provided</scope> + </dependency> + + <dependency> + <groupId>org.jboss.spec.javax.servlet</groupId> + <artifactId>jboss-servlet-api_3.1_spec</artifactId> + <scope>provided</scope> + </dependency> + + <!-- Annotation processor to generate the JPA metamodel classes for + typesafe criteria queries --> + <dependency> + <groupId>org.hibernate</groupId> + <artifactId>hibernate-jpamodelgen</artifactId> + <scope>provided</scope> + </dependency> + + <!-- Annotation processor that raising compilation errors whenever constraint + annotations are incorrectly used. --> + <dependency> + <groupId>org.hibernate</groupId> + <artifactId>hibernate-validator-annotation-processor</artifactId> + <scope>provided</scope> + </dependency> + + </dependencies> + + <build> + <finalName>create-queries</finalName> + <plugins> + <plugin> + <artifactId>maven-war-plugin</artifactId> + <version>${version.war.plugin}</version> + <extensions>false</extensions> + <configuration> + <failOnMissingWebXml>false</failOnMissingWebXml> + <archive> + <manifestEntries> + <Dependencies>com.google.guava,org.slf4j + </Dependencies> + </manifestEntries> + </archive> + </configuration> + </plugin> + </plugins> + </build> + +</project> diff --git a/labs/create-queries/src/main/java/com/redhat/training/model/Person.java b/labs/create-queries/src/main/java/com/redhat/training/model/Person.java new file mode 100644 index 0000000..50906a7 --- /dev/null +++ b/labs/create-queries/src/main/java/com/redhat/training/model/Person.java @@ -0,0 +1,58 @@ +package com.redhat.training.model; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.NamedQuery; + +@Entity +//add named query here + +public class Person{ + @Id + private Long id; + + private String name; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((id == null) ? 0 : id.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Person other = (Person) obj; + if (id == null) { + if (other.id != null) + return false; + } else if (!id.equals(other.id)) + return false; + return true; + } + + +} diff --git a/labs/create-queries/src/main/java/com/redhat/training/services/PersonService.java b/labs/create-queries/src/main/java/com/redhat/training/services/PersonService.java new file mode 100644 index 0000000..fc9707e --- /dev/null +++ b/labs/create-queries/src/main/java/com/redhat/training/services/PersonService.java @@ -0,0 +1,24 @@ +package com.redhat.training.services; + +import java.util.List; + +import javax.ejb.Stateless; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import javax.persistence.TypedQuery; +import javax.persistence.Query; + +import com.redhat.training.model.Person; + +@Stateless + +public class PersonService { + + @PersistenceContext(unitName="hello") + private EntityManager entityManager; + + // Get all Person objects in the Database + + //Get persons whose name matches the name given in the query + +} diff --git a/labs/create-queries/src/main/java/com/redhat/training/servlet/PingServlet.java b/labs/create-queries/src/main/java/com/redhat/training/servlet/PingServlet.java new file mode 100644 index 0000000..d413cdf --- /dev/null +++ b/labs/create-queries/src/main/java/com/redhat/training/servlet/PingServlet.java @@ -0,0 +1,60 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2015, Red Hat, Inc. and/or its affiliates, and individual + * contributors by the @authors tag. See the copyright.txt in the + * distribution for a full listing of individual contributors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * 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 CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.redhat.training.servlet; + +import java.io.IOException; +import java.io.PrintWriter; + +import javax.servlet.ServletException; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +/** + * <p> + * A simple servlet taking advantage of features added in 3.0. + * </p> + * + * <p> + * The servlet is registered and mapped to /HelloServlet using the {@linkplain WebServlet + * @HttpServlet}. + * </p> + * + * @author Pete Muir + * + */ +@SuppressWarnings("serial") +@WebServlet("/health") +public class PingServlet extends HttpServlet { + + static String PAGE_HEADER = "<html><head><title>Ping Servlet</title></head><body>"; + + static String PAGE_FOOTER = "</body></html>"; + + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + resp.setContentType("text/html"); + PrintWriter writer = resp.getWriter(); + writer.println(PAGE_HEADER); + writer.println("<h1>OK.</h1>"); + writer.println("It is now " + new java.util.Date().toString() + " at the server."); + writer.println(PAGE_FOOTER); + writer.close(); + } + +} diff --git a/labs/create-queries/src/main/java/com/redhat/training/ui/Hello.java b/labs/create-queries/src/main/java/com/redhat/training/ui/Hello.java new file mode 100755 index 0000000..41cb8a8 --- /dev/null +++ b/labs/create-queries/src/main/java/com/redhat/training/ui/Hello.java @@ -0,0 +1,60 @@ +package com.redhat.training.ui; + +import javax.enterprise.context.RequestScoped; +import javax.inject.Named; + +import com.redhat.training.model.Person; +import com.redhat.training.services.PersonService; + +import javax.inject.Inject; +import java.util.List; + + + +@RequestScoped +@Named("hello") +public class Hello { + private String name; + private Long id; + private List<Person> results; + + @Inject + private PersonService personService; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + //View all persons in the database table + /*public List<Person> getPersons() { + return personService.getAllPersons(); + }*/ + + //view all persons whose name matches the name given in the query + /*public void search() { + results = personService.getPersonsWithName(name); + }*/ + + public List<Person> getResults() { + return results; + } + + public void setResults(List<Person> results) { + this.results = results; + } + + + +} diff --git a/labs/create-queries/src/main/resources/META-INF/persistence.xml b/labs/create-queries/src/main/resources/META-INF/persistence.xml new file mode 100644 index 0000000..0c3c54f --- /dev/null +++ b/labs/create-queries/src/main/resources/META-INF/persistence.xml @@ -0,0 +1,15 @@ +<?xml version="1.0" encoding="UTF-8"?> +<persistence version="2.1" + xmlns="http://xmlns.jcp.org/xml/ns/persistence" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"> + <persistence-unit name="hello" transaction-type="JTA"> + <jta-data-source>java:jboss/datasources/MySQLDS</jta-data-source> + <properties> + <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" /> + <property name="hibernate.hbm2ddl.auto" value="create-drop" /> + <property name="hibernate.show_sql" value="true" /> + <property name="hibernate.format_sql" value="true" /> + </properties> + </persistence-unit> +</persistence> diff --git a/labs/create-queries/src/main/webapp/WEB-INF/beans.xml b/labs/create-queries/src/main/webapp/WEB-INF/beans.xml new file mode 100755 index 0000000..adcbeda --- /dev/null +++ b/labs/create-queries/src/main/webapp/WEB-INF/beans.xml @@ -0,0 +1,25 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + JBoss, Home of Professional Open Source + Copyright 2014, Red Hat, Inc. and/or its affiliates, and individual + contributors by the @authors tag. See the copyright.txt in the + distribution for a full listing of individual contributors. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + http://www.apache.org/licenses/LICENSE-2.0 + 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 CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> +<!-- This file can be an empty text file (0 bytes) --> +<!-- We're declaring the schema to save you time if you do have to configure + this in the future --> +<beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation=" + http://java.sun.com/xml/ns/javaee + http://java.sun.com/xml/ns/javaee/beans_1_0.xsd"> +</beans> diff --git a/labs/create-queries/src/main/webapp/WEB-INF/faces-config.xml b/labs/create-queries/src/main/webapp/WEB-INF/faces-config.xml new file mode 100755 index 0000000..d7054de --- /dev/null +++ b/labs/create-queries/src/main/webapp/WEB-INF/faces-config.xml @@ -0,0 +1,30 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + JBoss, Home of Professional Open Source + Copyright 2014, Red Hat, Inc. and/or its affiliates, and individual + contributors by the @authors tag. See the copyright.txt in the + distribution for a full listing of individual contributors. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + http://www.apache.org/licenses/LICENSE-2.0 + 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 CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> +<!-- This file is not required if you don't need any extra configuration. --> +<faces-config version="2.0" xmlns="http://java.sun.com/xml/ns/javaee" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation=" + http://java.sun.com/xml/ns/javaee + http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"> + + <!-- This descriptor activates the JSF 2.0 Servlet --> + + <!-- Write your navigation rules here. You are encouraged to use CDI + for creating @Named managed beans. --> + +</faces-config> diff --git a/labs/create-queries/src/main/webapp/WEB-INF/templates/default.xhtml b/labs/create-queries/src/main/webapp/WEB-INF/templates/default.xhtml new file mode 100755 index 0000000..2fe703c --- /dev/null +++ b/labs/create-queries/src/main/webapp/WEB-INF/templates/default.xhtml @@ -0,0 +1,34 @@ +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> +<html xmlns="http://www.w3.org/1999/xhtml" + xmlns:h="http://java.sun.com/jsf/html" + xmlns:ui="http://java.sun.com/jsf/facelets"> +<h:head> + <title><ui:insert name="title"> + [Title will be inserted here] + </ui:insert> + </title> + <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> + <h:outputStylesheet name="css/screen.css" /> +</h:head> +<h:body> + + <header> + <h:graphicImage name="images/redhat.png" /> + <div class="right"> + Application: + <ui:insert name="application_name"> + [Application name will be inserted here] + </ui:insert> + </div> + </header> + <div id="content"> + <ui:insert name="content"> + [Template content will be inserted here] + </ui:insert> + </div> + <footer> + JB183 - Enterprise Java Development - Red Hat Training + </footer> +</h:body> +</html> diff --git a/labs/create-queries/src/main/webapp/index.html b/labs/create-queries/src/main/webapp/index.html new file mode 100755 index 0000000..684299b --- /dev/null +++ b/labs/create-queries/src/main/webapp/index.html @@ -0,0 +1,23 @@ +<!-- + JBoss, Home of Professional Open Source + Copyright 2014, Red Hat, Inc. and/or its affiliates, and individual + contributors by the @authors tag. See the copyright.txt in the + distribution for a full listing of individual contributors. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + http://www.apache.org/licenses/LICENSE-2.0 + 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 CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> +<!-- Plain HTML page that kicks us into the app --> + +<html> +<head> +<meta http-equiv="Refresh" content="0; URL=index.jsf"> +</head> +</html> diff --git a/labs/create-queries/src/main/webapp/index.xhtml b/labs/create-queries/src/main/webapp/index.xhtml new file mode 100755 index 0000000..fe9ae31 --- /dev/null +++ b/labs/create-queries/src/main/webapp/index.xhtml @@ -0,0 +1,26 @@ +<?xml version="1.0" encoding="UTF-8"?> + +<ui:composition xmlns="http://www.w3.org/1999/xhtml" + xmlns:ui="http://java.sun.com/jsf/facelets" + xmlns:f="http://java.sun.com/jsf/core" + xmlns:h="http://java.sun.com/jsf/html" + template="/WEB-INF/templates/default.xhtml"> + <ui:define name="title"> + Create Queries Web Application + </ui:define> + <ui:define name="application_name"> + Create Queries Web Application + </ui:define> + <ui:define name="content"> + <h1>Create Queries</h1> + <br class="clear"/> + <h:form id="form"> + <br class="clear"/> + <h1> + <a href="/create-queries/viewPersons.xhtml">View All Users</a> + <br/> + <a href="/create-queries/viewPerson.xhtml">Search Users</a> + </h1> + </h:form> + </ui:define> +</ui:composition> diff --git a/labs/create-queries/src/main/webapp/resources/css/screen.css b/labs/create-queries/src/main/webapp/resources/css/screen.css new file mode 100755 index 0000000..0e284d1 --- /dev/null +++ b/labs/create-queries/src/main/webapp/resources/css/screen.css @@ -0,0 +1,79 @@ +body { + +} + +header { + background-color: #C40016; + height: 150px; + padding: 10px; + width: 95%; + margin: 0 auto; + border: 4px solid #000000; +} + +header .right { + display: inline-block; + float: right; + font-size: 40px; + font-weight: bolder; + height: 150px; + padding: 48px; + vertical-align: middle; +} + +#content { + background-color: #F2F2F2; + width: 91%; + margin: 0 auto; + padding: 100px 50px; + border: 2px solid #000000; +} + +footer { + border: 4px solid #000000; + background-color: #C40016; + height: 30px; + padding: 10px; + width: 95%; + margin: 0 auto; + text-align: center; + font-weight: bolder; + font-size: 27px; +} + +label{ + display: inline-block; + float: left; + clear: left; + width: 165px; +} +input { + display: inline-block; + float: left; + width: 362px; +} +select{ + display: inline-block; + float: left; + width: 371px; +} +textarea{ + display: inline-block; + float: left; +} + +.clear{ + clear: both; +} + +.messages{ + color: green; + font-weight: bolder; +} + +.btn{ + display: inline-block; + float: left; + clear: left; + width: 165px; +} \ No newline at end of file diff --git a/labs/create-queries/src/main/webapp/resources/images/redhat.png b/labs/create-queries/src/main/webapp/resources/images/redhat.png new file mode 100755 index 0000000..0984beb --- /dev/null +++ b/labs/create-queries/src/main/webapp/resources/images/redhat.png Binary files differ diff --git a/labs/create-queries/src/main/webapp/viewPerson.xhtml b/labs/create-queries/src/main/webapp/viewPerson.xhtml new file mode 100755 index 0000000..8ed91ba --- /dev/null +++ b/labs/create-queries/src/main/webapp/viewPerson.xhtml @@ -0,0 +1,43 @@ +<?xml version="1.0" encoding="UTF-8"?> + +<ui:composition xmlns="http://www.w3.org/1999/xhtml" + xmlns:ui="http://java.sun.com/jsf/facelets" + xmlns:f="http://java.sun.com/jsf/core" + xmlns:h="http://java.sun.com/jsf/html" + template="/WEB-INF/templates/default.xhtml"> + <ui:define name="title"> + Create Queries + </ui:define> + <ui:define name="application_name"> + Create Queries + </ui:define> + <ui:define name="content"> + <h1>Search Users</h1> + <br class="clear"/> + <h:form id="viewform"> + <p class="input"> + <h:outputLabel value="Name:" for="name" /> + <h:inputText value="#{hello.name}" id="name" required="true" requiredMessage="Name is required"/> + </p> + <br class="clear"/> + <br class="clear"/> + <p class="input"> + <h:commandButton action="#{hello.search}" value="Submit" styleClass="btn" /> + </p> + + <h:outputLabel value="Results:"/><br/> + <br class="clear"/> + <ul> + <ui:repeat value="#{hello.results}" var="person"> + <li> + <h:outputText value="#{person.id} -- #{person.name}"/> + + <br/> + + </li> + </ui:repeat> + </ul> + <a href="/create-queries/index.xhtml">Back</a> + </h:form> + </ui:define> +</ui:composition> diff --git a/labs/create-queries/src/main/webapp/viewPersons.xhtml b/labs/create-queries/src/main/webapp/viewPersons.xhtml new file mode 100755 index 0000000..fd7913c --- /dev/null +++ b/labs/create-queries/src/main/webapp/viewPersons.xhtml @@ -0,0 +1,32 @@ +<?xml version="1.0" encoding="UTF-8"?> + +<ui:composition xmlns="http://www.w3.org/1999/xhtml" + xmlns:ui="http://java.sun.com/jsf/facelets" + xmlns:f="http://java.sun.com/jsf/core" + xmlns:h="http://java.sun.com/jsf/html" + template="/WEB-INF/templates/default.xhtml"> + <ui:define name="title"> + Create Queries + </ui:define> + <ui:define name="application_name"> + Create Queries + </ui:define> + <ui:define name="content"> + <h1>User Log</h1> + <br class="clear"/> + <h:form id="viewform"> + + <h:outputLabel value="Users:"/> + <br class="clear"/> + <ul> + <ui:repeat value="#{hello.persons}" var="person"> + <li> + <h:outputText value="#{person.id} -- #{person.name}"/><br/> + + </li> + </ui:repeat> + </ul> + <a href="/create-queries/index.xhtml">Back</a> + </h:form> + </ui:define> +</ui:composition> diff --git a/labs/persist-entity/README.md b/labs/persist-entity/README.md new file mode 100644 index 0000000..56cae66 --- /dev/null +++ b/labs/persist-entity/README.md @@ -0,0 +1,12 @@ +# JB125 Java EE 7 Hello World web App + +##This app is composed of the following architecture: + +* Maven Project - builds a WAR +* Using JSF 2.2 for web interface, HTML5 facelets page, JSF backing beans, CDI scopes on beans, @Named +* Statelss EJB for services - uses JPA entity beans - exposes appropriate method as REST API using JAX-RS annotations +* JAX-RS Application class +* JPA entity bean to store names - uses H2 in-memory database +* Use EJB Singleton to lookup messaging connection and queue destination and inject into stateless EJB - lookup should only occur once on startup +* Servlet that returns "OK" - this is a health check - path should be [context]/health +* H2 (embedded in-memory DB) backend diff --git a/labs/persist-entity/pom.xml b/labs/persist-entity/pom.xml new file mode 100644 index 0000000..85e19dc --- /dev/null +++ b/labs/persist-entity/pom.xml @@ -0,0 +1,148 @@ +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + <artifactId>persist-entity</artifactId> + <packaging>war</packaging> + <name>Persist Entity web app Project</name> + <description>This is the persist-entity project</description> + <parent> + <groupId>com.redhat.training</groupId> + <artifactId>parent-pom</artifactId> + <version>1.0</version> + <relativePath>../pom.xml</relativePath> + </parent> + + <properties> + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> + </properties> + + <dependencies> + + <!-- First declare the APIs we depend on and need for compilation. All + of them are provided by JBoss EAP --> + + <!-- Import the CDI API, we use provided scope as the API is included in + JBoss EAP --> + <dependency> + <groupId>javax.enterprise</groupId> + <artifactId>cdi-api</artifactId> + <scope>provided</scope> + </dependency> + + <!-- Import the Common Annotations API (JSR-250), we use provided scope + as the API is included in JBoss EAP --> + <dependency> + <groupId>org.jboss.spec.javax.annotation</groupId> + <artifactId>jboss-annotations-api_1.2_spec</artifactId> + <scope>provided</scope> + </dependency> + + <!-- Import the JAX-RS API, we use provided scope as the API is included + in JBoss EAP --> + <dependency> + <groupId>org.jboss.spec.javax.ws.rs</groupId> + <artifactId>jboss-jaxrs-api_2.0_spec</artifactId> + <scope>provided</scope> + </dependency> + + <!-- Import the JPA API, we use provided scope as the API is included in + JBoss EAP --> + <dependency> + <groupId>org.hibernate.javax.persistence</groupId> + <artifactId>hibernate-jpa-2.1-api</artifactId> + <scope>provided</scope> + </dependency> + + <!-- Import the EJB API, we use provided scope as the API is included in + JBoss EAP --> + <dependency> + <groupId>org.jboss.spec.javax.ejb</groupId> + <artifactId>jboss-ejb-api_3.2_spec</artifactId> + <scope>provided</scope> + </dependency> + + <dependency> + <groupId>org.jboss.spec.javax.jms</groupId> + <artifactId>jboss-jms-api_2.0_spec</artifactId> + <scope>provided</scope> + </dependency> + + <!-- Bean Validation Implementation --> + <!-- Provides portable constraints such as @Email --> + <!-- Hibernate Validator is shipped in JBoss EAP --> + <dependency> + <groupId>org.hibernate</groupId> + <artifactId>hibernate-validator</artifactId> + <scope>provided</scope> + <exclusions> + <exclusion> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-api</artifactId> + </exclusion> + </exclusions> + </dependency> + + <dependency> + <groupId>org.hibernate</groupId> + <artifactId>hibernate-search-orm</artifactId> + <scope>provided</scope> + </dependency> + + <dependency> + <groupId>org.hibernate</groupId> + <artifactId>hibernate-entitymanager</artifactId> + <scope>provided</scope> + </dependency> + + <!-- Import the JSF API, we use provided scope as the API is included in + JBoss EAP --> + <dependency> + <groupId>org.jboss.spec.javax.faces</groupId> + <artifactId>jboss-jsf-api_2.2_spec</artifactId> + <scope>provided</scope> + </dependency> + + <dependency> + <groupId>org.jboss.spec.javax.servlet</groupId> + <artifactId>jboss-servlet-api_3.1_spec</artifactId> + <scope>provided</scope> + </dependency> + + <!-- Annotation processor to generate the JPA metamodel classes for + typesafe criteria queries --> + <dependency> + <groupId>org.hibernate</groupId> + <artifactId>hibernate-jpamodelgen</artifactId> + <scope>provided</scope> + </dependency> + + <!-- Annotation processor that raising compilation errors whenever constraint + annotations are incorrectly used. --> + <dependency> + <groupId>org.hibernate</groupId> + <artifactId>hibernate-validator-annotation-processor</artifactId> + <scope>provided</scope> + </dependency> + + </dependencies> + + <build> + <finalName>persist-entity</finalName> + <plugins> + <plugin> + <artifactId>maven-war-plugin</artifactId> + <version>${version.war.plugin}</version> + <extensions>false</extensions> + <configuration> + <failOnMissingWebXml>false</failOnMissingWebXml> + <archive> + <manifestEntries> + <Dependencies>com.google.guava,org.slf4j + </Dependencies> + </manifestEntries> + </archive> + </configuration> + </plugin> + </plugins> + </build> + +</project> diff --git a/labs/persist-entity/src/main/java/com/redhat/training/model/Person.java b/labs/persist-entity/src/main/java/com/redhat/training/model/Person.java new file mode 100644 index 0000000..ae89728 --- /dev/null +++ b/labs/persist-entity/src/main/java/com/redhat/training/model/Person.java @@ -0,0 +1,55 @@ +package com.redhat.training.model; +//add required libraries + +//add @Entity annotation here +public class Person { + + //add annotations for primary key + private Long id; + + //add @Column(name="name") annotation to map column in database table + private String personName; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getPersonName() { + return personName; + } + + public void setName(String personName) { + this.personName = personName; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((id == null) ? 0 : id.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Person other = (Person) obj; + if (id == null) { + if (other.id != null) + return false; + } else if (!id.equals(other.id)) + return false; + return true; + } + + +} diff --git a/labs/persist-entity/src/main/java/com/redhat/training/services/PersonService.java b/labs/persist-entity/src/main/java/com/redhat/training/services/PersonService.java new file mode 100644 index 0000000..3851693 --- /dev/null +++ b/labs/persist-entity/src/main/java/com/redhat/training/services/PersonService.java @@ -0,0 +1,55 @@ +package com.redhat.training.services; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.List; + +import javax.ejb.EJBException; +import javax.ejb.Stateless; +//import persistence related libraries +import javax.persistence.TypedQuery; + +import com.redhat.training.model.Person; + +@Stateless + +public class PersonService { + //TODO: obtain an EntityManager instance using @PersistenceContext + + + // Simple non-RESTy method for JSF bean invocation + public String hello(String name) { + try { + // let's grab the current date and time on the server + LocalDateTime today = LocalDateTime.now(); + + // format it nicely for on-screen display + DateTimeFormatter format = DateTimeFormatter.ofPattern("MMM dd yyyy hh:mm:ss a"); + String fdate = today.format(format); + + // Create a new Person object and persist to database + Person p = new Person(); + p.setName(name); + // call persist() method of entity manager to save the data + + // respond back with Hello and convert the name to UPPERCASE. Also, send the + // current time on the server. + return "Hello " + name.toUpperCase() + "!. " + "Time on the server is: " + fdate; + } catch (Exception e) { + throw new EJBException(e); + } + } + + + // TODO:add public String getPerson(Long id) method here to fetch result + // by Person id using find() method + + // Get all Person objects in the Database + public List<Person> getAllPersons() { + TypedQuery<Person> query = entityManager.createQuery("SELECT p FROM Person p", Person.class); + List<Person> persons = query.getResultList(); + + return persons; + } + +} diff --git a/labs/persist-entity/src/main/java/com/redhat/training/servlet/PingServlet.java b/labs/persist-entity/src/main/java/com/redhat/training/servlet/PingServlet.java new file mode 100644 index 0000000..d413cdf --- /dev/null +++ b/labs/persist-entity/src/main/java/com/redhat/training/servlet/PingServlet.java @@ -0,0 +1,60 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2015, Red Hat, Inc. and/or its affiliates, and individual + * contributors by the @authors tag. See the copyright.txt in the + * distribution for a full listing of individual contributors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * 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 CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.redhat.training.servlet; + +import java.io.IOException; +import java.io.PrintWriter; + +import javax.servlet.ServletException; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +/** + * <p> + * A simple servlet taking advantage of features added in 3.0. + * </p> + * + * <p> + * The servlet is registered and mapped to /HelloServlet using the {@linkplain WebServlet + * @HttpServlet}. + * </p> + * + * @author Pete Muir + * + */ +@SuppressWarnings("serial") +@WebServlet("/health") +public class PingServlet extends HttpServlet { + + static String PAGE_HEADER = "<html><head><title>Ping Servlet</title></head><body>"; + + static String PAGE_FOOTER = "</body></html>"; + + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + resp.setContentType("text/html"); + PrintWriter writer = resp.getWriter(); + writer.println(PAGE_HEADER); + writer.println("<h1>OK.</h1>"); + writer.println("It is now " + new java.util.Date().toString() + " at the server."); + writer.println(PAGE_FOOTER); + writer.close(); + } + +} diff --git a/labs/persist-entity/src/main/java/com/redhat/training/ui/Hello.java b/labs/persist-entity/src/main/java/com/redhat/training/ui/Hello.java new file mode 100755 index 0000000..917f672 --- /dev/null +++ b/labs/persist-entity/src/main/java/com/redhat/training/ui/Hello.java @@ -0,0 +1,94 @@ +package com.redhat.training.ui; + +import javax.enterprise.context.RequestScoped; +import javax.faces.application.FacesMessage; +import javax.faces.context.FacesContext; +import javax.inject.Named; +import javax.validation.ConstraintViolation; +import javax.validation.ConstraintViolationException; + +import com.redhat.training.model.Person; +import com.redhat.training.services.PersonService; + +import javax.inject.Inject; +import java.util.List; + +@RequestScoped +@Named("hello") +public class Hello { + private String name; + private Long id; + + @Inject + private PersonService personService; + + public void sayHello() { + try { + String response = personService.hello(name); + FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(response)); + }catch(Exception e){ + System.out.println(e.getCause()); + if(e.getCause() != null && e.getCause() instanceof ConstraintViolationException) { + ConstraintViolationException ex = (ConstraintViolationException) e.getCause(); + String violations = ""; + for(ConstraintViolation<?> cv: ex.getConstraintViolations()) { + + violations += cv.getMessage() + "\n"; + + System.out.println("Violations: "+violations); + } + FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(violations)); + } + + } + + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + /*public void getPerson() { + try { + String response = personService.getPerson(id); + FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(response)); + }catch(Exception e){ + System.out.println(e.getCause()); + if(e.getCause() != null && e.getCause() instanceof ConstraintViolationException) { + ConstraintViolationException ex = (ConstraintViolationException) e.getCause(); + String violations = ""; + for(ConstraintViolation<?> cv: ex.getConstraintViolations()) { + + violations += cv.getMessage() + "\n"; + + System.out.println("Violations: "+violations); + } + FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(violations)); + } + + } + + }*/ + + + /*public List<Person> getPersons() { + return personService.getAllPersons(); + + }*/ + + + + +} diff --git a/labs/persist-entity/src/main/resources/META-INF/persistence.xml b/labs/persist-entity/src/main/resources/META-INF/persistence.xml new file mode 100644 index 0000000..41466e2 --- /dev/null +++ b/labs/persist-entity/src/main/resources/META-INF/persistence.xml @@ -0,0 +1,15 @@ +<?xml version="1.0" encoding="UTF-8"?> +<persistence version="2.1" + xmlns="http://xmlns.jcp.org/xml/ns/persistence" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"> + <persistence-unit name="hello" transaction-type="JTA"> + <jta-data-source>java:jboss/datasources/ExampleDS</jta-data-source> + <properties> + <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" /> + <property name="hibernate.hbm2ddl.auto" value="create-drop" /> + <property name="hibernate.show_sql" value="true" /> + <property name="hibernate.format_sql" value="true" /> + </properties> + </persistence-unit> +</persistence> diff --git a/labs/persist-entity/src/main/webapp/WEB-INF/beans.xml b/labs/persist-entity/src/main/webapp/WEB-INF/beans.xml new file mode 100755 index 0000000..adcbeda --- /dev/null +++ b/labs/persist-entity/src/main/webapp/WEB-INF/beans.xml @@ -0,0 +1,25 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + JBoss, Home of Professional Open Source + Copyright 2014, Red Hat, Inc. and/or its affiliates, and individual + contributors by the @authors tag. See the copyright.txt in the + distribution for a full listing of individual contributors. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + http://www.apache.org/licenses/LICENSE-2.0 + 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 CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> +<!-- This file can be an empty text file (0 bytes) --> +<!-- We're declaring the schema to save you time if you do have to configure + this in the future --> +<beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation=" + http://java.sun.com/xml/ns/javaee + http://java.sun.com/xml/ns/javaee/beans_1_0.xsd"> +</beans> diff --git a/labs/persist-entity/src/main/webapp/WEB-INF/faces-config.xml b/labs/persist-entity/src/main/webapp/WEB-INF/faces-config.xml new file mode 100755 index 0000000..d7054de --- /dev/null +++ b/labs/persist-entity/src/main/webapp/WEB-INF/faces-config.xml @@ -0,0 +1,30 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + JBoss, Home of Professional Open Source + Copyright 2014, Red Hat, Inc. and/or its affiliates, and individual + contributors by the @authors tag. See the copyright.txt in the + distribution for a full listing of individual contributors. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + http://www.apache.org/licenses/LICENSE-2.0 + 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 CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> +<!-- This file is not required if you don't need any extra configuration. --> +<faces-config version="2.0" xmlns="http://java.sun.com/xml/ns/javaee" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation=" + http://java.sun.com/xml/ns/javaee + http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"> + + <!-- This descriptor activates the JSF 2.0 Servlet --> + + <!-- Write your navigation rules here. You are encouraged to use CDI + for creating @Named managed beans. --> + +</faces-config> diff --git a/labs/persist-entity/src/main/webapp/WEB-INF/templates/default.xhtml b/labs/persist-entity/src/main/webapp/WEB-INF/templates/default.xhtml new file mode 100755 index 0000000..2fe703c --- /dev/null +++ b/labs/persist-entity/src/main/webapp/WEB-INF/templates/default.xhtml @@ -0,0 +1,34 @@ +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> +<html xmlns="http://www.w3.org/1999/xhtml" + xmlns:h="http://java.sun.com/jsf/html" + xmlns:ui="http://java.sun.com/jsf/facelets"> +<h:head> + <title><ui:insert name="title"> + [Title will be inserted here] + </ui:insert> + </title> + <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> + <h:outputStylesheet name="css/screen.css" /> +</h:head> +<h:body> + + <header> + <h:graphicImage name="images/redhat.png" /> + <div class="right"> + Application: + <ui:insert name="application_name"> + [Application name will be inserted here] + </ui:insert> + </div> + </header> + <div id="content"> + <ui:insert name="content"> + [Template content will be inserted here] + </ui:insert> + </div> + <footer> + JB183 - Enterprise Java Development - Red Hat Training + </footer> +</h:body> +</html> diff --git a/labs/persist-entity/src/main/webapp/index.html b/labs/persist-entity/src/main/webapp/index.html new file mode 100755 index 0000000..684299b --- /dev/null +++ b/labs/persist-entity/src/main/webapp/index.html @@ -0,0 +1,23 @@ +<!-- + JBoss, Home of Professional Open Source + Copyright 2014, Red Hat, Inc. and/or its affiliates, and individual + contributors by the @authors tag. See the copyright.txt in the + distribution for a full listing of individual contributors. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + http://www.apache.org/licenses/LICENSE-2.0 + 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 CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> +<!-- Plain HTML page that kicks us into the app --> + +<html> +<head> +<meta http-equiv="Refresh" content="0; URL=index.jsf"> +</head> +</html> diff --git a/labs/persist-entity/src/main/webapp/index.xhtml b/labs/persist-entity/src/main/webapp/index.xhtml new file mode 100755 index 0000000..04c5bb3 --- /dev/null +++ b/labs/persist-entity/src/main/webapp/index.xhtml @@ -0,0 +1,34 @@ +<?xml version="1.0" encoding="UTF-8"?> + +<ui:composition xmlns="http://www.w3.org/1999/xhtml" + xmlns:ui="http://java.sun.com/jsf/facelets" + xmlns:f="http://java.sun.com/jsf/core" + xmlns:h="http://java.sun.com/jsf/html" + template="/WEB-INF/templates/default.xhtml"> + <ui:define name="title"> + Persist Entity web app + </ui:define> + <ui:define name="application_name"> + Persist Entity web app + </ui:define> + <ui:define name="content"> + <h1>Persist Entity web app</h1> + <br class="clear"/> + <h:form id="form"> + <p class="input"> + <h:outputLabel value="Enter your name:" for="name" /> + <h:inputText value="#{hello.name}" id="name" required="true" requiredMessage="Name is required"/> + </p> + <br class="clear"/> + <br class="clear"/> + <p class="input"> + <h:commandButton action="#{hello.sayHello()}" value="Submit" styleClass="btn" /> + </p> + <br class="clear"/> + <br class="clear"/> + <h:messages styleClass="messages"/> + <a href="/persist-entity/viewPersons.xhtml">View all names</a> + <a href="/persist-entity/viewPerson.xhtml">Search a name</a> + </h:form> + </ui:define> +</ui:composition> diff --git a/labs/persist-entity/src/main/webapp/resources/css/screen.css b/labs/persist-entity/src/main/webapp/resources/css/screen.css new file mode 100755 index 0000000..0e284d1 --- /dev/null +++ b/labs/persist-entity/src/main/webapp/resources/css/screen.css @@ -0,0 +1,79 @@ +body { + +} + +header { + background-color: #C40016; + height: 150px; + padding: 10px; + width: 95%; + margin: 0 auto; + border: 4px solid #000000; +} + +header .right { + display: inline-block; + float: right; + font-size: 40px; + font-weight: bolder; + height: 150px; + padding: 48px; + vertical-align: middle; +} + +#content { + background-color: #F2F2F2; + width: 91%; + margin: 0 auto; + padding: 100px 50px; + border: 2px solid #000000; +} + +footer { + border: 4px solid #000000; + background-color: #C40016; + height: 30px; + padding: 10px; + width: 95%; + margin: 0 auto; + text-align: center; + font-weight: bolder; + font-size: 27px; +} + +label{ + display: inline-block; + float: left; + clear: left; + width: 165px; +} +input { + display: inline-block; + float: left; + width: 362px; +} +select{ + display: inline-block; + float: left; + width: 371px; +} +textarea{ + display: inline-block; + float: left; +} + +.clear{ + clear: both; +} + +.messages{ + color: green; + font-weight: bolder; +} + +.btn{ + display: inline-block; + float: left; + clear: left; + width: 165px; +} \ No newline at end of file diff --git a/labs/persist-entity/src/main/webapp/resources/images/redhat.png b/labs/persist-entity/src/main/webapp/resources/images/redhat.png new file mode 100755 index 0000000..0984beb --- /dev/null +++ b/labs/persist-entity/src/main/webapp/resources/images/redhat.png Binary files differ diff --git a/labs/persist-entity/src/main/webapp/viewPerson.xhtml b/labs/persist-entity/src/main/webapp/viewPerson.xhtml new file mode 100755 index 0000000..89fab2e --- /dev/null +++ b/labs/persist-entity/src/main/webapp/viewPerson.xhtml @@ -0,0 +1,33 @@ +<?xml version="1.0" encoding="UTF-8"?> + +<ui:composition xmlns="http://www.w3.org/1999/xhtml" + xmlns:ui="http://java.sun.com/jsf/facelets" + xmlns:f="http://java.sun.com/jsf/core" + xmlns:h="http://java.sun.com/jsf/html" + template="/WEB-INF/templates/default.xhtml"> + <ui:define name="title"> + Persist Entity web app + </ui:define> + <ui:define name="application_name"> + Persist Entity web app + </ui:define> + <ui:define name="content"> + <h1>Persist Entity web app</h1> + <br class="clear"/> + <h:form id="Personform"> + <p class="input"> + <h:outputLabel value="Enter Id:" for="id" /> + <h:inputText value="#{hello.id}" id="id" required="true" requiredMessage="Id is required"/> + </p> + <br class="clear"/> + <br class="clear"/> + <p class="input"> + <h:commandButton action="#{hello.getPerson()}" value="Submit" styleClass="btn" /> + </p> + <br class="clear"/> + <br class="clear"/> + <h:messages styleClass="messages"/> + <a href="/persist-entity/index.xhtml">Back</a> + </h:form> + </ui:define> +</ui:composition> diff --git a/labs/persist-entity/src/main/webapp/viewPersons.xhtml b/labs/persist-entity/src/main/webapp/viewPersons.xhtml new file mode 100755 index 0000000..660d662 --- /dev/null +++ b/labs/persist-entity/src/main/webapp/viewPersons.xhtml @@ -0,0 +1,32 @@ +<?xml version="1.0" encoding="UTF-8"?> + +<ui:composition xmlns="http://www.w3.org/1999/xhtml" + xmlns:ui="http://java.sun.com/jsf/facelets" + xmlns:f="http://java.sun.com/jsf/core" + xmlns:h="http://java.sun.com/jsf/html" + template="/WEB-INF/templates/default.xhtml"> + <ui:define name="title"> + All who said hello + </ui:define> + <ui:define name="application_name"> + Say Hello + </ui:define> + <ui:define name="content"> + <h1>These people said hello</h1> + <br class="clear"/> + <h:form id="viewform"> + + <h:outputLabel value="Persons:"/> + <br class="clear"/> + <ul> + <ui:repeat value="#{hello.persons}" var="person"> + <li> + <strong>Name:</strong><h:outputText value="#{person.personName}"/><br/> + + </li> + </ui:repeat> + </ul> + <a href="/persist-entity/index.xhtml">Back</a> + </h:form> + </ui:define> +</ui:composition> diff --git a/solutions/create-queries/.gitignore b/solutions/create-queries/.gitignore new file mode 100644 index 0000000..d0152b8 --- /dev/null +++ b/solutions/create-queries/.gitignore @@ -0,0 +1,5 @@ +.classpath +.project +.tern-project +.settings +target diff --git a/solutions/create-queries/README.md b/solutions/create-queries/README.md new file mode 100644 index 0000000..56cae66 --- /dev/null +++ b/solutions/create-queries/README.md @@ -0,0 +1,12 @@ +# JB125 Java EE 7 Hello World web App + +##This app is composed of the following architecture: + +* Maven Project - builds a WAR +* Using JSF 2.2 for web interface, HTML5 facelets page, JSF backing beans, CDI scopes on beans, @Named +* Statelss EJB for services - uses JPA entity beans - exposes appropriate method as REST API using JAX-RS annotations +* JAX-RS Application class +* JPA entity bean to store names - uses H2 in-memory database +* Use EJB Singleton to lookup messaging connection and queue destination and inject into stateless EJB - lookup should only occur once on startup +* Servlet that returns "OK" - this is a health check - path should be [context]/health +* H2 (embedded in-memory DB) backend diff --git a/solutions/create-queries/mysql/hellodb.sql b/solutions/create-queries/mysql/hellodb.sql new file mode 100644 index 0000000..771d7c3 --- /dev/null +++ b/solutions/create-queries/mysql/hellodb.sql @@ -0,0 +1,14 @@ +USE todo; +DROP TABLE IF EXISTS `Person`; +CREATE TABLE `Person` (`id` BIGINT not null auto_increment primary key, `name` VARCHAR(40)); +INSERT INTO `Person` (`id`,`name`) VALUES (1,'John'); +INSERT INTO `Person` (`id`,`name`) VALUES (2,'Steve'); +INSERT INTO `Person` (`id`,`name`) VALUES (3,'Mark'); +INSERT INTO `Person` (`id`,`name`) VALUES (4,'Nick'); +INSERT INTO `Person` (`id`,`name`) VALUES (5,'Nita'); +INSERT INTO `Person` (`id`,`name`) VALUES (6,'John'); +INSERT INTO `Person` (`id`,`name`) VALUES (7,'Julia'); +INSERT INTO `Person` (`id`,`name`) VALUES (8,'Joe'); +INSERT INTO `Person` (`id`,`name`) VALUES (9,'Nita'); +INSERT INTO `Person` (`id`,`name`) VALUES (10,'Branden'); + diff --git a/solutions/create-queries/pom.xml b/solutions/create-queries/pom.xml new file mode 100644 index 0000000..eeb2f0a --- /dev/null +++ b/solutions/create-queries/pom.xml @@ -0,0 +1,148 @@ +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + <artifactId>create-queries-solution</artifactId> + <packaging>war</packaging> + <name>Create Queries web app Project</name> + <description>This is the create-queries project</description> + <parent> + <groupId>com.redhat.training</groupId> + <artifactId>parent-pom</artifactId> + <version>1.0</version> + <relativePath>../pom.xml</relativePath> + </parent> + + <properties> + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> + </properties> + + <dependencies> + + <!-- First declare the APIs we depend on and need for compilation. All + of them are provided by JBoss EAP --> + + <!-- Import the CDI API, we use provided scope as the API is included in + JBoss EAP --> + <dependency> + <groupId>javax.enterprise</groupId> + <artifactId>cdi-api</artifactId> + <scope>provided</scope> + </dependency> + + <!-- Import the Common Annotations API (JSR-250), we use provided scope + as the API is included in JBoss EAP --> + <dependency> + <groupId>org.jboss.spec.javax.annotation</groupId> + <artifactId>jboss-annotations-api_1.2_spec</artifactId> + <scope>provided</scope> + </dependency> + + <!-- Import the JAX-RS API, we use provided scope as the API is included + in JBoss EAP --> + <dependency> + <groupId>org.jboss.spec.javax.ws.rs</groupId> + <artifactId>jboss-jaxrs-api_2.0_spec</artifactId> + <scope>provided</scope> + </dependency> + + <!-- Import the JPA API, we use provided scope as the API is included in + JBoss EAP --> + <dependency> + <groupId>org.hibernate.javax.persistence</groupId> + <artifactId>hibernate-jpa-2.1-api</artifactId> + <scope>provided</scope> + </dependency> + + <!-- Import the EJB API, we use provided scope as the API is included in + JBoss EAP --> + <dependency> + <groupId>org.jboss.spec.javax.ejb</groupId> + <artifactId>jboss-ejb-api_3.2_spec</artifactId> + <scope>provided</scope> + </dependency> + + <dependency> + <groupId>org.jboss.spec.javax.jms</groupId> + <artifactId>jboss-jms-api_2.0_spec</artifactId> + <scope>provided</scope> + </dependency> + + <!-- Bean Validation Implementation --> + <!-- Provides portable constraints such as @Email --> + <!-- Hibernate Validator is shipped in JBoss EAP --> + <dependency> + <groupId>org.hibernate</groupId> + <artifactId>hibernate-validator</artifactId> + <scope>provided</scope> + <exclusions> + <exclusion> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-api</artifactId> + </exclusion> + </exclusions> + </dependency> + + <dependency> + <groupId>org.hibernate</groupId> + <artifactId>hibernate-search-orm</artifactId> + <scope>provided</scope> + </dependency> + + <dependency> + <groupId>org.hibernate</groupId> + <artifactId>hibernate-entitymanager</artifactId> + <scope>provided</scope> + </dependency> + + <!-- Import the JSF API, we use provided scope as the API is included in + JBoss EAP --> + <dependency> + <groupId>org.jboss.spec.javax.faces</groupId> + <artifactId>jboss-jsf-api_2.2_spec</artifactId> + <scope>provided</scope> + </dependency> + + <dependency> + <groupId>org.jboss.spec.javax.servlet</groupId> + <artifactId>jboss-servlet-api_3.1_spec</artifactId> + <scope>provided</scope> + </dependency> + + <!-- Annotation processor to generate the JPA metamodel classes for + typesafe criteria queries --> + <dependency> + <groupId>org.hibernate</groupId> + <artifactId>hibernate-jpamodelgen</artifactId> + <scope>provided</scope> + </dependency> + + <!-- Annotation processor that raising compilation errors whenever constraint + annotations are incorrectly used. --> + <dependency> + <groupId>org.hibernate</groupId> + <artifactId>hibernate-validator-annotation-processor</artifactId> + <scope>provided</scope> + </dependency> + + </dependencies> + + <build> + <finalName>create-queries</finalName> + <plugins> + <plugin> + <artifactId>maven-war-plugin</artifactId> + <version>${version.war.plugin}</version> + <extensions>false</extensions> + <configuration> + <failOnMissingWebXml>false</failOnMissingWebXml> + <archive> + <manifestEntries> + <Dependencies>com.google.guava,org.slf4j + </Dependencies> + </manifestEntries> + </archive> + </configuration> + </plugin> + </plugins> + </build> + +</project> diff --git a/solutions/create-queries/src/main/java/com/redhat/training/model/Person.java b/solutions/create-queries/src/main/java/com/redhat/training/model/Person.java new file mode 100644 index 0000000..8100410 --- /dev/null +++ b/solutions/create-queries/src/main/java/com/redhat/training/model/Person.java @@ -0,0 +1,60 @@ +package com.redhat.training.model; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.NamedQuery; + +@Entity +//add named query here +@NamedQuery( + name="getAllPersonsWithName", + query="select p from Person p where p.name = :pname") +public class Person{ + @Id + private Long id; + + private String name; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((id == null) ? 0 : id.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Person other = (Person) obj; + if (id == null) { + if (other.id != null) + return false; + } else if (!id.equals(other.id)) + return false; + return true; + } + + +} diff --git a/solutions/create-queries/src/main/java/com/redhat/training/services/PersonService.java b/solutions/create-queries/src/main/java/com/redhat/training/services/PersonService.java new file mode 100644 index 0000000..b71e9c9 --- /dev/null +++ b/solutions/create-queries/src/main/java/com/redhat/training/services/PersonService.java @@ -0,0 +1,44 @@ +package com.redhat.training.services; + +import java.util.List; + +import javax.ejb.Stateless; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import javax.persistence.TypedQuery; +import javax.persistence.Query; + +import com.redhat.training.model.Person; + +@Stateless + +public class PersonService { + + @PersistenceContext(unitName="hello") + private EntityManager entityManager; + + // Get all Person objects in the Database + public List<Person> getAllPersons() { + TypedQuery<Person> query = entityManager.createQuery("SELECT p FROM Person p", Person.class); + return query.getResultList(); + } + /*//Get persons whose name matches the name given in the query -- Named parameters + public List<Person> getPersonsWithName(String name) { + TypedQuery<Person> query = entityManager.createQuery("SELECT p from Person p where p.name =:pname", Person.class); + query.setParameter("pname", name); + return = query.getResultList(); + + }*/ + /* //Get persons whose name matches the name given in the query -- positional parameters + public List<Person> getPersonsWithName(String name) { + TypedQuery<Person> query = entityManager.createQuery("SELECT p from Person p where p.name =?1", Person.class); + query.setParameter(1, name); + return = query.getResultList(); + }*/ + //Get persons whose name matches the name given in the query + public List<Person> getPersonsWithName(String name) { + TypedQuery<Person> query = entityManager.createNamedQuery("getAllPersonsWithName", Person.class); + query.setParameter("pname", name); + return query.getResultList(); + } +} diff --git a/solutions/create-queries/src/main/java/com/redhat/training/servlet/PingServlet.java b/solutions/create-queries/src/main/java/com/redhat/training/servlet/PingServlet.java new file mode 100644 index 0000000..d413cdf --- /dev/null +++ b/solutions/create-queries/src/main/java/com/redhat/training/servlet/PingServlet.java @@ -0,0 +1,60 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2015, Red Hat, Inc. and/or its affiliates, and individual + * contributors by the @authors tag. See the copyright.txt in the + * distribution for a full listing of individual contributors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * 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 CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.redhat.training.servlet; + +import java.io.IOException; +import java.io.PrintWriter; + +import javax.servlet.ServletException; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +/** + * <p> + * A simple servlet taking advantage of features added in 3.0. + * </p> + * + * <p> + * The servlet is registered and mapped to /HelloServlet using the {@linkplain WebServlet + * @HttpServlet}. + * </p> + * + * @author Pete Muir + * + */ +@SuppressWarnings("serial") +@WebServlet("/health") +public class PingServlet extends HttpServlet { + + static String PAGE_HEADER = "<html><head><title>Ping Servlet</title></head><body>"; + + static String PAGE_FOOTER = "</body></html>"; + + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + resp.setContentType("text/html"); + PrintWriter writer = resp.getWriter(); + writer.println(PAGE_HEADER); + writer.println("<h1>OK.</h1>"); + writer.println("It is now " + new java.util.Date().toString() + " at the server."); + writer.println(PAGE_FOOTER); + writer.close(); + } + +} diff --git a/solutions/create-queries/src/main/java/com/redhat/training/ui/Hello.java b/solutions/create-queries/src/main/java/com/redhat/training/ui/Hello.java new file mode 100755 index 0000000..de62004 --- /dev/null +++ b/solutions/create-queries/src/main/java/com/redhat/training/ui/Hello.java @@ -0,0 +1,59 @@ +package com.redhat.training.ui; + +import javax.enterprise.context.RequestScoped; +import javax.inject.Named; + +import com.redhat.training.model.Person; +import com.redhat.training.services.PersonService; + +import javax.inject.Inject; +import java.util.List; + + + +@RequestScoped +@Named("hello") +public class Hello { + private String name; + private Long id; + private List<Person> results; + + @Inject + private PersonService personService; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + //View all persons in the database table + public List<Person> getPersons() { + return personService.getAllPersons(); + } + + //view all persons whose name matches the name given in the query + public void search() { + results = personService.getPersonsWithName(name); + } + + public List<Person> getResults() { + return results; + } + + public void setResults(List<Person> results) { + this.results = results; + } + + +} diff --git a/solutions/create-queries/src/main/resources/META-INF/persistence.xml b/solutions/create-queries/src/main/resources/META-INF/persistence.xml new file mode 100644 index 0000000..0c3c54f --- /dev/null +++ b/solutions/create-queries/src/main/resources/META-INF/persistence.xml @@ -0,0 +1,15 @@ +<?xml version="1.0" encoding="UTF-8"?> +<persistence version="2.1" + xmlns="http://xmlns.jcp.org/xml/ns/persistence" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"> + <persistence-unit name="hello" transaction-type="JTA"> + <jta-data-source>java:jboss/datasources/MySQLDS</jta-data-source> + <properties> + <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" /> + <property name="hibernate.hbm2ddl.auto" value="create-drop" /> + <property name="hibernate.show_sql" value="true" /> + <property name="hibernate.format_sql" value="true" /> + </properties> + </persistence-unit> +</persistence> diff --git a/solutions/create-queries/src/main/webapp/WEB-INF/beans.xml b/solutions/create-queries/src/main/webapp/WEB-INF/beans.xml new file mode 100755 index 0000000..adcbeda --- /dev/null +++ b/solutions/create-queries/src/main/webapp/WEB-INF/beans.xml @@ -0,0 +1,25 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + JBoss, Home of Professional Open Source + Copyright 2014, Red Hat, Inc. and/or its affiliates, and individual + contributors by the @authors tag. See the copyright.txt in the + distribution for a full listing of individual contributors. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + http://www.apache.org/licenses/LICENSE-2.0 + 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 CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> +<!-- This file can be an empty text file (0 bytes) --> +<!-- We're declaring the schema to save you time if you do have to configure + this in the future --> +<beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation=" + http://java.sun.com/xml/ns/javaee + http://java.sun.com/xml/ns/javaee/beans_1_0.xsd"> +</beans> diff --git a/solutions/create-queries/src/main/webapp/WEB-INF/faces-config.xml b/solutions/create-queries/src/main/webapp/WEB-INF/faces-config.xml new file mode 100755 index 0000000..d7054de --- /dev/null +++ b/solutions/create-queries/src/main/webapp/WEB-INF/faces-config.xml @@ -0,0 +1,30 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + JBoss, Home of Professional Open Source + Copyright 2014, Red Hat, Inc. and/or its affiliates, and individual + contributors by the @authors tag. See the copyright.txt in the + distribution for a full listing of individual contributors. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + http://www.apache.org/licenses/LICENSE-2.0 + 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 CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> +<!-- This file is not required if you don't need any extra configuration. --> +<faces-config version="2.0" xmlns="http://java.sun.com/xml/ns/javaee" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation=" + http://java.sun.com/xml/ns/javaee + http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"> + + <!-- This descriptor activates the JSF 2.0 Servlet --> + + <!-- Write your navigation rules here. You are encouraged to use CDI + for creating @Named managed beans. --> + +</faces-config> diff --git a/solutions/create-queries/src/main/webapp/WEB-INF/templates/default.xhtml b/solutions/create-queries/src/main/webapp/WEB-INF/templates/default.xhtml new file mode 100755 index 0000000..2fe703c --- /dev/null +++ b/solutions/create-queries/src/main/webapp/WEB-INF/templates/default.xhtml @@ -0,0 +1,34 @@ +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> +<html xmlns="http://www.w3.org/1999/xhtml" + xmlns:h="http://java.sun.com/jsf/html" + xmlns:ui="http://java.sun.com/jsf/facelets"> +<h:head> + <title><ui:insert name="title"> + [Title will be inserted here] + </ui:insert> + </title> + <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> + <h:outputStylesheet name="css/screen.css" /> +</h:head> +<h:body> + + <header> + <h:graphicImage name="images/redhat.png" /> + <div class="right"> + Application: + <ui:insert name="application_name"> + [Application name will be inserted here] + </ui:insert> + </div> + </header> + <div id="content"> + <ui:insert name="content"> + [Template content will be inserted here] + </ui:insert> + </div> + <footer> + JB183 - Enterprise Java Development - Red Hat Training + </footer> +</h:body> +</html> diff --git a/solutions/create-queries/src/main/webapp/index.html b/solutions/create-queries/src/main/webapp/index.html new file mode 100755 index 0000000..684299b --- /dev/null +++ b/solutions/create-queries/src/main/webapp/index.html @@ -0,0 +1,23 @@ +<!-- + JBoss, Home of Professional Open Source + Copyright 2014, Red Hat, Inc. and/or its affiliates, and individual + contributors by the @authors tag. See the copyright.txt in the + distribution for a full listing of individual contributors. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + http://www.apache.org/licenses/LICENSE-2.0 + 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 CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> +<!-- Plain HTML page that kicks us into the app --> + +<html> +<head> +<meta http-equiv="Refresh" content="0; URL=index.jsf"> +</head> +</html> diff --git a/solutions/create-queries/src/main/webapp/index.xhtml b/solutions/create-queries/src/main/webapp/index.xhtml new file mode 100755 index 0000000..fe9ae31 --- /dev/null +++ b/solutions/create-queries/src/main/webapp/index.xhtml @@ -0,0 +1,26 @@ +<?xml version="1.0" encoding="UTF-8"?> + +<ui:composition xmlns="http://www.w3.org/1999/xhtml" + xmlns:ui="http://java.sun.com/jsf/facelets" + xmlns:f="http://java.sun.com/jsf/core" + xmlns:h="http://java.sun.com/jsf/html" + template="/WEB-INF/templates/default.xhtml"> + <ui:define name="title"> + Create Queries Web Application + </ui:define> + <ui:define name="application_name"> + Create Queries Web Application + </ui:define> + <ui:define name="content"> + <h1>Create Queries</h1> + <br class="clear"/> + <h:form id="form"> + <br class="clear"/> + <h1> + <a href="/create-queries/viewPersons.xhtml">View All Users</a> + <br/> + <a href="/create-queries/viewPerson.xhtml">Search Users</a> + </h1> + </h:form> + </ui:define> +</ui:composition> diff --git a/solutions/create-queries/src/main/webapp/resources/css/screen.css b/solutions/create-queries/src/main/webapp/resources/css/screen.css new file mode 100755 index 0000000..0e284d1 --- /dev/null +++ b/solutions/create-queries/src/main/webapp/resources/css/screen.css @@ -0,0 +1,79 @@ +body { + +} + +header { + background-color: #C40016; + height: 150px; + padding: 10px; + width: 95%; + margin: 0 auto; + border: 4px solid #000000; +} + +header .right { + display: inline-block; + float: right; + font-size: 40px; + font-weight: bolder; + height: 150px; + padding: 48px; + vertical-align: middle; +} + +#content { + background-color: #F2F2F2; + width: 91%; + margin: 0 auto; + padding: 100px 50px; + border: 2px solid #000000; +} + +footer { + border: 4px solid #000000; + background-color: #C40016; + height: 30px; + padding: 10px; + width: 95%; + margin: 0 auto; + text-align: center; + font-weight: bolder; + font-size: 27px; +} + +label{ + display: inline-block; + float: left; + clear: left; + width: 165px; +} +input { + display: inline-block; + float: left; + width: 362px; +} +select{ + display: inline-block; + float: left; + width: 371px; +} +textarea{ + display: inline-block; + float: left; +} + +.clear{ + clear: both; +} + +.messages{ + color: green; + font-weight: bolder; +} + +.btn{ + display: inline-block; + float: left; + clear: left; + width: 165px; +} \ No newline at end of file diff --git a/solutions/create-queries/src/main/webapp/resources/images/redhat.png b/solutions/create-queries/src/main/webapp/resources/images/redhat.png new file mode 100755 index 0000000..0984beb --- /dev/null +++ b/solutions/create-queries/src/main/webapp/resources/images/redhat.png Binary files differ diff --git a/solutions/create-queries/src/main/webapp/viewPerson.xhtml b/solutions/create-queries/src/main/webapp/viewPerson.xhtml new file mode 100755 index 0000000..8ed91ba --- /dev/null +++ b/solutions/create-queries/src/main/webapp/viewPerson.xhtml @@ -0,0 +1,43 @@ +<?xml version="1.0" encoding="UTF-8"?> + +<ui:composition xmlns="http://www.w3.org/1999/xhtml" + xmlns:ui="http://java.sun.com/jsf/facelets" + xmlns:f="http://java.sun.com/jsf/core" + xmlns:h="http://java.sun.com/jsf/html" + template="/WEB-INF/templates/default.xhtml"> + <ui:define name="title"> + Create Queries + </ui:define> + <ui:define name="application_name"> + Create Queries + </ui:define> + <ui:define name="content"> + <h1>Search Users</h1> + <br class="clear"/> + <h:form id="viewform"> + <p class="input"> + <h:outputLabel value="Name:" for="name" /> + <h:inputText value="#{hello.name}" id="name" required="true" requiredMessage="Name is required"/> + </p> + <br class="clear"/> + <br class="clear"/> + <p class="input"> + <h:commandButton action="#{hello.search}" value="Submit" styleClass="btn" /> + </p> + + <h:outputLabel value="Results:"/><br/> + <br class="clear"/> + <ul> + <ui:repeat value="#{hello.results}" var="person"> + <li> + <h:outputText value="#{person.id} -- #{person.name}"/> + + <br/> + + </li> + </ui:repeat> + </ul> + <a href="/create-queries/index.xhtml">Back</a> + </h:form> + </ui:define> +</ui:composition> diff --git a/solutions/create-queries/src/main/webapp/viewPersons.xhtml b/solutions/create-queries/src/main/webapp/viewPersons.xhtml new file mode 100755 index 0000000..fd7913c --- /dev/null +++ b/solutions/create-queries/src/main/webapp/viewPersons.xhtml @@ -0,0 +1,32 @@ +<?xml version="1.0" encoding="UTF-8"?> + +<ui:composition xmlns="http://www.w3.org/1999/xhtml" + xmlns:ui="http://java.sun.com/jsf/facelets" + xmlns:f="http://java.sun.com/jsf/core" + xmlns:h="http://java.sun.com/jsf/html" + template="/WEB-INF/templates/default.xhtml"> + <ui:define name="title"> + Create Queries + </ui:define> + <ui:define name="application_name"> + Create Queries + </ui:define> + <ui:define name="content"> + <h1>User Log</h1> + <br class="clear"/> + <h:form id="viewform"> + + <h:outputLabel value="Users:"/> + <br class="clear"/> + <ul> + <ui:repeat value="#{hello.persons}" var="person"> + <li> + <h:outputText value="#{person.id} -- #{person.name}"/><br/> + + </li> + </ui:repeat> + </ul> + <a href="/create-queries/index.xhtml">Back</a> + </h:form> + </ui:define> +</ui:composition> diff --git a/solutions/persist-entity/.gitignore b/solutions/persist-entity/.gitignore new file mode 100644 index 0000000..d0152b8 --- /dev/null +++ b/solutions/persist-entity/.gitignore @@ -0,0 +1,5 @@ +.classpath +.project +.tern-project +.settings +target diff --git a/solutions/persist-entity/README.md b/solutions/persist-entity/README.md new file mode 100644 index 0000000..56cae66 --- /dev/null +++ b/solutions/persist-entity/README.md @@ -0,0 +1,12 @@ +# JB125 Java EE 7 Hello World web App + +##This app is composed of the following architecture: + +* Maven Project - builds a WAR +* Using JSF 2.2 for web interface, HTML5 facelets page, JSF backing beans, CDI scopes on beans, @Named +* Statelss EJB for services - uses JPA entity beans - exposes appropriate method as REST API using JAX-RS annotations +* JAX-RS Application class +* JPA entity bean to store names - uses H2 in-memory database +* Use EJB Singleton to lookup messaging connection and queue destination and inject into stateless EJB - lookup should only occur once on startup +* Servlet that returns "OK" - this is a health check - path should be [context]/health +* H2 (embedded in-memory DB) backend diff --git a/solutions/persist-entity/pom.xml b/solutions/persist-entity/pom.xml new file mode 100644 index 0000000..bb61927 --- /dev/null +++ b/solutions/persist-entity/pom.xml @@ -0,0 +1,148 @@ +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + <artifactId>persist-entity-solution</artifactId> + <packaging>war</packaging> + <name>Persist Entity web app Project</name> + <description>This is the persist-entity project</description> + <parent> + <groupId>com.redhat.training</groupId> + <artifactId>parent-pom</artifactId> + <version>1.0</version> + <relativePath>../pom.xml</relativePath> + </parent> + + <properties> + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> + </properties> + + <dependencies> + + <!-- First declare the APIs we depend on and need for compilation. All + of them are provided by JBoss EAP --> + + <!-- Import the CDI API, we use provided scope as the API is included in + JBoss EAP --> + <dependency> + <groupId>javax.enterprise</groupId> + <artifactId>cdi-api</artifactId> + <scope>provided</scope> + </dependency> + + <!-- Import the Common Annotations API (JSR-250), we use provided scope + as the API is included in JBoss EAP --> + <dependency> + <groupId>org.jboss.spec.javax.annotation</groupId> + <artifactId>jboss-annotations-api_1.2_spec</artifactId> + <scope>provided</scope> + </dependency> + + <!-- Import the JAX-RS API, we use provided scope as the API is included + in JBoss EAP --> + <dependency> + <groupId>org.jboss.spec.javax.ws.rs</groupId> + <artifactId>jboss-jaxrs-api_2.0_spec</artifactId> + <scope>provided</scope> + </dependency> + + <!-- Import the JPA API, we use provided scope as the API is included in + JBoss EAP --> + <dependency> + <groupId>org.hibernate.javax.persistence</groupId> + <artifactId>hibernate-jpa-2.1-api</artifactId> + <scope>provided</scope> + </dependency> + + <!-- Import the EJB API, we use provided scope as the API is included in + JBoss EAP --> + <dependency> + <groupId>org.jboss.spec.javax.ejb</groupId> + <artifactId>jboss-ejb-api_3.2_spec</artifactId> + <scope>provided</scope> + </dependency> + + <dependency> + <groupId>org.jboss.spec.javax.jms</groupId> + <artifactId>jboss-jms-api_2.0_spec</artifactId> + <scope>provided</scope> + </dependency> + + <!-- Bean Validation Implementation --> + <!-- Provides portable constraints such as @Email --> + <!-- Hibernate Validator is shipped in JBoss EAP --> + <dependency> + <groupId>org.hibernate</groupId> + <artifactId>hibernate-validator</artifactId> + <scope>provided</scope> + <exclusions> + <exclusion> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-api</artifactId> + </exclusion> + </exclusions> + </dependency> + + <dependency> + <groupId>org.hibernate</groupId> + <artifactId>hibernate-search-orm</artifactId> + <scope>provided</scope> + </dependency> + + <dependency> + <groupId>org.hibernate</groupId> + <artifactId>hibernate-entitymanager</artifactId> + <scope>provided</scope> + </dependency> + + <!-- Import the JSF API, we use provided scope as the API is included in + JBoss EAP --> + <dependency> + <groupId>org.jboss.spec.javax.faces</groupId> + <artifactId>jboss-jsf-api_2.2_spec</artifactId> + <scope>provided</scope> + </dependency> + + <dependency> + <groupId>org.jboss.spec.javax.servlet</groupId> + <artifactId>jboss-servlet-api_3.1_spec</artifactId> + <scope>provided</scope> + </dependency> + + <!-- Annotation processor to generate the JPA metamodel classes for + typesafe criteria queries --> + <dependency> + <groupId>org.hibernate</groupId> + <artifactId>hibernate-jpamodelgen</artifactId> + <scope>provided</scope> + </dependency> + + <!-- Annotation processor that raising compilation errors whenever constraint + annotations are incorrectly used. --> + <dependency> + <groupId>org.hibernate</groupId> + <artifactId>hibernate-validator-annotation-processor</artifactId> + <scope>provided</scope> + </dependency> + + </dependencies> + + <build> + <finalName>persist-entity</finalName> + <plugins> + <plugin> + <artifactId>maven-war-plugin</artifactId> + <version>${version.war.plugin}</version> + <extensions>false</extensions> + <configuration> + <failOnMissingWebXml>false</failOnMissingWebXml> + <archive> + <manifestEntries> + <Dependencies>com.google.guava,org.slf4j + </Dependencies> + </manifestEntries> + </archive> + </configuration> + </plugin> + </plugins> + </build> + +</project> diff --git a/solutions/persist-entity/src/main/java/com/redhat/training/model/Person.java b/solutions/persist-entity/src/main/java/com/redhat/training/model/Person.java new file mode 100644 index 0000000..230c0b8 --- /dev/null +++ b/solutions/persist-entity/src/main/java/com/redhat/training/model/Person.java @@ -0,0 +1,66 @@ +package com.redhat.training.model; + +//add required libraries +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Column; + +import java.io.Serializable; + +//add @Entity annotation here +@Entity +public class Person implements Serializable{ +//add annotations for primary key + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + +//add @Column(name="name") annotation to map column in database table + @Column(name="name") + private String personName; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getPersonName() { + return personName; + } + + public void setName(String personName) { + this.personName = personName; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((id == null) ? 0 : id.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Person other = (Person) obj; + if (id == null) { + if (other.id != null) + return false; + } else if (!id.equals(other.id)) + return false; + return true; + } + + +} diff --git a/solutions/persist-entity/src/main/java/com/redhat/training/services/PersonService.java b/solutions/persist-entity/src/main/java/com/redhat/training/services/PersonService.java new file mode 100644 index 0000000..faa1178 --- /dev/null +++ b/solutions/persist-entity/src/main/java/com/redhat/training/services/PersonService.java @@ -0,0 +1,62 @@ +package com.redhat.training.services; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.List; + +import javax.ejb.EJBException; +import javax.ejb.Stateless; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import javax.persistence.TypedQuery; + +import com.redhat.training.model.Person; + +@Stateless + +public class PersonService { + //TODO: obtain an EntityManager instance using @PersistenceContext + @PersistenceContext(unitName="hello") + private EntityManager entityManager; + + // Simple non-RESTy method for JSF bean invocation + public String hello(String name) { + try { + // let's grab the current date and time on the server + LocalDateTime today = LocalDateTime.now(); + + // format it nicely for on-screen display + DateTimeFormatter format = DateTimeFormatter.ofPattern("MMM dd yyyy hh:mm:ss a"); + String fdate = today.format(format); + + // Create a new Person object and persist to database + Person p = new Person(); + p.setName(name); + // call persist() method of entity manager to save the data + entityManager.persist(p); + + // respond back with Hello and convert the name to UPPERCASE. Also, send the + // current time on the server. + return "Hello " + name.toUpperCase() + "!. " + "Time on the server is: " + fdate; + } catch (Exception e) { + throw new EJBException(e); + } + } + + // TODO:add public String getPerson(Long id) method here to fetch result + //by Person id using find() method + + public String getPerson(Long id) { + + return entityManager.find(Person.class, id).getPersonName(); + } + + // Get all Person objects in the Database + public List<Person> getAllPersons() { + TypedQuery<Person> query = entityManager.createQuery("SELECT p FROM Person p", Person.class); + List<Person> persons = query.getResultList(); + + return persons; + } + +} diff --git a/solutions/persist-entity/src/main/java/com/redhat/training/servlet/PingServlet.java b/solutions/persist-entity/src/main/java/com/redhat/training/servlet/PingServlet.java new file mode 100644 index 0000000..d413cdf --- /dev/null +++ b/solutions/persist-entity/src/main/java/com/redhat/training/servlet/PingServlet.java @@ -0,0 +1,60 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2015, Red Hat, Inc. and/or its affiliates, and individual + * contributors by the @authors tag. See the copyright.txt in the + * distribution for a full listing of individual contributors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * 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 CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.redhat.training.servlet; + +import java.io.IOException; +import java.io.PrintWriter; + +import javax.servlet.ServletException; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +/** + * <p> + * A simple servlet taking advantage of features added in 3.0. + * </p> + * + * <p> + * The servlet is registered and mapped to /HelloServlet using the {@linkplain WebServlet + * @HttpServlet}. + * </p> + * + * @author Pete Muir + * + */ +@SuppressWarnings("serial") +@WebServlet("/health") +public class PingServlet extends HttpServlet { + + static String PAGE_HEADER = "<html><head><title>Ping Servlet</title></head><body>"; + + static String PAGE_FOOTER = "</body></html>"; + + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + resp.setContentType("text/html"); + PrintWriter writer = resp.getWriter(); + writer.println(PAGE_HEADER); + writer.println("<h1>OK.</h1>"); + writer.println("It is now " + new java.util.Date().toString() + " at the server."); + writer.println(PAGE_FOOTER); + writer.close(); + } + +} diff --git a/solutions/persist-entity/src/main/java/com/redhat/training/ui/Hello.java b/solutions/persist-entity/src/main/java/com/redhat/training/ui/Hello.java new file mode 100755 index 0000000..faa4eeb --- /dev/null +++ b/solutions/persist-entity/src/main/java/com/redhat/training/ui/Hello.java @@ -0,0 +1,96 @@ +package com.redhat.training.ui; + +import javax.enterprise.context.RequestScoped; +import javax.faces.application.FacesMessage; +import javax.faces.context.FacesContext; +import javax.inject.Named; +import javax.validation.ConstraintViolation; +import javax.validation.ConstraintViolationException; + +import com.redhat.training.model.Person; +import com.redhat.training.services.PersonService; + +import javax.inject.Inject; +import java.util.List; + + + +@RequestScoped +@Named("hello") +public class Hello { + private String name; + private Long id; + + @Inject + private PersonService personService; + + public void sayHello() { + try { + String response = personService.hello(name); + FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(response)); + }catch(Exception e){ + System.out.println(e.getCause()); + if(e.getCause() != null && e.getCause() instanceof ConstraintViolationException) { + ConstraintViolationException ex = (ConstraintViolationException) e.getCause(); + String violations = ""; + for(ConstraintViolation<?> cv: ex.getConstraintViolations()) { + + violations += cv.getMessage() + "\n"; + + System.out.println("Violations: "+violations); + } + FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(violations)); + } + + } + + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public void getPerson() { + try { + String response = personService.getPerson(id); + FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(response)); + }catch(Exception e){ + System.out.println(e.getCause()); + if(e.getCause() != null && e.getCause() instanceof ConstraintViolationException) { + ConstraintViolationException ex = (ConstraintViolationException) e.getCause(); + String violations = ""; + for(ConstraintViolation<?> cv: ex.getConstraintViolations()) { + + violations += cv.getMessage() + "\n"; + + System.out.println("Violations: "+violations); + } + FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(violations)); + } + + } + + } + + + public List<Person> getPersons() { + return personService.getAllPersons(); + + } + + + + +} \ No newline at end of file diff --git a/solutions/persist-entity/src/main/resources/META-INF/persistence.xml b/solutions/persist-entity/src/main/resources/META-INF/persistence.xml new file mode 100644 index 0000000..41466e2 --- /dev/null +++ b/solutions/persist-entity/src/main/resources/META-INF/persistence.xml @@ -0,0 +1,15 @@ +<?xml version="1.0" encoding="UTF-8"?> +<persistence version="2.1" + xmlns="http://xmlns.jcp.org/xml/ns/persistence" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"> + <persistence-unit name="hello" transaction-type="JTA"> + <jta-data-source>java:jboss/datasources/ExampleDS</jta-data-source> + <properties> + <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" /> + <property name="hibernate.hbm2ddl.auto" value="create-drop" /> + <property name="hibernate.show_sql" value="true" /> + <property name="hibernate.format_sql" value="true" /> + </properties> + </persistence-unit> +</persistence> diff --git a/solutions/persist-entity/src/main/webapp/WEB-INF/beans.xml b/solutions/persist-entity/src/main/webapp/WEB-INF/beans.xml new file mode 100755 index 0000000..adcbeda --- /dev/null +++ b/solutions/persist-entity/src/main/webapp/WEB-INF/beans.xml @@ -0,0 +1,25 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + JBoss, Home of Professional Open Source + Copyright 2014, Red Hat, Inc. and/or its affiliates, and individual + contributors by the @authors tag. See the copyright.txt in the + distribution for a full listing of individual contributors. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + http://www.apache.org/licenses/LICENSE-2.0 + 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 CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> +<!-- This file can be an empty text file (0 bytes) --> +<!-- We're declaring the schema to save you time if you do have to configure + this in the future --> +<beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation=" + http://java.sun.com/xml/ns/javaee + http://java.sun.com/xml/ns/javaee/beans_1_0.xsd"> +</beans> diff --git a/solutions/persist-entity/src/main/webapp/WEB-INF/faces-config.xml b/solutions/persist-entity/src/main/webapp/WEB-INF/faces-config.xml new file mode 100755 index 0000000..d7054de --- /dev/null +++ b/solutions/persist-entity/src/main/webapp/WEB-INF/faces-config.xml @@ -0,0 +1,30 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + JBoss, Home of Professional Open Source + Copyright 2014, Red Hat, Inc. and/or its affiliates, and individual + contributors by the @authors tag. See the copyright.txt in the + distribution for a full listing of individual contributors. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + http://www.apache.org/licenses/LICENSE-2.0 + 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 CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> +<!-- This file is not required if you don't need any extra configuration. --> +<faces-config version="2.0" xmlns="http://java.sun.com/xml/ns/javaee" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation=" + http://java.sun.com/xml/ns/javaee + http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"> + + <!-- This descriptor activates the JSF 2.0 Servlet --> + + <!-- Write your navigation rules here. You are encouraged to use CDI + for creating @Named managed beans. --> + +</faces-config> diff --git a/solutions/persist-entity/src/main/webapp/WEB-INF/templates/default.xhtml b/solutions/persist-entity/src/main/webapp/WEB-INF/templates/default.xhtml new file mode 100755 index 0000000..2fe703c --- /dev/null +++ b/solutions/persist-entity/src/main/webapp/WEB-INF/templates/default.xhtml @@ -0,0 +1,34 @@ +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> +<html xmlns="http://www.w3.org/1999/xhtml" + xmlns:h="http://java.sun.com/jsf/html" + xmlns:ui="http://java.sun.com/jsf/facelets"> +<h:head> + <title><ui:insert name="title"> + [Title will be inserted here] + </ui:insert> + </title> + <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> + <h:outputStylesheet name="css/screen.css" /> +</h:head> +<h:body> + + <header> + <h:graphicImage name="images/redhat.png" /> + <div class="right"> + Application: + <ui:insert name="application_name"> + [Application name will be inserted here] + </ui:insert> + </div> + </header> + <div id="content"> + <ui:insert name="content"> + [Template content will be inserted here] + </ui:insert> + </div> + <footer> + JB183 - Enterprise Java Development - Red Hat Training + </footer> +</h:body> +</html> diff --git a/solutions/persist-entity/src/main/webapp/index.html b/solutions/persist-entity/src/main/webapp/index.html new file mode 100755 index 0000000..684299b --- /dev/null +++ b/solutions/persist-entity/src/main/webapp/index.html @@ -0,0 +1,23 @@ +<!-- + JBoss, Home of Professional Open Source + Copyright 2014, Red Hat, Inc. and/or its affiliates, and individual + contributors by the @authors tag. See the copyright.txt in the + distribution for a full listing of individual contributors. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + http://www.apache.org/licenses/LICENSE-2.0 + 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 CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> +<!-- Plain HTML page that kicks us into the app --> + +<html> +<head> +<meta http-equiv="Refresh" content="0; URL=index.jsf"> +</head> +</html> diff --git a/solutions/persist-entity/src/main/webapp/index.xhtml b/solutions/persist-entity/src/main/webapp/index.xhtml new file mode 100755 index 0000000..04c5bb3 --- /dev/null +++ b/solutions/persist-entity/src/main/webapp/index.xhtml @@ -0,0 +1,34 @@ +<?xml version="1.0" encoding="UTF-8"?> + +<ui:composition xmlns="http://www.w3.org/1999/xhtml" + xmlns:ui="http://java.sun.com/jsf/facelets" + xmlns:f="http://java.sun.com/jsf/core" + xmlns:h="http://java.sun.com/jsf/html" + template="/WEB-INF/templates/default.xhtml"> + <ui:define name="title"> + Persist Entity web app + </ui:define> + <ui:define name="application_name"> + Persist Entity web app + </ui:define> + <ui:define name="content"> + <h1>Persist Entity web app</h1> + <br class="clear"/> + <h:form id="form"> + <p class="input"> + <h:outputLabel value="Enter your name:" for="name" /> + <h:inputText value="#{hello.name}" id="name" required="true" requiredMessage="Name is required"/> + </p> + <br class="clear"/> + <br class="clear"/> + <p class="input"> + <h:commandButton action="#{hello.sayHello()}" value="Submit" styleClass="btn" /> + </p> + <br class="clear"/> + <br class="clear"/> + <h:messages styleClass="messages"/> + <a href="/persist-entity/viewPersons.xhtml">View all names</a> + <a href="/persist-entity/viewPerson.xhtml">Search a name</a> + </h:form> + </ui:define> +</ui:composition> diff --git a/solutions/persist-entity/src/main/webapp/resources/css/screen.css b/solutions/persist-entity/src/main/webapp/resources/css/screen.css new file mode 100755 index 0000000..0e284d1 --- /dev/null +++ b/solutions/persist-entity/src/main/webapp/resources/css/screen.css @@ -0,0 +1,79 @@ +body { + +} + +header { + background-color: #C40016; + height: 150px; + padding: 10px; + width: 95%; + margin: 0 auto; + border: 4px solid #000000; +} + +header .right { + display: inline-block; + float: right; + font-size: 40px; + font-weight: bolder; + height: 150px; + padding: 48px; + vertical-align: middle; +} + +#content { + background-color: #F2F2F2; + width: 91%; + margin: 0 auto; + padding: 100px 50px; + border: 2px solid #000000; +} + +footer { + border: 4px solid #000000; + background-color: #C40016; + height: 30px; + padding: 10px; + width: 95%; + margin: 0 auto; + text-align: center; + font-weight: bolder; + font-size: 27px; +} + +label{ + display: inline-block; + float: left; + clear: left; + width: 165px; +} +input { + display: inline-block; + float: left; + width: 362px; +} +select{ + display: inline-block; + float: left; + width: 371px; +} +textarea{ + display: inline-block; + float: left; +} + +.clear{ + clear: both; +} + +.messages{ + color: green; + font-weight: bolder; +} + +.btn{ + display: inline-block; + float: left; + clear: left; + width: 165px; +} \ No newline at end of file diff --git a/solutions/persist-entity/src/main/webapp/resources/images/redhat.png b/solutions/persist-entity/src/main/webapp/resources/images/redhat.png new file mode 100755 index 0000000..0984beb --- /dev/null +++ b/solutions/persist-entity/src/main/webapp/resources/images/redhat.png Binary files differ diff --git a/solutions/persist-entity/src/main/webapp/viewPerson.xhtml b/solutions/persist-entity/src/main/webapp/viewPerson.xhtml new file mode 100755 index 0000000..89fab2e --- /dev/null +++ b/solutions/persist-entity/src/main/webapp/viewPerson.xhtml @@ -0,0 +1,33 @@ +<?xml version="1.0" encoding="UTF-8"?> + +<ui:composition xmlns="http://www.w3.org/1999/xhtml" + xmlns:ui="http://java.sun.com/jsf/facelets" + xmlns:f="http://java.sun.com/jsf/core" + xmlns:h="http://java.sun.com/jsf/html" + template="/WEB-INF/templates/default.xhtml"> + <ui:define name="title"> + Persist Entity web app + </ui:define> + <ui:define name="application_name"> + Persist Entity web app + </ui:define> + <ui:define name="content"> + <h1>Persist Entity web app</h1> + <br class="clear"/> + <h:form id="Personform"> + <p class="input"> + <h:outputLabel value="Enter Id:" for="id" /> + <h:inputText value="#{hello.id}" id="id" required="true" requiredMessage="Id is required"/> + </p> + <br class="clear"/> + <br class="clear"/> + <p class="input"> + <h:commandButton action="#{hello.getPerson()}" value="Submit" styleClass="btn" /> + </p> + <br class="clear"/> + <br class="clear"/> + <h:messages styleClass="messages"/> + <a href="/persist-entity/index.xhtml">Back</a> + </h:form> + </ui:define> +</ui:composition> diff --git a/solutions/persist-entity/src/main/webapp/viewPersons.xhtml b/solutions/persist-entity/src/main/webapp/viewPersons.xhtml new file mode 100755 index 0000000..660d662 --- /dev/null +++ b/solutions/persist-entity/src/main/webapp/viewPersons.xhtml @@ -0,0 +1,32 @@ +<?xml version="1.0" encoding="UTF-8"?> + +<ui:composition xmlns="http://www.w3.org/1999/xhtml" + xmlns:ui="http://java.sun.com/jsf/facelets" + xmlns:f="http://java.sun.com/jsf/core" + xmlns:h="http://java.sun.com/jsf/html" + template="/WEB-INF/templates/default.xhtml"> + <ui:define name="title"> + All who said hello + </ui:define> + <ui:define name="application_name"> + Say Hello + </ui:define> + <ui:define name="content"> + <h1>These people said hello</h1> + <br class="clear"/> + <h:form id="viewform"> + + <h:outputLabel value="Persons:"/> + <br class="clear"/> + <ul> + <ui:repeat value="#{hello.persons}" var="person"> + <li> + <strong>Name:</strong><h:outputText value="#{person.personName}"/><br/> + + </li> + </ui:repeat> + </ul> + <a href="/persist-entity/index.xhtml">Back</a> + </h:form> + </ui:define> +</ui:composition> -- Gitblit v1.9.3