سؤال

I am creating a new database via JS code on an action button, I then copy a set of XPages, Custom Controls etc into the new database. While I'm doing this I want to set the content of the xsp.properties to a know set of properties. I'm not sure how to get access from js to the xsp.properties file. I have created a Notes view of all design elements and can see three or four elements listed under WEB-INF/xsp.properties but not sure which one to read from. What I really want to do is make the xsp.properties in the new DB the same as the one I'm copying from.

Thanks

هل كانت مفيدة؟

المحلول

You can do this via the Java NAPI. For this, you have to create a new Java Class like this:

package ch.hasselba.xpages.util;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.Properties;

import com.ibm.designer.domino.napi.NotesAPIException;
import com.ibm.designer.domino.napi.NotesDatabase;
import com.ibm.designer.domino.napi.NotesNote;
import com.ibm.designer.domino.napi.NotesSession;
import com.ibm.designer.domino.napi.design.FileAccess;

public class Toolbox {

    /**
     * loads the properties from a file
     * 
     * @param dbPath full path of the database
     * @param fileName name of the file to load
     * @return the properties object
     */
    public Properties loadProperties(final String dbPath, final String fileName) {
        try {
            // load the file
            InputStream inStream = getFile( dbPath, fileName );

            // if file exists, init a properties object
            if (inStream != null) {
                Properties props = new Properties();
                props.load( inStream );
                return props;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * saves a property file to a database
     * 
     * @author Sven Hasselbach
     * 
     * @param dbPath full path of the database
     * @param fileName name of the file to load
     * @param props the properties object
     */
    public void saveProperties(final String dbPath, final String fileName, final Properties props) {
        try {
            // init Notes objects
            NotesSession nSession = new NotesSession();
            NotesDatabase nDB = nSession.getDatabaseByPath(dbPath);
            nDB.open();

            // store properties in byte array
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            props.store(bos, "My XSP Properties");

            // save the property file
            NotesNote nFile = FileAccess.getFileByPath(nDB, fileName);
            FileAccess.saveData(nFile, fileName, bos.toByteArray() );

            // recycle the objects
            nFile.recycle();
            nDB.recycle();
            nSession.recycle();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * loads a property file from a database
     * 
     * @author Sven Hasselbach
     * @param dbPath full path of the database
     * @param fileName name of the file to load
     * @return InputStream content of the file
     */
    private InputStream getFile(final String dbPath, final String fileName) {
        try {
            // init Notes objects
            NotesSession nSession = new NotesSession();
            NotesDatabase nDB = nSession.getDatabaseByPath(dbPath);
            nDB.open();

            // get the file
            NotesNote nNote = FileAccess.getFileByPath(nDB, fileName);
            InputStream inStream = FileAccess.readFileContentAsInputStream(nNote);

            // recycle the objects
            nNote.recycle();
            nDB.recycle();
            nSession.recycle();

            return inStream;
        } catch (NotesAPIException apiEx) {
            apiEx.printStackTrace();
        }
        return null;
    }

}

To compile this class, you have to add the Jar lwpd.domino.napi.jar to the build path.

Now you can create a button like this:

<xp:button
    value="Add property"
    id="button1">
    <xp:eventHandler
        event="onclick"
        submit="true"
        refreshMode="complete">
        <xp:this.action>
            <![CDATA[#{javascript:
                importPackage( ch.hasselba.xpages.util );
                var toolbox:ch.hasselba.xpages.utils.Toolbox = new Toolbox();
                var props:java.util.Properties = toolbox.loadProperties( database.getFilePath(), "WEB-INF/xsp.properties" );

                props.put( "Test", "123" );

                toolbox.saveProperties( database.getFilePath(), "WEB-INF/xsp.properties", props );
            }]]>
        </xp:this.action>
    </xp:eventHandler>
</xp:button>

نصائح أخرى

The easiest way to do this isn't copying design elements around, but to create the database from a template. Create a template that contains everything that shall be the same in all the databases you create. Give it a template name and create the databases based on the template (there's a call for that)

Look at it with Java eyes.

Properties file is standard resource of J2EE application with its own API. So according to this you can simply read and write properties in Java/SSJS:

    public void saveParamChanges() {
        try {
            Properties props = new Properties();
            props.setProperty("ServerAddress", serverAddr);
            props.setProperty("ServerPort", ""+serverPort);
            props.setProperty("ThreadCount", ""+threadCnt);
            File f = new File("server.properties");
            OutputStream out = new FileOutputStream( f );
            props.store(out, "This is an optional header comment string");
        }
        catch (Exception e ) {
            e.printStackTrace();
        }
    }

Also this snippet seems to be working.

I am almost sure you will need to tweak security policies...

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top