Merged w2p-57104_csv-import-delete-functionality into w2p-57107_mixing-entities-and-plaintest-values

This commit is contained in:
Raf Ponsaerts
2018-11-12 13:14:40 +01:00
10 changed files with 97 additions and 26 deletions

View File

@@ -638,6 +638,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);
@@ -671,7 +678,9 @@ public class MetadataImport {
handleRelationTypeMetadata(c, item, schema, element, qualifier, language, values, authorities, confidences);
} else {
handleRelationOtherMetadata(c, item, element, values);
for (String value : values) {
handleRelationOtherMetadata(c, item, element, value);
}
}
}
@@ -682,21 +691,21 @@ public class MetadataImport {
* @param c The relevant DSpace context
* @param item The item that the relationships will be made for
* @param element The string determining which relationshiptype is to be used
* @param values The value for the relationship
* @param value The value for the relationship
* @throws SQLException If something goes wrong
* @throws AuthorizeException If something goes wrong
*/
private void handleRelationOtherMetadata(Context c, Item item, String element, List<String> values)
private void handleRelationOtherMetadata(Context c, Item item, String element, String value)
throws SQLException, AuthorizeException {
Entity entity = entityService.findByItemId(c, item.getID());
boolean left = false;
List<RelationshipType> acceptableRelationshipTypes = new LinkedList<>();
String url = handleService.resolveToURL(c, values.get(0));
if (UUIDUtils.fromString(values.get(0)) == null && StringUtils.isNotBlank(url)) {
String url = handleService.resolveToURL(c, value);
if (UUIDUtils.fromString(value) == null && StringUtils.isNotBlank(url)) {
return;
}
Entity relationEntity = entityService.findByItemId(c, UUID.fromString(values.get(0)));
Entity relationEntity = entityService.findByItemId(c, UUID.fromString(value));
List<RelationshipType> leftRelationshipTypesForEntity = entityService.getLeftRelationshipTypes(c, entity);
@@ -727,30 +736,30 @@ public class MetadataImport {
return;
}
buildRelationObject(c, item, values, left, acceptableRelationshipTypes);
buildRelationObject(c, item, value, left, acceptableRelationshipTypes);
}
/**
* This method creates the relationship for the item and stores it in the database
* @param c The relevant DSpace context
* @param item The item for which this relationship will be constructed
* @param values The value for the relationship
* @param value The value for the relationship
* @param left A boolean indicating whether the item is the leftItem or the rightItem
* @param acceptableRelationshipTypes The acceptable relationship types
* @throws SQLException If something goes wrong
* @throws AuthorizeException If something goes wrong
*/
private void buildRelationObject(Context c, Item item, List<String> values, boolean left,
private void buildRelationObject(Context c, Item item, String value, boolean left,
List<RelationshipType> acceptableRelationshipTypes)
throws SQLException, AuthorizeException {
Relationship relationship = new Relationship();
RelationshipType acceptedRelationshipType = acceptableRelationshipTypes.get(0);
if (left) {
relationship.setLeftItem(item);
relationship.setRightItem(itemService.findByIdOrLegacyId(c, values.get(0)));
relationship.setRightItem(itemService.findByIdOrLegacyId(c, value));
} else {
relationship.setRightItem(item);
relationship.setLeftItem(itemService.findByIdOrLegacyId(c, values.get(0)));
relationship.setLeftItem(itemService.findByIdOrLegacyId(c, value));
}
relationship.setRelationshipType(acceptedRelationshipType);
relationship.setLeftPlace(relationshipService.findLeftPlaceByLeftItem(c, relationship.getLeftItem()) + 1);

View File

@@ -1292,21 +1292,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 {
@@ -1315,7 +1327,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));
}
}
@@ -1326,7 +1338,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, Concatenate> hashMaps = new HashMap<>();
@@ -1344,7 +1356,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

@@ -294,4 +294,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;
@@ -36,5 +37,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);