在Tridion 2011 SP1的存储扩展中没有调用DAO类中的实现方法

时间:2013-01-06 15:14:32

标签: tridion tridion-2011 tridion-content-delivery tridion-storage-extension

我没有从DAO Class调用我实现的方法。

我创建了一个名为search_dao_bundle.xml的bundle xml,如下所示,放在同一个位置,即我的cd_storage_xml所在的tridion_home / config。

<?xml version="1.0" encoding="UTF-8"?>
<StorageDAOBundles>
<StorageDAOBundle type="persistence">
    <StorageDAO typeMapping="PublishAction" class="com.tridion.storage.extension.search.JPAPublishActionDAO" />
</StorageDAOBundle>
</StorageDAOBundles>

之后我将我的包条目添加到我的cd_storage_conf.xml中,如下所示:

<StorageBindings>
    <Bundle src="search_dao_bundle.xml"/>
</StorageBindings>

并在我下面创建了我的新存储类型,如下所示:

<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="********" />
        <!--Property Name="portNumber" Value="1433" /-->
        <Property Name="databaseName" Value="********" />
        <Property Name="user" Value="********" />
        <Property Name="password" Value="********" />
    </DataSource>
</Storage>

之后我在下面进行了项目图示

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

我重启了我的部署服务,在我的核心日志

中得到了以下异常

以下是取自Mihai Code的样本DAO课程:

package com.tridion.storage.extension.search;

import java.util.HashMap;
import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import com.tridion.broker.StorageException;
import com.tridion.storage.extension.search.PublishActionDAO;
import com.tridion.storage.persistence.JPABaseDAO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component("JPAPublishActionDAO")
@Scope("prototype")
public class JPAPublishActionDAO extends JPABaseDAO implements PublishActionDAO
{
    private static Logger log = LoggerFactory.getLogger(JPAPublishActionDAO.class);
    public JPAPublishActionDAO(String storageId, EntityManagerFactory entityManagerFactory, String storageName) 
    {
        super(storageId, entityManagerFactory, storageName);        
        log.debug("Constructor of JPAPublishActionDAO- storageId:"+storageId);
        log.debug("Constructor of JPAPublishActionDAO- entityManagerFactory:"+entityManagerFactory.isOpen());       
        log.debug("Constructor of JPAPublishActionDAO- storageName:"+storageName);
    }

     public JPAPublishActionDAO(String storageId, EntityManagerFactory entityManagerFactory, EntityManager entityManager, String storageName) 
     {       
            super(storageId, entityManagerFactory, entityManager, storageName);         
     }

     public PublishAction store(PublishAction publishAction) throws StorageException 
     {
         log.debug("JPAPublishActionDAO store");
         //System.out.println("\n******************** From Store *************************************");
            PublishAction entity = (PublishAction) super.create(publishAction);
            return entity;
     }

     @SuppressWarnings("unchecked")
    public PublishAction findByPrimaryKey(long publishActionId) throws StorageException 
     {
            log.debug("JPAPublishActionDAO findByPrimaryKey");
            StringBuilder queryBuilder = new StringBuilder();
            queryBuilder.append("select c from PublishAction c where c.id = :id");

            @SuppressWarnings("rawtypes")
            Map queryParams = new HashMap();
            queryParams.put("id", Long.valueOf(publishActionId));
            log.debug("JPAPublishActionDAO findByPrimaryKey -> queryBuilder- " +queryBuilder.toString());
            return (PublishAction) super.executeQuerySingleResult(queryBuilder.toString(), queryParams);
     }

     @SuppressWarnings("unused")
    public PublishAction update(PublishAction publishAction) throws StorageException 
     {
            log.debug("JPAPublishActionDAO update");
            PublishAction existingPublishAction = findByPrimaryKey(publishAction.getId());

            log.debug("JPAPublishActionDAO update -> existingPublishAction- " +existingPublishAction.toString());
            if (existingPublishAction != null) 
            {
                   return (PublishAction) super.update(publishAction);
            } 
            else 
            {
                   throw new StorageException("Could not find publish action in storage to update!!!");
            }
     }

     public void remove(long publishActionId) throws StorageException 
     {
            log.debug("JPAPublishActionDAO remove");
            PublishAction foundPublishAction = findByPrimaryKey(publishActionId);
            log.debug("JPAPublishActionDAO remove -> foundPublishAction- " +foundPublishAction.toString());
            if (foundPublishAction != null) 
            {
                   super.remove(foundPublishAction);
            }
     }
}

我能够看到我的构造函数被调用,即我在核心文件日志中获取这些日志

log.debug("Constructor of JPAPublishActionDAO- storageId:"+storageId);
log.debug("Constructor of JPAPublishActionDAO- entityManagerFactory:"+entityManagerFactory.isOpen());       
log.debug("Constructor of JPAPublishActionDAO- storageName:"+storageName);

但是,我没有使用其他方法编写任何日志,例如方法 public PublishAction store log.debug(“JPAPublishActionDAO store”);

log.debug(“JPAPublishActionDAO findByPrimaryKey”);

log.debug(“JPAPublishActionDAO update”);

