Thursday, November 1, 2012

Access to XmlStore files from within Ws-App java code


Today I needed to have access from within Java Ws-App code to an XML definition located within the XML Store.
Within a Cordys BPM those webservices are available and can be easily drag and dropped into your BPM, but if you need this within your java code it can be easily accomplished by executing a Soap request.

public class XmlStore
{
    final String NS_CASEMNGT_INST = "http://schemas.cordys.com/casemanagement/instanceadministration/1.0";

    public static int getXMLObject(String userDn, String key)
    {
        int retVal = 0;
        int response = 0;

        try
        {
            // Build up the request to send.
            String[] paramNames = { "key" };
            Object[] paramValues = { key };
            SOAPRequestObject sro = new SOAPRequestObject(NS_CASEMNGT_INST , "GetXMLObject", paramNames, 
                        paramValues);

            // Set the proxy user if passed on
            if (StringUtils.isSet(userDn))
            {
                sro.setUser(userDn);
            }

            // Send the actual request.
            response = sro.sendAndWait();

            // Find the response document
            retVal = XPathHelper.selectSingleNode(response, "//xs:old/*", Constants.xmi);
        }
        catch (Exception e)
        {
            throw new CustomException(e, Messages.ERROR_GETTING_FILE_FROM_XML_STORE, key);
        }
        return retVal;
    }

And in the calling function you have than something like this:
Int xmlstoreObject = XMLStore.getXMLObject(BSF.getUser(), "/nl/project/xmlstore/xmlfile.xml");

Don't forget in your calling code to clean-up the NOM element by using the 'Node.delete(node)' function.
Otherwise memory leaks can be easily introduced.

Retrieval of user details of logged-in Cordys user

Retrieval of the user details of the logged-in user can be easily done using the BSF and LDAP build-in utility functions.

Below an example of a Java function which retrieves the ‘Full User Name’ and ‘email’ adres which is shown within the Cordys User Manager. Furthermore, it retrieves the ‘Full Organization Name’ from the Cordys Organisation Manager.

/**
  * This method returns the raadpleger details of the logged-in user.
  *
  * @return  A string array with 3 values:
  *  - value 1: Full Name of the logged-in user
  *  - value 2: Email of the logged-in user
  *  - value 3: Full Name of the organization of the user
 */
 public static String[] getRaadplegerDetails()
 {
    String[] raadplegerDetails = new String[3];

String userDN = BSF.getUser();

    String orgDN = BSF.getOrganization();
   
LDAPDirectory ldir = LDAPDirectory.getDefaultInstance();
        LDAPEntry userLdapEntry = ldir.read(userDN);
        LDAPEntry orgLdapEntry = ldir.read(orgDN);

        // Check if it's an organizational user or an authenticated one.
        LDAPAttribute attr = userLdapEntry.getAttribute("objectclass");
        String[] classes = attr.getStringValueArray();
        boolean found = false;

        for (String className : classes)
        {
            if ("busorganizationaluser".equals(className))

            {
                found = true;
                break;
            }
}

        // Read the entry of the authenticated user if the passed on DN was of an org user.
if (found)
        {
            String authUser = LDAPUtils.getAttrValue(userLdapEntry, "authenticationuser");
            userLdapEntry = ldir.read(authUser);
}

        // Set the Full User Name
        raadplegerDetails[0] = LDAPUtils.getAttrValue(userLdapEntry, "description");
        // Set the Email
        raadplegerDetails[1] = LDAPUtils.getAttrValue(userLdapEntry, "mail");
        // Set the Full Organization Name
        raadplegerDetails[2] = LDAPUtils.getAttrValue(orgLdapEntry, "description");
    }
       return raadplegerDetails;
   }