DS-3699 enhance wrapper to pass integration test;

DS-3484 catch generic Exception
This commit is contained in:
Luigi Andrea Pascarelli
2017-11-03 17:00:16 +01:00
parent cdd9049b6b
commit 216d7481bb
4 changed files with 64 additions and 18 deletions

View File

@@ -482,7 +482,7 @@ public abstract class DSpaceObjectServiceImpl<T extends DSpaceObject> implements
} }
protected void getAuthoritiesAndConfidences(String fieldKey, Collection collection, List<String> values, List<String> authorities, List<Integer> confidences, int i) { protected void getAuthoritiesAndConfidences(String fieldKey, Collection collection, List<String> values, List<String> authorities, List<Integer> confidences, int i) {
Choices c = choiceAuthorityService.getBestMatch(fieldKey, values.get(i), null, null); Choices c = choiceAuthorityService.getBestMatch(fieldKey, values.get(i), collection, null);
authorities.add(c.values.length > 0 ? c.values[0].authority : null); authorities.add(c.values.length > 0 ? c.values[0].authority : null);
confidences.add(c.confidence); confidences.add(c.confidence);
} }

View File

@@ -7,15 +7,17 @@
*/ */
package org.dspace.content.authority; package org.dspace.content.authority;
import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
import java.util.Map; import java.util.Map;
import java.util.Set;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.dspace.app.util.DCInputsReader; import org.dspace.app.util.DCInputsReader;
import org.dspace.app.util.DCInputsReaderException; import org.dspace.app.util.DCInputsReaderException;
import org.dspace.content.Collection; import org.dspace.content.Collection;
import org.dspace.core.Utils;
/** /**
* This authority is registered automatically by the ChoiceAuthorityService for * This authority is registered automatically by the ChoiceAuthorityService for
@@ -54,12 +56,30 @@ public class InputFormSelfRegisterWrapperAuthority implements ChoiceAuthority {
String formName; String formName;
try { try {
init(); init();
formName = dci.getInputFormNameByCollectionAndField(collection, field); if(collection==null) {
return delegates.get(formName).getMatches(field, query, collection, start, limit, locale); Set<Choice> choices = new HashSet<Choice>();
//workaround search in all authority configured
for(ChoiceAuthority ca : delegates.values()) {
Choices tmp = ca.getMatches(field, query, null, start, limit, locale);
if(tmp.total>0) {
Set<Choice> mySet = new HashSet<Choice>(Arrays.asList(tmp.values));
choices.addAll(mySet);
}
}
if(!choices.isEmpty()) {
Choice[] results = new Choice[choices.size()-1];
choices.toArray(results);
return new Choices(results, 0, choices.size(), Choices.CF_AMBIGUOUS, false);
}
}
else {
formName = dci.getInputFormNameByCollectionAndField(collection, field);
return delegates.get(formName).getMatches(field, query, collection, start, limit, locale);
}
} catch (DCInputsReaderException e) { } catch (DCInputsReaderException e) {
log.error(e.getMessage(), e); log.error(e.getMessage(), e);
} }
return null; return new Choices(Choices.CF_NOTFOUND);
} }
@Override @Override
@@ -67,12 +87,30 @@ public class InputFormSelfRegisterWrapperAuthority implements ChoiceAuthority {
String formName; String formName;
try { try {
init(); init();
formName = dci.getInputFormNameByCollectionAndField(collection, field); if(collection==null) {
return delegates.get(formName).getBestMatch(field, text, collection, locale); Set<Choice> choices = new HashSet<Choice>();
//workaround search in all authority configured
for(ChoiceAuthority ca : delegates.values()) {
Choices tmp = ca.getBestMatch(field, text, null, locale);
if(tmp.total>0) {
Set<Choice> mySet = new HashSet<Choice>(Arrays.asList(tmp.values));
choices.addAll(mySet);
}
}
if(!choices.isEmpty()) {
Choice[] results = new Choice[choices.size()-1];
choices.toArray(results);
return new Choices(results, 0, choices.size(), Choices.CF_UNCERTAIN, false);
}
}
else {
formName = dci.getInputFormNameByCollectionAndField(collection, field);
return delegates.get(formName).getBestMatch(field, text, collection, locale);
}
} catch (DCInputsReaderException e) { } catch (DCInputsReaderException e) {
log.error(e.getMessage(), e); log.error(e.getMessage(), e);
} }
return null; return new Choices(Choices.CF_NOTFOUND);
} }
@Override @Override

View File

@@ -259,8 +259,9 @@ public class MetadataAuthorityServiceImpl implements MetadataAuthorityService
if (!StringUtils.equals(dcinput.getInputType(), "qualdrop_value")) { if (!StringUtils.equals(dcinput.getInputType(), "qualdrop_value")) {
String fieldKey = makeFieldKey(dcinput.getSchema(), dcinput.getElement(), String fieldKey = makeFieldKey(dcinput.getSchema(), dcinput.getElement(),
dcinput.getQualifier()); dcinput.getQualifier());
controlled.put(fieldKey, true); boolean req = ConfigurationManager.getBooleanProperty("authority.required."+fieldKey, false);
isAuthorityRequired.put(fieldKey, true); controlled.put(fieldKey, true);
isAuthorityRequired.put(fieldKey, req);
} }
} }
} }

View File

@@ -7,6 +7,10 @@
*/ */
package org.dspace.app.rest.builder; package org.dspace.app.rest.builder;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Date;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.dspace.authorize.AuthorizeException; import org.dspace.authorize.AuthorizeException;
import org.dspace.authorize.ResourcePolicy; import org.dspace.authorize.ResourcePolicy;
@@ -16,7 +20,14 @@ import org.dspace.authorize.service.ResourcePolicyService;
import org.dspace.content.DSpaceObject; import org.dspace.content.DSpaceObject;
import org.dspace.content.Item; import org.dspace.content.Item;
import org.dspace.content.factory.ContentServiceFactory; import org.dspace.content.factory.ContentServiceFactory;
import org.dspace.content.service.*; import org.dspace.content.service.BitstreamService;
import org.dspace.content.service.BundleService;
import org.dspace.content.service.CollectionService;
import org.dspace.content.service.CommunityService;
import org.dspace.content.service.DSpaceObjectService;
import org.dspace.content.service.InstallItemService;
import org.dspace.content.service.ItemService;
import org.dspace.content.service.WorkspaceItemService;
import org.dspace.core.Constants; import org.dspace.core.Constants;
import org.dspace.core.Context; import org.dspace.core.Context;
import org.dspace.discovery.IndexingService; import org.dspace.discovery.IndexingService;
@@ -30,10 +41,6 @@ import org.joda.time.DateTimeZone;
import org.joda.time.MutablePeriod; import org.joda.time.MutablePeriod;
import org.joda.time.format.PeriodFormat; import org.joda.time.format.PeriodFormat;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Date;
/** /**
* Abstract builder to construct DSpace Objects * Abstract builder to construct DSpace Objects
*/ */
@@ -88,7 +95,7 @@ public abstract class AbstractBuilder<T extends DSpaceObject> {
} }
protected <B> B handleException(final Exception e) { protected <B> B handleException(final Exception e) {
log.error(e); log.error(e.getMessage(), e);
return null; return null;
} }
@@ -97,7 +104,7 @@ public abstract class AbstractBuilder<T extends DSpaceObject> {
protected <B extends AbstractBuilder<T>> B addMetadataValue(final T dso, final String schema, final String element, final String qualifier, final String value) { protected <B extends AbstractBuilder<T>> B addMetadataValue(final T dso, final String schema, final String element, final String qualifier, final String value) {
try { try {
getDsoService().addMetadata(context, dso, schema, element, qualifier, Item.ANY, value); getDsoService().addMetadata(context, dso, schema, element, qualifier, Item.ANY, value);
} catch (SQLException e) { } catch (Exception e) {
return handleException(e); return handleException(e);
} }
return (B) this; return (B) this;
@@ -106,7 +113,7 @@ public abstract class AbstractBuilder<T extends DSpaceObject> {
protected <B extends AbstractBuilder<T>> B setMetadataSingleValue(final T dso, final String schema, final String element, final String qualifier, final String value) { protected <B extends AbstractBuilder<T>> B setMetadataSingleValue(final T dso, final String schema, final String element, final String qualifier, final String value) {
try { try {
getDsoService().setMetadataSingleValue(context, dso, schema, element, qualifier, Item.ANY, value); getDsoService().setMetadataSingleValue(context, dso, schema, element, qualifier, Item.ANY, value);
} catch (SQLException e) { } catch (Exception e) {
return handleException(e); return handleException(e);
} }