All Downloads are FREE. Search and download functionalities are using the official Maven repository.

edu.internet2.middleware.grouper.app.dataProvider.GrouperLdapDataProviderQueryTargetDao Maven / Gradle / Ivy

There is a newer version: 5.13.5
Show newest version
package edu.internet2.middleware.grouper.app.dataProvider;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import edu.internet2.middleware.grouper.Member;
import edu.internet2.middleware.grouper.dataField.GrouperDataProviderQueryFieldConfig;
import edu.internet2.middleware.grouper.ldap.LdapAttribute;
import edu.internet2.middleware.grouper.ldap.LdapEntry;
import edu.internet2.middleware.grouper.ldap.LdapSearchScope;
import edu.internet2.middleware.grouper.ldap.LdapSessionUtils;
import edu.internet2.middleware.grouper.util.GrouperUtil;

public class GrouperLdapDataProviderQueryTargetDao extends GrouperDataProviderQueryTargetDao {

  @Override
  public List selectData(Map lowerColumnNameToZeroIndex) {
    List rows = new ArrayList();
    GrouperLdapDataProviderQueryConfig grouperDataProviderQueryConfig = (GrouperLdapDataProviderQueryConfig)this.getGrouperDataProviderQuery().retrieveGrouperDataProviderQueryConfig();
    List ldapAttributes = new ArrayList();

    retrieveMetadata(ldapAttributes, lowerColumnNameToZeroIndex);
   
    if (ldapAttributes.size() == 0) {
      //??
      return rows;
    }
    
    List ldapEntries = LdapSessionUtils.ldapSession().list(
        grouperDataProviderQueryConfig.getProviderQueryLdapConfigId(), 
        grouperDataProviderQueryConfig.getProviderQueryLdapBaseDn(), 
        LdapSearchScope.valueOfIgnoreCase(grouperDataProviderQueryConfig.getProviderQueryLdapSearchScope(), true), 
        grouperDataProviderQueryConfig.getProviderQueryLdapFilter(), 
        ldapAttributes.toArray(new String[0]), null);
    
    processLdapEntries(ldapAttributes, rows, ldapEntries);
    return rows;
  }
  
  private void processLdapEntries(List ldapAttributes, List rows, List ldapEntries) {
    for (LdapEntry ldapEntry : ldapEntries) {
      Object[] row = new Object[ldapAttributes.size()];
      
      for (int i = 0; i < ldapAttributes.size(); i++) {
        Object value = null;

        String ldapAttributeString = ldapAttributes.get(i);
        LdapAttribute ldapAttribute = ldapEntry.getAttribute(ldapAttributeString);
        if (ldapAttribute != null && ldapAttribute.getStringValues().size() > 0) {
          if (ldapAttribute.getStringValues().size() == 1) {
            value = ldapAttribute.getStringValues().iterator().next();
          } else {
            value = new HashSet(ldapAttribute.getStringValues()); 
          }
        }
        
        row[i] = value;
      }
      
      rows.add(row);
    }
  }
  
  private void retrieveMetadata(List ldapAttributes, Map lowerColumnNameToZeroIndex) {
    GrouperLdapDataProviderQueryConfig grouperDataProviderQueryConfig = (GrouperLdapDataProviderQueryConfig)this.getGrouperDataProviderQuery().retrieveGrouperDataProviderQueryConfig();

    for (GrouperDataProviderQueryFieldConfig grouperDataProviderQueryFieldConfig : grouperDataProviderQueryConfig.getGrouperDataProviderQueryFieldConfigs()) {
      ldapAttributes.add(grouperDataProviderQueryFieldConfig.getProviderDataFieldAttribute());
    }
    
    if (ldapAttributes.size() == 0) {
      //??
      return;
    }
    
    if (!ldapAttributes.contains(grouperDataProviderQueryConfig.getProviderQuerySubjectIdAttribute())) {
      ldapAttributes.add(grouperDataProviderQueryConfig.getProviderQuerySubjectIdAttribute());
    }
    
    for (int i = 0; i < ldapAttributes.size(); i++) {
      lowerColumnNameToZeroIndex.put(ldapAttributes.get(i).toLowerCase(), i);
    }
  }
  
  @Override
  public List selectChangeLogData(Map lowerColumnNameToZeroIndex, Timestamp changesFromTimestamp, Timestamp changesToTimestamp) {
    throw new RuntimeException("Not implemented");
  }
  
  @Override
  public List selectDataByMembers(Map lowerColumnNameToZeroIndex, Set members) {
    List rows = new ArrayList();
    GrouperLdapDataProviderQueryConfig grouperDataProviderQueryConfig = (GrouperLdapDataProviderQueryConfig)this.getGrouperDataProviderQuery().retrieveGrouperDataProviderQueryConfig();
    List ldapAttributes = new ArrayList();

    retrieveMetadata(ldapAttributes, lowerColumnNameToZeroIndex);
   
    if (ldapAttributes.size() == 0) {
      //??
      return rows;
    }
    
    if (members.size() > 0) {
      int batchSize = 200;
      List membersList = new ArrayList(members);
      
      int numberOfBatches = GrouperUtil.batchNumberOfBatches(membersList.size(), batchSize, true);
      for (int i=0;i batchMembers = GrouperUtil.batchList(membersList, batchSize, i);
        
        StringBuilder ldapFilter = new StringBuilder("(&" + grouperDataProviderQueryConfig.getProviderQueryLdapFilter() + "(|");
        for (Member member : batchMembers) {
          ldapFilter.append("(" + grouperDataProviderQueryConfig.getProviderQuerySubjectIdAttribute() + "=");

          if ("subjectIdentifier".equals(grouperDataProviderQueryConfig.getProviderQuerySubjectIdType())) {
            // we probably shouldn't assume this is subjectIdentifier0???
            ldapFilter.append(GrouperUtil.ldapFilterEscape(member.getSubjectIdentifier0()));
          } else {
            ldapFilter.append(GrouperUtil.ldapFilterEscape(member.getSubjectId()));
          }
          
          ldapFilter.append(")");
        }
        ldapFilter.append("))");
        List ldapEntries = LdapSessionUtils.ldapSession().list(
            grouperDataProviderQueryConfig.getProviderQueryLdapConfigId(), 
            grouperDataProviderQueryConfig.getProviderQueryLdapBaseDn(), 
            LdapSearchScope.valueOfIgnoreCase(grouperDataProviderQueryConfig.getProviderQueryLdapSearchScope(), true), 
            ldapFilter.toString(), 
            ldapAttributes.toArray(new String[0]), null);
        
        processLdapEntries(ldapAttributes, rows, ldapEntries);
      }
    }
    
    return rows;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy