[Task 57104] adding delete functionality for the csv import

This commit is contained in:
Raf Ponsaerts
2018-11-06 15:30:13 +01:00
parent d8ced6aa7d
commit bfd0bd7362
10 changed files with 84 additions and 15 deletions

View File

@@ -629,6 +629,13 @@ public class MetadataImport {
if (StringUtils.equals(schema, "relation")) {
List<RelationshipType> relationshipTypeList = relationshipTypeService.findByLeftOrRightLabel(c, element);
for (RelationshipType relationshipType : relationshipTypeList) {
for (Relationship relationship : relationshipService.findByItemAndRelationshipType(c, item, relationshipType)) {
relationshipService.delete(c, relationship);
relationshipService.update(c, relationship);
}
}
handleRelationMetadata(c, item, schema, element, qualifier, language, values, authorities, confidences);
} else {
itemService.clearMetadata(c, item, schema, element, qualifier, language);

View File

@@ -1291,21 +1291,33 @@ prevent the generation of resource policy entry values with null dspace_object a
*/
@Override
public List<MetadataValue> getMetadata(Item item, String schema, String element, String qualifier, String lang) {
List<MetadataValue> dbMetadataValues = super.getMetadata(item, schema, element, qualifier, lang);
if (StringUtils.equals(schema, "relation") && !StringUtils.equals(element, "type")) {
if (!(StringUtils.equals(schema, "*") && StringUtils.equals(element, "*") &&
StringUtils.equals(qualifier, "*") && StringUtils.equals(lang, "*"))) {
return dbMetadataValues;
List<MetadataValue> relationMetadata = getRelationshipMetadata(item, false);
List<MetadataValue> listToReturn = new LinkedList<>();
for (MetadataValue metadataValue : relationMetadata) {
if (StringUtils.equals(metadataValue.getMetadataField().getElement(), element)) {
listToReturn.add(metadataValue);
}
}
return listToReturn;
} else {
List<MetadataValue> dbMetadataValues = super.getMetadata(item, schema, element, qualifier, lang);
if (!(StringUtils.equals(schema, "*") && StringUtils.equals(element, "*") &&
StringUtils.equals(qualifier, "*") && StringUtils.equals(lang, "*"))) {
return dbMetadataValues;
}
List<MetadataValue> fullMetadataValueList = getRelationshipMetadata(item, true);
fullMetadataValueList.addAll(dbMetadataValues);
return fullMetadataValueList;
}
List<MetadataValue> fullMetadataValueList = getRelationshipMetadata(item);
fullMetadataValueList.addAll(dbMetadataValues);
return fullMetadataValueList;
}
@Override
public List<MetadataValue> getRelationshipMetadata(Item item) {
public List<MetadataValue> getRelationshipMetadata(Item item, boolean extra) {
Context context = new Context();
List<MetadataValue> fullMetadataValueList = new LinkedList<>();
try {
@@ -1314,7 +1326,7 @@ prevent the generation of resource policy entry values with null dspace_object a
if (StringUtils.isNotBlank(entityType)) {
List<Relationship> relationships = relationshipService.findByItem(context, item);
for (Relationship relationship : relationships) {
fullMetadataValueList.addAll(handleItemRelationship(item, entityType, relationship));
fullMetadataValueList.addAll(handleItemRelationship(item, entityType, relationship, extra));
}
}
@@ -1325,7 +1337,7 @@ prevent the generation of resource policy entry values with null dspace_object a
}
private List<MetadataValue> handleItemRelationship(Item item, String entityType,
Relationship relationship) {
Relationship relationship, boolean extra) {
List<MetadataValue> resultingMetadataValueList = new LinkedList<>();
RelationshipType relationshipType = relationship.getRelationshipType();
HashMap<String, List<String>> hashMaps = new HashMap<>();
@@ -1343,7 +1355,7 @@ prevent the generation of resource policy entry values with null dspace_object a
relationName = relationship.getRelationshipType().getRightLabel();
}
if (hashMaps != null) {
if (hashMaps != null && extra) {
resultingMetadataValueList.addAll(handleRelationshipTypeMetadataMappping(item, hashMaps,
otherItem, relationName));
}

View File

@@ -286,4 +286,17 @@ public class RelationshipServiceImpl implements RelationshipService {
}
return listToReturn;
}
public List<Relationship> findByItemAndRelationshipType(Context context, Item item,
RelationshipType relationshipType)
throws SQLException {
List<Relationship> list = this.findByItem(context, item);
List<Relationship> listToReturn = new LinkedList<>();
for (Relationship relationship : list) {
if (relationship.getRelationshipType().equals(relationshipType)) {
listToReturn.add(relationship);
}
}
return listToReturn;
}
}

View File

@@ -84,4 +84,8 @@ public class RelationshipTypeServiceImpl implements RelationshipTypeService {
}
relationshipTypeDAO.delete(context, relationshipType);
}
public List<RelationshipType> findByLeftOrRightLabel(Context context, String label) throws SQLException {
return relationshipTypeDAO.findByLeftOrRightLabel(context, label);
}
}

View File

@@ -8,6 +8,7 @@
package org.dspace.content.dao;
import java.sql.SQLException;
import java.util.List;
import org.dspace.content.EntityType;
import org.dspace.content.RelationshipType;
@@ -25,5 +26,6 @@ public interface RelationshipTypeDAO extends GenericDAO<RelationshipType> {
RelationshipType findbyTypesAndLabels(Context context,
EntityType leftType,EntityType rightType,String leftLabel,String rightLabel)
throws SQLException;
List<RelationshipType> findByLeftOrRightLabel(Context context, String label) throws SQLException;
}

View File

@@ -8,6 +8,7 @@
package org.dspace.content.dao.impl;
import java.sql.SQLException;
import java.util.List;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
@@ -37,4 +38,18 @@ public class RelationshipTypeDAOImpl extends AbstractHibernateDAO<RelationshipTy
return uniqueResult(context, criteriaQuery, false, RelationshipType.class, -1, -1);
}
public List<RelationshipType> findByLeftOrRightLabel(Context context, String label) throws SQLException {
CriteriaBuilder criteriaBuilder = getCriteriaBuilder(context);
CriteriaQuery criteriaQuery = getCriteriaQuery(criteriaBuilder, RelationshipType.class);
Root<RelationshipType> relationshipTypeRoot = criteriaQuery.from(RelationshipType.class);
criteriaQuery.select(relationshipTypeRoot);
criteriaQuery.where(
criteriaBuilder.or(
criteriaBuilder.equal(relationshipTypeRoot.get(RelationshipType_.leftLabel), label),
criteriaBuilder.equal(relationshipTypeRoot.get(RelationshipType_.rightLabel), label)
)
);
return list(context, criteriaQuery, true, RelationshipType.class, -1, -1);
}
}

