Sample Groovy Script for an Add Child Data Provisioning Operation

The following is a sample groovy script for adding child data.

Register the add child data script as follows:

import org.identityconnectors.framework.common.objects.*;
import java.text.*;
 
System.out.println("[addMultiValuedAttributeScript-Groovy] Adding Child data::"+ attributes);
childst =null;
try {
        //Adding Group data
        childDataEOSet = null;
        
        /**The child attributes are returned as a set of embedded objects. Each Embedded    object will provide a row of data in the child table.
        For example, if DBAT contains USER_GROUP as a child in OIM and contains two rows of groups data then, we will get a set of embedded objects with count 2 and each embedded      object represents a row in child data.
        This groovy script is based on a child table named USER_GROUP and containing USERID, GROUP_ID  as its columns.**/
        
        if(attributes.get("USER_GROUP")!=null)
        {
                childDataEOSet=attributes.get("USER_GROUP").getValue();
                childst = conn.prepareStatement("INSERT INTO USER_GROUP VALUES (?,?)");
                String id = attributes.get("__UID__").getValue().get(0);
                if(childDataEOSet !=null){
                        //Iterate through child data and insert into table
                        System.out.println("[addMultiValuedAttributeScript] Adding Group data.");
                        for( iterator = childDataEOSet.iterator(); iterator.hasNext(); )
                        {
                                eo = iterator.next();
                                attrsSet = eo.getAttributes();
                                grpattr=AttributeUtil.find("GROUPID",attrsSet);
                                if(grpattr!=null){
                                        groupid=grpattr.getValue().get(0);
                                        childst.setString(1, id);
                                        childst.setString(2, groupid);
                                        childst.executeUpdate();
                                        childst.clearParameters();
                                } };
                } } } finally {
        if (childst != null)
        childst.close();
};

try {
        //Adding Role data
        childDataEOSet = null;
        if(attributes.get("USER_ROLE")!=null){
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.S");
                DateFormat targetFormat = new SimpleDateFormat("dd-MMM-yy");
                childDataEOSet=attributes.get("USER_ROLE").getValue();
                childst = conn.prepareStatement("INSERT INTO USER_ROLE VALUES (?,?,?,?)");
                String id = attributes.get("__UID__").getValue().get(0);
                if(childDataEOSet !=null){
                        System.out.println("[addMultiValuedAttributeScript] Adding Role data.");
                        for( iterator = childDataEOSet.iterator(); iterator.hasNext(); ) {
                                eo = iterator.next();
                                attrsSet = eo.getAttributes();
                                roleattr=AttributeUtil.find("ROLEID",attrsSet);
                                fromdateAttr=AttributeUtil.find("FROMDATE",attrsSet);
                                todateAttr=AttributeUtil.find("TODATE",attrsSet);
                                                                if(roleattr!=null){
                                        roleid=roleattr.getValue().get(0);
                                childst.setString(1, id);
                                        childst.setString(2, roleid);
                                        fromdate = null;
                                        if(fromdateAttr!= null)
                                        {
                                                java.util.Date date= df.parse(fromdateAttr.getValue().get(0));
                                                fromdate = targetFormat.format(date);
                                        }
                                        childst.setString(3, fromdate);
                                        todate = null;
                                        if(todateAttr!= null)
                                        {
                                                java.util.Date date= df.parse(todateAttr.getValue().get(0));
                                                todate = targetFormat.format(date);
                                        }
                                        childst.setString(4, todate);
                                        childst.executeUpdate();
                                        childst.clearParameters();
                                } };
        } } } finally {
        if (childst != null)
        childst.close();
};