liam_davison
I'm new here

Converting a Beanshell script to a module

Jump to solution

Hello,

I am converting a large Beanshell script into a Java-based module. It's being added as a ExecutableToolbarActionsItem.

I am finding that things are different between Beanshell and Java when writing modules which run in ContentCreator.

For instance, context.getElement() returns a Page in a Beanshell script, but returns a PageRef when in Java.

My current problem is accessing Form values in the Java module. I need to get the PageRef selected in an FS_REFERENCE form field.

In my form button which calls the Beanshell script, I have written:

<FS_BUTTON name="sc_archive_page" onClick="script:copy_and_archive_page">

        <LANGINFOS>

          <LANGINFO lang="*" label="Create Archived Copy"/>

        </LANGINFOS>

        <PARAMS>

          <PARAM name="destination">#field.sc_copy_destination</PARAM>

        </PARAMS>

      </FS_BUTTON>

And then in the script I write:

PageRef destinationPage = destination.get().get().findStartNode();

In my equivalent Java codem, which is a Toolbar action and not a form button, I have written:

PageRef currentPageRef = (PageRef) context.getElement();

Page currentPage = currentPageRef.getPage();

FormData formData;

FormField<?> destinationFF, prevArchivedRefFF, pubDateFF;

TargetReference destinationTarget, prevArchiveTarget;

currentPage.setLock(true, true);

formData = currentPage.getFormData();

destinationFF = formData.get(language, SC_ARCHIVE_PAGE);

destinationTarget = (TargetReference) destinationFF.get();

pubDateFF = formData.get(language, "pt_publish_date");

 

if(destinationTarget == null || destinationTarget.isEmpty()) {

                                        showMessage(context, "The archive location folder has not been set yet. Please chose a location by editing the page heading and choosing the Archive tab, then chosing a folder in the 'Archive page to this location' field", RequestOperation.Kind.ERROR);

                                        return;

}

But this code fails with a cast error:

de.espirit.firstspirit.client.access.editor.ButtonValueEngineerFactory$Properties cannot be cast to de.espirit.firstspirit.access.editor.value.TargetReference

I don't understand why destinationFF.get() does not return an IDProvider or TargetReference. There is no mention of ButtonValueEngineerFactory in either the Access or Developer API, so for now I'm completely stuck.

Can anyone help? Is there any guidance on converting Beanshell code to Java module code?

With thanks,

Liam Davison

0 Kudos
1 Solution

Accepted Solutions

I have resolved my problem (I was referring to the wrong form element, an FS_BUTTON and not an FS_REFERENCE).

My general point still stands though - it's a difficult process to convert a Beanshell script to a Java program.

View solution in original post

0 Kudos
2 Replies
liam_davison
I'm new here

Part of the big challenge is that Beanshell handles all the types and casts for you. Trying to work out where and how to cast appropriately is not easy!

0 Kudos

I have resolved my problem (I was referring to the wrong form element, an FS_BUTTON and not an FS_REFERENCE).

My general point still stands though - it's a difficult process to convert a Beanshell script to a Java program.

0 Kudos