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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ public List<ClarinLicenseLabel> findAll(Context context) throws SQLException, Au
return clarinLicenseLabelDAO.findAll(context, ClarinLicenseLabel.class);
}

@Override
public ClarinLicenseLabel findByLabel(Context context, String label) throws SQLException {
return clarinLicenseLabelDAO.findByLabel(context, label);
}

@Override
public void delete(Context context, ClarinLicenseLabel license) throws SQLException, AuthorizeException {
if (!authorizeService.isAdmin(context)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ public List<ClarinLicense> findByNameLike(Context context, String name) throws S
return clarinLicenseDAO.findByNameLike(context, name);
}

@Override
public List<ClarinLicense> findByLabel(Context context, String label) throws SQLException {
return clarinLicenseDAO.findByLabel(context, label);
}

@Override
public void addLicenseMetadataToItem(Context context, ClarinLicense clarinLicense, Item item) throws SQLException {
if (Objects.isNull(clarinLicense) || Objects.isNull(item)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,6 @@ public interface ClarinLicenseDAO extends GenericDAO<ClarinLicense> {

List<ClarinLicense> findByNameLike(Context context, String name) throws SQLException;

List<ClarinLicense> findByLabel(Context context, String label) throws SQLException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
*/
package org.dspace.content.dao.clarin;

import java.sql.SQLException;

import org.dspace.content.clarin.ClarinLicenseLabel;
import org.dspace.core.Context;
import org.dspace.core.GenericDAO;

/**
Expand All @@ -19,4 +22,6 @@
* @author Milan Majchrak (milan.majchrak at dataquest.sk)
*/
public interface ClarinLicenseLabelDAO extends GenericDAO<ClarinLicenseLabel> {

ClarinLicenseLabel findByLabel(Context context, String label) throws SQLException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.SetJoin;

import org.dspace.content.clarin.ClarinLicense;
import org.dspace.content.clarin.ClarinLicenseLabel;
import org.dspace.content.clarin.ClarinLicenseLabel_;
import org.dspace.content.clarin.ClarinLicense_;
import org.dspace.content.dao.clarin.ClarinLicenseDAO;
import org.dspace.core.AbstractHibernateDAO;
Expand Down Expand Up @@ -54,4 +58,20 @@ public List<ClarinLicense> findByNameLike(Context context, String name) throws S
criteriaQuery.orderBy(criteriaBuilder.asc(clarinLicenseRoot.get(ClarinLicense_.name)));
return list(context, criteriaQuery, false, ClarinLicense.class, -1, -1);
}

@Override
public List<ClarinLicense> findByLabel(Context context, String label) throws SQLException {
CriteriaBuilder criteriaBuilder = getCriteriaBuilder(context);
CriteriaQuery<ClarinLicense> criteriaQuery = getCriteriaQuery(criteriaBuilder, ClarinLicense.class);
Root<ClarinLicense> clarinLicenseRoot = criteriaQuery.from(ClarinLicense.class);

SetJoin<ClarinLicense, ClarinLicenseLabel> labelJoin =
clarinLicenseRoot.joinSet(ClarinLicense_.CLARIN_LICENSE_LABELS);

Predicate labelPredicate = criteriaBuilder.equal(labelJoin.get(ClarinLicenseLabel_.LABEL), label);

criteriaQuery.select(clarinLicenseRoot).where(labelPredicate);

return list(context, criteriaQuery, false, ClarinLicense.class, -1, -1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,16 @@
*/
package org.dspace.content.dao.impl.clarin;

import java.sql.SQLException;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

import org.dspace.content.clarin.ClarinLicenseLabel;
import org.dspace.content.clarin.ClarinLicenseLabel_;
import org.dspace.content.dao.clarin.ClarinLicenseLabelDAO;
import org.dspace.core.AbstractHibernateDAO;
import org.dspace.core.Context;

/**
* Hibernate implementation of the Database Access Object interface class for the Clarin License Label object.
Expand All @@ -23,4 +30,14 @@ public class ClarinLicenseLabelDAOImpl extends AbstractHibernateDAO<ClarinLicens
protected ClarinLicenseLabelDAOImpl() {
super();
}

@Override
public ClarinLicenseLabel findByLabel(Context context, String label) throws SQLException {
CriteriaBuilder criteriaBuilder = getCriteriaBuilder(context);
CriteriaQuery<ClarinLicenseLabel> criteriaQuery = getCriteriaQuery(criteriaBuilder, ClarinLicenseLabel.class);
Root<ClarinLicenseLabel> cllRoot = criteriaQuery.from(ClarinLicenseLabel.class);
criteriaQuery.select(cllRoot);
criteriaQuery.where(criteriaBuilder.equal(cllRoot.get(ClarinLicenseLabel_.label), label));
return uniqueResult(context, criteriaQuery, true, ClarinLicenseLabel.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ ClarinLicenseLabel create(Context context, ClarinLicenseLabel clarinLicenseLabel
*/
ClarinLicenseLabel find(Context context, int valueId) throws SQLException;

/**
* Find the clarin license label object by label name
*
* @param context DSpace context object
* @param label label name of the searching clarin license label object
* @return found clarin license label object or null
* @throws SQLException if database error
*/
ClarinLicenseLabel findByLabel(Context context, String label) throws SQLException;

/**
* Find all clarin license label objects
* @param context DSpace context object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,16 @@ public interface ClarinLicenseService {
*/
List<ClarinLicense> findByNameLike(Context context, String name) throws SQLException;

/**
* Find Clarin Licenses by the license label.
*
* @param context DSpace context object
* @param label the license label
* @return List of clarin licenses which contain the specified license label.
* @throws SQLException if database error
*/
List<ClarinLicense> findByLabel(Context context, String label) throws SQLException;

void addLicenseMetadataToItem(Context context, ClarinLicense clarinLicense, Item item) throws SQLException;

void clearLicenseMetadataFromItem(Context context, Item item) throws SQLException;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--
-- 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/
--

ALTER TABLE license_label_extended_mapping
DROP CONSTRAINT IF EXISTS license_label_license_label_extended_mapping_fk;

-- here the "ON DELETE RESTRICT" clause (default clause) is used, which prevents deletion of a license_label record
-- when there are any license_label_extended_mapping records that reference it
ALTER TABLE license_label_extended_mapping
ADD CONSTRAINT license_label_license_label_extended_mapping_fk FOREIGN KEY (label_id) REFERENCES license_label(label_id);

ALTER TABLE license_label DROP CONSTRAINT IF EXISTS license_label_label_unique;
ALTER TABLE license_label ADD CONSTRAINT license_label_label_unique UNIQUE(label);
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--
-- 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/
--

ALTER TABLE license_label_extended_mapping
DROP CONSTRAINT IF EXISTS license_label_license_label_extended_mapping_fk;

-- here the "ON DELETE RESTRICT" clause (default clause) is used, which prevents deletion of a license_label record
-- when there are any license_label_extended_mapping records that reference it
ALTER TABLE license_label_extended_mapping
ADD CONSTRAINT license_label_license_label_extended_mapping_fk FOREIGN KEY (label_id) REFERENCES license_label(label_id);

ALTER TABLE license_label DROP CONSTRAINT IF EXISTS license_label_label_unique;
ALTER TABLE license_label ADD CONSTRAINT license_label_label_unique UNIQUE(label);
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.dspace.builder.BitstreamFormatBuilder;
import org.dspace.builder.BundleBuilder;
import org.dspace.builder.ClaimedTaskBuilder;
import org.dspace.builder.ClarinLicenseBuilder;
import org.dspace.builder.CollectionBuilder;
import org.dspace.builder.CommunityBuilder;
import org.dspace.builder.EPersonBuilder;
Expand Down Expand Up @@ -85,6 +86,7 @@ private void initMap() {
map.put(SiteBuilder.class.getName(), new ArrayList<>());
map.put(ProcessBuilder.class.getName(), new ArrayList<>());
map.put(PreviewContentBuilder.class.getName(), new ArrayList<>());
map.put(ClarinLicenseBuilder.class.getName(), new ArrayList<>());
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* 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.exception;

import javax.ws.rs.NotFoundException;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

/**
* Exception thrown when Clarin License Label not found
*
* @author Milan Kuchtiak
*/
@ResponseStatus(HttpStatus.NOT_FOUND)
public class ClarinLicenseLabelNotFoundException extends NotFoundException {

public ClarinLicenseLabelNotFoundException(String message) {
super(message);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,19 @@
import java.sql.SQLException;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import javax.servlet.http.HttpServletRequest;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.dspace.app.rest.exception.ClarinLicenseLabelNotFoundException;
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.ClarinLicense;
import org.dspace.content.clarin.ClarinLicenseLabel;
import org.dspace.content.service.clarin.ClarinLicenseLabelService;
import org.dspace.content.service.clarin.ClarinLicenseService;
import org.dspace.core.Context;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
Expand All @@ -36,9 +41,17 @@
@Component(ClarinLicenseLabelRest.CATEGORY + "." + ClarinLicenseLabelRest.NAME)
public class ClarinLicenseLabelRestRepository extends DSpaceRestRepository<ClarinLicenseLabelRest, Integer> {

private static final int MAX_LABEL_LENGTH = 5;

@Autowired
ClarinLicenseService clarinLicenseService;

@Autowired
ClarinLicenseLabelService clarinLicenseLabelService;

@Autowired
ObjectMapper objectMapper;

@Override
public ClarinLicenseLabelRest findOne(Context context, Integer id) {
ClarinLicenseLabel clarinLicenseLabel;
Expand Down Expand Up @@ -72,35 +85,107 @@ protected ClarinLicenseLabelRest createAndReturn(Context context)
// parse request body
ClarinLicenseLabelRest clarinLicenseLabelRest;
try {
clarinLicenseLabelRest = new ObjectMapper().readValue(
clarinLicenseLabelRest = 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())) {
throw new UnprocessableEntityException("CLARIN License Label title, label, icon cannot be null or empty");
checkLabelAndTitle(clarinLicenseLabelRest);
if (clarinLicenseLabelService.findByLabel(context, clarinLicenseLabelRest.getLabel().trim()) != null) {
throw new DSpaceBadRequestException("Clarin License Label with label " + clarinLicenseLabelRest.getLabel() +
" already exists");
}

// create
ClarinLicenseLabel clarinLicenseLabel;
clarinLicenseLabel = clarinLicenseLabelService.create(context);
// if (Objects.nonNull(clarinLicenseLabelRest.getId())) {
// clarinLicenseLabel.setId(clarinLicenseLabelRest.getId());
// }
clarinLicenseLabel.setLabel(clarinLicenseLabelRest.getLabel());
clarinLicenseLabel.setTitle(clarinLicenseLabelRest.getTitle());
clarinLicenseLabel.setIcon(clarinLicenseLabelRest.getIcon());
clarinLicenseLabel.setExtended(clarinLicenseLabelRest.isExtended());
updateClarinLicenseLabel(clarinLicenseLabel, clarinLicenseLabelRest);

clarinLicenseLabelService.update(context, clarinLicenseLabel);
// return
return converter.toRest(clarinLicenseLabel, utils.obtainProjection());
}

@Override
@PreAuthorize("hasAuthority('ADMIN')")
public ClarinLicenseLabelRest put(Context context,
HttpServletRequest request,
String apiCategory,
String model,
Integer id,
JsonNode jsonNode) throws SQLException, AuthorizeException {
ClarinLicenseLabel clarinLicenseLabel = clarinLicenseLabelService.find(context, id);
if (Objects.isNull(clarinLicenseLabel)) {
throw new ClarinLicenseLabelNotFoundException("Clarin License Label with id " + id + " was not found");
}

// parse request body
ClarinLicenseLabelRest clarinLicenseLabelRest;
try {
clarinLicenseLabelRest = objectMapper.treeToValue(jsonNode, ClarinLicenseLabelRest.class);
} catch (IOException excIO) {
throw new DSpaceBadRequestException("error parsing request body", excIO);
}

checkLabelAndTitle(clarinLicenseLabelRest);

ClarinLicenseLabel clarinLicenseLabelWithSameLabel = clarinLicenseLabelService.findByLabel(context,
clarinLicenseLabelRest.getLabel().trim());
if (clarinLicenseLabelWithSameLabel != null && !clarinLicenseLabelWithSameLabel.getID().equals(id)) {
throw new DSpaceBadRequestException("Clarin License Label with label " + clarinLicenseLabelRest.getLabel() +
" already exists");
}

clarinLicenseLabel.setId(id);
updateClarinLicenseLabel(clarinLicenseLabel, clarinLicenseLabelRest);

clarinLicenseLabelService.update(context, clarinLicenseLabel);

return converter.toRest(clarinLicenseLabel, utils.obtainProjection());
}

@Override
@PreAuthorize("hasAuthority('ADMIN')")
public void delete(Context context, Integer id) throws AuthorizeException {
ClarinLicenseLabel clarinLicenseLabel;
try {
clarinLicenseLabel = clarinLicenseLabelService.find(context, id);
if (Objects.isNull(clarinLicenseLabel)) {
throw new ClarinLicenseLabelNotFoundException("Clarin License Label with id " + id + " was not found");
}
List<ClarinLicense> licenses = clarinLicenseService.findByLabel(context, clarinLicenseLabel.getLabel());
if (!licenses.isEmpty()) {
throw new DSpaceBadRequestException("Clarin License Label " + clarinLicenseLabel.getLabel() +
" is in use and cannot be deleted");
}
clarinLicenseLabelService.delete(context, clarinLicenseLabel);
} catch (SQLException e) {
throw new RuntimeException(e.getMessage(), e);
}
}

private void checkLabelAndTitle(ClarinLicenseLabelRest clarinLicenseLabelRest) {
String label = Optional.ofNullable(clarinLicenseLabelRest.getLabel()).map(String::trim).orElse(null);
// validate fields
if (isBlank(label) || isBlank(clarinLicenseLabelRest.getTitle())) {
throw new DSpaceBadRequestException("CLARIN License Label title and label cannot be null or empty");
}
if (label.length() > MAX_LABEL_LENGTH) {
throw new DSpaceBadRequestException(
"CLARIN License Label -> label string cannot be longer than " + MAX_LABEL_LENGTH + " characters");
}
}

private static void updateClarinLicenseLabel(ClarinLicenseLabel clarinLicenseLabel,
ClarinLicenseLabelRest clarinLicenseLabelRest) {
clarinLicenseLabel.setLabel(clarinLicenseLabelRest.getLabel().trim());
clarinLicenseLabel.setTitle(clarinLicenseLabelRest.getTitle());
clarinLicenseLabel.setIcon(clarinLicenseLabelRest.getIcon());
clarinLicenseLabel.setExtended(clarinLicenseLabelRest.isExtended());
}

@Override
public Class<ClarinLicenseLabelRest> getDomainClass() {
Expand Down
Loading
Loading