From 41d5004b9e40b9e29b37c26cbd1c154b97e2fe7a Mon Sep 17 00:00:00 2001 From: Ravi Srinivasan <rsriniva@redhat.com> Date: Mon, 10 Sep 2018 14:31:01 +0200 Subject: [PATCH] Chap 5 labs --- labs/entity-relationships/src/main/webapp/index.xhtml | 43 + solutions/entity-relationships/pom.xml | 161 +++++ solutions/entity-relationships/src/main/java/com/redhat/training/model/UserGroup.java | 78 ++ solutions/entity-relationships/src/main/webapp/WEB-INF/faces-config.xml | 30 solutions/entity-relationships/src/main/webapp/WEB-INF/templates/default.xhtml | 34 + solutions/entity-relationships/src/main/webapp/resources/images/redhat.png | 0 labs/entity-relationships/src/main/webapp/WEB-INF/templates/default.xhtml | 34 + labs/entity-relationships/src/main/webapp/resources/images/redhat.png | 0 solutions/entity-relationships/.gitignore | 5 solutions/entity-relationships/src/main/webapp/WEB-INF/beans.xml | 25 labs/entity-relationships/src/main/java/com/redhat/training/model/User.java | 95 ++ labs/entity-relationships/src/main/java/com/redhat/training/model/Email.java | 77 ++ solutions/entity-relationships/src/main/java/com/redhat/training/ui/SelectItemConverter.java | 53 + labs/entity-relationships/.gitignore | 5 solutions/entity-relationships/src/main/java/com/redhat/training/ui/GroupViewBean.java | 84 ++ solutions/entity-relationships/src/main/webapp/index.html | 23 labs/entity-relationships/mydb.sql | 107 +++ labs/entity-relationships/pom.xml | 161 +++++ labs/entity-relationships/src/main/java/com/redhat/training/ejb/Resources.java | 21 labs/entity-relationships/src/main/webapp/WEB-INF/faces-config.xml | 30 labs/entity-relationships/src/main/java/com/redhat/training/ui/SelectItemConverter.java | 53 + solutions/entity-relationships/src/main/resources/META-INF/persistence.xml | 15 solutions/entity-relationships/mydb.sql | 107 +++ labs/entity-relationships/src/main/java/com/redhat/training/model/UserGroup.java | 79 ++ solutions/entity-relationships/src/main/webapp/index.xhtml | 43 + labs/entity-relationships/src/main/webapp/resources/css/screen.css | 79 ++ labs/entity-relationships/src/main/java/com/redhat/training/ejb/UserBean.java | 28 solutions/entity-relationships/src/main/webapp/resources/css/screen.css | 79 ++ labs/entity-relationships/src/main/resources/META-INF/persistence.xml | 15 solutions/entity-relationships/src/main/java/com/redhat/training/ejb/Resources.java | 21 labs/entity-relationships/src/main/webapp/WEB-INF/beans.xml | 25 labs/entity-relationships/src/main/java/com/redhat/training/ui/GroupViewBean.java | 89 ++ solutions/entity-relationships/src/main/java/com/redhat/training/model/Email.java | 76 ++ labs/entity-relationships/src/main/webapp/index.html | 23 solutions/entity-relationships/src/main/java/com/redhat/training/model/User.java | 95 ++ solutions/entity-relationships/src/main/java/com/redhat/training/ejb/UserBean.java | 26 36 files changed, 1,919 insertions(+), 0 deletions(-) diff --git a/labs/entity-relationships/.gitignore b/labs/entity-relationships/.gitignore new file mode 100644 index 0000000..d0152b8 --- /dev/null +++ b/labs/entity-relationships/.gitignore @@ -0,0 +1,5 @@ +.classpath +.project +.tern-project +.settings +target diff --git a/labs/entity-relationships/mydb.sql b/labs/entity-relationships/mydb.sql new file mode 100644 index 0000000..c8a406c --- /dev/null +++ b/labs/entity-relationships/mydb.sql @@ -0,0 +1,107 @@ +-- MySQL dump 10.14 Distrib 5.5.52-MariaDB, for Linux (x86_64) +-- +-- Host: services Database: todo +-- ------------------------------------------------------ +-- Server version 5.5.52-MariaDB + +/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; +/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; +/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; +/*!40101 SET NAMES utf8 */; +/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */; +/*!40103 SET TIME_ZONE='+00:00' */; +/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; +/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; +/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; +/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; + +USE todo; + +-- +-- Table structure for table `Email` +-- + +DROP TABLE IF EXISTS `Email`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `Email` ( + `id` bigint(20) NOT NULL AUTO_INCREMENT, + `address` varchar(255) DEFAULT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=latin1; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Dumping data for table `Email` +-- + +LOCK TABLES `Email` WRITE; +/*!40000 ALTER TABLE `Email` DISABLE KEYS */; +INSERT INTO `Email` VALUES (1,'development@example.com'),(2,'admin@example.com'),(3,'sales@example.com'),(4,'research@example.com'),(5,'ceo@example.com'),(6,'problems@example.com'),(7,'help-desk@example.com'),(8,'hr@example.com'),(9,'supplies@example.com'); +/*!40000 ALTER TABLE `Email` ENABLE KEYS */; +UNLOCK TABLES; + +-- +-- Table structure for table `User` +-- + +DROP TABLE IF EXISTS `User`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `User` ( + `id` bigint(20) NOT NULL AUTO_INCREMENT, + `name` varchar(255) DEFAULT NULL, + `emailID` bigint(20) DEFAULT NULL, + `groupID` bigint(20) DEFAULT NULL, + PRIMARY KEY (`id`), + KEY `FK4ac5lo10mt8w444x6qyag7noi` (`emailID`), + KEY `FKqw2hxxa638ygi29htecfitk9j` (`groupID`), + CONSTRAINT `FKqw2hxxa638ygi29htecfitk9j` FOREIGN KEY (`groupID`) REFERENCES `UserGroup` (`id`), + CONSTRAINT `FK4ac5lo10mt8w444x6qyag7noi` FOREIGN KEY (`emailID`) REFERENCES `Email` (`id`) +) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=latin1; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Dumping data for table `User` +-- + +LOCK TABLES `User` WRITE; +/*!40000 ALTER TABLE `User` DISABLE KEYS */; +INSERT INTO `User` (`id`,`name`,`emailID`,`groupID`) VALUES (1,'William',1,1),(2,'Rose',2,1),(3,'Pat',3,1),(4,'Rodney',4,2),(5,'Kim',5,2),(6,'Tom',6,2),(7,'Matt',7,3),(8,'George',8,3),(9,'Jean',9,3); +/*!40000 ALTER TABLE `User` ENABLE KEYS */; +UNLOCK TABLES; + +-- +-- Table structure for table `UserGroup` +-- + +DROP TABLE IF EXISTS `UserGroup`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `UserGroup` ( + `id` bigint(20) NOT NULL AUTO_INCREMENT, + `name` varchar(255) DEFAULT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Dumping data for table `UserGroup` +-- + +LOCK TABLES `UserGroup` WRITE; +/*!40000 ALTER TABLE `UserGroup` DISABLE KEYS */; +INSERT INTO `UserGroup` VALUES (1,'dev'),(2,'qa'),(3,'stage'); +/*!40000 ALTER TABLE `UserGroup` ENABLE KEYS */; +UNLOCK TABLES; +/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */; + +/*!40101 SET SQL_MODE=@OLD_SQL_MODE */; +/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; +/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; +/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; +/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; +/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; +/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; + +-- Dump completed on 2017-10-06 8:57:43 diff --git a/labs/entity-relationships/pom.xml b/labs/entity-relationships/pom.xml new file mode 100644 index 0000000..d773af4 --- /dev/null +++ b/labs/entity-relationships/pom.xml @@ -0,0 +1,161 @@ +<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>entity-relationships</artifactId> + <packaging>war</packaging> + <name>Configure Relationships web app Project</name> + <description>This is the entity relationships 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 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> + + + <!-- 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.hibernate.javax.persistence</groupId> + <artifactId>hibernate-jpa-2.1-api</artifactId> + <scope>provided</scope> + </dependency> + + <dependency> + <groupId>org.hibernate</groupId> + <artifactId>hibernate-entitymanager</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> + + + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <scope>test</scope> + </dependency> + + <dependency> + <groupId>org.jboss.arquillian.junit</groupId> + <artifactId>arquillian-junit-container</artifactId> + <scope>test</scope> + </dependency> + + <dependency> + <groupId>org.jboss.arquillian.protocol</groupId> + <artifactId>arquillian-protocol-servlet</artifactId> + <scope>test</scope> + </dependency> + + <!-- Need to explicitly reference this version, or else there will be an + exception due to a bug in Arquilian --> + <dependency> + <groupId>org.wildfly.arquillian</groupId> + <artifactId>wildfly-arquillian-common</artifactId> + <version>2.0.0.Final</version> + </dependency> + + </dependencies> + + <build> + <finalName>entity-relationships</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> + + <profiles> + <profile> + <!-- This profile skips all tests, though you can tune it to run just + unit tests based on a custom pattern --> + <!-- Separate profiles are provided for running all tests, including Arquillian + tests that execute in the specified container --> + <!-- Use this profile if you just want to build a WAR file without running + any unit tests --> + <!-- Run like this: mvn clean package -Pdefault --> + <id>default</id> + <build> + <plugins> + <plugin> + <artifactId>maven-surefire-plugin</artifactId> + <version>${version.surefire.plugin}</version> + <configuration> + <skip>true</skip> + </configuration> + </plugin> + </plugins> + </build> + </profile> + <profile> + <!-- An optional Arquillian testing profile that executes tests in a remote + JBoss EAP instance --> + <!-- Run with: mvn clean test -Parq-wildfly-remote or, simply run: mvn + test --> + <!-- To make it easy to run tests from within JBDS, this profile is set + as active by default --> + <id>arq-wildfly-remote</id> + <activation> + <activeByDefault>true</activeByDefault> + </activation> + <dependencies> + <dependency> + <groupId>org.wildfly.arquillian</groupId> + <artifactId>wildfly-arquillian-container-remote</artifactId> + <scope>test</scope> + </dependency> + </dependencies> + </profile> + </profiles> + +</project> diff --git a/labs/entity-relationships/src/main/java/com/redhat/training/ejb/Resources.java b/labs/entity-relationships/src/main/java/com/redhat/training/ejb/Resources.java new file mode 100644 index 0000000..d308997 --- /dev/null +++ b/labs/entity-relationships/src/main/java/com/redhat/training/ejb/Resources.java @@ -0,0 +1,21 @@ +package com.redhat.training.ejb; + +import java.util.logging.Logger; + +import javax.enterprise.inject.Produces; +import javax.enterprise.inject.spi.InjectionPoint; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; + +public class Resources { + + @Produces + @PersistenceContext + private EntityManager em; + + @Produces + public Logger produceLog(InjectionPoint injectionPoint) { + return Logger.getLogger(injectionPoint.getMember().getDeclaringClass().getName()); + } + +} diff --git a/labs/entity-relationships/src/main/java/com/redhat/training/ejb/UserBean.java b/labs/entity-relationships/src/main/java/com/redhat/training/ejb/UserBean.java new file mode 100644 index 0000000..06f3da6 --- /dev/null +++ b/labs/entity-relationships/src/main/java/com/redhat/training/ejb/UserBean.java @@ -0,0 +1,28 @@ +package com.redhat.training.ejb; + + +import java.util.HashSet; +import java.util.Set; + +import javax.ejb.Stateless; +import javax.inject.Inject; +import javax.persistence.EntityManager; +import javax.persistence.TypedQuery; + +import com.redhat.training.model.UserGroup; + +@Stateless +public class UserBean { + + @Inject + private EntityManager em; + + //TODO Add JOIN FETCH + public Set<UserGroup> getAllUserGroups(){ + TypedQuery<UserGroup> query = em.createQuery("SELECT g FROM UserGroup g" , UserGroup.class); + + return new HashSet<UserGroup>(query.getResultList()); + } + + +} diff --git a/labs/entity-relationships/src/main/java/com/redhat/training/model/Email.java b/labs/entity-relationships/src/main/java/com/redhat/training/model/Email.java new file mode 100644 index 0000000..259c611 --- /dev/null +++ b/labs/entity-relationships/src/main/java/com/redhat/training/model/Email.java @@ -0,0 +1,77 @@ +package com.redhat.training.model; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.OneToOne; + +@Entity +public class Email { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + private String address; + + //TODO map relationship + + private User user; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public User getUser() { + return user; + } + + public void setUser(User user) { + this.user = user; + } + + public String getAddress() { + return address; + } + + public void setAddress(String address) { + this.address = address; + } + + @Override + public String toString() { + return "Email [id=" + id + "]"; + } + + @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; + Email other = (Email) obj; + if (id == null) { + if (other.id != null) + return false; + } else if (!id.equals(other.id)) + return false; + return true; + } + + +} diff --git a/labs/entity-relationships/src/main/java/com/redhat/training/model/User.java b/labs/entity-relationships/src/main/java/com/redhat/training/model/User.java new file mode 100644 index 0000000..45762ba --- /dev/null +++ b/labs/entity-relationships/src/main/java/com/redhat/training/model/User.java @@ -0,0 +1,95 @@ +package com.redhat.training.model; + +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; +import javax.persistence.OneToOne; + +@Entity +public class User { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + private String name; + + //TODO map relationship + + private Email email; + + //TODO map relationship + + private UserGroup userGroup; + + 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; + } + + public UserGroup getDepartment() { + return userGroup; + } + + public void setDepartment(UserGroup userGroup) { + this.userGroup = userGroup; + } + + public Email getEmail() { + return email; + } + + public void setEmail(Email email) { + this.email = email; + } + + @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; + User other = (User) obj; + if (id == null) { + if (other.id != null) + return false; + } else if (!id.equals(other.id)) + return false; + return true; + } + + @Override + public String toString() { + return "User [id=" + id + ", name=" + name + ", group=" + userGroup + ", email=" + email + "]"; + } + + + + + +} diff --git a/labs/entity-relationships/src/main/java/com/redhat/training/model/UserGroup.java b/labs/entity-relationships/src/main/java/com/redhat/training/model/UserGroup.java new file mode 100644 index 0000000..2743dcc --- /dev/null +++ b/labs/entity-relationships/src/main/java/com/redhat/training/model/UserGroup.java @@ -0,0 +1,79 @@ +package com.redhat.training.model; + +import java.util.Set; + +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.OneToMany; + +@Entity +public class UserGroup { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + private String name; + + //TODO map relationship + + private Set<User> users; + + 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; + } + + public Set<User> getUsers() { + return users; + } + + public void setUsers(Set<User> users) { + this.users = users; + } + + @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; + UserGroup other = (UserGroup) obj; + if (id == null) { + if (other.id != null) + return false; + } else if (!id.equals(other.id)) + return false; + return true; + } + + @Override + public String toString() { + return "UserGroup [id=" + id + ", name=" + name + "]"; + } + +} diff --git a/labs/entity-relationships/src/main/java/com/redhat/training/ui/GroupViewBean.java b/labs/entity-relationships/src/main/java/com/redhat/training/ui/GroupViewBean.java new file mode 100644 index 0000000..b7d0f32 --- /dev/null +++ b/labs/entity-relationships/src/main/java/com/redhat/training/ui/GroupViewBean.java @@ -0,0 +1,89 @@ +package com.redhat.training.ui; + + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.logging.Logger; + +import javax.annotation.PostConstruct; +import javax.ejb.Stateless; +import javax.faces.bean.RequestScoped; +import javax.faces.event.ValueChangeEvent; +import javax.inject.Inject; +import javax.inject.Named; + +import com.redhat.training.ejb.UserBean; +import com.redhat.training.model.User; +import com.redhat.training.model.UserGroup; + +@Named("groupView") +@Stateless +@RequestScoped +public class GroupViewBean { + + private Set<UserGroup> groups; + + private UserGroup currentGroup; + + private Set<User> users; + + @Inject + private Logger log; + + @Inject + UserBean peopleBean; + + @PostConstruct + public void init() { + log.info("INIT!!!"); + groups = peopleBean.getAllUserGroups(); + + } + + public void update(ValueChangeEvent event) { + + + UserGroup group = (UserGroup) event.getNewValue(); + users = new HashSet<User>(group.getUsers()); +// users = peopleBean.getUsers(group); + + } + + public Set<UserGroup> getGroups() { + return groups; + } + + public void setGroups(Set<UserGroup> userGroups) { + this.groups = userGroups; + } + + public Set<User> getUsers() { + return users; + } + + public void setUsers(Set<User> users) { + this.users = users; + } + + public UserGroup getCurrentGroup() { + return currentGroup; + } + + public void setCurrentGroup(UserGroup currentGroup) { + this.currentGroup = currentGroup; + } + + public List<User> getUserList(){ + if(users != null) { + return new ArrayList<User>(users); + }else { + return new ArrayList<User>(); + } + } + + + + +} diff --git a/labs/entity-relationships/src/main/java/com/redhat/training/ui/SelectItemConverter.java b/labs/entity-relationships/src/main/java/com/redhat/training/ui/SelectItemConverter.java new file mode 100644 index 0000000..2f7f061 --- /dev/null +++ b/labs/entity-relationships/src/main/java/com/redhat/training/ui/SelectItemConverter.java @@ -0,0 +1,53 @@ +package com.redhat.training.ui; + +import java.util.Set; +import java.util.function.Predicate; + +import javax.faces.component.UIComponent; +import javax.faces.component.UISelectItems; +import javax.faces.context.FacesContext; +import javax.faces.convert.Converter; +import javax.faces.convert.FacesConverter; + +import com.redhat.training.model.UserGroup; + +@FacesConverter(value = "SelectItemToEntityConverter") +public class SelectItemConverter implements Converter { + + @Override + public Object getAsObject(FacesContext ctx, UIComponent comp, String value) { + Object o = null; + if (!(value == null || value.isEmpty())) { + o = this.getSelectedItemAsEntity(comp, value); + } + return o; + } + + @Override + public String getAsString(FacesContext ctx, UIComponent comp, Object value) { + String s = ""; + if (value != null) { + s = ((UserGroup) value).getId().toString(); + } + return s; + } + + private UserGroup getSelectedItemAsEntity(UIComponent comp, String value) { + UserGroup item = null; + + Set<UserGroup> selectItems = null; + for (UIComponent uic : comp.getChildren()) { + if (uic instanceof UISelectItems) { + Long itemId = Long.valueOf(value); + selectItems = (Set<UserGroup>) ((UISelectItems) uic).getValue(); + + if (itemId != null && selectItems != null && !selectItems.isEmpty()) { + Predicate<UserGroup> predicate = i -> i.getId().equals(itemId); + item = selectItems.stream().filter(predicate).findFirst().orElse(null); + } + } + } + + return item; + } +} diff --git a/labs/entity-relationships/src/main/resources/META-INF/persistence.xml b/labs/entity-relationships/src/main/resources/META-INF/persistence.xml new file mode 100644 index 0000000..4e1266f --- /dev/null +++ b/labs/entity-relationships/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="People" transaction-type="JTA"> + <jta-data-source>java:jboss/datasources/MySQLDS</jta-data-source> + <properties> + <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" /> + <property name="hibernate.hbm2ddl.auto" value="update" /> + <property name="hibernate.show_sql" value="true" /> + <property name="hibernate.format_sql" value="true" /> + </properties> + </persistence-unit> +</persistence> diff --git a/labs/entity-relationships/src/main/webapp/WEB-INF/beans.xml b/labs/entity-relationships/src/main/webapp/WEB-INF/beans.xml new file mode 100644 index 0000000..adcbeda --- /dev/null +++ b/labs/entity-relationships/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/entity-relationships/src/main/webapp/WEB-INF/faces-config.xml b/labs/entity-relationships/src/main/webapp/WEB-INF/faces-config.xml new file mode 100644 index 0000000..d7054de --- /dev/null +++ b/labs/entity-relationships/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/entity-relationships/src/main/webapp/WEB-INF/templates/default.xhtml b/labs/entity-relationships/src/main/webapp/WEB-INF/templates/default.xhtml new file mode 100644 index 0000000..2fe703c --- /dev/null +++ b/labs/entity-relationships/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/entity-relationships/src/main/webapp/index.html b/labs/entity-relationships/src/main/webapp/index.html new file mode 100644 index 0000000..684299b --- /dev/null +++ b/labs/entity-relationships/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/entity-relationships/src/main/webapp/index.xhtml b/labs/entity-relationships/src/main/webapp/index.xhtml new file mode 100644 index 0000000..e754a86 --- /dev/null +++ b/labs/entity-relationships/src/main/webapp/index.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"> + Group View web app + </ui:define> + <ui:define name="application_name"> + Group View web app + </ui:define> + <ui:define name="content"> + <h1>Group View web app</h1> + <br class="clear"/> + <h:form id="form"> + <p class="input"> + <h:outputLabel value="Select a Group:" for="group" /> + <h:selectOneMenu id="group" value="#{groupView.currentGroup}" valueChangeListener="#{groupView.update}" onchange="submit()" converter="SelectItemToEntityConverter"> + <f:selectItem itemLabel="Choose one ..." itemValue="#{null}"/> + <f:selectItems value="#{groupView.groups}" var="group" itemValue="#{group}" itemLabel="#{group.name}"/> + </h:selectOneMenu> + </p> + <br class="clear"/> + <br class="clear"/> + <p> + <br class="clear"/> + <br class="clear"/> + <h:outputLabel value="Employees:"/> + <br class="clear"/> + <ul> + <ui:repeat value="#{groupView.userList}" var="person"> + <li> + <strong>Name:</strong><h:outputText value="#{person.name}"/><br/> + <strong>Email:</strong><h:outputText value="#{person.email.address}"/> + </li> + </ui:repeat> + </ul> + </p> + </h:form> + </ui:define> +</ui:composition> diff --git a/labs/entity-relationships/src/main/webapp/resources/css/screen.css b/labs/entity-relationships/src/main/webapp/resources/css/screen.css new file mode 100644 index 0000000..0e284d1 --- /dev/null +++ b/labs/entity-relationships/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/entity-relationships/src/main/webapp/resources/images/redhat.png b/labs/entity-relationships/src/main/webapp/resources/images/redhat.png new file mode 100644 index 0000000..0984beb --- /dev/null +++ b/labs/entity-relationships/src/main/webapp/resources/images/redhat.png Binary files differ diff --git a/solutions/entity-relationships/.gitignore b/solutions/entity-relationships/.gitignore new file mode 100644 index 0000000..d0152b8 --- /dev/null +++ b/solutions/entity-relationships/.gitignore @@ -0,0 +1,5 @@ +.classpath +.project +.tern-project +.settings +target diff --git a/solutions/entity-relationships/mydb.sql b/solutions/entity-relationships/mydb.sql new file mode 100644 index 0000000..9d044b4 --- /dev/null +++ b/solutions/entity-relationships/mydb.sql @@ -0,0 +1,107 @@ +-- MySQL dump 10.14 Distrib 5.5.52-MariaDB, for Linux (x86_64) +-- +-- Host: services Database: todo +-- ------------------------------------------------------ +-- Server version 5.5.52-MariaDB + +/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; +/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; +/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; +/*!40101 SET NAMES utf8 */; +/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */; +/*!40103 SET TIME_ZONE='+00:00' */; +/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; +/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; +/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; +/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; + +-- +-- Table structure for table `Email` +-- + +USE todo; + +DROP TABLE IF EXISTS `Email`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `Email` ( + `id` bigint(20) NOT NULL AUTO_INCREMENT, + `address` varchar(255) DEFAULT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=latin1; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Dumping data for table `Email` +-- + +LOCK TABLES `Email` WRITE; +/*!40000 ALTER TABLE `Email` DISABLE KEYS */; +INSERT INTO `Email` VALUES (1,'development@example.com'),(2,'admin@example.com'),(3,'sales@example.com'),(4,'research@example.com'),(5,'ceo@example.com'),(6,'problems@example.com'),(7,'help-desk@example.com'),(8,'hr@example.com'),(9,'supplies@example.com'); +/*!40000 ALTER TABLE `Email` ENABLE KEYS */; +UNLOCK TABLES; + +-- +-- Table structure for table `User` +-- + +DROP TABLE IF EXISTS `User`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `User` ( + `id` bigint(20) NOT NULL AUTO_INCREMENT, + `name` varchar(255) DEFAULT NULL, + `emailID` bigint(20) DEFAULT NULL, + `groupID` bigint(20) DEFAULT NULL, + PRIMARY KEY (`id`), + KEY `FK4ac5lo10mt8w444x6qyag7noi` (`emailID`), + KEY `FKqw2hxxa638ygi29htecfitk9j` (`groupID`), + CONSTRAINT `FKqw2hxxa638ygi29htecfitk9j` FOREIGN KEY (`groupID`) REFERENCES `UserGroup` (`id`), + CONSTRAINT `FK4ac5lo10mt8w444x6qyag7noi` FOREIGN KEY (`emailID`) REFERENCES `Email` (`id`) +) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=latin1; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Dumping data for table `User` +-- + +LOCK TABLES `User` WRITE; +/*!40000 ALTER TABLE `User` DISABLE KEYS */; +INSERT INTO `User` (`id`,`name`,`emailID`,`groupID`) VALUES (1,'William',1,1),(2,'Rose',2,1),(3,'Pat',3,1),(4,'Rodney',4,2),(5,'Kim',5,2),(6,'Tom',6,2),(7,'Matt',7,3),(8,'George',8,3),(9,'Jean',9,3); +/*!40000 ALTER TABLE `User` ENABLE KEYS */; +UNLOCK TABLES; + +-- +-- Table structure for table `UserGroup` +-- + +DROP TABLE IF EXISTS `UserGroup`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `UserGroup` ( + `id` bigint(20) NOT NULL AUTO_INCREMENT, + `name` varchar(255) DEFAULT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Dumping data for table `UserGroup` +-- + +LOCK TABLES `UserGroup` WRITE; +/*!40000 ALTER TABLE `UserGroup` DISABLE KEYS */; +INSERT INTO `UserGroup` VALUES (1,'dev'),(2,'qa'),(3,'stage'); +/*!40000 ALTER TABLE `UserGroup` ENABLE KEYS */; +UNLOCK TABLES; +/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */; + +/*!40101 SET SQL_MODE=@OLD_SQL_MODE */; +/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; +/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; +/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; +/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; +/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; +/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; + +-- Dump completed on 2017-10-06 8:57:43 diff --git a/solutions/entity-relationships/pom.xml b/solutions/entity-relationships/pom.xml new file mode 100644 index 0000000..89303f4 --- /dev/null +++ b/solutions/entity-relationships/pom.xml @@ -0,0 +1,161 @@ +<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>entity-relationships-solution</artifactId> + <packaging>war</packaging> + <name>Configure Relationships web app Project</name> + <description>This is the entity relationships 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 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> + + + <!-- 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.hibernate.javax.persistence</groupId> + <artifactId>hibernate-jpa-2.1-api</artifactId> + <scope>provided</scope> + </dependency> + + <dependency> + <groupId>org.hibernate</groupId> + <artifactId>hibernate-entitymanager</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> + + + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <scope>test</scope> + </dependency> + + <dependency> + <groupId>org.jboss.arquillian.junit</groupId> + <artifactId>arquillian-junit-container</artifactId> + <scope>test</scope> + </dependency> + + <dependency> + <groupId>org.jboss.arquillian.protocol</groupId> + <artifactId>arquillian-protocol-servlet</artifactId> + <scope>test</scope> + </dependency> + + <!-- Need to explicitly reference this version, or else there will be an + exception due to a bug in Arquilian --> + <dependency> + <groupId>org.wildfly.arquillian</groupId> + <artifactId>wildfly-arquillian-common</artifactId> + <version>2.0.0.Final</version> + </dependency> + + </dependencies> + + <build> + <finalName>entity-relationships</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> + + <profiles> + <profile> + <!-- This profile skips all tests, though you can tune it to run just + unit tests based on a custom pattern --> + <!-- Separate profiles are provided for running all tests, including Arquillian + tests that execute in the specified container --> + <!-- Use this profile if you just want to build a WAR file without running + any unit tests --> + <!-- Run like this: mvn clean package -Pdefault --> + <id>default</id> + <build> + <plugins> + <plugin> + <artifactId>maven-surefire-plugin</artifactId> + <version>${version.surefire.plugin}</version> + <configuration> + <skip>true</skip> + </configuration> + </plugin> + </plugins> + </build> + </profile> + <profile> + <!-- An optional Arquillian testing profile that executes tests in a remote + JBoss EAP instance --> + <!-- Run with: mvn clean test -Parq-wildfly-remote or, simply run: mvn + test --> + <!-- To make it easy to run tests from within JBDS, this profile is set + as active by default --> + <id>arq-wildfly-remote</id> + <activation> + <activeByDefault>true</activeByDefault> + </activation> + <dependencies> + <dependency> + <groupId>org.wildfly.arquillian</groupId> + <artifactId>wildfly-arquillian-container-remote</artifactId> + <scope>test</scope> + </dependency> + </dependencies> + </profile> + </profiles> + +</project> diff --git a/solutions/entity-relationships/src/main/java/com/redhat/training/ejb/Resources.java b/solutions/entity-relationships/src/main/java/com/redhat/training/ejb/Resources.java new file mode 100644 index 0000000..d308997 --- /dev/null +++ b/solutions/entity-relationships/src/main/java/com/redhat/training/ejb/Resources.java @@ -0,0 +1,21 @@ +package com.redhat.training.ejb; + +import java.util.logging.Logger; + +import javax.enterprise.inject.Produces; +import javax.enterprise.inject.spi.InjectionPoint; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; + +public class Resources { + + @Produces + @PersistenceContext + private EntityManager em; + + @Produces + public Logger produceLog(InjectionPoint injectionPoint) { + return Logger.getLogger(injectionPoint.getMember().getDeclaringClass().getName()); + } + +} diff --git a/solutions/entity-relationships/src/main/java/com/redhat/training/ejb/UserBean.java b/solutions/entity-relationships/src/main/java/com/redhat/training/ejb/UserBean.java new file mode 100644 index 0000000..5c1efc8 --- /dev/null +++ b/solutions/entity-relationships/src/main/java/com/redhat/training/ejb/UserBean.java @@ -0,0 +1,26 @@ +package com.redhat.training.ejb; + +import java.util.HashSet; +import java.util.Set; + +import javax.ejb.Stateless; +import javax.inject.Inject; +import javax.persistence.EntityManager; +import javax.persistence.TypedQuery; + +import com.redhat.training.model.UserGroup; + +@Stateless +public class UserBean { + + @Inject + private EntityManager em; + + public Set<UserGroup> getAllUserGroups(){ + TypedQuery<UserGroup> query = em.createQuery("SELECT g FROM UserGroup g JOIN FETCH g.users" , UserGroup.class); + + return new HashSet<UserGroup>(query.getResultList()); + } + + +} diff --git a/solutions/entity-relationships/src/main/java/com/redhat/training/model/Email.java b/solutions/entity-relationships/src/main/java/com/redhat/training/model/Email.java new file mode 100644 index 0000000..0b124da --- /dev/null +++ b/solutions/entity-relationships/src/main/java/com/redhat/training/model/Email.java @@ -0,0 +1,76 @@ +package com.redhat.training.model; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.OneToOne; + +@Entity +public class Email { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + private String address; + + @OneToOne(mappedBy="email") + private User user; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public User getUser() { + return user; + } + + public void setUser(User user) { + this.user = user; + } + + public String getAddress() { + return address; + } + + public void setAddress(String address) { + this.address = address; + } + + @Override + public String toString() { + return "Email [id=" + id + "]"; + } + + @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; + Email other = (Email) obj; + if (id == null) { + if (other.id != null) + return false; + } else if (!id.equals(other.id)) + return false; + return true; + } + + +} diff --git a/solutions/entity-relationships/src/main/java/com/redhat/training/model/User.java b/solutions/entity-relationships/src/main/java/com/redhat/training/model/User.java new file mode 100644 index 0000000..a13d1d6 --- /dev/null +++ b/solutions/entity-relationships/src/main/java/com/redhat/training/model/User.java @@ -0,0 +1,95 @@ +package com.redhat.training.model; + +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; +import javax.persistence.OneToOne; + +@Entity +public class User { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + private String name; + + @ManyToOne + @JoinColumn(name="groupID") + private UserGroup userGroup; + + @OneToOne + @JoinColumn(name="emailID") + private Email email; + + 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; + } + + public UserGroup getDepartment() { + return userGroup; + } + + public void setDepartment(UserGroup userGroup) { + this.userGroup = userGroup; + } + + public Email getEmail() { + return email; + } + + public void setEmail(Email email) { + this.email = email; + } + + @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; + User other = (User) obj; + if (id == null) { + if (other.id != null) + return false; + } else if (!id.equals(other.id)) + return false; + return true; + } + + @Override + public String toString() { + return "User [id=" + id + ", name=" + name + ", group=" + userGroup + ", email=" + email + "]"; + } + + + + + +} diff --git a/solutions/entity-relationships/src/main/java/com/redhat/training/model/UserGroup.java b/solutions/entity-relationships/src/main/java/com/redhat/training/model/UserGroup.java new file mode 100644 index 0000000..3d59498 --- /dev/null +++ b/solutions/entity-relationships/src/main/java/com/redhat/training/model/UserGroup.java @@ -0,0 +1,78 @@ +package com.redhat.training.model; + +import java.util.Set; + +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.OneToMany; + +@Entity +public class UserGroup { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + private String name; + + @OneToMany(mappedBy="userGroup", fetch=FetchType.LAZY) + private Set<User> users; + + 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; + } + + public Set<User> getUsers() { + return users; + } + + public void setUsers(Set<User> users) { + this.users = users; + } + + @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; + UserGroup other = (UserGroup) obj; + if (id == null) { + if (other.id != null) + return false; + } else if (!id.equals(other.id)) + return false; + return true; + } + + @Override + public String toString() { + return "UserGroup [id=" + id + ", name=" + name + "]"; + } + +} diff --git a/solutions/entity-relationships/src/main/java/com/redhat/training/ui/GroupViewBean.java b/solutions/entity-relationships/src/main/java/com/redhat/training/ui/GroupViewBean.java new file mode 100644 index 0000000..99667ef --- /dev/null +++ b/solutions/entity-relationships/src/main/java/com/redhat/training/ui/GroupViewBean.java @@ -0,0 +1,84 @@ +package com.redhat.training.ui; + + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.logging.Logger; + +import javax.annotation.PostConstruct; +import javax.ejb.Stateless; +import javax.faces.bean.RequestScoped; +import javax.faces.event.ValueChangeEvent; +import javax.inject.Inject; +import javax.inject.Named; + +import com.redhat.training.ejb.UserBean; +import com.redhat.training.model.User; +import com.redhat.training.model.UserGroup; + +@Named("groupView") +@Stateless +@RequestScoped +public class GroupViewBean { + + private Set<UserGroup> groups; + + private UserGroup currentGroup; + + private Set<User> users; + + @Inject + private Logger log; + + @Inject + UserBean peopleBean; + + @PostConstruct + public void init() { + log.info("INIT!!!"); + groups = peopleBean.getAllUserGroups(); + + } + + public void update(ValueChangeEvent event) { + + UserGroup group = (UserGroup) event.getNewValue(); + users = new HashSet<User>(group.getUsers()); + + } + + public Set<UserGroup> getGroups() { + return groups; + } + + public void setGroups(Set<UserGroup> userGroups) { + this.groups = userGroups; + } + + public Set<User> getUsers() { + return users; + } + + public void setUsers(Set<User> users) { + this.users = users; + } + + public UserGroup getCurrentGroup() { + return currentGroup; + } + + public void setCurrentGroup(UserGroup currentGroup) { + this.currentGroup = currentGroup; + } + + public List<User> getUserList(){ + if(users != null) { + return new ArrayList<User>(users); + }else { + return new ArrayList<User>(); + } + } + +} diff --git a/solutions/entity-relationships/src/main/java/com/redhat/training/ui/SelectItemConverter.java b/solutions/entity-relationships/src/main/java/com/redhat/training/ui/SelectItemConverter.java new file mode 100644 index 0000000..2f7f061 --- /dev/null +++ b/solutions/entity-relationships/src/main/java/com/redhat/training/ui/SelectItemConverter.java @@ -0,0 +1,53 @@ +package com.redhat.training.ui; + +import java.util.Set; +import java.util.function.Predicate; + +import javax.faces.component.UIComponent; +import javax.faces.component.UISelectItems; +import javax.faces.context.FacesContext; +import javax.faces.convert.Converter; +import javax.faces.convert.FacesConverter; + +import com.redhat.training.model.UserGroup; + +@FacesConverter(value = "SelectItemToEntityConverter") +public class SelectItemConverter implements Converter { + + @Override + public Object getAsObject(FacesContext ctx, UIComponent comp, String value) { + Object o = null; + if (!(value == null || value.isEmpty())) { + o = this.getSelectedItemAsEntity(comp, value); + } + return o; + } + + @Override + public String getAsString(FacesContext ctx, UIComponent comp, Object value) { + String s = ""; + if (value != null) { + s = ((UserGroup) value).getId().toString(); + } + return s; + } + + private UserGroup getSelectedItemAsEntity(UIComponent comp, String value) { + UserGroup item = null; + + Set<UserGroup> selectItems = null; + for (UIComponent uic : comp.getChildren()) { + if (uic instanceof UISelectItems) { + Long itemId = Long.valueOf(value); + selectItems = (Set<UserGroup>) ((UISelectItems) uic).getValue(); + + if (itemId != null && selectItems != null && !selectItems.isEmpty()) { + Predicate<UserGroup> predicate = i -> i.getId().equals(itemId); + item = selectItems.stream().filter(predicate).findFirst().orElse(null); + } + } + } + + return item; + } +} diff --git a/solutions/entity-relationships/src/main/resources/META-INF/persistence.xml b/solutions/entity-relationships/src/main/resources/META-INF/persistence.xml new file mode 100644 index 0000000..4e1266f --- /dev/null +++ b/solutions/entity-relationships/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="People" transaction-type="JTA"> + <jta-data-source>java:jboss/datasources/MySQLDS</jta-data-source> + <properties> + <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" /> + <property name="hibernate.hbm2ddl.auto" value="update" /> + <property name="hibernate.show_sql" value="true" /> + <property name="hibernate.format_sql" value="true" /> + </properties> + </persistence-unit> +</persistence> diff --git a/solutions/entity-relationships/src/main/webapp/WEB-INF/beans.xml b/solutions/entity-relationships/src/main/webapp/WEB-INF/beans.xml new file mode 100644 index 0000000..adcbeda --- /dev/null +++ b/solutions/entity-relationships/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/entity-relationships/src/main/webapp/WEB-INF/faces-config.xml b/solutions/entity-relationships/src/main/webapp/WEB-INF/faces-config.xml new file mode 100644 index 0000000..d7054de --- /dev/null +++ b/solutions/entity-relationships/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/entity-relationships/src/main/webapp/WEB-INF/templates/default.xhtml b/solutions/entity-relationships/src/main/webapp/WEB-INF/templates/default.xhtml new file mode 100644 index 0000000..2fe703c --- /dev/null +++ b/solutions/entity-relationships/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/entity-relationships/src/main/webapp/index.html b/solutions/entity-relationships/src/main/webapp/index.html new file mode 100644 index 0000000..684299b --- /dev/null +++ b/solutions/entity-relationships/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/entity-relationships/src/main/webapp/index.xhtml b/solutions/entity-relationships/src/main/webapp/index.xhtml new file mode 100644 index 0000000..e754a86 --- /dev/null +++ b/solutions/entity-relationships/src/main/webapp/index.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"> + Group View web app + </ui:define> + <ui:define name="application_name"> + Group View web app + </ui:define> + <ui:define name="content"> + <h1>Group View web app</h1> + <br class="clear"/> + <h:form id="form"> + <p class="input"> + <h:outputLabel value="Select a Group:" for="group" /> + <h:selectOneMenu id="group" value="#{groupView.currentGroup}" valueChangeListener="#{groupView.update}" onchange="submit()" converter="SelectItemToEntityConverter"> + <f:selectItem itemLabel="Choose one ..." itemValue="#{null}"/> + <f:selectItems value="#{groupView.groups}" var="group" itemValue="#{group}" itemLabel="#{group.name}"/> + </h:selectOneMenu> + </p> + <br class="clear"/> + <br class="clear"/> + <p> + <br class="clear"/> + <br class="clear"/> + <h:outputLabel value="Employees:"/> + <br class="clear"/> + <ul> + <ui:repeat value="#{groupView.userList}" var="person"> + <li> + <strong>Name:</strong><h:outputText value="#{person.name}"/><br/> + <strong>Email:</strong><h:outputText value="#{person.email.address}"/> + </li> + </ui:repeat> + </ul> + </p> + </h:form> + </ui:define> +</ui:composition> diff --git a/solutions/entity-relationships/src/main/webapp/resources/css/screen.css b/solutions/entity-relationships/src/main/webapp/resources/css/screen.css new file mode 100644 index 0000000..0e284d1 --- /dev/null +++ b/solutions/entity-relationships/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/entity-relationships/src/main/webapp/resources/images/redhat.png b/solutions/entity-relationships/src/main/webapp/resources/images/redhat.png new file mode 100644 index 0000000..0984beb --- /dev/null +++ b/solutions/entity-relationships/src/main/webapp/resources/images/redhat.png Binary files differ -- Gitblit v1.9.3