Initial commit

This commit is the first commit, which creates the maven project
with ZK 8.5 and with the dependency to the persistence project.

An utility class DesktopEntityManagerManager is created to manage
a entity manager per-desktop.

A simple example with a simple VM is included to do some tests.
parents
/target/
.iml
.classpath
.project
.settings
# SI 17/18 Example Project: Web Layer
<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>
<groupId>sing</groupId>
<artifactId>proyectosi-webapp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<proyectosi-persistence.version>0.0.1-SNAPSHOT</proyectosi-persistence.version>
<zk.version>8.5.0</zk.version>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
</properties>
<repositories>
<repository>
<id>ZK CE</id>
<name>ZK CE Repository</name>
<url>http://mavensync.zkoss.org/maven2</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>dgpena</groupId>
<artifactId>si-example-persistence</artifactId>
<version>${proyectosi-persistence.version}</version>
</dependency>
<dependency>
<groupId>org.zkoss.zk</groupId>
<artifactId>zkbind</artifactId>
<version>${zk.version}</version>
</dependency>
<dependency>
<groupId>org.zkoss.zk</groupId>
<artifactId>zul</artifactId>
<version>${zk.version}</version>
</dependency>
<dependency>
<groupId>org.zkoss.zk</groupId>
<artifactId>zkplus</artifactId>
<version>${zk.version}</version>
</dependency>
<dependency>
<groupId>org.zkoss.zk</groupId>
<artifactId>zhtml</artifactId>
<version>${zk.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.3.14.v20161028</version>
</plugin>
</plugins>
</build>
</project>
package dgpena.siexample.webapp;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import org.zkoss.zk.ui.Desktop;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.util.DesktopCleanup;
/**
* Creates a EntityManger per ZK Desktop.
*
* A ZK Desktop is created per complete page. That is,
* when user refreshes the page or opens a new tab, or a new
* browser session, a new Desktop is created for this new page.
*
* Mapping an EntityManager to Desktops is useful to keep
* an opened EntityManager while the user is interacting with
* the same page, which facilitates keeping entity objects in
* session (e.g. a ViewModel) while keeping their persistent state,
* and thus avoiding, for example, lazy initialization exceptions,
* the need to make merges, re-finding entities by id, etc.
*/
public class DesktopEntityManagerManager {
private static final String ENTITY_MANAGER_NAME = "__ENTITY_MANAGER__";
private static EntityManagerFactory emf = null;
public static EntityManager getDesktopEntityManager() {
Desktop currentDesktop = Executions.getCurrent().getDesktop();
if (currentDesktop != null) {
if (currentDesktop.hasAttribute(ENTITY_MANAGER_NAME)) {
return (EntityManager) currentDesktop.getAttribute(ENTITY_MANAGER_NAME);
} else {
EntityManager newEm = createNewEntityMamanger();
currentDesktop.setAttribute(ENTITY_MANAGER_NAME, newEm);
currentDesktop.addListener(new DesktopCleanup() {
@Override
public void cleanup(Desktop arg0) throws Exception {
newEm.close();
}
});
return newEm;
}
} else {
throw new IllegalArgumentException("Desktop not found in this execution");
}
}
private static EntityManager createNewEntityMamanger() {
EntityManagerFactory emf = getOrCreateEntityManagerFactory();
return emf.createEntityManager();
}
private static EntityManagerFactory getOrCreateEntityManagerFactory() {
if (emf != null) {
return emf;
}
final String persistenceUnitName = Executions.getCurrent().getDesktop().getWebApp().getConfiguration()
.getPreference("persistence-unit-name", "");
if (persistenceUnitName.equals("")) {
throw new IllegalArgumentException("no 'persistence-unit-name' preference found in zk.xml");
} else {
emf = Persistence.createEntityManagerFactory(persistenceUnitName);
}
Runtime.getRuntime().addShutdownHook(new Thread(){
@Override
public void run() {
emf.close();
}
});
return emf;
}
}
\ No newline at end of file
package dgpena.siexample.webapp;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.NotifyChange;
public class SampleVM {
private String name;
private int counter;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getCounter() {
return counter;
}
public void setCounter(int counter) {
this.counter = counter;
}
@Command
@NotifyChange({"counter", "name"})
public void increment() {
if (this.name != null) {
this.name = new String(this.name);
}
this.counter ++;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<description>Example ZK application</description>
<display-name>ZKSampleApp</display-name>
</web-app>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<zk>
<preference>
<name>persistence-unit-name</name>
<value>si-database</value>
</preference>
</zk>
\ No newline at end of file
<html>
<body>Hola!</body>
</html>
\ No newline at end of file
<zk>
<window title="Hello World" border="normal"
apply="org.zkoss.bind.BindComposer"
viewModel="@id('vm') @init('dgpena.siexample.webapp.SampleVM')"
>
<vlayout>
<textbox value="@bind(vm.name)" onChanging="@command('increment')"></textbox>
<label value="@bind(vm.name)"></label>
<button label="increment" onClick="@command('increment')"></button>
<label value="@bind(vm.counter)"></label>
</vlayout>
</window>
</zk>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment