KickJava   Java API By Example, From Geeks To Geeks.

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


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.Area;
29 import net.killingar.forum.internal.Message;
30 import net.killingar.forum.internal.ParentIDItemTreeNode;
31 import net.killingar.forum.internal.TreeIterator;
32 import net.killingar.forum.internal.managers.AreaManager;
33
34 import java.sql.Timestamp JavaDoc;
35 import java.util.ArrayList JavaDoc;
36 import java.util.Iterator JavaDoc;
37
38 public class AreaInfo
39 {
40     long maxIndent = 0;
41     int noMessages = 0;
42     int noVisibleMessages = 0;
43     long firstnewID = 0;
44     Timestamp JavaDoc areaTime, areaTime2;
45     ParentIDItemTreeNode node;
46     boolean showHidden = false;
47     int msgsPerPage, numberOfPages, firstPageLength;
48     AreaManager areamgr;
49
50     Message firstMessage;
51     Message lastMessage;
52     Area area;
53
54     ArrayList JavaDoc pages = new ArrayList JavaDoc();
55
56     AreaInfo(AreaManager inAreamgr, long inAreaID, boolean inShowHidden, Timestamp JavaDoc inTime) throws java.lang.Exception JavaDoc
57     {
58         areamgr = inAreamgr;
59         showHidden = inShowHidden;
60
61         areaTime = areamgr.getTimeOnArea(inAreaID);
62         areaTime2 = inTime;
63         area = areamgr.getArea(inAreaID);
64         node = areamgr.getMessagesTree(inAreaID);
65         msgsPerPage = areamgr.getMessagesPerPage();
66
67         calcAreaInfo();
68     }
69
70     private void calcAreaInfo()
71     {
72         // get visible and total length
73
noMessages = 0;
74         noVisibleMessages = 0;
75         for (Iterator i = new TreeIterator(node); i.hasNext(); noMessages++)
76         {
77             ParentIDItemTreeNode foo = (ParentIDItemTreeNode)i.next();
78
79             Message message = (Message)foo.item;
80
81             if (message.getVisible())
82                 noVisibleMessages++;
83         }
84
85         // calculate start and end indexes
86
int index = 0, pageIndex = 0, visibleIndex = 0;//, startIndex = 0;
87
Page page = new Page(), lastPage = null;
88
89         numberOfPages = noVisibleMessages/msgsPerPage+1;
90         firstPageLength = noVisibleMessages%msgsPerPage;
91
92         for (Iterator i = new TreeIterator(node); i.hasNext(); index++)
93         {
94             ParentIDItemTreeNode foo = (ParentIDItemTreeNode)i.next();
95             Message message = (Message)foo.item;
96
97             // first and last message
98
if (firstMessage == null)
99                 firstMessage = message;
100
101             //
102
if (message.getVisible())
103             {
104                 visibleIndex++;
105
106                 lastMessage = message;
107
108                 // new message?
109
if (message.getTimecreated().after(areaTime))
110                 {
111                     page.hasNewMessages = true;
112
113                     if (firstnewID == 0)
114                         firstnewID = message.getId();
115                 }
116
117                 if (message.getLastChanged().after(areaTime2))
118                     page.hasNewMessages = true;
119             }
120             else
121             {
122                 // we need to include ending deleted messages
123
if (lastPage != null && lastPage.endIndex == index-1)
124                 {
125                     // move page boundary
126
lastPage.endIndex = index;
127                     page.startIndex = index+1;
128                 }
129
130                 // showing deleted and new message?
131
if (showHidden)
132                 {
133                     lastMessage = message;
134
135                     if (message.getTimecreated().after(areaTime))
136                     {
137                         page.hasNewMessages = true;
138
139                         if (firstnewID == 0)
140                             firstnewID = message.getId();
141                     }
142
143                     if (message.getLastChanged().after(areaTime2))
144                         page.hasNewMessages = true;
145                 }
146             }
147
148             // if hit a page boundary
149
if ((pages.size() == 0 && visibleIndex == firstPageLength) || visibleIndex == msgsPerPage)
150             {
151                 visibleIndex = 0;
152
153                 page.endIndex = index;
154                 lastPage = page;
155                 pages.add(page);
156                 page = new Page(index+1);
157             }
158         }
159
160         if (pages.size() == 0)
161             pages.add(page);
162     }
163
164     public int getMaxIndent() { return (int)maxIndent; }
165     public int getNoMessages() { return noMessages; }
166     public long getFirstnewID() { return firstnewID; }
167     public Timestamp JavaDoc getAreaTime() { return areaTime; }
168     public Message getFirstMessage() { return firstMessage; }
169     public Message getLastMessage() { return lastMessage; }
170     public ParentIDItemTreeNode getNode() { return node; }
171     public Area getArea() { return area; }
172 }
Popular Tags