Package com.orchestranetworks.addon.dama.ext


package com.orchestranetworks.addon.dama.ext

Provides the extension API.

Performing actions on drives and media type fields

  1. Performing actions on media fields:

    1. Get an instance of DigitalAssetComponentServices:

              //Provide component context with the information referring to a specific digital asset component
              DigitalAssetComponentContext componentContext = new DigitalAssetComponentContext(
                      aUserRecord,
                      aMediaPath,
                      aSession);
      
              DriveFactory driveFactory = DriveFactoryProvider.getDriveFactory();
      
              //Get instance of DigitalAssetComponentServices from 'componentContext'
              DigitalAssetComponentServices componentController = driveFactory
                      .getDigitalAssetComponentServices(componentContext);
      
    2. Upload, attach, detach assets and get digital asset keys attached in a media field:

                                                      
              //Upload and attach asset to records
              File uploadFile = new File("sampleFile.png");
              FileResource fileResource = new FileResource(uploadFile);
              List<FileResource> fileResources = new ArrayList<FileResource>();
              fileResources.add(fileResource);
              componentController.upload(fileResources, Action.ATTACH);
      
              //Attach digital asset to the media field of a record
              DigitalAssetKey assetKey = new DigitalAssetKey(PrimaryKey.parseString("assetKey"));
              List<DigitalAssetKey> assetKeys = new ArrayList<DigitalAssetKey>();
              assetKeys.add(assetKey);
              componentController.attach(assetKeys);
      
              //Detach digital asset
              componentController.detach(assetKeys);
              
              //Get the digital asset keys attached in the media field of a user record
              componentController.getDigitalAssetKeys();
      
  2. Performing actions on drives:

    1. Get instance of DriveManager:

              //Provide DriveContext with the information referring to a specific drive.
              DriveContext driveContext = new DriveContext(
                      this.userDataset,
                      this.table,
                      this.mediaPath,
                      this.session);
      
              DriveFactory driveFactory = DriveFactoryProvider.getDriveFactory();
              DriveManager driveManager = driveFactory.getDriveManager(driveContext);
      
    2. Actions: create, update, delete and download digital assets:

              //Create a new DigitalAsset
              File uploadFile = new File("sampleFile.png");
              FileResource fileResource = new FileResource(uploadFile);
              DigitalAssetSpec digitalAssetSpec = new DigitalAssetSpec(fileResource);
              digitalAssetSpec.setLabel(UserMessage.createInfo("new label"));
              digitalAssetSpec.setDescription(UserMessage.createInfo("new description"));
              DigitalAsset newAsset = driveManager.create(digitalAssetSpec);
      
              //Update a digital asset
              newAsset.setState(DigitalAssetState.DEACTIVATED);
              newAsset.setLabel(UserMessage.createInfo("updated label"));
              newAsset.setDescription(UserMessage.createInfo("updated label"));
              driveManager.update(newAsset);
      
              //Delete a digital asset
              driveManager.delete(newAsset.getKey());
      
              //Download asset - current version of asset
              MediaContent mediaContent = driveManager.getMediaContent(newAsset);
              mediaContent.getInputStream();
              mediaContent.getContentLength();
      
    3. Actions: create, update, delete and download digital asset versions:

              //Create a new version for a digital asset
              DigitalAssetKey assetKey = new DigitalAssetKey(PrimaryKey.parseString("assetKey"));
              File uploadFile = new File("sampleFile.png");
              FileResource fileResource = new FileResource(uploadFile);
              DigitalAssetVersionSpec versionSpec = new DigitalAssetVersionSpec(assetKey, fileResource);
              versionSpec.setCurrentVersion(true);
              versionSpec.setComment("comment");
              versionSpec.setVersionName("versionName");
              DigitalAssetVersion newVersion = driveManager.create(versionSpec);
      
              //Update a version
              newVersion.setComment("new comment");
              newVersion.setVersionName("new version name");
              driveManager.update(newVersion);
      
              //Delete a version
              driveManager.delete(newVersion.getKey());
      
              //Download version
              MediaContent mediaContent = driveManager.getMediaContent(newVersion);
              mediaContent.getInputStream();
              mediaContent.getContentLength();
      
    4. Browsing digital assets in a drive:

              //Create a new instance of SearchFilter to search digital asset
              SearchFilter searchFilter = new SearchFilter();
              searchFilter.setKeyword("anAssetName");
              searchFilter.setExtension("png");
              searchFilter.setType("image");
      
              //Filter by creation and updated DateRange
              DateRange creationDateRange = new DateRange();
              DateRange updatedDateRange = new DateRange();
              searchFilter.setCreationDateRange(creationDateRange);
              searchFilter.setUpdateDateRange(updatedDateRange);
      
              //Filter by tags
              List<String> tags = new ArrayList<String>();
              tags.add("tag");
              searchFilter.setTags(tags);
      
              //Result of filter will be sorted by SortBy
              SortCriteria creationDateSort = new SortCriteria(SortBy.CREATION_DATE, true);
              SortCriteria physicalNameSort = new SortCriteria(SortBy.PHYSICAL_NAME, true);
              List<SortCriteria> sortCriterias = new ArrayList<SortCriteria>();
              sortCriterias.add(physicalNameSort);
              sortCriterias.add(creationDateSort);
              searchFilter.setSortCriterias(sortCriterias);
      
              //Search digital asset by 'searchFilter'
              List<DigitalAsset> digitalAssets = driveManager.search(searchFilter);
      
              //Get all active digital assets in Drive
              List<DigitalAsset> digitalAssetsInDrive = driveManager.getDigitalAssets();
      
              //Look up asset by DigitalAssetKey
              DigitalAssetKey assetKey = new DigitalAssetKey(PrimaryKey.parseString("assetKey"));
              DigitalAsset digitalAsset = driveManager.getDigitalAsset(assetKey);
      

