12 Replies Latest reply: Feb 22, 2012 10:59 AM by DJ RSS

    Creating a Request via OIM 11g API

    DJ
      I'm trying to create a request in OIM using the API. I found this post ( OIM 11g - Setting Process Form Data through Request API ) which has the code to do it but I'm getting an error:

      Here is what I have
      OIMClient client = getOIMClient(url, username, password);
            long resKey = 0L;
            String str = null;
            String resourceKey = null;
            Map<String, String> searchMap = new HashMap<String, String>();
            tcResultSet resultSet = null;
            RequestData requestData = null;
            Beneficiary beneficiary = null;
            RequestBeneficiaryEntity entity = null;
            List<Beneficiary> beneficiaryList = new ArrayList<Beneficiary>();
            List<RequestBeneficiaryEntity> entityList = null;
            List<RequestBeneficiaryEntityAttribute> entityAttrList = null;
            String RESOURCE = "TESTRESOURCE_GTC";
      
            tcITResourceInstanceOperationsIntf operations = client.getService(tcITResourceInstanceOperationsIntf.class);
            Map<String, String> conditions = new HashMap<String, String>();
            conditions.put("IT Resources.Name", "TESTRESOURCE_GTC");
            resultSet = operations.findITResourceInstances(conditions);            
            resKey = resultSet.getLongValue("IT Resource.Key");
            resourceKey = Long.toString(resKey);
      
            RequestBeneficiaryEntityAttribute parantAttr = new RequestBeneficiaryEntityAttribute();
            parantAttr.setType(TYPE.String);
            parantAttr.setName("Title");
            parantAttr.setValue("Mr.");
      
            entityAttrList = new ArrayList<RequestBeneficiaryEntityAttribute>();
            entityAttrList.add(parantAttr);
      
            entity = new RequestBeneficiaryEntity();
            entity.setEntityKey(resourceKey);
            entity.setEntityType(RequestConstants.RESOURCE);
            entity.setEntitySubType(RESOURCE);
            entity.setEntityData(entityAttrList);
      
            entityList = new ArrayList<RequestBeneficiaryEntity>();
            entityList.add(entity);
      
            System.out.println("*********** USR KEY" + usrKey);
      
            beneficiary = new Beneficiary();
            beneficiary.setBeneficiaryType(Beneficiary.USER_BENEFICIARY);
            beneficiary.setBeneficiaryKey(usrKey);
            beneficiary.setTargetEntities(entityList);
      
            beneficiaryList.add(beneficiary);
      
            requestData = new RequestData();
            requestData.setRequestTemplateName("Self-Request Resource");
            requestData.setJustification("Test");
            requestData.setBeneficiaries(beneficiaryList);
      
            UnauthenticatedRequestService reqsrvc = client.getService(UnauthenticatedRequestService.class);
      
            str = reqsrvc.submitRequest(requestData);
      
            System.out.println("Value from submitRequest : '" + (str != null ? str : null) + "'");
      I'm getting

      <Feb 14, 2012 4:53:24 PM EST> <Error> <oracle.iam.request.impl> <IAM-2050128> <Invalid beneficiary key was specified for Self-Request. >
      oracle.iam.request.exception.InvalidRequestDataException: IAM-2050128:Invalid beneficiary key was specified for Self-Request.


      I can see that USER KEY is getting populated.

      Edited by: DJ on Feb 14, 2012 4:59 PM

      Edited by: DJ on Feb 14, 2012 5:00 PM
        • 1. Re: Creating a Request via OIM 11g API
          BikashBagaria
          You are setting the template name as "Self-Request Resource". Dose this template exists in OIM? If yes, one thing which you can try is by not passing any user key as you are doing: beneficiary.setBeneficiaryKey(usrKey);
          Whose key are you passing? You should pass the key of the user who is calling the oimclient.login.
          I haven't used this template in the code; but understand that you should not be adding any benificiary when using this template or at max you should be adding the key of the user who is logged in.

          -Bikash

          Edited by: Bikash Bagaria on Feb 15, 2012 3:51 AM
          • 2. Re: Creating a Request via OIM 11g API
            Kevin Pinsky
            Here is another link provided my Oracle with code samples. Check it out and see if it gets you what you are looking for:

            http://java.net/projects/openptk/sources/svn/content/branches/Oracle/OIM11g/examples/java/OIMClient/src/oim/client/request/RequestResourceCreate.java?rev=1402

            -Kevin
            • 3. Re: Creating a Request via OIM 11g API
              DJ
              Self-Request Resource template does exist in OIM. The User key is for a user who will be the beneficiary of the request (user who will get the resource assigned at the end). xelsysadm is the user who is calling client.login().

              If I don't include the beneficiary key that it throws an error that beneficiary key is invalid.
              • 4. Re: Creating a Request via OIM 11g API
                BikashBagaria
                beneficiary key and the user who is login should be same.

                -Bikash
                • 5. Re: Creating a Request via OIM 11g API
                  DJ
                  In my code above I'm using UnauthenticatedRequestService instead of RequestService. So shouldn't that mean anyone can create a request on behalf of some user?
                  • 6. Re: Creating a Request via OIM 11g API
                    BikashBagaria
                    I doubt that. The template of type self service should be authenticated.

                    -Bikash
                    • 7. Re: Creating a Request via OIM 11g API
                      DJ
                      If I change the template to a template of type Provision Resource, it works with the same exact parameters but doesn't work when I use Self-Request Resource type.
                      • 8. Re: Creating a Request via OIM 11g API
                        DJ
                        While creating a request to provision iPlanet User one of the errors I was getting:

                        oracle.iam.request.exception.RequestServiceException: IAM-2050061:Type mismatch for the attribute Server. The type passed is String, but the corresponding type in the data set is Long.:Server:String:Long


                        So I replaced the value of the attribute Server to the IT Resource.Key which is 5. After making the changes, it goes through approvals with no problems, but when I approve all the work items, it errors out with an error:

                        Error occurred while provisioning resource with key 45 to user 5

                        User Key is 45 and Resource Key is 5. I'm not sure why it swapped it.

                        Here is the code I'm using:
                        public static void createRequest(String userLogin, String templateName, String resourceName, HashMap<String, Object> attrs, OIMClient client) {
                        
                            try {
                              String usrKey = searchUserByLogin(userLogin, client).get(0).getId();
                        
                              long resKey = 0L;
                              String str = null;
                              String resourceKey = null;
                              Map<String, String> searchMap = new HashMap<String, String>();
                              tcResultSet resultSet = null;
                              RequestData requestData = null;
                              Beneficiary beneficiary = null;
                              RequestBeneficiaryEntity entity = null;
                              List<Beneficiary> beneficiaryList = new ArrayList<Beneficiary>();
                              List<RequestBeneficiaryEntity> entityList = null;
                              List<RequestBeneficiaryEntityAttribute> entityAttrList = new ArrayList<RequestBeneficiaryEntityAttribute>();
                        
                              tcITResourceInstanceOperationsIntf operations = client == null ? Platform.getService(tcITResourceInstanceOperationsIntf.class) : client.getService(tcITResourceInstanceOperationsIntf.class);
                              Map<String, String> conditions = new HashMap<String, String>();
                              conditions.put("IT Resources.Name", resourceName);
                              resultSet = operations.findITResourceInstances(conditions);
                              resKey = resultSet.getLongValue("IT Resource.Key");
                              resourceKey = Long.toString(resKey);
                        
                              for (String attr : attrs.keySet()) {
                                RequestBeneficiaryEntityAttribute parantAttr = new RequestBeneficiaryEntityAttribute();
                        
                                if (attrs.get(attr) instanceof String) {
                                  parantAttr.setType(TYPE.String);
                                  parantAttr.setName(attr);
                                  parantAttr.setValue(String.valueOf(attrs.get(attr)));
                                  entityAttrList.add(parantAttr);
                                } else if (attrs.get(attr) instanceof Long) {
                                  parantAttr.setType(TYPE.Long);
                                  parantAttr.setName(attr);
                                  parantAttr.setValue(String.valueOf(attrs.get(attr)));
                                  entityAttrList.add(parantAttr);
                                } else {
                                  // do nothing
                                }
                        
                              }
                        
                              entity = new RequestBeneficiaryEntity();
                              entity.setEntityKey(resourceKey);
                              entity.setEntityType(RequestConstants.RESOURCE);
                              entity.setEntitySubType(resourceName);
                              entity.setEntityData(entityAttrList);
                        
                              entityList = new ArrayList<RequestBeneficiaryEntity>();
                              entityList.add(entity);
                        
                              beneficiary = new Beneficiary();
                              beneficiary.setBeneficiaryType(Beneficiary.USER_BENEFICIARY);
                              beneficiary.setBeneficiaryKey(usrKey);
                              beneficiary.setTargetEntities(entityList);
                        
                              beneficiaryList.add(beneficiary);
                        
                              requestData = new RequestData();
                              requestData.setRequestTemplateName(templateName);
                              requestData.setJustification("Test");
                              requestData.setBeneficiaries(beneficiaryList);
                        
                              System.out.println("Resource Keys " + resKey + " " + resourceKey);
                              System.out.println("User Key " + usrKey);
                        
                              RequestService reqsrvc = client == null ? Platform.getService(RequestService.class) : client.getService(RequestService.class);
                        
                              str = reqsrvc.submitRequest(requestData);
                        
                              System.out.println("Value from submitRequest : '" + (str != null ? str : null) + "'");
                        
                            } catch (Throwable t) {
                              t.printStackTrace();
                            }
                        
                        
                          }
                        Edited by: DJ on Feb 16, 2012 5:01 PM
                        • 9. Re: Creating a Request via OIM 11g API
                          DJ
                          I compared the data written to the request tables for request created by UI and by API. I found that RBE_REQUEST_KEY in table REQUEST_BENEFICIARY_ENTITIES are different.

                          There is an API method which will let me set entity key while creating a request from an API but I'm not sure how to grab that ID or where can I find the valid ID. Right now it works from an API because I have the entity key hard coded to 10 (ID used by successfully completed requests) but not sure where/how to find a valid entity key.


                          Thanks
                          • 10. Re: Creating a Request via OIM 11g API
                            DJ
                            I figured it out. you need to set the entityKey on the RequestBeneficiaryEntity object to the key of the beneficiary (user who the request is for).
                            • 11. Re: Creating a Request via OIM 11g API
                              DJ
                              Can I pass in a attribute as a parameter when creating a request which is not part of the dataset?
                              • 12. Re: Creating a Request via OIM 11g API
                                DJ
                                I tried doing something like this but it's giving the Caused by: java.sql.SQLIntegrityConstraintViolationException: ORA-01400: cannot insert NULL into ("DEV_OIM"."REQUEST_ENTITIES"."REQUEST_ENTITY_TYPE") error.
                                RequestEntityAttribute attr = new RequestEntityAttribute();
                                      attr.setName("TESTID");
                                      attr.setValue("BLAHID");
                                      attr.setType(TYPE.String);
                                
                                      RequestEntity re = new RequestEntity();      
                                      re.setEntityKey("TESTKEY");      
                                      re.getEntityData().add(attr);
                                      re.setEntityKey(RequestConstants.REQUEST_ENTITY_TYPE);
                                
                                      List<RequestEntity> requestEntities = new ArrayList<RequestEntity>();
                                
                                      requestEntities.add(re);
                                
                                      requestData.setTargetEntities(requestEntities);
                                Is this the right way of doing it?