Dashboard > RIFE > ... > Templates > Templates stored in a database
RIFE Log In   View a printable version of the current page.
Templates stored in a database


Added by Geert Bevin, last edited by Joshua Hansen on Aug 03, 2007  (view change) show comment
Labels: 
(None)

Thanks to the Abstracted resource handling, templates can be stored, modified and retrieved from a database. To make this work you'll have to make sure that the resource structure has been installed in the database through the install() method, as described above.

The only thing that you need to do to make RIFE stop looking up templates through the classpath, is to make the template factory aware of the resource finder that you want to use instead. Since you typically do that for your whole application, this would be commonly done by modifying the globally known template factories through their setResourceFinder() method, for example:

Datasource ds = Datasources.getRepInstance().getDatasource("pgsql");
DatabaseResources mResources = DatabaseResourcesFactory.getInstance(ds);
TemplateFactory.ENGINEHTML.setResourceFinder(mResources);

From now on, all engine html templates will be retrieved from your database instead.

It's probably the easiest to do this in a custom participant, for example:

package com.mypackage.rep.participants;

import java.util.ArrayList;
import java.util.logging.Logger;

import com.uwyn.rife.cmf.dam.contentmanagers.DatabaseContentFactory;
import com.uwyn.rife.cmf.dam.contentmanagers.exceptions.InstallContentErrorException;
import com.uwyn.rife.config.Config;
import com.uwyn.rife.database.Datasource;
import com.uwyn.rife.database.Datasources;
import com.uwyn.rife.rep.BlockingParticipant;
import com.uwyn.rife.resources.DatabaseResources;
import com.uwyn.rife.resources.DatabaseResourcesFactory;
import com.uwyn.rife.resources.exceptions.ResourceWriterErrorException;
import com.uwyn.rife.template.TemplateFactory;

public class ParticipantTemplateFactory extends BlockingParticipant {
    private DatabaseResources    mResources = null;

    protected void initialize()
    {
    	String datasourceName = getParameter();
        Datasource ds = Datasources.getRepInstance().getDatasource(datasourceName);
        mResources = DatabaseResourcesFactory.getInstance(ds);
       	try {
            mResources.install();
            Logger.getLogger(this.getClass().getName()).warning("The"
            +this.getClass().getName()+" has been installed.");
        } catch (ResourceWriterErrorException e) {
            Logger.getLogger(this.getClass().getName()).warning("The "
            +this.getClass().getName()
            +" structure couldn't be installed, it probably already exists.");
        }
        TemplateFactory.ENGINEHTML.setResourceFinder(mResources);
    }

    protected Object _getObject(Object key)
    {
        return mResources;
    }

    protected ArrayList _getObjects(Object key)
    {
        ArrayList objects_list = new ArrayList();
        objects_list.add(mResources);
        return objects_list;
    }
}

Finally, don't forget to add this participant to your participants.xml file:

<!-- param is the name of the datasource -->
    <participant param="pgsql"
	blocking="true">com.mypackage.rep.participants.ParticipantTemplateFactory</participant>

Once the resource finder is installed, you can use DatabaseResources.addResource() to add resources to the database.

// NOT TESTED
DatabaseResources mResources = TemplateFactory.ENGINEHTML.getResourceFinder();
mResources.addResource(
    "db_template_name.html", 
    "<!--B 'block1'-->a block with value <!--V 'value1'/--><!--/B--><!--V 'value2'/-->"
);

Versions

  • Used source from Rife 1.4. The class DatabaseResources exists in the Rife 1.2 documentation, so it may work back that far.

References

  • Rife/Base 1.4 source: rife-source-1.4.zip
  • Rife/Crud 1.2 source: rife-crud-source-1.2.zip

Further examples, resources:



Are you enjoying Confluence? Please consider purchasing it today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.2.1a Build:#515 May 19, 2006) - Bug/feature request - Contact Administrators