KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > vlib > pages > NewBook


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;
16
17 import java.rmi.RemoteException JavaDoc;
18 import java.util.HashMap JavaDoc;
19 import java.util.Map JavaDoc;
20
21 import javax.ejb.CreateException JavaDoc;
22
23 import org.apache.tapestry.IRequestCycle;
24 import org.apache.tapestry.Tapestry;
25 import org.apache.tapestry.event.PageEvent;
26 import org.apache.tapestry.event.PageRenderListener;
27 import org.apache.tapestry.vlib.Protected;
28 import org.apache.tapestry.vlib.VirtualLibraryEngine;
29 import org.apache.tapestry.vlib.Visit;
30 import org.apache.tapestry.vlib.ejb.IOperations;
31
32 /**
33  * Collects information for a new book.
34  *
35  * @author Howard Lewis Ship
36  * @version $Id: NewBook.java,v 1.14 2004/02/19 17:37:48 hlship Exp $
37  *
38  **/

39
40 public abstract class NewBook extends Protected implements PageRenderListener
41 {
42
43     public abstract Map JavaDoc getAttributes();
44
45     public abstract void setAttributes(Map JavaDoc attributes);
46
47     public abstract String JavaDoc getPublisherName();
48
49     public void addBook(IRequestCycle cycle)
50     {
51         Map JavaDoc attributes = getAttributes();
52
53         Integer JavaDoc publisherId = (Integer JavaDoc) attributes.get("publisherId");
54         String JavaDoc publisherName = getPublisherName();
55
56         if (publisherId == null && Tapestry.isBlank(publisherName))
57         {
58             setErrorField("inputPublisherName", getMessage("need-publisher-name"));
59             return;
60         }
61
62         if (publisherId != null && Tapestry.isNonBlank(publisherName))
63         {
64             setErrorField("inputPublisherName", getMessage("leave-publisher-name-empty"));
65             return;
66         }
67
68         if (isInError())
69             return;
70
71         Visit visit = (Visit) getVisit();
72         Integer JavaDoc userId = visit.getUserId();
73         VirtualLibraryEngine vengine = (VirtualLibraryEngine) cycle.getEngine();
74
75         attributes.put("ownerId", userId);
76         attributes.put("holderId", userId);
77
78         int i = 0;
79         while (true)
80         {
81             try
82             {
83
84                 IOperations operations = vengine.getOperations();
85
86                 if (publisherId != null)
87                     operations.addBook(attributes);
88                 else
89                 {
90                     operations.addBook(attributes, publisherName);
91
92                     // Clear the app's cache of info; in this case, known publishers.
93

94                     vengine.clearCache();
95                 }
96
97                 break;
98             }
99             catch (CreateException JavaDoc ex)
100             {
101                 setError("Error adding book: " + ex.getMessage());
102                 return;
103             }
104             catch (RemoteException JavaDoc ex)
105             {
106                 vengine.rmiFailure("Remote exception adding new book.", ex, i++);
107             }
108         }
109
110         // Success. First, update the message property of the return page.
111

112         MyLibrary myLibrary = (MyLibrary) cycle.getPage("MyLibrary");
113
114         myLibrary.setMessage(format("added-book", attributes.get("title")));
115
116         myLibrary.activate(cycle);
117     }
118
119     public void pageBeginRender(PageEvent event)
120     {
121         if (getAttributes() == null)
122         {
123             Map JavaDoc attributes = new HashMap JavaDoc();
124
125             // Setup defaults for the new book.
126

127             attributes.put("lendable", Boolean.TRUE);
128
129             setAttributes(attributes);
130         }
131     }
132
133 }
Popular Tags