Reputation: 2662
So I have been working with Alfresco for a project I'm currently working on, and one of the newest requirements for the project is that I pull a sequence number from our Oracle database and populate a custom property within a space in Alfresco.
var conObj = new ActiveXObject('ADODB.Connection');
var connectionString = "Provider=OraOLEDB.Oracle;Data Source=(DESCRIPTION=(CID=GTU_APP)(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=IP ADDRESS)(PORT=XXXX)))(CONNECT_DATA=(SID=your_SID)(SERVER=DEDICATED)));User Id=username;Password=pwd;"
conObj.Open(connectionString);
var rs = new ActiveXObject("ADODB.Recordset");
var caseID;
sql = "INSERT INTO case(mod_dt,mod_user) values(sysdate,’user’) RETURNING id"
rs.Open(sql, conObj);
caseID = rs(0);
logger.log("The new case id is: " + caseID);
rs.close;
However, this returned saying:
Caused by: org.mozilla.javascript.EcmaError: ReferenceError: "ActiveXObject" is not defined. (workspace://SpacesStore/b3145512-e54d-4d9e-9655-0b6ae678e39b#141)
Which made me realize Alfresco's Javascript API doesn't have the full functionality of Javascript. I had read something about creating your own java class and calling them from within the Alfresco javascript, but I had not seen any good examples. Does anyone have experience with this or could they demonstrate a simple example of creating a java class to call from within Alfresco Javascript?
Upvotes: 0
Views: 1898
Reputation: 48336
ActiveXObject
is an IE specific thing, it's not available in non-IE browsers so it's hardly surprising that it isn't available in server-side JavaScript!
As I understand it, you're writing your JavaScript to be run as a rule, so it'll be executed in the Alfresco Repository tier. That makes life slightly easier. What you'll probably want to do is write some Java code that handles the connection and querying of Oracle, using Oracle's Java APIs (JDBC or similar).
When running in the repository, your JavaScript already has access to a large number of "root" objects, which can be used to perform a variety of operations on the repository. What you'll want to do is inject your new Java class as an additional one, so it's available for your rule script to use when it runs.
(If you were writing a webscript, then you could just arrange to have the java object made available to the JavaScript model of your webscript. However, as you're doing a rule, that's not an option)
To do that, have your new class extend BaseScopableProcessorExtension. Then, when you define a spring bean for it, set the extensionName
property to control the name it appears in JavaScript as. There are quite a few examples in Alfresco itself you can look at for this, ScriptSiteSevice
(bean id siteScriptService
) is one that springs to mind.
In case you're new to spring and Alfresco, I'd suggest you either wrap your whole thing up as a module (AMP), or cheat a little and just drop the context file in a new alfresco/extensions
directory under your tomcat shared classes. Your file would look something like:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE beans PUBLIC '-//SPRING//DTD BEAN//EN' 'http://www.springframework.org/dtd/spring-beans.dtd'>
<beans>
<bean id="myOracleQueryBean" parent="baseJavaScriptExtension"
class="com.my.comany.namespace.alfresco.OracleScriptQuery">
<!-- What it should be called in JS -->
<property name="extensionName">
<value>oracleQuery</value>
</property>
<!-- Inject any other things that your bean needs here -->
<!-- eg some Oracle stuff from Spring -->
</bean>
Upvotes: 2