<commit_before>/**
 * 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.content.packager;

import org.dspace.authorize.AuthorizeException;
import org.dspace.content.Collection;
import org.dspace.content.Community;
import org.dspace.content.DSpaceObject;
import org.dspace.content.Item;
import org.dspace.content.ItemIterator;
import org.dspace.content.crosswalk.CrosswalkException;
import org.dspace.core.Constants;
import org.dspace.core.Context;

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

/**
 * An abstract implementation of a DSpace Package Disseminator, which
 * implements a few helper/utility methods that most (all?) PackageDisseminators
 * may find useful.
 * <P>
 * First, implements recursive functionality in the disseminateAll()
 * method of the PackageIngester interface.  This method is setup to
 * recursively call disseminate() method.
 * <P>
 * All Package disseminators should either extend this abstract class
 * or implement <code>PackageDisseminator</code> to better suit their needs.
 *
 * @author <NAME>
 * @see PackageDisseminator
 */
public abstract class AbstractPackageDisseminator
        implements PackageDisseminator
{
    /**  List of all successfully disseminated package files */
    private List<File> packageFileList = new ArrayList<File>();

    /**
     * Recursively export one or more DSpace Objects as a series of packages.
     * This method will export the given DSpace Object as well as all referenced
     * DSpaceObjects (e.g. child objects) into a series of packages. The
     * initial object is exported to the location specified by the OutputStream.
     * All other packages are exported to the same directory location.
     * <p>
     * Package is any serialized representation of the item, at the discretion
     * of the implementing class.  It does not have to include content bitstreams.
     * <br>
     * Use the <code>params</code> parameter list to adjust the way the
     * package is made, e.g. including a "<code>metadataOnly</code>"
     * parameter might make the package a bare manifest in XML
     * instead of a Zip file including manifest and contents.
     * <br>
     * Throws an exception of the initial object is not acceptable or there is
     * a failure creating the package.
     *
     * @param context  DSpace context.
     * @param dso  initial DSpace object
     * @param params Properties-style list of options specific to this packager
     * @param pkgFile File where initial package should be written. All other
     *          packages will be written to the same directory as this File.
     * @throws PackageValidationException if package cannot be created or there is
     *  a fatal error in creating it.
     */
    public List<File> disseminateAll(Context context, DSpaceObject dso,
                     PackageParameters params, File pkgFile)
        throws PackageException, CrosswalkException,
               AuthorizeException, SQLException, IOException
    {
        //If unset, make sure the Parameters specifies this is a recursive dissemination
        if(!params.recursiveModeEnabled())
        {
            params.setRecursiveModeEnabled(true);
        }

        //try to disseminate the first object using provided PackageDisseminator
        disseminate(context, dso, params, pkgFile);

        //check if package was disseminated
        if(pkgFile.exists())
        {
            //add to list of successfully disseminated packages
            addToPackageList(pkgFile);

            //We can only recursively disseminate non-Items
            //(NOTE: Items have no children, as Bitstreams/Bundles are created from Item packages)
            if(dso.getType()!=Constants.ITEM)
            {
                //Determine where first file package was disseminated to, as all
                //others will be written to same directory
                String pkgDirectory = pkgFile.getCanonicalFile().getParent();
                if(!pkgDirectory.endsWith(File.separator))
                {
                    pkgDirectory += File.separator;
                }
                String fileExtension = PackageUtils.getFileExtension(pkgFile.getName());

                //recursively disseminate content, based on object type
                switch (dso.getType())
                {
                    case Constants.COLLECTION :
                        //Also find all Items in this Collection and disseminate
                        Collection collection = (Collection) dso;
                        ItemIterator iterator = collection.getAllItems();
                        while(iterator.hasNext())
                        {
                            Item item = iterator.next();

                            //disseminate all items (recursively!)<commit_msg>[DS-751] Generate Collection's list of children and the set of child  Item packages claiming Collection as parent in the same way, to avoid  abrupt exit.<commit_after>/**
 * 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.content.packager;

import org.dspace.authorize.AuthorizeException;
import org.dspace.content.Collection;
import org.dspace.content.Community;
import org.dspace.content.DSpaceObject;
import org.dspace.content.Item;
import org.dspace.content.ItemIterator;
import org.dspace.content.crosswalk.CrosswalkException;
import org.dspace.core.Constants;
import org.dspace.core.Context;

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

/**
 * An abstract implementation of a DSpace Package Disseminator, which
 * implements a few helper/utility methods that most (all?) PackageDisseminators
 * may find useful.
 * <P>
 * First, implements recursive functionality in the disseminateAll()
 * method of the PackageIngester interface.  This method is setup to
 * recursively call disseminate() method.
 * <P>
 * All Package disseminators should either extend this abstract class
 * or implement <code>PackageDisseminator</code> to better suit their needs.
 *
 * @author <NAME>
 * @see PackageDisseminator
 */
public abstract class AbstractPackageDisseminator
        implements PackageDisseminator
{
    /**  List of all successfully disseminated package files */
    private List<File> packageFileList = new ArrayList<File>();

    /**
     * Recursively export one or more DSpace Objects as a series of packages.
     * This method will export the given DSpace Object as well as all referenced
     * DSpaceObjects (e.g. child objects) into a series of packages. The
     * initial object is exported to the location specified by the OutputStream.
     * All other packages are exported to the same directory location.
     * <p>
     * Package is any serialized representation of the item, at the discretion
     * of the implementing class.  It does not have to include content bitstreams.
     * <br>
     * Use the <code>params</code> parameter list to adjust the way the
     * package is made, e.g. including a "<code>metadataOnly</code>"
     * parameter might make the package a bare manifest in XML
     * instead of a Zip file including manifest and contents.
     * <br>
     * Throws an exception of the initial object is not acceptable or there is
     * a failure creating the package.
     *
     * @param context  DSpace context.
     * @param dso  initial DSpace object
     * @param params Properties-style list of options specific to this packager
     * @param pkgFile File where initial package should be written. All other
     *          packages will be written to the same directory as this File.
     * @throws PackageValidationException if package cannot be created or there is
     *  a fatal error in creating it.
     */
    public List<File> disseminateAll(Context context, DSpaceObject dso,
                     PackageParameters params, File pkgFile)
        throws PackageException, CrosswalkException,
               AuthorizeException, SQLException, IOException
    {
        //If unset, make sure the Parameters specifies this is a recursive dissemination
        if(!params.recursiveModeEnabled())
        {
            params.setRecursiveModeEnabled(true);
        }

        //try to disseminate the first object using provided PackageDisseminator
        disseminate(context, dso, params, pkgFile);

        //check if package was disseminated
        if(pkgFile.exists())
        {
            //add to list of successfully disseminated packages
            addToPackageList(pkgFile);

            //We can only recursively disseminate non-Items
            //(NOTE: Items have no children, as Bitstreams/Bundles are created from Item packages)
            if(dso.getType()!=Constants.ITEM)
            {
                //Determine where first file package was disseminated to, as all
                //others will be written to same directory
                String pkgDirectory = pkgFile.getCanonicalFile().getParent();
                if(!pkgDirectory.endsWith(File.separator))
                {
                    pkgDirectory += File.separator;
                }
                String fileExtension = PackageUtils.getFileExtension(pkgFile.getName());

                //recursively disseminate content, based on object type
                switch (dso.getType())
                {
                    case Constants.COLLECTION :
                        //Also find all Items in this Collection and disseminate
                        Collection collection = (Collection) dso;
                        ItemIterator iterator = collection.getItems();
                        while(iterator.hasNext())
                        {
                            Item item = iterator.next();

                            //disseminate all items (recursively!)
{'fraction_non_alphanumeric': 0.06860727237087132, 'fraction_numerical': 0.00029403116730373417, 'mean_word_length': 2.519834425664022, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1894960', 'n_tokens_mistral': 2543, 'n_tokens_neox': 2393, 'n_words': 975}