308 lines
9.7 KiB
Java
308 lines
9.7 KiB
Java
/*
|
|
* Copyright (C) 2016 LibreCCM Foundation.
|
|
*
|
|
* This library is free software; you can redistribute it and/or
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
* License as published by the Free Software Foundation; either
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
*
|
|
* This library is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
* Lesser General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
* License along with this library; if not, write to the Free Software
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|
* MA 02110-1301 USA
|
|
*/
|
|
package org.librecms.assets;
|
|
|
|
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
|
|
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
|
import org.librecms.contentsection.Asset;
|
|
|
|
import java.io.Serializable;
|
|
import java.util.Objects;
|
|
|
|
import javax.activation.MimeType;
|
|
import javax.persistence.AssociationOverride;
|
|
import javax.persistence.Column;
|
|
import javax.persistence.Embedded;
|
|
import javax.persistence.Entity;
|
|
import javax.persistence.JoinColumn;
|
|
import javax.persistence.JoinTable;
|
|
import javax.persistence.Lob;
|
|
import javax.persistence.Table;
|
|
|
|
import org.hibernate.envers.Audited;
|
|
import org.hibernate.envers.NotAudited;
|
|
import org.libreccm.core.UnexpectedErrorException;
|
|
import org.libreccm.imexport.BlobJsonDeserializer;
|
|
import org.libreccm.imexport.BlobJsonSerializer;
|
|
import org.libreccm.jpa.utils.MimeTypeConverter;
|
|
import org.libreccm.l10n.LocalizedString;
|
|
import org.libreccm.imexport.MimeTypeJsonDeserializer;
|
|
import org.libreccm.imexport.MimeTypeJsonSerializer;
|
|
import org.librecms.contentsection.privileges.AssetPrivileges;
|
|
|
|
import java.io.InputStream;
|
|
import java.io.OutputStream;
|
|
import java.sql.Blob;
|
|
import java.sql.SQLException;
|
|
|
|
import javax.persistence.Basic;
|
|
import javax.persistence.Convert;
|
|
import javax.persistence.FetchType;
|
|
import javax.persistence.NamedQueries;
|
|
import javax.persistence.NamedQuery;
|
|
|
|
import static org.librecms.CmsConstants.*;
|
|
|
|
/**
|
|
* Base class for all assets storing binary information, like videos.
|
|
*
|
|
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
|
|
*/
|
|
@Entity
|
|
@Table(name = "BINARY_ASSETS", schema = DB_SCHEMA)
|
|
@Audited
|
|
@NamedQueries({
|
|
@NamedQuery(
|
|
name = "BinaryAsset.findById",
|
|
query = "SELECT DISTINCT a "
|
|
+ "FROM BinaryAsset a "
|
|
+ "LEFT JOIN a.permissions p "
|
|
+ "WHERE a.objectId = :assetId "
|
|
+ "AND ("
|
|
+ " ("
|
|
+ " p.grantee IN :roles "
|
|
+ " AND p.grantedPrivilege = "
|
|
+ " '" + AssetPrivileges.VIEW + "' "
|
|
+ " ) "
|
|
+ " OR true = :isSystemUser OR true = :isAdmin"
|
|
+ ")"
|
|
),
|
|
@NamedQuery(
|
|
name = "BinaryAsset.findByUuid",
|
|
query = "SELECT DISTINCT a "
|
|
+ "FROM BinaryAsset a "
|
|
+ "LEFT JOIN a.permissions p "
|
|
+ "WHERE a.uuid = :uuid "
|
|
+ "AND ("
|
|
+ " ("
|
|
+ " p.grantee IN :roles "
|
|
+ " AND p.grantedPrivilege = "
|
|
+ " '" + AssetPrivileges.VIEW + "' "
|
|
+ " ) "
|
|
+ " OR true = :isSystemUser OR true = :isAdmin"
|
|
+ ")"
|
|
),
|
|
@NamedQuery(
|
|
name = "BinaryAsset.findByContentSection",
|
|
query = "SELECT DISTINCT a "
|
|
+ "FROM BinaryAsset a "
|
|
+ "JOIN a.categories c "
|
|
+ "LEFT JOIN a.permissions p "
|
|
+ "WHERE c.category.section = :section "
|
|
+ "AND ("
|
|
+ " ("
|
|
+ " p.grantee IN :roles "
|
|
+ " AND p.grantedPrivilege = "
|
|
+ " '" + AssetPrivileges.VIEW + "' "
|
|
+ " ) "
|
|
+ " OR true = :isSystemUser OR true = :isAdmin"
|
|
+ ")"
|
|
),
|
|
@NamedQuery(
|
|
name = "BinaryAsset.findByNameInFolder",
|
|
query = "SELECT DISTINCT a "
|
|
+ "FROM BinaryAsset a "
|
|
+ "JOIN a.categories c "
|
|
+ "LEFT JOIN a.permissions p "
|
|
+ "WHERE c.category = :folder "
|
|
+ "AND c.type = '" + CATEGORIZATION_TYPE_FOLDER + "' "
|
|
+ "AND a.displayName = :name "
|
|
+ "AND ("
|
|
+ " ("
|
|
+ " p.grantee IN :roles "
|
|
+ " AND p.grantedPrivilege = '"
|
|
+ AssetPrivileges.VIEW + "'"
|
|
+ " ) "
|
|
+ " OR true = :isSystemUser OR true = :isAdmin"
|
|
+ " )"
|
|
)
|
|
})
|
|
public class BinaryAsset extends Asset implements Serializable {
|
|
|
|
private static final long serialVersionUID = -8540922051232103527L;
|
|
|
|
@Embedded
|
|
@AssociationOverride(
|
|
name = "values",
|
|
joinTable = @JoinTable(
|
|
name = "BINARY_ASSET_DESCRIPTIONS",
|
|
schema = DB_SCHEMA,
|
|
joinColumns = {
|
|
@JoinColumn(name = "ASSET_ID")
|
|
}
|
|
)
|
|
)
|
|
private LocalizedString description;
|
|
|
|
@Column(name = "FILENAME", length = 512, nullable = false)
|
|
private String fileName;
|
|
|
|
@Column(name = "MIME_TYPE", length = 512, nullable = false)
|
|
@Convert(converter = MimeTypeConverter.class)
|
|
@JsonSerialize(using = MimeTypeJsonSerializer.class)
|
|
@JsonDeserialize(using = MimeTypeJsonDeserializer.class)
|
|
private MimeType mimeType;
|
|
|
|
@Column(name = "ASSET_DATA")
|
|
@Lob
|
|
@Basic(fetch = FetchType.LAZY)
|
|
@NotAudited // Workaround until bug in Hibernate is resolved: https://hibernate.atlassian.net/browse/HHH-14725
|
|
@JsonSerialize(using = BlobJsonSerializer.class)
|
|
@JsonDeserialize(using = BlobJsonDeserializer.class)
|
|
private Blob data;
|
|
|
|
@Column(name = "DATA_SIZE")
|
|
private long size;
|
|
|
|
public BinaryAsset() {
|
|
super();
|
|
description = new LocalizedString();
|
|
//data = new byte[]{};
|
|
}
|
|
|
|
public LocalizedString getDescription() {
|
|
return description;
|
|
}
|
|
|
|
public void setDescription(final LocalizedString description) {
|
|
Objects.requireNonNull(description);
|
|
this.description = description;
|
|
}
|
|
|
|
public String getFileName() {
|
|
return fileName;
|
|
}
|
|
|
|
public void setFileName(final String fileName) {
|
|
this.fileName = fileName;
|
|
}
|
|
|
|
public MimeType getMimeType() {
|
|
return mimeType;
|
|
}
|
|
|
|
public void setMimeType(final MimeType mimeType) {
|
|
this.mimeType = mimeType;
|
|
}
|
|
|
|
public Blob getData() {
|
|
return data;
|
|
}
|
|
|
|
public long getDataSize() {
|
|
try {
|
|
return data.length();
|
|
} catch (SQLException ex) {
|
|
throw new UnexpectedErrorException(ex);
|
|
}
|
|
}
|
|
|
|
public InputStream getDataAsInputStream() {
|
|
try {
|
|
return data.getBinaryStream();
|
|
} catch (SQLException ex) {
|
|
throw new UnexpectedErrorException(ex);
|
|
}
|
|
}
|
|
|
|
public void setData(final Blob data) {
|
|
this.data = data;
|
|
}
|
|
|
|
public OutputStream getDataOutputStream() {
|
|
try {
|
|
return data.setBinaryStream(0);
|
|
} catch (SQLException ex) {
|
|
throw new UnexpectedErrorException(ex);
|
|
}
|
|
}
|
|
|
|
public long getSize() {
|
|
return size;
|
|
}
|
|
|
|
public void setSize(final long size) {
|
|
this.size = size;
|
|
}
|
|
|
|
@Override
|
|
public int hashCode() {
|
|
int hash = super.hashCode();
|
|
hash = 59 * hash + Objects.hashCode(description);
|
|
hash = 59 * hash + Objects.hashCode(fileName);
|
|
hash = 59 * hash + Objects.hashCode(mimeType);
|
|
hash = 59 * hash + Objects.hashCode(data);
|
|
hash = 59 * hash + (int) (size ^ (size >>> 32));
|
|
return hash;
|
|
}
|
|
|
|
@Override
|
|
public boolean equals(final Object obj) {
|
|
if (this == obj) {
|
|
return true;
|
|
}
|
|
if (obj == null) {
|
|
return false;
|
|
}
|
|
if (!super.equals(obj)) {
|
|
return false;
|
|
}
|
|
|
|
if (!(obj instanceof BinaryAsset)) {
|
|
return false;
|
|
}
|
|
final BinaryAsset other = (BinaryAsset) obj;
|
|
if (!(other.canEqual(this))) {
|
|
return false;
|
|
}
|
|
if (size != other.getSize()) {
|
|
return false;
|
|
}
|
|
if (!Objects.equals(fileName, other.getFileName())) {
|
|
return false;
|
|
}
|
|
if (!Objects.equals(description, other.getDescription())) {
|
|
return false;
|
|
}
|
|
if (!Objects.equals(mimeType, other.getMimeType())) {
|
|
return false;
|
|
}
|
|
return Objects.equals(data, other.getData());
|
|
}
|
|
|
|
@Override
|
|
public boolean canEqual(final Object obj) {
|
|
return obj instanceof BinaryAsset;
|
|
}
|
|
|
|
@Override
|
|
public String toString(final String data) {
|
|
return super.toString(String.format(", description = %s, "
|
|
+ "fileName = \"%s\", "
|
|
+ "mimeType = \"%s\", "
|
|
+ "size = %d%s",
|
|
Objects.toString(description),
|
|
fileName,
|
|
Objects.toString(mimeType),
|
|
size,
|
|
data));
|
|
}
|
|
|
|
}
|