Package com.orchestranetworks.addon.dama


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.

Examples of getting an asset's ID 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 ID from the Java bean object 'MediaType':

            String assetID = aMediaType.getAttachment();
    
  3. From the asset's ID, 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:

            Adaptation dataset;
            AdaptationTable table;
            Path mediapath;
            Session session;
    
            //Create API context
            DriveContext driveContext = new  DriveContext(dataset, table, mediapath, session);
    
  2. Create a DriveManager and upload new digital asset

            File fileToUpload = new File("theFile");
            DigitalAssetSpec digitalAssetSpec = new DigitalAssetSpec(new FileResource(fileToUpload));
            DigitalAsset digitalAsset = DriveFactoryProvider.getDriveFactory().getDriveManager(driveContext).create(digitalAssetSpec);
    
  3. Uploading assets or uploading and attach assets

            DigitalAssetComponentContext componentContext = new DigitalAssetComponentContext(
                    driveContext.getTable().lookupAdaptationByPrimaryKey(PrimaryKey.parseString(assetID),
                    driveContext.getPath(),
                    driveContext.getSession());
    
            DigitalAssetComponentServices componentServices = DriveFactoryProvider.getDriveFactory()
                            .getDigitalAssetComponentServices(componentContext);
    
            List<FileResource> fileResources = new ArrayList<FileResource>();
    
            fileResources.add(new FileResource(new File("file1")));
            fileResources.add(new FileResource(new File("file2")));
    
            //upload and attach
            OperationExecutionStatus operationExecutionStatus = componentServices
                            .upload(fileResources, Action.ATTACH);
            //upload only
            OperationExecutionStatus operationExecutionStatus = componentServices
                            .upload(fileResources, Action.NOTHING);
    
    
  4. Attach/Detach asset

            //After uploading, an object DigitalAsset is returned. You can get the digital asset primary key from this.
            String assetID = digitalAsset.getKey().getPrimaryKey().format();
    
            //there's a simple way to get the asset id, like that
            String assetID = digitalAsset.getAssetID();
    
            //get all asset in Drive
            DriveManager driveManager = DriveFactoryProvider.getDriveFactory()
                            .getDriveManager(driveContext);
            List<DigitalAsset> digitalAssets = driveManager.getDigitalAssets();
            List<DigitalAssetKey> digitalAssetKeys = new ArrayList<DigitalAssetKey>();
            for (DigitalAsset aDigitalAsset : digitalAssets)
            {
                    digitalAssetKeys.add(aDigitalAsset.getKey());
            }
    
            DigitalAssetComponentContext componentContext = new DigitalAssetComponentContext(
                    driveContext.getTable().lookupAdaptationByPrimaryKey(PrimaryKey.parseString(assetID),
                    driveContext.getPath(),
                    driveContext.getSession());
    
            DigitalAssetComponentServices componentServices = DriveFactoryProvider.getDriveFactory()
                            .getDigitalAssetComponentServices(componentContext);
    
            //attach
            OperationExecutionStatus operationExecutionStatus = componentServices
                            .attach(digitalAssetKeys);
            //detach
            OperationExecutionStatus operationExecutionStatus = componentServices
                            .detach(digitalAssetKeys);
    
    
  5. Getting details of an asset

            DigitalAssetKey digitalAssetKey = new DigitalAssetKey(aAssetPrimaryKey);//The asset primary key to get its details
            DigitalAsset digitalAsset = driveManager.getDigitalAsset(digitalAssetKey);
    
  6. Upload new version of an asset

            PrimaryKey assetPrimaryKey = PrimaryKey.parseString(assetID);
            DigitalAssetVersionSpec digitalAssetVersionSpec = new DigitalAssetVersionSpec(
                            new DigitalAssetKey(assetPrimaryKey),
                            new FileResource(new File("path/to/file")));
    
            //set the object attributes values
            digitalAssetVersionSpec.setVersionName("new version 1.1");
            digitalAssetVersionSpec.setComment("this is the asset new version");
            digitalAssetVersionSpec.setCurrentVersion(true);
    
            DigitalAssetVersion version = DriveFactoryProvider
                            .getDriveFactory()
                            .getDriveManager(driveContext)
                            .create(digitalAssetVersionSpec);
    
    
  7. Physically deleting asset

            DigitalAssetKey digitalAssetKey = new DigitalAssetKey(assetPrimaryKey);//The asset primary key to delete
            driveManager.delete(digitalAssetKey);
    
  8. Logically deleting asset

            OperationExecutionStatus operationExecutionStatusUpdate = DriveFactoryProvider
                            .getDriveFactory()
                            .getDriveManager(driveContext)
                            .update(version);
    
  9. Getting asset's content (download)

            DigitalAssetKey digitalAssetKey = new DigitalAssetKey(assetPrimaryKey);
            DigitalAsset digitalAsset = driveManager.getDigitalAsset(digitalAssetKey);
            DigitalAssetMediaContent assetContent = driveManager.getMediaContent(digitalAsset);
            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 assetId, String versionId, String extension)
            {
                    StringBuilder sb = new StringBuilder();
                    sb.append(assetId)
                            .append(APIConstants.UNDERSCORE)
                            .append(versionId)
                            .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);