下面是在 DAO 中跟踪组件和二进制文件的示例代码。
组件:要添加的示例代码,更新和删除也是如此
@Component("JPAComponentDAOExtension")
@Scope("prototype")
public class JPAComponentDAOExtension extends JPAComponentPresentationDAO implements ComponentPresentationDAO
{
public JPAComponentDAOExtension(String storageId, EntityManagerFactory entityManagerFactory, String storageName)
{
super(storageId, entityManagerFactory, storageName);
}
public JPAComponentDAOExtension(String storageId, EntityManagerFactory entityManagerFactory, EntityManager entityManager, String storageName)
{
super(storageId, entityManagerFactory, entityManager, storageName);
}
public void create(ComponentPresentation itemToCreate, ComponentPresentationTypeEnum componentPresentationType) throws StorageException
{
super.create(itemToCreate,componentPresentationType);
String tcmURI = Integer.toString(itemToCreate.getComponentId());
ItemDAO item = (ItemDAO) StorageManagerFactory.getDAO(itemToCreate.getPublicationId(),StorageTypeMapping.COMPONENT_META);
ComponentMeta meta = (ComponentMeta) item.findByPrimaryKey(itemToCreate.getPublicationId(),itemToCreate.getComponentId());
String schemaID = Integer.toString(meta.getSchemaId()) ;
PublishActionDAO publishActionDAO = (PublishActionDAO) StorageManagerFactory.getDefaultDAO("PublishAction");
PublishAction publishAction = new PublishAction();
publishAction.setAction("ADD");
publishAction.setTcmUri(tcmURI);
publishAction.setSchemaID(schemaID);
publishActionDAO.store(publishAction);
}
}
二进制:示例代码添加和更新和删除同样适用
@Component("JPABinaryDAOExtension")
@Scope("prototype")
public class JPABinaryDAOExtension extends JPABinaryContentDAO implements BinaryContentDAO
{
public JPABinaryDAOExtension(String storageId, EntityManagerFactory entityManagerFactory, String storageName)
{
super(storageId, entityManagerFactory, storageName);
}
public JPABinaryDAOExtension(String storageId, EntityManagerFactory entityManagerFactory, EntityManager entityManager, String storageName)
{
super(storageId, entityManagerFactory, entityManager, storageName);
}
public void create(final BinaryContent binaryContent, final String relativePath) throws StorageException
{
super.create(binaryContent, relativePath);
String url = relativePath;
String tcmURI = Integer.toString(binaryContent.getBinaryId());
ItemDAO item = (ItemDAO) StorageManagerFactory.getDAO(binaryContent.getPublicationId(),StorageTypeMapping.BINARY_META);
BinaryMeta binarymeta = (BinaryMeta) item.findBinaryByPrimaryKey(binaryContent.getPublicationId(),binaryContent.getBinaryId());
binarymeta.getBinaryType();//to get the binary type
//You can also check the Relative path as below for specific binary type entries as suggested by Mihai
if (relativePath.toLowerCase().endsWith(".pdf")) //Looking for PDFs only
{
PublishActionDAO publishActionDAO = (PublishActionDAO) StorageManagerFactory.getDefaultDAO("PublishAction");
PublishAction publishAction = new PublishAction();
publishAction.setAction("ADD");
publishAction.setUrl(url);
publishAction.setTcmUri(tcmURI);
publishActionDAO.store(publishAction);
}
}
}