forked from DSpace/DSpace
-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathItemsUpdates.java
More file actions
200 lines (174 loc) · 8.71 KB
/
ItemsUpdates.java
File metadata and controls
200 lines (174 loc) · 8.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/**
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
*
* http://www.dspace.org/license/
*/
package org.dspace.subscriptions.dSpaceObjectsUpdates;
import static org.apache.commons.lang3.StringUtils.trimToEmpty;
import java.sql.SQLException;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.Logger;
import org.dspace.content.DSpaceObject;
import org.dspace.content.Item;
import org.dspace.content.service.CollectionService;
import org.dspace.content.service.CommunityService;
import org.dspace.content.service.ItemService;
import org.dspace.core.Context;
import org.dspace.discovery.DiscoverQuery;
import org.dspace.discovery.DiscoverResult;
import org.dspace.discovery.IndexableObject;
import org.dspace.discovery.SearchService;
import org.dspace.discovery.configuration.DiscoveryConfiguration;
import org.dspace.discovery.configuration.DiscoveryConfigurationService;
import org.dspace.discovery.configuration.DiscoveryRelatedItemConfiguration;
import org.dspace.discovery.configuration.DiscoverySortConfiguration;
import org.dspace.discovery.configuration.DiscoverySortFieldConfiguration;
import org.dspace.discovery.configuration.DiscoverySortFunctionConfiguration;
import org.dspace.discovery.indexobject.IndexableCollection;
import org.dspace.discovery.indexobject.IndexableCommunity;
import org.dspace.discovery.indexobject.IndexableItem;
import org.dspace.subscriptions.ContentGenerator;
import org.dspace.subscriptions.service.DSpaceObjectUpdates;
/**
* Class which will be used to find
* all item objects updated related with subscribed DSO
*
* @author Alba Aliu
*/
public class ItemsUpdates implements DSpaceObjectUpdates {
private final CollectionService collectionService;
private final CommunityService communityService;
private final ItemService itemService;
private DiscoveryConfigurationService searchConfigurationService;
private SearchService searchService;
private final Logger log = org.apache.logging.log4j.LogManager.getLogger(ContentGenerator.class);
@Override
public List<IndexableObject> findUpdates(Context context, DSpaceObject dSpaceObject, String frequency) {
List<IndexableObject> list = new ArrayList<>();
// entity type found
String inverseRelationName = "RELATION." + itemService.getEntityTypeLabel((Item) dSpaceObject);
List<DiscoveryConfiguration> discoveryConfigurationList =
searchConfigurationService.getDiscoveryConfigurationWithPrefixName(inverseRelationName);
DiscoverQuery discoverQuery = null;
DiscoverResult searchResult = null;
IndexableObject indexableObject = resolveScope(context, dSpaceObject.getID().toString());
try {
for (DiscoveryConfiguration discoveryConfiguration : discoveryConfigurationList) {
discoverQuery = buildDiscoveryQuery(discoveryConfiguration, indexableObject);
discoverQuery.addFilterQueries("lastModified_dt:" + this.findLastFrequency(frequency));
searchResult = searchService.search(context, discoverQuery);
list.addAll(searchResult.getIndexableObjects());
}
} catch (Exception e) {
log.error(e);
}
return list;
}
private IndexableObject<?, ?> resolveScope(Context context, String scope) {
IndexableObject<?, ?> scopeObj = null;
if (StringUtils.isBlank(scope)) {
return scopeObj;
}
try {
UUID uuid = UUID.fromString(scope);
scopeObj = new IndexableCommunity(communityService.find(context, uuid));
if (scopeObj.getIndexedObject() == null) {
scopeObj = new IndexableCollection(collectionService.find(context, uuid));
}
if (scopeObj.getIndexedObject() == null) {
scopeObj = new IndexableItem(itemService.find(context, uuid));
}
} catch (IllegalArgumentException ex) {
String message = "The given scope string " + trimToEmpty(scope) + " is not a UUID";
log.error(message);
} catch (SQLException ex) {
String message = "Unable to retrieve DSpace Object with ID " + trimToEmpty(scope) + " from the database";
log.error(message);
}
return scopeObj;
}
private DiscoverQuery buildDiscoveryQuery(DiscoveryConfiguration discoveryConfiguration,
IndexableObject<?, ?> scope) throws SQLException {
DiscoverQuery discoverQuery = buildBaseQuery(discoveryConfiguration, scope);
discoverQuery.addDSpaceObjectFilter(IndexableItem.TYPE);
configureSorting(discoverQuery, discoveryConfiguration.getSearchSortConfiguration(), scope);
return discoverQuery;
}
private void configureSorting(DiscoverQuery queryArgs,
DiscoverySortConfiguration searchSortConfiguration,
final IndexableObject scope) {
String sortBy = getDefaultSortField(searchSortConfiguration);
String sortOrder = getDefaultSortDirection(searchSortConfiguration);
//Update Discovery query
DiscoverySortFieldConfiguration sortFieldConfiguration = searchSortConfiguration
.getSortFieldConfiguration(sortBy);
if (sortFieldConfiguration != null) {
String sortField;
if (DiscoverySortFunctionConfiguration.SORT_FUNCTION.equals(sortFieldConfiguration.getType())) {
sortField = MessageFormat.format(
((DiscoverySortFunctionConfiguration) sortFieldConfiguration).getFunction(scope.getID()),
scope.getID());
} else {
sortField = searchService
.toSortFieldIndex(
sortFieldConfiguration.getMetadataField(), sortFieldConfiguration.getType());
}
if ("asc".equalsIgnoreCase(sortOrder)) {
queryArgs.setSortField(sortField, DiscoverQuery.SORT_ORDER.asc);
} else if ("desc".equalsIgnoreCase(sortOrder)) {
queryArgs.setSortField(sortField, DiscoverQuery.SORT_ORDER.desc);
} else {
log.error(sortOrder + " is not a valid sort order");
}
} else {
log.error(sortBy + " is not a valid sort field");
}
}
private String getDefaultSortDirection(DiscoverySortConfiguration searchSortConfiguration) {
return searchSortConfiguration.getSortFields().iterator().next().getDefaultSortOrder().toString();
}
private String getDefaultSortField(DiscoverySortConfiguration searchSortConfiguration) {
String sortBy;// Attempt to find the default one, if none found we use SCORE
sortBy = "score";
if (Objects.nonNull(searchSortConfiguration.getSortFields()) &&
!searchSortConfiguration.getSortFields().isEmpty()) {
DiscoverySortFieldConfiguration defaultSort = searchSortConfiguration.getSortFields().get(0);
if (org.apache.commons.lang.StringUtils.isBlank(defaultSort.getMetadataField())) {
return sortBy;
}
sortBy = defaultSort.getMetadataField();
}
return sortBy;
}
private DiscoverQuery buildBaseQuery(DiscoveryConfiguration discoveryConfiguration, IndexableObject<?, ?> scope) {
DiscoverQuery discoverQuery = new DiscoverQuery();
if (discoveryConfiguration == null) {
return discoverQuery;
}
discoverQuery.setDiscoveryConfigurationName(discoveryConfiguration.getId());
List<String> filterQueries = discoveryConfiguration.getDefaultFilterQueries();
for (String filterQuery : filterQueries) {
if (discoveryConfiguration instanceof DiscoveryRelatedItemConfiguration) {
discoverQuery.addFilterQueries(MessageFormat.format(filterQuery, scope.getID()));
} else {
discoverQuery.addFilterQueries(filterQuery);
}
}
return discoverQuery;
}
public ItemsUpdates(CollectionService collectionService, CommunityService communityService, ItemService itemService,
DiscoveryConfigurationService searchConfigurationService, SearchService searchService) {
this.collectionService = collectionService;
this.communityService = communityService;
this.itemService = itemService;
this.searchConfigurationService = searchConfigurationService;
this.searchService = searchService;
}
}