26 |
26 |
import java.io.ByteArrayInputStream;
|
27 |
27 |
import java.io.ByteArrayOutputStream;
|
28 |
28 |
import java.io.File;
|
|
29 |
import java.io.FileInputStream;
|
29 |
30 |
import java.io.FileOutputStream;
|
30 |
31 |
import java.io.IOException;
|
31 |
32 |
import java.io.InputStream;
|
... | ... | |
143 |
144 |
import edu.ucsb.nceas.metacat.util.SystemUtil;
|
144 |
145 |
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
|
145 |
146 |
import edu.ucsb.nceas.utilities.XMLUtilities;
|
|
147 |
import edu.ucsb.nceas.utilities.export.HtmlToPdf;
|
146 |
148 |
import gov.loc.repository.bagit.Bag;
|
147 |
149 |
import gov.loc.repository.bagit.BagFactory;
|
148 |
150 |
import gov.loc.repository.bagit.writer.impl.ZipWriter;
|
... | ... | |
1899 |
1901 |
//Create a map of dataone ids and file names
|
1900 |
1902 |
Map<Identifier, String> fileNames = new HashMap<Identifier, String>();
|
1901 |
1903 |
|
|
1904 |
// track the pid-to-file mapping
|
|
1905 |
StringBuffer pidMapping = new StringBuffer();
|
|
1906 |
|
1902 |
1907 |
// find the package contents
|
1903 |
1908 |
SystemMetadata sysMeta = this.getSystemMetadata(session, pid);
|
1904 |
1909 |
if (ObjectFormatCache.getInstance().getFormat(sysMeta.getFormatId()).getFormatType().equals("RESOURCE")) {
|
... | ... | |
1915 |
1920 |
//Get the system metadata for this metadata object
|
1916 |
1921 |
SystemMetadata metadataSysMeta = this.getSystemMetadata(session, metadataID);
|
1917 |
1922 |
|
|
1923 |
// include user-friendly metadata
|
|
1924 |
if (ObjectFormatCache.getInstance().getFormat(metadataSysMeta.getFormatId()).getFormatType().equals("METADATA")) {
|
|
1925 |
InputStream metadataStream = this.get(session, metadataID);
|
|
1926 |
|
|
1927 |
try {
|
|
1928 |
// transform
|
|
1929 |
String format = "default";
|
|
1930 |
|
|
1931 |
DBTransform transformer = new DBTransform();
|
|
1932 |
String documentContent = IOUtils.toString(metadataStream, "UTF-8");
|
|
1933 |
String sourceType = metadataSysMeta.getFormatId().getValue();
|
|
1934 |
String targetType = "-//W3C//HTML//EN";
|
|
1935 |
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
1936 |
Writer writer = new OutputStreamWriter(baos , "UTF-8");
|
|
1937 |
// TODO: include more params?
|
|
1938 |
Hashtable<String, String[]> params = new Hashtable<String, String[]>();
|
|
1939 |
String localId = null;
|
|
1940 |
try {
|
|
1941 |
localId = IdentifierManager.getInstance().getLocalId(pid.getValue());
|
|
1942 |
} catch (McdbDocNotFoundException e) {
|
|
1943 |
throw new NotFound("1020", e.getMessage());
|
|
1944 |
}
|
|
1945 |
params.put("qformat", new String[] {format});
|
|
1946 |
params.put("docid", new String[] {localId});
|
|
1947 |
params.put("pid", new String[] {pid.getValue()});
|
|
1948 |
params.put("displaymodule", new String[] {"printall"});
|
|
1949 |
|
|
1950 |
transformer.transformXMLDocument(
|
|
1951 |
documentContent ,
|
|
1952 |
sourceType,
|
|
1953 |
targetType ,
|
|
1954 |
format,
|
|
1955 |
writer,
|
|
1956 |
params,
|
|
1957 |
null //sessionid
|
|
1958 |
);
|
|
1959 |
|
|
1960 |
// finally, get the HTML back
|
|
1961 |
ContentTypeByteArrayInputStream resultInputStream = new ContentTypeByteArrayInputStream(baos.toByteArray());
|
|
1962 |
|
|
1963 |
// write to temp file with correct css path
|
|
1964 |
File tmpDir = File.createTempFile("package_", "_dir");
|
|
1965 |
tmpDir.delete();
|
|
1966 |
tmpDir.mkdir();
|
|
1967 |
File htmlFile = File.createTempFile("metadata", ".html", tmpDir);
|
|
1968 |
File cssDir = new File(tmpDir, format);
|
|
1969 |
cssDir.mkdir();
|
|
1970 |
File cssFile = new File(tmpDir, format + "/" + format + ".css");
|
|
1971 |
String pdfFileName = metadataID.getValue().replaceAll("[^a-zA-Z0-9\\-\\.]", "_") + "-METADATA.pdf";
|
|
1972 |
File pdfFile = new File(tmpDir, pdfFileName);
|
|
1973 |
//File pdfFile = File.createTempFile("metadata", ".pdf", tmpDir);
|
|
1974 |
|
|
1975 |
// put the CSS file in place for the html to find it
|
|
1976 |
String originalCssPath = SystemUtil.getContextDir() + "/style/skins/" + format + "/" + format + ".css";
|
|
1977 |
IOUtils.copy(new FileInputStream(originalCssPath), new FileOutputStream(cssFile));
|
|
1978 |
|
|
1979 |
// write the HTML file
|
|
1980 |
IOUtils.copy(resultInputStream, new FileOutputStream(htmlFile));
|
|
1981 |
|
|
1982 |
// convert to PDF
|
|
1983 |
HtmlToPdf.export(htmlFile.getAbsolutePath(), pdfFile.getAbsolutePath());
|
|
1984 |
|
|
1985 |
//add to the package
|
|
1986 |
bag.addFileToPayload(pdfFile);
|
|
1987 |
pidMapping.append(metadataID.getValue() + " (pdf)" + "\t" + "data/" + pdfFile.getName() + "\n");
|
|
1988 |
|
|
1989 |
// mark for clean up after we are done
|
|
1990 |
htmlFile.delete();
|
|
1991 |
cssFile.delete();
|
|
1992 |
cssDir.delete();
|
|
1993 |
tempFiles.add(tmpDir);
|
|
1994 |
tempFiles.add(pdfFile); // delete this first later on
|
|
1995 |
|
|
1996 |
} catch (Exception e) {
|
|
1997 |
logMetacat.warn("Could not transform metadata", e);
|
|
1998 |
}
|
|
1999 |
}
|
|
2000 |
|
|
2001 |
|
1918 |
2002 |
//If this is in eml format, extract the filename and GUID from each entity in its package
|
1919 |
2003 |
if (metadataSysMeta.getFormatId().getValue().startsWith("eml://")) {
|
1920 |
2004 |
//Get the package
|
... | ... | |
1976 |
2060 |
tempDir = new File(tempDir.getPath() + "_dir");
|
1977 |
2061 |
tempDir.mkdir();
|
1978 |
2062 |
tempFiles.add(tempDir);
|
|
2063 |
File pidMappingFile = new File(tempDir, "pid-mapping.txt");
|
1979 |
2064 |
|
1980 |
|
// track the pid-to-file mapping
|
1981 |
|
StringBuffer pidMapping = new StringBuffer();
|
1982 |
|
|
1983 |
2065 |
// loop through the package contents
|
1984 |
2066 |
for (Identifier entryPid: packagePids) {
|
1985 |
2067 |
//Get the system metadata for each item
|
... | ... | |
2013 |
2095 |
}
|
2014 |
2096 |
|
2015 |
2097 |
//add the the pid to data file map
|
2016 |
|
File pidMappingFile = new File(tempDir, "pid-mapping.txt");
|
2017 |
2098 |
IOUtils.write(pidMapping.toString(), new FileOutputStream(pidMappingFile));
|
2018 |
2099 |
bag.addFileAsTag(pidMappingFile);
|
2019 |
2100 |
tempFiles.add(pidMappingFile);
|
... | ... | |
2037 |
2118 |
tempFiles.add(bagFile);
|
2038 |
2119 |
|
2039 |
2120 |
// clean up other temp files
|
2040 |
|
for(int i=tempFiles.size()-1; i>=0; i--){
|
2041 |
|
File tf = new File(tempFiles.get(i).getPath());
|
2042 |
|
tf.delete();
|
|
2121 |
for (int i=tempFiles.size()-1; i>=0; i--){
|
|
2122 |
tempFiles.get(i).delete();
|
2043 |
2123 |
}
|
2044 |
2124 |
|
2045 |
2125 |
} catch (IOException e) {
|
Include PDF version of the metadata in the package download. https://projects.ecoinformatics.org/ecoinfo/issues/6053