Sunday 10 June 2018

OIM API - Frequently Used Lookup Operations.

Read Lookup Values:-

public Map<String, String> getLookupMap(String lookupName) {
        final String logp = CN + " :: getLookupMap - ";
        LOGGER.debug(logp + "START");
        Map<String, String> lookupMap = new HashMap<String, String>();
        tcLookupOperationsIntf lookupService = Platform.getService(tcLookupOperationsIntf.class);
        try {
            tcResultSet resultSet = lookupService.getLookupValues(lookupName);
            String codeKey, meaningValue;
            for (int i = 0; i < resultSet.getRowCount(); i++) {
                resultSet.goToRow(i);
                codeKey = resultSet.getStringValue("Lookup Definition.Lookup Code Information.Code Key");
                meaningValue = resultSet.getStringValue("Lookup Definition.Lookup Code Information.Decode");
                lookupMap.put(codeKey, meaningValue);
                LOGGER.info(logp + " \nLookup Key: [" + codeKey + "], Lookup Value:[" + meaningValue + "])");
            }
        } catch (tcAPIException e) {
            LOGGER.severe(logp + "Exception occured while reading lookup " + e.getMessage());
            e.printStackTrace();
        } catch (tcInvalidLookupException e) {
            LOGGER.severe(logp + "Exception occured while reading lookup " + e.getMessage());
            e.printStackTrace();
        } catch (tcColumnNotFoundException e) {
            LOGGER.severe(logp + "Exception occured while reading lookup " + e.getMessage());
            e.printStackTrace();
        }
        LOGGER.debug(logp + "END");
        return lookupMap;
    }



 Create Lookup:-
 public void createLookup(String LookupName) {
     final String logp = CN + ":: createLookup- ";
     logger.info(logp + "START");
     tcLookupOperationsIntf lookupOper = null;
    
     try{
         lookupOper = Platform.getService(tcLookupOperationsIntf.class);
         logger.info(logp + " Creating Lookup : " + LookupName);
        
         //Invoking Lookup Create Operation
         lookupOper.addLookupCode(LookupName);
     }catch(Exception e){
         LOGGER.error(logp + "Exception occurred while creating lookup - " + e.getMessage(), e);
     }
    
     LOGGER.debug(logp + "END");
 } 



Add Lookup Values:-
public void addLookupValues(HashMap<String, String> lookupMap, String lookupName) throws Exception {
    String logp = CN + " :: addLookupValues - ";
    logger.info(logp + "START");
    tcLookupOperationsIntf lookupService = Platform.getService(tcLookupOperationsIntf.class);
    Set<String> set = lookupMap.keySet();
    Iterator iter = set.iterator();
    while (iter.hasNext()) {
        String key = (String)iter.next();
        if (!("".equalsIgnoreCase(lookupService.getDecodedValueForEncodedValue(lookupName,key).trim()))){
            logger.info(logp + "Value : " + key + " already in lookup. Updating the value");
            lookupService.removeLookupValue(lookupName, key);
            lookupService.addLookupValue(lookupName,key, lookupMap.get(key), "en","US"); 
        }
        else{
            logger.info(logp + "Value : " + key + " not present in lookup. Adding the value");
            lookupService.addLookupValue(lookupName,key, lookupMap.get(key), "en","US"); 
        }
    }
    logger.info(logp + "Lookup is appened with the new values");
    logger.info(logp + "END");
}



Remove Lookup Value:-
public void removeLookupValue(String Key, String Lookup, boolean caseSensitive) {
    final String logp = CN + "::removeLookupValue - ";
    LOGGER.debug(logp + "START");
    Map searchFor = new HashMap();
    searchFor.put("Lookup Definition.Lookup Code Information.Code Key", Key);
    tcLookupOperationsIntf lookupService = null;
    try {
        lookupService = (tcLookupOperationsIntf) Platform.getService(tcLookupOperationsIntf.class);
        tcResultSet results = lookupService.getLookupValues(Lookup, searchFor);
        for (int i = 0; i < results.getRowCount(); i++) {
            results.goToRow(i);
            String key = results.getStringValue("Lookup Definition.Lookup Code Information.Code Key");
            if (!caseSensitive && key.equalsIgnoreCase(Key)) {
                lookupService.removeLookupValue(Lookup, key);
            } else if (caseSensitive && key.equals(Key)) {
                lookupService.removeLookupValue(Lookup, key);
            }
        }
    } catch (Exception e) {
        LOGGER.error(logp + "Exception occurred - " + e.getMessage(), e);
    }

    LOGGER.debug("END");
}



Empty Lookup:-
public void emptyLookup(String lookupName){
    final String logp = CN + " ::emptyLookup - ";
    LOGGER.debug(logp + "START");
    tcLookupOperationsIntf lookupService = null;
   
    try {
        lookupService = Platform.getService(tcLookupOperationsIntf.class);
       
        HashMap<String, String> lookupMap = getLookupMap(lookupName);
        if (lookupMap.size() > 0) {
            for (String key : lookupMap.keySet()) {
                lookupService.removeLookupValue(lookupName, key);
            }
            LOGGER.debug(logp + "Lookup - " + lookupName + " emptied successfully");
        } else {
            LOGGER.debug(logp + "Lookup - " + lookupName + " is already empty.");
        }
    } catch (Exception e) {
        LOGGER.error(logp + "Exception in emptyLookup()" + e.getMessage());
    }

    LOGGER.debug(logp + "END");
}



Check If Lookup Present in OIM:-
public boolean isLookupPresent(String lookupName) throws Exception {
    String logp = CN + " :: isLookupPresent - ";
    logger.info(logp + "START");       
    boolean isLookupPresent = true;
    tcLookupOperationsIntf lookupService = Platform.getService(tcLookupOperationsIntf.class);
    Map lookupMap = new HashMap();  
    lookupMap.put("Lookup Definition.Lookup Code Information.Code Key",lookupName);
    tcResultSet resultSet = lookupService.findAvailableLookups(lookupMap);
    if(resultSet.getRowCount() == 0){
        isLookupPresent = false;
    }
    return isLookupPresent;
    logger.info(logp + "END");
}



Check if Lookup is Empty:-
public boolean isLookupEmpty(String lookupName) throws Exception {
    final String logp = CN + " ::
isLookupEmpty- ";
    LOGGER.debug(logp + "START");
    HashMap<String, String> lookupMap = new HashMap<String, String>();
    try {
        lookupMap = getLookupMap(lookupName);
    } catch (Exception e) {
        LOGGER.error(logp + "Exception in getLookupEmptyStatus()" + e.getMessage());
    }
    LOGGER.debug(logp + "END");

    return lookupMap.isEmpty();
}



Update the Group Name for Specified Lookup:-
public void updateLookupGroupName(String groupName, String lookupName) {
    String logp = CN + "updateLookupGroupName";
    logger.info(logp + "START");
    Connection connection = null;
    try {
        connection = Platform.getOperationalDS().getConnection();

        String sql = "update lku set lku_group = ? where lku_type_string_key = ?";
        PreparedStatement preparedStatement = null;

        preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1, groupName);
        preparedStatement.setString(2, lookupName);
        preparedStatement.executeUpdate();
    } catch (Exception e) {
        logger.error(logp + " Exception while updating group name: " + e);
    } finally {
        try {
            connection.close();
        } catch (Exception e) {
            logger.error(logp + " Exception while closing connection : " + e);
        }
    }
    logger.info(logp+ "END");
}


Happy Learning!!!

No comments:

Post a Comment