de.fhg.iais.asc.oai.localwriter.RepositoryWriter.java Source code

Java tutorial

Introduction

Here is the source code for de.fhg.iais.asc.oai.localwriter.RepositoryWriter.java

Source

package de.fhg.iais.asc.oai.localwriter;

/******************************************************************************
 * Copyright 2011 (c) Fraunhofer IAIS Netmedia  http://www.iais.fraunhofer.de *
 * ************************************************************************** *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may    *
 * not use this file except in compliance with the License.                   *
 * You may obtain a copy of the License at                                    *
 * http://www.apache.org/licenses/LICENSE-2.0                                 *
 * Unless required by applicable law or agreed to in writing,                 *
 * software distributed under the License is distributed on an "AS IS" BASIS, *
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   *
 * See the License for the specific language governing permissions and        *
 * limitations under the License.                                             *
 ******************************************************************************/

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

import org.apache.commons.io.Charsets;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import de.fhg.iais.commons.dbc.DbcException;

public class RepositoryWriter {

    private static final Logger LOG = LoggerFactory.getLogger(RepositoryWriter.class);

    private final File rootDir;

    private final String eventId;
    private int maxFilesEach = 0;
    private int countDown = 0;

    private String currentSpec = null;

    private String currentId = null;
    private final String currentServer;

    private final List<String> sets = new ArrayList<String>();
    private final List<String> idsInSet = new ArrayList<String>();

    public RepositoryWriter(File rootDir, int maxFilesEach, String oaipmhserveruri, String eventId) {
        this.rootDir = rootDir;
        this.maxFilesEach = maxFilesEach > 0 ? maxFilesEach : 0;
        this.eventId = eventId;
        try {
            FileUtils.forceMkdir(this.rootDir);
        } catch (IOException e) {
            LOG.error("", e);
        }
        LOG.info("Working folder: " + this.rootDir.getAbsolutePath());
        this.currentServer = oaipmhserveruri;
    }

    // ---------------------------------------------------------------------------------------

    private void writeIds() {
        StringBuilder xml = new StringBuilder(200);
        xml.append("<ids");
        xml.append(" set=\"").append(this.currentSpec).append("\"");
        xml.append(">\n");
        for (String id : this.idsInSet) {
            xml.append("\t<id>").append(id).append("</id>\n");
        }
        xml.append("</ids>");
        File file = new File(this.rootDir, this.currentSpec + ".xml");
        try {
            FileUtils.write(file, xml.toString(), Charsets.UTF_8);
        } catch (IOException e) {
            throw new DbcException(e);
        }
    }

    private void writeObject(String metadata) {
        String xmlDoc = buildSip(metadata);
        writeSipToRepository(xmlDoc);
    }

    private void writeSipToRepository(String sip) {
        File setDir = new File(this.rootDir, this.currentSpec);
        String filename = this.currentId;
        int colon = filename.indexOf(':');
        if (colon > -1) {
            filename = filename.substring(colon + 1);
        }
        filename = makeFilenameFilesystemCompatible(filename);
        File file = new File(setDir, filename + ".xml");
        try {
            FileUtils.write(file, sip, Charsets.UTF_8);
        } catch (IOException e) {
            throw new DbcException(e);
        }
    }

    private String buildSip(String metadata) {
        StringBuilder xml = new StringBuilder(200);
        xml.append("<item");
        xml.append(" oaipmhurl=\"").append(this.currentServer).append("\"");
        xml.append(" partner_id=\"").append(this.currentId).append("\"");
        xml.append(" ingestevent=\"").append(this.eventId).append("\"");
        xml.append(">\n");
        xml.append(metadata);
        xml.append("\n</item>");
        return xml.toString();
    }

    private void writeToc(String metadataPrefix, int totalRecords) {
        StringBuilder xml = new StringBuilder(200);
        xml.append("<toc");
        xml.append(" event=\"").append(this.eventId).append("\"");
        xml.append(" metadataPrefix=\"").append(metadataPrefix).append("\"");
        xml.append(" totalrecordsHarvested=\"").append(totalRecords).append("\"");
        xml.append(">\n");
        for (String set : this.sets) {
            xml.append("\t<set>").append(set).append("</set>\n");
        }
        xml.append("</toc>");
        try {
            FileUtils.write(new File(this.rootDir, "toc_" + this.eventId + ".xml"), xml.toString(), Charsets.UTF_8);
        } catch (IOException e) {
            throw new DbcException(e);
        }
    }

    /**
     * next Set
     * 
     * @param spec
     * @return
     */
    public boolean nextSetSpec(String spec) {
        if (this.idsInSet.size() > 0) {
            writeIds();
            this.idsInSet.clear();
        }
        this.sets.add(spec);
        this.currentSpec = spec;
        this.currentSpec = makeFilenameFilesystemCompatible(this.currentSpec);
        try {
            FileUtils.forceMkdir(new File(this.rootDir, this.currentSpec));
        } catch (IOException e) {
            LOG.error("", e);
        }
        this.countDown = this.maxFilesEach;
        return false;
    }

    public String getCurrentSpec() {
        return this.currentSpec;
    }

    /**
     * next Identifier
     * 
     * @param id
     * @return
     */
    public boolean nextIdentifier(String id) {
        this.currentId = id;
        return false;
    }

    /**
     * next Record
     * 
     * @param xml
     * @return
     */
    public boolean nextObject(String xml) {
        writeObject(xml);
        return this.countDown == 0 ? false : --this.countDown == 0;
    }

    /**
     * called at the end
     * 
     * @param metadataPrefix
     * @param totalRecords
     * @return
     */
    public boolean finish(String metadataPrefix, int totalRecords) {
        if (this.idsInSet.size() > 0) {
            writeIds();
            this.idsInSet.clear();
        }
        writeToc(metadataPrefix, totalRecords);
        return false;
    }

    /**
     * Makes sure that the files are file system compatible.
     * 
     * @param localFilename original filename
     * @return valid filename
     */
    private String makeFilenameFilesystemCompatible(String filename) {
        String localFilename = filename.replace(":", "_");
        localFilename = localFilename.replace("/", "_");
        localFilename = localFilename.replace("|", "_");
        return localFilename;
    }

    public File getRootDir() {
        return this.rootDir;
    }

}