Saturday, 24 March 2018

OIM API - Code Snippet to get Entitlement from OIM for the given Entitlement Name/Code.

 public Entitlement getEntitlement(String entName){
        final String logp = CN + ":: getEntitlementDetails - ";
        LOGGER.debug(logp + "START");

        if(null == entName || entName.trim().length() == 0){
                LOGGER.error(logp + "Entitmement name is null or empty");
                return null;
        }
        entName = entName.trim();

        Entitlement ent = null;
        try{
            SearchCriteria criteria = new SearchCriteria(Entitlement.ENTITLEMENT_NAME, entName, SearchCriteria.Operator.EQUAL);
           
            //get entitlement service
            EntitlementService entServ = Platform.getService(EntitlementService.class);
            List<Entitlement> entList = entServ.findEntitlements(criteria, null);

            if (entList.size() == 0 || entList.size() > 1) {
                LOGGER.error(logp + "Improper number of entitlements found for entitlement name " + entName + " - " + entList.size());
            }else{
                    ent = entList.get(0);
                    LOGGER.info(logp + "Successfully obtained entitlement - " + ent);
                    LOGGER.info(logp + "Entitlement Key - " + ent.getEntitlementKey());
                    LOGGER.info(logp + "Entitlement Display Name  - " + ent.getDisplayName());
                    LOGGER.info(logp + "IT Resource Key  - " + ent.getItResourceKey());
            }
        }catch (Exception e){
                LOGGER.error(logp + "Exception while fetching entitlement for entitlement name " + entName + " - " + e, e);
        }

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


Happy Learning!!!

No comments:

Post a Comment