001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.samples.flow.java;
018:
019: import org.apache.cocoon.components.flow.java.AbstractContinuable;
020: import org.apache.cocoon.components.flow.java.Continuable;
021: import org.apache.cocoon.components.flow.java.VarMap;
022: import org.apache.cocoon.forms.binding.BindingException;
023: import org.apache.cocoon.forms.flow.java.FormInstance;
024: import org.apache.cocoon.ojb.samples.bean.Employee;
025:
026: import org.apache.ojb.broker.Identity;
027: import org.apache.ojb.broker.PersistenceBroker;
028: import org.apache.ojb.broker.PersistenceBrokerFactory;
029: import org.apache.ojb.broker.query.Criteria;
030: import org.apache.ojb.broker.query.QueryByCriteria;
031:
032: import java.util.ArrayList;
033: import java.util.Collections;
034: import java.util.Comparator;
035: import java.util.Iterator;
036:
037: /**
038: * @version $Id: PersistenceFlow.java 487118 2006-12-14 08:12:09Z cziegeler $
039: */
040: public class PersistenceFlow extends AbstractContinuable {
041:
042: private transient PersistenceBroker broker;
043:
044: public PersistenceFlow() {
045: this .broker = PersistenceBrokerFactory
046: .defaultPersistenceBroker();
047: }
048:
049: public void doInsertEmployee() throws BindingException {
050:
051: int id = 1;
052: // Get id as parameter
053: if (getRequest().getParameter("id") != null)
054: id = Integer.parseInt(getRequest().getParameter("id"));
055:
056: // Create a empty Bean
057: Employee employee = new Employee();
058: // Fill some initial data to the bean
059:
060: employee.setId(id);
061: // Load form descriptor
062: FormInstance form = new FormInstance("forms/employee.xml");
063: // Load form binding
064: form.createBinding("forms/employee-binding.xml");
065: // Load the Bean to the form
066: form.load(employee);
067: // Let Cocoon Forms handle the form
068: form.show("form/employee");
069: // Update the Bean based on user input
070: form.save(employee);
071: // Update Bean in Database
072: broker.store(employee);
073: // Send response to the user
074: doShowEmployee();
075: }
076:
077: public void doUpdateEmployee() throws BindingException {
078: // Get id as parameter
079: int id = 1;
080: if (getRequest().getParameter("id") != null)
081: id = Integer.parseInt(getRequest().getParameter("id"));
082: else
083: throw new IllegalStateException("No parameter 'id'");
084:
085: // Create a empty Bean
086: Employee employee = new Employee();
087: // Fill some initial data to the bean
088: employee.setId(id);
089: // Load bean based on the given PrimaryKey
090: employee = (Employee) broker.getObjectByIdentity(new Identity(
091: employee, broker));
092: // Load form descriptor
093: FormInstance form = new FormInstance("forms/employee.xml");
094: // Load form binding
095: form.createBinding("forms/employee-binding.xml");
096: // Load the Bean to the form
097: form.load(employee);
098: // Let Cocoon Forms handle the form
099: form.show("form/employee");
100: // Update the Bean based on user input
101: form.save(employee);
102:
103: // Update Bean in Database
104: broker.store(employee);
105:
106: // Send response to the user
107: doShowEmployee();
108: }
109:
110: public void doRemoveEmployee() {
111: // Get id as parameter
112: int id = 1;
113: if (getRequest().getParameter("id") != null)
114: id = Integer.parseInt(getRequest().getParameter("id"));
115: else
116: throw new IllegalStateException("No parameter 'id'");
117:
118: // Create a empty Bean
119: Employee employee = new Employee();
120: // Fill some initial data to the bean
121: employee.setId(id);
122: // Load bean based on the given PrimaryKey
123: employee = (Employee) broker.getObjectByIdentity(new Identity(
124: employee, broker));
125: // Remove bean
126: broker.delete(employee);
127: // Send response to the user
128: doShowEmployee();
129: }
130:
131: public void doShowEmployee() {
132: // Query all objects
133: ArrayList results = new ArrayList();
134: // new Employee().getClass() is a fix for bug COCOON-1969
135: QueryByCriteria query = new QueryByCriteria(new Employee()
136: .getClass(), new Criteria());
137: for (Iterator i = broker.getCollectionByQuery(query).iterator(); i
138: .hasNext();) {
139: results.add(i.next());
140: }
141: // Sort result
142: Collections.sort(results, new EmployeeComparator());
143: // Send response to the user
144: sendPage("page/employee-result", new VarMap().add("employee",
145: results));
146: }
147:
148: public static class EmployeeComparator implements Comparator,
149: Continuable {
150: public int compare(Object o1, Object o2) {
151: return ((Employee) o1).getId() - ((Employee) o2).getId();
152: }
153:
154: public boolean equals(Object obj) {
155: return true;
156: }
157: }
158: }
|