Skip to content

Commit

Permalink
Improve / enhance AuthorityValue
Browse files Browse the repository at this point in the history
- Sets labels and otherMetadataMap
- Doesn't require field to be equal when
  comparing 'hasSameInformationAs'
- reordered some code and added javadoc
- took out 'item' param from updateItem
  • Loading branch information
kshepherd committed Sep 24, 2024
1 parent 2e5823b commit 9d80ef3
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 31 deletions.
115 changes: 86 additions & 29 deletions dspace-api/src/main/java/org/dspace/authority/AuthorityValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

Expand All @@ -25,7 +27,6 @@
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrInputDocument;
import org.dspace.authorize.AuthorizeException;
import org.dspace.content.Item;
import org.dspace.content.MetadataValue;
import org.dspace.content.factory.ContentServiceFactory;
import org.dspace.core.Context;
Expand All @@ -41,34 +42,42 @@ public class AuthorityValue {
/**
* The id of the record in solr
*/
private String id;
protected String id;

/**
* The metadata field that this authority value is for
*/
private String field;
protected String field;

/**
* The text value of this authority
*/
private String value;
protected String value;

/**
* When this authority record has been created
*/
private Date creationDate;
protected Date creationDate;

/**
* If this authority has been removed
*/
private boolean deleted;
protected boolean deleted;

/**
* represents the last time that DSpace got updated information from its external source
*/
private Date lastModified;
protected Date lastModified;

protected Map<String, List<String>> otherMetadataMap;

/**
* log4j logger
*/
private static final Logger log = LogManager.getLogger();

public AuthorityValue() {
this.otherMetadataMap = new LinkedHashMap<>();
}

public AuthorityValue(SolrDocument document) {
Expand Down Expand Up @@ -145,7 +154,8 @@ public void delete() {
}

/**
* Generate a solr record from this instance
* Generate a Solr record from this instance. The otherMetadataMap values will be iterated
* and added with a label_ prefix
*
* @return SolrInputDocument
*/
Expand All @@ -160,11 +170,20 @@ public SolrInputDocument getSolrInputDocument() {
doc.addField("creation_date", solrDateFormatter.format(getCreationDate()));
doc.addField("last_modified_date", solrDateFormatter.format(getLastModified()));
doc.addField("authority_type", getAuthorityType());
// Other metadata - set dynamic fields for each key
for (String metadataKey : otherMetadataMap.keySet()) {
List<String> metadata = otherMetadataMap.get(metadataKey);
for (String value : metadata) {
doc.addField("label_" + metadataKey, value);
}
}
return doc;
}

/**
* Initialize this instance based on a solr record
* Set values for this AuthorityValue based on a Solr record.
* Any supplementary information saved with the label_ prefix will be added to the
* otherMetadataMap with the label_ prefix stripped.
*
* @param document SolrDocument
*/
Expand All @@ -175,26 +194,36 @@ public void setValues(SolrDocument document) {
this.deleted = (Boolean) document.getFieldValue("deleted");
this.creationDate = (Date) document.getFieldValue("creation_date");
this.lastModified = (Date) document.getFieldValue("last_modified_date");
// Set other metadata
for (String key : document.getFieldNames()) {
if (key.startsWith("label_")) {
String otherMetadataKey = key.replace("label_", "");
Collection<Object> values = document.getFieldValues(key);
if (values != null) {
Collection<String> stringValues = (Collection<String>) (Object) values;
otherMetadataMap.put(otherMetadataKey, new ArrayList<>(stringValues));
}
}
}
}

/**
* Replace an item's DCValue with this authority
* Replace an item's DCValue with this authority value and key
*
* @param context context
* @param value metadata value
* @param currentItem item
* @throws SQLException if database error
* @throws AuthorizeException if authorization error
*/
public void updateItem(Context context, Item currentItem, MetadataValue value)
public void updateItem(Context context, MetadataValue value)
throws SQLException, AuthorizeException {
value.setValue(getValue());
value.setAuthority(getId());
ContentServiceFactory.getInstance().getMetadataValueService().update(context, value, true);
}

/**
* Information that can be used the choice ui.
* Information that can be used with the ChoiceAuthority UI
*
* @return map
*/
Expand All @@ -206,7 +235,7 @@ public Map<String, String> choiceSelectMap() {
* Build a list of ISO date formatters to parse various forms.
*
* <p><strong>Note:</strong> any formatter which does not parse a zone or
* offset must have a default zone set. See {@link stringToDate}.
* offset must have a default zone set. See {@link #stringToDate}.
*
* @return the formatters.
*/
Expand Down Expand Up @@ -244,20 +273,39 @@ static public Date stringToDate(String date) {
}

/**
* log4j logger
* Set a key-value pair in the otherMetadataMap.
*
* @param key the key for the metadata entry
* @param value the value for the metadata entry
*/
private static Logger log = LogManager.getLogger();
public void setMetadataMapEntry(String key, String value) {
List<String> values;
if (!this.otherMetadataMap.containsKey(key)) {
values = new ArrayList<>();
} else {
values = this.otherMetadataMap.get(key);
}
values.add(value);
this.otherMetadataMap.put(key, values);
}

@Override
public String toString() {
return "AuthorityValue{" +
"id='" + id + '\'' +
", field='" + field + '\'' +
", value='" + value + '\'' +
", creationDate=" + creationDate +
", deleted=" + deleted +
", lastModified=" + lastModified +
'}';
/**
* Retrieves the other metadata map of the AuthorityValue object.
* In the solr document this is in the form of label_{dc.example.field}
*
* @return the other metadata map of the AuthorityValue object
*/
public Map<String, List<String>> getOtherMetadataMap() {
return otherMetadataMap;
}

/**
* Set the other metadata map
*
* @param otherMetadataMap the map of fields and values
*/
public void setOtherMetadataMap(Map<String, List<String>> otherMetadataMap) {
this.otherMetadataMap = otherMetadataMap;
}

/**
Expand Down Expand Up @@ -312,9 +360,6 @@ public boolean hasTheSameInformationAs(Object o) {
if (deleted != that.deleted) {
return false;
}
if (field != null ? !field.equals(that.field) : that.field != null) {
return false;
}
if (id != null ? !id.equals(that.id) : that.id != null) {
return false;
}
Expand All @@ -324,4 +369,16 @@ public boolean hasTheSameInformationAs(Object o) {

return true;
}

@Override
public String toString() {
return "AuthorityValue{" +
"id='" + id + '\'' +
", field='" + field + '\'' +
", value='" + value + '\'' +
", creationDate=" + creationDate +
", deleted=" + deleted +
", lastModified=" + lastModified +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ protected void updateItems(AuthorityValue authority) {
while (itemIterator.hasNext()) {
Item next = itemIterator.next();
List<MetadataValue> metadata = itemService.getMetadata(next, authority.getField(), authority.getId());
authority.updateItem(context, next, metadata.get(0)); //should be only one
authority.updateItem(context, metadata.get(0)); //should be only one
List<MetadataValue> metadataAfter = itemService
.getMetadata(next, authority.getField(), authority.getId());
if (!metadata.get(0).getValue().equals(metadataAfter.get(0).getValue())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public List<AuthorityValue> getAuthorityValues(Context context, Item item, Map<S
}
if (value != null) {
if (requiresItemUpdate) {
value.updateItem(context, item, metadataValue);
value.updateItem(context, metadataValue);
try {
itemService.update(context, item);
} catch (Exception e) {
Expand Down

0 comments on commit 9d80ef3

Please sign in to comment.