diff --git a/.gitignore b/.gitignore index 2c1fdb4e1f..5aadb16a76 100644 --- a/.gitignore +++ b/.gitignore @@ -31,3 +31,5 @@ bin .clover *.swp !/wicket-war/src/main/resources/META-INF/MANIFEST.MF + + diff --git a/README.md b/README.md index 9fc89193fa..434e2aefdf 100644 --- a/README.md +++ b/README.md @@ -640,3 +640,4 @@ When instructed to use Byteman to halt the application, perform the following st When you are done testing the quickstart, remember to restore the configuration file with the backup copy you made in step 2 above. + diff --git a/deltaspike-beanmanagerprovider/README.md b/deltaspike-beanmanagerprovider/README.md new file mode 100644 index 0000000000..89ae4356de --- /dev/null +++ b/deltaspike-beanmanagerprovider/README.md @@ -0,0 +1,95 @@ +deltaspike-beanmanagerprovider: Shows how to use DeltaSpike BeanManagerProvider to access CDI in a EntityListener +====================================================== +Author: Rafael Benevides +Level: Intermediate +Technologies: CDI, Deltaspike, JPA, JSF +Summary: Shows how to use DeltaSpike BeanManagerProvider to access CDI in a EntityListener +Prerequisites: +Target Product: WFK + +What is it? +----------- + +This project demonstrates the use of DeltaSpike's BeanManagerProvider. + +`BeanmanagerProvider` provides access to the `BeanManager` by registering the current `BeanManager` during the startup. This is really handy if you like to access CDI functionality from places where no CDI based injection is available. If a simple but manual bean-lookup is needed, it's easier to use the `BeanProvider` instead. + +This projects uses an EntityListener to create audit records. EntityListeners aren't managed by CDI, so you can't inject dependencies. In this project we used `BeanManagerProvider` to get a reference to a Stateless EJB that is responsible to persist the audit records. + + +System requirements +------------------- + +All you need to build this project is Java 6.0 (Java SDK 1.6) or better, Maven 3.0 or better. + +The application this project produces is designed to be run on JBoss Enterprise Application Platform 6 or JBoss AS 7. + + +Configure Maven +--------------- + +If you have not yet done so, you must [Configure Maven](../README.md#mavenconfiguration) before testing the quickstarts. + + +Start JBoss Enterprise Application Platform 6 or JBoss AS 7 +------------------------- + +1. Open a command line and navigate to the root of the JBoss server directory. +2. The following shows the command line to start the server with the web profile: + + For Linux: JBOSS_HOME/bin/standalone.sh + For Windows: JBOSS_HOME\bin\standalone.bat + + +Build and Deploy the Quickstart +------------------------- + +_NOTE: The following build command assumes you have configured your Maven user settings. If you have not, you must include Maven setting arguments on the command line. See [Build and Deploy the Quickstarts](../README.md#buildanddeploy) for complete instructions and additional options._ + +1. Make sure you have started the JBoss Server as described above. +2. Open a command line and navigate to the root directory of this quickstart. +3. Type this command to build and deploy the archive: + + mvn clean package jboss-as:deploy +4. This will deploy `target/jboss-as-deltaspike-beanmanagerprovider.war` to the running instance of the server. + +Access the application +--------------------- + +Access the running application in a browser at the following URL: + +You are presented with a simple form to insert, edit, or remove contacts. + +- To insert a new contact, click the `New Contact` button. Complete the form fields and then click the `Save` button. +- To modify an existing contact, find the name in the *All Contacts* table and click the `Select for edit` button. The *Contacts* form fields are populated with the contact data. Modify the data and then click the `Save` button. +- To remove a contact, find the name in the *All Contacts* table and click the `Remove` button. A dialog appears asking 'Do you want to remove this record?'. Click the `OK` button to remove the contact. + +When you insert a contact, notice that both the *All Contacts* and *Audit Records* tables are updated. +When you edit a contact, notice that the *Audit Records* table is updated with the new contact data. + +Continue to insert, edit, or remove contacts to see registration of audit records. + +_NOTE: To fire the update audit, you must change one of the contact fields_ + + +Undeploy the Archive +-------------------- + +1. Make sure you have started the JBoss Server as described above. +2. Open a command line and navigate to the root directory of this quickstart. +3. When you are finished testing, type this command to undeploy the archive: + + mvn jboss-as:undeploy + +Run the Quickstart in JBoss Developer Studio or Eclipse +------------------------------------- + +You can also start the server and deploy the quickstarts from Eclipse using JBoss tools. For more information, see [Use JBoss Developer Studio or Eclipse to Run the Quickstarts](../README.md#useeclipse) + +Debug the Application +------------------------------------ + +If you want to debug the source code or look at the Javadocs of any library in the project, run either of the following commands to pull them into your local repository. The IDE should then detect them. + + mvn dependency:sources + mvn dependency:resolve -Dclassifier=javadoc diff --git a/deltaspike-beanmanagerprovider/pom.xml b/deltaspike-beanmanagerprovider/pom.xml new file mode 100644 index 0000000000..305b49862e --- /dev/null +++ b/deltaspike-beanmanagerprovider/pom.xml @@ -0,0 +1,190 @@ + + + + 4.0.0 + + org.jboss.as.quickstarts + jboss-as-deltaspike-beanmanagerprovider + 7.1.2-SNAPSHOT + war + JBoss AS Quickstarts: DeltaSpike Beanmanagerprovider + DeltaSpike Beanmanagerprovider: shows how to use DeltaSpike BeanManagerProvider to access CDI in a EntityListener + + http://jboss.org/jbossas + + + Apache License, Version 2.0 + repo + http://www.apache.org/licenses/LICENSE-2.0.html + + + + + + + UTF-8 + + + 7.3.Final + 1.0.3.Final + + + 2.3.1 + 2.1.1 + + + 1.6 + 1.6 + + + + + + + + org.jboss.bom + jboss-javaee-6.0-with-hibernate + ${version.org.jboss.bom} + pom + import + + + org.jboss.bom + jboss-javaee-6.0-with-deltaspike + ${version.org.jboss.bom} + pom + import + + + + + + + + + javax.enterprise + cdi-api + provided + + + + + org.jboss.spec.javax.annotation + jboss-annotations-api_1.1_spec + provided + + + + + org.jboss.spec.javax.faces + jboss-jsf-api_2.1_spec + provided + + + + + org.hibernate.javax.persistence + hibernate-jpa-2.0-api + provided + + + + + org.jboss.spec.javax.transaction + jboss-transaction-api_1.1_spec + provided + + + + + org.jboss.spec.javax.ejb + jboss-ejb-api_3.1_spec + provided + + + + + org.hibernate + hibernate-validator + provided + + + + + org.apache.deltaspike.core + deltaspike-core-api + compile + + + + + org.apache.deltaspike.core + deltaspike-core-impl + runtime + + + + + + ${project.artifactId} + + + maven-war-plugin + ${version.war.plugin} + + + false + + + + + org.jboss.as.plugins + jboss-as-maven-plugin + ${version.org.jboss.as.plugins.maven.plugin} + + + + maven-compiler-plugin + ${version.compiler.plugin} + + ${maven.compiler.source} + ${maven.compiler.target} + + + + + + + diff --git a/deltaspike-beanmanagerprovider/src/main/java/org/jboss/as/quickstarts/deltaspike/beanmanagerprovider/controller/ContactController.java b/deltaspike-beanmanagerprovider/src/main/java/org/jboss/as/quickstarts/deltaspike/beanmanagerprovider/controller/ContactController.java new file mode 100644 index 0000000000..5f71e1a22e --- /dev/null +++ b/deltaspike-beanmanagerprovider/src/main/java/org/jboss/as/quickstarts/deltaspike/beanmanagerprovider/controller/ContactController.java @@ -0,0 +1,147 @@ +/* + * JBoss, Home of Professional Open Source. + * Copyright 2012, Red Hat, Inc., and individual contributors + * as indicated by the @author tags. See the copyright.txt file in the + * distribution for a full listing of individual contributors. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ + +package org.jboss.as.quickstarts.deltaspike.beanmanagerprovider.controller; + +import java.io.Serializable; +import java.util.List; + +import javax.annotation.PostConstruct; +import javax.enterprise.context.Conversation; +import javax.enterprise.context.ConversationScoped; +import javax.enterprise.inject.Model; +import javax.enterprise.inject.Produces; +import javax.faces.application.FacesMessage; +import javax.faces.context.FacesContext; +import javax.inject.Inject; +import javax.inject.Named; + +import org.jboss.as.quickstarts.deltaspike.beanmanagerprovider.model.AuditContact; +import org.jboss.as.quickstarts.deltaspike.beanmanagerprovider.model.Contact; +import org.jboss.as.quickstarts.deltaspike.beanmanagerprovider.persistence.AuditRepository; +import org.jboss.as.quickstarts.deltaspike.beanmanagerprovider.persistence.ContactRepository; + +/** + * + * This class uses {@link ConversationScoped} and {@link Named} instead of {@link Model} because it holds the managed + * {@link Contact} entity instance accross the requests. + * + * @author Rafael Benevides + * + */ +@Named +@ConversationScoped +public class ContactController implements Serializable { + + private static final long serialVersionUID = 1L; + + @Inject + private FacesContext facesContext; + + @Inject + private ContactRepository contactRepository; + + @Inject + private AuditRepository auditRepository; + + @Inject + private Conversation conversation; + + private Contact contact; + + // return the managed entity instance + public Contact getContact() { + return contact; + } + + public void save() { + // Define the faces message based on the entity state + String msg = contact.getId() == null ? "New Contact added" : "Contact updated"; + try { + contactRepository.persist(contact); + } catch (Exception e) { + // discard the conversation (and the entity manager) on any exception + conversation.end(); + msg = e.getMessage(); + } + // add the message to be showed on the jsf page + facesContext.addMessage(null, new FacesMessage(msg)); + } + + public void remove(Contact contact) { + contactRepository.remove(contact); + facesContext.addMessage(null, new FacesMessage("Contact Removed")); + this.contact = new Contact(); + } + + // select an instance from the table to edition + public void selectForEdit(Contact contact) { + this.contact = contact; + } + + /** + * This method will promote the conversation when this component is constructed through the {@link PostConstruct} annotation + * This will also create a new entity to be managed/edited + */ + @PostConstruct + public void newContact() { + if (conversation.isTransient()) { + conversation.begin(); + } + contact = new Contact(); + } + + /** + * Produces the conversation number to be used on the page footer + * + * @return + */ + @Produces + @Named + public String getConversationNumber() { + return "Conversation Id: " + conversation.getId(); + } + + /** + * Produces the information to be used on *All Contacts* table. + * + * + * @return + */ + @Produces + @Named + public List getAllContacts() { + return contactRepository.getAllContacts(); + } + + /** + * Produces the information to be used on *Audit Records* table + * + * @return + */ + @Produces + @Named + public List getAuditRecords() { + return auditRepository.getAllAuditRecords(); + } + +} diff --git a/deltaspike-beanmanagerprovider/src/main/java/org/jboss/as/quickstarts/deltaspike/beanmanagerprovider/model/AuditContact.java b/deltaspike-beanmanagerprovider/src/main/java/org/jboss/as/quickstarts/deltaspike/beanmanagerprovider/model/AuditContact.java new file mode 100644 index 0000000000..47552eacbb --- /dev/null +++ b/deltaspike-beanmanagerprovider/src/main/java/org/jboss/as/quickstarts/deltaspike/beanmanagerprovider/model/AuditContact.java @@ -0,0 +1,133 @@ +/* + * JBoss, Home of Professional Open Source. + * Copyright 2012, Red Hat, Inc., and individual contributors + * as indicated by the @author tags. See the copyright.txt file in the + * distribution for a full listing of individual contributors. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ + +package org.jboss.as.quickstarts.deltaspike.beanmanagerprovider.model; + +import java.util.Date; + +import javax.persistence.Entity; +import javax.persistence.EnumType; +import javax.persistence.Enumerated; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.Table; +import javax.persistence.Temporal; +import javax.persistence.TemporalType; + +/** + * @author Rafael Benevides + * + */ +@Entity +@Table(name = "AuditContact") +public class AuditContact { + + @Id + @GeneratedValue + private Long id; + + private String contactEmail; + + private String contactName; + + private String contactPhone; + + @Enumerated(EnumType.STRING) + private OPERATION operation; + + @Temporal(TemporalType.TIMESTAMP) + private Date dateTime = new Date(); + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public OPERATION getOperation() { + return operation; + } + + public void setOperation(OPERATION operation) { + this.operation = operation; + } + + public void setContactEmail(String contactEmail) { + this.contactEmail = contactEmail; + } + + public String getContactEmail() { + return contactEmail; + } + + public Date getDateTime() { + return dateTime; + } + + public void setDateTime(Date dateTime) { + this.dateTime = dateTime; + } + + public String getContactName() { + return contactName; + } + + public void setContactName(String contactName) { + this.contactName = contactName; + } + + public String getContactPhone() { + return contactPhone; + } + + public void setContactPhone(String contactPhone) { + this.contactPhone = contactPhone; + } + + @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; + AuditContact other = (AuditContact) obj; + if (id == null) { + if (other.id != null) + return false; + } else if (!id.equals(other.id)) + return false; + return true; + } + +} diff --git a/deltaspike-beanmanagerprovider/src/main/java/org/jboss/as/quickstarts/deltaspike/beanmanagerprovider/model/Contact.java b/deltaspike-beanmanagerprovider/src/main/java/org/jboss/as/quickstarts/deltaspike/beanmanagerprovider/model/Contact.java new file mode 100644 index 0000000000..bc392693d8 --- /dev/null +++ b/deltaspike-beanmanagerprovider/src/main/java/org/jboss/as/quickstarts/deltaspike/beanmanagerprovider/model/Contact.java @@ -0,0 +1,131 @@ +/* + * JBoss, Home of Professional Open Source. + * Copyright 2012, Red Hat, Inc., and individual contributors + * as indicated by the @author tags. See the copyright.txt file in the + * distribution for a full listing of individual contributors. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ + +package org.jboss.as.quickstarts.deltaspike.beanmanagerprovider.model; + +import java.io.Serializable; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.EntityListeners; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.Table; +import javax.persistence.UniqueConstraint; +import javax.validation.constraints.Digits; +import javax.validation.constraints.NotNull; +import javax.validation.constraints.Pattern; +import javax.validation.constraints.Size; + +import org.hibernate.validator.constraints.Email; +import org.hibernate.validator.constraints.NotEmpty; +import org.jboss.as.quickstarts.deltaspike.beanmanagerprovider.persistence.AuditContactListener; + +/** + * @author Rafael Benevides + * + */ +@SuppressWarnings("serial") +@Entity +@Table(name = "Contact", uniqueConstraints = @UniqueConstraint(columnNames = "email")) +// Here we register the AuditContactListener class as the listener to entity events +@EntityListeners(AuditContactListener.class) +public class Contact implements Serializable { + + @Id + @GeneratedValue + private Long id; + + @NotNull + @Size(min = 1, max = 25) + @Pattern(regexp = "[A-Za-z ]*", message = "must contain only letters and spaces") + private String name; + + @NotNull + @NotEmpty + @Email + private String email; + + @NotNull + @Size(min = 10, max = 12) + @Digits(fraction = 0, integer = 12) + @Column(name = "phone_number") + private String phoneNumber; + + 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 String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public String getPhoneNumber() { + return phoneNumber; + } + + public void setPhoneNumber(String phoneNumber) { + this.phoneNumber = phoneNumber; + } + + @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; + Contact other = (Contact) obj; + if (id == null) { + if (other.id != null) + return false; + } else if (!id.equals(other.id)) + return false; + return true; + } + +} diff --git a/deltaspike-beanmanagerprovider/src/main/java/org/jboss/as/quickstarts/deltaspike/beanmanagerprovider/model/OPERATION.java b/deltaspike-beanmanagerprovider/src/main/java/org/jboss/as/quickstarts/deltaspike/beanmanagerprovider/model/OPERATION.java new file mode 100644 index 0000000000..31f7aa458c --- /dev/null +++ b/deltaspike-beanmanagerprovider/src/main/java/org/jboss/as/quickstarts/deltaspike/beanmanagerprovider/model/OPERATION.java @@ -0,0 +1,37 @@ +/* + * JBoss, Home of Professional Open Source. + * Copyright 2012, Red Hat, Inc., and individual contributors + * as indicated by the @author tags. See the copyright.txt file in the + * distribution for a full listing of individual contributors. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ + +package org.jboss.as.quickstarts.deltaspike.beanmanagerprovider.model; + +/** + * Database operations type + * + * @author Rafael Benevides + * + */ +public enum OPERATION { + + INSERT, + UPDATE, + DELETE + +} diff --git a/deltaspike-beanmanagerprovider/src/main/java/org/jboss/as/quickstarts/deltaspike/beanmanagerprovider/persistence/AuditContactListener.java b/deltaspike-beanmanagerprovider/src/main/java/org/jboss/as/quickstarts/deltaspike/beanmanagerprovider/persistence/AuditContactListener.java new file mode 100644 index 0000000000..d6f235944e --- /dev/null +++ b/deltaspike-beanmanagerprovider/src/main/java/org/jboss/as/quickstarts/deltaspike/beanmanagerprovider/persistence/AuditContactListener.java @@ -0,0 +1,116 @@ +/* + * JBoss, Home of Professional Open Source. + * Copyright 2012, Red Hat, Inc., and individual contributors + * as indicated by the @author tags. See the copyright.txt file in the + * distribution for a full listing of individual contributors. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ + +package org.jboss.as.quickstarts.deltaspike.beanmanagerprovider.persistence; + +import java.lang.annotation.Annotation; + +import javax.ejb.Stateless; +import javax.enterprise.context.spi.CreationalContext; +import javax.enterprise.inject.spi.Bean; +import javax.enterprise.inject.spi.BeanManager; +import javax.persistence.PostPersist; +import javax.persistence.PostRemove; +import javax.persistence.PostUpdate; + +import org.apache.deltaspike.core.api.provider.BeanManagerProvider; +import org.jboss.as.quickstarts.deltaspike.beanmanagerprovider.model.AuditContact; +import org.jboss.as.quickstarts.deltaspike.beanmanagerprovider.model.Contact; +import org.jboss.as.quickstarts.deltaspike.beanmanagerprovider.model.OPERATION; + +/** + * This Listener is not managed by CDI lifecycle so we cannot inject {@link AuditRepository}. + * + * In this case, we use {@link BeanManagerProvider} to access a {@link AuditRepository} instance + * + * @author Rafael Benevides + * + */ +public class AuditContactListener { + + // Injection does not work because this class is not managed by CDI + // NOTE: JPA 2.1 will support CDI Injection in EntityListener - in Java EE 7 + private AuditRepository auditRepository; + + /** + * This methos gets the {@link BeanManager} from the {@link BeanManagerProvider} and uses it to get a instance of + * {@link AuditRepository} {@link Stateless} EJB + * + * @return EJB reference + */ + @SuppressWarnings({ "rawtypes" }) + public AuditRepository getAuditRepositoryInstance() { + BeanManager bm = BeanManagerProvider.getInstance().getBeanManager(); + Bean bean = bm.resolve(bm.getBeans(AuditRepository.class, new Annotation[] {})); + CreationalContext cc = bm.createCreationalContext(bean); + return (AuditRepository) bm.getReference(bean, AuditRepository.class, cc); + } + + /** + * Method called after {@link Contact} is persisted + * + * @param contact + */ + @PostPersist + public void created(Contact contact) { + auditRepository = getAuditRepositoryInstance(); + AuditContact a = new AuditContact(); + a.setContactEmail(contact.getEmail()); + a.setContactName(contact.getName()); + a.setContactPhone(contact.getPhoneNumber()); + a.setOperation(OPERATION.INSERT); + auditRepository.persist(a); + } + + /** + * Method called after {@link Contact} is updated + * + * @param contact + */ + @PostUpdate + public void updated(Contact contact) { + auditRepository = getAuditRepositoryInstance(); + AuditContact a = new AuditContact(); + a.setContactEmail(contact.getEmail()); + a.setContactName(contact.getName()); + a.setContactPhone(contact.getPhoneNumber()); + a.setOperation(OPERATION.UPDATE); + auditRepository.persist(a); + } + + /** + * Method called after {@link Contact} is removed + * + * @param contact + */ + + @PostRemove + public void removed(Contact contact) { + auditRepository = getAuditRepositoryInstance(); + AuditContact a = new AuditContact(); + a.setContactEmail(contact.getEmail()); + a.setContactName(contact.getName()); + a.setContactPhone(contact.getPhoneNumber()); + a.setOperation(OPERATION.DELETE); + auditRepository.persist(a); + } +} diff --git a/deltaspike-beanmanagerprovider/src/main/java/org/jboss/as/quickstarts/deltaspike/beanmanagerprovider/persistence/AuditRepository.java b/deltaspike-beanmanagerprovider/src/main/java/org/jboss/as/quickstarts/deltaspike/beanmanagerprovider/persistence/AuditRepository.java new file mode 100644 index 0000000000..03dd95a652 --- /dev/null +++ b/deltaspike-beanmanagerprovider/src/main/java/org/jboss/as/quickstarts/deltaspike/beanmanagerprovider/persistence/AuditRepository.java @@ -0,0 +1,73 @@ +/* + * JBoss, Home of Professional Open Source. + * Copyright 2012, Red Hat, Inc., and individual contributors + * as indicated by the @author tags. See the copyright.txt file in the + * distribution for a full listing of individual contributors. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ + +package org.jboss.as.quickstarts.deltaspike.beanmanagerprovider.persistence; + +import java.io.Serializable; +import java.util.List; + +import javax.ejb.Stateless; +import javax.ejb.TransactionAttribute; +import javax.ejb.TransactionAttributeType; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; + +import org.jboss.as.quickstarts.deltaspike.beanmanagerprovider.model.AuditContact; + +/** + * + * This class is responsible for dealing with the persistence of {@link AuditContact} entities + * + * @author Rafael Benevides + * + */ +// This class is {@link Stateless} because there is no need to hold a state +@Stateless +public class AuditRepository implements Serializable { + + private static final long serialVersionUID = 1L; + + @PersistenceContext + private EntityManager entityManager; + + /** + * The persistence of AuditContact is made on a new transaction since this happens after the Contact entity has been + * persisted + * + * @param audit + */ + @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW) + public void persist(AuditContact audit) { + entityManager.persist(audit); + entityManager.flush(); + } + + /** + * Retrieve all AudittRecords from the {@link EntityManager} + * + * @return + */ + @SuppressWarnings("unchecked") + public List getAllAuditRecords() { + return entityManager.createQuery("SELECT a FROM AuditContact a").getResultList(); + } +} diff --git a/deltaspike-beanmanagerprovider/src/main/java/org/jboss/as/quickstarts/deltaspike/beanmanagerprovider/persistence/ContactRepository.java b/deltaspike-beanmanagerprovider/src/main/java/org/jboss/as/quickstarts/deltaspike/beanmanagerprovider/persistence/ContactRepository.java new file mode 100644 index 0000000000..95bfeafc9c --- /dev/null +++ b/deltaspike-beanmanagerprovider/src/main/java/org/jboss/as/quickstarts/deltaspike/beanmanagerprovider/persistence/ContactRepository.java @@ -0,0 +1,80 @@ +/* + * JBoss, Home of Professional Open Source. + * Copyright 2012, Red Hat, Inc., and individual contributors + * as indicated by the @author tags. See the copyright.txt file in the + * distribution for a full listing of individual contributors. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ + +package org.jboss.as.quickstarts.deltaspike.beanmanagerprovider.persistence; + +import java.util.List; + +import javax.ejb.Stateful; +import javax.enterprise.context.ConversationScoped; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import javax.persistence.PersistenceContextType; + +import org.jboss.as.quickstarts.deltaspike.beanmanagerprovider.model.Contact; + +/** + * + * This class is responsible for dealing with the persistence of {@link Contact} entities + * + * @author Rafael Benevides + * + */ +// This class is Stateful because we need to keep the EntityManager opened during the conversation scope. +@Stateful +@ConversationScoped +public class ContactRepository { + + @PersistenceContext(type = PersistenceContextType.EXTENDED) + private EntityManager entityManager; + + /** + * Persit the {@link Contact} + * + * @param contact + */ + public void persist(Contact contact) { + entityManager.persist(contact); + entityManager.flush(); + } + + /** + * Removes the {@link Contact} + * + * @param contact + */ + public void remove(Contact contact) { + entityManager.remove(contact); + entityManager.flush(); + } + + /** + * Retrieve all {@link Contact} + * + * @return + */ + @SuppressWarnings("unchecked") + public List getAllContacts() { + return entityManager.createQuery("SELECT m From Contact m").getResultList(); + } + +} diff --git a/deltaspike-beanmanagerprovider/src/main/java/org/jboss/as/quickstarts/deltaspike/beanmanagerprovider/util/Resources.java b/deltaspike-beanmanagerprovider/src/main/java/org/jboss/as/quickstarts/deltaspike/beanmanagerprovider/util/Resources.java new file mode 100644 index 0000000000..2231e50694 --- /dev/null +++ b/deltaspike-beanmanagerprovider/src/main/java/org/jboss/as/quickstarts/deltaspike/beanmanagerprovider/util/Resources.java @@ -0,0 +1,64 @@ +/* + * JBoss, Home of Professional Open Source. + * Copyright 2012, Red Hat, Inc., and individual contributors + * as indicated by the @author tags. See the copyright.txt file in the + * distribution for a full listing of individual contributors. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ + +package org.jboss.as.quickstarts.deltaspike.beanmanagerprovider.util; + +import java.io.Serializable; +import java.util.TimeZone; + +import javax.enterprise.context.RequestScoped; +import javax.enterprise.inject.Produces; +import javax.faces.context.FacesContext; +import javax.inject.Named; + +/** + * + * This class uses CDI to alias Java EE resources, such as the TimeZone and FacesContext, to CDI beans + * + *

+ * Example injection on a managed bean field: + *

+ * + *
+ * @Inject
+ * private FacesContext facesContext;
+ * 
+ * @author Rafael Benevides + * + */ +public class Resources implements Serializable { + + private static final long serialVersionUID = 1L; + + @Produces + @RequestScoped + public FacesContext produceFacesContext() { + return FacesContext.getCurrentInstance(); + } + + @Produces + @Named + public TimeZone applicationTimezone(){ + return TimeZone.getDefault(); + } + +} diff --git a/deltaspike-beanmanagerprovider/src/main/resources/META-INF/persistence.xml b/deltaspike-beanmanagerprovider/src/main/resources/META-INF/persistence.xml new file mode 100644 index 0000000000..a2fa531e4e --- /dev/null +++ b/deltaspike-beanmanagerprovider/src/main/resources/META-INF/persistence.xml @@ -0,0 +1,29 @@ + + + + + + + java:jboss/datasources/DeltaSpikeBMPQuickstartDS + + + + + + + diff --git a/deltaspike-beanmanagerprovider/src/main/resources/import.sql b/deltaspike-beanmanagerprovider/src/main/resources/import.sql new file mode 100644 index 0000000000..918fa70f81 --- /dev/null +++ b/deltaspike-beanmanagerprovider/src/main/resources/import.sql @@ -0,0 +1,2 @@ +-- You can use this file to load seed data into the database using SQL statements +insert into Contact (id, name, email, phone_number) values (0, 'John Smith', 'john.smith@mailinator.com', '2125551212') diff --git a/deltaspike-beanmanagerprovider/src/main/webapp/WEB-INF/beans.xml b/deltaspike-beanmanagerprovider/src/main/webapp/WEB-INF/beans.xml new file mode 100644 index 0000000000..a8d193ae74 --- /dev/null +++ b/deltaspike-beanmanagerprovider/src/main/webapp/WEB-INF/beans.xml @@ -0,0 +1,6 @@ + + + diff --git a/deltaspike-beanmanagerprovider/src/main/webapp/WEB-INF/deltaspike-bmp-quickstart-ds.xml b/deltaspike-beanmanagerprovider/src/main/webapp/WEB-INF/deltaspike-bmp-quickstart-ds.xml new file mode 100644 index 0000000000..9c988ddb78 --- /dev/null +++ b/deltaspike-beanmanagerprovider/src/main/webapp/WEB-INF/deltaspike-bmp-quickstart-ds.xml @@ -0,0 +1,32 @@ + + + + + + + jdbc:h2:mem:deltaspike-bmp-quickstart;DB_CLOSE_ON_EXIT=FALSE;DB_CLOSE_DELAY=-1 + + h2 + + sa + sa + + + + diff --git a/deltaspike-beanmanagerprovider/src/main/webapp/WEB-INF/faces-config.xml b/deltaspike-beanmanagerprovider/src/main/webapp/WEB-INF/faces-config.xml new file mode 100644 index 0000000000..b3d2ce3408 --- /dev/null +++ b/deltaspike-beanmanagerprovider/src/main/webapp/WEB-INF/faces-config.xml @@ -0,0 +1,9 @@ + + + + + diff --git a/deltaspike-beanmanagerprovider/src/main/webapp/contactsCRUD.xhtml b/deltaspike-beanmanagerprovider/src/main/webapp/contactsCRUD.xhtml new file mode 100644 index 0000000000..d38eb70c92 --- /dev/null +++ b/deltaspike-beanmanagerprovider/src/main/webapp/contactsCRUD.xhtml @@ -0,0 +1,99 @@ + + + + + Contacts + Contacts + +

+ + + + + + + + +
+ + + +

+

+

    +
  • To insert a new contact: Click the New Contact button, complete the form fields, and then click the Save button. Notice that both the All Contacts and Audit Records tables are updated.
  • +
  • To modify an existing contact: Find the name in the All Contacts table and click the Select for edit button. Modify the data and then click the Save button. Notice that the Audit Records table is updated with the new contact data.
  • +
  • To remove a contact: Find the name in the All Contacts table and click the Remove button. Notice that the Audit Records table is updated with the new contact data.
  • +
+ *Note: To fire the UPDATE audit, you must change one of the contact fields + + +
+ +

All Contacts

+ + + + Id + #{varContact.id} + + + Name + #{varContact.name} + + + email + #{varContact.email} + + + Phone Number + #{varContact.phoneNumber} + + + Operations + + + + + +

+


+

Audit Records

+ + + Contact e-Mail + #{varAudit.contactEmail} + + + Contact Name + #{varAudit.contactName} + + + Contact Phone + #{varAudit.contactPhone} + + + Date/Time + + + + + + + Operation + #{varAudit.operation} + + +

+ + + + + + + + \ No newline at end of file diff --git a/deltaspike-beanmanagerprovider/src/main/webapp/index.html b/deltaspike-beanmanagerprovider/src/main/webapp/index.html new file mode 100644 index 0000000000..e3bf8b57a2 --- /dev/null +++ b/deltaspike-beanmanagerprovider/src/main/webapp/index.html @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/deltaspike-beanmanagerprovider/src/main/webapp/resources/jdf/input.xhtml b/deltaspike-beanmanagerprovider/src/main/webapp/resources/jdf/input.xhtml new file mode 100644 index 0000000000..1d9ee5c00a --- /dev/null +++ b/deltaspike-beanmanagerprovider/src/main/webapp/resources/jdf/input.xhtml @@ -0,0 +1,26 @@ + + + +Not present in rendered output + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/deltaspike-beanmanagerprovider/src/main/webapp/template.xhtml b/deltaspike-beanmanagerprovider/src/main/webapp/template.xhtml new file mode 100644 index 0000000000..7e50cbd617 --- /dev/null +++ b/deltaspike-beanmanagerprovider/src/main/webapp/template.xhtml @@ -0,0 +1,67 @@ + + + + <ui:insert name="pageTitle">Page Title</ui:insert> + + + + + + + + + + + + + + + + + + + + + + +
+ Page Header +
+ + + + + + +
+ + Page Body +
+
+ +
+ + + \ No newline at end of file