Implementing a new storage manager of a drive type - Google drive

  1. Implement ResourceIdentifier:

    public final class GoogleResouceIdentifier implements ResourceIdentifier
    {
            private final String folderID;
    
            public GoogleResouceIdentifier(String folderID)
            {
                    this.folderID = folderID;
            }
    
            public String format()
            {
                    return this.folderID;
            }
    
    }
    
  2. Implement a ResourceIdentifierFactory

    public final class GoogleIdentifierFactory implements ResourceIdentifierFactory
    {
            public ResourceIdentifier parse(String identifier)
            {
                    return new GoogleResouceIdentifier(identifier);
            }
    }
    
  3. Implement a StorageManager

    public final class GoogleDriveStorageManager extends StorageManager
    {
            public ResourceIdentifier upload(Resource resource, ResourceIdentifier target)
            {
                    try
                    {
                            return this.upload(resource, target.format());
                    }
                    catch (Exception ex)
                    {
                            TestLogger.getLoggingCategory().error("Upload failed", ex);
                            return null;
                    }
            }
    
            public boolean delete(ResourceIdentifier target)
            {
                    try
                    {
                            Drive service = GoogleDriveAPIHelper.getInstance().getDriveService();
                            service.files().delete(target.format()).execute();
                            return true;
                    }
                    catch (Exception ex)
                    {
                            TestLogger.getLoggingCategory().error("Delete failed", ex);
                            return false;
                    }
            }
    
            public MediaContent getMediaContent(DigitalAsset asset)
            {
                    Drive service;
                    try
                    {
                            service = GoogleDriveAPIHelper.getInstance().getDriveService();
                            HttpResponse httpResponse = service.files().get(asset.getExternalID()).executeMedia();
                            return new MediaContentImpl(
                                    httpResponse.getContent(),
                                    httpResponse.getHeaders().getContentLength().longValue());
                    }
                    catch (Exception ex)
                    {
                            TestLogger.getLoggingCategory().error("Failed", ex);
                            return null;
                    }
            }
    
            public MediaContent getMediaContent(DigitalAssetVersion version)
            {
                    Drive service;
                    try
                    {
                            service = GoogleDriveAPIHelper.getInstance().getDriveService();
                            HttpResponse httpResponse = service.files().get(version.getExternalId()).executeMedia();
                            return new MediaContentImpl(
                                    httpResponse.getContent(),
                                    httpResponse.getHeaders().getContentLength().longValue());
                    }
                    catch (Exception ex)
                    {
                            TestLogger.getLoggingCategory().error("Failed", ex);
                            return null;
                    }
            }
    
            public ResourceIdentifierFactory getIdentifierFactory()
            {
                    return new GoogleIdentifierFactory();
            }
    
            private ResourceIdentifier upload(Resource resource, String folderID) throws Exception
            {
                    //Build a new authorized API client service.
                    Drive service = GoogleDriveAPIHelper.getInstance().getDriveService();
                    File googleFile = new File();
                    googleFile.setName(resource.getFile().getName());
                    //gooleFile.setParents(Collections.singletonList(folderID));
                    FileContent mediaContent = new FileContent(
                            this.identifyFileTypeUsingMimetypesFileTypeMap(resource.getFile().getName()),
                            resource.getFile());
                    File uploadedFile = service.files()
                            .create(gooleFile, mediaContent)
                            .setFields("id")
                            .execute();
    
                    ResourceIdentifierFactory googleIdentifierFactory = this.getIdentifierFactory();
                    return googleIdentifierFactory.parse(uploadedFile.getId());
            }
    
            private String identifyFileTypeUsingMimetypesFileTypeMap(String fileName)
            {
                    return new MimetypesFileTypeMap().getContentType(fileName);
            }
    }
    
  4. Register new StorageManager for the GoogleDrive DriveType

            //get DriveFactory
            DriveFactory driveFactory = DriveFactoryProvider.getDriveFactory();
            
            //Instance new DriveType and new GoogleDriveStorageManager
            DriveType googleDriveType = new DriveType("GoogleDrive");
            StorageManager googleDriveStorageManager = new GoogleDriveStorageManager();
            
            //register google drive storage manager with the google drive type
            driveFactory.registerStorageManager(googleDriveType, googleDriveStorageManager);
            
            //get storageManager by driveType
            driveFactory.getStorageManager(googleDriveType);
    
  5. To perform actions with the new GoogleDriveStorageManager implementation. Please follow the steps below:
    1. Log in as Administrator and navigate to the {addon.label.full} dataset.
    2. Open the Drive type table and create a new record. In the Code field, select GoogleDrive and create a new drive type.
    3. Open the Drive table and create a new record. Input new data for the new drive and choose GoogleDrive as the Drive type of the drive.
    4. Provide any required information for the drive or the digital asset component referring to the drive as the context to get the DriveManager or DigitalAssetComponentServices. Please refer to the Performing actions on drives and media type fields section.

