UserDetailsCallableObject

UserDetailsCallableObject.java

packagecom.examples.bckgco;
 
importjava.util.Locale;
 
importcom.sap.caf.eu.gp.co.api.GPCallableObjectFactory;
importcom.sap.caf.eu.gp.co.api.GPStandardResourceAccessor;
importcom.sap.caf.eu.gp.co.api.IGPBackgroundCallableObject;
importcom.sap.caf.eu.gp.co.api.IGPCOResultStateInfo;
importcom.sap.caf.eu.gp.co.api.IGPExceptionInfo;
importcom.sap.caf.eu.gp.co.api.IGPExecutionContext;
importcom.sap.caf.eu.gp.co.api.IGPTechnicalDescription;
importcom.sap.caf.eu.gp.exception.api.GPEngineException;
importcom.sap.caf.eu.gp.exception.api.GPInvocationException;
importcom.sap.caf.eu.gp.exception.api.GPTechnicalCallableObjectException;
importcom.sap.caf.eu.gp.structure.api.IGPAttributeInfo;
importcom.sap.caf.eu.gp.structure.api.IGPStructure;
importcom.sap.caf.eu.gp.structure.api.IGPStructureInfo;
importcom.sap.security.api.IUser;
importcom.sap.security.api.UMException;
importcom.sap.security.api.UMFactory;
importcom.sap.tc.logging.Category;
importcom.sap.tc.logging.Location;
importcom.sap.tc.logging.Severity;
 
publicclass UserDetailsCallableObject implements IGPBackgroundCallableObject {
  
   public static Location logger =
        Location.getLocation(UserDetailsCallableObject.class);
 
//initialize the resource accessor class with the resource bundle as a parameter
GPStandardResourceAccessorresourceAccessor =
        new UserDetailsResourceAccessor("com.examples.bckgco.UserDetails");
 
 
publicIGPTechnicalDescription getDescription(Locale originalLocale) {
      try {
 
       //create technical description instance
       IGPTechnicalDescription technicalDescription =
         GPCallableObjectFactory.createTechnicalDescription(
            "CO_NAME",
            "CO_DESCRIPTION",
            resourceAccessor,
            originalLocale);
 
       //get root structure for input parameters
       IGPStructureInfo input = technicalDescription.getInputStructureInfo();
 
       //define attributes for input parameters
       IGPAttributeInfo userId =
         input.addAttribute("UserID", IGPAttributeInfo.BASE_STRING);
       userId.setMultiplicity(IGPAttributeInfo.MULITIPLICITY_1_1);
 
       //get root structure for output parameters
       IGPStructureInfo output =
         technicalDescription.getOutputStructureInfo();
 
       //add a sub-structure for the output
       IGPStructureInfo user = output.addStructure("User");
 
       //define attributes in output sub-structure
       user.addAttribute("firstName", IGPAttributeInfo.BASE_STRING);
       user.addAttribute("lastName", IGPAttributeInfo.BASE_STRING);
       user.addAttribute("phone", IGPAttributeInfo.BASE_STRING);
       user.addAttribute("fax", IGPAttributeInfo.BASE_STRING);
 
       //add a result state for successful execution
       IGPCOResultStateInfo success =
         technicalDescription.addResultState("Success");
       success.setDescriptionKey("Success_desc");
       IGPExceptionInfo exception = technicalDescription.addProcessException("EXCEPTION_NO_USER_FOUND");
       return technicalDescription;
 
     } catch (GPInvocationException e) {
       logger.logT(
         Severity.ERROR,
         Category.APPLICATIONS,
         "Incorrect technical name");
       logger.traceThrowableT(
         Severity.ERROR,
         "Exception while creating technical description: ",
        e);
       return null;
     }
   }
 
publicvoid execute(IGPExecutionContext executionContext)
      throws GPTechnicalCallableObjectException {
         String userId = null;
 
     try {
 
       //retrieve the runtime representation of the input structure  
       IGPStructure input = executionContext.getInputStructure();
 
       //retrieve the value of the input parameter
       userId = (String) input.getAttributeAsString("UserID");
 
       //retrieve the runtime representation of the output structure
      IGPStructure output = executionContext.getOutputStructure();
      IGPStructure user = output.addStructure("User");
 
       try {
 
         //find user in user management
         IUser umeUser =
            UMFactory.getUserFactory().getUserByLogonID(userId);
 
         //set values to output parameters
         user.setAttributeValue("firstName", umeUser.getFirstName());
         user.setAttributeValue("lastName", umeUser.getLastName());
        user.setAttributeValue("phone",umeUser.getTelephone());
         user.setAttributeValue("fax", umeUser.getFax());
 
        //set result state
         executionContext.setResultState("Success");
 
       } catch (UMException e) {
         //set process exception if user is not found
        executionContext.setProcessException("EXCEPTION_NO_USER_FOUND");
 
       }
 
       //complete object execution
       executionContext.processingComplete();
 
     } catch (GPInvocationException e) {
       throw new GPTechnicalCallableObjectException(
           logger,
            resourceAccessor,
            "ERROR_PARAMETERS",
           e);
     } catch (GPEngineException e) {
       throw new GPTechnicalCallableObjectException(
           logger,
            resourceAccessor,
            "INTERNAL_ERROR",
         e);
     }
   }

}