Extending Content Delivery Storage in SDL Tridion 2011 part 3

Configuration

Tutorial Guide:

Part 1: The Concepts
Part 2: The Code
Part 3: Configuration and Testing

The new storage types needs to be registered with the Tridion Content Delivery storage framework. We need to perform the following steps:

Create an XML file called search_dao_bundle.xml and paste in it the following markup. This will basically map the JPA implementation class JPAPublishActionDAO to the new storage type PublishAction:

<?xml version="1.0" encoding="UTF-8"?>

<StorageDAOBundles>

       <StorageDAOBundle type="persistence">

              <StorageDAO typeMapping="PublishAction"

                     class="com.tridion.storage.extension.search.JPAPublishActionDAO" />

       </StorageDAOBundle>

</StorageDAOBundles>

 

Next, declare the new DAOBundle XML with the cd_storage_conf.xml, by placing the following markup inside the <Storages> node:

<StorageBindings>

       <Bundle src="search_dao_bundle.xml" />

</StorageBindings>

 

Next, define a new Storage in cd_storage_conf.xml, specifying the details of the new database (the one we will use to store PublishAction data in). In the code below we create a new storage with id searchdb to a MSSQL database:

<Storage Type="persistence" Id="searchdb" dialect="MSSQL"

       Class="com.tridion.storage.persistence.JPADAOFactory">

       <Pool Type="jdbc" Size="5" MonitorInterval="60" IdleTimeout="120"

              CheckoutTimeout="120" />

       <DataSource Class="com.microsoft.sqlserver.jdbc.SQLServerDataSource">

              <Property Name="serverName" Value="[host]" />

              <Property Name="portNumber" Value="[port]" />

              <Property Name="databaseName" Value="[databasename]" />

              <Property Name="user" Value="[user]" />

              <Property Name="password" Value="[password]" />

       </DataSource>

</Storage>

 

Finally, associate the new storage type PublishAction with the storage id searchdb:

<Item typeMapping="PublishAction" cached="false" storageId="searchdb" />

 

For reference purpose, find below the full cd_storage_conf.xml:

<?xml version="1.0" encoding="UTF-8"?>

<Configuration Version="6.0"

       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

       xsi:noNamespaceSchemaLocation="schemas/cd_storage_conf.xsd">

       <Global>

              <Storages>

                     <StorageBindings>

                           <Bundle src="search_dao_bundle.xml" />

                     </StorageBindings>

 

                     <Storage Type="persistence" Id="brokerdb" dialect="MSSQL"

                           Class="com.tridion.storage.persistence.JPADAOFactory">

                           <Pool Type="jdbc" Size="5" MonitorInterval="60" IdleTimeout="120"

                                  CheckoutTimeout="120" />

                           <DataSource Class="com.microsoft.sqlserver.jdbc.SQLServerDataSource">

                                  <Property Name="serverName" Value="[host]" />

                                  <Property Name="portNumber" Value="[port]" />

                                  <Property Name="databaseName" Value="[databasename]" />

                                  <Property Name="user" Value="[user]" />

                                  <Property Name="password" Value="[password]" />

                           </DataSource>

                     </Storage>

 

                     <Storage Type="persistence" Id="searchdb" dialect="MSSQL"

                           Class="com.tridion.storage.persistence.JPADAOFactory">

                           <Pool Type="jdbc" Size="5" MonitorInterval="60" IdleTimeout="120"

                                  CheckoutTimeout="120" />

                           <DataSource Class="com.microsoft.sqlserver.jdbc.SQLServerDataSource">

                                  <Property Name="serverName" Value="[host]" />

                                  <Property Name="portNumber" Value="[port]" />

                                  <Property Name="databaseName" Value="[databasename]" />

                                  <Property Name="user" Value="[user]" />

                                  <Property Name="password" Value="[password]" />

                           </DataSource>

                     </Storage>

              </Storages>

       </Global>

 

       <ItemTypes defaultStorageId="brokerdb" cached="true">

              <Item typeMapping="PublishAction" cached="false" storageId="searchdb" />

       </ItemTypes>

</Configuration>

Testing your Work

"I don't usually test my code, but when I do, I use JUnit"

The following TestCase uses the JUnit framework and performs the CRUD pattern on the PublishAction storage type:

package com.tridion.extension.search.test;

 

import static org.junit.Assert.fail;

 

import java.util.Date;

 

import org.junit.Test;

 

import com.tridion.broker.StorageException;

import com.tridion.storage.StorageManagerFactory;

import com.tridion.storage.extension.search.PublishAction;

import com.tridion.storage.extension.search.PublishActionDAO;

 

/**

 * @author Mihai Cadariu

 */

public class DAOTestCase {

 

       @Test

       public void testDAO() {

              try {

                     PublishActionDAO publishActionDAO = (PublishActionDAO) StorageManagerFactory.getDefaultDAO("PublishAction");

 

                     PublishAction publishAction = new PublishAction();

                     publishAction.setAction("testStore action");

                     publishAction.setUrl("testStore url");

                     publishAction.setTcmUri("testStore tcmUri");

                     publishAction.setCreationDate(new Date());

                     publishAction.setContent("testStore content");

 

                     // Store

                     publishAction = publishActionDAO.store(publishAction);

                     long id = publishAction.getId();

 

                     // FindByPrimaryKey

                     publishAction = publishActionDAO.findByPrimaryKey(id);

                     if (publishAction == null) {

                           fail("TestFindByPrimaryKey failed: cannot retrieve object with pk " + id);

                     }

 

                     String content = publishAction.getContent();

                     content += " MODIFIED ";

                     publishAction.setContent(content);

 

                     // Update

                     publishActionDAO.update(publishAction);

 

                     // Remove

                     publishActionDAO.remove(id);

              } catch (StorageException se) {

                     fail("TestDAO failed: Exception occurred " + se);

                     se.printStackTrace();

              }

       }

}