1 16 package org.apache.cocoon.samples.flow.java; 17 18 import org.apache.cocoon.components.flow.java.AbstractContinuable; 19 import org.apache.cocoon.components.flow.java.Continuable; 20 import org.apache.cocoon.components.flow.java.VarMap; 21 import org.apache.cocoon.forms.binding.BindingException; 22 import org.apache.cocoon.forms.flow.java.FormInstance; 23 import org.apache.cocoon.ojb.samples.bean.Employee; 24 25 import org.apache.ojb.broker.Identity; 26 import org.apache.ojb.broker.PersistenceBroker; 27 import org.apache.ojb.broker.PersistenceBrokerFactory; 28 import org.apache.ojb.broker.query.Criteria; 29 import org.apache.ojb.broker.query.QueryByCriteria; 30 31 import java.util.ArrayList ; 32 import java.util.Collections ; 33 import java.util.Comparator ; 34 import java.util.Iterator ; 35 36 39 public class PersistenceFlow extends AbstractContinuable { 40 41 private transient PersistenceBroker broker; 42 43 public PersistenceFlow() { 44 this.broker = PersistenceBrokerFactory.defaultPersistenceBroker(); 45 } 46 47 public void doInsertEmployee() throws BindingException { 48 Employee employee = new Employee(); 50 employee.setId(1); 52 FormInstance form = new FormInstance("forms/employee.xml"); 54 form.createBinding("forms/employee-binding.xml"); 56 form.load(employee); 58 form.show("form/employee"); 60 form.save(employee); 62 broker.store(employee); 64 doShowEmployee(); 66 } 67 68 public void doUpdateEmployee() throws BindingException { 69 int id = 1; 71 if (getRequest().getParameter("id")!=null) 72 id = Integer.parseInt(getRequest().getParameter("id")); 73 else 74 throw new IllegalStateException ("No parameter 'id'"); 75 76 Employee employee = new Employee(); 78 employee.setId(id); 80 employee = (Employee) broker.getObjectByIdentity(new Identity(employee, broker)); 82 FormInstance form = new FormInstance("forms/employee.xml"); 84 form.createBinding("forms/employee-binding.xml"); 86 form.load(employee); 88 form.show("form/employee"); 90 form.save(employee); 92 93 broker.store(employee); 95 96 doShowEmployee(); 98 } 99 100 public void doRemoveEmployee() { 101 int id = 1; 103 if (getRequest().getParameter("id")!=null) 104 id = Integer.parseInt(getRequest().getParameter("id")); 105 else 106 throw new IllegalStateException ("No parameter 'id'"); 107 108 Employee employee = new Employee(); 110 employee.setId(id); 112 employee = (Employee) broker.getObjectByIdentity(new Identity(employee, broker)); 114 broker.delete(employee); 116 doShowEmployee(); 118 } 119 120 public void doShowEmployee() { 121 ArrayList results = new ArrayList (); 123 QueryByCriteria query = new QueryByCriteria(Employee.class, new Criteria()); 124 for(Iterator i=broker.getCollectionByQuery(query).iterator(); i.hasNext();) { 125 results.add(i.next()); 126 } 127 Collections.sort(results, new EmployeeComparator()); 129 sendPage("page/employee-result", new VarMap().add("employee", results)); 131 } 132 133 public static class EmployeeComparator implements Comparator , Continuable { 134 public int compare(Object o1, Object o2) { 135 return ((Employee)o1).getId()-((Employee)o2).getId(); 136 } 137 138 public boolean equals(Object obj) { 139 return true; 140 } 141 } 142 } 143 | Popular Tags |