2916 |
15 Nov 06 |
nicklas |
1 |
/* |
2916 |
15 Nov 06 |
nicklas |
$Id$ |
2916 |
15 Nov 06 |
nicklas |
3 |
|
3675 |
16 Aug 07 |
jari |
Copyright (C) 2006 Nicklas Nordborg |
2916 |
15 Nov 06 |
nicklas |
5 |
|
2916 |
15 Nov 06 |
nicklas |
This file is part of BASE - BioArray Software Environment. |
2916 |
15 Nov 06 |
nicklas |
Available at http://base.thep.lu.se/ |
2916 |
15 Nov 06 |
nicklas |
8 |
|
2916 |
15 Nov 06 |
nicklas |
BASE is free software; you can redistribute it and/or |
2916 |
15 Nov 06 |
nicklas |
modify it under the terms of the GNU General Public License |
4479 |
05 Sep 08 |
jari |
as published by the Free Software Foundation; either version 3 |
2916 |
15 Nov 06 |
nicklas |
of the License, or (at your option) any later version. |
2916 |
15 Nov 06 |
nicklas |
13 |
|
2916 |
15 Nov 06 |
nicklas |
BASE is distributed in the hope that it will be useful, |
2916 |
15 Nov 06 |
nicklas |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
2916 |
15 Nov 06 |
nicklas |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
2916 |
15 Nov 06 |
nicklas |
GNU General Public License for more details. |
2916 |
15 Nov 06 |
nicklas |
18 |
|
2916 |
15 Nov 06 |
nicklas |
You should have received a copy of the GNU General Public License |
4515 |
11 Sep 08 |
jari |
along with BASE. If not, see <http://www.gnu.org/licenses/>. |
2916 |
15 Nov 06 |
nicklas |
21 |
*/ |
2916 |
15 Nov 06 |
nicklas |
22 |
package net.sf.basedb.util; |
2916 |
15 Nov 06 |
nicklas |
23 |
|
2916 |
15 Nov 06 |
nicklas |
24 |
import net.sf.basedb.core.DbControl; |
2916 |
15 Nov 06 |
nicklas |
25 |
import net.sf.basedb.core.Item; |
2916 |
15 Nov 06 |
nicklas |
26 |
import net.sf.basedb.core.Ownable; |
4587 |
15 Oct 08 |
martin |
27 |
import net.sf.basedb.core.OwnedItem; |
2916 |
15 Nov 06 |
nicklas |
28 |
import net.sf.basedb.core.Permission; |
2916 |
15 Nov 06 |
nicklas |
29 |
import net.sf.basedb.core.BaseException; |
2916 |
15 Nov 06 |
nicklas |
30 |
import net.sf.basedb.core.User; |
2916 |
15 Nov 06 |
nicklas |
31 |
|
2916 |
15 Nov 06 |
nicklas |
32 |
import java.util.Set; |
2916 |
15 Nov 06 |
nicklas |
33 |
|
2916 |
15 Nov 06 |
nicklas |
34 |
/** |
2916 |
15 Nov 06 |
nicklas |
35 |
|
4587 |
15 Oct 08 |
martin |
@author Nicklas, Martin |
2916 |
15 Nov 06 |
nicklas |
@version 2.2 |
2916 |
15 Nov 06 |
nicklas |
@base.modified $Date$ |
2916 |
15 Nov 06 |
nicklas |
39 |
*/ |
2916 |
15 Nov 06 |
nicklas |
40 |
public class OwnableUtil |
2916 |
15 Nov 06 |
nicklas |
41 |
{ |
2916 |
15 Nov 06 |
nicklas |
42 |
|
2916 |
15 Nov 06 |
nicklas |
43 |
/** |
2916 |
15 Nov 06 |
nicklas |
Change the owner of all specified items. |
2916 |
15 Nov 06 |
nicklas |
@param dc The DbControl to use for database access |
2916 |
15 Nov 06 |
nicklas |
@param itemType The type of items |
2916 |
15 Nov 06 |
nicklas |
@param items A set with the ID:s of all items |
2916 |
15 Nov 06 |
nicklas |
@param newOwner The new owner, or null to set the owner to the |
2916 |
15 Nov 06 |
nicklas |
currently logged in user |
2916 |
15 Nov 06 |
nicklas |
@return The number of items the owner could be changed on |
2916 |
15 Nov 06 |
nicklas |
@throws BaseException If there is an error |
2916 |
15 Nov 06 |
nicklas |
52 |
*/ |
2916 |
15 Nov 06 |
nicklas |
53 |
public static int setOwner(DbControl dc, Item itemType, Set<Integer> items, User newOwner) |
2916 |
15 Nov 06 |
nicklas |
54 |
throws BaseException |
2916 |
15 Nov 06 |
nicklas |
55 |
{ |
2916 |
15 Nov 06 |
nicklas |
56 |
int numOwned = 0; |
2916 |
15 Nov 06 |
nicklas |
57 |
if (newOwner == null) newOwner = User.getById(dc, dc.getSessionControl().getLoggedInUserId()); |
2916 |
15 Nov 06 |
nicklas |
58 |
for (Integer id : items) |
2916 |
15 Nov 06 |
nicklas |
59 |
{ |
2916 |
15 Nov 06 |
nicklas |
60 |
if (id != null) |
2916 |
15 Nov 06 |
nicklas |
61 |
{ |
7610 |
27 Feb 19 |
nicklas |
62 |
Ownable item = itemType.getById(dc, id); |
2916 |
15 Nov 06 |
nicklas |
63 |
if (item.hasPermission(Permission.SET_OWNER)) |
2916 |
15 Nov 06 |
nicklas |
64 |
{ |
2916 |
15 Nov 06 |
nicklas |
65 |
item.setOwner(newOwner); |
2916 |
15 Nov 06 |
nicklas |
66 |
numOwned++; |
2916 |
15 Nov 06 |
nicklas |
67 |
} |
2916 |
15 Nov 06 |
nicklas |
68 |
} |
2916 |
15 Nov 06 |
nicklas |
69 |
} |
2916 |
15 Nov 06 |
nicklas |
70 |
return numOwned; |
2916 |
15 Nov 06 |
nicklas |
71 |
} |
2916 |
15 Nov 06 |
nicklas |
72 |
|
4587 |
15 Oct 08 |
martin |
73 |
/** |
4587 |
15 Oct 08 |
martin |
Change the owner of all specified items. |
4587 |
15 Oct 08 |
martin |
@param dc The DbControl to use for database access. |
4587 |
15 Oct 08 |
martin |
@param items A set with {@link net.sf.basedb.core.OwnedItem}. |
4587 |
15 Oct 08 |
martin |
@param newOwner The new owner, or null to set the owner to the currently |
4587 |
15 Oct 08 |
martin |
logged in user. |
4587 |
15 Oct 08 |
martin |
@return The number of items the owner could be changed on. |
4587 |
15 Oct 08 |
martin |
@since 2.9 |
4587 |
15 Oct 08 |
martin |
81 |
*/ |
4587 |
15 Oct 08 |
martin |
82 |
public static int setOwner(DbControl dc, Set<OwnedItem> items, User newOwner) |
4587 |
15 Oct 08 |
martin |
83 |
{ |
4587 |
15 Oct 08 |
martin |
84 |
int numOwned = 0; |
4587 |
15 Oct 08 |
martin |
85 |
if (newOwner == null) newOwner = User.getById(dc, dc.getSessionControl().getLoggedInUserId()); |
4587 |
15 Oct 08 |
martin |
86 |
for (OwnedItem ownedItem : items) |
4587 |
15 Oct 08 |
martin |
87 |
{ |
4587 |
15 Oct 08 |
martin |
88 |
if (ownedItem != null) |
4587 |
15 Oct 08 |
martin |
89 |
{ |
7610 |
27 Feb 19 |
nicklas |
90 |
Ownable item = ownedItem.getType().getById(dc, ownedItem.getId()); |
4587 |
15 Oct 08 |
martin |
91 |
if (item.hasPermission(Permission.SET_OWNER)) |
4587 |
15 Oct 08 |
martin |
92 |
{ |
4587 |
15 Oct 08 |
martin |
93 |
item.setOwner(newOwner); |
4587 |
15 Oct 08 |
martin |
94 |
numOwned++; |
4587 |
15 Oct 08 |
martin |
95 |
} |
4587 |
15 Oct 08 |
martin |
96 |
} |
4587 |
15 Oct 08 |
martin |
97 |
} |
4587 |
15 Oct 08 |
martin |
98 |
return numOwned; |
4587 |
15 Oct 08 |
martin |
99 |
} |
2916 |
15 Nov 06 |
nicklas |
100 |
} |