How to create custom properties for a resource environment provider using wasadmin scripting

Here’s an example wasadmin script (in Jython) that demonstrates how to create and/or update custom properties for resource environment providers. This one, in particular, is a good template for scripting the creation and/or updating of dynamic content spot mappings for a WebSphere Portal theme.

First, I want to give a shout out to Gabriel Aberasturi, System Analist at Grupo Versia who helped me find this long desired script by answering my question in LinkedIn; he pointed me to IBM documentation for this that none of my searches had returned. Also, after posting the original article, my friend Jared Piper, who works at the Neiman Marcus Group, provided a much improved script, which has now replaced my old example in this article. I could never be any good if it wasn’t for a strong community of people like Gabriel and Jared who take the time to help. That’s exactly why I blog what I learn – to give back. Where would any of us be without the virtual community that makes us stronger? I know I speak for everyone in ours when I say to Gabriel and those who take the time to share that we salute you.

Anyway, here’s the script…

Jython: Create dynamic content spot mappings

# To execute, navigate to wp_profile/bin and use:
# wsadmin -lang jython -user wpsadmin -password wpsadmin -conntype SOAP -host mojo.base22.com -port 10025 -f C:/workspace/themeScripts/updated_example.py
 
# Identify the parent ID and assign it to the newrep variable.
newrep = AdminConfig.getid('/ResourceEnvironmentProvider:WP DynamicContentSpotMappings/')
print newrep
 
# Identify the required attributes
#print AdminConfig.required('J2EEResourceProperty')
 
# Identify all possible attributes
#print AdminConfig.attributes('J2EEResourceProperty')
 
# Set up a list of dictionaries for the properties. Each Dictionary should have "name" and "value" at a minimum. Look at line 10 for possible attributes
themePropertyList = [
    {"name": "MyTheme_toolbar", "value": "res:/wps/themeModules/modules/pagebuilder/jsp/toolbar.jsp,wp_toolbar"},
    {"name": "MyTheme_status", "value": "res:/MyTheme/themes/html/dynamicSpots/status.jsp,wp_status_bar"},
    {"name": "MyTheme_sideNav", "value": "res:/MyTheme/themes/html/dynamicSpots/sideNavigation.jsp?startLevel=2"},
    {"name": "MyTheme_projectMenu", "value": "res:/wps/themeModules/modules/pagebuilder/jsp/projectMenu.jsp,wp_project_menu"},
    {"name": "MyTheme_preview", "value": "res:/wps/themeModules/modules/pagebuilder/jsp/preview.jsp,wp_preview"},
    {"name": "MyTheme_asaPortlet", "value": "res:/wps/themeModules/modules/asa/jsp/asaPortlet.jsp, wp_analytics"},
    {"name": "MyTheme_asaHead", "value": "res:/wps/themeModules/modules/asa/jsp/head.jsp, wp_analytics"},
    {"name": "MyTheme_topNav", "value": "res:/MyTheme/themes/html/dynamicSpots/navigation.jsp?rootClass=wpthemeHeaderNav&startLevel=0&primeRoot=true"},
    {"name": "MyTheme_pageModeToggle", "value": "res:/MyTheme/themes/html/dynamicSpots/pageModeToggle.jsp, wp_toolbar"},
    {"name": "MyTheme_primaryNav", "value": "res:/MyTheme/themes/html/dynamicSpots/navigation.jsp?rootClass=wpthemePrimaryNav%20wpthemeLeft&startLevel=1"},
    {"name": "MyTheme_secondaryNav", "value": "res:/MyTheme/themes/html/dynamicSpots/navigation.jsp?rootClass=wpthemeSecondaryNav&startLevel=2&levelsDisplayed=2"},
    {"name": "MyTheme_crumbTrail", "value": "res:/MyTheme/themes/html/dynamicSpots/crumbTrail.jsp?rootClass=wpthemeCrumbTrail&startLevel=2"},
    {"name": "MyTheme_layout", "value": "lm:template"},
    {"name": "MyTheme_asa", "value": "res:/wps/themeModules/modules/asa/jsp/asa.jsp,wp_analytics"},
    {"name": "MyTheme_search", "value": "res:/MyTheme/themes/html/dynamicSpots/modules/search/coveo-search.jsp"}
]
 
# Get the J2EE resource property set:
propSet = AdminConfig.showAttribute(newrep, 'propertySet')
resourceProperties = AdminConfig.list("J2EEResourceProperty", propSet).splitlines()
 
# Loop through properties we are adding
for property in themePropertyList:
    found = 0
    for resourceProperty in resourceProperties:
        if (AdminConfig.showAttribute(resourceProperty, "name") == property["name"]):
            # Modify if values are different
            if (AdminConfig.showAttribute(resourceProperty, "value") != property["value"]):
                print("Found and modified: ", property["name"])
                print AdminConfig.modify(resourceProperty, [['value', property["value"]]])
            else:
                print("Found but did not modify: ", property["name"])
            found = 1
            break
    if found == 0:
        # Add to the property set
        print("Creating new property: ", property["name"], " ", property["value"])
        print AdminConfig.create('J2EEResourceProperty', propSet, [["name", property["name"]], ["value", property["value"]]])
 
# Save the configuration changes:
AdminConfig.save()
 
# Display finished message
print "Config Saved."