Skip navigation links

Package com.orchestranetworks.addon.dama

Provides features that allow end-users to perform CRUD actions on digital assets and allows users to get the Edit asset service component.

See: Description

Package com.orchestranetworks.addon.dama Description

Provides features that allow end-users to perform CRUD actions on digital assets and allows users to get the Edit asset service component.

Examples of getting an asset's UUID from a field using the 'MediaType' field

  1. Get the value from the 'MediaType' field:

    The data model used in the following example has a table that contains a field with './Media' path, which is associated with {addon.label}'s 'MediaType' field:

            Adaptation aRecord = aTable.lookUpAdaptationByPrimaryKey(recordPrimaryKey);
            SchemaNode schemaNode = aRecord.getSchemaNode().getNode(Path.parse("./Media"));
            int maxOccurs = schemaNode.getMaxOccurs();
    
            List<MediaType> mediaTypes = new ArrayList<MediaType>();
            Object value = aRecord.get(Path.parse("./Media"));
            if (maxOccurs <= 1)
            {
                    mediaTypes.add((MediaType) value);
            }
            else
            {
                    mediaTypes.addAll((List<MediaType>) value);
            }
    
  2. Get the asset's UUID from the Java bean object 'MediaType':

            String assetUUID = aMediaType.getAttachment();
    
  3. From the asset's UUID, we use the add-on's API to perform operations on assets. Please see the next example for more details.

Examples of using a DriveManager

- The built-in DriveManger used in this example supports upload to the file system. You can use your own DriveManager implementation (See example below):

  1. To use the API, you need to define an API context with parameters:

            Session: current session
            Adaptation: current user dataset
            Path: field path using 'MediaType'
            BasedURL: a String that provide user based URL to append to the URL of asset.
            
            //Create API context
            Context context = new Context(session, dataset, mediaFieldPath, "http://www.abc.com");
    
  2. Create a DriveManager:

            DriveManager driveManager = DriveFactory.getDriveManager(aDriveType);
    
  3. Uploading new asset

            File file = new File("/home/ebx/file.png");
            DigitalAssetSpec digitalAssetSpec = new DigitalAssetSpec(file, label, description, context);
            DigitalAsset digitalAsset = driveManager.create(digitalAssetSpec);
    
  4. Attaching asset

            //After uploading, an object DigitalAsset is returned. You can get the digital asset primary key from this.
            String assetUUID = digitalAsset.getDigitalAssetContext().getDigitalAssetKey().getDigitalAssetPrimaryKey().format();
            //Create a new procedure to update new value to the MediaType field. 
            //Note: The Drive of new uploaded asset and the Drive of target 'MediaType' field should be the same.
            int maxOccurs = aNode.getMaxOccurs();
            ValueContextForUpdate contextForUpdate = pContext.getContext(aRecordName);
            if (maxOccurs <= 1) {
                    MediaType mediaType = new MediaType();
                    mediaType.setAttachment(assetUUID);
                    contextForUpdate.setValue(mediaType, aPath);
            }
            else
            {
                    List<MediaType> mediaTypes = new ArrayList<MediaType>();
                    MediaType mediaType = new MediaType();
                    mediaType.setAttachment(assetUUID);
                    mediaTypes.add(mediaType);
                    contextForUpdate.setValue(mediaTypes, aPath);
            }
            aProcedure.doModifyContent(aRecord, contextForUpdate);
    
  5. Detaching asset

            int maxOccurs = aNode.getMaxOccurs();
            ValueContextForUpdate contextForUpdate = pContext.getContext(aRecordName);
            if (maxOccurs <= 1){
                    MediaType mediaType = new MediaType();
                    mediaType. setAttachment("anAssetUUID");
                    contextForUpdate.setValue(mediaType, aPath);
            }
            else
            {
                    List<MediaType> mediaTypes = new ArrayList<MediaType>();
                    mediaTypes.add(aMediaType);
                    contextForUpdate.setValue(mediaTypes, aPath);
            }
            aProcedure.doModifyContent(aRecord, contextForUpdate);
    
  6. Getting details of an asset

            DigitalAssetKey digitalAssetKey = new DigitalAssetKey(aAssetPrimaryKey);//The asset primary key to get its details
            DigitalAssetContext digitalAssetContext = new DigitalAssetContext(digitalAssetKey,context);
            DigitalAsset digitalAsset = driveManager.getDigitalAsset(digitalAssetContext );
    
  7. Importing new version of an asset

       
            File file = new File("/home/ebx/newFile.png");
            
            DigitalAssetKey digitalAssetKey = new DigitalAssetKey(aAssetPrimaryKey);//The asset primary key to create new version
            DigitalAssetContext digitalAssetContext = new DigitalAssetContext(digitalAssetKey,context);
            DigitalAssetVersionSpec digitalAssetVersionSpec = new DigitalAssetVersionSpec(
                                                                    aDigitalAssetContext, 
                                                                    file, 
                                                                    "aBusinessName", 
                                                                    "aComment", 
                                                                    isCurrentVersion);//Determine if the new version is current version
            Version version = driveManager.create(digitalAssetVersionSpec);//Validate file and upload new version to current asset
    
  8. Physically deleting asset

            DigitalAssetKey digitalAssetKey = new DigitalAssetKey(aAssetPrimaryKey);//The asset primary key to delete
            DigitalAssetContext digitalAssetContext = new DigitalAssetContext(digitalAssetKey,context);
            driveManager.delete(digitalAssetContext);
    
  9. Logically deleting asset

            DigitalAssetUpdateContext updateContext = new DigitalAssetUpdateContext(anAssetContext, "aLabel", "aDescription", DigitalAssetState.DEACTIVATED);
            driveManager.update(digitalAssetUpdateContext);
    
  10. Getting asset's content (download)

            DigitalAssetKey digitalAssetKey = new DigitalAssetKey(aAssetPrimaryKey);
            DigitalAssetMediaContent assetContent = driveManager.getDigitalAssetMediaContent(digitalAssetKey);
            InputStream inputStream = assetContent.getInputStream();//Get inputstream from assetContent to download
    

