Dalet Flex documentation has moved!
This page is no longer actively maintained. For the latest documentation, please visit us at our new support portal: https://support.dalet.com
def execute() {
def assetService = flexSdkClient.assetService
def assetPlaceholder = NewAssetPlaceholder.builder()
.type('media-asset')
.name('demo asset 1')
.build()
assetService.createAsset(assetPlaceholder)
}
We assume that the variant v1 exists and has a default metadata definition set for media assets.
def execute() {
def assetService = flexSdkClient.assetService
def assetPlaceholder = NewAssetPlaceholder.builder()
.type('media-asset')
.name('demo asset 1')
.variant('v1')
.build()
def asset = assetService.createAsset(assetPlaceholder)
def metadata = assetService.getAssetMetadata(asset.getId())
def field = metadata.getField('string-field')
field.setValue('Example String')
assetService.setAssetMetadata(asset.id, metadata)
}
def execute() {
def asset = context.asset
// Workflow variables
context.setWorkflowStringVariable('assetTitle', asset.name)
context.setWorkflowDateVariable('lastModified', asset.lastModified)
// Job variables
context.setJobStringVariable('referenceName', asset.referenceName)
context.setJobVariable('publishedDate', asset.publishedDate)
}
We assume that a metadata definition exists with ID 214, and that is has a searchable string field called actor-name.
def execute() {
def assetService = flexSdkClient.assetService
def metadata = 'actor-name:Bernard Reeves'
def query = new AssetApiQuery(
metadataDefinitionId: 214,
metadata: [metadata]
)
def results = assetService.getAssets(query)
context.logInfo("${results.totalCount} results")
}
In this more thorough example, this scripts looks up a film’s metadata from IMDB. We are assuming:
release-date and tag genre fieldsposter and its default metadata definition has string hostname and `path fieldsdef API_KEY = '**REDACTED**'
def BASE_URL = 'https://api.themoviedb.org/3'
GroovyScriptContext cxt
FlexSdkClient sdkClient
Object execute() {
cxt = (GroovyScriptContext) context
sdkClient = (FlexSdkClient) flexSdkClient
def asset = cxt.asset
def film = lookupFilmOnImdb(asset.name)
def filmDetails = lookupFilmDetailsOnImdb(film)
setFilmMetadata(asset, filmDetails)
createFilmImagePlaceholder(asset, filmDetails.poster_path)
}
def lookupFilmOnImdb(String query) {
def urlQuery = URLEncoder.encode(query, 'utf-8')
def data = get("$BASE_URL/search/movie?api_key=${API_KEY}&language=en-US&query=$urlQuery&page=1")
return data.get('results').get(0)
}
def lookupFilmDetailsOnImdb(movie) {
return get("$BASE_URL/movie/$movie.id?api_key=${API_KEY}&language=en-GB&append_to_response=release_dates")
}
def get(String url) {
def json = new URL(url).getText()
return new groovy.json.JsonSlurper().parseText(json)
}
def setFilmMetadata(Asset asset, filmDetails) {
def assetService = sdkClient.getAssetService()
def metadata = assetService.getAssetMetadata(asset)
def releaseDate = new java.text.SimpleDateFormat('yyyy-MM-dd').parse(filmDetails.release_date)
metadata.getField('release-date').setValue(releaseDate)
def genre = filmDetails.genres.get(0)
// Create a tag for the genre if one doesn't exist
def tagService = sdkClient.tagService
def tagCollection = tagService.getTagCollection('genre')
if (tagService.getTag(tagCollection, genre.name) == null) {
tagService.addTag(tagCollection, genre.name)
}
metadata.getField('genre').setValue(genre.name)
assetService.setAssetMetadata(asset, metadata)
}
Asset createFilmImagePlaceholder(Asset asset, String imagePath) {
def assetService = sdkClient.getAssetService()
def imageAssetPlaceholder = new NewAssetPlaceholder(
name: asset.name,
type: 'image-asset',
variant: 'poster',
workspaceId: cxt.job.workspace.id,
parentId: asset.getId()
)
def imageAsset = assetService.createAsset(imageAssetPlaceholder)
def metadata = assetService.getAssetMetadata(imageAsset)
metadata.getField('hostname').setValue('image.tmdb.org')
metadata.getField('path').setValue("/t/p/original${imagePath}" as String)
// If we fail to set the metadata, then we want to delete the image asset
try {
assetService.setAssetMetadata(imageAsset, metadata)
}
catch (Throwable t) {
assetService.destroy(imageAsset.id)
throw t
}
return imageAsset
}
Note
Notice that we roll back the creation of the child image asset if setting the metadata fails. This means that we know we will not have an image asset on the system that is incomplete. Manual transactional handling is covered in depth in Transactions within Scripting.
This script creates a new collection, set its sharing properties and then updates its metadata. We are assuming:
c1 exists and has a metadata definition named md1 assigned to itdef execute() {
def collectionService = flexSdkClient.collectionService
def variant = getVariant('c1')
def metadataDefinition = getMetadataDefinition('md1')
// Create collection
def collectionRequest = CreateCollectionRequest.builder()
.name('my-collection')
.variantId(variant.id)
.metadataDefinitionEntityId(metadataDefinition.id)
.build()
def collection = collectionService.createCollection(collectionRequest)
try {
// Set sharing permissions
def sharing = Sharing.builder()
.readAcl(Arrays.asList(2227L))
.build()
def updateCollection = UpdateCollection.builder()
.sharing(sharing)
.build()
collection = collectionService.updateCollection(collection, updateCollection)
// Update metadata
def metadata = collectionService.getCollectionMetadata(collection)
metadata.getField('new-string-1').setValue('my value')
collectionService.setCollectionMetadata(collection, metadata)
context.logInfo('Created and updated collection {}', collection.getUuid())
}
catch (Exception e) {
// Rollback in case post-creation updates fail
collectionService.deleteCollection(collection)
throw e
}
}
def getMetadataDefinition(String name) {
def definitionService = flexSdkClient.metadataDefinitionService
def definitions = definitionService.getMetadataDefinitions(MetadataDefinitionApiQuery.builder()
.name(name)
.build())
if (definitions.totalCount == 0) {
throw new RuntimeException("Metadata Definition ${name} does not exist")
}
return definitions.getMetadataDefinitions().get(0)
}
def getVariant(String name) {
def variantService = flexSdkClient.variantService
def variants = variantService.getVariants(VariantApiQuery.builder()
.name(name)
.exactNameMatch(true)
.build())
if (variants.totalCount == 0) {
throw new RuntimeException("Variant ${name} does not exist")
}
return variants.variants.get(0)
}