KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > vlib > components > BookLink


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.components;
16
17 import java.sql.Timestamp JavaDoc;
18
19 import org.apache.tapestry.BaseComponent;
20 import org.apache.tapestry.IEngine;
21 import org.apache.tapestry.vlib.Visit;
22 import org.apache.tapestry.vlib.ejb.Book;
23
24 /**
25  * Creates a link to the {@link org.apache.tapestry.vlib.pages.ViewBook}
26  * page using the external service.
27  *
28  *
29  * <table border=1>
30  * <tr> <th>Parameter</th>
31  * <th>Type</th>
32  * <th>Direction</th>
33  * <th>Required</th>
34  * <th>Default</th>
35  * <th>Description</th>
36  * </tr>
37  *
38  * <tr>
39  * <td>book</td>
40  * <td>{@link Book}</td>
41  * <td>in</td>
42  * <td>yes</td>
43  * <td>&nbsp;</td>
44  * <td>The {@link Book} to create a link to.</td>
45  * </tr>
46  *
47  * </table>
48  *
49  * <p>Informal parameters are allowed.
50  *
51  * @author Howard Lewis Ship
52  * @version $Id: BookLink.java,v 1.4 2004/02/19 17:38:04 hlship Exp $
53  *
54  **/

55
56 public abstract class BookLink extends BaseComponent
57 {
58     /**
59      * One week, in milliseconds (1/1000 second). Books that have been added in the last
60      * week are marked new, until the user logs in, at which point, its books
61      * added since the user last logged in.
62      *
63      **/

64
65     private static final long ONE_WEEK_MILLIS = 1000l * 60l * 60l * 24l * 7l;
66
67     public boolean isNewlyAdded()
68     {
69         IEngine engine = getPage().getEngine();
70         Visit visit = (Visit) engine.getVisit();
71         Timestamp JavaDoc lastAccess = null;
72
73         if (visit != null)
74             lastAccess = visit.getLastAccess();
75
76         Book book = getBook();
77
78         Timestamp JavaDoc dateAdded = book.getDateAdded();
79
80         // Some old records may not contain a value for dateAdded
81

82         if (dateAdded == null)
83             return false;
84
85         // If don't know the last access time (because the user
86
// hasn't logged in yet), then show anything newer
87
// than a week.
88

89         if (lastAccess == null)
90         {
91             long now = System.currentTimeMillis();
92
93             return (now - dateAdded.getTime()) <= ONE_WEEK_MILLIS;
94         }
95
96         // Return true if lastAccess is earlier than date added.
97

98         return lastAccess.compareTo(dateAdded) <= 0;
99     }
100
101     public abstract Book getBook();
102 }
Popular Tags