import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import sailpoint.api.SailPointContext;
import sailpoint.connector.Connector;
import sailpoint.connector.ConnectorFactory;
import sailpoint.object.Application;
import sailpoint.object.Filter;
import sailpoint.object.Identity;
import sailpoint.object.QueryOptions;
import sailpoint.tools.Util;
import sailpoint.object.ResourceObject;
import sailpoint.tools.CloseableIterator;
import sailpoint.tools.GeneralException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class UsernameGeneration{
static SailPointContext context = null;
static Identity identity = null;
static Log log = LogFactory.getLog("com.mighty.rule");
static boolean issAMAccountNameUniueInAD2(SailPointContext context, String sAMAccountName) {
if (sAMAccountName == null || sAMAccountName.isEmpty()) {
log.debug("sAMAccountName is null / empty");
return false;
}
boolean unique = true;
String searchDN = null;
String appConnName = null;
Application application = null;
Application appCopy = null;
String searchFilter = "(sAMAccountName=" + sAMAccountName + ")";
List dnList = null;
Map setUpMap = new HashMap();
Connector appConnector = null;
CloseableIterator iterator = null;
try {
application = context.getObjectByName(Application.class, "AD");
appCopy = (Application) application.deepCopy(context);
appCopy.setPasswordPolicies(null);
appConnName = appCopy.getConnector();
if (appCopy.getAttributes().getMap().get("domainSettings").get(0).get("domainDN") != null) {
searchDN = appCopy.getAttributes().getMap().get("domainSettings").get(0).get("domainDN");
}
setUpMap.put("searchDN", searchDN);
setUpMap.put("searchFilter", searchFilter);
dnList.add(0, setUpMap);
appCopy.setAttribute("searchDN", dnList);
appCopy.setAttribute("referral", "ignore");
appCopy.setAttribute("useHasMoreElements", true);
appCopy.setCustomizationRule(null);
appConnector = ConnectorFactory.getConnector(appCopy, null);
iterator = appConnector.iterateObjects(Connector.TYPE_ACCOUNT, null, null);
try {
if (iterator != null && iterator.hasNext()) {
ResourceObject user = (ResourceObject) iterator.next();
unique = false;
}
} catch (Exception e) {
}
} catch (GeneralException e) {
log.error("GeneralException: " + e.getMessage());
} finally {
if (iterator != null) {
iterator.close();
}
}
return unique;
}
static boolean issAMAccountNameUniueInAD(SailPointContext context, String sAMAccountName) {
if (sAMAccountName == null || sAMAccountName.isEmpty()) {
log.debug("sAMAccountName is null / empty");
return false;
}
boolean unique = true;
String appConnName = null;
Application application = null;
Application appCopy = null;
int i = 0;
String searchFilter = "(sAMAccountName=" + sAMAccountName + ")";
List<HashMap> dnList = null;
Map setUpMap = new HashMap();
CloseableIterator iterator = null;
try {
application = context.getObjectByName(Application.class, "AD");
appCopy = (Application) application.deepCopy(context);
appCopy.setPasswordPolicies(null);
appConnName = appCopy.getConnector();
dnList = (List) appCopy.getAttributes().getMap().get("domainSettings");
for (HashMap domain : dnList) {
String searchDN = domain.get("domainDN").toString();
setUpMap.put("searchDN", searchDN);
setUpMap.put("searchFilter", searchFilter);
dnList.add(i, domain);
i++;
}
appCopy.setAttribute("searchDN", dnList);
appCopy.setAttribute("referral", "ignore");
appCopy.setAttribute("useHasMoreElements", true);
appCopy.setCustomizationRule(null);
Connector appConnector = sailpoint.connector.ConnectorFactory.getConnector(appCopy, null);
iterator = appConnector.iterateObjects(Connector.TYPE_ACCOUNT, null, null);
try {
if (iterator != null && iterator.hasNext()) {
ResourceObject user = (ResourceObject) iterator.next();
unique = false;
}
} catch (Exception e) {
}
} catch (GeneralException e) {
log.error("GeneralException: " + e.getMessage());
} finally {
if (iterator != null) {
iterator.close();
}
}
return unique;
}
static boolean isNameUniueInIIQ(SailPointContext context, String cubeName) {
if (cubeName == null || cubeName.isEmpty()) {
return false;
}
boolean unique = true;
Identity identityLookup = null;
try {
identityLookup = context.getObjectByName(Identity.class, cubeName);
if (identityLookup != null) {
unique = false;
return unique;
} else {
return unique;
}
} catch (GeneralException e) {
log.error("GeneralException" + e.getMessage());
}
return false;
}
static boolean isNameUniueInHR(SailPointContext context, String userName) {
if (userName == null || userName.isEmpty()) {
return false;
}
boolean unique = true;
Filter filter = null;
QueryOptions ops = new QueryOptions();
filter = Filter.eq(userName, userName);
try {
Iterator iterator = context.search(Identity.class, ops);
if (null != iterator && iterator.hasNext()) {
unique = false;
return unique;
} else {
return unique;
}
} catch (GeneralException e) {
log.error("GeneralException: " + e.getMessage());
}
return false;
}
static String generateUserName(SailPointContext context, Identity identity) {
String retVal = null;
String type = "";
String firstName = null;
String lastName = null;
String firstNameSub = null;
String lastNameSub = null;
int firstLength = 0;
int lastLenght = 0;
int index = 1;
if (null != identity && null != identity.getAttribute("userType")) {
type = identity.getAttribute("userType").toString();
if (type.equalsIgnoreCase("Employee")) {
if (Util.isNotNullOrEmpty(identity.getFirstname()) && Util.isNotNullOrEmpty(identity.getLastname())) {
firstName = identity.getFirstname().toLowerCase();
lastName = identity.getLastname().toLowerCase();
firstLength = firstName.length();
lastLenght = lastName.length();
firstNameSub = firstName;
lastNameSub = lastName;
if (lastLenght > 11) {
lastNameSub = lastNameSub.substring(0, 11);
}
retVal = firstName.substring(0, 1) + lastNameSub;
if (retVal != null) {
String baseName = retVal;
while (issAMAccountNameUniueInAD(context, retVal)) {
if (firstLength >= (++index)) {
retVal = firstName.substring(0, index) + lastNameSub;
if (retVal.length() > 12) {
retVal = retVal.substring(0, 12);
}
} else {
return null;
}
}
while (isNameUniueInIIQ(context, retVal)) {
if (firstLength >= (++index)) {
retVal = firstName.substring(0, index) + lastNameSub;
if (retVal.length() > 12) {
retVal = retVal.substring(0, 12);
}
} else {
return null;
}
}
while (isNameUniueInHR(context, retVal)) {
if (firstLength >= (++index)) {
retVal = firstName.substring(0, index) + lastNameSub;
if (retVal.length() > 12) {
retVal = retVal.substring(0, 12);
}
} else {
return null;
}
}
}
}
} else if (type.equalsIgnoreCase("Consultant")) {
if (Util.isNotNullOrEmpty(identity.getFirstname()) && Util.isNotNullOrEmpty(identity.getLastname())) {
firstName = identity.getFirstname().toLowerCase();
lastName = identity.getLastname().toLowerCase();
firstLength = firstName.length();
lastLenght = lastName.length();
firstNameSub = firstName;
lastNameSub = lastName;
if (lastLenght > 9) {
lastNameSub = lastNameSub.substring(0, 9);
}
retVal = "c-" + firstName.substring(0, 1) + lastNameSub;
if (retVal != null) {
String baseName = retVal;
while (!issAMAccountNameUniueInAD(context, retVal)) {
if (firstLength >= (++index)) {
retVal = firstName.substring(0, index) + lastNameSub;
if (retVal.length() > 10) {
retVal = retVal.substring(0, 10);
}
} else {
return null;
}
}
while (!isNameUniueInIIQ(context, retVal)) {
if (firstLength >= (++index)) {
retVal = firstName.substring(0, index) + lastNameSub;
if (retVal.length() > 10) {
retVal = retVal.substring(0, 10);
}
} else {
return null;
}
}
while (!isNameUniueInHR(context, retVal)) {
if (firstLength >= (++index)) {
retVal = firstName.substring(0, index) + lastNameSub;
if (retVal.length() > 12) {
retVal = retVal.substring(0, 12);
}
} else {
return null;
}
}
}
}
} else {
return null;
}
}
return retVal;
}
public static void main(String[] args) {
String sAMAccountName = null;
generateUserName(context, identity);
issAMAccountNameUniueInAD2(context, sAMAccountName);
}
}