<commit_before>package nz.ac.lconz.irr.curate.task;

import org.dspace.authorize.AuthorizeException;
import org.dspace.authorize.AuthorizeManager;
import org.dspace.authorize.ResourcePolicy;
import org.dspace.content.Collection;
import org.dspace.content.Community;
import org.dspace.content.DSpaceObject;
import org.dspace.core.Constants;
import org.dspace.core.Context;
import org.dspace.curate.AbstractCurationTask;
import org.dspace.curate.Curator;

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

/**
 * @author <NAME> <EMAIL> for the LCoNZ Institutional Research Repositories
 */
public class CopyCommunityPoliciesToCollections extends AbstractCurationTask {
	@Override
	public int perform(DSpaceObject dso) throws IOException {
		if (dso == null || dso.getType() != Constants.COMMUNITY) {
			String message = "This curation task must be run on a community";
			report(message);
			setResult(message);
			return Curator.CURATE_SKIP;
		}
		Community community = (Community) dso;
		Context context = null;
		try {
			context = new Context();
			Collection[] collections = community.getCollections();

			for (Collection collection : collections) {
				AuthorizeManager.inheritPolicies(context, community, collection);
			}

			context.commit();
			context = null;

			String message = String.format("%d child collections inherited the policies of the community\"%d\"", collections.length, community.getName());
			report(message);
			setResult(message);

			return Curator.CURATE_SUCCESS;
		} catch (SQLException e) {<commit_msg>1859 Remove duplicate policies when inheriting<commit_after>package nz.ac.lconz.irr.curate.task;

import org.dspace.authorize.AuthorizeException;
import org.dspace.authorize.AuthorizeManager;
import org.dspace.authorize.ResourcePolicy;
import org.dspace.content.Collection;
import org.dspace.content.Community;
import org.dspace.content.DSpaceObject;
import org.dspace.core.Constants;
import org.dspace.core.Context;
import org.dspace.curate.AbstractCurationTask;
import org.dspace.curate.Curator;
import org.dspace.curate.Distributive;

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

/**
 * @author <NAME> <EMAIL> for the LCoNZ Institutional Research Repositories
 */
@Distributive
public class CopyCommunityPoliciesToCollections extends AbstractCurationTask {
	@Override
	public int perform(DSpaceObject dso) throws IOException {
		if (dso == null || dso.getType() != Constants.COMMUNITY) {
			String message = "This curation task must be run on a community";
			report(message);
			setResult(message);
			return Curator.CURATE_SKIP;
		}
		Community community = (Community) dso;
		Context context = null;
		try {
			context = new Context();
			Collection[] collections = community.getCollections();

			for (Collection collection : collections) {
				AuthorizeManager.inheritPolicies(context, community, collection);

				// then remove duplicate policies
				List<ResourcePolicy> policies = AuthorizeManager.getPolicies(context, collection);
				Set<ResourcePolicy> alreadySeen = new HashSet<ResourcePolicy>();
				for (ResourcePolicy policy : policies) {
					if (alreadySeen.contains(policy)) {
						policy.delete();
					} else {
						alreadySeen.add(policy);
					}
				}
			}

			context.commit();
			context = null;

			String message = String.format("%d child collections inherited the policies of the community \"%s\"", collections.length, community.getName());
			report(message);
			setResult(message);

			return Curator.CURATE_SUCCESS;
		} catch (SQLException e) {
{'fraction_non_alphanumeric': 0.10549330418147035, 'fraction_numerical': 0.0010931948619841486, 'mean_word_length': 4.828025477707007, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5328949', 'n_tokens_mistral': 1191, 'n_tokens_neox': 1063, 'n_words': 262}