5675 |
28 Jun 11 |
nicklas |
1 |
/* |
5675 |
28 Jun 11 |
nicklas |
$Id $ |
5675 |
28 Jun 11 |
nicklas |
3 |
|
5675 |
28 Jun 11 |
nicklas |
Copyright (C) 2011 Your name |
5675 |
28 Jun 11 |
nicklas |
5 |
|
5675 |
28 Jun 11 |
nicklas |
This file is part of BASE - BioArray Software Environment. |
5675 |
28 Jun 11 |
nicklas |
Available at http://base.thep.lu.se/ |
5675 |
28 Jun 11 |
nicklas |
8 |
|
5675 |
28 Jun 11 |
nicklas |
BASE is free software; you can redistribute it and/or |
5675 |
28 Jun 11 |
nicklas |
modify it under the terms of the GNU General Public License |
5675 |
28 Jun 11 |
nicklas |
as published by the Free Software Foundation; either version 3 |
5675 |
28 Jun 11 |
nicklas |
of the License, or (at your option) any later version. |
5675 |
28 Jun 11 |
nicklas |
13 |
|
5675 |
28 Jun 11 |
nicklas |
BASE is distributed in the hope that it will be useful, |
5675 |
28 Jun 11 |
nicklas |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
5675 |
28 Jun 11 |
nicklas |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
5675 |
28 Jun 11 |
nicklas |
GNU General Public License for more details. |
5675 |
28 Jun 11 |
nicklas |
18 |
|
5675 |
28 Jun 11 |
nicklas |
You should have received a copy of the GNU General Public License |
5675 |
28 Jun 11 |
nicklas |
along with BASE. If not, see <http://www.gnu.org/licenses/>. |
5675 |
28 Jun 11 |
nicklas |
21 |
*/ |
5675 |
28 Jun 11 |
nicklas |
package net.sf.basedb.core; |
5675 |
28 Jun 11 |
nicklas |
import net.sf.basedb.core.data.ChildData; |
5675 |
28 Jun 11 |
nicklas |
import net.sf.basedb.core.data.SharedData; |
5675 |
28 Jun 11 |
nicklas |
25 |
/** |
5675 |
28 Jun 11 |
nicklas |
This class is used to represent a child item to {@link AnyItem} in BASE. |
5675 |
28 Jun 11 |
nicklas |
27 |
|
5675 |
28 Jun 11 |
nicklas |
@author Your name |
5675 |
28 Jun 11 |
nicklas |
@since 3.0 |
5675 |
28 Jun 11 |
nicklas |
@see ChildData |
5675 |
28 Jun 11 |
nicklas |
@base.modified $Date$ |
5675 |
28 Jun 11 |
nicklas |
32 |
*/ |
5675 |
28 Jun 11 |
nicklas |
public class AChildItem |
5675 |
28 Jun 11 |
nicklas |
extends ChildItem<AnyChildData> |
5675 |
28 Jun 11 |
nicklas |
implements Nameable |
5675 |
28 Jun 11 |
nicklas |
36 |
{ |
5675 |
28 Jun 11 |
nicklas |
37 |
|
5675 |
28 Jun 11 |
nicklas |
private static final QueryRuntimeFilter RUNTIME_FILTER = |
5675 |
28 Jun 11 |
nicklas |
new QueryRuntimeFilterFactory.ChildFilter(Item.CHILDITEM, Item.ANYITEM); |
5675 |
28 Jun 11 |
nicklas |
40 |
|
5675 |
28 Jun 11 |
nicklas |
41 |
/** |
5675 |
28 Jun 11 |
nicklas |
Create a new <code>ChildItem</code> item. |
5675 |
28 Jun 11 |
nicklas |
43 |
|
5675 |
28 Jun 11 |
nicklas |
@param dc The <code>DbControl</code> which will be used for |
5675 |
28 Jun 11 |
nicklas |
permission checking and database access |
5675 |
28 Jun 11 |
nicklas |
@param parent The <code>AnyItem</code> which is the parent |
5675 |
28 Jun 11 |
nicklas |
@return The new <code>ChildItem</code> item |
5675 |
28 Jun 11 |
nicklas |
@throws BaseException If there is an error |
5675 |
28 Jun 11 |
nicklas |
@see AnyItem#newChildItem() |
5675 |
28 Jun 11 |
nicklas |
50 |
*/ |
5675 |
28 Jun 11 |
nicklas |
public static AChildItem getNew(DbControl dc, AnyItem parent) |
5675 |
28 Jun 11 |
nicklas |
throws BaseException |
5675 |
28 Jun 11 |
nicklas |
53 |
{ |
5675 |
28 Jun 11 |
nicklas |
AChildItem c = dc.newItem(AChildItem.class); |
5675 |
28 Jun 11 |
nicklas |
c.setParent(parent); |
5675 |
28 Jun 11 |
nicklas |
c.setName("New child item"); |
5675 |
28 Jun 11 |
nicklas |
return c; |
5675 |
28 Jun 11 |
nicklas |
58 |
} |
5675 |
28 Jun 11 |
nicklas |
59 |
|
5675 |
28 Jun 11 |
nicklas |
60 |
/** |
5675 |
28 Jun 11 |
nicklas |
Get a <code>ChildItem</code> item when you know the id. |
5675 |
28 Jun 11 |
nicklas |
62 |
|
5675 |
28 Jun 11 |
nicklas |
@param dc The <code>DbControl</code> which will be used for |
5675 |
28 Jun 11 |
nicklas |
permission checking and database access. |
5675 |
28 Jun 11 |
nicklas |
@param id The id of the item to load |
5675 |
28 Jun 11 |
nicklas |
@return The <code>ChildItem</code> item |
5675 |
28 Jun 11 |
nicklas |
@throws ItemNotFoundException If an item with the specified |
5675 |
28 Jun 11 |
nicklas |
id is not found |
5675 |
28 Jun 11 |
nicklas |
@throws PermissionDeniedException If the logged in user doesn't |
5675 |
28 Jun 11 |
nicklas |
have {@link Permission#READ} permission to the item |
5675 |
28 Jun 11 |
nicklas |
@throws BaseException If there is another error |
5675 |
28 Jun 11 |
nicklas |
72 |
*/ |
5675 |
28 Jun 11 |
nicklas |
public static AChildItem getById(DbControl dc, int id) |
5675 |
28 Jun 11 |
nicklas |
throws ItemNotFoundException, PermissionDeniedException, BaseException |
5675 |
28 Jun 11 |
nicklas |
75 |
{ |
5675 |
28 Jun 11 |
nicklas |
AChildItem c = dc.loadItem(AChildItem.class, id); |
5675 |
28 Jun 11 |
nicklas |
if (c == null) throw new ItemNotFoundException("AChildItem[id="+id+"]"); |
5675 |
28 Jun 11 |
nicklas |
return c; |
5675 |
28 Jun 11 |
nicklas |
79 |
} |
5675 |
28 Jun 11 |
nicklas |
80 |
|
5675 |
28 Jun 11 |
nicklas |
81 |
/** |
5675 |
28 Jun 11 |
nicklas |
Get a query configured to retrieve children for the specified parent. |
5675 |
28 Jun 11 |
nicklas |
83 |
|
5675 |
28 Jun 11 |
nicklas |
@param parent The parent to retreive child items for, null is allowed if |
5675 |
28 Jun 11 |
nicklas |
the logged in user has generic READ permission for AnyItem:s in which case |
5675 |
28 Jun 11 |
nicklas |
all ChildItems will be returned |
5675 |
28 Jun 11 |
nicklas |
@return An {@link ItemQuery} object |
5675 |
28 Jun 11 |
nicklas |
@see AnyItem#getChildItems() |
5675 |
28 Jun 11 |
nicklas |
89 |
*/ |
5675 |
28 Jun 11 |
nicklas |
public static ItemQuery<AChildItem> getQuery(AnyItem parent) |
5675 |
28 Jun 11 |
nicklas |
91 |
{ |
5675 |
28 Jun 11 |
nicklas |
Query<AChildItem> query = null; |
5675 |
28 Jun 11 |
nicklas |
if (parent != null) |
5675 |
28 Jun 11 |
nicklas |
94 |
{ |
5675 |
28 Jun 11 |
nicklas |
query = new ItemQuery<AChildItem>(AChildItem.class, null); |
5675 |
28 Jun 11 |
nicklas |
query.restrictPermanent( |
5675 |
28 Jun 11 |
nicklas |
Restrictions.eq( |
5675 |
28 Jun 11 |
nicklas |
Hql.property("anyItem"), |
5675 |
28 Jun 11 |
nicklas |
Hql.entity(parent) |
5675 |
28 Jun 11 |
nicklas |
100 |
) |
5675 |
28 Jun 11 |
nicklas |
101 |
); |
5675 |
28 Jun 11 |
nicklas |
102 |
} |
5675 |
28 Jun 11 |
nicklas |
else |
5675 |
28 Jun 11 |
nicklas |
104 |
{ |
5675 |
28 Jun 11 |
nicklas |
query = new ItemQuery<AChildItem>(AChildItem.class, RUNTIME_FILTER); |
5675 |
28 Jun 11 |
nicklas |
106 |
} |
5675 |
28 Jun 11 |
nicklas |
return query; |
5675 |
28 Jun 11 |
nicklas |
108 |
} |
5675 |
28 Jun 11 |
nicklas |
109 |
|
5675 |
28 Jun 11 |
nicklas |
// Constructor |
5675 |
28 Jun 11 |
nicklas |
AChildItem(ChildData childData) |
5675 |
28 Jun 11 |
nicklas |
112 |
{ |
5675 |
28 Jun 11 |
nicklas |
super(childData); |
5675 |
28 Jun 11 |
nicklas |
114 |
} |
5675 |
28 Jun 11 |
nicklas |
115 |
|
5675 |
28 Jun 11 |
nicklas |
116 |
/* |
5675 |
28 Jun 11 |
nicklas |
From the Nameable interface |
5675 |
28 Jun 11 |
nicklas |
118 |
------------------------------------------- |
5675 |
28 Jun 11 |
nicklas |
119 |
*/ |
5675 |
28 Jun 11 |
nicklas |
public String getName() |
5675 |
28 Jun 11 |
nicklas |
121 |
{ |
5675 |
28 Jun 11 |
nicklas |
return getData().getName(); |
5675 |
28 Jun 11 |
nicklas |
123 |
} |
5675 |
28 Jun 11 |
nicklas |
public void setName(String name) |
5675 |
28 Jun 11 |
nicklas |
throws PermissionDeniedException, InvalidDataException |
5675 |
28 Jun 11 |
nicklas |
126 |
{ |
5675 |
28 Jun 11 |
nicklas |
checkPermission(Permission.WRITE); |
5675 |
28 Jun 11 |
nicklas |
NameableUtil.setName(getData(), name); |
5675 |
28 Jun 11 |
nicklas |
129 |
} |
5675 |
28 Jun 11 |
nicklas |
public String getDescription() |
5675 |
28 Jun 11 |
nicklas |
131 |
{ |
5675 |
28 Jun 11 |
nicklas |
return getData().getDescription(); |
5675 |
28 Jun 11 |
nicklas |
133 |
} |
5675 |
28 Jun 11 |
nicklas |
public void setDescription(String description) |
5675 |
28 Jun 11 |
nicklas |
throws PermissionDeniedException, InvalidDataException |
5675 |
28 Jun 11 |
nicklas |
136 |
{ |
5675 |
28 Jun 11 |
nicklas |
checkPermission(Permission.WRITE); |
5675 |
28 Jun 11 |
nicklas |
NameableUtil.setDescription(getData(), description); |
5675 |
28 Jun 11 |
nicklas |
139 |
} |
5675 |
28 Jun 11 |
nicklas |
140 |
// ------------------------------------------- |
5675 |
28 Jun 11 |
nicklas |
141 |
/* |
5675 |
28 Jun 11 |
nicklas |
From the BasicItem class |
5675 |
28 Jun 11 |
nicklas |
143 |
------------------------------------------- |
5675 |
28 Jun 11 |
nicklas |
144 |
*/ |
5675 |
28 Jun 11 |
nicklas |
145 |
/** |
5675 |
28 Jun 11 |
nicklas |
Always return FALSE. |
5675 |
28 Jun 11 |
nicklas |
It is very seldom a child item is referenced by some other item than |
5675 |
28 Jun 11 |
nicklas |
the parent item. |
5675 |
28 Jun 11 |
nicklas |
149 |
*/ |
5675 |
28 Jun 11 |
nicklas |
public boolean isUsed() |
5675 |
28 Jun 11 |
nicklas |
throws BaseException |
5675 |
28 Jun 11 |
nicklas |
152 |
{ |
5675 |
28 Jun 11 |
nicklas |
return false; |
5675 |
28 Jun 11 |
nicklas |
154 |
} |
5675 |
28 Jun 11 |
nicklas |
155 |
// ------------------------------------------- |
5675 |
28 Jun 11 |
nicklas |
156 |
/* |
5675 |
28 Jun 11 |
nicklas |
From the ChildItem class |
5675 |
28 Jun 11 |
nicklas |
158 |
------------------------------------------- |
5675 |
28 Jun 11 |
nicklas |
159 |
*/ |
5675 |
28 Jun 11 |
nicklas |
SharedData getSharedParent() |
5675 |
28 Jun 11 |
nicklas |
161 |
{ |
5675 |
28 Jun 11 |
nicklas |
return getData().getParent(); |
5675 |
28 Jun 11 |
nicklas |
163 |
} |
5675 |
28 Jun 11 |
nicklas |
164 |
// ------------------------------------------- |
5675 |
28 Jun 11 |
nicklas |
165 |
|
5675 |
28 Jun 11 |
nicklas |
166 |
/** |
5675 |
28 Jun 11 |
nicklas |
Get the parent AnyItem this child item belongs to. |
5675 |
28 Jun 11 |
nicklas |
@return The <code>AnyItem</code> item |
5675 |
28 Jun 11 |
nicklas |
@throws PermissionDeniedException If the logged in user doesn't have |
5675 |
28 Jun 11 |
nicklas |
{@link Permission#READ} permission |
5675 |
28 Jun 11 |
nicklas |
@throws BaseException If there is another error |
5675 |
28 Jun 11 |
nicklas |
172 |
*/ |
5675 |
28 Jun 11 |
nicklas |
public AnyItem getParent() |
5675 |
28 Jun 11 |
nicklas |
throws PermissionDeniedException, BaseException |
5675 |
28 Jun 11 |
nicklas |
175 |
{ |
5675 |
28 Jun 11 |
nicklas |
return getDbControl().getItem(AnyItem.class, getData().getParent()); |
5675 |
28 Jun 11 |
nicklas |
177 |
} |
5675 |
28 Jun 11 |
nicklas |
178 |
/** |
5675 |
28 Jun 11 |
nicklas |
Set the parent AnyItem this child item belongs to. Can only be set |
5675 |
28 Jun 11 |
nicklas |
on a new item. |
5675 |
28 Jun 11 |
nicklas |
@throws InvalidDataException If the parent is null |
5675 |
28 Jun 11 |
nicklas |
182 |
*/ |
5675 |
28 Jun 11 |
nicklas |
void setParent(AnyItem parent) |
5675 |
28 Jun 11 |
nicklas |
throws PermissionDeniedException, InvalidDataException |
5675 |
28 Jun 11 |
nicklas |
185 |
{ |
5675 |
28 Jun 11 |
nicklas |
checkPermission(Permission.WRITE); |
5675 |
28 Jun 11 |
nicklas |
if (parent == null) throw new InvalidUseOfNullException("parent"); |
5675 |
28 Jun 11 |
nicklas |
getData().setParent(parent.getData()); |
5675 |
28 Jun 11 |
nicklas |
189 |
} |
5675 |
28 Jun 11 |
nicklas |
190 |
|
5675 |
28 Jun 11 |
nicklas |
191 |
} |
5675 |
28 Jun 11 |
nicklas |
192 |
|