Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Internal/content preview #691

Merged
merged 13 commits into from
Aug 7, 2024
155 changes: 155 additions & 0 deletions dspace-api/src/main/java/org/dspace/content/PreviewContent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
/**
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
*
* http://www.dspace.org/license/
*/
package org.dspace.content;

import java.util.Hashtable;
import java.util.Map;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToOne;
import javax.persistence.MapKeyColumn;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

import org.dspace.core.Context;
import org.dspace.core.ReloadableEntity;

/**
* Database entity representation of the previewcontent table.
* It is not possible to create entity from FileInfo class (without modifications)
* so we created PreviewContent (which serves as an entity for FileInfo)
* with corresponding database table previewcontent.
*
* @author Michaela Paurikova (dspace at dataquest.sk)
*/
@Entity
@Table(name = "previewcontent")
public class PreviewContent implements ReloadableEntity<Integer> {

@Id
@Column(name = "previewcontent_id")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "previewcontent_previewcontent_id_seq")
@SequenceGenerator(name = "previewcontent_previewcontent_id_seq",
sequenceName = "previewcontent_previewcontent_id_seq", allocationSize = 1)
private Integer id;

@ManyToOne(fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST})
@JoinColumn(name = "bitstream_id")
private Bitstream bitstream;

@Column(name = "name")
public String name;

@Column(name = "content")
public String content;

@Column(name = "isDirectory")
public boolean isDirectory;

@Column(name = "size")
public String size;

@OneToMany(cascade = CascadeType.ALL)
@JoinTable(
name = "preview2preview",
joinColumns = @JoinColumn(name = "parent_id"),
inverseJoinColumns = @JoinColumn(name = "child_id")
)
@MapKeyColumn(name = "name")
public Map<String, PreviewContent> sub = new Hashtable<>();

/**
* Protected constructor.
*/
protected PreviewContent() {}

/**
* Protected constructor, create object using:
* {@link org.dspace.content.service.PreviewContentService#create(Context, PreviewContent)}
*/
protected PreviewContent(PreviewContent previewContent) {
this.bitstream = previewContent.getBitstream();
this.name = previewContent.getName();
this.content = previewContent.getContent();
this.isDirectory = previewContent.isDirectory();
this.size = previewContent.getSize();
this.sub = previewContent.getSubPreviewContents();
}

/**
* Protected constructor, create object using:
* {@link org.dspace.content.service.PreviewContentService#create(Context, Bitstream, String, String, boolean,
* String, Map<String, PreviewContent>)}
*/
protected PreviewContent(Bitstream bitstream, String name, String content, boolean isDirectory, String size,
Map<String, PreviewContent> subPreviewContents) {
this.bitstream = bitstream;
this.name = name;
this.content = content;
this.isDirectory = isDirectory;
this.size = size;
this.sub = subPreviewContents;
}

@Override
public Integer getID() {
return id;
}

public Bitstream getBitstream() {
return bitstream;
}

