001/*
002 *  Copyright 2017 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.plugins.frontedition;
017
018import java.util.Map;
019
020import org.apache.commons.lang3.StringUtils;
021
022import org.ametys.cms.repository.Content;
023import org.ametys.plugins.explorer.resources.Resource;
024import org.ametys.plugins.repository.metadata.BinaryMetadata;
025
026/**
027 * Override of MetadataManager, just for front-edition, to get metadata as json for files.
028 */
029public class FileMetadataManager extends org.ametys.cms.contenttype.MetadataManager
030{
031    /** Avalon ROLE. */
032    @SuppressWarnings("hiding")
033    public static final String ROLE = FileMetadataManager.class.getName();
034
035    /**
036     * Get the JSON representation of a {@link Resource}
037     * @param resource The resource
038     * @return The resource as JSON
039     */
040    public Map<String, Object> readResource(Resource resource)
041    {
042        return _resourceAsJson(resource);
043    }
044
045    /**
046     * Get the JSON representation of a {@link BinaryMetadata}
047     * @param content The content holding the binary metadata
048     * @param binaryMetadata The resource
049     * @param metadataPath The path to the metadata
050     * @return The binary as JSON
051     */
052    public Map<String, Object> readBinaryMetadata(Content content, BinaryMetadata binaryMetadata, String metadataPath)
053    {
054        String prefix = metadataPath.contains("/") ? StringUtils.substringBeforeLast(metadataPath, "/") + "/" : "";
055        String metadataName = metadataPath.contains("/") ? StringUtils.substringAfterLast(metadataPath, "/") : metadataPath;
056        return _binaryAsJson(content, binaryMetadata, prefix, metadataName);
057    }
058}