/* (c) Copyright 2012 SailPoint Technologies, Inc., All Rights Reserved. */ package sailpoint.reporting.datasource; import net.sf.jasperreports.engine.JRException; import net.sf.jasperreports.engine.JRField; import sailpoint.api.SailPointContext; import sailpoint.object.Attributes; import sailpoint.object.Filter; import sailpoint.object.Identity; import sailpoint.object.Link; import sailpoint.object.LinkExternalAttribute; import sailpoint.object.LiveReport; import sailpoint.object.QueryOptions; import sailpoint.object.Sort; import sailpoint.task.Monitor; import sailpoint.tools.GeneralException; import sailpoint.tools.Util; import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; import java.util.List; public class CustomUserDataSource implements JavaDataSource { private Monitor monitor; private SailPointContext context; private QueryOptions baseQueryOptions; private Integer startRow; private Integer pageSize; private Object[] currentRow; private Iterator iterator; private List reportList = new ArrayList(); private List applicationIds; private Integer resultSize; private Integer index = 0; public void initialize(SailPointContext context, LiveReport report, Attributes arguments, String groupBy, List sort) throws GeneralException { this.context = context; baseQueryOptions = new QueryOptions(); // System.out.println("Arguments: "+arguments.getKeys()); if (arguments.containsKey("Excluded Application List")) { applicationIds = arguments.getList("Excluded Application List"); Filter filter = Filter.in("links.application.id", applicationIds); baseQueryOptions.add(Filter.not(filter)); // System.out.println("CustomUserDataSource Application Name: // "+applicationIds.toString()); // baseQueryOptions.add(Filter.in("links.application.id", // applicationIds)); } if (arguments.containsKey("Only show terminated users?")) { Boolean isInactive = arguments.getBoolean("Only show terminated users?"); System.out.println("CustomUserDataSource Inactive: " + isInactive); baseQueryOptions.add(Filter.eq("inactive", isInactive)); // Fetch Identity only if IIQDisabled = true // baseQueryOptions.add(Filter.eq("links.disabled", "true")); } if (sort != null) { for (Sort sortItem : sort) { baseQueryOptions.addOrdering(sortItem.getField(), sortItem.isAscending()); } } if (groupBy != null) baseQueryOptions.setGroupBys(Arrays.asList(groupBy)); } private void prepare() throws GeneralException { QueryOptions ops = new QueryOptions(baseQueryOptions); System.out.println("CustomUserDataSource Query Options: " + ops); if (startRow != null && startRow > 0) { ops.setFirstRow(startRow); } if (pageSize != null && pageSize > 0) { ops.setResultLimit(pageSize); } iterator = context.search(Identity.class, ops, Arrays.asList("name", "displayName", "links.application.name")); resultSize = 0; while (iterator.hasNext()) { Object[] row = iterator.next(); String identityName = (String) row[0]; String applicationName = (String) row[2]; System.out.println("----------------------------------------------"); System.out.println("Processing Identity : " + identityName); System.out.println("Processing Application Name : " + applicationName); // System.out.println("Index = "+index+", resultSize = "+resultSize); try { Filter f = Filter.and(Filter.eq("application.name", applicationName), Filter.eq("identity.name", identityName)); QueryOptions queryOption = new QueryOptions(); queryOption.addFilter(f); List appLinks = context.getObjects(Link.class, queryOption); if (appLinks != null) { for (Link appLink : appLinks) { // System.out.println("Application : "+appLink.getApplicationName()); String isDisabled = (String) appLink.getAttribute("IIQDisabled"); System.out.println("IIQDisabled: " + isDisabled); if (isDisabled != null && isDisabled.equalsIgnoreCase("true")) { System.out.println("Account Disabled : " + isDisabled + ", No need to add to report..."); } else { System.out.println("Add to report..."); reportList.add(row); resultSize++; } } } } catch (GeneralException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("----------------------------------------------"); } } public boolean next() throws JRException { if (iterator == null) { try { prepare(); } catch (GeneralException e) { throw new JRException(e); } } if (index < resultSize) { currentRow = reportList.get(index); index++; return true; } return false; } public Object getFieldValue(String field) throws GeneralException { if ("name".equals(field)) { // System.out.println("Report User ID: "+currentRow[0]); return currentRow[0]; } else if ("displayName".equals(field)) { return currentRow[1]; } else if ("application".equals(field)) { return currentRow[2]; } else { throw new GeneralException("Unknown column '" + field + "'"); } } public void setLimit(int startRow, int pageSize) { this.startRow = startRow; this.pageSize = pageSize; } public int getSizeEstimate() throws GeneralException { return context.countObjects(Identity.class, baseQueryOptions); } public void close() { } public Object getFieldValue(JRField jrField) throws JRException { String name = jrField.getName(); try { return getFieldValue(name); } catch (GeneralException e) { throw new JRException(e); } } public void setMonitor(Monitor monitor) { this.monitor = monitor; } public QueryOptions getBaseQueryOptions() { return baseQueryOptions; } /** * Unused since this is not an hql report. */ public String getBaseHql() { return null; } }