public void setBitstream(Bitstream bitstream) {
this.bitstream = bitstream;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getContent() {
return content;
}

public void setContent(String content) {
this.content = content;
}

public boolean isDirectory() {
return isDirectory;
}

public void setDirectory(boolean directory) {
isDirectory = directory;
}

public String getSize() {
return size;
}

public void setSize(String size) {
this.size = size;
}

public Map<String, PreviewContent> getSubPreviewContents() {
return sub;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/**
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
*
* http://www.dspace.org/license/
*/
package org.dspace.content;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.dspace.authorize.AuthorizeException;
import org.dspace.authorize.service.AuthorizeService;
import org.dspace.content.dao.PreviewContentDAO;
import org.dspace.content.service.PreviewContentService;
import org.dspace.core.Context;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

/**
* Service implementation for the PreviewContent object.
*
* @author Michaela Paurikova (dspace at dataquest.sk)
*/
public class PreviewContentServiceImpl implements PreviewContentService {

/**
* logger
*/
private static final Logger log = LoggerFactory.getLogger(PreviewContentServiceImpl.class);


@Autowired
PreviewContentDAO previewContentDAO;
@Autowired(required = true)
AuthorizeService authorizeService;

@Override
public PreviewContent create(Context context, Bitstream bitstream, String name, String content,
boolean isDirectory, String size, Map<String, PreviewContent> subPreviewContents)
throws SQLException {
//no authorization required!
// Create a table row
PreviewContent previewContent = previewContentDAO.create(context, new PreviewContent(bitstream, name, content,
isDirectory, size, subPreviewContents));
log.info("Created new preview content of ID = {}", previewContent.getID());
return previewContent;
}

@Override
public PreviewContent create(Context context, PreviewContent previewContent) throws SQLException {
//no authorization required!
PreviewContent newPreviewContent = previewContentDAO.create(context, new PreviewContent(previewContent));
log.info("Created new preview content of ID = {}", newPreviewContent.getID());
return newPreviewContent;
}

@Override
public void delete(Context context, PreviewContent previewContent) throws SQLException, AuthorizeException {
if (!authorizeService.isAdmin(context)) {
throw new AuthorizeException(
"You must be an admin to delete an CLARIN Content Preview");
}
previewContentDAO.delete(context, previewContent);
}

@Override
public PreviewContent find(Context context, int valueId) throws SQLException {
return previewContentDAO.findByID(context, PreviewContent.class, valueId);
}

@Override
public List<PreviewContent> findByBitstream(Context context, UUID bitstreamId) throws SQLException {
return previewContentDAO.findByBitstream(context, bitstreamId);
}

@Override
public List<PreviewContent> findRootByBitstream(Context context, UUID bitstreamId) throws SQLException {
return previewContentDAO.findRootByBitstream(context, bitstreamId);
}

@Override
public List<PreviewContent> findAll(Context context) throws SQLException {
return previewContentDAO.findAll(context, PreviewContent.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
*
* http://www.dspace.org/license/
*/
package org.dspace.content.dao;

import java.sql.SQLException;
import java.util.List;
import java.util.UUID;

import org.dspace.content.PreviewContent;
import org.dspace.core.Context;
import org.dspace.core.GenericDAO;

/**
* Database Access Object interface class for the PreviewContent object.
* This class should only be accessed from a single service and should never be exposed outside of the API
*
* @author Michaela Paurikova (dspace at dataquest.sk)
*/
public interface PreviewContentDAO extends GenericDAO<PreviewContent> {
/**
* Find all preview content based on ID of bitstream the preview content is added to.
*
* @param context DSpace context
* @param bitstreamId The bitstream ID
* @return List of found preview content
* @throws SQLException If a database error occurs
*/
List<PreviewContent> findByBitstream(Context context, UUID bitstreamId) throws SQLException;

/**
* Find all preview content based on bitstream that are the root directory.
*
* @param context DSpace context
* @param bitstreamId The bitstream ID
* @return List of found preview content
* @throws SQLException If a database error occurs
*/
List<PreviewContent> findRootByBitstream(Context context, UUID bitstreamId) throws SQLException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
*
* http://www.dspace.org/license/
*/
package org.dspace.content.dao.impl;

import java.sql.SQLException;
import java.util.List;
import java.util.UUID;
import javax.persistence.Query;

import org.dspace.content.PreviewContent;
import org.dspace.content.dao.PreviewContentDAO;
import org.dspace.core.AbstractHibernateDAO;
import org.dspace.core.Context;

/**
* Hibernate implementation of the Database Access Object interface class for the PreviewContent object.
* This class should never be accessed directly.
*
* @author Michaela Paurikova (dspace at dataquest.sk)
*/
public class PreviewContentDAOImpl extends AbstractHibernateDAO<PreviewContent> implements PreviewContentDAO {

protected PreviewContentDAOImpl() {
super();
}

@Override
public List<PreviewContent> findByBitstream(Context context, UUID bitstreamId) throws SQLException {
Query query = createQuery(context, "SELECT pc FROM " + PreviewContent.class.getSimpleName() +
" as pc join pc.bitstream as b WHERE b.id = :bitstream_id");
query.setParameter("bitstream_id", bitstreamId);
query.setHint("org.hibernate.cacheable", Boolean.TRUE);
return findMany(context, query);
}

@Override
public List<PreviewContent> findRootByBitstream(Context context, UUID bitstreamId) throws SQLException {
// select only data from the previewcontent table whose ID is not a child in the preview2preview table
Query query = createQuery(context,
"SELECT pc FROM " + PreviewContent.class.getSimpleName() + " pc " +
"JOIN pc.bitstream b " +
"WHERE b.id = :bitstream_id " +
"AND pc.id NOT IN (SELECT child.id FROM " + PreviewContent.class.getSimpleName() + " parent " +
"JOIN parent.sub child)"
);
query.setParameter("bitstream_id", bitstreamId);
query.setHint("org.hibernate.cacheable", Boolean.TRUE);
return findMany(context, query);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.dspace.content.service.MetadataFieldService;
import org.dspace.content.service.MetadataSchemaService;
import org.dspace.content.service.MetadataValueService;
import org.dspace.content.service.PreviewContentService;
import org.dspace.content.service.RelationshipService;
import org.dspace.content.service.RelationshipTypeService;
import org.dspace.content.service.SiteService;
Expand Down Expand Up @@ -76,6 +77,7 @@ public abstract class ContentServiceFactory {
public abstract SiteService getSiteService();

public abstract SubscribeService getSubscribeService();
public abstract PreviewContentService getPreviewContentService();

/**
* Return the implementation of the RelationshipTypeService interface
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.dspace.content.service.MetadataFieldService;
import org.dspace.content.service.MetadataSchemaService;
import org.dspace.content.service.MetadataValueService;
import org.dspace.content.service.PreviewContentService;
import org.dspace.content.service.RelationshipService;
import org.dspace.content.service.RelationshipTypeService;
import org.dspace.content.service.SiteService;
Expand Down Expand Up @@ -83,6 +84,8 @@ public class ContentServiceFactoryImpl extends ContentServiceFactory {
private EntityTypeService entityTypeService;
@Autowired(required = true)
private EntityService entityService;
@Autowired(required = true)
private PreviewContentService previewContentService;

@Autowired(required = true)
private DspaceObjectClarinService dspaceObjectClarinService;
Expand Down Expand Up @@ -165,6 +168,11 @@ public SubscribeService getSubscribeService() {
return subscribeService ;
}

@Override
public PreviewContentService getPreviewContentService() {
return previewContentService;
}

@Override
public RelationshipTypeService getRelationshipTypeService() {
return relationshipTypeService;
Expand Down
Loading
Loading