1    /*
2     * Copyright 2009 :torweg free software group
3     *
4     * This program is free software: you can redistribute it and/or modify
5     * it under the terms of the GNU General Public License as published by
6     * the Free Software Foundation, either version 3 of the License, or
7     * (at your option) any later version.
8     * 
9     * This program is distributed in the hope that it will be useful,
10    * but WITHOUT ANY WARRANTY; without even the implied warranty of
11    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    * GNU General Public License for more details.
13    * 
14    * You should have received a copy of the GNU General Public License
15    * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16    *
17    */
18   package org.torweg.pulse.accesscontrol;
19   
20   import java.util.List;
21   
22   import net.sf.json.JSONObject;
23   
24   import org.hibernate.Criteria;
25   import org.hibernate.Session;
26   import org.hibernate.criterion.Order;
27   import org.hibernate.criterion.Restrictions;
28   import org.torweg.pulse.bundle.JDOMable;
29   import org.torweg.pulse.util.entity.AbstractNamableEntity;
30   
31   /**
32    * Defines JSON-support and helper-methods for entities in the access-control.
33    * 
34    * @author Daniel Dietz
35    * @version $Revision: 2013 $
36    */
37   public abstract class AbstractAccessControlObject extends AbstractNamableEntity
38           implements JDOMable {
39   
40       /**
41        * The serialVersionUID.
42        */
43       private static final long serialVersionUID = 8971330317680818769L;
44   
45       /**
46        * Serializes the state of the {@code AbstractAccessControlObject} to a
47        * {@code JSONObject}.
48        * <p>
49        * Is supposed to be overridden by extending sub-classes.
50        * </p>
51        * 
52        * @return the {@code AbstractAccessControlObject} as a {@code JSONObject}
53        */
54       public JSONObject toJSON() {
55           JSONObject json = new JSONObject();
56           json.put("clazz", getClass().getCanonicalName());
57           // id : if available, [n.a.] otherwise
58           if (getId() != null) {
59               json.put("id", getId());
60           } else {
61               json.put("id", "[n.a.]");
62           }
63           return json;
64       }
65   
66       /**
67        * Loads the {@code <? extends AbstractAccessControlObject>}s specified by
68        * clazz associated with the {@code <? extends AbstractAccessControlObject>}
69        * s specified by alias specified further by id.
70        * 
71        * @param s
72        *            the hibernate<sup>TM</sup>-{@code Session}
73        * @param clazz
74        *            the {@code Class<? extends AbstractAccessControlObject>}
75        * @param alias
76        *            the alias
77        * @param id
78        *            the id
79        * 
80        * @return a {@code List<? extends AbstractAccessControlObject>}
81        */
82       protected final List<? extends AbstractAccessControlObject> getAssociatedAbstractAccessControlObjects(
83               final Session s,
84               final Class<? extends AbstractAccessControlObject> clazz,
85               final String alias, final Long id) {
86           @SuppressWarnings("unchecked")
87           List<AbstractAccessControlObject> acObjects = (List<AbstractAccessControlObject>) getLoadCriteriaForClassWithAlias(
88                   s, clazz, alias, id).addOrder(Order.asc("name").ignoreCase()).list();
89           return acObjects;
90       }
91   
92       /**
93        * Returns a {@code Criteria} to load the
94        * {@code AbstractAccessControlObject}s associated with the given
95        * {@code Class<?
96        * extends AbstractBasicEntity>}.
97        * 
98        * @param s
99        *            the hibernate<sup>TM</sup> {@code Session}
100       * @param c
101       *            the {@code Class<? extends AbstractBasicEntity>} to build the
102       *            {@code Criteria} for
103       * @param alias
104       *            the alias to be used
105       * @param id
106       *            the id of the {@code AbstractAccessControlObject}
107       * 
108       * @return a {@code Criteria} to load the
109       *         {@code AbstractAccessControlObject}s associated with the given
110       *         {@code Class<? extends AbstractBasicEntity>}
111       */
112      protected final Criteria getLoadCriteriaForClassWithAlias(final Session s,
113              final Class<? extends AbstractAccessControlObject> c,
114              final String alias, final Long id) {
115          Criteria criteria = s.createCriteria(c).createAlias(alias, "a")
116                  .add(Restrictions.eq("a.id", id));
117          return criteria;
118      };
119  
120  }
121