source: trunk/doc/src/docbook/examples/AChildItem.java.txt @ 5675

Last change on this file since 5675 was 5675, checked in by Nicklas Nordborg, 11 years ago
  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Id
File size: 5.2 KB
Line 
1/*
2  $Id $
3
4  Copyright (C) 2011 Your name
5
6  This file is part of BASE - BioArray Software Environment.
7  Available at http://base.thep.lu.se/
8
9  BASE is free software; you can redistribute it and/or
10  modify it under the terms of the GNU General Public License
11  as published by the Free Software Foundation; either version 3
12  of the License, or (at your option) any later version.
13
14  BASE is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  GNU General Public License for more details.
18
19  You should have received a copy of the GNU General Public License
20  along with BASE. If not, see <http://www.gnu.org/licenses/>.
21*/
22package net.sf.basedb.core;
23import net.sf.basedb.core.data.ChildData;
24import net.sf.basedb.core.data.SharedData;
25/**
26  This class is used to represent a child item to {@link AnyItem} in BASE.
27
28  @author Your name
29  @since 3.0
30  @see ChildData
31  @base.modified $Date: 2011-06-28 13:19:45 +0000 (Tue, 28 Jun 2011) $
32*/
33public class AChildItem
34  extends ChildItem<AnyChildData>
35  implements Nameable
36{
37
38  private static final QueryRuntimeFilter RUNTIME_FILTER =
39    new QueryRuntimeFilterFactory.ChildFilter(Item.CHILDITEM, Item.ANYITEM);
40
41  /**
42    Create a new <code>ChildItem</code> item.
43
44    @param dc The <code>DbControl</code> which will be used for
45      permission checking and database access
46    @param parent The <code>AnyItem</code> which is the parent
47    @return The new <code>ChildItem</code> item
48    @throws BaseException If there is an error
49    @see AnyItem#newChildItem()
50  */
51  public static AChildItem getNew(DbControl dc, AnyItem parent)
52    throws BaseException
53  {
54    AChildItem c = dc.newItem(AChildItem.class);
55    c.setParent(parent);
56    c.setName("New child item");
57    return c;
58  }
59
60  /**
61    Get a <code>ChildItem</code> item when you know the id.
62
63    @param dc The <code>DbControl</code> which will be used for
64      permission checking and database access.
65    @param id The id of the item to load
66    @return The <code>ChildItem</code> item
67    @throws ItemNotFoundException If an item with the specified
68      id is not found
69    @throws PermissionDeniedException If the logged in user doesn't
70      have {@link Permission#READ} permission to the item
71    @throws BaseException If there is another error
72  */
73  public static AChildItem getById(DbControl dc, int id)
74    throws ItemNotFoundException, PermissionDeniedException, BaseException
75  {
76    AChildItem c = dc.loadItem(AChildItem.class, id);
77    if (c == null) throw new ItemNotFoundException("AChildItem[id="+id+"]");
78    return c;
79  }
80
81  /**
82    Get a query configured to retrieve children for the specified parent.
83   
84    @param parent The parent to retreive child items for, null is allowed if
85      the logged in user has generic READ permission for AnyItem:s in which case
86      all ChildItems will be returned
87    @return An {@link ItemQuery} object
88    @see AnyItem#getChildItems()
89  */
90  public static ItemQuery<AChildItem> getQuery(AnyItem parent)
91  {
92    Query<AChildItem> query = null;
93    if (parent != null)
94    {
95      query = new ItemQuery<AChildItem>(AChildItem.class, null);
96      query.restrictPermanent(
97        Restrictions.eq(
98          Hql.property("anyItem"),
99          Hql.entity(parent)
100        )
101      );
102    }
103    else
104    {
105      query = new ItemQuery<AChildItem>(AChildItem.class, RUNTIME_FILTER);
106    }
107    return query;
108  }
109
110  // Constructor
111  AChildItem(ChildData childData)
112  {
113    super(childData);
114  }
115
116  /*
117    From the Nameable interface
118    -------------------------------------------
119  */
120  public String getName()
121  {
122    return getData().getName();
123  }
124  public void setName(String name)
125    throws PermissionDeniedException, InvalidDataException
126  {
127    checkPermission(Permission.WRITE);
128    NameableUtil.setName(getData(), name);
129  }
130  public String getDescription()
131  {
132    return getData().getDescription();
133  }
134  public void setDescription(String description)
135    throws PermissionDeniedException, InvalidDataException
136  {
137    checkPermission(Permission.WRITE);
138    NameableUtil.setDescription(getData(), description);
139  }
140  // -------------------------------------------
141  /*
142    From the BasicItem class
143    -------------------------------------------
144  */
145  /**
146    Always return FALSE.
147    It is very seldom a child item is referenced by some other item than
148    the parent item.
149  */
150  public boolean isUsed()
151    throws BaseException
152  {
153    return false;
154  }
155  // -------------------------------------------
156  /*
157    From the ChildItem class
158    -------------------------------------------
159  */
160  SharedData getSharedParent()
161  {
162    return getData().getParent();
163  }
164  // -------------------------------------------
165
166  /**
167    Get the parent AnyItem this child item belongs to.
168    @return The <code>AnyItem</code> item
169    @throws PermissionDeniedException If the logged in user doesn't have
170      {@link Permission#READ} permission
171    @throws BaseException If there is another error
172  */
173  public AnyItem getParent()
174    throws PermissionDeniedException, BaseException
175  {
176    return getDbControl().getItem(AnyItem.class, getData().getParent());
177  }
178  /**
179    Set the parent AnyItem this child item belongs to. Can only be set
180    on a new item.
181    @throws InvalidDataException If the parent is null
182  */
183  void setParent(AnyItem parent)
184    throws PermissionDeniedException, InvalidDataException
185  {
186    checkPermission(Permission.WRITE);
187    if (parent == null) throw new InvalidUseOfNullException("parent");
188    getData().setParent(parent.getData());
189  }
190
191}
192 
Note: See TracBrowser for help on using the repository browser.