src/core/net/sf/basedb/util/ShareableUtil.java

Code
Comments
Other
Rev Date Author Line
1713 14 Dec 05 nicklas 1 /*
1713 14 Dec 05 nicklas 2   $Id$
1713 14 Dec 05 nicklas 3
3675 16 Aug 07 jari 4   Copyright (C) 2005 Nicklas Nordborg
4889 06 Apr 09 nicklas 5   Copyright (C) 2006 Jari Häkkinen, Nicklas Nordborg
3675 16 Aug 07 jari 6   Copyright (C) 2007 Martin Svensson
1713 14 Dec 05 nicklas 7
2304 22 May 06 jari 8   This file is part of BASE - BioArray Software Environment.
2304 22 May 06 jari 9   Available at http://base.thep.lu.se/
1713 14 Dec 05 nicklas 10
1713 14 Dec 05 nicklas 11   BASE is free software; you can redistribute it and/or
1713 14 Dec 05 nicklas 12   modify it under the terms of the GNU General Public License
4479 05 Sep 08 jari 13   as published by the Free Software Foundation; either version 3
1713 14 Dec 05 nicklas 14   of the License, or (at your option) any later version.
1713 14 Dec 05 nicklas 15
1713 14 Dec 05 nicklas 16   BASE is distributed in the hope that it will be useful,
1713 14 Dec 05 nicklas 17   but WITHOUT ANY WARRANTY; without even the implied warranty of
1713 14 Dec 05 nicklas 18   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1713 14 Dec 05 nicklas 19   GNU General Public License for more details.
1713 14 Dec 05 nicklas 20
1713 14 Dec 05 nicklas 21   You should have received a copy of the GNU General Public License
4515 11 Sep 08 jari 22   along with BASE. If not, see <http://www.gnu.org/licenses/>.
1713 14 Dec 05 nicklas 23 */
1713 14 Dec 05 nicklas 24 package net.sf.basedb.util;
1713 14 Dec 05 nicklas 25
3552 06 Jul 07 martin 26 import net.sf.basedb.core.BaseException;
1713 14 Dec 05 nicklas 27 import net.sf.basedb.core.DbControl;
3552 06 Jul 07 martin 28 import net.sf.basedb.core.Group;
1713 14 Dec 05 nicklas 29 import net.sf.basedb.core.Item;
3552 06 Jul 07 martin 30 import net.sf.basedb.core.ItemResultIterator;
3552 06 Jul 07 martin 31 import net.sf.basedb.core.MultiPermissions;
3552 06 Jul 07 martin 32 import net.sf.basedb.core.Nameable;
2389 16 Jun 06 nicklas 33 import net.sf.basedb.core.Permission;
2389 16 Jun 06 nicklas 34 import net.sf.basedb.core.PermissionDeniedException;
3552 06 Jul 07 martin 35 import net.sf.basedb.core.Project;
1713 14 Dec 05 nicklas 36 import net.sf.basedb.core.SharedItem;
3552 06 Jul 07 martin 37 import net.sf.basedb.core.User;
1713 14 Dec 05 nicklas 38
3552 06 Jul 07 martin 39 import java.util.ArrayList;
3552 06 Jul 07 martin 40 import java.util.Collections;
3552 06 Jul 07 martin 41 import java.util.HashSet;
3552 06 Jul 07 martin 42 import java.util.List;
1713 14 Dec 05 nicklas 43 import java.util.Set;
1713 14 Dec 05 nicklas 44
1713 14 Dec 05 nicklas 45 /**
1713 14 Dec 05 nicklas 46
1713 14 Dec 05 nicklas 47   @author Nicklas
1713 14 Dec 05 nicklas 48   @version 2.0
1713 14 Dec 05 nicklas 49   @base.modified $Date$
1713 14 Dec 05 nicklas 50 */
1713 14 Dec 05 nicklas 51 public class ShareableUtil
1713 14 Dec 05 nicklas 52 {
1713 14 Dec 05 nicklas 53
1713 14 Dec 05 nicklas 54   public static MultiPermissions getMultiPermissions(DbControl dc, Item itemType, Set<Integer> items)
1713 14 Dec 05 nicklas 55     throws BaseException
1713 14 Dec 05 nicklas 56   {
1713 14 Dec 05 nicklas 57     Set<SharedItem> ss = new HashSet<SharedItem>(items.size());
1713 14 Dec 05 nicklas 58     for (Integer id : items)
1713 14 Dec 05 nicklas 59     {
1713 14 Dec 05 nicklas 60       if (id != null)
1713 14 Dec 05 nicklas 61       {
7610 27 Feb 19 nicklas 62         SharedItem item = itemType.getById(dc, id);
2389 16 Jun 06 nicklas 63         if (item.hasPermission(Permission.SET_PERMISSION))
2389 16 Jun 06 nicklas 64         {
2389 16 Jun 06 nicklas 65           ss.add(item);
2389 16 Jun 06 nicklas 66         }
1713 14 Dec 05 nicklas 67       }
1713 14 Dec 05 nicklas 68     }
2389 16 Jun 06 nicklas 69     if (ss.size() == 0)
2389 16 Jun 06 nicklas 70     {
2389 16 Jun 06 nicklas 71       throw new PermissionDeniedException("You don't have permission to share any of the selected items.");
2389 16 Jun 06 nicklas 72     }
1713 14 Dec 05 nicklas 73     return new MultiPermissions(ss);
1713 14 Dec 05 nicklas 74   }
3552 06 Jul 07 martin 75   
3552 06 Jul 07 martin 76   /**
3552 06 Jul 07 martin 77      Gets a list with the projects, groups and users(in that order) that 
3552 06 Jul 07 martin 78      an item is shared to.
4034 05 Dec 07 martin 79      @param dc DbControl to use when accessing the database. 
4034 05 Dec 07 martin 80       @param item The item that is shared
3552 06 Jul 07 martin 81       @return A <code>List</code> with items implementing {@link net.sf.basedb.core.Nameable}
3552 06 Jul 07 martin 82       @since 2.4
3552 06 Jul 07 martin 83   */
3552 06 Jul 07 martin 84   public static List<Nameable> getSharedTo(DbControl dc, SharedItem item)
3552 06 Jul 07 martin 85   {
3552 06 Jul 07 martin 86     List<Nameable> sharees = new ArrayList<Nameable>();
3552 06 Jul 07 martin 87     if (item.isShared())
3552 06 Jul 07 martin 88     {
3552 06 Jul 07 martin 89       MultiPermissions mp = new MultiPermissions(Collections.singleton(item));
3552 06 Jul 07 martin 90       ItemResultIterator<Project> projects = mp.getProjects().iterate(dc);
3552 06 Jul 07 martin 91       ItemResultIterator<Group> groups = mp.getGroups().iterate(dc);
3552 06 Jul 07 martin 92       ItemResultIterator<User> users = mp.getUsers().iterate(dc);
3552 06 Jul 07 martin 93       
3552 06 Jul 07 martin 94       while (projects.hasNext())
3552 06 Jul 07 martin 95       {
3552 06 Jul 07 martin 96         sharees.add(projects.next());
3552 06 Jul 07 martin 97       }    
3552 06 Jul 07 martin 98       while (groups.hasNext())
3552 06 Jul 07 martin 99       {
3552 06 Jul 07 martin 100         sharees.add(groups.next());
3552 06 Jul 07 martin 101       }
3552 06 Jul 07 martin 102       while (users.hasNext())
3552 06 Jul 07 martin 103       {
3552 06 Jul 07 martin 104         sharees.add(users.next());
3552 06 Jul 07 martin 105       }
3552 06 Jul 07 martin 106     }
3552 06 Jul 07 martin 107     return sharees;
3552 06 Jul 07 martin 108   }
1713 14 Dec 05 nicklas 109 }