package edu.harvard.iq.dataverse;

import edu.harvard.iq.dataverse.authorization.AuthenticationServiceBean;
import edu.harvard.iq.dataverse.authorization.RoleAssignee;
import edu.harvard.iq.dataverse.authorization.groups.GroupServiceBean;
import edu.harvard.iq.dataverse.authorization.groups.impl.builtin.AllUsers;
import edu.harvard.iq.dataverse.authorization.groups.impl.builtin.AuthenticatedUsers;
import edu.harvard.iq.dataverse.authorization.users.GuestUser;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 * The place to obtain {@link RoleAssignee}s, based on their identifiers.
 * 
 * @author michael
 */
@Stateless
@Named
public class RoleAssigneeServiceBean {
    private static final Logger logger = Logger.getLogger(RoleAssigneeServiceBean.class.getName());
    @PersistenceContext(unitName = "VDCNet-ejbPU")
    private EntityManager em;
    
    @EJB
    AuthenticationServiceBean authSvc;

    @EJB
    GroupServiceBean groupSvc;
    
    Map<String, RoleAssignee> predefinedRoleAssignees = new TreeMap<>();
    
    @PostConstruct
    void setup() {
        GuestUser gu = new GuestUser();
        predefinedRoleAssignees.put(gu.getIdentifier(), gu);
        predefinedRoleAssignees.put(AuthenticatedUsers.get().getIdentifier(), AuthenticatedUsers.get());
        predefinedRoleAssignees.put(AllUsers.get().getIdentifier(), AllUsers.get());
    }
    
    public RoleAssignee getRoleAssignee( String identifier ) {
        switch ( identifier.charAt(0) ) {
            case ':':
                return predefinedRoleAssignees.get(identifier);
            case '@':
                return authSvc.getAuthenticatedUser(identifier.substring(1));
            case '&':
                return groupSvc.getGroup(identifier.substring(1));
            default: 
                throw new IllegalArgumentException("Unsupported assignee identifier '" + identifier + "'");
        }
    }
    
    public List<RoleAssignment> getAssignmentsFor( String roleAssigneeIdentifier ) {
        return em.createNamedQuery("RoleAssignment.listByAssigneeIdentifier", RoleAssignment.class)
                 .setParameter("assigneeIdentifier", roleAssigneeIdentifier )
                 .getResultList();
    }
}

@@ -444,23 +444,204 @@
 ion.
-users.GuestUser
+groups.impl.explicit.ExplicitGroup;%0Aimport edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser;%0Aimport edu.harvard.iq.dataverse.authorization.users.GuestUser;%0Aimport java.util.ArrayList
 ;%0Aim
@@ -1024,17 +1024,16 @@
 iers.%0A *
- 
 %0A * @aut
@@ -1105,16 +1105,17 @@
 eBean %7B%0A
+%0A
     priv
@@ -1287,21 +1287,17 @@
 ger em;%0A
-    
 %0A
+
     @EJB
@@ -1377,21 +1377,17 @@
 oupSvc;%0A
-    
 %0A
+
     Map%3C
@@ -1451,21 +1451,17 @@
 ap%3C%3E();%0A
-    
 %0A
+
     @Pos
@@ -1783,28 +1783,24 @@
 t());%0A    %7D%0A
-    
 %0A    public 
@@ -1828,17 +1828,16 @@
 ssignee(
- 
 String i
@@ -1837,33 +1837,32 @@
 tring identifier
- 
 ) %7B%0A        swit
@@ -1865,17 +1865,16 @@
 switch (
- 
 identifi
@@ -1885,17 +1885,16 @@
 harAt(0)
- 
 ) %7B%0A    
@@ -2184,17 +2184,16 @@
 default:
- 
 %0A       
@@ -2309,20 +2309,16 @@
 %7D%0A    %7D%0A
-    
 %0A    pub
@@ -2360,17 +2360,16 @@
 entsFor(
- 
 String r
@@ -2389,17 +2389,16 @@
 entifier
- 
 ) %7B%0A    
@@ -2509,17 +2509,16 @@
         
- 
 .setPara
@@ -2571,45 +2571,621 @@
 fier
- 
 )
+%0A                .getResultList();
 %0A    
+%7D%0A%0A
     
-         .getResultList();
+public List%3CAuthenticatedUser%3E getExplicitUsers(RoleAssignee ra) %7B%0A        List%3CAuthenticatedUser%3E explicitUsers = new ArrayList();%0A        if (ra instanceof AuthenticatedUser) %7B%0A            explicitUsers.add((AuthenticatedUser) ra);%0A        %7D else if (ra instanceof ExplicitGroup) %7B%0A            ExplicitGroup group = (ExplicitGroup) ra;%0A            for (String raIdentifier : group.getContainedRoleAssgineeIdentifiers()) %7B%0A                explicitUsers.addAll(getExplicitUsers(getRoleAssignee(raIdentifier)));%0A            %7D%0A        %7D%0A%0A        return explicitUsers;%0A
 %0A   

add notifications for requested file access