/* $Id: AChildItem.txt 4889 2009-04-06 12:52:39Z nicklas $ Copyright (C) 2005 Nicklas Nordborg Copyright (C) 2006 Jari Häkkinen This file is part of BASE - BioArray Software Environment. Available at http://base.thep.lu.se/ BASE is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. BASE is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with BASE. If not, see . */ package net.sf.basedb.core; import net.sf.basedb.core.data.ChildData; import net.sf.basedb.core.data.SharedData; /** This class is used to represent a child item to {@link AnyItem} in BASE. @author Your name @version 2.0 @see ChildData @base.modified $Date: 2009-04-06 14:52:39 +0200 (må, 06 apr 2009) $ */ public class AChildItem extends ChildItem implements Nameable { private static final QueryRuntimeFilter RUNTIME_FILTER = new QueryRuntimeFilterFactory.ChildFilter(Item.CHILDITEM, Item.ANYITEM); /** Create a new ChildItem item. @param dc The DbControl which will be used for permission checking and database access @param parent The AnyItem which is the parent @return The new ChildItem item @throws BaseException If there is an error @see AnyItem#newChildItem() */ public static AChildItem getNew(DbControl dc, AnyItem parent) throws BaseException { AChildItem c = dc.newItem(AChildItem.class); c.setParent(parent); c.setName("New child item"); return c; } /** Get a ChildItem item when you know the id. @param dc The DbControl which will be used for permission checking and database access. @param id The id of the item to load @return The ChildItem item @throws ItemNotFoundException If an item with the specified id is not found @throws PermissionDeniedException If the logged in user doesn't have {@link Permission#READ} permission to the item @throws BaseException If there is another error */ public static AChildItem getById(DbControl dc, int id) throws ItemNotFoundException, PermissionDeniedException, BaseException { AChildItem c = dc.loadItem(AChildItem.class, id); if (c == null) throw new ItemNotFoundException("AChildItem[id="+id+"]"); return c; } /** Get a query configured to retrieve children for the specified parent. @param parent The parent to retreive child items for, null is allowed if the logged in user has generic READ permission for AnyItem:s in which case all ChildItems will be returned @return An {@link ItemQuery} object @see AnyItem#getChildItems() */ public static ItemQuery getQuery(AnyItem parent) { Query query = null; if (parent != null) { query = new ItemQuery(AChildItem.class, null); query.restrictPermanent( Restrictions.eq( Hql.property("anyItem"), Hql.entity(parent) ) ); } else { query = new ItemQuery(AChildItem.class, RUNTIME_FILTER); } return query; } // Constructor AChildItem(ChildData childData) { super(childData); } /* From the Nameable interface ------------------------------------------- */ public String getName() { return getData().getName(); } public void setName(String name) throws PermissionDeniedException, InvalidDataException { checkPermission(Permission.WRITE); NameableUtil.setName(getData(), name); } public String getDescription() { return getData().getDescription(); } public void setDescription(String description) throws PermissionDeniedException, InvalidDataException { checkPermission(Permission.WRITE); NameableUtil.setDescription(getData(), description); } // ------------------------------------------- /* From the BasicItem class ------------------------------------------- */ /** Always return FALSE. It is very seldom a child item is referenced by some other item than the parent item. */ public boolean isUsed() throws BaseException { return false; } // ------------------------------------------- /* From the ChildItem class ------------------------------------------- */ SharedData getSharedParent() { return getData().getParent(); } // ------------------------------------------- /** Get the parent AnyItem this child item belongs to. @return The AnyItem item @throws PermissionDeniedException If the logged in user doesn't have {@link Permission#READ} permission @throws BaseException If there is another error */ public AnyItem getParent() throws PermissionDeniedException, BaseException { return getDbControl().getItem(AnyItem.class, getData().getParent()); } /** Set the parent AnyItem this child item belongs to. Can only be set on a new item. @throws InvalidDataException If the parent is null */ void setParent(AnyItem parent) throws PermissionDeniedException, InvalidDataException { checkPermission(Permission.WRITE); if (parent == null) throw new InvalidUseOfNullException("parent"); getData().setParent(parent.getData()); } }