001/*
002 *  Copyright 2019 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.cms.data;
017
018import java.io.ByteArrayOutputStream;
019import java.io.IOException;
020
021import org.apache.commons.codec.digest.DigestUtils;
022import org.slf4j.Logger;
023import org.slf4j.LoggerFactory;
024
025import org.ametys.core.util.POIHolder;
026import org.ametys.core.util.POIHolder.Point;
027import org.ametys.plugins.repository.data.repositorydata.RepositoryData;
028
029/**
030 * Class representing a binary
031 */
032public class Binary extends NamedResource implements File, POIHolder
033{
034    /** The binary's logger */
035    protected static Logger __logger = LoggerFactory.getLogger(Binary.class);
036
037    /** the binary's hash */
038    protected String _hash;
039
040    /** The coordinates of the point of interest */
041    protected Point _poi;
042    
043    /**
044     * Default constructor
045     */
046    public Binary()
047    {
048        // Empty constructor
049    }
050    
051    /**
052     * Constructor to use when reading the binary from the repository
053     * @param repositoryData the repository data containing the resource's data
054     * @param hash the hash of the resource's data
055     */
056    public Binary(RepositoryData repositoryData, String hash)
057    {
058        super(repositoryData);
059        _hash = hash;
060    }
061    
062    @Override
063    protected void closeOutputStream(ByteArrayOutputStream outputStream) throws IOException
064    {
065        super.closeOutputStream(outputStream);
066        _hash = DigestUtils.sha1Hex(_buffer);
067    }
068
069    /**
070     * Retrieves the hash of the resource's data
071     * @return the hash of the resource's data
072     */
073    public String getHash()
074    {
075        return _hash;
076    }
077    
078    public String getName()
079    {
080        return getFilename();
081    }
082    
083    public Point getPOI()
084    {
085        return _poi;
086    }
087    
088    /**
089     * Sets the Point of Interest (POI) for this object.
090     * @param point the {@link Point} to set as the POI, may be {@code null} to remove it
091     */
092    public void setPOI(Point point)
093    {
094        _poi = point;
095    }
096}