KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > samples > flow > java > PersistenceFlow


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc;
32 import java.util.Collections JavaDoc;
33 import java.util.Comparator JavaDoc;
34 import java.util.Iterator JavaDoc;
35
36 /**
37  * @version $Id: PersistenceFlow.java 155366 2005-02-25 19:54:12Z vgritsenko $
38  */

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         // Create a empty Bean
49
Employee employee = new Employee();
50         // Fill some initial data to the bean
51
employee.setId(1);
52         // Load form descriptor
53
FormInstance form = new FormInstance("forms/employee.xml");
54         // Load form binding
55
form.createBinding("forms/employee-binding.xml");
56         // Load the Bean to the form
57
form.load(employee);
58         // Let Cocoon Forms handle the form
59
form.show("form/employee");
60         // Update the Bean based on user input
61
form.save(employee);
62         // Update Bean in Database
63
broker.store(employee);
64         // Send response to the user
65
doShowEmployee();
66     }
67
68     public void doUpdateEmployee() throws BindingException {
69         // Get id as parameter
70
int id = 1;
71         if (getRequest().getParameter("id")!=null)
72             id = Integer.parseInt(getRequest().getParameter("id"));
73         else
74             throw new IllegalStateException JavaDoc("No parameter 'id'");
75
76         // Create a empty Bean
77
Employee employee = new Employee();
78         // Fill some initial data to the bean
79
employee.setId(id);
80         // Load bean based on the given PrimaryKey
81
employee = (Employee) broker.getObjectByIdentity(new Identity(employee, broker));
82         // Load form descriptor
83
FormInstance form = new FormInstance("forms/employee.xml");
84         // Load form binding
85
form.createBinding("forms/employee-binding.xml");
86         // Load the Bean to the form
87
form.load(employee);
88         // Let Cocoon Forms handle the form
89
form.show("form/employee");
90         // Update the Bean based on user input
91
form.save(employee);
92
93         // Update Bean in Database
94
broker.store(employee);
95
96         // Send response to the user
97
doShowEmployee();
98     }
99
100     public void doRemoveEmployee() {
101         // Get id as parameter
102
int id = 1;
103         if (getRequest().getParameter("id")!=null)
104             id = Integer.parseInt(getRequest().getParameter("id"));
105         else
106             throw new IllegalStateException JavaDoc("No parameter 'id'");
107
108         // Create a empty Bean
109
Employee employee = new Employee();
110         // Fill some initial data to the bean
111
employee.setId(id);
112         // Load bean based on the given PrimaryKey
113
employee = (Employee) broker.getObjectByIdentity(new Identity(employee, broker));
114         // Remove bean
115
broker.delete(employee);
116         // Send response to the user
117
doShowEmployee();
118     }
119
120     public void doShowEmployee() {
121         // Query all objects
122
ArrayList JavaDoc results = new ArrayList JavaDoc();
123         QueryByCriteria query = new QueryByCriteria(Employee.class, new Criteria());
124         for(Iterator JavaDoc i=broker.getCollectionByQuery(query).iterator(); i.hasNext();) {
125             results.add(i.next());
126         }
127         // Sort result
128
Collections.sort(results, new EmployeeComparator());
129         // Send response to the user
130
sendPage("page/employee-result", new VarMap().add("employee", results));
131     }
132
133     public static class EmployeeComparator implements Comparator JavaDoc, Continuable {
134         public int compare(Object JavaDoc o1, Object JavaDoc o2) {
135             return ((Employee)o1).getId()-((Employee)o2).getId();
136         }
137
138         public boolean equals(Object JavaDoc obj) {
139             return true;
140         }
141     }
142 }
143
Popular Tags