Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: ru/gammalabs/ice/forum/MessageHomeImpl.java


1   /*
2    * $Id: MessageHomeImpl.java,v 1.4 2003/02/12 08:17:05 saterenkoav Exp $
3    *
4    * Copyright (c) 2002, by GammaLabs
5    * ==================================================================
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public
8    * License as published by the Free Software Foundation; either
9    * version 2.1 of the License, or (at your option) any later version.
10   *
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14   * Lesser General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public
17   * License along with this library; if not, write to the Free Software
18   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19   * ==================================================================
20   */
21  package ru.gammalabs.ice.forum;
22  
23  import ru.gammalabs.ice.structure.PartitionPermission;
24  import ru.gammalabs.ice.structure.PartitionPermissionTypes;
25  import ru.gammalabs.ice.framework.*;
26  import ru.gammalabs.ice.utils.Silencer;
27  
28  import java.util.*;
29  import java.sql.Connection;
30  
31  public class MessageHomeImpl extends RepresentativeFactory implements MessageHome
32  {
33    // TODO: разобраться почему кеш работает медленно (замечено, что на админской части работает на порядок быстрее)
34    private static Map partitionThreads;
35  
36    public Message findMessageById(long id)
37      throws ObjectNotFoundException, UnauthorizedAccessException
38    {
39      Message message;
40      Connection connection = null;
41      try
42      {
43        connection = DBUtil.getConnection();
44        MessageDAO dao = DAOConfiguration.getDefaultInstance().getMessageDAO(connection);
45        MessageModel model = dao.findMessageById(id);
46  
47        PartitionPermission permission = new PartitionPermission(model.getPartitionId(), PartitionPermissionTypes.READ);
48        this.getActor().checkPermission(permission);
49  
50        message = this.register(model);
51      }
52      finally
53      {
54        Silencer.close(connection);
55      }
56  
57      return message;
58    }
59  
60    public List findChildMessages(Message parentMessage)
61    {
62      List result = new ArrayList();
63      Connection connection = null;
64      try
65      {
66        connection = DBUtil.getConnection();
67        MessageDAO dao = DAOConfiguration.getDefaultInstance().getMessageDAO(connection);
68        List models;
69  
70        PartitionPermission permission = new PartitionPermission(parentMessage.getPartitionId(), PartitionPermissionTypes.CHANGE);
71  
72        if (this.getActor().hasPermission(permission))
73        {
74          models = dao.findAllMessagesByParentId(parentMessage.getId());
75        }
76        else
77        {
78          models = dao.findOpenedMessagesByParentId(parentMessage.getId());
79        }
80  
81        for (Iterator iterator = models.iterator(); iterator.hasNext();)
82        {
83          MessageModel model = (MessageModel)iterator.next();
84          Message message = this.register(model);
85          result.add(message);
86        }
87      }
88      finally
89      {
90        Silencer.close(connection);
91      }
92  
93      return result;
94    }
95  
96      private List findThreadsByPartitionId(long id)
97      throws UnauthorizedAccessException
98    {
99      PartitionPermission permission = new PartitionPermission(id, PartitionPermissionTypes.READ);
100     this.getActor().checkPermission(permission);
101 
102     List result = new ArrayList();
103     Connection connection = null;
104     try
105     {
106       connection = DBUtil.getConnection();
107       MessageDAO dao = DAOConfiguration.getDefaultInstance().getMessageDAO(connection);
108 
109       List models;
110 
111       permission = new PartitionPermission(id, PartitionPermissionTypes.CHANGE);
112 
113       // заменить в дальнейшем на проверку пермисий пользователя
114       if (this.getActor().hasPermission(permission))
115       {
116         models = dao.findAllThreadsByPartitionId(id);
117       }
118       else
119       {
120         models = dao.findOpenedThreadsByPartitionId(id);
121       }
122 
123       for (Iterator iterator = models.iterator(); iterator.hasNext();)
124       {
125         MessageModel model = (MessageModel)iterator.next();
126         Message message = this.register(model);
127         result.add(message);
128       }
129     }
130     finally
131     {
132       Silencer.close(connection);
133     }
134 
135     return result;
136   }
137 
138   public List findMessagesByPartitionId(long partitionId)
139     throws UnauthorizedAccessException
140   {
141     List threads = null;
142 
143     if (partitionThreads == null)
144     {
145       partitionThreads = new HashMap();
146     }
147 
148     if (!partitionThreads.containsKey(new Long(partitionId)))
149     {
150             threads = this.findThreadsByPartitionId(partitionId);
151       partitionThreads.put(new Long(partitionId), threads);
152     }
153     else
154     {
155       threads = (List) partitionThreads.get(new Long(partitionId));
156     }
157 
158     List plainThreads = new ArrayList();
159 
160     for (Iterator iterator = threads.iterator(); iterator.hasNext();)
161     {
162       Message message = (Message)iterator.next();
163       List thread = this.generateMessageList(message);
164       plainThreads.add(thread);
165     }
166 
167     return plainThreads;
168   }
169 
170   private List generateMessageList(Message message)
171     throws UnauthorizedAccessException
172   {
173     PartitionPermission permission = new PartitionPermission(message.getPartitionId(), PartitionPermissionTypes.READ);
174     this.getActor().checkPermission(permission);
175 
176     return this.generateMessageListRecursively(message, 0);
177   }
178 
179   private List generateMessageListRecursively(Message message, int level)
180   {
181         List messages = new ArrayList();
182 
183     message.setLevel(level);
184     messages.add(message);
185 
186     for (Iterator iterator = message.getChilds().iterator(); iterator.hasNext();)
187     {
188       Message msg = (Message)iterator.next();
189       List childs = this.generateMessageListRecursively(msg, level + 1);
190       if (!childs.isEmpty())
191       {
192         messages.addAll(childs);
193       }
194     }
195 
196     return messages;
197   }
198 
199   public void remove(Message message)
200       throws UnauthorizedAccessException
201   {
202     PartitionPermission permission = new PartitionPermission(message.getPartitionId(), PartitionPermissionTypes.CHANGE);
203     this.getActor().checkPermission(permission);
204 
205     Connection connection = null;
206     try
207     {
208       connection = DBUtil.getConnection();
209       MessageDAO dao = DAOConfiguration.getDefaultInstance().getMessageDAO(connection);
210       removeCascade(message, dao);
211     }
212     finally
213     {
214       Silencer.close(connection);
215     }
216   }
217 
218   private void removeCascade(Message message, MessageDAO dao)
219   {
220     List childMessages = message.getChilds();
221 
222     for (Iterator iterator = childMessages.iterator(); iterator.hasNext();)
223     {
224       Message childMessage = (Message)iterator.next();
225       this.removeCascade(childMessage, dao);
226     }
227 
228     dao.remove(message.getId());
229     reCache();
230   }
231 
232   public Message createRoot(long partitionId)
233     throws UnauthorizedAccessException
234   {
235     PartitionPermission permission = new PartitionPermission(partitionId, PartitionPermissionTypes.READ);
236     this.getActor().checkPermission(permission);
237 
238     Message message = new Message(this);
239     registerRepresentative(message);
240     message.setParentId(0);
241     message.setPartitionId(partitionId);
242     message.setPersonId(this.getActor().getActivePerson().getId());
243     message.setDate();
244 
245     return message;
246   }
247 
248   private Message register(MessageModel model)
249   {
250     Message message = new Message(model, this);
251     registerRepresentative(message);
252     return message;
253   }
254 
255   public void reCache()
256   {
257     partitionThreads = null;
258   }
259 }