Skip to content

Commit

Permalink
Viewing history
Browse files Browse the repository at this point in the history
  • Loading branch information
adamw committed Sep 28, 2011
1 parent c964f22 commit be08646
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 1 deletion.
Expand Up @@ -19,6 +19,7 @@ public class URLRewriteConfiguration extends HttpConfigurationProvider {
.addRule(Join.path("/").to("/index.jsf"))
.addRule(Join.path("/{domain}").where("domain").matches(ENTITY_NAME).to("/scaffold/{domain}/list.jsf").withInboundCorrection())
.addRule(Join.path("/{domain}/{id}").where("domain").matches(ENTITY_NAME).where("id").matches("\\d+").to("/scaffold/{domain}/view.jsf").withInboundCorrection())
.addRule(Join.path("/{domain}/history/{id}").where("domain").matches(ENTITY_NAME).where("id").matches("\\d+").to("/scaffold/{domain}/history.jsf").withInboundCorrection())
.addRule(Join.path("/{domain}/create").where("domain").matches(ENTITY_NAME).to("/scaffold/{domain}/create.jsf").withInboundCorrection())
.addRule(Join.path("/404").to("/404.jsf"))
.addRule(Join.path("/error").to("/500.jsf"));
Expand Down
76 changes: 76 additions & 0 deletions src/main/java/pl/softwaremill/demo/view/PersonHistoryBean.java
@@ -0,0 +1,76 @@
package pl.softwaremill.demo.view;

import org.hibernate.envers.AuditReader;
import org.hibernate.envers.AuditReaderFactory;
import org.hibernate.envers.DefaultRevisionEntity;
import org.hibernate.envers.query.AuditEntity;
import org.jboss.seam.transaction.Transactional;
import pl.softwaremill.demo.domain.Person;

import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.EntityManager;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
* @author Adam Warski (adam at warski dot org)
*/
@Transactional
@Named
@RequestScoped
public class PersonHistoryBean {
@Inject
private EntityManager entityManager;

private List<PersonHistoryEntry> list;

public List<PersonHistoryEntry> getList() {
if (list == null) {
AuditReader auditReader = AuditReaderFactory.get(entityManager);
@SuppressWarnings({"unchecked"}) List<Object[]> revDatas = (List<Object[]>) auditReader
.createQuery()
.forRevisionsOfEntity(Person.class, false, false)
.add(AuditEntity.id().eq(id))
.getResultList();

list = new ArrayList<PersonHistoryEntry>();
for (Object[] revData : revDatas) {
list.add(new PersonHistoryEntry((Person) revData[0], (DefaultRevisionEntity) revData[1]));
}
}

return list;
}

private Long id;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public static class PersonHistoryEntry {
private final Person person;
private final String changeDate;

public PersonHistoryEntry(Person person, DefaultRevisionEntity revision) {
this.person = person;
this.changeDate = DateFormat.getTimeInstance().format(revision.getRevisionDate());
}

public String getChangeDate() {
return changeDate;
}

public Person getPerson() {
return person;
}
}
}
57 changes: 57 additions & 0 deletions src/main/webapp/scaffold/person/history.xhtml
@@ -0,0 +1,57 @@
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:m="http://metawidget.org/faces"
xmlns:c="http://java.sun.com/jsp/jstl/core">

<f:metadata>
<f:viewParam name="id" value="#{personHistoryBean.id}">
<f:validateLongRange minimum="1" />
</f:viewParam>
</f:metadata>

<ui:composition template="/resources/scaffold/forge-template.xhtml">

<ui:param name="pageTitle" value="View Person History" />

<ui:define name="header">
Person History
</ui:define>

<ui:define name="subheader">
Changes made to your object
</ui:define>

<ui:define name="footer">
</ui:define>

<ui:define name="main">
<h:form id="form">
<h:messages globalOnly="true"/>
<br/>

<ui:repeat var="entry" value="#{personHistoryBean.list}">

<li>
<m:metawidget value="#{entry.person}" readOnly="true" rendererType="simple" style="margin-right: 0.5em"/>
changed on
#{entry.changeDate}
</li>

</ui:repeat>

<br/>

<h:link value="Back" outcome="view">
<f:param name="id" value="#{personHistoryBean.id}" />
<f:param name="edit" value="false" />
</h:link>
</h:form>
</ui:define>

</ui:composition>
</html>
7 changes: 6 additions & 1 deletion src/main/webapp/scaffold/person/view.xhtml
Expand Up @@ -43,7 +43,12 @@
<c:if test="#{ _editMode != null and _editMode ne 'false' }">
<h:commandLink value="Save" action="#{personBean.save()}" />
</c:if>


<h:panelGroup rendered="#{ _editMode == null or _editMode eq 'false' }">
<h:link value="History" outcome="history" includeViewParams="true">
<f:param name="id" value="#{personBean.id}"/>
</h:link> |
</h:panelGroup>
<h:link value="View all" outcome="list" rendered="#{ _editMode == null or _editMode eq 'false' }" /> |
<h:link value="Edit" outcome="view" includeViewParams="true" rendered="#{ _editMode == null or _editMode eq 'false' }">
<f:param name="edit" value="true" />
Expand Down

0 comments on commit be08646

Please sign in to comment.