package com.k_int.aggregator.dpp.util;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.Session;

import com.k_int.aggregator.datamodel.AggregatorSourceHDO;
import com.k_int.svc.identity.service.IdentityService;
import com.k_int.svc.identity.service.IdentityServiceException;
import com.k_int.svc.identity.service.TargetObjectIdentifier;

public class AuthUtil {
  private static final Log log = LogFactory.getLog(AuthUtil.class);
  
  public static Map<String, AggregatorSourceHDO> getValidSourcesForDepositor(IdentityService ident_service, 
                                                                      String depositor_id,
                                                                      Session sess) {
    Map<String, AggregatorSourceHDO> sources = new HashMap<String, AggregatorSourceHDO>();
    
    try 
    {
      TargetObjectIdentifier[] grants = ident_service.listGrantsFor(ident_service.usernameToId(depositor_id),
                                                                    "com.k_int.aggregator.permission.deposit");
      for (int i=0; i<grants.length; i++) {
        // Lookup each provider.
        List<String> id_comps = grants[i].getIdentifierComponents();
        if (id_comps.size()>0) {
          Long src_id             = new Long(id_comps.get(0));
          AggregatorSourceHDO src = (AggregatorSourceHDO) sess.get(AggregatorSourceHDO.class,src_id);
          if (src!=null) {
            sources.put(src.getIdentifier(), src);
          }
        }
      }
    } catch (IdentityServiceException ise ) {
      log.warn("Problem listing data providers this user can upload on behalf of ", ise);
    }
    
    return sources;
  }
  
  
  public static Map<String, AggregatorSourceHDO> getAllSources(Session sess) 
  {
      Map<String, AggregatorSourceHDO> sources = new HashMap<String, AggregatorSourceHDO>();

      try 
      {
          Query q = sess.createQuery("Select r from AggregatorSourceHDO r order by r.identifier");
          @SuppressWarnings("unchecked")
		  Iterator<AggregatorSourceHDO> i = q.list().iterator();
          while(i.hasNext())
          {
              AggregatorSourceHDO src = i.next();
              sources.put(src.getIdentifier(), src);
          }
      }
      catch(Exception e)
      {
          log.warn("Problem listing all data providers ", e);
      }
      return sources;
}
  
  public static Map<String, String> getValidNamesForDepositor(IdentityService ident_service, 
      String depositor_id,
      Session sess) {
    Map<String, String> sources = new HashMap<String, String>();
    
    Map<String, AggregatorSourceHDO> valid_sources = getValidSourcesForDepositor(ident_service, depositor_id, sess);
    
    if (valid_sources.size()>0) 
    for (Entry<String, AggregatorSourceHDO> e : valid_sources.entrySet()) {
      sources.put(e.getKey(), e.getValue().getName());
    }
    
    return sources;
    
  }
  
 
}
