eu.delving.sip.actions.ImportAction.java Source code

Java tutorial

Introduction

Here is the source code for eu.delving.sip.actions.ImportAction.java

Source

/*
 * Copyright 2011, 2012 Delving BV
 *
 * Licensed under the EUPL, Version 1.0 or as soon they
 * will be approved by the European Commission - subsequent
 * versions of the EUPL (the "Licence");
 * you may not use this work except in compliance with the
 * Licence.
 * You may obtain a copy of the Licence at:
 *
 * http://ec.europa.eu/idabc/eupl
 *
 * Unless required by applicable law or agreed to in
 * writing, software distributed under the Licence is
 * distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied.
 * See the Licence for the specific language governing
 * permissions and limitations under the Licence.
 */

package eu.delving.sip.actions;

import eu.delving.sip.base.Harvestor;
import eu.delving.sip.base.Swing;
import eu.delving.sip.files.DataSetState;
import eu.delving.sip.files.Storage;
import eu.delving.sip.model.DataSetModel;
import eu.delving.sip.model.FactModel;
import eu.delving.sip.model.SipModel;
import org.apache.commons.lang.StringUtils;

import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

import static eu.delving.sip.base.KeystrokeHelper.MENU_I;
import static eu.delving.sip.base.KeystrokeHelper.configAction;
import static eu.delving.sip.base.SwingHelper.ICON_IMPORT;
import static eu.delving.sip.files.DataSetState.ABSENT;

/**
 * Import a new file from the file system or initiate a harvest of data from an OAI-PMH source.
 *
 * @author Gerald de Jong <gerald@delving.eu>
 */

public class ImportAction extends AbstractAction {
    private JDesktopPane parent;
    private SipModel sipModel;
    private final String RECENT_DIR = "recentImportDirectory";
    private JDialog dialog;
    private ChooseFileAction chooseFileAction = new ChooseFileAction();
    private HarvestAction harvestAction = new HarvestAction();
    private JFileChooser chooser = new JFileChooser("XML Metadata Source File");

    public ImportAction(JDesktopPane parent, SipModel sipModel) {
        configAction(this, "Import new source data", ICON_IMPORT, MENU_I);
        this.parent = parent;
        this.sipModel = sipModel;
        this.dialog = new JDialog(SwingUtilities.getWindowAncestor(parent), "Input Source",
                Dialog.ModalityType.APPLICATION_MODAL);
        setEnabled(false);
        prepareDialog();
        prepareChooser(sipModel);
        sipModel.getDataSetModel().addListener(new DataSetModel.SwingListener() {
            @Override
            public void stateChanged(DataSetModel model, DataSetState state) {
                setEnabled(state != ABSENT);
            }
        });
    }

    @Override
    public void actionPerformed(ActionEvent actionEvent) {
        Dimension all = parent.getSize();
        Dimension d = dialog.getSize();
        dialog.setLocation((all.width - d.width) / 2, (all.height - d.height) / 2);
        dialog.setVisible(true);
    }

