Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions dspace-api/src/main/java/org/dspace/content/Bitstream.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
@Entity
@Table(name = "bitstream")
public class Bitstream extends DSpaceObject implements DSpaceObjectLegacySupport {

@Column(name = "bitstream_id", insertable = false, updatable = false)
private Integer legacyId;

Expand Down Expand Up @@ -79,7 +80,6 @@ public class Bitstream extends DSpaceObject implements DSpaceObjectLegacySupport
@Transient
private transient BitstreamService bitstreamService;


/**
* Protected constructor, create object using:
* {@link org.dspace.content.service.BitstreamService#create(Context, Bundle, InputStream)}
Expand Down Expand Up @@ -434,5 +434,4 @@ public void setAcceptanceDate(Context context, DCDate acceptanceDate) throws SQL
getBitstreamService()
.setMetadataSingleValue(context, this, "dcterms", "accessRights", null, null, acceptanceDate.toString());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import java.util.UUID;

import org.apache.commons.collections4.CollectionUtils;
Expand All @@ -27,10 +28,13 @@
import org.dspace.authorize.ResourcePolicy;
import org.dspace.authorize.service.AuthorizeService;
import org.dspace.authorize.service.ResourcePolicyService;
import org.dspace.content.clarin.ClarinLicense;
import org.dspace.content.dao.BundleDAO;
import org.dspace.content.service.BitstreamService;
import org.dspace.content.service.BundleService;
import org.dspace.content.service.ItemService;
import org.dspace.content.service.clarin.ClarinLicenseResourceMappingService;
import org.dspace.content.service.clarin.ClarinLicenseService;
import org.dspace.core.Constants;
import org.dspace.core.Context;
import org.dspace.core.LogHelper;
Expand Down Expand Up @@ -62,6 +66,10 @@ public class BundleServiceImpl extends DSpaceObjectServiceImpl<Bundle> implement
protected AuthorizeService authorizeService;
@Autowired(required = true)
protected ResourcePolicyService resourcePolicyService;
@Autowired(required = true)
protected ClarinLicenseService clarinLicenseService;
@Autowired(required = true)
protected ClarinLicenseResourceMappingService clarinLicenseResourceMappingService;

protected BundleServiceImpl() {
super();
Expand Down Expand Up @@ -160,7 +168,6 @@ public void addBitstream(Context context, Bundle bundle, Bitstream bitstream)
bundle.addBitstream(bitstream);
bitstream.getBundles().add(bundle);


context.addEvent(new Event(Event.ADD, Constants.BUNDLE, bundle.getID(),
Constants.BITSTREAM, bitstream.getID(), String.valueOf(bitstream.getSequenceID()),
getIdentifiers(context, bundle)));
Expand All @@ -169,6 +176,52 @@ public void addBitstream(Context context, Bundle bundle, Bitstream bitstream)
// FIXME: multiple inclusion is affected by this...
authorizeService.inheritPolicies(context, bundle, bitstream);
bitstreamService.update(context, bitstream);

// Add Clarin License to the bitstream
try {
if (!Objects.equals(bundle.getName(), Constants.CONTENT_BUNDLE_NAME)) {
return;
}

if (Objects.isNull(owningItem)) {
return;
}

List<MetadataValue> dcRights =
itemService.getMetadata(owningItem, "dc", "rights", null, Item.ANY);
List<MetadataValue> dcRightsUri =
itemService.getMetadata(owningItem, "dc", "rights", "uri", Item.ANY);

String licenseUri = null;
if(CollectionUtils.isNotEmpty(dcRights)) {
if ( dcRights.size() != dcRightsUri.size() ) {
log.warn( String.format("Harvested bitstream [%s / %s] has different length of dc_rights and dc_rights_uri",
bitstream.getName(), bitstream.getHandle()));
licenseUri = "unknown";
}else {
licenseUri = Objects.requireNonNull(dcRightsUri.get(0)).getValue();
}
}

ClarinLicense clarinLicense = this.clarinLicenseService.findByDefinition(context, licenseUri);
if (Objects.isNull(clarinLicense)) {
log.info("Cannot find clarin license with definition: " + licenseUri);
}

List<Bundle> bundles = owningItem.getBundles(Constants.CONTENT_BUNDLE_NAME);
for (Bundle clarinBundle : bundles) {
List<Bitstream> bitstreamList = clarinBundle.getBitstreams();
for (Bitstream bundleBitstream : bitstreamList) {
// in case bitstream ID exists in license table for some reason .. just remove it
this.clarinLicenseResourceMappingService.detachLicenses(context, bitstream);
}
// add the license to bitstream
this.clarinLicenseResourceMappingService.attachLicense(context, clarinLicense, bitstream);
}
} catch (SQLException e) {
log.error("Something went wrong in the maintenance of clarin license in the bitstream bundle: "
+ e.getSQLState());
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.dspace.workflow.WorkflowService;
import org.springframework.beans.factory.annotation.Autowired;

/**
/**
* Service implementation for the WorkspaceItem object.
* This class is responsible for all business logic calls for the WorkspaceItem object and is autowired by spring.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
package org.dspace.content.clarin;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
Expand All @@ -21,9 +22,11 @@
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

import org.dspace.authorize.ResourcePolicy;
import org.dspace.core.ReloadableEntity;

/**
Expand All @@ -50,7 +53,10 @@ public class ClarinLicense implements ReloadableEntity<Integer> {
name = "license_label_extended_mapping",
joinColumns = @JoinColumn(name = "license_id"),
inverseJoinColumns = @JoinColumn(name = "label_id"))
Set<ClarinLicenseLabel> clarinLicenseLabels = new HashSet<>();;
Set<ClarinLicenseLabel> clarinLicenseLabels = new HashSet<>();

@OneToMany(fetch = FetchType.LAZY, mappedBy = "license", cascade = CascadeType.PERSIST)
private final List<ClarinLicenseResourceMapping> clarinLicenseResourceMappings = new ArrayList<>();

// @Column(name = "eperson_id")
// private Integer epersonId;
Expand Down Expand Up @@ -115,6 +121,19 @@ public void setLicenseLabels(Set<ClarinLicenseLabel> clarinLicenseLabels) {
this.clarinLicenseLabels = clarinLicenseLabels;
}

public List<ClarinLicenseResourceMapping> getClarinLicenseResourceMappings() {
return clarinLicenseResourceMappings;
}

public ClarinLicenseLabel getNonExtendedClarinLicenseLabel() {
for (ClarinLicenseLabel cll : getLicenseLabels()) {
if (!cll.isExtended()) {
return cll;
}
}
return null;
}

@Override
public Integer getID() {
return id;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package org.dspace.content.clarin;

import org.dspace.content.Bitstream;
import org.dspace.core.ReloadableEntity;

import javax.persistence.*;
import java.util.UUID;

@Entity
@Table(name = "license_resource_mapping")
public class ClarinLicenseResourceMapping implements ReloadableEntity<Integer> {

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

@ManyToOne(fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST})
@JoinColumn(name = "license_id")
private ClarinLicense license;

@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "bitstream_uuid", referencedColumnName = "uuid")
private Bitstream bitstream;

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

public void setId(Integer id) {
this.id = id;
}

public Bitstream getBitstream() {
return bitstream;
}

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

public ClarinLicense getLicense() {
return license;
}

public void setLicense(ClarinLicense license) {
this.license = license;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
package org.dspace.content.clarin;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.NullArgumentException;
import org.dspace.authorize.AuthorizeException;
import org.dspace.authorize.service.AuthorizeService;
import org.dspace.content.Bitstream;
import org.dspace.content.dao.clarin.ClarinLicenseResourceMappingDAO;
import org.dspace.content.service.BitstreamService;
import org.dspace.content.service.clarin.ClarinLicenseResourceMappingService;
import org.dspace.content.service.clarin.ClarinLicenseService;
import org.dspace.core.Context;
import org.dspace.core.LogHelper;
import org.hibernate.ObjectNotFoundException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import javax.ws.rs.NotFoundException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.UUID;

public class ClarinLicenseResourceMappingServiceImpl implements ClarinLicenseResourceMappingService {

private static final Logger log = LoggerFactory.getLogger(ClarinLicenseServiceImpl.class);

@Autowired
ClarinLicenseResourceMappingDAO clarinLicenseResourceMappingDAO;

@Autowired
ClarinLicenseService clarinLicenseService;

@Autowired
BitstreamService bitstreamService;

@Autowired
AuthorizeService authorizeService;

@Override
public ClarinLicenseResourceMapping create(Context context) throws SQLException {
// Create a table row
ClarinLicenseResourceMapping clarinLicenseResourceMapping = clarinLicenseResourceMappingDAO.create(context, new ClarinLicenseResourceMapping());

log.info(LogHelper.getHeader(context, "create_clarin_license_resource_mapping", "clarin_license_resource_mapping_id="
+ clarinLicenseResourceMapping.getID()));

return clarinLicenseResourceMapping;
}

@Override
public ClarinLicenseResourceMapping create(Context context, ClarinLicenseResourceMapping clarinLicenseResourceMapping) throws SQLException {
return clarinLicenseResourceMappingDAO.create(context, clarinLicenseResourceMapping);
}

@Override
public ClarinLicenseResourceMapping create(Context context, Integer licenseId, UUID bitstreamUuid) throws SQLException {
ClarinLicenseResourceMapping clarinLicenseResourceMapping = new ClarinLicenseResourceMapping();
ClarinLicense clarinLicense = clarinLicenseService.find(context, licenseId);
if (Objects.isNull(clarinLicense)) {
throw new NotFoundException("Cannot find the license with id: " + licenseId);
}

Bitstream bitstream = bitstreamService.find(context, bitstreamUuid);
if (Objects.isNull(bitstream)) {
throw new NotFoundException("Cannot find the bitstream with id: " + bitstreamUuid);
}
clarinLicenseResourceMapping.setLicense(clarinLicense);
clarinLicenseResourceMapping.setBitstream(bitstream);

return clarinLicenseResourceMappingDAO.create(context, clarinLicenseResourceMapping);
}

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

@Override
public List<ClarinLicenseResourceMapping> findAllByLicenseId(Context context, Integer licenseId) throws SQLException {
List<ClarinLicenseResourceMapping> mappings = clarinLicenseResourceMappingDAO.findAll(context, ClarinLicenseResourceMapping.class);
List<ClarinLicenseResourceMapping> mappingsByLicenseId = new ArrayList<>();
for (ClarinLicenseResourceMapping mapping: mappings) {
if (Objects.equals(mapping.getLicense().getID(), licenseId)) {
mappingsByLicenseId.add(mapping);
}
}
return mappingsByLicenseId;
}

@Override
public void update(Context context, ClarinLicenseResourceMapping newClarinLicenseResourceMapping) throws SQLException {
if (Objects.isNull(newClarinLicenseResourceMapping)) {
throw new NullArgumentException("Cannot update clarin license resource mapping because the new clarin license resource mapping is null");
}

ClarinLicenseResourceMapping foundClarinLicenseResourceMapping = find(context, newClarinLicenseResourceMapping.getID());
if (Objects.isNull(foundClarinLicenseResourceMapping)) {
throw new ObjectNotFoundException(newClarinLicenseResourceMapping.getID(), "Cannot update the license resource mapping because" +
" the clarin license resource mapping wasn't found " +
"in the database.");
}

clarinLicenseResourceMappingDAO.save(context, newClarinLicenseResourceMapping);
}

@Override
public void delete(Context context, ClarinLicenseResourceMapping clarinLicenseResourceMapping) throws SQLException {
clarinLicenseResourceMappingDAO.delete(context, clarinLicenseResourceMapping);
}

@Override
public void detachLicenses(Context context, Bitstream bitstream) throws SQLException {
List<ClarinLicenseResourceMapping> clarinLicenseResourceMappings =
clarinLicenseResourceMappingDAO.findByBitstreamUUID(context, bitstream.getID());

if (CollectionUtils.isEmpty(clarinLicenseResourceMappings)) {
log.info("Cannot detach licenses because bitstream with id: " + bitstream.getID() + " is not " +
"attached to any license.");
return;
}

clarinLicenseResourceMappings.forEach(clarinLicenseResourceMapping -> {
try {
this.delete(context, clarinLicenseResourceMapping);
} catch (SQLException e) {
log.error(e.getMessage());
}
});
}

@Override
public void attachLicense(Context context, ClarinLicense clarinLicense, Bitstream bitstream) throws SQLException {
ClarinLicenseResourceMapping clarinLicenseResourceMapping = this.create(context);
if (Objects.isNull(clarinLicenseResourceMapping)) {
throw new NotFoundException("Cannot create the ClarinLicenseResourceMapping.");
}
if (Objects.isNull(clarinLicense) || Objects.isNull(bitstream)) {
throw new NullArgumentException("Clarin License or Bitstream cannot be null.");
}

clarinLicenseResourceMapping.setBitstream(bitstream);
clarinLicenseResourceMapping.setLicense(clarinLicense);

clarinLicenseResourceMappingDAO.save(context, clarinLicenseResourceMapping);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ public ClarinLicense find(Context context, int valueId) throws SQLException {
return clarinLicenseDAO.findByID(context, ClarinLicense.class, valueId);
}

@Override
public ClarinLicense findByDefinition(Context context, String definition) throws SQLException {
return clarinLicenseDAO.findByDefinition(context, definition);
}

@Override
public List<ClarinLicense> findAll(Context context) throws SQLException, AuthorizeException {
if (!authorizeService.isAdmin(context)) {
Expand Down
Loading