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

Quick Search    Search Deep

Source code: com/RuntimeCollective/permission/bean/PermissibleExtension.java


1   /* $Header: /home/CVS/rjp/src/com/RuntimeCollective/permission/bean/PermissibleExtension.java,v 1.5 2003/10/13 15:42:42 fabrice Exp $
2    * $Revision: 1.5 $
3    * $Date: 2003/10/13 15:42:42 $
4    *
5    * ====================================================================
6    *
7    * Josephine : http://www.runtime-collective.com/josephine/index.html
8    *
9    * Copyright (C) 2003 Runtime Collective
10   * 
11   * This product includes software developed by the
12   * Apache Software Foundation (http://www.apache.org/).
13   *
14   * This library is free software; you can redistribute it and/or
15   * modify it under the terms of the GNU Lesser General Public
16   * License as published by the Free Software Foundation; either
17   * version 2.1 of the License, or (at your option) any later version.
18   *
19   * This library is distributed in the hope that it will be useful,
20   * but WITHOUT ANY WARRANTY; without even the implied warranty of
21   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22   * Lesser General Public License for more details.
23   *
24   * You should have received a copy of the GNU Lesser General Public
25   * License along with this library; if not, write to the Free Software
26   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
27   *
28   */
29  
30  package com.RuntimeCollective.permission.bean;
31  
32  import com.RuntimeCollective.permission.PermissionException;
33  import com.RuntimeCollective.permission.bean.Permissible;
34  import com.RuntimeCollective.permission.bean.SimplePermissible;
35  import com.RuntimeCollective.webapps.RuntimeDataSource;
36  import com.RuntimeCollective.webapps.RuntimeParameters;
37  import com.RuntimeCollective.webapps.bean.EntityBean;
38  
39  import java.sql.SQLException;
40  import java.util.HashMap;
41  import java.util.Iterator;
42  
43  /**
44   * An extension which can be used to stick the Permissible behaviour to any bean.
45   *
46   * @version $Id: PermissibleExtension.java,v 1.5 2003/10/13 15:42:42 fabrice Exp $
47   */
48  public class PermissibleExtension extends SimplePermissible implements Permissible {
49  
50      private static final String SELECT_ID = "select id from "; 
51      private static final String WHERE_BEAN_ID = " where bean_id = ";
52      private static final String DELETE_FROM = "delete from ";
53      private static final String WHERE_ID = " where id = ";
54      private static final String SELECT_DATA = "select bean_id from "; 
55  
56      private static final String ESC = "'";
57      private static final String EMPTY_STRING = "";
58      private static final String SPACE = " ";
59  
60      public static final String DATABASE_TABLE = "permission_permext";
61      private static final String FIELD_BEAN_ID = "bean_id";
62  
63  
64      /**
65       * Save this bean in the database.
66       */
67      public void save() {
68  
69          // clear the cache for this group, if not clustered
70          if (!RuntimeParameters.isClustered()) {
71              clearFromExtIdCache(getId());
72          }
73  
74          super.save();
75  
76          // beanId shouldn't be null, really
77          try {
78              Integer beanInt = (beanId == EntityBean.NULL_ID) ? null : new Integer(beanId);
79              RuntimeDataSource.save(id, DATABASE_TABLE,
80                                     new String[] { FIELD_BEAN_ID },
81                                     new Object[] { beanInt }
82                                     );
83          } catch (SQLException e) {
84              RuntimeParameters.logError(this, "Could not save.", e);
85              e.printStackTrace();
86          }
87      }
88  
89      /**
90       * Delete this bean from the database.
91       */
92      public void delete() {
93  
94          // clear the cache for this group, if not clustered
95          if (!RuntimeParameters.isClustered()) {
96              clearFromExtIdCache(getId());
97          }
98  
99          try {
100             RuntimeDataSource.update(DELETE_FROM+DATABASE_TABLE+WHERE_ID+id);
101         } catch (SQLException e) {
102             RuntimeParameters.logError(this, "Could not delete.", e);
103             e.printStackTrace();
104         }
105 
106         super.delete();
107     }
108 
109     /**
110      * Constructs a new blank bean with a unique id.
111      */
112     public PermissibleExtension() throws SQLException {
113         super();
114     }
115 
116     /**
117      * Gets a bean from the RuntimeDataSource, given an id.
118      *
119      * @param id id of the Moderated bean
120      * @exception SQLException thrown if no bean with such an id exits
121      */    
122     public PermissibleExtension(int id) throws SQLException {
123 
124         super(id);
125 
126         Object[] results = RuntimeDataSource.queryRow(SELECT_DATA+DATABASE_TABLE+WHERE_ID+id);
127         if (results.length != 1) {
128             throw new SQLException("Cannot load PermissibleExtension with id="+id+" : "
129                                    +results.length+" fields found in "+DATABASE_TABLE+".");
130         }
131 
132         if (results[0] != null)
133             beanId = Integer.parseInt(results[0].toString());
134         else
135             beanId = EntityBean.NULL_ID;
136     }
137 
138     /** The id of the EntityBean this PermissibleExtension is for. */
139     protected int beanId = EntityBean.NULL_ID;
140 
141     /** Gets the bean. */
142     public EntityBean getEntityBean() {
143         return (EntityBean) RuntimeParameters.getStore().get(EntityBean.class.getName(), beanId);
144     }
145 
146     /** Sets the bean. */
147     public void setEntityBean(EntityBean bean) {
148         if (bean == null)
149             this.beanId = EntityBean.NULL_ID;
150         else
151             this.beanId = bean.getId();
152     }
153     
154     public String toString() {
155         return PermissibleExtension.class.getName() + " " + id + " ( " + super.toString() + " )";
156     }
157 
158     // -------------------- PermissibleExtension statics -------------------------        
159     
160     /**
161      * Gets the PermissibleExtension for a particular bean, if one exists.
162      */
163     public static PermissibleExtension getFor(EntityBean bean) {
164 
165         if (bean == null) {
166             return null;
167         }
168 
169         Integer beanId = new Integer(bean.getId());
170         int extId = EntityBean.NULL_ID;
171 
172         // try the cache, if not clustered
173         if (!RuntimeParameters.isClustered()) {
174             Integer idInt = (Integer) extIdCache.get(beanId);
175             if (idInt != null) {
176                 extId = ((Integer) idInt).intValue();
177             }
178         }
179 
180         // hit the db if we don't know the id already
181         boolean foundInDb = false;
182         if (extId == EntityBean.NULL_ID) {
183             try {
184                 int[] ids = RuntimeDataSource.queryInts(SELECT_ID+DATABASE_TABLE+WHERE_BEAN_ID+bean.getId());
185                 if (ids.length > 0) {
186                     extId = ids[0];
187                     foundInDb = true;
188                 }
189                 
190             } catch (SQLException e) {
191                 RuntimeParameters.logError("PermissibleExtension", "Could not getFor.", e);
192                 e.printStackTrace();
193             }
194         }
195 
196         // if not clustered, and if found in db, update the cache
197         if (!RuntimeParameters.isClustered() && foundInDb) {
198             extIdCache.put(beanId, new Integer(extId));
199         }
200 
201         // return what we've found, if anything
202         if (extId == EntityBean.NULL_ID) {
203             return null;
204         } else {
205             return (PermissibleExtension) RuntimeParameters.getStore().get(PermissibleExtension.class.getName(), extId);
206         }
207     }
208 
209     /** Cache of beanId-id mappings, used only if not clustered. */
210     private static HashMap extIdCache = new HashMap();
211 
212     /** To notice the cache of stale data. */
213     public void clearFromExtIdCache(int extId) {
214         Integer idInt = new Integer(extId);
215 
216         if (extIdCache.containsValue(idInt)) {
217             Integer beanId;
218             for (Iterator it = extIdCache.keySet().iterator(); it.hasNext(); ) {
219                 beanId = (Integer) it.next();
220                 if (extId == beanId.intValue()) {
221                     extIdCache.remove(beanId);
222                     break;
223                 }
224             }
225         }
226     } 
227 
228     /**
229      * Gets or creates the PermissibleExtension for a particular bean.
230      * <p>
231      * NOTE: newly created PermissibleExtension objects are NOT saved -
232      * you'll need to do that.
233      */
234     public static PermissibleExtension getOrCreateFor(EntityBean bean) {
235 
236         if (bean == null)
237             return null;
238   
239         PermissibleExtension permExt = PermissibleExtension.getFor(bean);
240         if (permExt != null) {
241             return permExt;
242         }
243 
244         // ok we'll need to create it
245         permExt = (PermissibleExtension) RuntimeParameters.getStore().create(PermissibleExtension.class.getName());
246         permExt.setEntityBean(bean);
247         
248         // not saving it - caller is responsible!
249         return permExt;
250     }    
251 }
252 
253 
254 
255