1
votes

I’m using he XPages REST control to access resources in a database that is different from the db in which the REST control itself is located. That is easy, I’m just using the databaseName property In SSJS I would like to get a handle to the external database ( the one referred to in the databaseName property) and that seems to be rather challenging In this particular case I need that handle in a view column formula to access data on a parent document. If there was no external database involved, I would simply use the global database property, but that will not work in this case as it points to the database that host the REST control.

2
Not sure if this helps but this is how I connect to an external database in Bluemix to create a searchable REST service - xomino.com/2015/11/27/…MarkyRoden
store this external databaseName in the xsp.properties or in an own property file. So the whole application can benefit from this parameterFrank van der Linden

2 Answers

1
votes

It was not pointed out in my original question, but I do have a config-servlet, that defines the path to the external database.

The question was more about getting a handle to the database object in a way that do not create to much overhead. After a discussion with Paul Winters at slack(OpenNTF channel) yesterday, I’m now confident that opening the external db in a view column is perfectly ok.

Rather than looking for a handle to the external source within the service, I simply open the db in code whenever neededSo my code for the final solution boils down to a few simple lines of code:

var pid = rowData.getColumnValue("r_companyid"); var path = configBean.getDbPath( no.mycompany.myapp.Configuration.DB_PATH_CONTACTS ); var contactsDb:NotesDatabase = session.getDatabase(database.getServer(), path); var docParent:NotesDocument = contactsDb.getDocumentByUNID(pid); retVal = docParent.getItemValueString("CompanyName");

1
votes

I highly recommend what Frank van der Linden recommended in the comments, to set it as an xsp property, which can be easily referenced by any property getting method/function you use, and has the nice side effect of making your data connection call be easily copied/pasted between applications.

Example:

I have the data property name stored as an xsp property, in <app>/WebContent/WEB-INF/xsp.properties:

xsp.local.data=MyApp_data.nsf

I happen to use a managed bean to perform the app config work:

<managed-bean>
  <managed-bean-name>confBean</managed-bean-name>
  <managed-bean-scope>application</managed-bean-scope>
  <managed-bean-class>com.myApp.config.AppUtil</managed-bean-class>
</managed-bean>

This means that my AppUtil bean sets the property to the db path:

ExtLibUtil.getXspContext().getProperty("xsp.local.data", "MyApp_data.nsf")

*note: the first parameter is the property name, the second is the 'default' (fail-over) value, in the event it doesn't find the property's value

Then to pull the full path for your xe:restService, all you would need to do is shove in the values, like so (assuming the same server, example shows an xe:viewJsonService with default columns ):

<?xml version="1.0" encoding="UTF-8"?>
<xp:view
    xmlns:xp="http://www.ibm.com/xsp/core"
    xmlns:xe="http://www.ibm.com/xsp/coreex">
    <xe:restService
        id="restService1"
        pathInfo="data">
        <xe:this.service>
            <xe:viewJsonService
                databaseName="#{confBean.dbAppPath}"
                viewName="SomeView"
                defaultColumns="true" />
        </xe:this.service>
    </xe:restService>
</xp:view>

[Update]

As Sven Hasselbach points out in the comments below, a managed property would achieve about the same result, without editing the xsp properties file and keeps the property (which is explicit for the app) inline with the managed bean definition. Taken from his answer on XPages managed beans and scoped variables, here's an example of how Sven does the same, using a managed property:

in faces-config.xml

<managed-bean>
    <managed-bean-name>myBean</managed-bean-name>
    <managed-bean-class>ch.hasselba.xpages.MyBean</managed-bean-class>
    <managed-bean-scope>application</managed-bean-scope>

    <managed-property>
        <property-name>dbName</property-name>
        <value>MyName</value>
        <property-class>java.lang.String</property-class>
    </managed-property>
</managed-bean>

In his config bean, it's defined as any property (a la POJO), with private property and public getter/setter:

//...
private String dbName;

public void setDbName(String dbName) {
    this.dbName = dbName;
}

public String getDbName() {
    return dbName;
}
//...

Usable just as any managed bean property:

<xp:text
    value="#{myBean.dbName}" />

[/Update]