Page 1 of 1

Overriding the StaticAssetCustomPersistenceHandler [FIXED]

Posted: Wed Apr 24, 2013 5:50 am
by 22dec1960
Hi,

I would like to delete assets from database table. I followed the following posts.



I put the following configuration into applicationContext-admin.xml

Code: Select all

<bean id="blCustomPersistenceHandlerFilters" class="org.springframework.beans.factory.config.ListFactoryBean" scope="prototype">
      <property name="sourceList">
         <list>
            <bean class="org.broadleafcommerce.openadmin.server.service.handler.DefaultCustomPersistenceHandlerFilter">
               <property name="filterCustomPersistenceHandlerClassnames">
                  <list>
                     <value>org.broadleafcommerce.cms.admin.server.handler.StaticAssetCustomPersistenceHandler</value>
                  </list>
               </property>
            </bean>
         </list>
      </property>
   </bean>
   
   <bean id="blCustomPersistenceHandlers" class="org.springframework.beans.factory.config.ListFactoryBean" scope="prototype">
      <property name="sourceList">
         <list>
            <bean class="com.mine.admin.server.service.handler.MineAssetCustomPersistenceHandler"/>
         </list>
      </property>
   </bean>


And I do my own custom handler as following:

package com.mine.admin.server.service.handler;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.broadleafcommerce.cms.admin.server.handler.StaticAssetCustomPersistenceHandler;
import org.broadleafcommerce.cms.file.dao.StaticAssetDao;
import org.broadleafcommerce.cms.file.dao.StaticAssetStorageDao;
import org.broadleafcommerce.cms.file.domain.StaticAsset;
import org.broadleafcommerce.cms.file.domain.StaticAssetStorage;
import org.broadleafcommerce.common.exception.ServiceException;
import org.broadleafcommerce.openadmin.client.dto.Entity;
import org.broadleafcommerce.openadmin.client.dto.FieldMetadata;
import org.broadleafcommerce.openadmin.client.dto.PersistencePackage;
import org.broadleafcommerce.openadmin.server.dao.DynamicEntityDao;
import org.broadleafcommerce.openadmin.server.service.persistence.module.RecordHelper;

import javax.annotation.Resource;

import java.util.Map;

/**
*
* @author 22dec1960
*
*/
public class MineAssetCustomPersistenceHandler extends StaticAssetCustomPersistenceHandler {

private static final Log LOG = LogFactory.getLog(MineAssetCustomPersistenceHandler.class);

@Resource(name="blStaticAssetDao")
protected StaticAssetDao staticAssetDao;

@Resource(name="blStaticAssetStorageDao")
protected StaticAssetStorageDao staticAssetStorageDao;

@Override
public void remove(PersistencePackage persistencePackage, DynamicEntityDao dynamicEntityDao, RecordHelper helper) throws ServiceException {
Entity entity = persistencePackage.getEntity();
try {
Map<String, FieldMetadata> adminProperties = getMergedProperties();
Object primaryKey = helper.getPrimaryKey(entity, adminProperties);
StaticAsset adminInstance = (StaticAsset) dynamicEntityDao.retrieve(Class.forName(entity.getType()[0]), primaryKey);
StaticAssetStorage assetStorage = staticAssetStorageDao.readStaticAssetStorageByStaticAssetId(adminInstance.getId());
staticAssetDao.delete(adminInstance);
staticAssetStorageDao.delete(assetStorage);
} catch (Exception e) {
LOG.error("Unable to perform delete for entity: "+entity.getType()[0], e);
throw new ServiceException("Unable to delete entity for " + entity.getType()[0], e);
}
}

}


staticAssetDao.delete(adminInstance); does as required and it removes the record from "BLC_STATIC_ASSET" table. But staticAssetStorageDao.delete(assetStorage); does nothing but I hope it should also delete from "BLC_STATIC_ASSET_STRG" table.

what may be a problem?

Thanks,
22dec1960

My broadleafcommerce version is 2.0.2-GA

Re: Overriding the StaticAssetCustomPersistenceHandler

Posted: Wed Apr 24, 2013 9:02 am
by phillipuniverse
Try using the service instead of the dao (StaticAssetStorageService).

Re: Overriding the StaticAssetCustomPersistenceHandler

Posted: Wed Apr 24, 2013 11:02 pm
by 22dec1960
Thanks phillipuniverse for your reply and it is ok now.

Cheers!
22dec1960