Skip to content

Commit

Permalink
Merge branch 'dspace-8.0-rc1-sistedes' into dspace-8.0-sistedes
Browse files Browse the repository at this point in the history
  • Loading branch information
abelgomez committed Jul 3, 2024
2 parents b2ae1d5 + 87bbff4 commit a246a12
Show file tree
Hide file tree
Showing 24 changed files with 2,453 additions and 24 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,6 @@ rebel.xml

## Ignore jenv configuration
.java-version

## Ignore mergetool backup files
*.orig
105 changes: 105 additions & 0 deletions dspace-api/src/main/java/org/dspace/ctask/general/FilterMedia.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package org.dspace.ctask.general;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.ArrayUtils;
import org.dspace.app.mediafilter.FormatFilter;
import org.dspace.app.mediafilter.factory.MediaFilterServiceFactory;
import org.dspace.app.mediafilter.service.MediaFilterService;
import org.dspace.content.DSpaceObject;
import org.dspace.content.Item;
import org.dspace.core.Constants;
import org.dspace.core.SelfNamedPlugin;
import org.dspace.core.factory.CoreServiceFactory;
import org.dspace.curate.AbstractCurationTask;
import org.dspace.curate.Curator;
import org.dspace.services.factory.DSpaceServicesFactory;

/**
* Curation taks to apply media filters. Useful to execute this curation task as
* part of a submission workflow.
*
* @author agomez
*/
public class FilterMedia extends AbstractCurationTask {

// Key (in dspace.cfg) which lists all enabled filters by name
private static final String MEDIA_FILTER_PLUGINS_KEY = "filter.plugins";

// Prefix (in dspace.cfg) for all filter properties
private static final String FILTER_PREFIX = "filter";

// Suffix (in dspace.cfg) for input formats supported by each filter
private static final String INPUT_FORMATS_SUFFIX = "inputFormats";

// The MediaFilterService
private MediaFilterService mediaFilterService = MediaFilterServiceFactory.getInstance().getMediaFilterService();

protected String result = null;

// Static initializer
{
// Configure all enabled filters in the MediaFilterService
// Adapted from org.dspace.app.mediafilter.MediaFilterScript
String[] filterNames = DSpaceServicesFactory.getInstance().getConfigurationService().getArrayProperty(MEDIA_FILTER_PLUGINS_KEY);

List<FormatFilter> filterList = new ArrayList<>();
Map<String, List<String>> filterFormats = new HashMap<>();

for (int i = 0; i < filterNames.length; i++) {
FormatFilter filter = (FormatFilter) CoreServiceFactory.getInstance().getPluginService().getNamedPlugin(FormatFilter.class, filterNames[i]);
filterList.add(filter);
String filterClassName = filter.getClass().getName();
String pluginName = null;
if (SelfNamedPlugin.class.isAssignableFrom(filter.getClass())) {
pluginName = ((SelfNamedPlugin) filter).getPluginInstanceName();
}
String[] formats = DSpaceServicesFactory.getInstance().getConfigurationService().getArrayProperty(
FILTER_PREFIX + "." + filterClassName + (pluginName != null ? "." + pluginName : "") + "." + INPUT_FORMATS_SUFFIX);
if (ArrayUtils.isNotEmpty(formats)) {
filterFormats.put(filterClassName + (pluginName != null ? MediaFilterService.FILTER_PLUGIN_SEPARATOR + pluginName : ""),
Arrays.asList(formats));
}
}
mediaFilterService.setFilterFormats(filterFormats);
mediaFilterService.setFilterClasses(filterList);
}

protected String taskProperty(String name, String defaultValue) {
return super.taskProperty(name) != null ? super.taskProperty(name) : defaultValue;
}

@Override
public int perform(DSpaceObject dso) throws IOException {
if (dso instanceof Item) {
try {
if (mediaFilterService.filterItem(Curator.curationContext(), (Item) dso)) {
result = "Media filters succesfully applied on Item " + dso.getID();
setResult(result);
report(result);
return Curator.CURATE_SUCCESS;
} else {
result = "No media filters applied on Item " + dso.getID();
setResult(result);
report(result);
return Curator.CURATE_SKIP;
}
} catch (Exception e) {
result = "Failed to filter media on Item " + dso.getID();
setResult(result);
report(result);
return Curator.CURATE_FAIL;
}
} else {
result = "Skipping element that cannot be media filtered: " + dso.getID() + "[" + Constants.typeText[dso.getType()] + "]";
setResult(result);
report(result);
return Curator.CURATE_SKIP;
}
}
}
114 changes: 114 additions & 0 deletions dspace-api/src/main/java/org/dspace/ctask/general/FixJcis.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package org.dspace.ctask.general;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.dspace.content.Collection;
import org.dspace.content.Community;
import org.dspace.content.DSpaceObject;
import org.dspace.content.Item;
import org.dspace.content.MetadataFieldName;
import org.dspace.content.MetadataValue;
import org.dspace.content.factory.ContentServiceFactory;
import org.dspace.content.service.CollectionService;
import org.dspace.content.service.CommunityService;
import org.dspace.content.service.ItemService;
import org.dspace.curate.AbstractCurationTask;
import org.dspace.curate.Curator;

