How to store only PDF type of Binaries in my storage extension using Custom DAO in Tridion 2011

144 views Asked by At

I have written custom DAO to Add/Update/Delete specific records for my Binaries in my persistence type of storage extension in Tridion 2011.

Below is sample code where I am trying to use my custom DAO class/interface (PublishActionDAO and PublishAction) in my custom binary DAO (JPABinaryDAOExtension).

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());
        PublishActionDAO publishActionDAO = (PublishActionDAO) StorageManagerFactory.getDefaultDAO("PublishAction");
        PublishAction publishAction = new PublishAction();
        publishAction.setAction("Add");
        publishAction.setUrl(url);
        publishAction.setTcmUri(tcmURI);
        publishActionDAO.store(publishAction);
    }
}

Above is sample code for Add when a new binary is created and same goes for Update and Delete, I am going to store Action as Add, binary publish URL, its TCMURI. I can get these data easily in above code. Now the problem which I am facing is that I just need to store records for PDFs types of Binaries only and for other type of binaries like (JPG/Word etc.) no record entry.

Edit: Is below solution will work for me or making object for binaries it will slow down the performance.

ItemDAO item = (ItemDAO) StorageManagerFactory.getDAO(binaryContent.getPublicationId(),StorageTypeMapping.BINARY_META);
BinaryMeta binarymeta = (BinaryMeta) item.findBinaryByPrimaryKey(binaryContent.getPublicationId(),binaryContent.getBinaryId());
binarymeta.getBinaryType();
1

There are 1 answers

1
Mihai Cădariu On BEST ANSWER

Strictly checking for the type can be done by verifying the extension of the relativePath in your create method:

    if (!relativePath.toLowerCase().endsWith(".pdf")) { return; }

However, there is a better way of handling this -- in your cd_storage_conf.xml, map the Binary type using a specific extension. Then the CD storage framework will only call your DAO if the extension matches:

    <Item typeMapping="Binary" itemExtension=".pdf" storageId="myStorage"/>