KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > vlib > pages > admin > EditPublishers


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

15 package org.apache.tapestry.vlib.pages.admin;
16
17 import java.rmi.RemoteException JavaDoc;
18 import java.util.List JavaDoc;
19
20 import javax.ejb.FinderException JavaDoc;
21 import javax.ejb.RemoveException JavaDoc;
22
23 import org.apache.hivemind.ApplicationRuntimeException;
24 import org.apache.tapestry.IRequestCycle;
25 import org.apache.tapestry.PageRedirectException;
26 import org.apache.tapestry.Tapestry;
27 import org.apache.tapestry.event.PageEvent;
28 import org.apache.tapestry.event.PageRenderListener;
29 import org.apache.tapestry.form.ListEditMap;
30 import org.apache.tapestry.vlib.AdminPage;
31 import org.apache.tapestry.vlib.VirtualLibraryEngine;
32 import org.apache.tapestry.vlib.ejb.IOperations;
33 import org.apache.tapestry.vlib.ejb.Publisher;
34
35 /**
36  * Allows editting of the publishers in the database, including deleting
37  * publishers (which can be dangerous if any books are linked to the publisher).
38  *
39  * @author Howard Lewis Ship
40  * @version $Id: EditPublishers.java,v 1.10 2004/04/30 15:17:21 hlship Exp $
41  *
42  **/

43
44 public abstract class EditPublishers extends AdminPage implements PageRenderListener
45 {
46     public abstract ListEditMap getListEditMap();
47
48     public abstract void setListEditMap(ListEditMap listEditMap);
49
50     public abstract Publisher getPublisher();
51
52     public abstract void setPublisher(Publisher publisher);
53
54     public void synchronizePublisher(IRequestCycle cycle)
55     {
56         ListEditMap map = getListEditMap();
57
58         Publisher publisher = (Publisher) map.getValue();
59
60         if (publisher == null)
61         {
62             setError(getMessage("out-of-date"));
63             throw new PageRedirectException(this);
64         }
65
66         setPublisher(publisher);
67     }
68
69     /**
70      * Reads all publishers from the database, building the list
71      * of publisher ids, and the map from id to Publisher.
72      * Also, sets the deletedPublisherIds property to an empty set.
73      *
74      **/

75
76     private void readPublishers()
77     {
78         VirtualLibraryEngine vengine = (VirtualLibraryEngine) getEngine();
79         Publisher[] publishers = null;
80
81         int i = 0;
82         while (true)
83         {
84             try
85             {
86                 IOperations operations = vengine.getOperations();
87
88                 publishers = operations.getPublishers();
89
90                 break;
91             }
92             catch (RemoteException JavaDoc ex)
93             {
94                 vengine.rmiFailure(getMessage("read-failure"), ex, i++);
95             }
96         }
97
98         ListEditMap map = new ListEditMap();
99
100         int count = Tapestry.size(publishers);
101
102         for (i = 0; i < count; i++)
103             map.add(publishers[i].getId(), publishers[i]);
104
105         setListEditMap(map);
106     }
107
108     public void processForm(IRequestCycle cycle)
109     {
110         if (isInError())
111             return;
112
113         ListEditMap map = getListEditMap();
114         List JavaDoc updateList = map.getValues();
115         List JavaDoc deletedIds = map.getDeletedKeys();
116
117         Publisher[] updated = (Publisher[]) updateList.toArray(new Publisher[updateList.size()]);
118
119         Integer JavaDoc[] deleted =
120             deletedIds == null
121                 ? null
122                 : (Integer JavaDoc[]) deletedIds.toArray(new Integer JavaDoc[deletedIds.size()]);
123
124         // Now, push the updates through to the database
125

126         VirtualLibraryEngine vengine = (VirtualLibraryEngine) getEngine();
127
128         int i = 0;
129         while (true)
130         {
131             try
132             {
133                 IOperations operations = vengine.getOperations();
134
135                 operations.updatePublishers(updated, deleted);
136
137                 break;
138             }
139             catch (FinderException JavaDoc ex)
140             {
141                 throw new ApplicationRuntimeException(ex);
142             }
143             catch (RemoveException JavaDoc ex)
144             {
145                 throw new ApplicationRuntimeException(ex);
146             }
147             catch (RemoteException JavaDoc ex)
148             {
149                 vengine.rmiFailure(getMessage("update-failure"), ex, i++);
150             }
151         }
152
153         // Clear any cached info about publishers.
154

155         vengine.clearCache();
156     }
157
158     public void pageBeginRender(PageEvent event)
159     {
160         readPublishers();
161     }
162
163 }
Popular Tags