可能是什么原因,我的实体类名称(PublishAction.java)和接口类(PublishActionDAO.java)与给出的示例代码相同。

3 个答案:

答案 0 :(得分:3)

PublishAction类型不是默认的Tridion类型之一,这意味着默认情况下不会使用它。为了使用DAO,您需要在部署过程中以某种方式调用它,通常是从Deployer模块中调用它。你能看看你使用这个PublishActionDAO的方式和位置吗?

答案 1 :(得分:3)

我无法在评论中发布格式化代码,因此这个新答案。

@sea_gull是对的 - 你确实需要调用新的DAO。这是一个新类型的原因,因此Content Delivery存储机制将不知道如何处理它。你必须以某种方式调用它(可能来自部署者模块,但不一定)。我使用单元测试来调用它(只是为了证明它有效)。

这是我用来调用存储扩展的示例单元测试代码:

package com.tridion.extension.search.test;

import static org.junit.Assert.fail;

import java.util.Date;

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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 {

    private final Logger log = LoggerFactory.getLogger(DAOTestCase.class);

    /**
     * Test method for
     * {@link com.tridion.storage.extension.search.PublishActionDAO#store(com.tridion.storage.search.PublishAction)}.
     */
    @Test
    public void testDAO() {
        try {
            log.debug("Get PublishActionDAO");
            PublishActionDAO publishActionDAO = (PublishActionDAO) StorageManagerFactory.getDefaultDAO("PublishAction");

            log.debug("Create new PublishAction bean");
            PublishAction publishAction = new PublishAction();
            publishAction.setAction("testStore action");
            publishAction.setContent("testStore content");
            publishAction.setTcmUri("testStore tcmUri");
            publishAction.setUrl("testStore url");
            publishAction.setCreationDate(new Date());

            // Store
            log.debug("Store bean");
            publishAction = publishActionDAO.store(publishAction);
            log.debug("Stored bean " + publishAction);
            long id = publishAction.getId();

            // FindByPrimaryKey
            log.debug("Find PublishAction by PK=" + id);
            publishAction = publishActionDAO.findByPrimaryKey(id);
            log.debug("Found bean " + publishAction);

            if (publishAction == null) {
                log.error("Cannot find bean");
                fail("TestFindByPrimaryKey failed: cannot retrieve object with pk " + id);
            }

            log.debug("Modifying bean content");
            String content = publishAction.getContent();
            content += "\r\nMODIFIED " + new Date();
            publishAction.setContent(content);

            // Update
            log.debug("Update bean");
            publishActionDAO.update(publishAction);

            // Remove
            log.debug("Remove bean");
            publishActionDAO.remove(id);
        } catch (StorageException se) {
            log.debug("TestDAO failed: Exception occurred " + se);
            fail("TestDAO failed: Exception occurred " + se);
            se.printStackTrace();
        }
    }
}

如果从Deployer扩展中调用代码,这是我使用的示例代码:

public class PageDeployModule extends PageDeploy {

    private final Logger log = LoggerFactory.getLogger(PageDeployModule.class);

    public PageDeployModule(Configuration config, Processor processor) throws ConfigurationException {
        super(config, processor);
    }

    /**
     * Process the page to be published
     */
    @Override
    protected void processPage(Page page, File pageFile) throws ProcessingException {
        log.debug("Called processPage");

        super.processPage(page, pageFile);
        processItem(page);
    }

    private void processItem(Page page) {
        log.debug("Called processItem");
        try {
            SearchConfiguration config = SearchConfiguration.getInstance();
            String externalUrl = config.getExternalAccessUrl() + page.getURLPath();
            String internalUrl = config.getInternalAccessUrl() + page.getURLPath();

            PublishAction publishAction = new PublishAction();
            publishAction.setAction("Publish");
            publishAction.setTcmUri(page.getId().toString());
            publishAction.setUrl(externalUrl);
            publishAction.setContent(Utils.getPageContent(internalUrl));

            PublishActionDAO publishActionDAO = (PublishActionDAO) StorageManagerFactory.getDefaultDAO("PublishAction");
            publishAction = publishActionDAO.store(publishAction);
            log.debug("Stored bean " + publishAction);
        } catch (StorageException se) {
            log.error("Exception occurred " + se);
        }
    }
}

您可以对PageUndeploy使用相同的方法,将操作标记为“取消发布”。

答案 2 :(得分:0)

需要更多信息: 1)在想要调用这些方法时,您实际发布了什么 - 动态CP或页面

2)您从哪里收到此代码?我没有在你的代码中看到任何“创建”方法......你从哪里收到这段代码

在您之前关于“没有命名Bean加载”错误的帖子中; Nuno在Pankaj Gaur(即我)发起的关于Tridion论坛的讨论(也给出了那里的论坛链接)提到了......你提到了吗?

请注意,如果您的构造函数被调用,那么您的配置中至少没有问题;它是您要发布的类型中的代码或不匹配。

另请注意,如果您尝试重新发布某些内容而不进行任何更改,则可能无法加载存储扩展(或者可能无法调用这些方法);所以我在debuging期间的建议,publsh总是在你的演示文稿中做出一些改变之后。

我希望它有所帮助,否则将共享骨架代码和JAR文件,我将尝试