C Common Utility Code Examples

This appendix provides an example code of common utilities that are often used when working with the Oracle Communications Unified Inventory Management (UIM) application program interfaces (APIs).

Example C-1 Common Utility Code

public boolean hasErrors() 
{
    boolean hasErrors = false;
    UserEnvironment userEnvironment = UserEnvironmentFactory.getUserEnvironment();
    if (userEnvironment != null) 
    {
        FeedbackProvider feedbackProvider = userEnvironment.getFeedbackProvider();
        hasErrors = feedbackProvider.hasMessages(FeedbackLevel.ERROR);
    }
    return hasErrors;
}


public FeedbackProvider getFeedbackProvider() 
{ 
    FeedbackProvider feedbackProvider = null; 
    UserEnvironment userEnvironment = getUserEnvironment(); 
    if (userEnvironment != null) 
    {
        feedbackProvider = userEnvironment.getFeedbackProvider();
    }
    return feedbackProvider;
}


protected static void commitOrRollback(UserTransaction ut)throws Exception
{
    FeedbackProvider feedbackProvider =
        getUserEnvironment().getFeedbackProvider();
    if (feedbackProvider.hasMessages(FeedbackLevel.ERROR))
    {
        if (ut != null && ut.getStatus() == Status.STATUS_ACTIVE)
            ut.rollback();
    }
    else
    {
        if (ut != null && ut.getStatus() == Status.STATUS_ACTIVE)
            ut.commit();
    }
}


protected static UserEnvironment startUserEnvironment()throws Exception
{
    UserEnvironment userEnvironment = null;
    try {
        UserEnvironment = getUserEnvironment();
        if (userEnvironment != null) 
        {
            //Reset the User Context in User Environment.
            userEnvironment.reset();
            //Begin the UserEnvironment before it is first used.
            userEnvironment.begin();
            //Reset the Feedback Provider in User Environment.
            userEnvironment.getFeedbackProvider().reset();
        }
    }
    catch (Exception e) {
        e.printStackTrace();
        throw e;
    }
    return userEnvironment;
}


protected static void endUserEnvironment(UserEnvironment userEnvironment) 
{
    if (userEnvironment == null)
        return;
    
    	userEnvironment.getFeedbackProvider().reset();
    	userEnvironment.end();}


protected static UserEnvironment getUserEnvironment() throws Exception 
{ 
    UserEnvironment userEnvironment = null; 
    try { 
        //Utils is oracle.communications.platform.util.Utils
        InitialContext initialContext = Utils.getInitialContext(); 
        String jndiContextName = "inv"; 
        String userEnvironmentName = "UserEnvironment"; 
 
        userEnvironment = (UserEnvironment)initialContext.lookup
                          (jndiContextName + "/" + userEnvironmentName); 
 
        initialContext.close(); 
    }
    catch (Exception e) { 
        e.printStackTrace(); 
        throw e; 
    } 
    return userEnvironment; 
} 

Example C-2 Changing date and time to local formats

//When working with custom characteristics of type date or datetime, with localization
<<place import of Utility here>>
//Either date string should be set using "MM/DD/YYYY hh:mm:ss" / " MM/DD/YYYY" formats.
Char.setValue("10/20/2023");
//Or Before setting the date value a conversion should be done to change the Date to UIM date
//format using an inbuilt utility method convertToUIMDateString for Date type
//convertToUIMDateTimeString and for datetime.
Date date = Utils.convertToUIMDateTimeString(new Date(2023 - 1900 , 9,18));
Char.setValue(date);