KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > killingar > forum > actions > hotlist > AreaGroups


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.hotlist;
27
28 import net.killingar.forum.actions.Utils;
29 import net.killingar.forum.internal.Area;
30 import net.killingar.forum.internal.IDItemImpl;
31 import net.killingar.forum.internal.managers.AreaManager;
32 import net.killingar.forum.internal.managers.TimeManager;
33
34 import java.sql.SQLException JavaDoc;
35 import java.sql.Timestamp JavaDoc;
36 import java.util.ArrayList JavaDoc;
37 import java.util.List JavaDoc;
38 import java.util.Map JavaDoc;
39
40 public class AreaGroups extends ActionHotlistSupport implements webwork.action.ParameterAware
41 {
42     TimeManager timemgr;
43     List JavaDoc areaGroups;
44     boolean addAreaGroupAccess;
45     Map JavaDoc parameters;
46
47     public void setSession(Map JavaDoc map)
48     {
49         super.setSession(map);
50         synchronized (map)
51         {
52             try
53             {
54                 timemgr = (TimeManager)super.manager.getManager(TimeManager.class.getName());
55                 if (timemgr == null)
56                     addErrorMessage("failed to initialize time manager, null returned ");
57             }
58             catch (Exception JavaDoc exception)
59             {
60                 addErrorMessage("failed to initialize time manager, exception thrown: " + exception.toString());
61             }
62         }
63     }
64
65     public void setParameters(Map JavaDoc parameters) { this.parameters = parameters; }
66     public List JavaDoc getAreaGroups() { return areaGroups; }
67     public boolean getAddAreaGroupAccess() { return addAreaGroupAccess; }
68
69     static class AreaGroupData
70     {
71         net.killingar.forum.internal.AreaGroup areaGroup;
72         boolean unread;
73         String JavaDoc indent;
74         boolean subscribePassive;
75         boolean subscribeActive;
76         boolean subscribeNone;
77
78         public net.killingar.forum.internal.AreaGroup getAreaGroup() { return areaGroup; }
79         public boolean getUnread() { return unread; }
80         public String JavaDoc getIndent() { return indent; }
81         public boolean getSubscribePassive() { return subscribePassive; }
82         public boolean getSubscribeActive() { return subscribeActive; }
83         public boolean getSubscribeNone() { return subscribeNone; }
84
85         AreaGroupData(net.killingar.forum.internal.AreaGroup areagroup, boolean unread, long indent, boolean subscribePassive, boolean subscribeActive)
86         {
87             areaGroup = areagroup;
88             this.unread = unread;
89             this.indent = Utils.buildStringOnlyIndent(indent);
90             this.subscribePassive = subscribePassive;
91             this.subscribeActive = subscribeActive;
92             if (!subscribePassive && !subscribeActive)
93                 subscribeNone = true;
94             else
95                 subscribeNone = false;
96         }
97     }
98
99     public void prepare() throws Exception JavaDoc
100     {
101         net.killingar.forum.internal.AreaGroup areaGroups[] = super.areamgr.getAreaGroups();
102
103         long indent[] = new long[areaGroups.length];
104         Utils.buildFlatTree(areaGroups, indent);
105
106         for (int k = 0; k < areaGroups.length; k++)
107             if (areaGroups[k] == null)
108                 throw new NullPointerException JavaDoc(Long.toString(k));
109
110         boolean unread[] = getUnread(super.areamgr, timemgr, areaGroups);
111         this.areaGroups = new ArrayList JavaDoc(areaGroups.length);
112         addAreaGroupAccess = super.manager.hasAccess(super.manager.getUserID(), 1024L);
113
114         if (parameters.containsKey("save"))
115         {
116             for (int i = 0; i < areaGroups.length; i++)
117             {
118                 String JavaDoc key = Long.toString(areaGroups[i].getId());
119                 if (parameters.containsKey(key))
120                 {
121                     String JavaDoc[] values = (String JavaDoc[])parameters.get(key);
122
123                     if (values != null && values.length != 0)
124                     {
125                         Area[] areas = areamgr.getAreasInGroup(areaGroups[i].getId());
126                         boolean passive[] = new boolean[areas.length];
127                         boolean active[] = new boolean[areas.length];
128
129                         for (int j = 0; j < areas.length; j++)
130                         {
131                             passive[j] = areaHotlistmgr.hasArea(areas[j].getId(), 1);
132                             active[j] = areaHotlistmgr.hasArea(areas[j].getId(), 0);
133                         }
134
135                         boolean changed = false;
136
137                         if (values[0].equals("passive"))
138                         {
139                             if (!areaHotlistmgr.hasAreaGroup(areaGroups[i].getId(), 1))
140                             {
141                 areaHotlistmgr.removeAreaGroup(areaGroups[i].getId(), 0);
142                                 areaHotlistmgr.addAreaGroup(areaGroups[i].getId(), 1);
143
144                                 changed = true;
145                             }
146                         }
147                         else if (values[0].equals("active"))
148                         {
149                             if (!areaHotlistmgr.hasAreaGroup(areaGroups[i].getId(), 0))
150                             {
151                 areaHotlistmgr.removeAreaGroup(areaGroups[i].getId(), 1);
152                                 areaHotlistmgr.addAreaGroup(areaGroups[i].getId(), 0);
153
154                                 changed = true;
155                             }
156                         }
157                         else if (values[0].equals("none"))
158                         {
159                             if (areaHotlistmgr.hasAreaGroup(areaGroups[i].getId(), 0) || areaHotlistmgr.hasAreaGroup(areaGroups[i].getId(), 1))
160                             {
161                 areaHotlistmgr.removeAreaGroup(areaGroups[i].getId(), 0);
162                                 areaHotlistmgr.removeAreaGroup(areaGroups[i].getId(), 1);
163
164                                 changed = true;
165                             }
166                         }
167
168                         // fix the areas
169
if (changed)
170                         {
171                             for (int j = 0; j < areas.length; j++)
172                             {
173                                 areaHotlistmgr.removeArea(areas[j].getId(), 0);
174                                 areaHotlistmgr.removeArea(areas[j].getId(), 1);
175
176                                 if (passive[j])
177                                 {
178                                     areaHotlistmgr.addArea(areas[j].getId(), 1);
179                                 }
180                                 else if (active[j])
181                                 {
182                                     areaHotlistmgr.addArea(areas[j].getId(), 0);
183                                 }
184                                 // else // none
185
}
186                         }
187                     }
188                 }
189             }
190         }
191
192         for (int i = 0; i < areaGroups.length; i++)
193         {
194             this.areaGroups.add(new AreaGroupData(areaGroups[i], unread[i], indent[i], areaHotlistmgr.hasAreaGroup(areaGroups[i].getId(), 1), areaHotlistmgr.hasAreaGroup(areaGroups[i].getId(), 0)));
195             if (super.areamgr.hasAccess(super.manager.getUserID(), areaGroups[i].getId(), 1024L))
196                 addAreaGroupAccess = true;
197         }
198     }
199
200     protected String JavaDoc doExecute()
201     {
202         return SUCCESS;
203     }
204
205     private final boolean[] getUnread(AreaManager areamanager, TimeManager timemanager, net.killingar.forum.internal.AreaGroup aareagroup[])
206         throws SQLException JavaDoc
207     {
208         Area aarea[] = areamanager.getAreas();
209         long al[] = new long[aarea.length];
210         for (int i = 0; i < al.length; i++)
211             al[i] = ((IDItemImpl)(aarea[i])).ID;
212
213         Timestamp JavaDoc atimestamp[] = timemanager.getTimes(TimeManager.systemAreas, al);
214         Timestamp JavaDoc atimestamp1[] = timemanager.getUserDataTimes(TimeManager.systemAreas, al);
215         boolean aflag[] = new boolean[aareagroup.length];
216         for (int j = 0; j < aareagroup.length; j++)
217             aflag[j] = false;
218
219         for (int k = 0; k < aareagroup.length; k++)
220         {
221             for (int l = 0; l < aarea.length; l++)
222             {
223                 if (aarea[l].areaGroupID !=
224                     aareagroup[k].ID)
225                     continue;
226                 if (atimestamp1[l] == null || !atimestamp1[l].before(atimestamp[l]))
227                     continue;
228                 aflag[k] = true;
229                 break;
230             }
231     }
232
233     return aflag;
234     }
235 }
236
Popular Tags