All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.dspace.content.crosswalk.SubscriptionDsoMetadataForEmailCompose Maven / Gradle / Ivy

The newest version!
/**
 * 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.crosswalk;

import static org.dspace.content.Item.ANY;

import java.io.OutputStream;
import java.io.PrintStream;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import org.dspace.content.DSpaceObject;
import org.dspace.content.Item;
import org.dspace.content.service.ItemService;
import org.dspace.core.Constants;
import org.dspace.core.Context;
import org.dspace.handle.factory.HandleServiceFactory;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * Creates a String to be sent as email body for subscriptions
 *
 * @author Alba Aliu
 */
public class SubscriptionDsoMetadataForEmailCompose implements StreamDisseminationCrosswalk {

    private List metadata = new ArrayList<>();

    @Autowired
    private ItemService itemService;

    @Override
    public boolean canDisseminate(Context context, DSpaceObject dso) {
        return Objects.nonNull(dso) && dso.getType() == Constants.ITEM;
    }

    @Override
    public void disseminate(Context context, DSpaceObject dso, OutputStream out) throws SQLException {
        if (dso.getType() == Constants.ITEM) {
            Item item = (Item) dso;
            PrintStream printStream = new PrintStream(out);
            for (String actualMetadata : metadata) {
                String[] split = actualMetadata.split("\\.");
                String qualifier = null;
                if (split.length == 3) {
                    qualifier = split[2];
                }
                var metadataValue = itemService.getMetadataFirstValue(item, split[0], split[1], qualifier, ANY);
                printStream.print(metadataValue + " ");
            }
            String itemURL = HandleServiceFactory.getInstance()
                                                 .getHandleService()
                                                 .resolveToURL(context, item.getHandle());
            printStream.print(itemURL);
            printStream.print("\n");
            printStream.close();
        }
    }

    @Override
    public String getMIMEType() {
        return "text/plain";
    }

    public List getMetadata() {
        return metadata;
    }

    public void setMetadata(List metadata) {
        this.metadata = metadata;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy