Coverage Report - org.melati.poem.GroupCapabilityTable
 
Classes in this File Line Coverage Branch Coverage Complexity
GroupCapabilityTable
100%
18/18
100%
4/4
2
 
 1  
 /*
 2  
  * $Source$
 3  
  * $Revision$
 4  
  *
 5  
  * Copyright (C) 2000 William Chesters
 6  
  *
 7  
  * Part of Melati (http://melati.org), a framework for the rapid
 8  
  * development of clean, maintainable web applications.
 9  
  *
 10  
  * Melati is free software; Permission is granted to copy, distribute
 11  
  * and/or modify this software under the terms either:
 12  
  *
 13  
  * a) the GNU General Public License as published by the Free Software
 14  
  *    Foundation; either version 2 of the License, or (at your option)
 15  
  *    any later version,
 16  
  *
 17  
  *    or
 18  
  *
 19  
  * b) any version of the Melati Software License, as published
 20  
  *    at http://melati.org
 21  
  *
 22  
  * You should have received a copy of the GNU General Public License and
 23  
  * the Melati Software License along with this program;
 24  
  * if not, write to the Free Software Foundation, Inc.,
 25  
  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA to obtain the
 26  
  * GNU General Public License and visit http://melati.org to obtain the
 27  
  * Melati Software License.
 28  
  *
 29  
  * Feel free to contact the Developers of Melati (http://melati.org),
 30  
  * if you would like to work out a different arrangement than the options
 31  
  * outlined here.  It is our intention to allow Melati to be used by as
 32  
  * wide an audience as possible.
 33  
  *
 34  
  * This program is distributed in the hope that it will be useful,
 35  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 36  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 37  
  * GNU General Public License for more details.
 38  
  *
 39  
  * Contact details for copyright holder:
 40  
  *
 41  
  *     William Chesters <williamc At paneris.org>
 42  
  *     http://paneris.org/~williamc
 43  
  *     Obrechtstraat 114, 2517VX Den Haag, The Netherlands
 44  
  */
 45  
 
 46  
 package org.melati.poem;
 47  
 
 48  
 import java.util.Enumeration;
 49  
 
 50  
 import org.melati.poem.generated.GroupCapabilityTableBase;
 51  
 
 52  
 /**
 53  
  * The {@link Table} of {@link GroupCapability}s.
 54  
  *
 55  
  * Every Melati DB has this table.
 56  
  * This table will contain at least the tuple
 57  
  * <code>Melati database administrators</code>:<code>_administer_</code>. 
 58  
  *
 59  
  * Melati POEM generated, programmer modifiable stub 
 60  
  * for a <code>GroupCapabilityTable</code> object.
 61  
  * <p>
 62  
  * Description: 
 63  
  *   A record that users belonging to a given group possess a given 
 64  
  *   capability. 
 65  
  * </p>
 66  
  *
 67  
  * 
 68  
  * <table> 
 69  
  * <caption>
 70  
  * Field summary for SQL table <code>GroupCapability</code>
 71  
  * </caption>
 72  
  * <tr><th>Name</th><th>Type</th><th>Description</th></tr>
 73  
  * <tr><td> id </td><td> Integer </td><td> The Table Row Object ID </td></tr> 
 74  
  * <tr><td> group </td><td> Group </td><td> The user-group which has the 
 75  
  * capability </td></tr> 
 76  
  * <tr><td> capability </td><td> Capability </td><td> The capability 
 77  
  * </td></tr> 
 78  
  * </table> 
 79  
  * 
 80  
  * See  org.melati.poem.prepro.TableDef#generateTableMainJava 
 81  
  */
 82  
 
 83  
 public class GroupCapabilityTable<T extends GroupCapability> extends GroupCapabilityTableBase<T> {
 84  
 
 85  
  /**
 86  
   * Constructor.
 87  
   * 
 88  
   * See org.melati.poem.prepro.TableDef#generateTableMainJava 
 89  
   * @param database          the POEM database we are using
 90  
   * @param name              the name of this <code>Table</code>
 91  
   * @param definitionSource  which definition is being used
 92  
   * @throws PoemException    if anything goes wrong
 93  
   */
 94  
   public GroupCapabilityTable(
 95  
       Database database, String name,
 96  
       DefinitionSource definitionSource) throws PoemException {
 97  64
     super(database, name, definitionSource);
 98  64
   }
 99  
 
 100  
   // programmer's domain-specific code here
 101  
 
 102  
   public void postInitialise() {
 103  41
     super.postInitialise();
 104  
 
 105  41
     Database d = getDatabase();
 106  41
     GroupCapability admin = (GroupCapability)newPersistent();
 107  41
     admin.setGroup(d.getGroupTable().administratorsGroup());
 108  41
     admin.setCapability(d.administerCapability());
 109  
 
 110  41
     if (!exists(admin))
 111  20
       create(admin);
 112  
 
 113  41
   }
 114  
   
 115  
   /**
 116  
    * Make sure that a record exists.
 117  
    *
 118  
    * @return the existing or newly created {@link GroupCapability}
 119  
    */
 120  
   public GroupCapability ensure(Group group, Capability capability) {
 121  4
      GroupCapability p = (GroupCapability)newPersistent();
 122  4
      p.setGroup(group);
 123  4
      p.setCapability(capability);
 124  4
      Enumeration<T> them = selection(p);
 125  4
      if (them.hasMoreElements()) 
 126  1
        return (GroupCapability)them.nextElement();
 127  
      else {
 128  3
        p.makePersistent();
 129  3
        return p;
 130  
      }
 131  
    }
 132  
 
 133  
 }