/**
* Curation task to fix the JCIS name
*
* @author agomez
*/
public class FixJcis extends AbstractCurationTask {

private CollectionService collectionService = ContentServiceFactory.getInstance().getCollectionService();

protected String result = null;

protected String taskProperty(String name, String defaultValue) {
return super.taskProperty(name) != null ? super.taskProperty(name) : defaultValue;
}

@Override
public int perform(DSpaceObject dso) throws IOException {

List<MetadataFieldName> fields = new ArrayList<MetadataFieldName>();

fields.add(new MetadataFieldName("bs.edition.name"));
fields.add(new MetadataFieldName("dc.relation.ispartof"));
fields.add(new MetadataFieldName("bs.conference.name"));
fields.add(new MetadataFieldName("bs.proceedings.name"));
fields.add(new MetadataFieldName("dc.description"));
fields.add(new MetadataFieldName("dc.description.abstract"));

try {

for (MetadataFieldName field : fields) {
if (dso instanceof Item) {
ItemService service = itemService;
List<MetadataValue> values= service.getMetadata((Item) dso, field.schema, field.element, field.qualifier, Item.ANY);
if (!values.isEmpty()) {
service.removeMetadataValues(Curator.curationContext(), (Item) dso, values);
values.forEach(v -> {
try {
service.addMetadata(Curator.curationContext(), (Item) dso, field.schema, field.element, field.qualifier, null,
v.getValue().replaceAll("Jornadas de Ingeniería de Ciencia e Ingeniería de Servicios",
"Jornadas de Ciencia e Ingeniería de Servicios"));
} catch (SQLException e) {
report("An error occurred: " + e.getMessage());
}
});
}
} else if (dso instanceof Community) {
CommunityService service = communityService;
List<MetadataValue> values= service.getMetadata((Community) dso, field.schema, field.element, field.qualifier, Item.ANY);
if (!values.isEmpty()) {
service.removeMetadataValues(Curator.curationContext(), (Community) dso, values);
values.forEach(v -> {
try {
service.addMetadata(Curator.curationContext(), (Community) dso, field.schema, field.element, field.qualifier, null,
v.getValue().replaceAll("Jornadas de Ingeniería de Ciencia e Ingeniería de Servicios",
"Jornadas de Ciencia e Ingeniería de Servicios"));
} catch (SQLException e) {
report("An error occurred: " + e.getMessage());
}
});
}
} else if (dso instanceof Collection) {
CollectionService service = collectionService;
List<MetadataValue> values= service.getMetadata((Collection) dso, field.schema, field.element, field.qualifier, Item.ANY);
if (!values.isEmpty()) {
service.removeMetadataValues(Curator.curationContext(), (Collection) dso, values);
values.forEach(v -> {
try {
service.addMetadata(Curator.curationContext(), (Collection) dso, field.schema, field.element, field.qualifier, null,
v.getValue().replaceAll("Jornadas de Ingeniería de Ciencia e Ingeniería de Servicios",
"Jornadas de Ciencia e Ingeniería de Servicios"));
} catch (SQLException e) {
report("An error occurred: " + e.getMessage());
}
});
}
} else {
result = "Unsupported DSpaceObject type: " + dso.getClass().getName();
setResult(result);
report(result);
return Curator.CURATE_SKIP;
}
}
} catch (Exception e) {
result = "Failed rename attribute in DSpaceObject " + dso.getID();
setResult(result);
report(result);
return Curator.CURATE_FAIL;
}
result = "JCIS name succesfully fixed in DSpaceObject " + dso.getID();
setResult(result);
report(result);
return Curator.CURATE_SUCCESS;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package org.dspace.ctask.general;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.dspace.authorize.AuthorizeException;
import org.dspace.authorize.factory.AuthorizeServiceFactory;
import org.dspace.authorize.service.ResourcePolicyService;
import org.dspace.content.Bitstream;
import org.dspace.content.Bundle;
import org.dspace.content.DSpaceObject;
import org.dspace.content.Item;
import org.dspace.core.Constants;
import org.dspace.core.Context;
import org.dspace.curate.AbstractCurationTask;
import org.dspace.curate.Curator;

/**
* Curation taks to make all {@link DSpaceObject}s private
*
* @author agomez
*/
public class MakeAdminOnly extends AbstractCurationTask {

private ResourcePolicyService resourcePolicyService = AuthorizeServiceFactory.getInstance().getResourcePolicyService();

protected String result = null;

protected String taskProperty(String name, String defaultValue) {
return super.taskProperty(name) != null ? super.taskProperty(name) : defaultValue;
}

@Override
public int perform(DSpaceObject dso) throws IOException {
if (dso instanceof Item) {
if (!((Item) dso).isArchived()) {
result = "Skipping not archived Item: " + dso.getID();
setResult(result);
report(result);
return Curator.CURATE_SKIP;
}
}
try {
Context context = Curator.curationContext();
resourcePolicyService.removePolicies(context, dso, Constants.READ);
if (dso instanceof Item) {
Item item = (Item) dso;
List<Bundle> bundles = new ArrayList<Bundle>();
bundles.addAll(item.getBundles("ORIGINAL"));
bundles.addAll(item.getBundles("TEXT"));
bundles.addAll(item.getBundles("THUMBNAIL"));
for (Bundle bundle : bundles) {
resourcePolicyService.removePolicies(context, bundle, Constants.READ);
for (Bitstream bitstream: bundle.getBitstreams()) {
resourcePolicyService.removePolicies(context, bitstream, Constants.READ);
}
}
}
result = "DSpaceObject has been successfully made private only for Administrators: " + dso.getID();
setResult(result);
report(result);
return Curator.CURATE_SUCCESS;
} catch (SQLException | AuthorizeException e) {
result = "Unable to change authorization policy for " + dso.getID();
setResult(result);
report(result);
return Curator.CURATE_ERROR;
}
}
}
85 changes: 85 additions & 0 deletions dspace-api/src/main/java/org/dspace/ctask/general/MakePublic.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package org.dspace.ctask.general;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.dspace.authorize.AuthorizeException;
import org.dspace.authorize.ResourcePolicy;
import org.dspace.authorize.factory.AuthorizeServiceFactory;
import org.dspace.authorize.service.AuthorizeService;
import org.dspace.authorize.service.ResourcePolicyService;
import org.dspace.content.Bitstream;
import org.dspace.content.Bundle;
import org.dspace.content.DSpaceObject;
import org.dspace.content.Item;
import org.dspace.core.Constants;
import org.dspace.core.Context;
import org.dspace.curate.AbstractCurationTask;
import org.dspace.curate.Curator;
import org.dspace.eperson.Group;
import org.dspace.eperson.factory.EPersonServiceFactory;
import org.dspace.eperson.service.GroupService;

/**
* Curation taks to make all {@link DSpaceObject}s public
*
* @author agomez
*/
public class MakePublic extends AbstractCurationTask {

private ResourcePolicyService resourcePolicyService = AuthorizeServiceFactory.getInstance().getResourcePolicyService();
private AuthorizeService authorizeService = AuthorizeServiceFactory.getInstance().getAuthorizeService();
private GroupService groupService = EPersonServiceFactory.getInstance().getGroupService();

protected String result = null;

protected String taskProperty(String name, String defaultValue) {
return super.taskProperty(name) != null ? super.taskProperty(name) : defaultValue;
}

@Override
public int perform(DSpaceObject dso) throws IOException {
if (dso instanceof Item) {
if (!((Item) dso).isArchived()) {
result = "Skipping not archived Item: " + dso.getID();
setResult(result);
report(result);
return Curator.CURATE_SKIP;
}
}
try {
Context context = Curator.curationContext();
Group anonymous = groupService.findByName(context, Group.ANONYMOUS);
resourcePolicyService.removePolicies(context, dso, Constants.READ);
if (dso instanceof Item) {
Item item = (Item) dso;
authorizeService.createResourcePolicy(context, dso, anonymous, null, Constants.READ, ResourcePolicy.TYPE_INHERITED);
List<Bundle> bundles = new ArrayList<Bundle>();
bundles.addAll(item.getBundles("ORIGINAL"));
bundles.addAll(item.getBundles("TEXT"));
bundles.addAll(item.getBundles("THUMBNAIL"));
for (Bundle bundle : bundles) {
resourcePolicyService.removePolicies(context, bundle, Constants.READ);
authorizeService.createResourcePolicy(context, bundle, anonymous, null, Constants.READ, ResourcePolicy.TYPE_INHERITED);
for (Bitstream bitstream: bundle.getBitstreams()) {
resourcePolicyService.removePolicies(context, bitstream, Constants.READ);
authorizeService.createResourcePolicy(context, bitstream, anonymous, null, Constants.READ, null);
}
}
} else {
authorizeService.createResourcePolicy(context, dso, anonymous, null, Constants.READ, null);
}
result = "DSpaceObject has been successfully made public: " + dso.getID();
setResult(result);
report(result);
return Curator.CURATE_SUCCESS;
} catch (SQLException | AuthorizeException e) {
result = "Unable to change authorization policy for " + dso.getID();
setResult(result);
report(result);
return Curator.CURATE_ERROR;
}
}
}
Loading

0 comments on commit a246a12

Please sign in to comment.