/* Copyright (C) 2005-2012, by the President and Fellows of Harvard College. 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. Dataverse Network - A web application to share, preserve and analyze research data. Developed at the Institute for Quantitative Social Science, Harvard University. Version 3.0. */ /** * */ package edu.harvard.iq.dvn.ingest.dsb.impl; import java.util.*; import java.util.logging.*; import java.io.*; import org.apache.commons.lang.*; import org.apache.commons.lang.text.StrSubstitutor; import static java.lang.System.*; /** * @author asone * */ public class DvnReplicationREADMEFileWriter { // instance fields private static Logger dbgLog = Logger.getLogger(DvnReplicationREADMEFileWriter.class.getPackage().getName()); // value map public Map<String, String> valueMap = new LinkedHashMap<String, String>(); public static String headerTemplate = "README for the Version ${dataverse_version_no} of " + "the Dataverse Network application.\n"; public String generateHeaderBlock(){ StrSubstitutor sub = new StrSubstitutor(valueMap); return sub.replace(headerTemplate); } public static String pt1Template = "_How to Replicate Analysis_\n\n"+ "1. If you do not already have R (${R_min_verion_no} or later) installed on your \n" + " computer, install it from:\n\n" + " http://cran.r-project.org\n\n"+ " Note: Some analyses require additional modules, the replication file \n" + " will attempt to install any that are missing, as long as your system is \n" + " connected to the Internet.\n\n" + " Note: Because some graphics are generated by dev2bitmap() [Graphics \n" + " Device for Bitmap Files via GhostScript], your computer may need \n" + " ghostscript (see the Details section of dev2bitmap in the R Reference \n" + " manual). \n"; public String generatePt1Block(){ StrSubstitutor sub = new StrSubstitutor(valueMap); return sub.replace(pt1Template); } public static String pt2Template = "2. Open the accompanying " + "'zipFile_xxxxxx.zip' file, and copy the\n" + " files in it to a new location (for example, your desktop).\n\n" + " Note: This normally contains four files for an advanced statistical\n" + " analysis request:\n\n"+ " 1. README file README_xxxxxx.txt\n"+ " 2. RData file dvnDataFramefile_xxxxxx.RData\n"+ " 3. R-command file RcommandFile_xxxxxx.R\n"+ " 4. R-function file dvn_helper.R\n\n"+ " where xxxxxx is an arbitrary identification numbers.\n\n"; public String generatePt2Block(){ StrSubstitutor sub = new StrSubstitutor(valueMap); return sub.replace(pt2Template); } public static String pt3Template = "3. Run R\n\n"; public String generatePt3Block(){ StrSubstitutor sub = new StrSubstitutor(valueMap); return sub.replace(pt3Template); } public static String pt4Template = "4. From the R menus, change the working" + " directory to the location of the\n" + " replication files (for example, your desktop)\n\n"+ " Note: In windows, from R, use File -> 'Change dir ...'\n"+ " in Mac OS X, use Misc => 'Change Working Directory ...'\n\n"; public String generatePt4Block(){ StrSubstitutor sub = new StrSubstitutor(valueMap); return sub.replace(pt4Template); } public static String pt5Template = "5. Run the R-command file\n"+ " From R, use File->'Source File', and select the Rcommandfile.xxxxxx.ddddd.R\n\n"; public String generatePt5Block(){ StrSubstitutor sub = new StrSubstitutor(valueMap); return sub.replace(pt5Template); } public static String pt6Template = "6. View results -- for a cross-tabulation and descriptive statistics cases,\n"+ " results will open automatically in your browser rather than the R GUI.\n\n\n"; public String generatePt6Block(){ StrSubstitutor sub = new StrSubstitutor(valueMap); return sub.replace(pt6Template); } public static String supportTemplate = "_To Get Support_\n\n"+ " You may want to consult the 'Finding and Using Data' section of the Users Guides on the Dataverse\n"+ " Network Project website first:\n\n"+ " ${datausersGuideUrl}\n\n"+ " especially, the 'Tabular Data' page of the 'Subset and Analyze' section:\n\n"+ " ${tabdataGuideUrl}\n\n"+ " If you need to contact us about results of this modeling request,\n" + " besides your questions and comments, please include the following\n" + " request-tracking information:\n\n" + " ************************************************************\n"+ " PID=>${PID}\n"+ " Date=>${RexecDate}\n"+ " Host=>${dsbHost}\n"+ " R_version=>${R_min_verion_no}\n"+ " Title=>${studyTitle}\n"+ " No=>${studyNo}\n"+ " URL=>${studyURL}\n"+ " Type=>${option}\n"+ " ************************************************************\n"+ "\n"+ " Thank you for using the Dataverse Network.\n\n" + " Dataverse Network Project\n"+ " dvn_support@help.hmdc.harvard.edu\n"+ " http://thedata.org/contact\n\n"; public String generatePtSupportBlock(){ String dvnGuidesUrl = ""; String hostName = System.getProperty("dvn.inetAddress"); dvnGuidesUrl = "http://" + hostName + "/guides"; String datausersGuideUrl = dvnGuidesUrl + "/dataverse-user-main.html#finding-and-using-data"; String tabdataGuideUrl = dvnGuidesUrl + "/dataverse-user-main.html#tabular-data"; valueMap.put("datausersGuideUrl", datausersGuideUrl); valueMap.put("tabdataGuideUrl", tabdataGuideUrl); // TODO: -? // These URLs, for the data guides, should probably be put on the map // in the code that normally calls this class to generate the README // file, where the rest of valueMap is populated. -- L.A., v3.6 StrSubstitutor sub = new StrSubstitutor(valueMap); return sub.replace(supportTemplate); } public static String citationBlockTitle = "_Data-set Replication Citation:_\n\n"; public static String citationWholesetTitle = "1. Citation for the full data set you chose_:\n\n"; public static String citationSubsetTitle = "2. Citation for this subset you chose_:\n\n"; public static String citationSubsetingCriteriaTitle = "3. Row selection criteria for the subset used in your analysis_:\n\n"; public static String citationTemplate = citationBlockTitle + citationWholesetTitle + " ${offlineCitation}\n\n" + citationSubsetTitle + " ${offlineCitation}\n" + " ${variableList}" + " [VarGrp/@var(DDI)];\n"+ " ${fileUNF}\n\n" + citationSubsetingCriteriaTitle + " ${subsettingCriteria}\n\n"; public String generatePtCitationBlock(){ StrSubstitutor sub = new StrSubstitutor(valueMap); return sub.replace(citationTemplate); } /** * @param valueMap */ public DvnReplicationREADMEFileWriter(Map<String, String> valueMap) { this.valueMap = valueMap; } public void writeREADMEfile(File outFile) { writeREADMEfile(outFile, false); } public void writeREADMEfile(File outFile, boolean citeOnly){ OutputStream outs = null; try { outs = new BufferedOutputStream(new FileOutputStream(outFile)); PrintWriter pw = new PrintWriter(new OutputStreamWriter(outs, "utf8"), true); pw.println(generateHeaderBlock()+"\n"); if (!citeOnly) { pw.println(generatePt1Block()); pw.println(generatePt2Block()); pw.println(generatePt3Block()); pw.println(generatePt4Block()); pw.println(generatePt5Block()); pw.println(generatePt6Block()); } pw.println(generatePtCitationBlock()); pw.println(generatePtSupportBlock()); outs.close(); } catch (IOException ex) { ex.printStackTrace(); } } }