source: trunk/doc/src/docbook/examples/AnyItem.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: 8.8 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.AnyData;
24/**
25  This class is used to represent an AnyItem in BASE.
26
27  @author Your name
28  @since 3.0
29  @see AnyData
30  @base.modified $Date: 2011-06-28 13:19:45 +0000 (Tue, 28 Jun 2011) $
31*/
32public class AnyItem
33  extends CommonItem<AnyData>
34  implements Subtypable
35{
36
37  /**
38    Create a new <code>AnyItem</code> item.
39 
40    @param dc The <code>DbControl</code> which will be used for
41      permission checking and database access
42    @return The new <code>AnyItem</code> item
43    @throws BaseException If there is an error
44  */
45  public static AnyItem getNew(DbControl dc)
46    throws BaseException
47  {
48    AnyItem a = dc.newItem(AnyItem.class);
49    a.setName("New any item");
50    return a;
51  }
52
53  /**
54    Get an <code>AnyItem</code> item when you know the id.
55 
56    @param dc The <code>DbControl</code> which will be used for
57      permission checking and database access.
58    @param id The id of the item to load
59    @return The <code>AnyItem</code> item
60    @throws ItemNotFoundException If an item with the specified
61      id is not found
62    @throws PermissionDeniedException If the logged in user doesn't
63      have read permission to the item
64    @throws BaseException If there is another error
65  */
66  public static AnyItem getById(DbControl dc, int id)
67    throws ItemNotFoundException, PermissionDeniedException, BaseException
68  {
69    AnyItem a = dc.loadItem(AnyItem.class, id);
70    if (a == null) throw new ItemNotFoundException("AnyItem[id="+id+"]");
71    return a;
72  }
73
74  /**
75    Get a query configured to retrieve anyitems.
76    @return An {@link ItemQuery} object
77  */
78  public static ItemQuery<AnyItem> getQuery()
79  {
80    return new ItemQuery<AnyItem>(AnyItem.class);
81  }
82
83  // Constructor
84  AnyItem(AnyData anyData)
85  {
86    super(anyData);
87  }
88
89  /*
90    From the BasicItem class
91    -------------------------------------------
92  */
93  /**
94    TODO - Check if:
95    <ul>
96    <li>other items are using this AnyItem
97    </ul>
98    (or remove this method)
99  */
100  public boolean isUsed()
101    throws BaseException
102  {
103    org.hibernate.Session session = getDbControl().getHibernateSession();
104    org.hibernate.Query query =
105      HibernateUtil.getPredefinedQuery(session, "GET_OTHER_ITEM_FOR_ANYITEM", "count(*)");
106    /*
107      SELECT {1}
108      FROM OtherItemData o
109      WHERE o.anyItem = :anyItem
110    */
111    query.setEntity("anyItem", this.getData());
112    boolean used = HibernateUtil.loadData(Long.class, query) > 0;
113    return used || super.isUsed();
114  }
115 
116  /**
117    TODO - Get all:
118    <ul>
119    <li>other items using this AnyItem
120    <ul>
121    (or remove this method)
122  */
123  @Override
124  public Set<ItemProxy> getUsingItems()
125  {
126    Set<ItemProxy> using = super.getUsingItems();
127    org.hibernate.Session session = getDbControl().getHibernateSession();
128     
129    // Other items
130    org.hibernate.Query query = HibernateUtil.getPredefinedQuery(session,
131        "GET_OTHER_ITEM_FOR_ANYITEM", "o.id");
132    /*
133      SELECT {1}
134      FROM OtherItemData o
135      WHERE o.anyItem = :anyItem
136    */
137    query.setEntity("anyItem", this.getData());
138    addUsingItems(using, Item.OTHER, query);
139    return using;
140  }
141 
142  /**
143    TODO - grant or deny extra permissions or remove this method
144  */
145  void initPermissions(int granted, int denied)
146    throws BaseException
147  {
148    // Place your extra permissions here
149    super.initPermissions(granted, denied);
150  }
151  /**
152    TODO - set default items
153    (or remove this method
154  */
155  void setProjectDefaults(Project activeProject)
156    throws BaseException
157  {
158    // The protocol used to create the item
159    if (!protocolHasBeenSet)
160    {
161      ProtocolData protocol =
162        (ProtocolData)activeProject.findDefaultRelatedData(dc, this, Item.PROTOCOL, false);
163      if (protocol != null)
164      {
165        getData().setProtocol(protocol);
166        protocolHasBeenSet = true;
167      }
168    }
169  }
170  // -------------------------------------------
171  /*
172    From the Subtypable interface
173    -----------------------------
174  */
175  @Override
176  @SubtypableRelatedItems({Item.PROTOCOL})
177  public ItemSubtype getItemSubtype()
178  {
179    return getDbControl().getItem(ItemSubtype.class, getData().getItemSubtype());
180  }
181  @Override
182  public void setItemSubtype(ItemSubtype subtype)
183  {
184    checkPermission(Permission.WRITE);
185    if (subtype != null)
186    {
187      subtype.setOnItem(this);
188    }
189    else
190    {
191      getData().setItemSubtype(null);
192    }
193  }
194  // -------------------------------------------
195
196  // TODO - Methods below this line are examples only, modify or remove as needed
197  /**
198    Get the value of the string property.
199  */
200  public String getStringProperty()
201  {
202    return getData().getStringProperty();
203  }
204  public static final int MAX_STRINGPROPERTY_LENGTH = AnyData.MAX_STRINGPROPERTY_LENGTH;
205  /**
206    Set the value of the string property. Null values are not
207    allowed and the length must be shorter than
208    {@link #MAX_STRINGPROPERTY_LENGTH}.
209    @param value The new value
210    @throws PermissionDeniedException If the logged in user
211      doesn't have write permission
212    @throws InvalidDataException If the value is null or too long
213  */
214  public void setStringProperty(String value)
215    throws PermissionDeniedException, InvalidDataException
216  {
217    checkPermission(Permission.WRITE);
218    getData.setStringProperty(
219      StringUtil.setNotNullString(value, "stringProperty", MAX_STRINGPROPERTY_LENGTH)
220    );
221  }
222
223  /**
224    Get the value of the int property.
225  */
226  public int getIntProperty()
227  {
228    return getData().getIntProperty();
229  }
230  /**
231    Set the value of the int property. The value mustn't be less than
232    zero.
233    @param value The new value
234    @throws PermissionDeniedException If the logged in user
235      doesn't have write permission
236    @throws InvalidDataException If the value is less than zero
237  */
238  public void setIntProperty(int value)
239    throws PermissionDeniedException, InvalidDataException
240  {
241    checkPermission(Permission.WRITE);
242    getData.setIntProperty(
243      IntegerUtil.checkMin(value, "intProperty", 0)
244    );
245  }
246
247  /**
248    Get the value of the boolean property.
249  */
250  public boolean isBooleanProperty()
251  {
252    return getData().isBooleanProperty();
253  }
254  /**
255    Set the value of the boolean property.
256    @param value The new value
257    @throws PermissionDeniedException If the logged in user
258      doesn't have write permission
259  */
260  public void setBooleanProperty(boolean value)
261    throws PermissionDeniedException
262  {
263    checkPermission(Permission.WRITE);
264    getData.setBooleanProperty(value);
265  }
266
267  /**
268    Get the value of the date property.
269    @return A date object or null if unknown
270  */
271  public Date getDateProperty()
272  {
273    return DateUtil.copy(getData().getDateProperty());
274  }
275  /**
276    Set the value of the date property. Null values are allowed.
277    @param value The new value
278    @throws PermissionDeniedException If the logged in user
279      doesn't have write permission
280  */
281  public void setDateProperty(Date value)
282    throws PermissionDeniedException
283  {
284    checkPermission(Permission.WRITE);
285    getData().setDateProperty(DateUtil.setNullableDate(value, "dateProperty"));
286  }
287 
288  /**
289    Get the associated other item.
290    @return The <code>OtherItem</code> item
291    @throws PermissionDeniedException If the logged in user
292      doesn't have read permission
293    @throws BaseException If there is another error
294  */
295  public OtherItem getOtherItem()
296    throws PermissionDeniedException, BaseException
297  {
298    return getDbControl().getItem(OtherItem.class, getData().getOtherItem());
299  }
300  /**
301    Set the associated item. Null is not allowed.
302    @param other The other item
303    @throws PermissionDeniedException If the logged in user
304      doesn't have write permission
305    @throws InvalidDataException If the other item is null
306    @throws BaseException If there is another error
307  */
308  public void setOtherItem(OtherItem other)
309    throws PermissionDeniedException, InvalidDataException, BaseException
310  {
311    checkPermission(Permission.WRITE);
312    if (otherItem == null) throw new InvalidUseOfNullException("otherItem");
313    getData().setOtherItem(otherItem.getData());
314  }
315
316  /**
317    Create a child item for this any item.
318    @return The new <code>ChildItem</code> object
319    @throws PermissionDeniedException If the logged in user doesn't have
320      write permission
321    @throws BaseException If there is another error
322  */
323  public AChildItem newChildItem()
324    throws PermissionDeniedException, BaseException
325  {
326    checkPermission(Permission.WRITE);
327    return AChildItem.getNew(getDbControl(), this);
328  }
329 
330  /**
331    Get a query that will return all child items for this any item.
332    @return An {@link ItemQuery} object
333  */
334  public ItemQuery<AChildItem> getChildItems()
335  {
336    return AChildItem.getQuery(this);
337  }
338
339}
Note: See TracBrowser for help on using the repository browser.