View File

@@ -648,6 +648,6 @@ public interface ItemService extends DSpaceObjectService<Item>, DSpaceObjectLega
* @param item The Item that will be processed through it's Relationships
* @return The list of MetadataValue objects constructed through the Relationships
*/
public List<MetadataValue> getRelationshipMetadata(Item item);
public List<MetadataValue> getRelationshipMetadata(Item item, boolean extra);
}

View File

@@ -13,6 +13,7 @@ import java.util.List;
import org.dspace.authorize.AuthorizeException;
import org.dspace.content.Item;
import org.dspace.content.Relationship;
import org.dspace.content.RelationshipType;
import org.dspace.core.Context;
import org.dspace.service.DSpaceCRUDService;
@@ -71,4 +72,8 @@ public interface RelationshipService extends DSpaceCRUDService<Relationship> {
* @throws SQLException If something goes wrong
*/
int findRightPlaceByRightItem(Context context, Item item) throws SQLException;
public List<Relationship> findByItemAndRelationshipType(Context context, Item item,
RelationshipType relationshipType)
throws SQLException;
}

View File

@@ -52,4 +52,15 @@ public interface RelationshipTypeService extends DSpaceCRUDService<RelationshipT
* @throws SQLException If something goes wrong
*/
List<RelationshipType> findAll(Context context) throws SQLException;
/**
* Retrieves all RelationshipType objects that have a left or right label that is
* equal to the given String
* @param context The relevant DSpace context
* @param label The label that has to match
* @return The list of all RelationshipType objects that have a left or right label
* that is equal to the given label param
* @throws SQLException If something goes wrong
*/
List<RelationshipType> findByLeftOrRightLabel(Context context, String label) throws SQLException;
}

View File

@@ -96,7 +96,7 @@ public class ItemConverter extends DSpaceObjectConverter<org.dspace.content.Item
List<MetadataValue> fullList = new LinkedList<>();
fullList.addAll(obj.getMetadata());
fullList.addAll(itemService.getRelationshipMetadata(obj));
fullList.addAll(itemService.getRelationshipMetadata(obj, true));
List<MetadataEntryRest> metadata = super.convertMetadataToRest(fullList);
item.setMetadata(metadata);