Skip to content

Commit db2767f

Browse files
committed
GND_8: Update AuthorityValue and GNDAuthorityValue
Improve AuthorityValue: - 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 GND_8: Update AuthorityValue and GNDAuthorityValue GNDAuthorityValue was not init'ing properly. In fact, it is not required for normal GND usage, but it does let us do things like last mod comparison GND_8: Authority Value models
1 parent 2502be9 commit db2767f

File tree

4 files changed

+77
-142
lines changed

4 files changed

+77
-142
lines changed

dspace-api/src/main/java/org/dspace/authority/AuthorityValue.java

Lines changed: 64 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import org.apache.solr.common.SolrDocument;
2828
import org.apache.solr.common.SolrInputDocument;
2929
import org.dspace.authorize.AuthorizeException;
30-
import org.dspace.content.Item;
3130
import org.dspace.content.MetadataValue;
3231
import org.dspace.content.factory.ContentServiceFactory;
3332
import org.dspace.core.Context;
@@ -43,34 +42,39 @@ public class AuthorityValue {
4342
/**
4443
* The id of the record in solr
4544
*/
46-
private String id;
45+
protected String id;
4746

4847
/**
4948
* The metadata field that this authority value is for
5049
*/
51-
private String field;
50+
protected String field;
5251

5352
/**
5453
* The text value of this authority
5554
*/
56-
private String value;
55+
protected String value;
5756

5857
/**
5958
* When this authority record has been created
6059
*/
61-
private Date creationDate;
60+
protected Date creationDate;
6261

6362
/**
6463
* If this authority has been removed
6564
*/
66-
private boolean deleted;
65+
protected boolean deleted;
6766

6867
/**
6968
* represents the last time that DSpace got updated information from its external source
7069
*/
71-
private Date lastModified;
70+
protected Date lastModified;
7271

73-
private Map<String, List<String>> otherMetadataMap;
72+
protected Map<String, List<String>> otherMetadataMap;
73+
74+
/**
75+
* log4j logger
76+
*/
77+
private static final Logger log = LogManager.getLogger();
7478

7579
public AuthorityValue() {
7680
this.otherMetadataMap = new LinkedHashMap<>();
@@ -150,7 +154,8 @@ public void delete() {
150154
}
151155

152156
/**
153-
* Generate a solr record from this instance
157+
* Generate a Solr record from this instance. The otherMetadataMap values will be iterated
158+
* and added with a label_ prefix
154159
*
155160
* @return SolrInputDocument
156161
*/
@@ -176,7 +181,9 @@ public SolrInputDocument getSolrInputDocument() {
176181
}
177182

178183
/**
179-
* Initialize this instance based on a solr record
184+
* Set values for this AuthorityValue based on a Solr record.
185+
* Any supplementary information saved with the label_ prefix will be added to the
186+
* otherMetadataMap with the label_ prefix stripped.
180187
*
181188
* @param document SolrDocument
182189
*/
@@ -190,34 +197,33 @@ public void setValues(SolrDocument document) {
190197
// Set other metadata
191198
for (String key : document.getFieldNames()) {
192199
if (key.startsWith("label_")) {
193-
String gndKey = key.replace("label_", "");
200+
String otherMetadataKey = key.replace("label_", "");
194201
Collection<Object> values = document.getFieldValues(key);
195202
if (values != null) {
196203
Collection<String> stringValues = (Collection<String>) (Object) values;
197-
otherMetadataMap.put(gndKey, new ArrayList<>(stringValues));
204+
otherMetadataMap.put(otherMetadataKey, new ArrayList<>(stringValues));
198205
}
199206
}
200207
}
201208
}
202209

203210
/**
204-
* Replace an item's DCValue with this authority
211+
* Replace an item's DCValue with this authority value and key
205212
*
206213
* @param context context
207214
* @param value metadata value
208-
* @param currentItem item
209215
* @throws SQLException if database error
210216
* @throws AuthorizeException if authorization error
211217
*/
212-
public void updateItem(Context context, Item currentItem, MetadataValue value)
218+
public void updateItem(Context context, MetadataValue value)
213219
throws SQLException, AuthorizeException {
214220
value.setValue(getValue());
215221
value.setAuthority(getId());
216222
ContentServiceFactory.getInstance().getMetadataValueService().update(context, value, true);
217223
}
218224

219225
/**
220-
* Information that can be used the choice ui.
226+
* Information that can be used with the ChoiceAuthority UI
221227
*
222228
* @return map
223229
*/
@@ -229,7 +235,7 @@ public Map<String, String> choiceSelectMap() {
229235
* Build a list of ISO date formatters to parse various forms.
230236
*
231237
* <p><strong>Note:</strong> any formatter which does not parse a zone or
232-
* offset must have a default zone set. See {@link stringToDate}.
238+
* offset must have a default zone set. See {@link #stringToDate}.
233239
*
234240
* @return the formatters.
235241
*/
@@ -267,20 +273,39 @@ static public Date stringToDate(String date) {
267273
}
268274

269275
/**
270-
* log4j logger
276+
* Set a key-value pair in the otherMetadataMap.
277+
*
278+
* @param key the key for the metadata entry
279+
* @param value the value for the metadata entry
271280
*/
272-
private static Logger log = LogManager.getLogger();
281+
public void setMetadataMapEntry(String key, String value) {
282+
List<String> values;
283+
if (!this.otherMetadataMap.containsKey(key)) {
284+
values = new ArrayList<>();
285+
} else {
286+
values = this.otherMetadataMap.get(key);
287+
}
288+
values.add(value);
289+
this.otherMetadataMap.put(key, values);
290+
}
273291

274-
@Override
275-
public String toString() {
276-
return "AuthorityValue{" +
277-
"id='" + id + '\'' +
278-
", field='" + field + '\'' +
279-
", value='" + value + '\'' +
280-
", creationDate=" + creationDate +
281-
", deleted=" + deleted +
282-
", lastModified=" + lastModified +
283-
'}';
292+
/**
293+
* Retrieves the other metadata map of the AuthorityValue object.
294+
* In the solr document this is in the form of label_{dc.example.field}
295+
*
296+
* @return the other metadata map of the AuthorityValue object
297+
*/
298+
public Map<String, List<String>> getOtherMetadataMap() {
299+
return otherMetadataMap;
300+
}
301+
302+
/**
303+
* Set the other metadata map
304+
*
305+
* @param otherMetadataMap the map of fields and values
306+
*/
307+
public void setOtherMetadataMap(Map<String, List<String>> otherMetadataMap) {
308+
this.otherMetadataMap = otherMetadataMap;
284309
}
285310

286311
/**
@@ -335,9 +360,6 @@ public boolean hasTheSameInformationAs(Object o) {
335360
if (deleted != that.deleted) {
336361
return false;
337362
}
338-
if (field != null ? !field.equals(that.field) : that.field != null) {
339-
return false;
340-
}
341363
if (id != null ? !id.equals(that.id) : that.id != null) {
342364
return false;
343365
}
@@ -348,22 +370,15 @@ public boolean hasTheSameInformationAs(Object o) {
348370
return true;
349371
}
350372

351-
public void setMetadataMapEntry(String key, String value) {
352-
List<String> values;
353-
if (!this.otherMetadataMap.containsKey(key)) {
354-
values = new ArrayList<>();
355-
} else {
356-
values = this.otherMetadataMap.get(key);
357-
}
358-
values.add(value);
359-
this.otherMetadataMap.put(key, values);
360-
}
361-
362-
public Map<String, List<String>> getOtherMetadataMap() {
363-
return otherMetadataMap;
364-
}
365-
366-
public void setOtherMetadataMap(Map<String, List<String>> otherMetadataMap) {
367-
this.otherMetadataMap = otherMetadataMap;
373+
@Override
374+
public String toString() {
375+
return "AuthorityValue{" +
376+
"id='" + id + '\'' +
377+
", field='" + field + '\'' +
378+
", value='" + value + '\'' +
379+
", creationDate=" + creationDate +
380+
", deleted=" + deleted +
381+
", lastModified=" + lastModified +
382+
'}';
368383
}
369384
}

dspace-api/src/main/java/org/dspace/authority/GNDAuthorityValue.java

Lines changed: 11 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,10 @@
77
*/
88
package org.dspace.authority;
99

10-
import java.sql.SQLException;
11-
import java.util.ArrayList;
1210
import java.util.Date;
13-
import java.util.List;
14-
import java.util.Map;
1511

1612
import org.apache.logging.log4j.LogManager;
1713
import org.apache.logging.log4j.Logger;
18-
import org.apache.solr.common.SolrDocument;
19-
import org.apache.solr.common.SolrInputDocument;
20-
import org.dspace.authorize.AuthorizeException;
21-
import org.dspace.content.Item;
22-
import org.dspace.content.MetadataValue;
23-
import org.dspace.content.factory.ContentServiceFactory;
24-
import org.dspace.core.Context;
2514

2615
/**
2716
* GND Authority Value is a simple extension of authority value that allows arbitrary
@@ -30,11 +19,12 @@
3019
* @author Kim Shepherd
3120
*/
3221
public class GNDAuthorityValue extends AuthorityValue {
22+
// Logger
23+
private static final Logger log = LogManager.getLogger();
3324

34-
private Map<String, List<String>> otherMetadataMap;
25+
public GNDAuthorityValue() {
3526

36-
// Logger
37-
private static final Logger log = LogManager.getLogger(GNDAuthorityValue.class);
27+
}
3828

3929
@Override
4030
public String getAuthorityType() {
@@ -43,88 +33,18 @@ public String getAuthorityType() {
4333

4434
@Override
4535
public Date getLastModified() {
46-
if (this.otherMetadataMap.containsKey("gnd.date.modified")
47-
&& this.otherMetadataMap.get("gnd.date.modified").size() > 0) {
48-
return stringToDate(this.otherMetadataMap.get("gnd.date.modified").get(0));
36+
if (otherMetadataMap.containsKey("gnd.date.modified")
37+
&& otherMetadataMap.get("gnd.date.modified").size() > 0) {
38+
return stringToDate(otherMetadataMap.get("gnd.date.modified").get(0));
4939
}
5040
return super.getLastModified();
5141
}
5242

53-
public void setMetadataMapEntry(String key, String value) {
54-
List<String> values;
55-
if (!this.otherMetadataMap.containsKey(key)) {
56-
values = new ArrayList<>();
57-
} else {
58-
values = this.otherMetadataMap.get(key);
59-
}
60-
values.add(value);
61-
this.otherMetadataMap.put(key, values);
62-
}
63-
64-
public Map<String, List<String>> getOtherMetadataMap() {
65-
return otherMetadataMap;
66-
}
67-
68-
public void setOtherMetadataMap(Map<String, List<String>> otherMetadataMap) {
69-
this.otherMetadataMap = otherMetadataMap;
70-
}
71-
72-
/**
73-
* Generate a solr record from this instance
74-
* @return SolrInputDocument
75-
*/
76-
@Override
77-
public SolrInputDocument getSolrInputDocument() {
78-
SolrInputDocument doc = super.getSolrInputDocument();
79-
80-
// Other metadata - set dynamic fields for each key
81-
// for (String metadataKey : otherMetadataMap.keySet()) {
82-
// List<String> metadata = otherMetadataMap.get(metadataKey);
83-
// for (String value : metadata) {
84-
// doc.addField("label_" + metadataKey, value);
85-
// }
86-
// }
87-
88-
return doc;
89-
}
90-
91-
/**
92-
* Set the values of this AuthorityValue based on the existing Solr record
93-
*
94-
* @param document SolrDocument
95-
*/
96-
@Override
97-
public void setValues(SolrDocument document) {
98-
super.setValues(document);
99-
// Set other metadata
100-
// for (String key : document.getFieldNames()) {
101-
// if (key.startsWith("label_")) {
102-
// String gndKey = key.replace("label_", "");
103-
// Collection<Object> values = document.getFieldValues(key);
104-
// if (values != null) {
105-
// Collection<String> stringValues = (Collection<String>) (Object) values;
106-
// otherMetadataMap.put(gndKey, new ArrayList<>(stringValues));
107-
// }
108-
// }
109-
// }
110-
}
111-
112-
/**
113-
* Replace an item's DCValue with this authority
114-
* Unlike base authority, we are not going to actually update the id at this stage. Or are we?
115-
*
116-
* @param context context
117-
* @param value metadata value
118-
* @param currentItem item
119-
* @throws SQLException if database error
120-
* @throws AuthorizeException if authorization error
121-
*/
12243
@Override
123-
public void updateItem(Context context, Item currentItem, MetadataValue value)
124-
throws SQLException, AuthorizeException {
125-
value.setValue(getValue());
126-
value.setAuthority(getId());
127-
ContentServiceFactory.getInstance().getMetadataValueService().update(context, value, true);
44+
public AuthorityValue newInstance(String info) {
45+
GNDAuthorityValue authorityValue = new GNDAuthorityValue();
46+
authorityValue.setValue(info);
47+
return authorityValue;
12848
}
12949

13050
}

dspace-api/src/main/java/org/dspace/authority/UpdateAuthorities.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ protected void updateItems(AuthorityValue authority) {
168168
while (itemIterator.hasNext()) {
169169
Item next = itemIterator.next();
170170
List<MetadataValue> metadata = itemService.getMetadata(next, authority.getField(), authority.getId());
171-
authority.updateItem(context, next, metadata.get(0)); //should be only one
171+
authority.updateItem(context, metadata.get(0)); //should be only one
172172
List<MetadataValue> metadataAfter = itemService
173173
.getMetadata(next, authority.getField(), authority.getId());
174174
if (!metadata.get(0).getValue().equals(metadataAfter.get(0).getValue())) {

dspace-api/src/main/java/org/dspace/authority/indexer/DSpaceAuthorityIndexer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public List<AuthorityValue> getAuthorityValues(Context context, Item item, Map<S
107107
}
108108
if (value != null) {
109109
if (requiresItemUpdate) {
110-
value.updateItem(context, item, metadataValue);
110+
value.updateItem(context, metadataValue);
111111
try {
112112
itemService.update(context, item);
113113
} catch (Exception e) {

0 commit comments

Comments
 (0)