KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > killingar > forum > actions > area > View


1 /* Copyright 2000-2005 Anders Hovmöller
2  *
3  * The person or persons who have associated their work with
4  * this document (the "Dedicator") hereby dedicate the entire
5  * copyright in the work of authorship identified below (the
6  * "Work") to the public domain.
7  *
8  * Dedicator makes this dedication for the benefit of the
9  * public at large and to the detriment of Dedicator's heirs
10  * and successors. Dedicator intends this dedication to be an
11  * overt act of relinquishment in perpetuity of all present
12  * and future rights under copyright law, whether vested or
13  * contingent, in the Work. Dedicator understands that such
14  * relinquishment of all rights includes the relinquishment of
15  * all rights to enforce (by lawsuit or otherwise) those
16  * copyrights in the Work.
17  *
18  * Dedicator recognizes that, once placed in the public
19  * domain, the Work may be freely reproduced, distributed,
20  * transmitted, used, modified, built upon, or otherwise
21  * exploited by anyone for any purpose, commercial or non-
22  * commercial, and in any way, including by methods that have
23  * not yet been invented or conceived.
24  */

25
26 package net.killingar.forum.actions.area;
27
28 import net.killingar.forum.internal.*;
29
30 import java.sql.Timestamp JavaDoc;
31 import java.util.Iterator JavaDoc;
32
33 public class View extends ActionAreaSupport
34 {
35   // Private --------------------------------------------------------
36
static final long NOT_SET = -1;
37
38   // Attributes -----------------------------------------------------
39
static final java.text.SimpleDateFormat JavaDoc dateFormatter = new java.text.SimpleDateFormat JavaDoc("EEE' 'MMM' 'dd' 'HH:mm");
40
41   Iterator JavaDoc messages = null;
42   long areaID = -1;
43   Area area = null;
44   boolean showHidden = false;
45   boolean showingUnread = false;
46   long time = NOT_SET, setTime = NOT_SET;
47     Timestamp JavaDoc timeTimestamp; // for performance reasons
48
Timestamp JavaDoc timeTimestamp2; // for performance reasons
49
private AreaInfo areaInfo;
50     long maxIndent = 0;
51
52   int startPageID = -1;
53   int pageID = -1;
54
55     protected AreaCustomizer areaCustomizer;
56
57   // Methods --------------------------------------------------------
58

59   // Types ----------------------------------------------------------
60

61   // Setters --------------------------------------------------------
62
public void setAreaID (long areaID) { this.areaID = areaID; }
63   public void setPage (int pageID) { this.pageID = pageID; }
64   public void setShowHidden (boolean showHidden) { this.showHidden = showHidden; }
65   public void setTime (long time) { this.time = time; }
66     public void setSetTime (long time) { this.setTime = time; }
67
68   // Getters --------------------------------------------------------
69
public boolean getShowingUnread(){ return showingUnread; }
70     public Iterator JavaDoc getMessages() { return messages; }
71   public Area getArea() { return area; }
72     public long getAreaID() { return areaID; }
73   public boolean getShowHidden() { return showHidden; }
74   public long getTime() { return time; }
75
76   public int getStartPage() { return startPageID+1; }
77   public int getEndPage() { return pageID+1; }
78
79   public int getStartPageID() { return startPageID; }
80   public int getPageID() { return pageID; }
81     public int getNumberOfPages(){ return areaInfo.pages.size(); }
82     public int getLastPage() { return areaInfo.numberOfPages-1; }
83   public long getPrevPage() { return startPageID-1; }
84   public long getNextPage() { return pageID+1; }
85     public AreaInfo getAreaInfo() { return areaInfo; }
86
87     public String JavaDoc getMessageView() { return areaCustomizer.getMessageView(); }
88     public String JavaDoc getView() { return areaCustomizer.getView(); }
89     public AreaCustomizer getAreaCustomizer() { return areaCustomizer; }
90
91     // utility methods for the view
92
public long getColspan(int in)
93     {
94         return maxIndent+1-in;
95     }
96
97     public boolean getBodyEmpty(Message m)
98     {
99         return m.getBody() == null || m.getBody().length() == 0;
100     }
101
102     public boolean getDeleteAccess(Message message) throws java.sql.SQLException JavaDoc, AccessDeniedException
103     {
104         return
105             message.getOwnerID() == manager.getUserID() ||
106             areamgr.hasAccess(manager.getUserID(), area.getAreaGroupID(), AccessLevel.removeMessage);
107     }
108
109     public boolean getUnread(Message m)
110     {
111         return m.lastChanged.after(timeTimestamp);//areaInfo.getAreaTime());
112
}
113
114     public String JavaDoc getCssClass(Message m)
115     {
116         if (m.lastChanged.after(timeTimestamp2))
117             return " unread";
118         else if (m.lastChanged.after(timeTimestamp))
119             return " unread2";
120         else
121             return "";
122     }
123
124     public boolean getEditable(long messageID) throws java.sql.SQLException JavaDoc, AccessDeniedException
125     {
126         //return false;
127
ParentIDItemTreeNode node = areamgr.getMessageSubtree(areaID, messageID);
128         return ((Message)node.item).ownerID == manager.getUserID() && !node.hasSubnodes();
129     }
130
131   // Implementation -------------------------------------------------
132
protected void doValidation()
133   {
134     // Validate input
135
if (areaID == -1)
136       addErrorMessage("no area specified");
137         else
138         {
139             try
140             {
141                 area = areamgr.getArea(areaID);
142             }
143             catch(Exception JavaDoc e)
144             {
145                 addErrorMessage("error getting area ("+e.getMessage()+")");
146             }
147
148             if (area == null)
149                 addErrorMessage("invalid area ID specified");
150         }
151   }
152
153     protected String JavaDoc doExecute()
154   {
155     try
156     {
157       // handle time
158
if (setTime != NOT_SET)
159             areamgr.setRead(areaID, new Timestamp JavaDoc(setTime));
160
161             if (time == NOT_SET)
162                 time = System.currentTimeMillis();
163
164       // get messages
165
ParentIDItemTreeNode messagesTree = areamgr.getMessagesTree(areaID);
166
167             areaInfo = new AreaInfo(areamgr, areaID, showHidden, new Timestamp JavaDoc(time));
168
169             areaCustomizer = AbstractAreaCustomizer.getAreaCustomizer(areaInfo.area);
170
171             // find last page and assure that we're not out of bounds
172
if (pageID == -1 || pageID >= getNumberOfPages())
173                 pageID = getNumberOfPages()-1;
174             if (pageID < 0)
175                 pageID = 0;
176
177             // if there is nothing new, set the last item to be marked as the first new, this is for scrolling purposes
178
if (areaInfo.firstnewID == 0 && areaInfo.lastMessage != null)
179                 areaInfo.firstnewID = areaInfo.lastMessage.getId();
180
181             // adjust startPageID for new messages
182
startPageID = pageID;
183             for (int i = 0; i < areaInfo.pages.size() && i < pageID; i++)
184             {
185                 if (((Page)areaInfo.pages.get(i)).getHasNewMessages())
186                 {
187                     startPageID = i;
188                     break;
189                 }
190             }
191
192             //((Page)areaInfo.pages.get(startPageID-1)).getHasNewMessages())
193
//startPageID--;
194

195             // get maxIndent
196
Page page = (Page)areaInfo.pages.get(pageID);
197             Page startPage = (Page)areaInfo.pages.get(startPageID);
198             for (
199                 Iterator JavaDoc i =
200                     new FilterMessagesIterator(showHidden,
201                         new SubsetIterator(startPage.getStartIndex(), page.getEndIndex(),
202                             new TreeIterator(messagesTree)));
203                 i.hasNext();)
204             {
205                 ParentIDItemTreeNode foo = (ParentIDItemTreeNode)i.next();
206                 if (foo.indent > maxIndent)
207                     maxIndent = foo.indent;
208             }
209
210             if (startPage.getHasNewMessages())
211                 showingUnread = true;
212
213             // set final iterator to be used
214
messages =
215                 new FilterMessagesIterator(showHidden,
216                     new SubsetIterator(startPage.getStartIndex(), page.getEndIndex(),
217                         new TreeIterator(messagesTree)));
218
219       // handle time
220
if (time == NOT_SET)
221                 time = areaInfo.areaTime.getTime();
222
223       if (setTime == NOT_SET)
224                 areamgr.setRead(areaID);
225
226             timeTimestamp = new Timestamp JavaDoc(time);
227             timeTimestamp2 = new Timestamp JavaDoc(areaInfo.areaTime.getTime());
228
229       return SUCCESS;
230     }
231     catch (Exception JavaDoc e)
232     {
233       addErrorMessage("displaying area page failed, exception thrown ("+e.toString()+")");
234       e.printStackTrace();
235       return ERROR;
236     }
237   }
238 }
Popular Tags