LDAP Framework
This small opensource framework is intended to simplify accessing and processing data stored in a LDAP data source. It is built on Java javax.naming.ldap package and allows you to write LDAP queries in SQL style.
Usage example:
public void createOrganization(Organization organization) throws DAOException {
String parentDN = "topOrganizationTree";
try {
String query = "CREATE IN OU=? SET objectClass=idmOrganziation name=? " +
"idmOrgName=? idmOrgBusinessUnit=? idmOrgType=? idmIsActive=? st=? " +
"idmStatusDate=?";
LDAPStatement s = new LDAPStatement(query, ldapDAO);
s.setString(1, organization.getOrgId() + "," + parentDN);
s.setString(2, organization.getOrgId());
s.setString(3, organization.getName());
s.setString(4, organization.getBusinessUnit());
s.setString(5, organization.getIdentityType().getSubType());
s.setBoolean(6, organization.isActive());
s.setString(7, organization.getState());
s.setDate(8, organization.getStatusDate());
String pk = s.create();
organization.setId(pk);
}
catch(Exception ex) {
throw new DAOException(ex);
}
}
public void updateOrganization(Organization organization) throws DAOException {
try {
String query = "UPDATE ? SET idmOrgName=? idmOrgBusinessUnit=? idmOrgType=? " +
"idmIsActive=? st=? idmStatusDate=?";
LDAPStatement s = new LDAPStatement(query, ldapDAO);
s.setString(1, organization.getId());
s.setString(2, organization.getName());
s.setString(3, organization.getBusinessUnit());
s.setString(4, organization.getIdentityType());
s.setBoolean(5, organization.isActive());
s.setString(6, organization.getState());
s.setDate(7, organization.getStatusDate());
s.execute();
}
catch(Exception ex) {
throw new DAOException(ex);
}
}
public Organization readOrganization(String id) throws DAOException {
Organization organization = null;
try {
String parentDN = "topOrganizationTree";
String query = "SEARCH name idmOrgName idmOrgBusinessUnit idmOrgType " +
"idmIsActive st idmStatusDate IN ? WHERE (distinguishedName=?) 2";
LDAPStatement s = new LDAPStatement(query, ldapDAO);
s.setString(1, parentDN);
s.setString(2, id);
LDAPResultSet resultSet = s.execute();
if(resultSet.next()) {
organization = load(resultSet);
}
}
catch(Exception ex) {
throw new DAOException(ex);
}
return organization;
}
public List findByName(String name, String identitySubType, String state,
String businessUnit) throws DAOException {
if(isEmpty(name)) name = "*";
if(isEmpty(identitySubType)) identitySubType = "*";
if(isEmpty(state)) state = "*";
try
{
String parentDN = "topOrganizationTree";
String query =
"SEARCH name idmOrgName idmOrgBusinessUnit idmOrgType idmIsActive st " +
"idmStatusDate IN ? " +
"WHERE (&(objectClass=idmOrganziation) " +
"(&(&(&(idmOrgName=?)(idmOrgType=?))(st=?))(idmOrgBusinessUnit=?))) 2 " +
"ORDER BY idmOrgName";
LDAPStatement s = new LDAPStatement(query, ldapDAO);
s.setString(1, parentDN);
s.setString(2, name);
s.setString(3, identitySubType);
s.setString(4, state);
s.setString(5, businessUnit);
LDAPResultSet resultSet = s.execute();
ArrayList results = new ArrayList();
while(resultSet.next()) {
results.add(load(resultSet));
}
return results;
}
catch(LDAPQueryException ex) {
throw new DAOException(ex);
}
}
public List readMGA() throws DAOException {
try {
String parentDN = getUserInfo().getScope();
String query = LDAPStatements.getLDAPStatment("readMGA");
LDAPStatement s = new LDAPStatement(query, this);
s.setString(1, parentDN);
LDAPResultSet resultSet = s.execute();
ArrayList results = new ArrayList();
while(resultSet.next()) {
results.add(load(resultSet));
}
return results;
}
catch(LDAPQueryException ex) {
throw new DAOException(ex);
}
}
public void deleteOrganization(String organizationId) throws DAOException {
try {
String query = LDAPStatements.getLDAPStatment("deleteOrganization");
LDAPStatement s = new LDAPStatement(query, this);
s.setString(1, organizationId);
s.execute();
}
catch(LDAPQueryException ex) {
throw new DAOException(ex);
}
}
private Organization load(LDAPResultSet resultSet) throws DAOException {
if(resultSet == null ) return null;
Organization org = new Organization();
org.setId(resultSet.getDN());
org.setOrgId(resultSet.getString(1));
org.setName(resultSet.getString(2));
org.setBusinessUnit(resultSet.getString(3));
org.setIdentitySubType(resultSet.getString(4));
org.setActive(resultSet.getBoolean(5));
org.setState(resultSet.getString(6));
org.setStatusDate(resultSet.getDate(7));
return org;
}