[CST-7756] refactoring

This commit is contained in:
Mykhaylo
2023-02-01 17:13:42 +01:00
parent de44d9dbe7
commit 6a02994c5b

View File

@@ -7,23 +7,23 @@
*/ */
package org.dspace.subscriptions; package org.dspace.subscriptions;
import static org.dspace.core.Constants.COLLECTION;
import static org.dspace.core.Constants.COMMUNITY;
import static org.dspace.core.Constants.ITEM;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.dspace.content.Collection;
import org.dspace.content.Community;
import org.dspace.content.DSpaceObject; import org.dspace.content.DSpaceObject;
import org.dspace.content.Item;
import org.dspace.core.Context; import org.dspace.core.Context;
import org.dspace.discovery.IndexableObject; import org.dspace.discovery.IndexableObject;
import org.dspace.eperson.Subscription; import org.dspace.eperson.Subscription;
@@ -32,8 +32,6 @@ import org.dspace.scripts.DSpaceRunnable;
import org.dspace.scripts.handler.DSpaceRunnableHandler; import org.dspace.scripts.handler.DSpaceRunnableHandler;
import org.dspace.subscriptions.service.DSpaceObjectUpdates; import org.dspace.subscriptions.service.DSpaceObjectUpdates;
import org.dspace.subscriptions.service.SubscriptionGenerator; import org.dspace.subscriptions.service.SubscriptionGenerator;
import org.hibernate.proxy.HibernateProxy;
import org.hibernate.proxy.LazyInitializer;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
/** /**
@@ -55,29 +53,24 @@ public class SubscriptionEmailNotificationServiceImpl implements SubscriptionEma
private Map<String, DSpaceObjectUpdates> contentUpdates = new HashMap<>(); private Map<String, DSpaceObjectUpdates> contentUpdates = new HashMap<>();
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
private Map<String, SubscriptionGenerator> subscriptionType2generators = new HashMap<>(); private Map<String, SubscriptionGenerator> subscriptionType2generators = new HashMap<>();
@SuppressWarnings("rawtypes")
private List<IndexableObject> communities = new ArrayList<>();
@SuppressWarnings("rawtypes")
private List<IndexableObject> collections = new ArrayList<>();
@SuppressWarnings("rawtypes")
private List<IndexableObject> items = new ArrayList<>();
@Autowired @Autowired
private SubscribeService subscribeService; private SubscribeService subscribeService;
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
public SubscriptionEmailNotificationServiceImpl(SubscribeService subscribeService, public SubscriptionEmailNotificationServiceImpl(Map<String, Set<String>> param2values,
Map<String, Set<String>> param2values,
Map<String, DSpaceObjectUpdates> contentUpdates, Map<String, DSpaceObjectUpdates> contentUpdates,
Map<String, SubscriptionGenerator> subscriptionType2generators) { Map<String, SubscriptionGenerator> subscriptionType2generators) {
this.param2values = param2values; this.param2values = param2values;
this.contentUpdates = contentUpdates; this.contentUpdates = contentUpdates;
this.subscribeService = subscribeService;
this.subscriptionType2generators = subscriptionType2generators; this.subscriptionType2generators = subscriptionType2generators;
} }
@SuppressWarnings("unchecked") @SuppressWarnings({ "rawtypes", "unchecked" })
public void perform(Context context, DSpaceRunnableHandler handler, String subscriptionType, String frequency) { public void perform(Context context, DSpaceRunnableHandler handler, String subscriptionType, String frequency) {
List<IndexableObject> items = new ArrayList<>();
List<IndexableObject> communities = new ArrayList<>();
List<IndexableObject> collections = new ArrayList<>();
try { try {
List<Subscription> subscriptions = List<Subscription> subscriptions =
findAllSubscriptionsBySubscriptionTypeAndFrequency(context, subscriptionType, frequency); findAllSubscriptionsBySubscriptionTypeAndFrequency(context, subscriptionType, frequency);
@@ -86,19 +79,21 @@ public class SubscriptionEmailNotificationServiceImpl implements SubscriptionEma
// the list of the person who has subscribed // the list of the person who has subscribed
int iterator = 0; int iterator = 0;
for (Subscription subscription : subscriptions) { for (Subscription subscription : subscriptions) {
DSpaceObject dSpaceObject = getdSpaceObject(subscription); DSpaceObject dSpaceObject = subscription.getDSpaceObject();
if (dSpaceObject instanceof Community) {
communities.addAll(contentUpdates.get(Community.class.getSimpleName().toLowerCase(Locale.ROOT)) if (dSpaceObject.getType() == COMMUNITY) {
.findUpdates(context, dSpaceObject, frequency)); communities.addAll(contentUpdates.get("community")
} else if (dSpaceObject instanceof Collection) { .findUpdates(context, dSpaceObject, frequency));
collections.addAll(contentUpdates.get(Collection.class.getSimpleName().toLowerCase(Locale.ROOT)) } else if (dSpaceObject.getType() == COLLECTION) {
.findUpdates(context, dSpaceObject, frequency)); collections.addAll(contentUpdates.get("collection")
} else if (dSpaceObject instanceof Item) { .findUpdates(context, dSpaceObject, frequency));
items.addAll(contentUpdates.get(Item.class.getSimpleName().toLowerCase(Locale.ROOT)) } else if (dSpaceObject.getType() == ITEM) {
.findUpdates(context, dSpaceObject, frequency)); items.addAll(contentUpdates.get("item").findUpdates(context, dSpaceObject, frequency));
} }
var ePerson = subscription.getEPerson(); var ePerson = subscription.getEPerson();
if (iterator < subscriptions.size() - 1) { if (iterator < subscriptions.size() - 1) {
// as the subscriptions are ordered by eperson id, so we send them by ePerson
if (ePerson.equals(subscriptions.get(iterator + 1).getEPerson())) { if (ePerson.equals(subscriptions.get(iterator + 1).getEPerson())) {
iterator++; iterator++;
continue; continue;
@@ -127,16 +122,15 @@ public class SubscriptionEmailNotificationServiceImpl implements SubscriptionEma
} }
} }
private DSpaceObject getdSpaceObject(Subscription subscription) { /**
DSpaceObject dSpaceObject = subscription.getDSpaceObject(); * Return all Subscriptions by subscriptionType and frequency ordered by ePerson ID
if (subscription.getDSpaceObject() instanceof HibernateProxy) { * if there are none it returns an empty list
HibernateProxy hibernateProxy = (HibernateProxy) subscription.getDSpaceObject(); *
LazyInitializer initializer = hibernateProxy.getHibernateLazyInitializer(); * @param context DSpace context
dSpaceObject = (DSpaceObject) initializer.getImplementation(); * @param subscriptionType Could be "content" or "statistics". NOTE: in DSpace we have only "content"
} * @param frequency Could be "D" stand for Day, "W" stand for Week, and "M" stand for Month
return dSpaceObject; * @return
} */
private List<Subscription> findAllSubscriptionsBySubscriptionTypeAndFrequency(Context context, private List<Subscription> findAllSubscriptionsBySubscriptionTypeAndFrequency(Context context,
String subscriptionType, String frequency) { String subscriptionType, String frequency) {
try { try {