diff --git a/magnolia-dam-templating/src/main/java/info/magnolia/dam/templating/functions/DamTemplatingFunctions.java b/magnolia-dam-templating/src/main/java/info/magnolia/dam/templating/functions/DamTemplatingFunctions.java new file mode 100644 index 0000000..d3e7a17 --- /dev/null +++ b/magnolia-dam-templating/src/main/java/info/magnolia/dam/templating/functions/DamTemplatingFunctions.java @@ -0,0 +1,151 @@ +/** + * This file Copyright (c) 2014 Magnolia International + * Ltd. (http://www.magnolia-cms.com). All rights reserved. + * + * + * This file is dual-licensed under both the Magnolia + * Network Agreement and the GNU General Public License. + * You may elect to use one or the other of these licenses. + * + * This file is distributed in the hope that it will be + * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the + * implied warranty of MERCHANTABILITY or FITNESS FOR A + * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT. + * Redistribution, except as permitted by whichever of the GPL + * or MNA you select, is prohibited. + * + * 1. For the GPL license (GPL), you can redistribute and/or + * modify this file under the terms of the GNU General + * Public License, Version 3, as published by the Free Software + * Foundation. You should have received a copy of the GNU + * General Public License, Version 3 along with this program; + * if not, write to the Free Software Foundation, Inc., 51 + * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * 2. For the Magnolia Network Agreement (MNA), this file + * and the accompanying materials are made available under the + * terms of the MNA which accompanies this distribution, and + * is available at http://www.magnolia-cms.com/mna.html + * + * Any modifications to this file must keep this entire header + * intact. + * + */ +package info.magnolia.dam.templating.functions; + +import info.magnolia.dam.api.Asset; +import info.magnolia.dam.api.AssetProviderRegistry; +import info.magnolia.dam.api.AssetQuery; +import info.magnolia.dam.api.AssetRenderer; +import info.magnolia.dam.api.AssetRendition; +import info.magnolia.dam.api.Folder; +import info.magnolia.dam.api.Item; +import info.magnolia.dam.templating.model.AssetMap; + +import java.util.Iterator; + +import javax.inject.Inject; +import javax.inject.Singleton; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.net.MediaType; + +/** + * Dam templating function exposed in FTL's as "damfn". This class exposed + * useful methods for FTL's and Model Class. + */ +@Singleton +public class DamTemplatingFunctions { + + private static final Logger log = LoggerFactory.getLogger(DamTemplatingFunctions.class); + + private final AssetProviderRegistry providerRegistry; + + @Inject + public DamTemplatingFunctions(AssetProviderRegistry providerRegistry) { + this.providerRegistry = providerRegistry; + } + + /** + * @param itemKey {@link ItemKey#asString()}. + * @return null if asset was not found. + */ + public Asset getAsset(String itemKey) { + return null; + } + + /** + * @param providerId + * @param itemPath relative path to the Asset. + * @return null if asset was not found. + * @throws IllegalArgumentException if the requested provider is not an implementation of {@link PathAwareAssetProvider}. + */ + public Asset getAsset(String providerId, String itemPath) { + return null; + } + + /** + * @param itemKey {@link ItemKey#asString()}. + * @return null if folder was not found. + */ + public Folder getFolder(String itemKey) { + return null; + } + + /** + * @param providerId + * @param itemPath relative path to the Folder. + * @return null if folder was not found. + * @throws IllegalArgumentException if the requested provider is not an implementation of {@link PathAwareAssetProvider}. + */ + public Folder getFolder(String providerId, String itemPath) { + return null; + } + + /** + * Set {@link AssetQuery#includesFolders()} or {@link AssetQuery#includesAssets()} in order to restrict the returned Items. + */ + public Iterator getItems(String providerId, AssetQuery query) { + return null; + } + + // TODO? + public Iterator getItems(AssetQuery query) { + // loop to all providers --> perform the query ? + return null; + } + + public AssetRendition getRendition(Asset asset, MediaType mediaType, String renditionName) { + AssetRenderer renderer = providerRegistry.getRendererFor(asset, mediaType); + if (renderer.canRender(asset, mediaType)) { + return renderer.render(asset, mediaType, renditionName); + } + return null; + } + + // TODO SHOULD WE ?? + public AssetRendition getRendition(Asset asset, String renditionName) { + return getRendition(asset, MediaType.parse(asset.getMimeType()), renditionName); + } + + public AssetRendition getRendition(String itemKey, MediaType mediaType, String renditionName) { + return getRendition(getAsset(itemKey), mediaType, renditionName); + } + + public boolean provides(String providerId, MediaType mediaType) { + return false; + } + + // TODO ? + public AssetMap getAssetMap(Asset asset) { + // HOWTO ?? + // ? from the asset, get all metadata, and copy the props + return null; + } + + public AssetMap getAssetMap(String itemKey) { + return getAssetMap(getAsset(itemKey)); + } +}