    private void prepareDialog() {
        JButton cancel = new JButton("Cancel");
        cancel.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                dialog.setVisible(false);
            }
        });
        JPanel bp = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        bp.add(cancel);
        JPanel p = new JPanel(new GridLayout(1, 0, 15, 15));
        p.setBorder(BorderFactory.createEmptyBorder(15, 15, 15, 15));
        p.add(new JButton(chooseFileAction));
        p.add(new JButton(harvestAction));
        dialog.getContentPane().add(p, BorderLayout.CENTER);
        dialog.getContentPane().add(bp, BorderLayout.SOUTH);
        dialog.pack();
    }

    private void prepareChooser(SipModel sipModel) {
        File directory = new File(sipModel.getPreferences().get(RECENT_DIR, System.getProperty("user.home")));
        chooser.setCurrentDirectory(directory);
        chooser.setFileFilter(new FileFilter() {
            @Override
            public boolean accept(File file) {
                return file.isFile() && (file.getName().endsWith(".xml") || file.getName().endsWith(".xml.gz")
                        || file.getName().endsWith(".xml.zip") || file.getName().endsWith(".csv"));
            }

            @Override
            public String getDescription() {
                return "Files ending with .xml, .xml.gz, .xml.zip, or .csv";
            }
        });
        chooser.setMultiSelectionEnabled(false);
    }

    private boolean selectInputFile(File file) {
        if (!file.exists() || sipModel.getDataSetModel().isEmpty())
            return false;
        String spec = sipModel.getDataSetModel().getDataSet().getSpec();
        boolean doImport = sipModel.getFeedback().confirm("Verify your choice",
                String.format("<html>Import this file<br><br>" + "<pre><strong>%s</strong></pre><br>"
                        + "into data set '<strong>%s</strong>'?<br>", file.getAbsolutePath(), spec // todo: could snag description and things from facts, if they were hardcoded
                ));
        if (doImport) {
            setEnabled(false);
            sipModel.importSource(file, new Swing() {
                @Override
                public void run() {
                    setEnabled(true);
                }
            });
            return true;
        }
        return false;
    }

    private class ChooseFileAction extends AbstractAction {
        private ChooseFileAction() {
            super("<html><center><br><h2>Read</h2>Import from XML/CSV file<br>from your computer's file system<br><br>");
        }

        @Override
        public void actionPerformed(ActionEvent actionEvent) {
            dialog.setVisible(false);
            int choiceMade = chooser.showOpenDialog(parent);
            if (choiceMade == JFileChooser.APPROVE_OPTION) {
                File file = chooser.getSelectedFile();
                sipModel.getPreferences().put(RECENT_DIR, file.getAbsolutePath());
                selectInputFile(file); // it's a boolean
            }
        }
    }

    private class HarvestAction extends AbstractAction {

        private HarvestAction() {
            super("<html><center><br><h2>Harvest</h2>Harvest from OAI-PMH server<br>for which you have the URL<br><br>");
        }

        @Override
        public void actionPerformed(ActionEvent actionEvent) {
            dialog.setVisible(false);
            if (sipModel.getDataSetModel().isEmpty())
                return;
            FactModel hints = sipModel.getStatsModel().getHintsModel();
            String url = hints.get(Storage.HARVEST_URL);
            String prefix = hints.get(Storage.HARVEST_PREFIX);
            String spec = hints.get(Storage.HARVEST_SPEC);
            JTextField harvestUrl = new JTextField(null == url ? "" : url, 45);
            JTextField harvestPrefix = new JTextField(null == prefix ? "" : prefix);
            JTextField harvestSpec = new JTextField(null == spec ? "" : spec);
            if (!sipModel.getFeedback().form("OAI-PMH server details", "Server", harvestUrl, "Prefix",
                    harvestPrefix, "Spec", harvestSpec)) {
                return;
            }
            if (!StringUtils.isEmpty(harvestUrl.getText())) {
                try {
                    new URL(harvestUrl.getText());
                    hints.set(Storage.HARVEST_URL, harvestUrl.getText());
                    hints.set(Storage.HARVEST_PREFIX, harvestPrefix.getText());
                    hints.set(Storage.HARVEST_SPEC, harvestSpec.getText());
                    performHarvest(harvestUrl.getText(), harvestPrefix.getText(), harvestSpec.getText());
                } catch (MalformedURLException e) {
                    sipModel.getFeedback().alert("Malformed URL: " + harvestUrl);
                }
            }
        }

        private void performHarvest(final String harvestUrl, final String harvestPrefix, final String harvestSpec) {
            sipModel.exec(new Harvestor(sipModel.getDataSetModel().getDataSet(), new Harvestor.Context() {

                @Override
                public String harvestUrl() {
                    return harvestUrl;
                }

                @Override
                public String harvestPrefix() {
                    return harvestPrefix;
                }

                @Override
                public String harvestSpec() {
                    return harvestSpec;
                }
            }));
        }
    }
}