package edu.harvard.iq.dataverse.authorization.groups.impl.ipaddress;

import edu.harvard.iq.dataverse.authorization.groups.GroupProvider;
import edu.harvard.iq.dataverse.authorization.users.User;
import edu.harvard.iq.dataverse.authorization.users.UserRequestMetadata;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Logger;

/**
 * Creates {@link IpGroup}s.
 * @author michael
 */
public class IpGroupProvider implements GroupProvider<IpGroup> {
    
    private static final Logger logger = Logger.getLogger(IpGroupProvider.class.getCanonicalName());

    private final IpGroupsServiceBean ipGroupsService;

    public IpGroupProvider(IpGroupsServiceBean ipGroupsService) {
        this.ipGroupsService = ipGroupsService;
    }
    
    @Override
    public String getGroupProviderAlias() {
        return "ip";
    }

    @Override
    public String getGroupProviderInfo() {
        return "Groups users by their current IP address";
    }

    @Override
    public Set<IpGroup> groupsFor(User u) {
//        Un-comment below lines if request metadata is null to get a workaround. Then open a bug and assign to @michbarsinai
//        UserRequestMetadata userRequestMetadata = u.getRequestMetadata();
//        if (userRequestMetadata == null) {
//            logger.info("In the groupsFor(User u) method, u.getRequestMetadata() was null for user \"" + u.getIdentifier() + "\". Returning empty set. See also https://github.com/IQSS/dataverse/issues/1354");
//            return Collections.EMPTY_SET;
//        }
        return updateProvider(ipGroupsService.findAllIncludingIp(u.getRequestMetadata().getIpAddress()));
    }

    @Override
    public IpGroup get(String groupAlias) {
        return setProvider(ipGroupsService.getByGroupName(groupAlias));
    }
    
    public IpGroup get(Long id) {
        return setProvider(ipGroupsService.get(id));
    }
    
    @Override
    public Set<IpGroup> findAll() {
        return updateProvider( new HashSet<>(ipGroupsService.findAll()));
    }
    
    private IpGroup setProvider( IpGroup g ) {
        g.setProvider(this);
        return g;
    }
    
    private Set<IpGroup> updateProvider( Set<IpGroup> groups ) {
        for ( IpGroup g : groups ) {
            g.setProvider(this);
        }
        return groups;
    }
    
    public IpGroup store(IpGroup grp) {
        grp.setProvider(this);
        return ipGroupsService.store(grp);
    }

    public void deleteGroup(IpGroup grp) {
        ipGroupsService.deleteGroup(grp);
    }
}

@@ -1177,18 +1177,416 @@
 arsinai%0A
-//
+        /**%0A         * @todo Per above, uncommenting the lines below and assigning a new%0A         * ticket to @michbarsinai: IP Groups: can no longer upload files via%0A         * SWORD - https://github.com/IQSS/dataverse/issues/1360%0A         *%0A         * What other SWORD operation may not be working? They are documented at%0A         * http://guides.dataverse.org/en/latest/api/sword.html%0A         */%0A
         
@@ -1651,18 +1651,16 @@
 data();%0A
-//
         
@@ -1690,26 +1690,24 @@
  == null) %7B%0A
-//
             
@@ -1903,18 +1903,16 @@
 1354%22);%0A
-//
         
@@ -1945,18 +1945,16 @@
 TY_SET;%0A
-//
         

add workaround to get SWORD file upload working #1360