From 15a3c913cc0209db5eabbe7eab7a8468b9273fc8 Mon Sep 17 00:00:00 2001 From: Michaela Paurikova Date: Mon, 19 Sep 2022 10:04:06 +0200 Subject: [PATCH 1/8] created clarin license resource mapping --- .../java/org/dspace/content/Bitstream.java | 3 +- .../dspace/content/clarin/ClarinLicense.java | 2 +- .../clarin/ClarinLicenseResourceMapping.java | 55 +++++++++ ...arinLicenseResourceMappingServiceImpl.java | 107 ++++++++++++++++++ .../ClarinLicenseResourceMappingDAO.java | 7 ++ .../ClarinLicenseResourceMappingDAOImpl.java | 11 ++ .../content/factory/ClarinServiceFactory.java | 4 +- .../factory/ClarinServiceFactoryImpl.java | 11 ++ .../ClarinLicenseResourceMappingService.java | 25 ++++ ...07.28__Upgrade_to_Lindat_Clarin_schema.sql | 8 +- ...07.28__Upgrade_to_Lindat_Clarin_schema.sql | 7 +- .../ClarinLicenseResourceMappingBuilder.java | 71 ++++++++++++ .../converter/ClarinLicenseConverter.java | 21 +++- .../ClarinLicenseRestRepository.java | 8 ++ .../rest/ClarinLicenseRestRepositoryIT.java | 74 ++++++++++++ dspace/config/hibernate.cfg.xml | 1 + .../config/spring/api/core-dao-services.xml | 1 + dspace/config/spring/api/core-services.xml | 1 + 18 files changed, 401 insertions(+), 16 deletions(-) create mode 100644 dspace-api/src/main/java/org/dspace/content/clarin/ClarinLicenseResourceMapping.java create mode 100644 dspace-api/src/main/java/org/dspace/content/clarin/ClarinLicenseResourceMappingServiceImpl.java create mode 100644 dspace-api/src/main/java/org/dspace/content/dao/clarin/ClarinLicenseResourceMappingDAO.java create mode 100644 dspace-api/src/main/java/org/dspace/content/dao/impl/clarin/ClarinLicenseResourceMappingDAOImpl.java create mode 100644 dspace-api/src/main/java/org/dspace/content/service/clarin/ClarinLicenseResourceMappingService.java create mode 100644 dspace-api/src/test/java/org/dspace/builder/ClarinLicenseResourceMappingBuilder.java diff --git a/dspace-api/src/main/java/org/dspace/content/Bitstream.java b/dspace-api/src/main/java/org/dspace/content/Bitstream.java index 451a3b75784d..15ebbd16a9d9 100644 --- a/dspace-api/src/main/java/org/dspace/content/Bitstream.java +++ b/dspace-api/src/main/java/org/dspace/content/Bitstream.java @@ -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; @@ -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)} @@ -434,5 +434,4 @@ public void setAcceptanceDate(Context context, DCDate acceptanceDate) throws SQL getBitstreamService() .setMetadataSingleValue(context, this, "dcterms", "accessRights", null, null, acceptanceDate.toString()); } - } diff --git a/dspace-api/src/main/java/org/dspace/content/clarin/ClarinLicense.java b/dspace-api/src/main/java/org/dspace/content/clarin/ClarinLicense.java index f1f81fc2c248..cdd7284ec77b 100644 --- a/dspace-api/src/main/java/org/dspace/content/clarin/ClarinLicense.java +++ b/dspace-api/src/main/java/org/dspace/content/clarin/ClarinLicense.java @@ -35,7 +35,7 @@ public class ClarinLicense implements ReloadableEntity { name = "license_label_extended_mapping", joinColumns = @JoinColumn(name = "license_id"), inverseJoinColumns = @JoinColumn(name = "label_id")) - Set clarinLicenseLabels = new HashSet<>();; + Set clarinLicenseLabels = new HashSet<>(); // @Column(name = "eperson_id") // private Integer epersonId; diff --git a/dspace-api/src/main/java/org/dspace/content/clarin/ClarinLicenseResourceMapping.java b/dspace-api/src/main/java/org/dspace/content/clarin/ClarinLicenseResourceMapping.java new file mode 100644 index 000000000000..2b93541b17fd --- /dev/null +++ b/dspace-api/src/main/java/org/dspace/content/clarin/ClarinLicenseResourceMapping.java @@ -0,0 +1,55 @@ +package org.dspace.content.clarin; + +import org.dspace.core.ReloadableEntity; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.SequenceGenerator; +import javax.persistence.Table; +import java.util.UUID; + +@Entity +@Table(name = "license_resource_mapping") +public class ClarinLicenseResourceMapping implements ReloadableEntity { + + @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; + + @Column(name = "bitstream_uuid") + private UUID bitstreamUuid = null; + + @Column(name = "license_id") + private Integer licenseId = 0; + + @Override + public Integer getID() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public UUID getBitstreamId() { + return bitstreamUuid; + } + + public void setBitstreamId(UUID bitstreamId) { + this.bitstreamUuid = bitstreamId; + } + + public Integer getLicenseId() { + return licenseId; + } + + public void setLicenseId(Integer licenseId) { + this.licenseId = licenseId; + } +} diff --git a/dspace-api/src/main/java/org/dspace/content/clarin/ClarinLicenseResourceMappingServiceImpl.java b/dspace-api/src/main/java/org/dspace/content/clarin/ClarinLicenseResourceMappingServiceImpl.java new file mode 100644 index 000000000000..e467caeca045 --- /dev/null +++ b/dspace-api/src/main/java/org/dspace/content/clarin/ClarinLicenseResourceMappingServiceImpl.java @@ -0,0 +1,107 @@ +package org.dspace.content.clarin; + +import org.apache.commons.lang.NullArgumentException; +import org.dspace.authorize.AuthorizeException; +import org.dspace.authorize.service.AuthorizeService; +import org.dspace.content.dao.clarin.ClarinLicenseResourceMappingDAO; +import org.dspace.content.service.clarin.ClarinLicenseResourceMappingService; +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 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 + AuthorizeService authorizeService; + + @Override + public ClarinLicenseResourceMapping create(Context context) throws SQLException, AuthorizeException { + if (!authorizeService.isAdmin(context)) { + throw new AuthorizeException( + "You must be an admin to create an Clarin License"); + } + + // 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, AuthorizeException { + if (!authorizeService.isAdmin(context)) { + throw new AuthorizeException( + "You must be an admin to create an Clarin License Resource Mapping"); + } + return clarinLicenseResourceMappingDAO.create(context, clarinLicenseResourceMapping); + } + + @Override + public ClarinLicenseResourceMapping create(Context context, Integer licenseId, UUID bitstreamUuid) throws SQLException, AuthorizeException { + if (!authorizeService.isAdmin(context)) { + throw new AuthorizeException( + "You must be an admin to create an Clarin License Resource Mapping"); + } + + ClarinLicenseResourceMapping clarinLicenseResourceMapping = new ClarinLicenseResourceMapping(); + clarinLicenseResourceMapping.setLicenseId(licenseId); + clarinLicenseResourceMapping.setBitstreamId(bitstreamUuid); + + 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 findAllByLicenseId(Context context, Integer licenseId) throws SQLException { + List mappings = clarinLicenseResourceMappingDAO.findAll(context, ClarinLicenseResourceMapping.class); + List mappingsByLicenseId = new ArrayList<>(); + for (ClarinLicenseResourceMapping mapping: mappings) { + if (mapping.getLicenseId() == 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); + } +} diff --git a/dspace-api/src/main/java/org/dspace/content/dao/clarin/ClarinLicenseResourceMappingDAO.java b/dspace-api/src/main/java/org/dspace/content/dao/clarin/ClarinLicenseResourceMappingDAO.java new file mode 100644 index 000000000000..3d2a07ab43ad --- /dev/null +++ b/dspace-api/src/main/java/org/dspace/content/dao/clarin/ClarinLicenseResourceMappingDAO.java @@ -0,0 +1,7 @@ +package org.dspace.content.dao.clarin; + +import org.dspace.content.clarin.ClarinLicenseResourceMapping; +import org.dspace.core.GenericDAO; + +public interface ClarinLicenseResourceMappingDAO extends GenericDAO { +} diff --git a/dspace-api/src/main/java/org/dspace/content/dao/impl/clarin/ClarinLicenseResourceMappingDAOImpl.java b/dspace-api/src/main/java/org/dspace/content/dao/impl/clarin/ClarinLicenseResourceMappingDAOImpl.java new file mode 100644 index 000000000000..2475b6ee41b0 --- /dev/null +++ b/dspace-api/src/main/java/org/dspace/content/dao/impl/clarin/ClarinLicenseResourceMappingDAOImpl.java @@ -0,0 +1,11 @@ +package org.dspace.content.dao.impl.clarin; + +import org.dspace.content.clarin.ClarinLicenseResourceMapping; +import org.dspace.content.dao.clarin.ClarinLicenseResourceMappingDAO; +import org.dspace.core.AbstractHibernateDAO; + +public class ClarinLicenseResourceMappingDAOImpl extends AbstractHibernateDAO implements ClarinLicenseResourceMappingDAO { + protected ClarinLicenseResourceMappingDAOImpl() { + super(); + } +} diff --git a/dspace-api/src/main/java/org/dspace/content/factory/ClarinServiceFactory.java b/dspace-api/src/main/java/org/dspace/content/factory/ClarinServiceFactory.java index 1346fefb9b9f..1f77dab1c400 100644 --- a/dspace-api/src/main/java/org/dspace/content/factory/ClarinServiceFactory.java +++ b/dspace-api/src/main/java/org/dspace/content/factory/ClarinServiceFactory.java @@ -1,7 +1,7 @@ package org.dspace.content.factory; -import org.dspace.content.service.BundleService; import org.dspace.content.service.clarin.ClarinLicenseLabelService; +import org.dspace.content.service.clarin.ClarinLicenseResourceMappingService; import org.dspace.content.service.clarin.ClarinLicenseService; import org.dspace.services.factory.DSpaceServicesFactory; @@ -11,6 +11,8 @@ public abstract class ClarinServiceFactory { public abstract ClarinLicenseLabelService getClarinLicenseLabelService(); + public abstract ClarinLicenseResourceMappingService getClarinResourceMappingService(); + public static ClarinServiceFactory getInstance() { return DSpaceServicesFactory.getInstance().getServiceManager() .getServiceByName("clarinServiceFactory", ClarinServiceFactory.class); diff --git a/dspace-api/src/main/java/org/dspace/content/factory/ClarinServiceFactoryImpl.java b/dspace-api/src/main/java/org/dspace/content/factory/ClarinServiceFactoryImpl.java index ad390ce92117..c8cab1a6de49 100644 --- a/dspace-api/src/main/java/org/dspace/content/factory/ClarinServiceFactoryImpl.java +++ b/dspace-api/src/main/java/org/dspace/content/factory/ClarinServiceFactoryImpl.java @@ -1,7 +1,9 @@ package org.dspace.content.factory; +import org.dspace.content.clarin.ClarinLicenseResourceMapping; import org.dspace.content.service.BitstreamFormatService; import org.dspace.content.service.clarin.ClarinLicenseLabelService; +import org.dspace.content.service.clarin.ClarinLicenseResourceMappingService; import org.dspace.content.service.clarin.ClarinLicenseService; import org.springframework.beans.factory.annotation.Autowired; @@ -13,6 +15,10 @@ public class ClarinServiceFactoryImpl extends ClarinServiceFactory { @Autowired(required = true) private ClarinLicenseLabelService clarinLicenseLabelService; + @Autowired(required = true) + private ClarinLicenseResourceMappingService clarinLicenseResourceMappingService; + + @Override public ClarinLicenseService getClarinLicenseService() { return clarinLicenseService; @@ -22,4 +28,9 @@ public ClarinLicenseService getClarinLicenseService() { public ClarinLicenseLabelService getClarinLicenseLabelService() { return clarinLicenseLabelService; } + + @Override + public ClarinLicenseResourceMappingService getClarinResourceMappingService() { + return clarinLicenseResourceMappingService; + } } diff --git a/dspace-api/src/main/java/org/dspace/content/service/clarin/ClarinLicenseResourceMappingService.java b/dspace-api/src/main/java/org/dspace/content/service/clarin/ClarinLicenseResourceMappingService.java new file mode 100644 index 000000000000..5be5589318a0 --- /dev/null +++ b/dspace-api/src/main/java/org/dspace/content/service/clarin/ClarinLicenseResourceMappingService.java @@ -0,0 +1,25 @@ +package org.dspace.content.service.clarin; + +import org.dspace.authorize.AuthorizeException; +import org.dspace.content.clarin.ClarinLicense; +import org.dspace.content.clarin.ClarinLicenseLabel; +import org.dspace.content.clarin.ClarinLicenseResourceMapping; +import org.dspace.core.Context; + +import java.sql.SQLException; +import java.util.List; +import java.util.UUID; + +public interface ClarinLicenseResourceMappingService { + + ClarinLicenseResourceMapping create(Context context) throws SQLException, AuthorizeException; + ClarinLicenseResourceMapping create(Context context, ClarinLicenseResourceMapping clarinLicenseResourceMapping) throws SQLException, AuthorizeException; + ClarinLicenseResourceMapping create(Context context, Integer licenseId, UUID bitstreamUuid) throws SQLException, AuthorizeException; + + ClarinLicenseResourceMapping find(Context context, int valueId) throws SQLException; + List findAllByLicenseId(Context context, Integer licenseId) throws SQLException; + + void update(Context context, ClarinLicenseResourceMapping newClarinLicenseResourceMapping) throws SQLException; + + void delete(Context context, ClarinLicenseResourceMapping clarinLicenseResourceMapping) throws SQLException; +} diff --git a/dspace-api/src/main/resources/org/dspace/storage/rdbms/sqlmigration/h2/V7.2_2022.07.28__Upgrade_to_Lindat_Clarin_schema.sql b/dspace-api/src/main/resources/org/dspace/storage/rdbms/sqlmigration/h2/V7.2_2022.07.28__Upgrade_to_Lindat_Clarin_schema.sql index 223c542d94c9..08e7d28ec1f3 100644 --- a/dspace-api/src/main/resources/org/dspace/storage/rdbms/sqlmigration/h2/V7.2_2022.07.28__Upgrade_to_Lindat_Clarin_schema.sql +++ b/dspace-api/src/main/resources/org/dspace/storage/rdbms/sqlmigration/h2/V7.2_2022.07.28__Upgrade_to_Lindat_Clarin_schema.sql @@ -110,9 +110,8 @@ CREATE SEQUENCE license_label_label_id_seq CREATE TABLE license_resource_mapping ( mapping_id integer NOT NULL, - bitstream_id integer, - license_id integer, - active boolean DEFAULT true + bitstream_uuid uuid UNIQUE, + license_id integer ); @@ -261,6 +260,9 @@ ALTER TABLE license_resource_mapping ADD CONSTRAINT license_definition_license_resource_mapping_fk FOREIGN KEY (license_id) REFERENCES license_definition(license_id) ON DELETE CASCADE; +ALTER TABLE license_resource_mapping + ADD CONSTRAINT bitstream_license_resource_mapping_fk FOREIGN KEY (bitstream_uuid) REFERENCES bitstream(uuid) ON DELETE CASCADE; + -- -- Name: license_label_license_definition_fk; Type: FK CONSTRAINT; Schema: public; Owner: dspace -- diff --git a/dspace-api/src/main/resources/org/dspace/storage/rdbms/sqlmigration/postgres/V7.2_2022.07.28__Upgrade_to_Lindat_Clarin_schema.sql b/dspace-api/src/main/resources/org/dspace/storage/rdbms/sqlmigration/postgres/V7.2_2022.07.28__Upgrade_to_Lindat_Clarin_schema.sql index 3eb5ca2d6302..32f1a403e1d9 100644 --- a/dspace-api/src/main/resources/org/dspace/storage/rdbms/sqlmigration/postgres/V7.2_2022.07.28__Upgrade_to_Lindat_Clarin_schema.sql +++ b/dspace-api/src/main/resources/org/dspace/storage/rdbms/sqlmigration/postgres/V7.2_2022.07.28__Upgrade_to_Lindat_Clarin_schema.sql @@ -134,9 +134,8 @@ SELECT pg_catalog.setval('license_label_label_id_seq', 19, true); CREATE TABLE license_resource_mapping ( mapping_id integer NOT NULL, - bitstream_id integer, - license_id integer, - active boolean DEFAULT true + bitstream_uuid uuid UNIQUE, + license_id integer ); @@ -303,6 +302,8 @@ ALTER TABLE ONLY license_label_extended_mapping ALTER TABLE ONLY license_resource_mapping ADD CONSTRAINT license_definition_license_resource_mapping_fk FOREIGN KEY (license_id) REFERENCES license_definition(license_id) ON DELETE CASCADE; +ALTER TABLE ONLY license_resource_mapping + ADD CONSTRAINT bitstream_license_resource_mapping_fk FOREIGN KEY (bitstream_uuid) REFERENCES bitstream(uuid) ON DELETE CASCADE; -- -- Name: license_label_license_definition_fk; Type: FK CONSTRAINT; Schema: public; Owner: dspace diff --git a/dspace-api/src/test/java/org/dspace/builder/ClarinLicenseResourceMappingBuilder.java b/dspace-api/src/test/java/org/dspace/builder/ClarinLicenseResourceMappingBuilder.java new file mode 100644 index 000000000000..df3395d4192e --- /dev/null +++ b/dspace-api/src/test/java/org/dspace/builder/ClarinLicenseResourceMappingBuilder.java @@ -0,0 +1,71 @@ +package org.dspace.builder; + +import org.dspace.authorize.AuthorizeException; +import org.dspace.content.clarin.ClarinLicense; +import org.dspace.content.clarin.ClarinLicenseResourceMapping; +import org.dspace.content.factory.ClarinServiceFactory; +import org.dspace.content.service.clarin.ClarinLicenseResourceMappingService; +import org.dspace.core.Context; + +import java.sql.SQLException; + +public class ClarinLicenseResourceMappingBuilder extends AbstractBuilder { + + private ClarinLicenseResourceMappingService clarinLicenseResourceMappingService = ClarinServiceFactory.getInstance().getClarinResourceMappingService(); + + private ClarinLicenseResourceMapping clarinLicenseResourceMapping; + + protected ClarinLicenseResourceMappingBuilder(Context context) { + super(context); + } + + public static ClarinLicenseResourceMappingBuilder createClarinLicenseResourceMapping(final Context context) { + ClarinLicenseResourceMappingBuilder builder = new ClarinLicenseResourceMappingBuilder(context); + return builder.create(context); + } + + private ClarinLicenseResourceMappingBuilder create(final Context context) { + this.context = context; + try { + clarinLicenseResourceMapping = clarinLicenseResourceMappingService.create(context); + } catch (Exception e) { + return handleException(e); + } + return this; + } + + @Override + public void cleanup() throws Exception { + try (Context c = new Context()) { + c.turnOffAuthorisationSystem(); + // Ensure object and any related objects are reloaded before checking to see what needs cleanup + clarinLicenseResourceMapping = c.reloadEntity(clarinLicenseResourceMapping); + delete(c, clarinLicenseResourceMapping); + c.complete(); + indexingService.commit(); + } + } + + @Override + public ClarinLicenseResourceMapping build() throws SQLException, AuthorizeException { + try { + context.dispatchEvents(); + indexingService.commit(); + return clarinLicenseResourceMapping; + } catch (Exception e) { + return handleException(e); + } + } + + @Override + public void delete(Context c, ClarinLicenseResourceMapping dso) throws Exception { + if (dso != null) { + getService().delete(c, dso); + } + } + + @Override + protected ClarinLicenseResourceMappingService getService() { + return clarinLicenseResourceMappingService; + } +} diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/converter/ClarinLicenseConverter.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/converter/ClarinLicenseConverter.java index 343a63b9c0fc..e6970551dc9f 100644 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/converter/ClarinLicenseConverter.java +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/converter/ClarinLicenseConverter.java @@ -1,26 +1,30 @@ package org.dspace.app.rest.converter; -import org.dspace.app.rest.converter.ConverterService; -import org.dspace.app.rest.converter.DSpaceConverter; import org.dspace.app.rest.model.ClarinLicenseLabelRest; import org.dspace.app.rest.model.ClarinLicenseRest; -import org.dspace.app.rest.model.CollectionRest; import org.dspace.app.rest.projection.Projection; -import org.dspace.content.Collection; import org.dspace.content.clarin.ClarinLicense; import org.dspace.content.clarin.ClarinLicenseLabel; +import org.dspace.content.service.clarin.ClarinLicenseResourceMappingService; +import org.dspace.xoai.services.api.context.ContextService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; +import javax.inject.Inject; import java.util.ArrayList; import java.util.List; @Component public class ClarinLicenseConverter implements DSpaceConverter { - @Autowired private ConverterService converter; + @Inject + private ContextService contextService; + + @Autowired + private ClarinLicenseResourceMappingService clarinLicenseResourceMappingService; + @Override public ClarinLicenseRest convert(ClarinLicense modelObject, Projection projection) { ClarinLicenseRest license = new ClarinLicenseRest(); @@ -35,7 +39,12 @@ public ClarinLicenseRest convert(ClarinLicense modelObject, Projection projectio // license.setExtendedClarinLicenseLabels(modelObject.getLicenseLabels(), projection); // license.setClarinLicenseLabel(modelObject.getLicenseLabels(), projection); // TODO find out which bitstreams are using this license - license.setBitstreams(0); + try { + license.setBitstreams(clarinLicenseResourceMappingService.findAllByLicenseId(contextService.getContext(), modelObject.getID()).size()); + } catch (Exception e) { + throw new RuntimeException("Message"); + } + return license; } diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/ClarinLicenseRestRepository.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/ClarinLicenseRestRepository.java index a505fa68575d..7121131cdba5 100644 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/ClarinLicenseRestRepository.java +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/ClarinLicenseRestRepository.java @@ -2,6 +2,7 @@ import org.dspace.app.rest.model.ClarinLicenseRest; import org.dspace.content.clarin.ClarinLicense; +import org.dspace.content.service.clarin.ClarinLicenseResourceMappingService; import org.dspace.content.service.clarin.ClarinLicenseService; import org.dspace.core.Context; import org.springframework.beans.factory.annotation.Autowired; @@ -20,6 +21,9 @@ public class ClarinLicenseRestRepository extends DSpaceRestRepository findAll(Context context, Pageable pageable) { try { List clarinLicenseList = clarinLicenseService.findAll(context); + //zmena bitstream +// for (ClarinLicense clarinLicense: clarinLicenseList) { +// clarinLicense.setBitstreams(clarinLicenseResourceMappingService.findAllByLicenseId(context, clarinLicense.getID()).size()); +// } return converter.toRestPage(clarinLicenseList, pageable, utils.obtainProjection()); } catch (SQLException e) { throw new RuntimeException(e.getMessage(), e); diff --git a/dspace-server-webapp/src/test/java/org/dspace/app/rest/ClarinLicenseRestRepositoryIT.java b/dspace-server-webapp/src/test/java/org/dspace/app/rest/ClarinLicenseRestRepositoryIT.java index f5b04fbfb2fb..b69b6fe54bbc 100644 --- a/dspace-server-webapp/src/test/java/org/dspace/app/rest/ClarinLicenseRestRepositoryIT.java +++ b/dspace-server-webapp/src/test/java/org/dspace/app/rest/ClarinLicenseRestRepositoryIT.java @@ -9,9 +9,18 @@ import org.dspace.app.rest.test.AbstractControllerIntegrationTest; import org.dspace.builder.ClarinLicenseBuilder; import org.dspace.builder.ClarinLicenseLabelBuilder; +import org.dspace.builder.ClarinLicenseResourceMappingBuilder; +import org.dspace.builder.CollectionBuilder; +import org.dspace.builder.CommunityBuilder; +import org.dspace.builder.ItemBuilder; +import org.dspace.content.Collection; +import org.dspace.content.Item; import org.dspace.content.clarin.ClarinLicense; import org.dspace.content.clarin.ClarinLicenseLabel; +import org.dspace.content.clarin.ClarinLicenseResourceMapping; +import org.dspace.content.service.ItemService; import org.dspace.content.service.clarin.ClarinLicenseLabelService; +import org.dspace.content.service.clarin.ClarinLicenseResourceMappingService; import org.dspace.content.service.clarin.ClarinLicenseService; import org.hamcrest.Matchers; import org.junit.Assert; @@ -30,6 +39,8 @@ public class ClarinLicenseRestRepositoryIT extends AbstractControllerIntegrationTest { + private static final String AUTHOR = "Test author name"; + @Autowired ClarinLicenseService clarinLicenseService; @@ -39,6 +50,15 @@ public class ClarinLicenseRestRepositoryIT extends AbstractControllerIntegration @Autowired ClarinLicenseConverter clarinLicenseConverter; + @Autowired + ClarinLicenseResourceMappingService clarinLicenseResourceMappingService; + + private Item publicItem1; + + private Item publicItem2; + + private Item publicItem3; + ClarinLicense firstCLicense; ClarinLicense secondCLicense; @@ -92,6 +112,55 @@ public void setup() throws Exception { secondCLicense.setLicenseLabels(secondClarinLicenseLabels); clarinLicenseService.update(context, secondCLicense); + //create community for items + parentCommunity = CommunityBuilder.createCommunity(context) + .withName("Parent Community") + .build(); + + //create collection for items + Collection col1 = CollectionBuilder.createCollection(context, parentCommunity).withName("Collection 1").build(); + Collection col2 = CollectionBuilder.createCollection(context, parentCommunity).withName("Collection 2").build(); + Collection col3 = CollectionBuilder.createCollection(context, parentCommunity).withName("Collection 3").build(); + + // create two items with the first license + publicItem1 = ItemBuilder.createItem(context, col1) + .withTitle("Item 1") + .withIssueDate("2022-10-17") + .withAuthor("Smith, Donald").withAuthor("Doe, John") + .withSubject("ExtraEntry") + .build(); + ClarinLicenseResourceMapping clarinLicenseResourceMapping1 = ClarinLicenseResourceMappingBuilder + .createClarinLicenseResourceMapping(context).build(); + clarinLicenseResourceMapping1.setLicenseId(firstCLicense.getID()); + clarinLicenseResourceMapping1.setBitstreamId(publicItem1.getID()); + clarinLicenseResourceMappingService.update(context, clarinLicenseResourceMapping1); + + publicItem2 = ItemBuilder.createItem(context, col2) + .withTitle("Public item 2") + .withIssueDate("2016-02-13") + .withAuthor("Smith, Maria").withAuthor("Doe, Jane") + .withSubject("TestingForMore").withSubject("ExtraEntry") + .build(); + ClarinLicenseResourceMapping clarinLicenseResourceMapping2 = ClarinLicenseResourceMappingBuilder + .createClarinLicenseResourceMapping(context).build(); + clarinLicenseResourceMapping1.setLicenseId(firstCLicense.getID()); + clarinLicenseResourceMapping1.setBitstreamId(publicItem2.getID()); + clarinLicenseResourceMappingService.update(context, clarinLicenseResourceMapping2); + + //create item with the second license + publicItem3 = ItemBuilder.createItem(context, col3) + .withTitle("Public item 3") + .withIssueDate("2016-02-13") + .withAuthor("Smith, Maria").withAuthor("Doe, Jane") + .withSubject("AnotherTest").withSubject("TestingForMore") + .withSubject("ExtraEntry") + .build(); + ClarinLicenseResourceMapping clarinLicenseResourceMapping3 = ClarinLicenseResourceMappingBuilder + .createClarinLicenseResourceMapping(context).build(); + clarinLicenseResourceMapping1.setLicenseId(secondCLicense.getID()); + clarinLicenseResourceMapping1.setBitstreamId(publicItem3.getID()); + clarinLicenseResourceMappingService.update(context, clarinLicenseResourceMapping3); + context.restoreAuthSystemState(); } @@ -129,5 +198,10 @@ public void findAll() throws Exception { ; } + @Test + public void findAllBitstreamByLicenseId() throws Exception { + //first license is used in firstItem and secondItem + Assert.assertEquals(this.clarinLicenseResourceMappingService.findAllByLicenseId(context, firstCLicense.getID()), 2); + } } diff --git a/dspace/config/hibernate.cfg.xml b/dspace/config/hibernate.cfg.xml index 57dce8c84df6..03ad78b13ccf 100644 --- a/dspace/config/hibernate.cfg.xml +++ b/dspace/config/hibernate.cfg.xml @@ -66,6 +66,7 @@ + diff --git a/dspace/config/spring/api/core-dao-services.xml b/dspace/config/spring/api/core-dao-services.xml index 2f470524e866..2acafcddaf0e 100644 --- a/dspace/config/spring/api/core-dao-services.xml +++ b/dspace/config/spring/api/core-dao-services.xml @@ -44,6 +44,7 @@ + diff --git a/dspace/config/spring/api/core-services.xml b/dspace/config/spring/api/core-services.xml index 6da31ef9f5e4..937c47a4fe9a 100644 --- a/dspace/config/spring/api/core-services.xml +++ b/dspace/config/spring/api/core-services.xml @@ -75,6 +75,7 @@ + From cf58ea350a9e98ff4c7b375536041e521eb9bc92 Mon Sep 17 00:00:00 2001 From: Michaela Paurikova Date: Tue, 20 Sep 2022 07:35:42 +0200 Subject: [PATCH 2/8] error with integration tests - active transaction --- .../content/WorkspaceItemServiceImpl.java | 1 + ...arinLicenseResourceMappingServiceImpl.java | 2 +- .../ClarinLicenseResourceMappingBuilder.java | 1 - .../ClarinLicenseRestRepository.java | 8 -- .../rest/ClarinLicenseRestRepositoryIT.java | 74 ------------------- 5 files changed, 2 insertions(+), 84 deletions(-) diff --git a/dspace-api/src/main/java/org/dspace/content/WorkspaceItemServiceImpl.java b/dspace-api/src/main/java/org/dspace/content/WorkspaceItemServiceImpl.java index d891dcf638e4..4284f6f09890 100644 --- a/dspace-api/src/main/java/org/dspace/content/WorkspaceItemServiceImpl.java +++ b/dspace-api/src/main/java/org/dspace/content/WorkspaceItemServiceImpl.java @@ -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. diff --git a/dspace-api/src/main/java/org/dspace/content/clarin/ClarinLicenseResourceMappingServiceImpl.java b/dspace-api/src/main/java/org/dspace/content/clarin/ClarinLicenseResourceMappingServiceImpl.java index e467caeca045..e77c0719e49f 100644 --- a/dspace-api/src/main/java/org/dspace/content/clarin/ClarinLicenseResourceMappingServiceImpl.java +++ b/dspace-api/src/main/java/org/dspace/content/clarin/ClarinLicenseResourceMappingServiceImpl.java @@ -32,7 +32,7 @@ public class ClarinLicenseResourceMappingServiceImpl implements ClarinLicenseRes public ClarinLicenseResourceMapping create(Context context) throws SQLException, AuthorizeException { if (!authorizeService.isAdmin(context)) { throw new AuthorizeException( - "You must be an admin to create an Clarin License"); + "You must be an admin to create an Clarin License Resource Mapping"); } // Create a table row diff --git a/dspace-api/src/test/java/org/dspace/builder/ClarinLicenseResourceMappingBuilder.java b/dspace-api/src/test/java/org/dspace/builder/ClarinLicenseResourceMappingBuilder.java index df3395d4192e..c163bd3c9f08 100644 --- a/dspace-api/src/test/java/org/dspace/builder/ClarinLicenseResourceMappingBuilder.java +++ b/dspace-api/src/test/java/org/dspace/builder/ClarinLicenseResourceMappingBuilder.java @@ -1,7 +1,6 @@ package org.dspace.builder; import org.dspace.authorize.AuthorizeException; -import org.dspace.content.clarin.ClarinLicense; import org.dspace.content.clarin.ClarinLicenseResourceMapping; import org.dspace.content.factory.ClarinServiceFactory; import org.dspace.content.service.clarin.ClarinLicenseResourceMappingService; diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/ClarinLicenseRestRepository.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/ClarinLicenseRestRepository.java index 7121131cdba5..a505fa68575d 100644 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/ClarinLicenseRestRepository.java +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/ClarinLicenseRestRepository.java @@ -2,7 +2,6 @@ import org.dspace.app.rest.model.ClarinLicenseRest; import org.dspace.content.clarin.ClarinLicense; -import org.dspace.content.service.clarin.ClarinLicenseResourceMappingService; import org.dspace.content.service.clarin.ClarinLicenseService; import org.dspace.core.Context; import org.springframework.beans.factory.annotation.Autowired; @@ -21,9 +20,6 @@ public class ClarinLicenseRestRepository extends DSpaceRestRepository findAll(Context context, Pageable pageable) { try { List clarinLicenseList = clarinLicenseService.findAll(context); - //zmena bitstream -// for (ClarinLicense clarinLicense: clarinLicenseList) { -// clarinLicense.setBitstreams(clarinLicenseResourceMappingService.findAllByLicenseId(context, clarinLicense.getID()).size()); -// } return converter.toRestPage(clarinLicenseList, pageable, utils.obtainProjection()); } catch (SQLException e) { throw new RuntimeException(e.getMessage(), e); diff --git a/dspace-server-webapp/src/test/java/org/dspace/app/rest/ClarinLicenseRestRepositoryIT.java b/dspace-server-webapp/src/test/java/org/dspace/app/rest/ClarinLicenseRestRepositoryIT.java index b69b6fe54bbc..f5b04fbfb2fb 100644 --- a/dspace-server-webapp/src/test/java/org/dspace/app/rest/ClarinLicenseRestRepositoryIT.java +++ b/dspace-server-webapp/src/test/java/org/dspace/app/rest/ClarinLicenseRestRepositoryIT.java @@ -9,18 +9,9 @@ import org.dspace.app.rest.test.AbstractControllerIntegrationTest; import org.dspace.builder.ClarinLicenseBuilder; import org.dspace.builder.ClarinLicenseLabelBuilder; -import org.dspace.builder.ClarinLicenseResourceMappingBuilder; -import org.dspace.builder.CollectionBuilder; -import org.dspace.builder.CommunityBuilder; -import org.dspace.builder.ItemBuilder; -import org.dspace.content.Collection; -import org.dspace.content.Item; import org.dspace.content.clarin.ClarinLicense; import org.dspace.content.clarin.ClarinLicenseLabel; -import org.dspace.content.clarin.ClarinLicenseResourceMapping; -import org.dspace.content.service.ItemService; import org.dspace.content.service.clarin.ClarinLicenseLabelService; -import org.dspace.content.service.clarin.ClarinLicenseResourceMappingService; import org.dspace.content.service.clarin.ClarinLicenseService; import org.hamcrest.Matchers; import org.junit.Assert; @@ -39,8 +30,6 @@ public class ClarinLicenseRestRepositoryIT extends AbstractControllerIntegrationTest { - private static final String AUTHOR = "Test author name"; - @Autowired ClarinLicenseService clarinLicenseService; @@ -50,15 +39,6 @@ public class ClarinLicenseRestRepositoryIT extends AbstractControllerIntegration @Autowired ClarinLicenseConverter clarinLicenseConverter; - @Autowired - ClarinLicenseResourceMappingService clarinLicenseResourceMappingService; - - private Item publicItem1; - - private Item publicItem2; - - private Item publicItem3; - ClarinLicense firstCLicense; ClarinLicense secondCLicense; @@ -112,55 +92,6 @@ public void setup() throws Exception { secondCLicense.setLicenseLabels(secondClarinLicenseLabels); clarinLicenseService.update(context, secondCLicense); - //create community for items - parentCommunity = CommunityBuilder.createCommunity(context) - .withName("Parent Community") - .build(); - - //create collection for items - Collection col1 = CollectionBuilder.createCollection(context, parentCommunity).withName("Collection 1").build(); - Collection col2 = CollectionBuilder.createCollection(context, parentCommunity).withName("Collection 2").build(); - Collection col3 = CollectionBuilder.createCollection(context, parentCommunity).withName("Collection 3").build(); - - // create two items with the first license - publicItem1 = ItemBuilder.createItem(context, col1) - .withTitle("Item 1") - .withIssueDate("2022-10-17") - .withAuthor("Smith, Donald").withAuthor("Doe, John") - .withSubject("ExtraEntry") - .build(); - ClarinLicenseResourceMapping clarinLicenseResourceMapping1 = ClarinLicenseResourceMappingBuilder - .createClarinLicenseResourceMapping(context).build(); - clarinLicenseResourceMapping1.setLicenseId(firstCLicense.getID()); - clarinLicenseResourceMapping1.setBitstreamId(publicItem1.getID()); - clarinLicenseResourceMappingService.update(context, clarinLicenseResourceMapping1); - - publicItem2 = ItemBuilder.createItem(context, col2) - .withTitle("Public item 2") - .withIssueDate("2016-02-13") - .withAuthor("Smith, Maria").withAuthor("Doe, Jane") - .withSubject("TestingForMore").withSubject("ExtraEntry") - .build(); - ClarinLicenseResourceMapping clarinLicenseResourceMapping2 = ClarinLicenseResourceMappingBuilder - .createClarinLicenseResourceMapping(context).build(); - clarinLicenseResourceMapping1.setLicenseId(firstCLicense.getID()); - clarinLicenseResourceMapping1.setBitstreamId(publicItem2.getID()); - clarinLicenseResourceMappingService.update(context, clarinLicenseResourceMapping2); - - //create item with the second license - publicItem3 = ItemBuilder.createItem(context, col3) - .withTitle("Public item 3") - .withIssueDate("2016-02-13") - .withAuthor("Smith, Maria").withAuthor("Doe, Jane") - .withSubject("AnotherTest").withSubject("TestingForMore") - .withSubject("ExtraEntry") - .build(); - ClarinLicenseResourceMapping clarinLicenseResourceMapping3 = ClarinLicenseResourceMappingBuilder - .createClarinLicenseResourceMapping(context).build(); - clarinLicenseResourceMapping1.setLicenseId(secondCLicense.getID()); - clarinLicenseResourceMapping1.setBitstreamId(publicItem3.getID()); - clarinLicenseResourceMappingService.update(context, clarinLicenseResourceMapping3); - context.restoreAuthSystemState(); } @@ -198,10 +129,5 @@ public void findAll() throws Exception { ; } - @Test - public void findAllBitstreamByLicenseId() throws Exception { - //first license is used in firstItem and secondItem - Assert.assertEquals(this.clarinLicenseResourceMappingService.findAllByLicenseId(context, firstCLicense.getID()), 2); - } } From 90f02226ee6e408fd2f0620a182d1f42c66a226f Mon Sep 17 00:00:00 2001 From: Michaela Paurikova Date: Tue, 20 Sep 2022 07:47:28 +0200 Subject: [PATCH 3/8] checkout from license administrator --- .../org/dspace/builder/AbstractBuilder.java | 9 + .../dspace/builder/ClarinLicenseBuilder.java | 19 +- .../app/rest/model/ClarinLicenseRest.java | 45 +-- .../ClarinLicenseRestRepository.java | 134 +++++++++ .../rest/ClarinLicenseRestRepositoryIT.java | 273 +++++++++++++++++- .../rest/matcher/ClarinLicenseMatcher.java | 14 + 6 files changed, 445 insertions(+), 49 deletions(-) diff --git a/dspace-api/src/test/java/org/dspace/builder/AbstractBuilder.java b/dspace-api/src/test/java/org/dspace/builder/AbstractBuilder.java index 06deacaca473..319ee7537f48 100644 --- a/dspace-api/src/test/java/org/dspace/builder/AbstractBuilder.java +++ b/dspace-api/src/test/java/org/dspace/builder/AbstractBuilder.java @@ -21,6 +21,7 @@ import org.dspace.authorize.service.ResourcePolicyService; import org.dspace.builder.util.AbstractBuilderCleanupUtil; import org.dspace.content.Bitstream; +import org.dspace.content.factory.ClarinServiceFactory; import org.dspace.content.factory.ContentServiceFactory; import org.dspace.content.service.BitstreamFormatService; import org.dspace.content.service.BitstreamService; @@ -36,6 +37,8 @@ import org.dspace.content.service.RelationshipTypeService; import org.dspace.content.service.SiteService; import org.dspace.content.service.WorkspaceItemService; +import org.dspace.content.service.clarin.ClarinLicenseLabelService; +import org.dspace.content.service.clarin.ClarinLicenseService; import org.dspace.core.Context; import org.dspace.discovery.IndexingService; import org.dspace.eperson.factory.EPersonServiceFactory; @@ -95,6 +98,8 @@ public abstract class AbstractBuilder { static ProcessService processService; static RequestItemService requestItemService; static VersioningService versioningService; + static ClarinLicenseService clarinLicenseService; + static ClarinLicenseLabelService clarinLicenseLabelService; protected Context context; @@ -151,6 +156,8 @@ public static void init() { inProgressUserService = XmlWorkflowServiceFactory.getInstance().getInProgressUserService(); poolTaskService = XmlWorkflowServiceFactory.getInstance().getPoolTaskService(); workflowItemRoleService = XmlWorkflowServiceFactory.getInstance().getWorkflowItemRoleService(); + clarinLicenseService = ClarinServiceFactory.getInstance().getClarinLicenseService(); + clarinLicenseLabelService = ClarinServiceFactory.getInstance().getClarinLicenseLabelService(); } @@ -183,6 +190,8 @@ public static void destroy() { processService = null; requestItemService = null; versioningService = null; + clarinLicenseService = null; + clarinLicenseLabelService = null; } diff --git a/dspace-api/src/test/java/org/dspace/builder/ClarinLicenseBuilder.java b/dspace-api/src/test/java/org/dspace/builder/ClarinLicenseBuilder.java index d918cf107ec7..481bbd4ca900 100644 --- a/dspace-api/src/test/java/org/dspace/builder/ClarinLicenseBuilder.java +++ b/dspace-api/src/test/java/org/dspace/builder/ClarinLicenseBuilder.java @@ -1,6 +1,7 @@ package org.dspace.builder; import org.dspace.authorize.AuthorizeException; +import org.dspace.content.WorkspaceItem; import org.dspace.content.clarin.ClarinLicense; import org.dspace.content.factory.ClarinServiceFactory; import org.dspace.content.service.clarin.ClarinLicenseService; @@ -8,11 +9,10 @@ import org.dspace.eperson.Group; import java.sql.SQLException; +import java.util.Objects; public class ClarinLicenseBuilder extends AbstractBuilder { - private ClarinLicenseService clarinLicenseService = ClarinServiceFactory.getInstance().getClarinLicenseService(); - private ClarinLicense clarinLicense; protected ClarinLicenseBuilder(Context context) { @@ -34,6 +34,21 @@ private ClarinLicenseBuilder create(final Context context) { return this; } + public static void deleteClarinLicense(Integer id) throws Exception { + if (Objects.isNull(id)) { + return; + } + try (Context c = new Context()) { + c.turnOffAuthorisationSystem(); + ClarinLicense clarinLicense = clarinLicenseService.find(c, id); + + if (clarinLicense != null) { + clarinLicenseService.delete(c, clarinLicense); + } + c.complete(); + } + } + @Override public void cleanup() throws Exception { try (Context c = new Context()) { diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/model/ClarinLicenseRest.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/model/ClarinLicenseRest.java index dcd2839ca942..77b23c8b28f5 100644 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/model/ClarinLicenseRest.java +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/model/ClarinLicenseRest.java @@ -1,21 +1,11 @@ package org.dspace.app.rest.model; -import com.fasterxml.jackson.annotation.JsonAnyGetter; -import com.fasterxml.jackson.annotation.JsonAnySetter; import com.fasterxml.jackson.annotation.JsonProperty; import org.dspace.app.rest.RestResourceController; -import org.dspace.app.rest.converter.ConverterService; -import org.dspace.app.rest.converter.DSpaceConverter; -import org.dspace.app.rest.projection.Projection; -import org.dspace.content.Collection; -import org.dspace.content.clarin.ClarinLicenseLabel; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.ArrayList; import java.util.List; -import java.util.SortedMap; -import java.util.TreeMap; @Component public class ClarinLicenseRest extends BaseObjectRest { @@ -23,11 +13,7 @@ public class ClarinLicenseRest extends BaseObjectRest { public static final String NAME = "clarinlicense"; public static final String CATEGORY = RestAddressableModel.CORE; - /** - * Map of ClarinLicenseLabelRest, it throws error if it is Array List - */ - @JsonAnySetter - private SortedMap> extendedLicenseLabelsMap = new TreeMap(); + private List extendedClarinLicenseLabels; private ClarinLicenseLabelRest clarinLicenseLabel; private String name; private String definition; @@ -38,18 +24,15 @@ public class ClarinLicenseRest extends BaseObjectRest { public ClarinLicenseRest() { } - /** - * Gets the map. - * - * @return the map of keys to ordered values. - */ - @JsonAnyGetter - public SortedMap> getExtendedLicenseLabelsMap() { - return extendedLicenseLabelsMap; + public List getExtendedClarinLicenseLabels() { + if (extendedClarinLicenseLabels == null) { + extendedClarinLicenseLabels = new ArrayList<>(); + } + return extendedClarinLicenseLabels; } - public void setExtendedLicenseLabelsMap(SortedMap> extendedLicenseLabelsMap) { - this.extendedLicenseLabelsMap = extendedLicenseLabelsMap; + public void setExtendedClarinLicenseLabels(List extendedClarinLicenseLabels) { + this.extendedClarinLicenseLabels = extendedClarinLicenseLabels; } public ClarinLicenseLabelRest getClarinLicenseLabel() { @@ -124,16 +107,4 @@ public Class getController() { public String getCategory() { return CATEGORY; } - -// @Override -// public boolean equals(Object object) { -// return object instanceof ClarinLicenseRest && ((ClarinLicenseRest) object).getMap().equals(map); -// } -// -// @Override -// public int hashCode() { -// return new HashCodeBuilder(7, 37) -// .append(this.getMap()) -// .toHashCode(); -// } } diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/ClarinLicenseRestRepository.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/ClarinLicenseRestRepository.java index a505fa68575d..845c4cf11476 100644 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/ClarinLicenseRestRepository.java +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/ClarinLicenseRestRepository.java @@ -1,18 +1,39 @@ package org.dspace.app.rest.repository; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.gson.Gson; +import org.apache.commons.lang3.StringUtils; +import org.dspace.app.rest.exception.DSpaceBadRequestException; +import org.dspace.app.rest.exception.UnprocessableEntityException; +import org.dspace.app.rest.model.ClarinLicenseLabelRest; import org.dspace.app.rest.model.ClarinLicenseRest; +import org.dspace.app.rest.model.MetadataFieldRest; +import org.dspace.authorize.AuthorizeException; +import org.dspace.content.MetadataField; +import org.dspace.content.NonUniqueMetadataException; import org.dspace.content.clarin.ClarinLicense; +import org.dspace.content.clarin.ClarinLicenseLabel; import org.dspace.content.service.clarin.ClarinLicenseService; import org.dspace.core.Context; +import org.json.JSONObject; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; +import org.springframework.data.rest.webmvc.ResourceNotFoundException; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.stereotype.Component; +import javax.servlet.http.HttpServletRequest; +import java.io.IOException; import java.sql.SQLException; +import java.util.HashSet; import java.util.List; import java.util.Objects; +import java.util.Set; + +import static java.lang.Integer.parseInt; +import static org.apache.commons.lang3.StringUtils.isBlank; @Component(ClarinLicenseRest.CATEGORY + "." + ClarinLicenseRest.NAME) public class ClarinLicenseRestRepository extends DSpaceRestRepository { @@ -37,6 +58,7 @@ public ClarinLicenseRest findOne(Context context, Integer idValue) { } @Override + @PreAuthorize("hasAuthority('ADMIN')") public Page findAll(Context context, Pageable pageable) { try { List clarinLicenseList = clarinLicenseService.findAll(context); @@ -46,9 +68,121 @@ public Page findAll(Context context, Pageable pageable) { } } + @Override + @PreAuthorize("hasAuthority('ADMIN')") + protected ClarinLicenseRest createAndReturn(Context context) + throws AuthorizeException, SQLException { + + // parse request body + ClarinLicenseRest clarinLicenseRest; + try { + clarinLicenseRest = new ObjectMapper().readValue( + getRequestService().getCurrentRequest().getHttpServletRequest().getInputStream(), + ClarinLicenseRest.class + ); + } catch (IOException excIO) { + throw new DSpaceBadRequestException("error parsing request body", excIO); + } + + // validate fields + if (isBlank(clarinLicenseRest.getName()) || isBlank(clarinLicenseRest.getDefinition())) { + throw new UnprocessableEntityException("Clarin License name, definition, " + + "license label cannot be null or empty"); + } + + // create + ClarinLicense clarinLicense; + clarinLicense = clarinLicenseService.create(context); + clarinLicense.setName(clarinLicenseRest.getName()); + clarinLicense.setLicenseLabels(this.getClarinLicenseLabels(clarinLicenseRest.getClarinLicenseLabel(), + clarinLicenseRest.getExtendedClarinLicenseLabels())); + clarinLicense.setDefinition(clarinLicenseRest.getDefinition()); + clarinLicense.setConfirmation(clarinLicenseRest.getConfirmation()); + clarinLicense.setRequiredInfo(clarinLicenseRest.getRequiredInfo()); + + clarinLicenseService.update(context, clarinLicense); + // return + return converter.toRest(clarinLicense, utils.obtainProjection()); + } + + @Override + @PreAuthorize("hasAuthority('ADMIN')") + protected ClarinLicenseRest put(Context context, HttpServletRequest request, String apiCategory, String model, + Integer id, JsonNode jsonNode) throws SQLException, AuthorizeException { + + ClarinLicenseRest clarinLicenseRest = new Gson().fromJson(jsonNode.toString(), ClarinLicenseRest.class); + + if (Objects.isNull(clarinLicenseRest)) { + throw new RuntimeException("Cannot parse ClarinLicenseRest object from request."); + } + + if (Objects.isNull(clarinLicenseRest.getClarinLicenseLabel()) || + Objects.isNull(clarinLicenseRest.getExtendedClarinLicenseLabels()) || + StringUtils.isBlank(clarinLicenseRest.getName()) || + StringUtils.isBlank(clarinLicenseRest.getDefinition())) { + throw new UnprocessableEntityException("The ClarinLicense doesn't have required properties or some " + + "some property is null."); + } + + ClarinLicense clarinLicense = clarinLicenseService.find(context, id); + if (Objects.isNull(clarinLicense)) { + throw new ResourceNotFoundException("Clarin License with id: " + id + " not found"); + } + + clarinLicense.setName(clarinLicenseRest.getName()); + clarinLicense.setRequiredInfo(clarinLicenseRest.getRequiredInfo()); + clarinLicense.setDefinition(clarinLicenseRest.getDefinition()); + clarinLicense.setConfirmation(clarinLicenseRest.getConfirmation()); + clarinLicense.setLicenseLabels(this.getClarinLicenseLabels(clarinLicenseRest.getClarinLicenseLabel(), + clarinLicenseRest.getExtendedClarinLicenseLabels())); + + clarinLicenseService.update(context, clarinLicense); + + return converter.toRest(clarinLicense, utils.obtainProjection()); + } + + @Override + @PreAuthorize("hasAuthority('ADMIN')") + protected void delete(Context context, Integer id) throws AuthorizeException { + + try { + ClarinLicense clarinLicense = clarinLicenseService.find(context, id); + + if (Objects.isNull(clarinLicense)) { + throw new ResourceNotFoundException("Clarin license with id: " + id + " not found"); + } + + clarinLicenseService.delete(context, clarinLicense); + } catch (SQLException e) { + throw new RuntimeException("Error while trying to delete " + ClarinLicenseRest.NAME + " with id: " + id, e); + } + } + @Override public Class getDomainClass() { return ClarinLicenseRest.class; } + private Set getClarinLicenseLabels(ClarinLicenseLabelRest clarinLicenseLabelRest, + List extendedClarinLicenseLabels) { + Set clarinLicenseLabels = new HashSet<>(); + + clarinLicenseLabels.add(getClarinLicenseLabelFromRest(clarinLicenseLabelRest)); + extendedClarinLicenseLabels.forEach(cllr -> { + clarinLicenseLabels.add(getClarinLicenseLabelFromRest(cllr)); + }); + + return clarinLicenseLabels; + } + + private ClarinLicenseLabel getClarinLicenseLabelFromRest(ClarinLicenseLabelRest clarinLicenseLabelRest) { + ClarinLicenseLabel clarinLicenseLabel = new ClarinLicenseLabel(); + clarinLicenseLabel.setLabel(clarinLicenseLabelRest.getLabel()); + clarinLicenseLabel.setTitle(clarinLicenseLabelRest.getTitle()); + clarinLicenseLabel.setExtended(clarinLicenseLabelRest.isExtended()); + clarinLicenseLabel.setIcon(clarinLicenseLabelRest.getIcon()); + clarinLicenseLabel.setId(clarinLicenseLabelRest.getId()); + return clarinLicenseLabel; + } + } diff --git a/dspace-server-webapp/src/test/java/org/dspace/app/rest/ClarinLicenseRestRepositoryIT.java b/dspace-server-webapp/src/test/java/org/dspace/app/rest/ClarinLicenseRestRepositoryIT.java index f5b04fbfb2fb..83c80d048c1d 100644 --- a/dspace-server-webapp/src/test/java/org/dspace/app/rest/ClarinLicenseRestRepositoryIT.java +++ b/dspace-server-webapp/src/test/java/org/dspace/app/rest/ClarinLicenseRestRepositoryIT.java @@ -1,6 +1,9 @@ package org.dspace.app.rest; +import com.fasterxml.jackson.databind.ObjectMapper; import org.dspace.app.rest.converter.ClarinLicenseConverter; +import org.dspace.app.rest.converter.ConverterService; +import org.dspace.app.rest.converter.DSpaceConverter; import org.dspace.app.rest.matcher.ClarinLicenseLabelMatcher; import org.dspace.app.rest.matcher.ClarinLicenseMatcher; import org.dspace.app.rest.model.ClarinLicenseLabelRest; @@ -9,6 +12,7 @@ import org.dspace.app.rest.test.AbstractControllerIntegrationTest; import org.dspace.builder.ClarinLicenseBuilder; import org.dspace.builder.ClarinLicenseLabelBuilder; +import org.dspace.builder.WorkspaceItemBuilder; import org.dspace.content.clarin.ClarinLicense; import org.dspace.content.clarin.ClarinLicenseLabel; import org.dspace.content.service.clarin.ClarinLicenseLabelService; @@ -18,12 +22,25 @@ import org.junit.Before; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.util.ObjectUtils; +import java.util.ArrayList; import java.util.HashSet; +import java.util.List; +import java.util.Objects; +import java.util.Set; +import java.util.SortedMap; +import java.util.TreeMap; +import java.util.concurrent.atomic.AtomicReference; +import static com.jayway.jsonpath.JsonPath.read; +import static com.jayway.jsonpath.matchers.JsonPathMatchers.hasJsonPath; import static org.hamcrest.Matchers.in; import static org.hamcrest.Matchers.is; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @@ -76,7 +93,7 @@ public void setup() throws Exception { // add ClarinLicenseLabels to the ClarinLicense HashSet firstClarinLicenseLabels = new HashSet<>(); firstClarinLicenseLabels.add(firstCLicenseLabel); - firstClarinLicenseLabels.add(secondCLicenseLabel); +// firstClarinLicenseLabels.add(secondCLicenseLabel); firstClarinLicenseLabels.add(thirdCLicenseLabel); firstCLicense.setLicenseLabels(firstClarinLicenseLabels); clarinLicenseService.update(context, firstCLicense); @@ -108,7 +125,8 @@ public void clarinLicensesAndLicenseLabelsAreInitialized() throws Exception { @Test public void findAll() throws Exception { - getClient().perform(get("/api/core/clarinlicenses")) + String authTokenAdmin = getAuthToken(admin.getEmail(), password); + getClient(authTokenAdmin).perform(get("/api/core/clarinlicenses")) .andExpect(status().isOk()) .andExpect(content().contentType(contentType)) .andExpect(jsonPath("$._embedded.clarinlicenses", Matchers.hasItem( @@ -117,17 +135,252 @@ public void findAll() throws Exception { .andExpect(jsonPath("$._embedded.clarinlicenses", Matchers.hasItem( ClarinLicenseMatcher.matchClarinLicense(secondCLicense)) )) - .andExpect(jsonPath("$._embedded.clarinlicenses[0].clarinLicenseLabel", Matchers.hasItem( - ClarinLicenseLabelMatcher.matchClarinLicenseLabel(firstCLicense.getLicenseLabels().get(2))) + .andExpect(jsonPath("$._embedded.clarinlicenses[0].clarinLicenseLabel", Matchers.is( + ClarinLicenseLabelMatcher.matchClarinLicenseLabel( + Objects.requireNonNull(getNonExtendedLicenseLabel(firstCLicense.getLicenseLabels())))) )) - .andExpect(jsonPath("$._embedded.clarinlicenses[0].extendedClarinLicenseLabels", Matchers.hasValue(firstCLicense.getLicenseLabels().get(1)))) -// .andExpect(jsonPath("$._embedded.clarinlicenses[0].extendedClarinLicenseLabels", Matchers.hasItem( -// ClarinLicenseLabelMatcher.matchClarinLicenseLabel(firstCLicense.getLicenseLabels().get(0))) -// )) -// .andExpect(jsonPath("$._links.self.href", -// Matchers.containsString("/api/core/clarinlicenses"))) + .andExpect(jsonPath("$._embedded.clarinlicenses[0].extendedClarinLicenseLabels", + Matchers.hasItem( + ClarinLicenseLabelMatcher.matchClarinLicenseLabel( + Objects.requireNonNull(getExtendedLicenseLabels( + firstCLicense.getLicenseLabels()))) + ))) + .andExpect(jsonPath("$._links.self.href", + Matchers.containsString("/api/core/clarinlicenses"))) ; } + @Test + public void create() throws Exception { + ClarinLicenseRest clarinLicenseRest = new ClarinLicenseRest(); + clarinLicenseRest.setName("name"); + clarinLicenseRest.setBitstreams(0); + clarinLicenseRest.setConfirmation(4); + clarinLicenseRest.setRequiredInfo("Not required"); + clarinLicenseRest.setDefinition("definition"); + clarinLicenseConverter.setExtendedClarinLicenseLabels(clarinLicenseRest, firstCLicense.getLicenseLabels(), + Projection.DEFAULT); + clarinLicenseConverter.setClarinLicenseLabel(clarinLicenseRest, firstCLicense.getLicenseLabels(), + Projection.DEFAULT); + + // id of created clarin license + AtomicReference idRef = new AtomicReference<>(); + String authTokenAdmin = getAuthToken(admin.getEmail(), password); + try { + getClient(authTokenAdmin).perform(post("/api/core/clarinlicenses") + .content(new ObjectMapper().writeValueAsBytes(clarinLicenseRest)) + .contentType(org.springframework.http.MediaType.APPLICATION_JSON)) + .andExpect(status().isCreated()) + .andExpect(jsonPath("$.name", is(clarinLicenseRest.getName()))) + .andExpect(jsonPath("$.definition", + is(clarinLicenseRest.getDefinition()))) + .andExpect(jsonPath("$.confirmation", + is(clarinLicenseRest.getConfirmation()))) + .andExpect(jsonPath("$.requiredInfo", + is(clarinLicenseRest.getRequiredInfo()))) + .andExpect(jsonPath("$.bitstreams", + is(clarinLicenseRest.getBitstreams()))) + .andExpect(jsonPath("$.type", + is(ClarinLicenseRest.NAME))) + + .andExpect(jsonPath("$.clarinLicenseLabel.label", + is(clarinLicenseRest.getClarinLicenseLabel().getLabel()))) + .andExpect(jsonPath("$.clarinLicenseLabel.title", + is(clarinLicenseRest.getClarinLicenseLabel().getTitle()))) + .andExpect(jsonPath("$.clarinLicenseLabel.extended", + is(clarinLicenseRest.getClarinLicenseLabel().isExtended()))) + .andExpect(jsonPath("$.clarinLicenseLabel.type", + is(ClarinLicenseLabelRest.NAME))) + + .andExpect(jsonPath("$.extendedClarinLicenseLabels[0].label", + is(clarinLicenseRest.getExtendedClarinLicenseLabels().get(0).getLabel()))) + .andExpect(jsonPath("$.extendedClarinLicenseLabels[0].title", + is(clarinLicenseRest.getExtendedClarinLicenseLabels().get(0).getTitle()))) + .andExpect(jsonPath("$.extendedClarinLicenseLabels[0].extended", + is(clarinLicenseRest.getExtendedClarinLicenseLabels().get(0).isExtended()))) + .andExpect(jsonPath("$.extendedClarinLicenseLabels[0].type", + is(ClarinLicenseLabelRest.NAME))) + .andDo(result -> idRef.set(read(result.getResponse().getContentAsString(), + "$.id"))); + } finally { + if (Objects.nonNull(idRef.get())) { + // remove created clarin license + ClarinLicenseBuilder.deleteClarinLicense(idRef.get()); + } + } + } + + // Edit + @Test + public void update() throws Exception { + context.turnOffAuthorisationSystem(); + // clarin license to update + ClarinLicense clarinLicense = ClarinLicenseBuilder.createClarinLicense(context).build(); + clarinLicense.setName("default name"); + clarinLicense.setDefinition("default definition"); + clarinLicense.setConfirmation(0); + clarinLicense.setRequiredInfo("default info"); + + Set clarinLicenseLabels = new HashSet<>(); + clarinLicenseLabels.add(firstCLicenseLabel); + clarinLicenseLabels.add(secondCLicenseLabel); + clarinLicense.setLicenseLabels(clarinLicenseLabels); + + // clarin license with updated values + ClarinLicense clarinLicenseUpdated = ClarinLicenseBuilder.createClarinLicense(context).build(); + clarinLicenseUpdated.setName("updated name"); + clarinLicenseUpdated.setDefinition("updated definition"); + clarinLicenseUpdated.setConfirmation(4); + clarinLicenseUpdated.setRequiredInfo("updated info"); + + Set clarinLicenseLabelUpdated = new HashSet<>(); + clarinLicenseLabelUpdated.add(firstCLicenseLabel); + clarinLicenseLabelUpdated.add(thirdCLicenseLabel); + clarinLicenseUpdated.setLicenseLabels(clarinLicenseLabelUpdated); + context.restoreAuthSystemState(); + + ClarinLicenseRest clarinLicenseRest = clarinLicenseConverter.convert(clarinLicenseUpdated, Projection.DEFAULT); + + String authTokenAdmin = getAuthToken(admin.getEmail(), password); + getClient(authTokenAdmin).perform(get("/api/core/clarinlicenses/" + clarinLicense.getID())) + .andExpect(status().isOk()); + + getClient(authTokenAdmin).perform(put("/api/core/clarinlicenses/" + clarinLicense.getID()) + .content(new ObjectMapper().writeValueAsBytes(clarinLicenseRest)) + .contentType(org.springframework.http.MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()); + + getClient(authTokenAdmin).perform(get("/api/core/clarinlicenses/" + clarinLicense.getID())) + .andExpect(status().isOk()) + .andExpect(jsonPath("$", Matchers.is( + ClarinLicenseMatcher.matchClarinLicenseWithoutId(clarinLicenseUpdated)) + )); + } + + // 403 + @Test + public void forbiddenUpdateClarinLicense() throws Exception { + context.turnOffAuthorisationSystem(); + // clarin license to update + ClarinLicense clarinLicense = ClarinLicenseBuilder.createClarinLicense(context).build(); + + clarinLicense.setName("default name"); + clarinLicense.setDefinition("default definition"); + clarinLicense.setConfirmation(0); + clarinLicense.setRequiredInfo("default info"); + + Set clarinLicenseLabels = new HashSet<>(); + clarinLicenseLabels.add(firstCLicenseLabel); + clarinLicenseLabels.add(thirdCLicenseLabel); + clarinLicense.setLicenseLabels(clarinLicenseLabels); + context.restoreAuthSystemState(); + + ClarinLicenseRest clarinLicenseRest = clarinLicenseConverter.convert(clarinLicense, Projection.DEFAULT); + String authTokenUser = getAuthToken(eperson.getEmail(), password); + getClient(authTokenUser).perform(delete("/api/core/clarinlicenses/" + clarinLicense.getID()) + .content(new ObjectMapper().writeValueAsBytes(clarinLicenseRest)) + .contentType(org.springframework.http.MediaType.APPLICATION_JSON)) + .andExpect(status().isForbidden()) + ; + } + // 404 + @Test + public void notFoundUpdateClarinLicense() throws Exception { + context.turnOffAuthorisationSystem(); + // clarin license to update + ClarinLicense clarinLicense = ClarinLicenseBuilder.createClarinLicense(context).build(); + + clarinLicense.setName("default name"); + clarinLicense.setDefinition("default definition"); + clarinLicense.setConfirmation(0); + clarinLicense.setRequiredInfo("default info"); + + Set clarinLicenseLabels = new HashSet<>(); + clarinLicenseLabels.add(firstCLicenseLabel); + clarinLicenseLabels.add(thirdCLicenseLabel); + clarinLicense.setLicenseLabels(clarinLicenseLabels); + context.restoreAuthSystemState(); + + ClarinLicenseRest clarinLicenseRest = clarinLicenseConverter.convert(clarinLicense, Projection.DEFAULT); + + String authTokenAdmin = getAuthToken(admin.getEmail(), password); + getClient(authTokenAdmin).perform(put("/api/core/clarinlicenses/" + clarinLicense.getID() + "124679") + .content(new ObjectMapper().writeValueAsBytes(clarinLicenseRest)) + .contentType(org.springframework.http.MediaType.APPLICATION_JSON)) + .andExpect(status().isNotFound()) + ; + } + + // 204 + @Test + public void deleteClarinLicense() throws Exception { + context.turnOffAuthorisationSystem(); + ClarinLicense clarinLicense = ClarinLicenseBuilder.createClarinLicense(context).build(); + context.restoreAuthSystemState(); + + String authTokenAdmin = getAuthToken(admin.getEmail(), password); + getClient(authTokenAdmin).perform(get("/api/core/clarinlicenses/" + clarinLicense.getID())) + .andExpect(status().isOk()); + + getClient(authTokenAdmin).perform(delete("/api/core/clarinlicenses/" + clarinLicense.getID())) + .andExpect(status().isNoContent()); + + getClient(authTokenAdmin).perform(get("/api/core/clarinlicenses/" + clarinLicense.getID())) + .andExpect(status().isNotFound()); + } + + // 401 + @Test + public void unauthorizedDeleteClarinLicense() throws Exception { + context.turnOffAuthorisationSystem(); + ClarinLicense clarinLicense = ClarinLicenseBuilder.createClarinLicense(context).build(); + context.restoreAuthSystemState(); + + getClient().perform(delete("/api/core/clarinlicenses/" + clarinLicense.getID())) + .andExpect(status().isUnauthorized()) + ; + } + + // 403 + @Test + public void forbiddenDeleteClarinLicense() throws Exception { + context.turnOffAuthorisationSystem(); + ClarinLicense clarinLicense = ClarinLicenseBuilder.createClarinLicense(context).build(); + context.restoreAuthSystemState(); + + String authTokenUser = getAuthToken(eperson.getEmail(), password); + getClient(authTokenUser).perform(delete("/api/core/clarinlicenses/" + clarinLicense.getID())) + .andExpect(status().isForbidden()) + ; + } + // 404 + @Test + public void notFoundDeleteClarinLicense() throws Exception { + String authTokenAdmin = getAuthToken(admin.getEmail(), password); + getClient(authTokenAdmin).perform(delete("/api/core/clarinlicenses/" + 1239990)) + .andExpect(status().isNotFound()) + ; + } + + private ClarinLicenseLabel getNonExtendedLicenseLabel(List clarinLicenseLabelList) { + for (ClarinLicenseLabel clarinLicenseLabel : clarinLicenseLabelList) { + if (clarinLicenseLabel.isExtended()) { + continue; + } + return clarinLicenseLabel; + } + return null; + } + + private ClarinLicenseLabel getExtendedLicenseLabels(List clarinLicenseLabelList) { + for (ClarinLicenseLabel clarinLicenseLabel : clarinLicenseLabelList) { + if (!clarinLicenseLabel.isExtended()) { + continue; + } + return clarinLicenseLabel; + } + return null; + } + } diff --git a/dspace-server-webapp/src/test/java/org/dspace/app/rest/matcher/ClarinLicenseMatcher.java b/dspace-server-webapp/src/test/java/org/dspace/app/rest/matcher/ClarinLicenseMatcher.java index f9fab57009de..8ef6e5b3964d 100644 --- a/dspace-server-webapp/src/test/java/org/dspace/app/rest/matcher/ClarinLicenseMatcher.java +++ b/dspace-server-webapp/src/test/java/org/dspace/app/rest/matcher/ClarinLicenseMatcher.java @@ -31,4 +31,18 @@ public static Matcher matchClarinLicense(ClarinLicense clarinLic Matchers.containsString("/api/core/clarinlicenses/" + clarinLicense.getID())) ); } + + // when the Clarin License is updated + public static Matcher matchClarinLicenseWithoutId(ClarinLicense clarinLicense) { + return allOf( + hasJsonPath("$.name", is(clarinLicense.getName())), + hasJsonPath("$.definition", is(clarinLicense.getDefinition())), + hasJsonPath("$.type", is(ClarinLicenseRest.NAME)), + hasJsonPath("$.confirmation", is(clarinLicense.getConfirmation())), + hasJsonPath("$.requiredInfo", is(clarinLicense.getRequiredInfo())), + hasJsonPath("$.bitstreams", Matchers.not(Matchers.empty())), + hasJsonPath("$.clarinLicenseLabel", Matchers.not(Matchers.empty())), + hasJsonPath("$.extendedClarinLicenseLabels", Matchers.not(Matchers.empty())) + ); + } } From 74e8f194a1e8e2ab7338496ce764fc4e2df253f8 Mon Sep 17 00:00:00 2001 From: Michaela Paurikova Date: Thu, 22 Sep 2022 14:56:06 +0200 Subject: [PATCH 4/8] tests not working --- .../app/rest/converter/ClarinLicenseConverter.java | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/converter/ClarinLicenseConverter.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/converter/ClarinLicenseConverter.java index e6970551dc9f..31c78b0b893c 100644 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/converter/ClarinLicenseConverter.java +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/converter/ClarinLicenseConverter.java @@ -66,16 +66,8 @@ public void setExtendedClarinLicenseLabels(ClarinLicenseRest licenseRest, List cLicenseLabels, From 24eb8bc40b5c70a61e96bcbb0f383c951cbae6fe Mon Sep 17 00:00:00 2001 From: Michaela Paurikova Date: Fri, 23 Sep 2022 08:32:40 +0200 Subject: [PATCH 5/8] checkout with feature/lf-4-license-administrator --- .../dspace/content/clarin/ClarinLicense.java | 39 +++--- .../content/clarin/ClarinLicenseLabel.java | 28 ++-- ...ava => ClarinLicenseLabelServiceImpl.java} | 53 ++++++-- .../clarin/ClarinLicenseResourceMapping.java | 9 +- .../clarin/ClarinLicenseServiceImpl.java | 40 +++--- .../content/dao/clarin/ClarinLicenseDAO.java | 15 +++ .../dao/clarin/ClarinLicenseLabelDAO.java | 15 +++ .../dao/impl/clarin/ClarinLicenseDAOImpl.java | 14 ++ .../clarin/ClarinLicenseLabelDAOImpl.java | 17 ++- .../content/factory/ClarinServiceFactory.java | 15 ++- .../factory/ClarinServiceFactoryImpl.java | 18 ++- .../clarin/ClarinLicenseLabelService.java | 69 +++++++++- .../service/clarin/ClarinLicenseService.java | 76 ++++++++++- .../org/dspace/builder/AbstractBuilder.java | 4 + .../dspace/builder/ClarinLicenseBuilder.java | 21 ++- .../builder/ClarinLicenseLabelBuilder.java | 23 ++-- .../ClarinLicenseResourceMappingBuilder.java | 3 - .../converter/ClarinLicenseConverter.java | 53 ++++---- .../ClarinLicenseLabelConverter.java | 18 ++- .../rest/model/ClarinLicenseLabelRest.java | 12 ++ .../app/rest/model/ClarinLicenseRest.java | 29 ++-- .../ClarinLicenseLabelRestRepository.java | 74 ++++++++++- .../ClarinLicenseRestRepository.java | 39 +++--- .../ClarinLicenseLabelRestRepositoryIT.java | 94 +++++++++++++ .../rest/ClarinLicenseRestRepositoryIT.java | 125 ++++++++++++++---- .../matcher/ClarinLicenseLabelMatcher.java | 32 ++++- .../rest/matcher/ClarinLicenseMatcher.java | 21 ++- dspace/config/spring/api/core-services.xml | 2 +- 28 files changed, 771 insertions(+), 187 deletions(-) rename dspace-api/src/main/java/org/dspace/content/clarin/{ClarinClarinLicenseLabelServiceImpl.java => ClarinLicenseLabelServiceImpl.java} (64%) create mode 100644 dspace-server-webapp/src/test/java/org/dspace/app/rest/ClarinLicenseLabelRestRepositoryIT.java diff --git a/dspace-api/src/main/java/org/dspace/content/clarin/ClarinLicense.java b/dspace-api/src/main/java/org/dspace/content/clarin/ClarinLicense.java index cdd7284ec77b..9a773a8e0413 100644 --- a/dspace-api/src/main/java/org/dspace/content/clarin/ClarinLicense.java +++ b/dspace-api/src/main/java/org/dspace/content/clarin/ClarinLicense.java @@ -1,7 +1,16 @@ +/** + * 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.clarin; -import org.dspace.core.ReloadableEntity; - +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Set; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; @@ -14,17 +23,23 @@ import javax.persistence.ManyToMany; import javax.persistence.SequenceGenerator; import javax.persistence.Table; -import java.util.Arrays; -import java.util.HashSet; -import java.util.List; -import java.util.Set; +import org.dspace.core.ReloadableEntity; + +/** + * Class representing a clarin license in DSpace. + * Clarin License is license for the bitstreams of the item. The item could have only one type of the Clarin License. + * The Clarin License is selected in the submission process. + * Admin could manage Clarin Licenses in the License Administration page. + * + * @author Milan Majchrak (milan.majchrak at dataquest.sk) + */ @Entity @Table(name = "license_definition") public class ClarinLicense implements ReloadableEntity { @Id - @Column(name="license_id") + @Column(name = "license_id") @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "license_definition_license_id_seq") @SequenceGenerator(name = "license_definition_license_id_seq", sequenceName = "license_definition_license_id_seq", allocationSize = 1) @@ -35,7 +50,7 @@ public class ClarinLicense implements ReloadableEntity { name = "license_label_extended_mapping", joinColumns = @JoinColumn(name = "license_id"), inverseJoinColumns = @JoinColumn(name = "label_id")) - Set clarinLicenseLabels = new HashSet<>(); + Set clarinLicenseLabels = new HashSet<>();; // @Column(name = "eperson_id") // private Integer epersonId; @@ -55,10 +70,6 @@ public class ClarinLicense implements ReloadableEntity { public ClarinLicense() { } - public void setClarinLicenseLabels(Set clarinLicenseLabels) { - this.clarinLicenseLabels = clarinLicenseLabels; - } - public String getName() { return name; } @@ -67,10 +78,6 @@ public void setName(String name) { this.name = name; } - public Integer getId() { - return id; - } - public void setId(Integer id) { this.id = id; } diff --git a/dspace-api/src/main/java/org/dspace/content/clarin/ClarinLicenseLabel.java b/dspace-api/src/main/java/org/dspace/content/clarin/ClarinLicenseLabel.java index f83533be59b3..38312568f7f9 100644 --- a/dspace-api/src/main/java/org/dspace/content/clarin/ClarinLicenseLabel.java +++ b/dspace-api/src/main/java/org/dspace/content/clarin/ClarinLicenseLabel.java @@ -1,7 +1,14 @@ +/** + * 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.clarin; -import org.dspace.core.ReloadableEntity; - +import java.util.ArrayList; +import java.util.List; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; @@ -10,15 +17,22 @@ import javax.persistence.ManyToMany; import javax.persistence.SequenceGenerator; import javax.persistence.Table; -import java.util.ArrayList; -import java.util.List; +import org.dspace.core.ReloadableEntity; + +/** + * Class representing a clarin license label of the clarin license. The clarin license could have one + * non-extended license label and multiple extended license labels. + * The license label could be defined in the License Administration table. + * + * @author Milan Majchrak (milan.majchrak at dataquest.sk) + */ @Entity @Table(name = "license_label") public class ClarinLicenseLabel implements ReloadableEntity { @Id - @Column(name="label_id") + @Column(name = "label_id") @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "license_label_label_id_seq") @SequenceGenerator(name = "license_label_label_id_seq", sequenceName = "license_label_label_id_seq", allocationSize = 1) @@ -42,10 +56,6 @@ public class ClarinLicenseLabel implements ReloadableEntity { public ClarinLicenseLabel() { } - public Integer getId() { - return id; - } - public void setId(Integer id) { this.id = id; } diff --git a/dspace-api/src/main/java/org/dspace/content/clarin/ClarinClarinLicenseLabelServiceImpl.java b/dspace-api/src/main/java/org/dspace/content/clarin/ClarinLicenseLabelServiceImpl.java similarity index 64% rename from dspace-api/src/main/java/org/dspace/content/clarin/ClarinClarinLicenseLabelServiceImpl.java rename to dspace-api/src/main/java/org/dspace/content/clarin/ClarinLicenseLabelServiceImpl.java index 340eefa170ae..8bd684873afb 100644 --- a/dspace-api/src/main/java/org/dspace/content/clarin/ClarinClarinLicenseLabelServiceImpl.java +++ b/dspace-api/src/main/java/org/dspace/content/clarin/ClarinLicenseLabelServiceImpl.java @@ -1,5 +1,16 @@ +/** + * 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.clarin; +import java.sql.SQLException; +import java.util.List; +import java.util.Objects; + import org.apache.commons.lang.NullArgumentException; import org.dspace.authorize.AuthorizeException; import org.dspace.authorize.service.AuthorizeService; @@ -12,10 +23,15 @@ import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; -import java.sql.SQLException; -import java.util.Objects; - -public class ClarinClarinLicenseLabelServiceImpl implements ClarinLicenseLabelService { +/** + * Service implementation for the Clarin License Label object. + * This class is responsible for all business logic calls for the Clarin License Label object and is autowired + * by spring. + * This class should never be accessed directly. + * + * @author Milan Majchrak (milan.majchrak at dataquest.sk) + */ +public class ClarinLicenseLabelServiceImpl implements ClarinLicenseLabelService { private static final Logger log = LoggerFactory.getLogger(ClarinLicenseServiceImpl.class); @@ -57,19 +73,40 @@ public ClarinLicenseLabel find(Context context, int valueId) throws SQLException } @Override - public void delete(Context context, ClarinLicenseLabel license) throws SQLException { + public List findAll(Context context) throws SQLException, AuthorizeException { + if (!authorizeService.isAdmin(context)) { + throw new AuthorizeException( + "You must be an admin to create an Clarin License Label"); + } + + return clarinLicenseLabelDAO.findAll(context, ClarinLicenseLabel.class); + } + + @Override + public void delete(Context context, ClarinLicenseLabel license) throws SQLException, AuthorizeException { + if (!authorizeService.isAdmin(context)) { + throw new AuthorizeException( + "You must be an admin to create an Clarin License Label"); + } + clarinLicenseLabelDAO.delete(context, license); } @Override - public void update(Context context, ClarinLicenseLabel newClarinLicenseLabel) throws SQLException { + public void update(Context context, ClarinLicenseLabel newClarinLicenseLabel) throws SQLException, + AuthorizeException { + if (!authorizeService.isAdmin(context)) { + throw new AuthorizeException( + "You must be an admin to create an Clarin License Label"); + } + if (Objects.isNull(newClarinLicenseLabel)) { throw new NullArgumentException("Cannot update licenseLabel because the clarinLicenseLabel is null"); } - ClarinLicenseLabel foundClarinLicenseLabel = find(context, newClarinLicenseLabel.getId()); + ClarinLicenseLabel foundClarinLicenseLabel = find(context, newClarinLicenseLabel.getID()); if (Objects.isNull(foundClarinLicenseLabel)) { - throw new ObjectNotFoundException(newClarinLicenseLabel.getId(), "Cannot update the clarinLicenseLabel " + + throw new ObjectNotFoundException(newClarinLicenseLabel.getID(), "Cannot update the clarinLicenseLabel " + "because the licenseLabel wasn't found in the database."); } diff --git a/dspace-api/src/main/java/org/dspace/content/clarin/ClarinLicenseResourceMapping.java b/dspace-api/src/main/java/org/dspace/content/clarin/ClarinLicenseResourceMapping.java index 2b93541b17fd..62c669d36f21 100644 --- a/dspace-api/src/main/java/org/dspace/content/clarin/ClarinLicenseResourceMapping.java +++ b/dspace-api/src/main/java/org/dspace/content/clarin/ClarinLicenseResourceMapping.java @@ -2,13 +2,7 @@ import org.dspace.core.ReloadableEntity; -import javax.persistence.Column; -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; -import javax.persistence.SequenceGenerator; -import javax.persistence.Table; +import javax.persistence.*; import java.util.UUID; @Entity @@ -21,7 +15,6 @@ public class ClarinLicenseResourceMapping implements ReloadableEntity { @SequenceGenerator(name = "license_resource_mapping_mapping_id_seq", sequenceName = "license_resource_mapping_mapping_id_seq", allocationSize = 1) private Integer id; - @Column(name = "bitstream_uuid") private UUID bitstreamUuid = null; diff --git a/dspace-api/src/main/java/org/dspace/content/clarin/ClarinLicenseServiceImpl.java b/dspace-api/src/main/java/org/dspace/content/clarin/ClarinLicenseServiceImpl.java index 5b502c601b26..7965b56b5d65 100644 --- a/dspace-api/src/main/java/org/dspace/content/clarin/ClarinLicenseServiceImpl.java +++ b/dspace-api/src/main/java/org/dspace/content/clarin/ClarinLicenseServiceImpl.java @@ -1,26 +1,21 @@ package org.dspace.content.clarin; +import java.sql.SQLException; +import java.util.List; +import java.util.Objects; + import org.apache.commons.lang.NullArgumentException; -import org.checkerframework.checker.units.qual.C; import org.dspace.authorize.AuthorizeException; import org.dspace.authorize.service.AuthorizeService; import org.dspace.content.dao.clarin.ClarinLicenseDAO; import org.dspace.content.service.clarin.ClarinLicenseService; -import org.dspace.core.Constants; import org.dspace.core.Context; import org.dspace.core.LogHelper; -import org.dspace.eperson.Group; -import org.dspace.eperson.GroupServiceImpl; -import org.dspace.event.Event; import org.hibernate.ObjectNotFoundException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; -import java.sql.SQLException; -import java.util.List; -import java.util.Objects; - public class ClarinLicenseServiceImpl implements ClarinLicenseService { private static final Logger log = LoggerFactory.getLogger(ClarinLicenseServiceImpl.class); @@ -63,26 +58,41 @@ public ClarinLicense find(Context context, int valueId) throws SQLException { } @Override - public List findAll(Context context) throws SQLException { + public List findAll(Context context) throws SQLException, AuthorizeException { + if (!authorizeService.isAdmin(context)) { + throw new AuthorizeException( + "You must be an admin to create an Clarin License"); + } + return clarinLicenseDAO.findAll(context, ClarinLicense.class); } @Override - public void delete(Context context, ClarinLicense clarinLicense) throws SQLException { + public void delete(Context context, ClarinLicense clarinLicense) throws SQLException, AuthorizeException { + if (!authorizeService.isAdmin(context)) { + throw new AuthorizeException( + "You must be an admin to create an Clarin License"); + } + clarinLicenseDAO.delete(context, clarinLicense); } @Override - public void update(Context context, ClarinLicense newClarinLicense) throws SQLException { + public void update(Context context, ClarinLicense newClarinLicense) throws SQLException, AuthorizeException { + if (!authorizeService.isAdmin(context)) { + throw new AuthorizeException( + "You must be an admin to create an Clarin License"); + } + if (Objects.isNull(newClarinLicense)) { throw new NullArgumentException("Cannot update clarin license because the new clarin license is null"); } - ClarinLicense foundClarinLicense = find(context, newClarinLicense.getId()); + ClarinLicense foundClarinLicense = find(context, newClarinLicense.getID()); if (Objects.isNull(foundClarinLicense)) { - throw new ObjectNotFoundException(newClarinLicense.getId(), "Cannot update the license because the clarin license wasn't found " + - "in the database."); + throw new ObjectNotFoundException(newClarinLicense.getID(), + "Cannot update the license because the clarin license wasn't found in the database."); } clarinLicenseDAO.save(context, newClarinLicense); diff --git a/dspace-api/src/main/java/org/dspace/content/dao/clarin/ClarinLicenseDAO.java b/dspace-api/src/main/java/org/dspace/content/dao/clarin/ClarinLicenseDAO.java index 850ceb9c5fa9..f9e2b0a18c7a 100644 --- a/dspace-api/src/main/java/org/dspace/content/dao/clarin/ClarinLicenseDAO.java +++ b/dspace-api/src/main/java/org/dspace/content/dao/clarin/ClarinLicenseDAO.java @@ -1,7 +1,22 @@ +/** + * 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.clarin; import org.dspace.content.clarin.ClarinLicense; import org.dspace.core.GenericDAO; +/** + * Database Access Object interface class for the Clarin License object. + * The implementation of this class is responsible for all database calls for the Clarin License object + * and is autowired by spring This class should only be accessed from a single service and should never be exposed + * outside the API + * + * @author Milan Majchrak (milan.majchrak at dataquest.sk) + */ public interface ClarinLicenseDAO extends GenericDAO { } diff --git a/dspace-api/src/main/java/org/dspace/content/dao/clarin/ClarinLicenseLabelDAO.java b/dspace-api/src/main/java/org/dspace/content/dao/clarin/ClarinLicenseLabelDAO.java index 1b58fc3e7cb7..1abd25b7a96a 100644 --- a/dspace-api/src/main/java/org/dspace/content/dao/clarin/ClarinLicenseLabelDAO.java +++ b/dspace-api/src/main/java/org/dspace/content/dao/clarin/ClarinLicenseLabelDAO.java @@ -1,7 +1,22 @@ +/** + * 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.clarin; import org.dspace.content.clarin.ClarinLicenseLabel; import org.dspace.core.GenericDAO; +/** + * Database Access Object interface class for the Clarin License Label object. + * The implementation of this class is responsible for all database calls for the Clarin License Label object + * and is autowired by spring This class should only be accessed from a single service and should never be exposed + * outside the API + * + * @author Milan Majchrak (milan.majchrak at dataquest.sk) + */ public interface ClarinLicenseLabelDAO extends GenericDAO { } diff --git a/dspace-api/src/main/java/org/dspace/content/dao/impl/clarin/ClarinLicenseDAOImpl.java b/dspace-api/src/main/java/org/dspace/content/dao/impl/clarin/ClarinLicenseDAOImpl.java index 8ace711fc70f..56e7b8e14715 100644 --- a/dspace-api/src/main/java/org/dspace/content/dao/impl/clarin/ClarinLicenseDAOImpl.java +++ b/dspace-api/src/main/java/org/dspace/content/dao/impl/clarin/ClarinLicenseDAOImpl.java @@ -1,9 +1,23 @@ +/** + * 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.clarin; import org.dspace.content.clarin.ClarinLicense; import org.dspace.content.dao.clarin.ClarinLicenseDAO; import org.dspace.core.AbstractHibernateDAO; +/** + * Hibernate implementation of the Database Access Object interface class for the Clarin License object. + * This class is responsible for all database calls for the Clarin License object and is autowired by spring + * This class should never be accessed directly. + * + * @author Milan Majchrak (milan.majchrak at dataquest.sk) + */ public class ClarinLicenseDAOImpl extends AbstractHibernateDAO implements ClarinLicenseDAO { protected ClarinLicenseDAOImpl() { super(); diff --git a/dspace-api/src/main/java/org/dspace/content/dao/impl/clarin/ClarinLicenseLabelDAOImpl.java b/dspace-api/src/main/java/org/dspace/content/dao/impl/clarin/ClarinLicenseLabelDAOImpl.java index a8334f892548..1bf2179a3935 100644 --- a/dspace-api/src/main/java/org/dspace/content/dao/impl/clarin/ClarinLicenseLabelDAOImpl.java +++ b/dspace-api/src/main/java/org/dspace/content/dao/impl/clarin/ClarinLicenseLabelDAOImpl.java @@ -1,10 +1,25 @@ +/** + * 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.clarin; import org.dspace.content.clarin.ClarinLicenseLabel; import org.dspace.content.dao.clarin.ClarinLicenseLabelDAO; import org.dspace.core.AbstractHibernateDAO; -public class ClarinLicenseLabelDAOImpl extends AbstractHibernateDAO implements ClarinLicenseLabelDAO { +/** + * Hibernate implementation of the Database Access Object interface class for the Clarin License Label object. + * This class is responsible for all database calls for the Clarin License Label object and is autowired by spring + * This class should never be accessed directly. + * + * @author Milan Majchrak (milan.majchrak at dataquest.sk) + */ +public class ClarinLicenseLabelDAOImpl extends AbstractHibernateDAO + implements ClarinLicenseLabelDAO { protected ClarinLicenseLabelDAOImpl() { super(); } diff --git a/dspace-api/src/main/java/org/dspace/content/factory/ClarinServiceFactory.java b/dspace-api/src/main/java/org/dspace/content/factory/ClarinServiceFactory.java index 1f77dab1c400..e4a54ecb8d70 100644 --- a/dspace-api/src/main/java/org/dspace/content/factory/ClarinServiceFactory.java +++ b/dspace-api/src/main/java/org/dspace/content/factory/ClarinServiceFactory.java @@ -1,3 +1,10 @@ +/** + * 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.factory; import org.dspace.content.service.clarin.ClarinLicenseLabelService; @@ -5,13 +12,19 @@ import org.dspace.content.service.clarin.ClarinLicenseService; import org.dspace.services.factory.DSpaceServicesFactory; +/** + * Abstract factory to get services for the clarin package, use ClarinServiceFactory.getInstance() to retrieve an + * implementation + * + * @author Milan Majchrak (milan.majchrak at dataquest.sk) + */ public abstract class ClarinServiceFactory { public abstract ClarinLicenseService getClarinLicenseService(); public abstract ClarinLicenseLabelService getClarinLicenseLabelService(); - public abstract ClarinLicenseResourceMappingService getClarinResourceMappingService(); + public abstract ClarinLicenseResourceMappingService getClarinLicenseResourceMappingService(); public static ClarinServiceFactory getInstance() { return DSpaceServicesFactory.getInstance().getServiceManager() diff --git a/dspace-api/src/main/java/org/dspace/content/factory/ClarinServiceFactoryImpl.java b/dspace-api/src/main/java/org/dspace/content/factory/ClarinServiceFactoryImpl.java index c8cab1a6de49..af42bc5f0607 100644 --- a/dspace-api/src/main/java/org/dspace/content/factory/ClarinServiceFactoryImpl.java +++ b/dspace-api/src/main/java/org/dspace/content/factory/ClarinServiceFactoryImpl.java @@ -1,12 +1,23 @@ +/** + * 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.factory; -import org.dspace.content.clarin.ClarinLicenseResourceMapping; -import org.dspace.content.service.BitstreamFormatService; import org.dspace.content.service.clarin.ClarinLicenseLabelService; import org.dspace.content.service.clarin.ClarinLicenseResourceMappingService; import org.dspace.content.service.clarin.ClarinLicenseService; import org.springframework.beans.factory.annotation.Autowired; +/** + * Factory implementation to get services for the clarin package, use ClarinServiceFactory.getInstance() + * to retrieve an implementation + * + * @author Milan Majchrak (milan.majchrak at dataquest.sk) + */ public class ClarinServiceFactoryImpl extends ClarinServiceFactory { @Autowired(required = true) @@ -18,7 +29,6 @@ public class ClarinServiceFactoryImpl extends ClarinServiceFactory { @Autowired(required = true) private ClarinLicenseResourceMappingService clarinLicenseResourceMappingService; - @Override public ClarinLicenseService getClarinLicenseService() { return clarinLicenseService; @@ -30,7 +40,7 @@ public ClarinLicenseLabelService getClarinLicenseLabelService() { } @Override - public ClarinLicenseResourceMappingService getClarinResourceMappingService() { + public ClarinLicenseResourceMappingService getClarinLicenseResourceMappingService() { return clarinLicenseResourceMappingService; } } diff --git a/dspace-api/src/main/java/org/dspace/content/service/clarin/ClarinLicenseLabelService.java b/dspace-api/src/main/java/org/dspace/content/service/clarin/ClarinLicenseLabelService.java index ab84409f599a..adb56ecc238d 100644 --- a/dspace-api/src/main/java/org/dspace/content/service/clarin/ClarinLicenseLabelService.java +++ b/dspace-api/src/main/java/org/dspace/content/service/clarin/ClarinLicenseLabelService.java @@ -1,20 +1,81 @@ +/** + * 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.service.clarin; +import java.sql.SQLException; +import java.util.List; + import org.dspace.authorize.AuthorizeException; import org.dspace.content.clarin.ClarinLicenseLabel; import org.dspace.core.Context; -import java.sql.SQLException; - +/** + * Service interface class for the Clarin License Label object. + * The implementation of this class is responsible for all business logic calls for the Clarin License Label object + * and is autowired by spring + * + * @author Milan Majchrak (milan.majchrak at dataquest.sk) + */ public interface ClarinLicenseLabelService { + + /** + * Create a new clarin license label. Authorization is done inside this method. + * @param context DSpace context object + * @return the newly created clarin license label + * @throws SQLException if database error + * @throws AuthorizeException the user in not admin + */ ClarinLicenseLabel create(Context context) throws SQLException, AuthorizeException; + /** + * Create a new clarin license label. Authorization is done inside this method. + * @param context DSpace context object + * @param clarinLicenseLabel new clarin license label object data + * @return the newly created clarin license label + * @throws SQLException if database error + * @throws AuthorizeException the user in not admin + */ ClarinLicenseLabel create(Context context, ClarinLicenseLabel clarinLicenseLabel) throws SQLException, AuthorizeException; + /** + * Find the clarin license label object by id + * @param context DSpace context object + * @param valueId id of the searching clarin license label object + * @return found clarin license label object or null + * @throws SQLException if database error + */ ClarinLicenseLabel find(Context context, int valueId) throws SQLException; - void delete(Context context, ClarinLicenseLabel clarinLicenseLabel) throws SQLException; + /** + * Find all clarin license label objects + * @param context DSpace context object + * @return list of all clarin license label objects + * @throws SQLException if database error + * @throws AuthorizeException the user in not admin + */ + List findAll(Context context) throws SQLException, AuthorizeException; + + /** + * Delete the clarin license label by id. The id is retrieved from passed clarin license label object. + * @param context DSpace context object + * @param clarinLicenseLabel object to delete + * @throws SQLException if database error + * @throws AuthorizeException the user in not admin + */ + void delete(Context context, ClarinLicenseLabel clarinLicenseLabel) throws SQLException, AuthorizeException; - void update(Context context, ClarinLicenseLabel newClarinLicenseLabel) throws SQLException; + /** + * Update the clarin license label object by id. The id is retrieved from passed clarin license label object. + * @param context DSpace context object + * @param newClarinLicenseLabel with new clarin license label object values + * @throws SQLException if database error + * @throws AuthorizeException the user is not admin + */ + void update(Context context, ClarinLicenseLabel newClarinLicenseLabel) throws SQLException, AuthorizeException; } diff --git a/dspace-api/src/main/java/org/dspace/content/service/clarin/ClarinLicenseService.java b/dspace-api/src/main/java/org/dspace/content/service/clarin/ClarinLicenseService.java index c2c4eb5f6de3..f67be3f972de 100644 --- a/dspace-api/src/main/java/org/dspace/content/service/clarin/ClarinLicenseService.java +++ b/dspace-api/src/main/java/org/dspace/content/service/clarin/ClarinLicenseService.java @@ -1,24 +1,86 @@ +/** + * 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.service.clarin; +import java.sql.SQLException; +import java.util.List; + import org.dspace.authorize.AuthorizeException; import org.dspace.content.clarin.ClarinLicense; import org.dspace.core.Context; -import java.sql.SQLException; -import java.util.List; - +/** + * Service interface class for the Clarin License object. + * The implementation of this class is responsible for all business logic calls for the Clarin License object + * and is autowired by spring + * + * @author Milan Majchrak (milan.majchrak at dataquest.sk) + */ public interface ClarinLicenseService { + /** + * Create a new clarin license. Authorization is done inside this method. + * + * @param context @param context DSpace context object + * @return the newly created clarin license + * @throws SQLException if database error + * @throws AuthorizeException the user in not admin + */ ClarinLicense create(Context context) throws SQLException, AuthorizeException; + /** + * Create a new clarin license. Authorization is done inside this method. + * + * @param context DSpace context object + * @param clarinLicense new clarin license object data + * @return the newly created clarin license + * @throws SQLException if database error + * @throws AuthorizeException the user in not admin + */ ClarinLicense create(Context context, ClarinLicense clarinLicense) throws SQLException, AuthorizeException; + /** + * Find the clarin license object by id + * + * @param context DSpace context object + * @param valueId id of the searching clarin license object + * @return found clarin license object or null + * @throws SQLException if database error + */ ClarinLicense find(Context context, int valueId) throws SQLException; - List findAll(Context context) throws SQLException; - - void delete(Context context, ClarinLicense clarinLicense) throws SQLException; + /** + * Find all clarin license objects + * + * @param context DSpace context object + * @return list of all clarin license objects + * @throws SQLException if database error + * @throws AuthorizeException the user in not admin + */ + List findAll(Context context) throws SQLException, AuthorizeException; - void update(Context context, ClarinLicense newClarinLicense) throws SQLException; + /** + * Delete the clarin license by id. The id is retrieved from the passed clarin license object. + * + * @param context DSpace context object + * @param clarinLicense object to delete + * @throws SQLException if database error + * @throws AuthorizeException the user in not admin + */ + void delete(Context context, ClarinLicense clarinLicense) throws SQLException, AuthorizeException; + /** + * Update the clarin license object by id. The id is retrieved from passed clarin license object. + * + * @param context DSpace context object + * @param newClarinLicense with new clarin license object values + * @throws SQLException if database error + * @throws AuthorizeException the user is not admin + */ + void update(Context context, ClarinLicense newClarinLicense) throws SQLException, AuthorizeException; } diff --git a/dspace-api/src/test/java/org/dspace/builder/AbstractBuilder.java b/dspace-api/src/test/java/org/dspace/builder/AbstractBuilder.java index 319ee7537f48..427f2690d316 100644 --- a/dspace-api/src/test/java/org/dspace/builder/AbstractBuilder.java +++ b/dspace-api/src/test/java/org/dspace/builder/AbstractBuilder.java @@ -38,6 +38,7 @@ import org.dspace.content.service.SiteService; import org.dspace.content.service.WorkspaceItemService; import org.dspace.content.service.clarin.ClarinLicenseLabelService; +import org.dspace.content.service.clarin.ClarinLicenseResourceMappingService; import org.dspace.content.service.clarin.ClarinLicenseService; import org.dspace.core.Context; import org.dspace.discovery.IndexingService; @@ -100,6 +101,7 @@ public abstract class AbstractBuilder { static VersioningService versioningService; static ClarinLicenseService clarinLicenseService; static ClarinLicenseLabelService clarinLicenseLabelService; + static ClarinLicenseResourceMappingService clarinLicenseResourceMappingService; protected Context context; @@ -158,6 +160,7 @@ public static void init() { workflowItemRoleService = XmlWorkflowServiceFactory.getInstance().getWorkflowItemRoleService(); clarinLicenseService = ClarinServiceFactory.getInstance().getClarinLicenseService(); clarinLicenseLabelService = ClarinServiceFactory.getInstance().getClarinLicenseLabelService(); + clarinLicenseResourceMappingService = ClarinServiceFactory.getInstance().getClarinLicenseResourceMappingService(); } @@ -192,6 +195,7 @@ public static void destroy() { versioningService = null; clarinLicenseService = null; clarinLicenseLabelService = null; + clarinLicenseResourceMappingService = null; } diff --git a/dspace-api/src/test/java/org/dspace/builder/ClarinLicenseBuilder.java b/dspace-api/src/test/java/org/dspace/builder/ClarinLicenseBuilder.java index 481bbd4ca900..7bf0a863e1f9 100644 --- a/dspace-api/src/test/java/org/dspace/builder/ClarinLicenseBuilder.java +++ b/dspace-api/src/test/java/org/dspace/builder/ClarinLicenseBuilder.java @@ -1,16 +1,25 @@ +/** + * 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.builder; +import java.sql.SQLException; +import java.util.Objects; + import org.dspace.authorize.AuthorizeException; -import org.dspace.content.WorkspaceItem; import org.dspace.content.clarin.ClarinLicense; -import org.dspace.content.factory.ClarinServiceFactory; import org.dspace.content.service.clarin.ClarinLicenseService; import org.dspace.core.Context; -import org.dspace.eperson.Group; - -import java.sql.SQLException; -import java.util.Objects; +/** + * Builder to construct Clarin License objects + * + * @author Milan Majchrak (milan.majchrak at dataquest.sk) + */ public class ClarinLicenseBuilder extends AbstractBuilder { private ClarinLicense clarinLicense; diff --git a/dspace-api/src/test/java/org/dspace/builder/ClarinLicenseLabelBuilder.java b/dspace-api/src/test/java/org/dspace/builder/ClarinLicenseLabelBuilder.java index 4e5557667b15..3715776aa7de 100644 --- a/dspace-api/src/test/java/org/dspace/builder/ClarinLicenseLabelBuilder.java +++ b/dspace-api/src/test/java/org/dspace/builder/ClarinLicenseLabelBuilder.java @@ -1,21 +1,26 @@ +/** + * 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.builder; +import java.sql.SQLException; + import org.dspace.authorize.AuthorizeException; -import org.dspace.content.clarin.ClarinLicense; import org.dspace.content.clarin.ClarinLicenseLabel; -import org.dspace.content.factory.ClarinServiceFactory; import org.dspace.content.service.clarin.ClarinLicenseLabelService; -import org.dspace.content.service.clarin.ClarinLicenseService; import org.dspace.core.Context; -import org.dspace.eperson.Group; - -import java.sql.SQLException; +/** + * Builder to construct Clarin License Label objects + * + * @author Milan Majchrak (milan.majchrak at dataquest.sk) + */ public class ClarinLicenseLabelBuilder extends AbstractBuilder { - private ClarinLicenseLabelService clarinLicenseLabelService = - ClarinServiceFactory.getInstance().getClarinLicenseLabelService(); - private ClarinLicenseLabel clarinLicenseLabel; protected ClarinLicenseLabelBuilder(Context context) { diff --git a/dspace-api/src/test/java/org/dspace/builder/ClarinLicenseResourceMappingBuilder.java b/dspace-api/src/test/java/org/dspace/builder/ClarinLicenseResourceMappingBuilder.java index c163bd3c9f08..71726fb0a614 100644 --- a/dspace-api/src/test/java/org/dspace/builder/ClarinLicenseResourceMappingBuilder.java +++ b/dspace-api/src/test/java/org/dspace/builder/ClarinLicenseResourceMappingBuilder.java @@ -9,9 +9,6 @@ import java.sql.SQLException; public class ClarinLicenseResourceMappingBuilder extends AbstractBuilder { - - private ClarinLicenseResourceMappingService clarinLicenseResourceMappingService = ClarinServiceFactory.getInstance().getClarinResourceMappingService(); - private ClarinLicenseResourceMapping clarinLicenseResourceMapping; protected ClarinLicenseResourceMappingBuilder(Context context) { diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/converter/ClarinLicenseConverter.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/converter/ClarinLicenseConverter.java index 31c78b0b893c..99f878ac9688 100644 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/converter/ClarinLicenseConverter.java +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/converter/ClarinLicenseConverter.java @@ -1,50 +1,50 @@ +/** + * 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.app.rest.converter; +import java.util.ArrayList; +import java.util.List; + import org.dspace.app.rest.model.ClarinLicenseLabelRest; import org.dspace.app.rest.model.ClarinLicenseRest; import org.dspace.app.rest.projection.Projection; import org.dspace.content.clarin.ClarinLicense; import org.dspace.content.clarin.ClarinLicenseLabel; -import org.dspace.content.service.clarin.ClarinLicenseResourceMappingService; -import org.dspace.xoai.services.api.context.ContextService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; -import javax.inject.Inject; -import java.util.ArrayList; -import java.util.List; - +/** + * This is the converter from/to the Clarin License in the DSpace API data model and the + * REST data model + * Clarin License Rest object has clarin license labels separated to the list of the extended clarin license labels + * and one non-extended clarin license label + * + * @author Milan Majchrak (milan.majchrak at dataquest.sk) + */ @Component public class ClarinLicenseConverter implements DSpaceConverter { - @Autowired - private ConverterService converter; - - @Inject - private ContextService contextService; @Autowired - private ClarinLicenseResourceMappingService clarinLicenseResourceMappingService; + private ConverterService converter; @Override public ClarinLicenseRest convert(ClarinLicense modelObject, Projection projection) { ClarinLicenseRest license = new ClarinLicenseRest(); license.setProjection(projection); - license.setId(modelObject.getId()); + license.setId(modelObject.getID()); license.setName(modelObject.getName()); license.setConfirmation(modelObject.getConfirmation()); license.setDefinition(modelObject.getDefinition()); license.setRequiredInfo(modelObject.getRequiredInfo()); setExtendedClarinLicenseLabels(license, modelObject.getLicenseLabels(), projection); setClarinLicenseLabel(license, modelObject.getLicenseLabels(), projection); -// license.setExtendedClarinLicenseLabels(modelObject.getLicenseLabels(), projection); -// license.setClarinLicenseLabel(modelObject.getLicenseLabels(), projection); - // TODO find out which bitstreams are using this license - try { - license.setBitstreams(clarinLicenseResourceMappingService.findAllByLicenseId(contextService.getContext(), modelObject.getID()).size()); - } catch (Exception e) { - throw new RuntimeException("Message"); - } - + // TODO find out which bitstreams are using by this license + license.setBitstreams(0); return license; } @@ -53,8 +53,9 @@ public Class getModelClass() { return ClarinLicense.class; } + /** - * Add ExtendedClarinLicenseLabel list to the map + * Clarin license labels separate to the list of the extended clarin license labels */ public void setExtendedClarinLicenseLabels(ClarinLicenseRest licenseRest, List cLicenseLabels, Projection projection) { @@ -66,10 +67,14 @@ public void setExtendedClarinLicenseLabels(ClarinLicenseRest licenseRest, List cLicenseLabels, Projection projection) { DSpaceConverter clarinLicenseLabelConverter = diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/converter/ClarinLicenseLabelConverter.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/converter/ClarinLicenseLabelConverter.java index 49b7b7ca08bf..871caa625d04 100644 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/converter/ClarinLicenseLabelConverter.java +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/converter/ClarinLicenseLabelConverter.java @@ -1,13 +1,23 @@ +/** + * 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.app.rest.converter; -import org.dspace.app.rest.converter.ConverterService; -import org.dspace.app.rest.converter.DSpaceConverter; import org.dspace.app.rest.model.ClarinLicenseLabelRest; import org.dspace.app.rest.projection.Projection; import org.dspace.content.clarin.ClarinLicenseLabel; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; +/** + * This is the converter from/to the Clarin License Label in the DSpace API data model and the + * REST data model + * + * @author Milan Majchrak (milan.majchrak at dataquest.sk) + */ @Component public class ClarinLicenseLabelConverter implements DSpaceConverter { @@ -15,7 +25,7 @@ public class ClarinLicenseLabelConverter implements DSpaceConverter { public static final String NAME = "clarinlicenselabel"; diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/model/ClarinLicenseRest.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/model/ClarinLicenseRest.java index 77b23c8b28f5..40ed2298d514 100644 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/model/ClarinLicenseRest.java +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/model/ClarinLicenseRest.java @@ -1,12 +1,26 @@ +/** + * 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.app.rest.model; +import java.util.ArrayList; +import java.util.List; + import com.fasterxml.jackson.annotation.JsonProperty; import org.dspace.app.rest.RestResourceController; import org.springframework.stereotype.Component; -import java.util.ArrayList; -import java.util.List; - +/** + * The Clarin License REST Resource + * Clarin License Rest object has clarin license labels separated to the list of the extended clarin license labels + * and one non-extended clarin license label. + * + * @author Milan Majchrak (milan.majchrak at dataquest.sk) + */ @Component public class ClarinLicenseRest extends BaseObjectRest { @@ -83,15 +97,6 @@ public void setBitstreams(Integer bitstreams) { this.bitstreams = bitstreams; } - // public ClarinLicenseLabelListRest getLicenseLabel() { -// return clarinLicenseLabels; -// } -// -// public void setLicenseLabel(ClarinLicenseLabelListRest licenseLabel) { -// this.clarinLicenseLabels = licenseLabel; -// } - - @Override @JsonProperty(access = JsonProperty.Access.READ_ONLY) public String getType() { diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/ClarinLicenseLabelRestRepository.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/ClarinLicenseLabelRestRepository.java index 4c6de2ea1f5f..f314f4159ca8 100644 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/ClarinLicenseLabelRestRepository.java +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/ClarinLicenseLabelRestRepository.java @@ -1,13 +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.app.rest.repository; +import static org.apache.commons.lang3.StringUtils.isBlank; + +import java.io.IOException; +import java.sql.SQLException; +import java.util.List; + +import com.fasterxml.jackson.databind.ObjectMapper; +import org.apache.commons.lang3.ArrayUtils; +import org.dspace.app.rest.exception.DSpaceBadRequestException; +import org.dspace.app.rest.exception.UnprocessableEntityException; import org.dspace.app.rest.model.ClarinLicenseLabelRest; +import org.dspace.authorize.AuthorizeException; +import org.dspace.content.clarin.ClarinLicenseLabel; +import org.dspace.content.service.clarin.ClarinLicenseLabelService; import org.dspace.core.Context; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; +import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.stereotype.Component; +/** + * This is the repository responsible to manage Clarin License Label Rest object + * + * @author Milan Majchrak (milan.majchrak at dataquest.sk) + */ @Component(ClarinLicenseLabelRest.CATEGORY + "." + ClarinLicenseLabelRest.NAME) public class ClarinLicenseLabelRestRepository extends DSpaceRestRepository { + + @Autowired + ClarinLicenseLabelService clarinLicenseLabelService; + @Override public ClarinLicenseLabelRest findOne(Context context, Integer integer) { return null; @@ -15,9 +46,50 @@ public ClarinLicenseLabelRest findOne(Context context, Integer integer) { @Override public Page findAll(Context context, Pageable pageable) { - return null; + try { + List clarinLicenseLabelList = clarinLicenseLabelService.findAll(context); + return converter.toRestPage(clarinLicenseLabelList, pageable, utils.obtainProjection()); + } catch (SQLException | AuthorizeException e) { + throw new RuntimeException(e.getMessage(), e); + } } + // create + @Override + @PreAuthorize("hasAuthority('ADMIN')") + protected ClarinLicenseLabelRest createAndReturn(Context context) + throws AuthorizeException, SQLException { + + // parse request body + ClarinLicenseLabelRest clarinLicenseLabelRest; + try { + clarinLicenseLabelRest = new ObjectMapper().readValue( + getRequestService().getCurrentRequest().getHttpServletRequest().getInputStream(), + ClarinLicenseLabelRest.class + ); + } catch (IOException excIO) { + throw new DSpaceBadRequestException("error parsing request body", excIO); + } + + // validate fields + if (isBlank(clarinLicenseLabelRest.getLabel()) || isBlank(clarinLicenseLabelRest.getTitle()) || + ArrayUtils.isEmpty(clarinLicenseLabelRest.getIcon())) { + throw new UnprocessableEntityException("Clarin License Label title, label, icon cannot be null or empty"); + } + + // create + ClarinLicenseLabel clarinLicenseLabel; + clarinLicenseLabel = clarinLicenseLabelService.create(context); + clarinLicenseLabel.setLabel(clarinLicenseLabelRest.getLabel()); + clarinLicenseLabel.setTitle(clarinLicenseLabelRest.getTitle()); + clarinLicenseLabel.setIcon(clarinLicenseLabelRest.getIcon()); + + clarinLicenseLabelService.update(context, clarinLicenseLabel); + // return + return converter.toRest(clarinLicenseLabel, utils.obtainProjection()); + } + + @Override public Class getDomainClass() { return ClarinLicenseLabelRest.class; diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/ClarinLicenseRestRepository.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/ClarinLicenseRestRepository.java index 845c4cf11476..82594c339a97 100644 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/ClarinLicenseRestRepository.java +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/ClarinLicenseRestRepository.java @@ -1,5 +1,22 @@ +/** + * 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.app.rest.repository; +import static org.apache.commons.lang3.StringUtils.isBlank; + +import java.io.IOException; +import java.sql.SQLException; +import java.util.HashSet; +import java.util.List; +import java.util.Objects; +import java.util.Set; +import javax.servlet.http.HttpServletRequest; + import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.gson.Gson; @@ -8,15 +25,11 @@ import org.dspace.app.rest.exception.UnprocessableEntityException; import org.dspace.app.rest.model.ClarinLicenseLabelRest; import org.dspace.app.rest.model.ClarinLicenseRest; -import org.dspace.app.rest.model.MetadataFieldRest; import org.dspace.authorize.AuthorizeException; -import org.dspace.content.MetadataField; -import org.dspace.content.NonUniqueMetadataException; import org.dspace.content.clarin.ClarinLicense; import org.dspace.content.clarin.ClarinLicenseLabel; import org.dspace.content.service.clarin.ClarinLicenseService; import org.dspace.core.Context; -import org.json.JSONObject; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; @@ -24,17 +37,11 @@ import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.stereotype.Component; -import javax.servlet.http.HttpServletRequest; -import java.io.IOException; -import java.sql.SQLException; -import java.util.HashSet; -import java.util.List; -import java.util.Objects; -import java.util.Set; - -import static java.lang.Integer.parseInt; -import static org.apache.commons.lang3.StringUtils.isBlank; - +/** + * This is the repository responsible to manage Clarin License Rest object + * + * @author Milan Majchrak (milan.majchrak at dataquest.sk) + */ @Component(ClarinLicenseRest.CATEGORY + "." + ClarinLicenseRest.NAME) public class ClarinLicenseRestRepository extends DSpaceRestRepository { @@ -63,7 +70,7 @@ public Page findAll(Context context, Pageable pageable) { try { List clarinLicenseList = clarinLicenseService.findAll(context); return converter.toRestPage(clarinLicenseList, pageable, utils.obtainProjection()); - } catch (SQLException e) { + } catch (SQLException | AuthorizeException e) { throw new RuntimeException(e.getMessage(), e); } } diff --git a/dspace-server-webapp/src/test/java/org/dspace/app/rest/ClarinLicenseLabelRestRepositoryIT.java b/dspace-server-webapp/src/test/java/org/dspace/app/rest/ClarinLicenseLabelRestRepositoryIT.java new file mode 100644 index 000000000000..96a1893d9cc4 --- /dev/null +++ b/dspace-server-webapp/src/test/java/org/dspace/app/rest/ClarinLicenseLabelRestRepositoryIT.java @@ -0,0 +1,94 @@ +/** + * 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.app.rest; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import org.dspace.app.rest.matcher.ClarinLicenseLabelMatcher; +import org.dspace.app.rest.test.AbstractControllerIntegrationTest; +import org.dspace.builder.ClarinLicenseLabelBuilder; +import org.dspace.content.clarin.ClarinLicenseLabel; +import org.dspace.content.service.clarin.ClarinLicenseLabelService; +import org.hamcrest.Matchers; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; + +/** + * Integration tests for the Clarin License Label Rest Repository + * + * @author Milan Majchrak (milan.majchrak at dataquest.sk) + */ +public class ClarinLicenseLabelRestRepositoryIT extends AbstractControllerIntegrationTest { + + @Autowired + ClarinLicenseLabelService clarinLicenseLabelService; + + ClarinLicenseLabel firstCLicenseLabel; + ClarinLicenseLabel secondCLicenseLabel; + ClarinLicenseLabel thirdCLicenseLabel; + + @Before + public void setup() throws Exception { + context.turnOffAuthorisationSystem(); + // create LicenseLabels + firstCLicenseLabel = ClarinLicenseLabelBuilder.createClarinLicenseLabel(context).build(); + firstCLicenseLabel.setLabel("CC"); + firstCLicenseLabel.setExtended(true); + firstCLicenseLabel.setTitle("CLL Title1"); + firstCLicenseLabel.setIcon(new byte[100]); + clarinLicenseLabelService.update(context, firstCLicenseLabel); + + secondCLicenseLabel = ClarinLicenseLabelBuilder.createClarinLicenseLabel(context).build(); + secondCLicenseLabel.setLabel("CCC"); + secondCLicenseLabel.setExtended(true); + secondCLicenseLabel.setTitle("CLL Title2"); + secondCLicenseLabel.setIcon(new byte[200]); + clarinLicenseLabelService.update(context, secondCLicenseLabel); + + thirdCLicenseLabel = ClarinLicenseLabelBuilder.createClarinLicenseLabel(context).build(); + thirdCLicenseLabel.setLabel("DBC"); + thirdCLicenseLabel.setExtended(false); + thirdCLicenseLabel.setTitle("CLL Title3"); + thirdCLicenseLabel.setIcon(new byte[300]); + clarinLicenseLabelService.update(context, thirdCLicenseLabel); + context.restoreAuthSystemState(); + } + + @Test + public void clarinLicenseLabelsAreInitialized() throws Exception { + Assert.assertNotNull(firstCLicenseLabel); + Assert.assertNotNull(secondCLicenseLabel); + Assert.assertNotNull(thirdCLicenseLabel); + } + + @Test + public void findAll() throws Exception { + String authTokenAdmin = getAuthToken(admin.getEmail(), password); + getClient(authTokenAdmin).perform(get("/api/core/clarinlicenselabels")) + .andExpect(status().isOk()) + .andExpect(content().contentType(contentType)) + .andExpect(jsonPath("$._embedded.clarinlicenselabels", Matchers.hasItem( + ClarinLicenseLabelMatcher.matchClarinLicenseLabel(firstCLicenseLabel)) + )) + .andExpect(jsonPath("$._embedded.clarinlicenselabels", Matchers.hasItem( + ClarinLicenseLabelMatcher.matchClarinLicenseLabel(secondCLicenseLabel)) + )) + .andExpect(jsonPath("$._embedded.clarinlicenselabels", Matchers.hasItem( + ClarinLicenseLabelMatcher.matchClarinLicenseLabel(thirdCLicenseLabel)) + )) + .andExpect(jsonPath("$._links.self.href", + Matchers.containsString("/api/core/clarinlicenselabels"))) + ; + } + +} diff --git a/dspace-server-webapp/src/test/java/org/dspace/app/rest/ClarinLicenseRestRepositoryIT.java b/dspace-server-webapp/src/test/java/org/dspace/app/rest/ClarinLicenseRestRepositoryIT.java index 83c80d048c1d..cf0265676a0a 100644 --- a/dspace-server-webapp/src/test/java/org/dspace/app/rest/ClarinLicenseRestRepositoryIT.java +++ b/dspace-server-webapp/src/test/java/org/dspace/app/rest/ClarinLicenseRestRepositoryIT.java @@ -1,50 +1,57 @@ +/** + * 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.app.rest; +import static com.jayway.jsonpath.JsonPath.read; +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertEquals; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import java.util.HashSet; +import java.util.List; +import java.util.Objects; +import java.util.Set; +import java.util.concurrent.atomic.AtomicReference; + import com.fasterxml.jackson.databind.ObjectMapper; import org.dspace.app.rest.converter.ClarinLicenseConverter; -import org.dspace.app.rest.converter.ConverterService; -import org.dspace.app.rest.converter.DSpaceConverter; import org.dspace.app.rest.matcher.ClarinLicenseLabelMatcher; import org.dspace.app.rest.matcher.ClarinLicenseMatcher; import org.dspace.app.rest.model.ClarinLicenseLabelRest; import org.dspace.app.rest.model.ClarinLicenseRest; import org.dspace.app.rest.projection.Projection; import org.dspace.app.rest.test.AbstractControllerIntegrationTest; -import org.dspace.builder.ClarinLicenseBuilder; -import org.dspace.builder.ClarinLicenseLabelBuilder; -import org.dspace.builder.WorkspaceItemBuilder; +import org.dspace.builder.*; +import org.dspace.content.Collection; +import org.dspace.content.Item; import org.dspace.content.clarin.ClarinLicense; import org.dspace.content.clarin.ClarinLicenseLabel; +import org.dspace.content.clarin.ClarinLicenseResourceMapping; import org.dspace.content.service.clarin.ClarinLicenseLabelService; +import org.dspace.content.service.clarin.ClarinLicenseResourceMappingService; import org.dspace.content.service.clarin.ClarinLicenseService; import org.hamcrest.Matchers; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.util.ObjectUtils; - -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Objects; -import java.util.Set; -import java.util.SortedMap; -import java.util.TreeMap; -import java.util.concurrent.atomic.AtomicReference; - -import static com.jayway.jsonpath.JsonPath.read; -import static com.jayway.jsonpath.matchers.JsonPathMatchers.hasJsonPath; -import static org.hamcrest.Matchers.in; -import static org.hamcrest.Matchers.is; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +/** + * Integration tests for the Clarin License Rest Repository + * + * @author Milan Majchrak (milan.majchrak at dataquest.sk) + */ public class ClarinLicenseRestRepositoryIT extends AbstractControllerIntegrationTest { @Autowired @@ -56,6 +63,8 @@ public class ClarinLicenseRestRepositoryIT extends AbstractControllerIntegration @Autowired ClarinLicenseConverter clarinLicenseConverter; + @Autowired + ClarinLicenseResourceMappingService clarinLicenseResourceMappingService; ClarinLicense firstCLicense; ClarinLicense secondCLicense; @@ -63,6 +72,11 @@ public class ClarinLicenseRestRepositoryIT extends AbstractControllerIntegration ClarinLicenseLabel secondCLicenseLabel; ClarinLicenseLabel thirdCLicenseLabel; + Item publicItem1; + + Item publicItem2; + Item publicItem3; + @Before public void setup() throws Exception { context.turnOffAuthorisationSystem(); @@ -109,6 +123,58 @@ public void setup() throws Exception { secondCLicense.setLicenseLabels(secondClarinLicenseLabels); clarinLicenseService.update(context, secondCLicense); + //create collection for items + parentCommunity = CommunityBuilder.createCommunity(context) + .withName("Parent Community") + .build(); + Collection col1 = CollectionBuilder.createCollection(context, parentCommunity).withName("Collection1").build(); + Collection col2 = CollectionBuilder.createCollection(context, parentCommunity).withName("Collection2").build(); + Collection col3 = CollectionBuilder.createCollection(context, parentCommunity).withName("Collection3").build(); + + //create two items with the first license + publicItem1 = ItemBuilder.createItem(context, col1) + .withTitle("Public item 1") + .withIssueDate("2022-10-17") + .withAuthor("Smith, Donald").withAuthor("Doe, John") + .withSubject("ExtraEntry") + .build(); + + publicItem2 = ItemBuilder.createItem(context, col2) + .withTitle("Public item 2") + .withIssueDate("2016-02-13") + .withAuthor("Smith, Maria").withAuthor("Doe, Jane") + .withSubject("TestingForMore").withSubject("ExtraEntry") + .build(); + + //create item with the second license + publicItem3 = ItemBuilder.createItem(context, col3) + .withTitle("Public item 3") + .withIssueDate("2016-02-13") + .withAuthor("Smith, Maria").withAuthor("Doe, Jane") + .withSubject("AnotherTest").withSubject("TestingForMore") + .withSubject("ExtraEntry") + .build(); + + ClarinLicenseResourceMapping clarinLicenseResourceMapping1 = ClarinLicenseResourceMappingBuilder + .createClarinLicenseResourceMapping(context).build(); + clarinLicenseResourceMapping1.setLicenseId(firstCLicense.getID()); + clarinLicenseResourceMapping1.setBitstreamId(publicItem1.getID()); + clarinLicenseResourceMappingService.update(context, clarinLicenseResourceMapping1); + + +// ClarinLicenseResourceMapping clarinLicenseResourceMapping2 = ClarinLicenseResourceMappingBuilder +// .createClarinLicenseResourceMapping(context).build(); +// clarinLicenseResourceMapping2.setLicenseId(firstCLicense.getID()); +// clarinLicenseResourceMapping2.setBitstreamId(publicItem2.getID()); +// clarinLicenseResourceMappingService.update(context, clarinLicenseResourceMapping2); + + + ClarinLicenseResourceMapping clarinLicenseResourceMapping3 = ClarinLicenseResourceMappingBuilder + .createClarinLicenseResourceMapping(context).build(); + clarinLicenseResourceMapping3.setLicenseId(secondCLicense.getID()); + clarinLicenseResourceMapping3.setBitstreamId(publicItem3.getID()); + clarinLicenseResourceMappingService.update(context, clarinLicenseResourceMapping3); + context.restoreAuthSystemState(); } @@ -150,6 +216,11 @@ public void findAll() throws Exception { ; } + @Test + public void findAllBitstreamByLicenseId() throws Exception { + assertEquals(clarinLicenseResourceMappingService.findAllByLicenseId(context,firstCLicense.getID()),0); + } + @Test public void create() throws Exception { ClarinLicenseRest clarinLicenseRest = new ClarinLicenseRest(); diff --git a/dspace-server-webapp/src/test/java/org/dspace/app/rest/matcher/ClarinLicenseLabelMatcher.java b/dspace-server-webapp/src/test/java/org/dspace/app/rest/matcher/ClarinLicenseLabelMatcher.java index 34abe233eb7e..d2dffcc93b0f 100644 --- a/dspace-server-webapp/src/test/java/org/dspace/app/rest/matcher/ClarinLicenseLabelMatcher.java +++ b/dspace-server-webapp/src/test/java/org/dspace/app/rest/matcher/ClarinLicenseLabelMatcher.java @@ -1,13 +1,25 @@ +/** + * 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.app.rest.matcher; -import org.dspace.app.rest.model.ClarinLicenseLabelRest; -import org.dspace.content.clarin.ClarinLicenseLabel; -import org.hamcrest.Matcher; - import static com.jayway.jsonpath.matchers.JsonPathMatchers.hasJsonPath; import static org.hamcrest.Matchers.allOf; import static org.hamcrest.Matchers.is; +import org.dspace.app.rest.model.ClarinLicenseLabelRest; +import org.dspace.content.clarin.ClarinLicenseLabel; +import org.hamcrest.Matcher; + +/** + * Utility class to construct a Matcher for a clarin license label + * + * @author Milan Majchrak (milan.majchrak at dataquest.sk) + */ public class ClarinLicenseLabelMatcher { private ClarinLicenseLabelMatcher() { @@ -16,8 +28,16 @@ private ClarinLicenseLabelMatcher() { public static Matcher matchClarinLicenseLabel(ClarinLicenseLabel clarinLicenseLabel) { return allOf( - hasJsonPath("$.id", is(clarinLicenseLabel.getID())), - hasJsonPath("$.definition", is(clarinLicenseLabel.getLabel())), + hasJsonPath("$.label", is(clarinLicenseLabel.getLabel())), + hasJsonPath("$.type", is(ClarinLicenseLabelRest.NAME)), + hasJsonPath("$.title", is(clarinLicenseLabel.getTitle())), + hasJsonPath("$.extended", is(clarinLicenseLabel.isExtended())) + ); + } + + public static Matcher matchExtendedClarinLicenseLabel(ClarinLicenseLabel clarinLicenseLabel) { + return allOf( + hasJsonPath("$.label", is(clarinLicenseLabel.getLabel())), hasJsonPath("$.type", is(ClarinLicenseLabelRest.NAME)), hasJsonPath("$.title", is(clarinLicenseLabel.getTitle())), hasJsonPath("$.extended", is(clarinLicenseLabel.isExtended())) diff --git a/dspace-server-webapp/src/test/java/org/dspace/app/rest/matcher/ClarinLicenseMatcher.java b/dspace-server-webapp/src/test/java/org/dspace/app/rest/matcher/ClarinLicenseMatcher.java index 8ef6e5b3964d..baa3b7cbf2db 100644 --- a/dspace-server-webapp/src/test/java/org/dspace/app/rest/matcher/ClarinLicenseMatcher.java +++ b/dspace-server-webapp/src/test/java/org/dspace/app/rest/matcher/ClarinLicenseMatcher.java @@ -1,15 +1,26 @@ +/** + * 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.app.rest.matcher; +import static com.jayway.jsonpath.matchers.JsonPathMatchers.hasJsonPath; +import static org.hamcrest.Matchers.allOf; +import static org.hamcrest.Matchers.is; + import org.dspace.app.rest.model.ClarinLicenseRest; -import org.dspace.content.MetadataValue; import org.dspace.content.clarin.ClarinLicense; import org.hamcrest.Matcher; import org.hamcrest.Matchers; -import static com.jayway.jsonpath.matchers.JsonPathMatchers.hasJsonPath; -import static org.hamcrest.Matchers.allOf; -import static org.hamcrest.Matchers.is; - +/** + * Utility class to construct a Matcher for a clarin license + * + * @author Milan Majchrak (milan.majchrak at dataquest.sk) + */ public class ClarinLicenseMatcher { private ClarinLicenseMatcher() { diff --git a/dspace/config/spring/api/core-services.xml b/dspace/config/spring/api/core-services.xml index 937c47a4fe9a..6c578efa6da1 100644 --- a/dspace/config/spring/api/core-services.xml +++ b/dspace/config/spring/api/core-services.xml @@ -74,7 +74,7 @@ - + From 80b90f26cb5d63e652649ec2dc10e242de8e2413 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?MilanMajchr=C3=A1k?= Date: Fri, 23 Sep 2022 15:38:36 +0200 Subject: [PATCH 6/8] The bitstream is mapped to the license --- .../org/dspace/content/BundleServiceImpl.java | 55 ++++++++++++- .../dspace/content/clarin/ClarinLicense.java | 21 ++++- .../clarin/ClarinLicenseResourceMapping.java | 28 ++++--- ...arinLicenseResourceMappingServiceImpl.java | 80 ++++++++++++++----- .../clarin/ClarinLicenseServiceImpl.java | 5 ++ .../content/dao/clarin/ClarinLicenseDAO.java | 6 ++ .../ClarinLicenseResourceMappingDAO.java | 7 ++ .../dao/impl/clarin/ClarinLicenseDAOImpl.java | 16 ++++ .../ClarinLicenseResourceMappingDAOImpl.java | 20 +++++ .../ClarinLicenseResourceMappingService.java | 5 ++ .../service/clarin/ClarinLicenseService.java | 2 + ...07.28__Upgrade_to_Lindat_Clarin_schema.sql | 2 +- ...07.28__Upgrade_to_Lindat_Clarin_schema.sql | 2 +- .../rest/ClarinLicenseRestRepositoryIT.java | 55 +++++++++---- .../config/registries/dublin-core-types.xml | 7 ++ 15 files changed, 260 insertions(+), 51 deletions(-) diff --git a/dspace-api/src/main/java/org/dspace/content/BundleServiceImpl.java b/dspace-api/src/main/java/org/dspace/content/BundleServiceImpl.java index aa32983362de..c6803d6d2c05 100644 --- a/dspace-api/src/main/java/org/dspace/content/BundleServiceImpl.java +++ b/dspace-api/src/main/java/org/dspace/content/BundleServiceImpl.java @@ -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; @@ -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; @@ -62,6 +66,10 @@ public class BundleServiceImpl extends DSpaceObjectServiceImpl 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(); @@ -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))); @@ -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 dcRights = + itemService.getMetadata(owningItem, "dc", "rights", null, Item.ANY); + List 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 bundles = owningItem.getBundles(Constants.CONTENT_BUNDLE_NAME); + for (Bundle clarinBundle : bundles) { + List 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 diff --git a/dspace-api/src/main/java/org/dspace/content/clarin/ClarinLicense.java b/dspace-api/src/main/java/org/dspace/content/clarin/ClarinLicense.java index 9a773a8e0413..90c38c895f05 100644 --- a/dspace-api/src/main/java/org/dspace/content/clarin/ClarinLicense.java +++ b/dspace-api/src/main/java/org/dspace/content/clarin/ClarinLicense.java @@ -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; @@ -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; /** @@ -50,7 +53,10 @@ public class ClarinLicense implements ReloadableEntity { name = "license_label_extended_mapping", joinColumns = @JoinColumn(name = "license_id"), inverseJoinColumns = @JoinColumn(name = "label_id")) - Set clarinLicenseLabels = new HashSet<>();; + Set clarinLicenseLabels = new HashSet<>(); + + @OneToMany(fetch = FetchType.LAZY, mappedBy = "license", cascade = CascadeType.PERSIST) + private final List clarinLicenseResourceMappings = new ArrayList<>(); // @Column(name = "eperson_id") // private Integer epersonId; @@ -115,6 +121,19 @@ public void setLicenseLabels(Set clarinLicenseLabels) { this.clarinLicenseLabels = clarinLicenseLabels; } + public List getClarinLicenseResourceMappings() { + return clarinLicenseResourceMappings; + } + + public ClarinLicenseLabel getNonExtendedClarinLicenseLabel() { + for (ClarinLicenseLabel cll : getLicenseLabels()) { + if (!cll.isExtended()) { + return cll; + } + } + return null; + } + @Override public Integer getID() { return id; diff --git a/dspace-api/src/main/java/org/dspace/content/clarin/ClarinLicenseResourceMapping.java b/dspace-api/src/main/java/org/dspace/content/clarin/ClarinLicenseResourceMapping.java index 62c669d36f21..9c89108ad676 100644 --- a/dspace-api/src/main/java/org/dspace/content/clarin/ClarinLicenseResourceMapping.java +++ b/dspace-api/src/main/java/org/dspace/content/clarin/ClarinLicenseResourceMapping.java @@ -1,5 +1,6 @@ package org.dspace.content.clarin; +import org.dspace.content.Bitstream; import org.dspace.core.ReloadableEntity; import javax.persistence.*; @@ -15,11 +16,14 @@ public class ClarinLicenseResourceMapping implements ReloadableEntity { @SequenceGenerator(name = "license_resource_mapping_mapping_id_seq", sequenceName = "license_resource_mapping_mapping_id_seq", allocationSize = 1) private Integer id; - @Column(name = "bitstream_uuid") - private UUID bitstreamUuid = null; - @Column(name = "license_id") - private Integer licenseId = 0; + @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() { @@ -30,19 +34,19 @@ public void setId(Integer id) { this.id = id; } - public UUID getBitstreamId() { - return bitstreamUuid; + public Bitstream getBitstream() { + return bitstream; } - public void setBitstreamId(UUID bitstreamId) { - this.bitstreamUuid = bitstreamId; + public void setBitstream(Bitstream bitstream) { + this.bitstream = bitstream; } - public Integer getLicenseId() { - return licenseId; + public ClarinLicense getLicense() { + return license; } - public void setLicenseId(Integer licenseId) { - this.licenseId = licenseId; + public void setLicense(ClarinLicense license) { + this.license = license; } } diff --git a/dspace-api/src/main/java/org/dspace/content/clarin/ClarinLicenseResourceMappingServiceImpl.java b/dspace-api/src/main/java/org/dspace/content/clarin/ClarinLicenseResourceMappingServiceImpl.java index e77c0719e49f..ebd77e9fb276 100644 --- a/dspace-api/src/main/java/org/dspace/content/clarin/ClarinLicenseResourceMappingServiceImpl.java +++ b/dspace-api/src/main/java/org/dspace/content/clarin/ClarinLicenseResourceMappingServiceImpl.java @@ -1,10 +1,14 @@ 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; @@ -12,6 +16,7 @@ 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; @@ -25,16 +30,17 @@ public class ClarinLicenseResourceMappingServiceImpl implements ClarinLicenseRes @Autowired ClarinLicenseResourceMappingDAO clarinLicenseResourceMappingDAO; + @Autowired + ClarinLicenseService clarinLicenseService; + + @Autowired + BitstreamService bitstreamService; + @Autowired AuthorizeService authorizeService; @Override - public ClarinLicenseResourceMapping create(Context context) throws SQLException, AuthorizeException { - if (!authorizeService.isAdmin(context)) { - throw new AuthorizeException( - "You must be an admin to create an Clarin License Resource Mapping"); - } - + public ClarinLicenseResourceMapping create(Context context) throws SQLException { // Create a table row ClarinLicenseResourceMapping clarinLicenseResourceMapping = clarinLicenseResourceMappingDAO.create(context, new ClarinLicenseResourceMapping()); @@ -45,24 +51,24 @@ public ClarinLicenseResourceMapping create(Context context) throws SQLException, } @Override - public ClarinLicenseResourceMapping create(Context context, ClarinLicenseResourceMapping clarinLicenseResourceMapping) throws SQLException, AuthorizeException { - if (!authorizeService.isAdmin(context)) { - throw new AuthorizeException( - "You must be an admin to create an Clarin License Resource Mapping"); - } + 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, AuthorizeException { - if (!authorizeService.isAdmin(context)) { - throw new AuthorizeException( - "You must be an admin to create an Clarin License Resource Mapping"); + 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); } - ClarinLicenseResourceMapping clarinLicenseResourceMapping = new ClarinLicenseResourceMapping(); - clarinLicenseResourceMapping.setLicenseId(licenseId); - clarinLicenseResourceMapping.setBitstreamId(bitstreamUuid); + 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); } @@ -77,7 +83,7 @@ public List findAllByLicenseId(Context context, In List mappings = clarinLicenseResourceMappingDAO.findAll(context, ClarinLicenseResourceMapping.class); List mappingsByLicenseId = new ArrayList<>(); for (ClarinLicenseResourceMapping mapping: mappings) { - if (mapping.getLicenseId() == licenseId) { + if (Objects.equals(mapping.getLicense().getID(), licenseId)) { mappingsByLicenseId.add(mapping); } } @@ -104,4 +110,40 @@ public void update(Context context, ClarinLicenseResourceMapping newClarinLicens public void delete(Context context, ClarinLicenseResourceMapping clarinLicenseResourceMapping) throws SQLException { clarinLicenseResourceMappingDAO.delete(context, clarinLicenseResourceMapping); } + + @Override + public void detachLicenses(Context context, Bitstream bitstream) throws SQLException { + List 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); + } } diff --git a/dspace-api/src/main/java/org/dspace/content/clarin/ClarinLicenseServiceImpl.java b/dspace-api/src/main/java/org/dspace/content/clarin/ClarinLicenseServiceImpl.java index 7965b56b5d65..50d37557d0b3 100644 --- a/dspace-api/src/main/java/org/dspace/content/clarin/ClarinLicenseServiceImpl.java +++ b/dspace-api/src/main/java/org/dspace/content/clarin/ClarinLicenseServiceImpl.java @@ -57,6 +57,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 findAll(Context context) throws SQLException, AuthorizeException { if (!authorizeService.isAdmin(context)) { diff --git a/dspace-api/src/main/java/org/dspace/content/dao/clarin/ClarinLicenseDAO.java b/dspace-api/src/main/java/org/dspace/content/dao/clarin/ClarinLicenseDAO.java index f9e2b0a18c7a..0fafb6fc22b7 100644 --- a/dspace-api/src/main/java/org/dspace/content/dao/clarin/ClarinLicenseDAO.java +++ b/dspace-api/src/main/java/org/dspace/content/dao/clarin/ClarinLicenseDAO.java @@ -8,8 +8,11 @@ package org.dspace.content.dao.clarin; import org.dspace.content.clarin.ClarinLicense; +import org.dspace.core.Context; import org.dspace.core.GenericDAO; +import java.sql.SQLException; + /** * Database Access Object interface class for the Clarin License object. * The implementation of this class is responsible for all database calls for the Clarin License object @@ -19,4 +22,7 @@ * @author Milan Majchrak (milan.majchrak at dataquest.sk) */ public interface ClarinLicenseDAO extends GenericDAO { + + ClarinLicense findByDefinition(Context context, String definition) throws SQLException; + } diff --git a/dspace-api/src/main/java/org/dspace/content/dao/clarin/ClarinLicenseResourceMappingDAO.java b/dspace-api/src/main/java/org/dspace/content/dao/clarin/ClarinLicenseResourceMappingDAO.java index 3d2a07ab43ad..6781a90d3c34 100644 --- a/dspace-api/src/main/java/org/dspace/content/dao/clarin/ClarinLicenseResourceMappingDAO.java +++ b/dspace-api/src/main/java/org/dspace/content/dao/clarin/ClarinLicenseResourceMappingDAO.java @@ -1,7 +1,14 @@ package org.dspace.content.dao.clarin; import org.dspace.content.clarin.ClarinLicenseResourceMapping; +import org.dspace.core.Context; import org.dspace.core.GenericDAO; +import java.sql.SQLException; +import java.util.List; +import java.util.UUID; + public interface ClarinLicenseResourceMappingDAO extends GenericDAO { + + List findByBitstreamUUID(Context context, UUID bitstreamUUID) throws SQLException; } diff --git a/dspace-api/src/main/java/org/dspace/content/dao/impl/clarin/ClarinLicenseDAOImpl.java b/dspace-api/src/main/java/org/dspace/content/dao/impl/clarin/ClarinLicenseDAOImpl.java index 56e7b8e14715..ef38d964096b 100644 --- a/dspace-api/src/main/java/org/dspace/content/dao/impl/clarin/ClarinLicenseDAOImpl.java +++ b/dspace-api/src/main/java/org/dspace/content/dao/impl/clarin/ClarinLicenseDAOImpl.java @@ -10,6 +10,10 @@ import org.dspace.content.clarin.ClarinLicense; import org.dspace.content.dao.clarin.ClarinLicenseDAO; import org.dspace.core.AbstractHibernateDAO; +import org.dspace.core.Context; + +import javax.persistence.Query; +import java.sql.SQLException; /** * Hibernate implementation of the Database Access Object interface class for the Clarin License object. @@ -22,4 +26,16 @@ public class ClarinLicenseDAOImpl extends AbstractHibernateDAO im protected ClarinLicenseDAOImpl() { super(); } + + @Override + public ClarinLicense findByDefinition(Context context, String definition) throws SQLException { + Query query = createQuery(context, "SELECT cl " + + "FROM ClarinLicense cl " + + "WHERE cl.definition = :definition"); + + query.setParameter("definition", definition); + query.setHint("org.hibernate.cacheable", Boolean.TRUE); + + return singleResult(query); + } } diff --git a/dspace-api/src/main/java/org/dspace/content/dao/impl/clarin/ClarinLicenseResourceMappingDAOImpl.java b/dspace-api/src/main/java/org/dspace/content/dao/impl/clarin/ClarinLicenseResourceMappingDAOImpl.java index 2475b6ee41b0..edcaeb58789a 100644 --- a/dspace-api/src/main/java/org/dspace/content/dao/impl/clarin/ClarinLicenseResourceMappingDAOImpl.java +++ b/dspace-api/src/main/java/org/dspace/content/dao/impl/clarin/ClarinLicenseResourceMappingDAOImpl.java @@ -1,11 +1,31 @@ package org.dspace.content.dao.impl.clarin; +import org.dspace.content.Bitstream; +import org.dspace.content.clarin.ClarinLicense; import org.dspace.content.clarin.ClarinLicenseResourceMapping; import org.dspace.content.dao.clarin.ClarinLicenseResourceMappingDAO; import org.dspace.core.AbstractHibernateDAO; +import org.dspace.core.Context; + +import javax.persistence.Query; +import java.sql.SQLException; +import java.util.List; +import java.util.UUID; public class ClarinLicenseResourceMappingDAOImpl extends AbstractHibernateDAO implements ClarinLicenseResourceMappingDAO { protected ClarinLicenseResourceMappingDAOImpl() { super(); } + + @Override + public List findByBitstreamUUID(Context context, UUID bitstreamUUID) throws SQLException { + Query query = createQuery(context, "SELECT clrm " + + "FROM ClarinLicenseResourceMapping clrm " + + "WHERE clrm.bitstream.id = :bitstreamUUID"); + + query.setParameter("bitstreamUUID", bitstreamUUID); + query.setHint("org.hibernate.cacheable", Boolean.TRUE); + + return list(query); + } } diff --git a/dspace-api/src/main/java/org/dspace/content/service/clarin/ClarinLicenseResourceMappingService.java b/dspace-api/src/main/java/org/dspace/content/service/clarin/ClarinLicenseResourceMappingService.java index 5be5589318a0..eaa791ae6e9c 100644 --- a/dspace-api/src/main/java/org/dspace/content/service/clarin/ClarinLicenseResourceMappingService.java +++ b/dspace-api/src/main/java/org/dspace/content/service/clarin/ClarinLicenseResourceMappingService.java @@ -1,6 +1,7 @@ package org.dspace.content.service.clarin; import org.dspace.authorize.AuthorizeException; +import org.dspace.content.Bitstream; import org.dspace.content.clarin.ClarinLicense; import org.dspace.content.clarin.ClarinLicenseLabel; import org.dspace.content.clarin.ClarinLicenseResourceMapping; @@ -22,4 +23,8 @@ public interface ClarinLicenseResourceMappingService { void update(Context context, ClarinLicenseResourceMapping newClarinLicenseResourceMapping) throws SQLException; void delete(Context context, ClarinLicenseResourceMapping clarinLicenseResourceMapping) throws SQLException; + + void detachLicenses(Context context, Bitstream bitstream) throws SQLException; + + void attachLicense(Context context, ClarinLicense clarinLicense, Bitstream bitstream) throws SQLException, AuthorizeException; } diff --git a/dspace-api/src/main/java/org/dspace/content/service/clarin/ClarinLicenseService.java b/dspace-api/src/main/java/org/dspace/content/service/clarin/ClarinLicenseService.java index f67be3f972de..d716eb1fea88 100644 --- a/dspace-api/src/main/java/org/dspace/content/service/clarin/ClarinLicenseService.java +++ b/dspace-api/src/main/java/org/dspace/content/service/clarin/ClarinLicenseService.java @@ -54,6 +54,8 @@ public interface ClarinLicenseService { */ ClarinLicense find(Context context, int valueId) throws SQLException; + ClarinLicense findByDefinition(Context context, String definition) throws SQLException; + /** * Find all clarin license objects * diff --git a/dspace-api/src/main/resources/org/dspace/storage/rdbms/sqlmigration/h2/V7.2_2022.07.28__Upgrade_to_Lindat_Clarin_schema.sql b/dspace-api/src/main/resources/org/dspace/storage/rdbms/sqlmigration/h2/V7.2_2022.07.28__Upgrade_to_Lindat_Clarin_schema.sql index 08e7d28ec1f3..95a16bad6824 100644 --- a/dspace-api/src/main/resources/org/dspace/storage/rdbms/sqlmigration/h2/V7.2_2022.07.28__Upgrade_to_Lindat_Clarin_schema.sql +++ b/dspace-api/src/main/resources/org/dspace/storage/rdbms/sqlmigration/h2/V7.2_2022.07.28__Upgrade_to_Lindat_Clarin_schema.sql @@ -110,7 +110,7 @@ CREATE SEQUENCE license_label_label_id_seq CREATE TABLE license_resource_mapping ( mapping_id integer NOT NULL, - bitstream_uuid uuid UNIQUE, + bitstream_uuid uuid, license_id integer ); diff --git a/dspace-api/src/main/resources/org/dspace/storage/rdbms/sqlmigration/postgres/V7.2_2022.07.28__Upgrade_to_Lindat_Clarin_schema.sql b/dspace-api/src/main/resources/org/dspace/storage/rdbms/sqlmigration/postgres/V7.2_2022.07.28__Upgrade_to_Lindat_Clarin_schema.sql index 32f1a403e1d9..89e792fdb420 100644 --- a/dspace-api/src/main/resources/org/dspace/storage/rdbms/sqlmigration/postgres/V7.2_2022.07.28__Upgrade_to_Lindat_Clarin_schema.sql +++ b/dspace-api/src/main/resources/org/dspace/storage/rdbms/sqlmigration/postgres/V7.2_2022.07.28__Upgrade_to_Lindat_Clarin_schema.sql @@ -134,7 +134,7 @@ SELECT pg_catalog.setval('license_label_label_id_seq', 19, true); CREATE TABLE license_resource_mapping ( mapping_id integer NOT NULL, - bitstream_uuid uuid UNIQUE, + bitstream_uuid uuid, license_id integer ); diff --git a/dspace-server-webapp/src/test/java/org/dspace/app/rest/ClarinLicenseRestRepositoryIT.java b/dspace-server-webapp/src/test/java/org/dspace/app/rest/ClarinLicenseRestRepositoryIT.java index cf0265676a0a..d6903bfdedd9 100644 --- a/dspace-server-webapp/src/test/java/org/dspace/app/rest/ClarinLicenseRestRepositoryIT.java +++ b/dspace-server-webapp/src/test/java/org/dspace/app/rest/ClarinLicenseRestRepositoryIT.java @@ -8,8 +8,11 @@ package org.dspace.app.rest; import static com.jayway.jsonpath.JsonPath.read; +import static org.apache.commons.codec.CharEncoding.UTF_8; +import static org.apache.commons.io.IOUtils.toInputStream; import static org.hamcrest.Matchers.is; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; @@ -18,6 +21,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import java.io.InputStream; import java.util.HashSet; import java.util.List; import java.util.Objects; @@ -33,6 +37,7 @@ import org.dspace.app.rest.projection.Projection; import org.dspace.app.rest.test.AbstractControllerIntegrationTest; import org.dspace.builder.*; +import org.dspace.content.Bitstream; import org.dspace.content.Collection; import org.dspace.content.Item; import org.dspace.content.clarin.ClarinLicense; @@ -72,6 +77,12 @@ public class ClarinLicenseRestRepositoryIT extends AbstractControllerIntegration ClarinLicenseLabel secondCLicenseLabel; ClarinLicenseLabel thirdCLicenseLabel; + ClarinLicenseResourceMapping clarinLicenseResourceMapping1; + ClarinLicenseResourceMapping clarinLicenseResourceMapping2; + + Bitstream firstBitstream; + Bitstream secondBitstream; + Item publicItem1; Item publicItem2; @@ -137,6 +148,10 @@ public void setup() throws Exception { .withIssueDate("2022-10-17") .withAuthor("Smith, Donald").withAuthor("Doe, John") .withSubject("ExtraEntry") + .withMetadata("dc", "rights", null, firstCLicense.getName()) + .withMetadata("dc", "rights", "uri", firstCLicense.getDefinition()) +// .withMetadata("dc", "rights", "label", +// Objects.requireNonNull(firstCLicense.getNonExtendedClarinLicenseLabel()).getLabel()) .build(); publicItem2 = ItemBuilder.createItem(context, col2) @@ -155,25 +170,28 @@ public void setup() throws Exception { .withSubject("ExtraEntry") .build(); - ClarinLicenseResourceMapping clarinLicenseResourceMapping1 = ClarinLicenseResourceMappingBuilder - .createClarinLicenseResourceMapping(context).build(); - clarinLicenseResourceMapping1.setLicenseId(firstCLicense.getID()); - clarinLicenseResourceMapping1.setBitstreamId(publicItem1.getID()); - clarinLicenseResourceMappingService.update(context, clarinLicenseResourceMapping1); - + // create bitstreams and add them with licenses to the clarin license resource mapping + firstBitstream = BitstreamBuilder.createBitstream(context, publicItem1, toInputStream("test 1", UTF_8)) + .withFormat("test format") + .build(); -// ClarinLicenseResourceMapping clarinLicenseResourceMapping2 = ClarinLicenseResourceMappingBuilder -// .createClarinLicenseResourceMapping(context).build(); -// clarinLicenseResourceMapping2.setLicenseId(firstCLicense.getID()); -// clarinLicenseResourceMapping2.setBitstreamId(publicItem2.getID()); -// clarinLicenseResourceMappingService.update(context, clarinLicenseResourceMapping2); + secondBitstream = BitstreamBuilder.createBitstream(context, publicItem1, toInputStream("test 2", UTF_8)) + .withFormat("test format") + .build(); + clarinLicenseResourceMapping1 = ClarinLicenseResourceMappingBuilder + .createClarinLicenseResourceMapping(context).build(); + firstCLicense.getClarinLicenseResourceMappings().add(clarinLicenseResourceMapping1); + clarinLicenseResourceMapping1.setLicense(firstCLicense); + clarinLicenseResourceMapping1.setBitstream(firstBitstream); + clarinLicenseResourceMappingService.update(context, clarinLicenseResourceMapping1); - ClarinLicenseResourceMapping clarinLicenseResourceMapping3 = ClarinLicenseResourceMappingBuilder + clarinLicenseResourceMapping2 = ClarinLicenseResourceMappingBuilder .createClarinLicenseResourceMapping(context).build(); - clarinLicenseResourceMapping3.setLicenseId(secondCLicense.getID()); - clarinLicenseResourceMapping3.setBitstreamId(publicItem3.getID()); - clarinLicenseResourceMappingService.update(context, clarinLicenseResourceMapping3); + firstCLicense.getClarinLicenseResourceMappings().add(clarinLicenseResourceMapping2); + clarinLicenseResourceMapping2.setLicense(firstCLicense); + clarinLicenseResourceMapping2.setBitstream(secondBitstream); + clarinLicenseResourceMappingService.update(context, clarinLicenseResourceMapping2); context.restoreAuthSystemState(); } @@ -218,7 +236,12 @@ public void findAll() throws Exception { @Test public void findAllBitstreamByLicenseId() throws Exception { - assertEquals(clarinLicenseResourceMappingService.findAllByLicenseId(context,firstCLicense.getID()),0); + ClarinLicense cl = clarinLicenseService.find(context, firstCLicense.getID()); + assertNotNull(cl); + assertEquals(cl.getClarinLicenseResourceMappings().size(),2); +// ClarinLicenseResourceMapping clarinLicenseResourceMapping = clarinLicenseResourceMappingService.find(context, clarinLicenseResourceMapping1.getID()); +// assertEquals(clarinLicenseResourceMapping.getBitstream().getID(), firstBitstream.getID()); + } @Test diff --git a/dspace/config/registries/dublin-core-types.xml b/dspace/config/registries/dublin-core-types.xml index 50e79b0d6151..d1107eea1c19 100644 --- a/dspace/config/registries/dublin-core-types.xml +++ b/dspace/config/registries/dublin-core-types.xml @@ -486,6 +486,13 @@ References terms governing use and reproduction. + + dc + rights + label + Rights label should be one of PUB, RES, ACA. + + dc source From c9644f7b2e40c8855aa144e8f9b16342c6efd886 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?MilanMajchr=C3=A1k?= Date: Fri, 23 Sep 2022 16:48:18 +0200 Subject: [PATCH 7/8] The bitstream is mapped to the license in the IT too --- .../rest/ClarinLicenseRestRepositoryIT.java | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/dspace-server-webapp/src/test/java/org/dspace/app/rest/ClarinLicenseRestRepositoryIT.java b/dspace-server-webapp/src/test/java/org/dspace/app/rest/ClarinLicenseRestRepositoryIT.java index d6903bfdedd9..cd7bfcfeab5b 100644 --- a/dspace-server-webapp/src/test/java/org/dspace/app/rest/ClarinLicenseRestRepositoryIT.java +++ b/dspace-server-webapp/src/test/java/org/dspace/app/rest/ClarinLicenseRestRepositoryIT.java @@ -178,22 +178,11 @@ public void setup() throws Exception { secondBitstream = BitstreamBuilder.createBitstream(context, publicItem1, toInputStream("test 2", UTF_8)) .withFormat("test format") .build(); + context.restoreAuthSystemState(); - clarinLicenseResourceMapping1 = ClarinLicenseResourceMappingBuilder - .createClarinLicenseResourceMapping(context).build(); - firstCLicense.getClarinLicenseResourceMappings().add(clarinLicenseResourceMapping1); - clarinLicenseResourceMapping1.setLicense(firstCLicense); - clarinLicenseResourceMapping1.setBitstream(firstBitstream); - clarinLicenseResourceMappingService.update(context, clarinLicenseResourceMapping1); - - clarinLicenseResourceMapping2 = ClarinLicenseResourceMappingBuilder - .createClarinLicenseResourceMapping(context).build(); - firstCLicense.getClarinLicenseResourceMappings().add(clarinLicenseResourceMapping2); - clarinLicenseResourceMapping2.setLicense(firstCLicense); - clarinLicenseResourceMapping2.setBitstream(secondBitstream); - clarinLicenseResourceMappingService.update(context, clarinLicenseResourceMapping2); + // without commit the clarin license resource mappings aren't mapped into th clarin license object + context.commit(); - context.restoreAuthSystemState(); } @Test @@ -237,6 +226,7 @@ public void findAll() throws Exception { @Test public void findAllBitstreamByLicenseId() throws Exception { ClarinLicense cl = clarinLicenseService.find(context, firstCLicense.getID()); + List clarinLicenseResourceMappings = clarinLicenseResourceMappingService.findAllByLicenseId(context, firstCLicense.getID()); assertNotNull(cl); assertEquals(cl.getClarinLicenseResourceMappings().size(),2); // ClarinLicenseResourceMapping clarinLicenseResourceMapping = clarinLicenseResourceMappingService.find(context, clarinLicenseResourceMapping1.getID()); From b7663c1a76b484180cd97c171001d2f4864e29bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?MilanMajchr=C3=A1k?= Date: Fri, 23 Sep 2022 17:02:14 +0200 Subject: [PATCH 8/8] a little refactoring --- .../rest/ClarinLicenseRestRepositoryIT.java | 42 ++++++++----------- 1 file changed, 17 insertions(+), 25 deletions(-) diff --git a/dspace-server-webapp/src/test/java/org/dspace/app/rest/ClarinLicenseRestRepositoryIT.java b/dspace-server-webapp/src/test/java/org/dspace/app/rest/ClarinLicenseRestRepositoryIT.java index cd7bfcfeab5b..fa85fd43f13c 100644 --- a/dspace-server-webapp/src/test/java/org/dspace/app/rest/ClarinLicenseRestRepositoryIT.java +++ b/dspace-server-webapp/src/test/java/org/dspace/app/rest/ClarinLicenseRestRepositoryIT.java @@ -77,12 +77,6 @@ public class ClarinLicenseRestRepositoryIT extends AbstractControllerIntegration ClarinLicenseLabel secondCLicenseLabel; ClarinLicenseLabel thirdCLicenseLabel; - ClarinLicenseResourceMapping clarinLicenseResourceMapping1; - ClarinLicenseResourceMapping clarinLicenseResourceMapping2; - - Bitstream firstBitstream; - Bitstream secondBitstream; - Item publicItem1; Item publicItem2; @@ -142,7 +136,8 @@ public void setup() throws Exception { Collection col2 = CollectionBuilder.createCollection(context, parentCommunity).withName("Collection2").build(); Collection col3 = CollectionBuilder.createCollection(context, parentCommunity).withName("Collection3").build(); - //create two items with the first license + // create two items with the first license + // the publicItem1 has license information added to the metadata publicItem1 = ItemBuilder.createItem(context, col1) .withTitle("Public item 1") .withIssueDate("2022-10-17") @@ -161,7 +156,7 @@ public void setup() throws Exception { .withSubject("TestingForMore").withSubject("ExtraEntry") .build(); - //create item with the second license + // create item with the second license publicItem3 = ItemBuilder.createItem(context, col3) .withTitle("Public item 3") .withIssueDate("2016-02-13") @@ -170,19 +165,8 @@ public void setup() throws Exception { .withSubject("ExtraEntry") .build(); - // create bitstreams and add them with licenses to the clarin license resource mapping - firstBitstream = BitstreamBuilder.createBitstream(context, publicItem1, toInputStream("test 1", UTF_8)) - .withFormat("test format") - .build(); - - secondBitstream = BitstreamBuilder.createBitstream(context, publicItem1, toInputStream("test 2", UTF_8)) - .withFormat("test format") - .build(); context.restoreAuthSystemState(); - // without commit the clarin license resource mappings aren't mapped into th clarin license object - context.commit(); - } @Test @@ -224,14 +208,22 @@ public void findAll() throws Exception { } @Test - public void findAllBitstreamByLicenseId() throws Exception { + public void findAllBitstreamsAttachedToLicense() throws Exception { + context.turnOffAuthorisationSystem(); + // create bitstreams and add them with licenses to the clarin license resource mapping + BitstreamBuilder.createBitstream(context, publicItem1, toInputStream("test 1", UTF_8)) + .withFormat("test format") + .build(); + + BitstreamBuilder.createBitstream(context, publicItem1, toInputStream("test 2", UTF_8)) + .withFormat("test format") + .build(); + context.restoreAuthSystemState(); + // without commit the clarin license resource mappings aren't mapped into th clarin license object + context.commit(); + ClarinLicense cl = clarinLicenseService.find(context, firstCLicense.getID()); - List clarinLicenseResourceMappings = clarinLicenseResourceMappingService.findAllByLicenseId(context, firstCLicense.getID()); - assertNotNull(cl); assertEquals(cl.getClarinLicenseResourceMappings().size(),2); -// ClarinLicenseResourceMapping clarinLicenseResourceMapping = clarinLicenseResourceMappingService.find(context, clarinLicenseResourceMapping1.getID()); -// assertEquals(clarinLicenseResourceMapping.getBitstream().getID(), firstBitstream.getID()); - } @Test