001package com.identityworksllc.iiq.common; 002 003import sailpoint.api.Formicator; 004import sailpoint.api.SailPointContext; 005import sailpoint.object.Application; 006import sailpoint.object.AttributeDefinition; 007import sailpoint.object.Attributes; 008import sailpoint.object.Field; 009import sailpoint.object.Form; 010import sailpoint.object.ManagedAttribute; 011import sailpoint.object.Schema; 012import sailpoint.tools.GeneralException; 013import sailpoint.tools.MapUtil; 014import sailpoint.tools.Util; 015 016import java.util.Arrays; 017import java.util.HashMap; 018import java.util.List; 019import java.util.Map; 020 021public class ManagedAttributeUtilities extends AbstractBaseUtility { 022 public enum BuildFormOption { 023 RequireComments, 024 AllowExit, 025 AllowCancel 026 } 027 public ManagedAttributeUtilities(SailPointContext context) { 028 super(context); 029 } 030 031 public Form buildForm(Map<String, Object> map, BuildFormOption... options) throws GeneralException { 032 Form form = new Form(); 033 034 boolean edit = Util.isNullOrEmpty((String) MapUtil.get(map, "sys.nativeIdentity")); 035 036 String appName = Util.otoa(MapUtil.get(map, "sys.application")); 037 String type = Util.otoa(MapUtil.get(map, "sys.type")); 038 039 Application application = context.getObject(Application.class, appName); 040 041 form.setPageTitle(edit ? "Update entitlement" : "Create entitlement"); 042 form.setBasePath("formModel"); 043 044 Form existingForm = application.getProvisioningForm(edit ? Form.Type.Update : Form.Type.Create, type); 045 form.setType(existingForm.getType()); 046 form.setTitle(existingForm.getTitle()); 047 form.setObjectType(existingForm.getObjectType()); 048 form.setOwnerDefinition(existingForm.getOwnerDefinition()); 049 form.setMergeProfiles(existingForm.isMergeProfiles()); 050 form.setIncludeHiddenFields(existingForm.isIncludeHiddenFields()); 051 form.setApplication(application); 052 053 List<Form.Section> sections = existingForm.getSections(); 054 055 for(Form.Section section : Util.safeIterable(sections)) { 056 Form.Section copied = (Form.Section) section.deepCopy(context); 057 form.add(copied); 058 } 059 060 Formicator formicator = new Formicator(context); 061 062 for(BuildFormOption option : options) { 063 if (option == BuildFormOption.RequireComments) { 064 Field commentsField = new Field(); 065 commentsField.setName("sys.comments"); 066 commentsField.setType("string"); 067 commentsField.setRequired(true); 068 commentsField.setSection("__comments"); 069 070 formicator.assemble(form, List.of(commentsField)); 071 } else if (option == BuildFormOption.AllowExit) { 072 Form.Button exitButton = new Form.Button(); 073 exitButton.setAction("next"); 074 exitButton.setLabel("Exit Workflow"); 075 exitButton.setParameter("exitWorkflow"); 076 exitButton.setValue("true"); 077 exitButton.setSkipValidation(true); 078 079 form.add(exitButton); 080 } else if (option == BuildFormOption.AllowCancel) { 081 Form.Button cancelButton = new Form.Button(); 082 cancelButton.setAction("cancel"); 083 cancelButton.setLabel("Cancel"); 084 085 form.add(cancelButton); 086 } 087 } 088 089 Form.Button nextButton = new Form.Button(); 090 nextButton.setAction("next"); 091 nextButton.setLabel(edit ? "Update" : "Create"); 092 093 return form; 094 } 095 096 public Map<String, Object> getCreateManagedAttributeMap(String applicationName, String attribute) throws GeneralException { 097 Application application = context.getObject(Application.class, applicationName); 098 if (application == null) { 099 throw new IllegalArgumentException("No such application: " + applicationName); 100 } 101 102 Schema accountSchema = application.getAccountSchema(); 103 if (accountSchema == null) { 104 throw new IllegalArgumentException("Application " + applicationName + " has no account schema??"); 105 } 106 107 AttributeDefinition attrDef = accountSchema.getAttributeDefinition(attribute); 108 109 String attrSchemaName = attrDef.getSchemaObjectType(); 110 111 Schema schema = application.getSchema(attrSchemaName); 112 if (schema == null) { 113 throw new IllegalArgumentException("Attribute " + attribute + " specifies non-existent schema: " + attrSchemaName); 114 } 115 116 Map<String, Object> result = new HashMap<>(); 117 result.put("attribute", attrDef.getName()); 118 result.put("value", ""); 119 result.put("displayName", ""); 120 121 MapUtil.put(result, "sys.nativeIdentity", null); 122 MapUtil.put(result, "sys.application", application.getId()); 123 MapUtil.put(result, "sys.type", schema.getName()); 124 MapUtil.put(result, "sys.attribute", attrDef.getName()); 125 126 return result; 127 } 128 129 public Map<String, Object> getUpdateManagedAttributeMap(ManagedAttribute ma) throws GeneralException { 130 if (ma == null) { 131 throw new IllegalArgumentException("Cannot provide a null managed attribute"); 132 } 133 134 Application application = ma.getApplication(); 135 Schema schema = application.getSchema(ma.getType()); 136 137 Map<String, Object> result = new HashMap<>(); 138 139 result.put("attribute", ma.getAttribute()); 140 result.put("value", ma.getValue()); 141 result.put("displayName", ma.getDisplayName()); 142 143 for(String key : schema.getAttributeNames()) { 144 Object existing = ma.getAttribute(key); 145 if (Utilities.isSomething(existing)) { 146 result.put(key, existing); 147 } 148 } 149 150 MapUtil.put(result, "sys.id", ma.getId()); 151 MapUtil.put(result, "sys.nativeIdentity", null); 152 MapUtil.put(result, "sys.application", application.getId()); 153 MapUtil.put(result, "sys.type", schema.getName()); 154 MapUtil.put(result, "sys.attribute", ma.getAttribute()); 155 156 157 158 return result; 159 } 160}