Example of extending DriveManager:

  1. Implement DigitalAssetMediaContent:

    public class SampleDigitalAssetMediaContent implements DigitalAssetMediaContent
    {
            private final InputStream inputStream;
    
            private final long contentLength;
    
            public SampleDigitalAssetMediaContent(InputStream inputStream, long contentLength)
            {
                    this.inputStream = inputStream;
                    this.contentLength = contentLength;
            }
    
            public InputStream getInputStream()
            {
                    return this.inputStream;
            }
    
            public long getContentLength()
            {
                    return this.contentLength;
            }
    
            public void closeInputStream() throws DAMException
            {
                    if (this.inputStream != null)
                    {
                            try
                            {
                                    this.inputStream.close();
                            }
                            catch (IOException ex)
                            {
                                    throw new DAMException(ex);
                            }
                    }
            }
    }
    
  2. Implement SampleInternalIdentifierFactory:

    public final class SampleIdentifierFactory extends ResourceIdentifierFactory
    {
            public ResourceIdentifier parse(String identifier)
            {
                    return new SampleIdentifier(identifier);
            }
    }
    
  3. Implement SampleIdentifier:

    
    public class SampleIdentifier extends ResourceIdentifier
    {
            private final String filePath;
    
            public SampleIdentifier(String filePath)
            {
                    this.filePath = filePath;
            }
    
            public String format()
            {
                    return this.filePath;
            }
    }
    
  4. Implement StorageManager to store/delete/get physical file:

    public class SampleStorageManager extends StorageManager
    {
            public SampleStorageManager(Repository repository)
            {
                    super(repository);
            }
    
            public ResourceIdentifier uploadResource(
                    File uploadedFile,
                    ResourceIdentifier resourceIdentifier) throws DAMException
            {
                    File targetFile = new File(resourceIdentifier.format());
                    try
                    {
                            InputStream fileInputStream = new FileInputStream(uploadedFile);
                            OutputStream outputStream = new FileOutputStream(targetFile);
    
                            byte[] buffer = new byte[1024];
                            int c = 0;
    
                            try
                            {
                                    while ((c = fileInputStream.read(buffer, 0, buffer.length)) > 0)
                                    {
                                            outputStream.write(buffer, 0, c);
                                            outputStream.flush();
                                    }
                            }
                            finally
                            {
                                    outputStream.close();
                                    fileInputStream.close();
                            }
                            return this.getIdentifierFactory().parse(uploadedFile.getName());
                    }
                    catch (IOException ioException)
                    {
                            //Log the exception
                            throw new DAMException("Unable to save file in folder");
                    }
    
            }
    
            public boolean deleteResource(ResourceIdentifier identifier) throws DAMException
            {
                    File file = new File(identifier.format());
                    return file.delete();
            }
            
            private Adaptation getDAMConfigurationDataset(Repository repository)
            {
                    AdaptationHome damHome = repository.lookupHome(APIConstants.CONFIGURATION.DAM_HOME);
                    return damHome.findAdaptationOrNull(APIConstants.CONFIGURATION.DAM_DATASET_NAME);
            }
            
            private String buildFileName(String uuidAsset, String uuidVersion, String extension)
            {
                    StringBuilder sb = new StringBuilder();
                    sb.append(uuidAsset)
                            .append(APIConstants.UNDERSCORE)
                            .append(uuidVersion)
                            .append(APIConstants.DOT)
                            .append(extension);
                    return sb.toString();
            }
    
            public DigitalAssetMediaContent getDigitalAssetMediaContent(DigitalAssetKey digitalAssetKey)
                    throws DAMException
            {
                    Adaptation digitalAssetRecord = this.getDAMConfigurationDataset(this.repository)
                            .getTable(DrivePaths._DigitalAssetGroup_DigitalAsset.getPathInSchema())
                            .lookupAdaptationByPrimaryKey(digitalAssetKey.getDigitalAssetPrimaryKey());
    
                    String fileName = this.buildFileName(
                            digitalAssetRecord.getString(DrivePaths._DigitalAssetGroup_DigitalAsset._Uuid),
                            digitalAssetRecord
                                    .getString(DrivePaths._DigitalAssetGroup_DigitalAsset._FKCurrentVersion),
                            digitalAssetRecord.getString(DrivePaths._DigitalAssetGroup_DigitalAsset._FKMimeType));
                    File physicalFile = new File(fileName);
    
                    return this.getDigitalAssetMediaContentFromFile(physicalFile);
            }
    
            public DigitalAssetMediaContent getDigitalAssetMediaContent(DigitalAssetVersionKey versionKey)
                    throws DAMException
            {
                    Adaptation versionRecord = this.getDAMConfigurationDataset(this.repository)
                            .getTable(DrivePaths._DigitalAssetGroup_DigitalAssetVersion.getPathInSchema())
                            .lookupAdaptationByPrimaryKey(versionKey.getPrimaryKey());
    
                    String fileName = this.buildFileName(
                            versionRecord.getString(DrivePaths._DigitalAssetGroup_DigitalAsset._Uuid),
                            versionRecord.getString(DrivePaths._DigitalAssetGroup_DigitalAsset._FKCurrentVersion),
                            versionRecord.getString(DrivePaths._DigitalAssetGroup_DigitalAsset._FKMimeType));
    
                    File physicalFile = new File(fileName);
                    return this.getDigitalAssetMediaContentFromFile(physicalFile);
            }
    
            private DigitalAssetMediaContent getDigitalAssetMediaContentFromFile(File physicalFile)
                    throws DAMException
            {
                    if (!physicalFile.exists())
                    {
                            throw new DAMException("File not found");
                    }
                    long contentLength = physicalFile.length();
                    InputStream inputStream;
                    DigitalAssetMediaContent digitalAssetMediaContent;
                    try
                    {
                            inputStream = new FileInputStream(physicalFile);
                            digitalAssetMediaContent = new DigitalAssetMediaContentImpl(inputStream, contentLength);
                    }
                    catch (Exception ex)
                    {
                            throw new DAMException(ex);
                    }
                    return digitalAssetMediaContent;
            }
    
            public ResourceIdentifierFactory getIdentifierFactory()
            {
                    return new SampleIdentifierFactory();
            }
    }
    
  5. Implement DriveManager:

    public class SampleDriveManager extends DriveManager
    {
            public SampleDriveManager(Repository repository)
            {
                    super(repository);
            }
    
            public StorageManager getStorageManager(Repository repository)
            {
                    return new SampleStorageManager(repository);
            }
    }
    

Example of getting UIHttpManagerComponent of the 'Edit digital asset' service:

  1. Create a new EditAssetServiceSpec:

            DigitalAssetKey digitalAssetKey = new DigitalAssetKey(anAssetPrimaryKey);
            Path mediaFieldPath = Path.parse("/root/aTable/aMediaField");
            boolean isReadonly = false;
            EditAssetServiceSpec editAssetServiceSpec = new EditAssetServiceSpec(digitalAssetKey, aDataset, mediaFieldPath, isReadonly);
    
  2. Get UIHttpManagerComponent of the 'Edit digital asset' service:

            UIHttpManagerComponent editServiceComponent = EditAssetServiceComponent.getEditDigitalAssetService(editAssetServiceSpec); 
    
Skip navigation links

Add-ons Version 4.5.9.

Copyright TIBCO Software Inc. 2001-2022. All rights reserved.
All third party product and company names and third party marks mentioned in this document are the property of their respective owners and are mentioned for identification.