mark göricke it consulting

freelancer

Update a Flex Attribute using Asset API

The following code loads a Flex Asset with the AssetId assetId and updates the value of a Flex Attribute attribute with the object value. The value of value ;-) can be null. The code can be used in Java classes as well as in JSP Templates.

A hint: If the Flex Attribute is of type blob and the value is null, the AssetFrameWork will also delete the file, that contains the blob data.

 
 import com.fatwire.assetapi.data.AssetData;
 import com.fatwire.assetapi.data.AssetDataManager;
 import com.fatwire.assetapi.data.AssetId;
 import com.fatwire.assetapi.data.AttributeData;
 import com.fatwire.assetapi.data.BlobObject;
 import com.fatwire.system.Session;
 import com.fatwire.system.SessionFactory;
 import com.fatwire.assetapi.common.AssetAccessException;
 import com.fatwire.assetapi.common.AssetNotExistException;
// you will need to create a new session, when you run this code in an AssetListener!
 String UPDATE_USER      = "aUser";
 String UPDATE_PASSWD   = "aUsersPassword";
 /**
 *
 */
 public void updateAttribute(AssetId assetId, String attribute, Object value){
Session ses = SessionFactory.newSession(UPDATE_USER, UPDATE_PASSWD);
 AssetDataManager mgr = (AssetDataManager)session.getManager(AssetDataManager.class.getName());
Object oldValue = null;
Iterable<AssetData> assets;
 try {
 assets = mgr.read(Arrays.asList(assetId));
 java.util.List<AssetData> updateData = new ArrayList<AssetData>();
 for(AssetData asset : assets){
// set to null or to new value
 if(value==null || !value.equals(asset.getAttributeData(attribute).getData())){
updateData.add(asset);
 oldValue = asset.getAttributeData(attribute).getData();
 asset.getAttributeData(attribute).setData(value);
 }
 }
 if(updateData.size()>0){
 mgr.update(updateData);
 LOGGER.debug("updateAttribute.updated: " + assetId + ", " + attribute + "=" + value);
 LOGGER.debug("updateAttribute.updated: attribute=" + attribute + ", old=" + oldValue + ", new=" + value);
 }
 } catch (AssetNotExistException e1) {
 LOGGER.error("updateAttribute", e1);
 } catch (AssetAccessException e1) {
 LOGGER.error("updateAttribute", e1);
 }
 }

Feel free to contact me, if you need more information or just leave a comment …

, , , , , , , ,