Performing backup and restore operations on the {addon.label.full} dataset

  1. Backing up the {addon.label.full} dataset:

            //Use BackupRestoreFactory to get an instance of BackupService
            BackupService backupService = BackupRestoreFactory.getBackupService();
            
            //Create a new instance of BackupSpec with the name of the backup file, folder for the backup file and EBX® session.
            BackupSpec backupSpec = new BackupSpec(aSession, "aFileName", aFolder);
            
            //Backup the {addon.label.full} dataset and return a File as the backup File.
            File backupFile = backupService.backup(backupSpec);
    
  2. Restoring a digital asset to a specified location:

            //Use BackupRestoreFactory to get an instance of RestoreService
            RestoreService restoreService = BackupRestoreFactory.getRestoreService();
            
            //Create a new instance of RestoreSpec with the location of the backup file and EBX® session
            RestoreSpec restoreSpec = new RestoreSpec(backupFile, aSession);
            
            //Get an instance of RestoreDetail
            RestoreDetail restoreDetail = restoreService.getRestoreDetails(restoreSpec);
            
            //Get drives information and input new location for drives
            List<DriveInformation> driveInformations = restoreDetail.getDriveInformations();
            driveInformations.get(0).setNewLocation(aString);
            
            //Restore the {addon.label.full} dataset with 'restoreDetail'
            //OperationExecutionStatus will be returned as the result of the execution.
            OperationExecutionStatus status = restoreService.restore(restoreDetail);
    

Adding a custom filter

  1. Implement an EditorFilter:

    public class SampleEditorFilter implements EditorFilter
    {
            public boolean accept(EditorFilterContext editorFilterContext)
            {
                    try
                    {
                            //Get the default directory. If you don't use the EBX® default directory, use DirectoryHandler or your own implementation of custom directory.
                            DirectoryDefault directoryDefault = DirectoryDefault
                                    .getInstance(Repository.getDefault());
    
                            //Get the author of the digital asset being evaluated
                            String profileAuthor = editorFilterContext.getDigitalAsset()
                                    .getCurrentVersion()
                                    .getProfileAuthor();
                            UserReference author = (UserReference) UserReference.parse(profileAuthor);
                            
                            //Get the roles of the author
                            List<Role> roles = directoryDefault.getRolesForUser(author);
    
                            //Get the current user
                            UserReference userReference = editorFilterContext.getSession().getUserReference();
                            for (Role role : roles)
                            {
                                    //Check whether the current user and author have the same role
                                    if (directoryDefault.isUserInRole(userReference, role))
                                    {
                                            return true;
                                    }
                            }
                    }
                    catch (Exception ex)
                    {
                            //Handle any exceptions
                    }
                    return false;
            }
    }