use of org.akaza.openclinica.dao.hibernate.MeasurementUnitDao in project OpenClinica by OpenClinica.
the class CreateCRFVersionServlet method uploadFile.
/**
* Uploads the excel version file
*
* @param version
* @throws Exception
*/
public String uploadFile(String theDir, CRFVersionBean version) throws Exception {
List<File> theFiles = uploadHelper.returnFiles(request, context, theDir);
// Enumeration files = multi.getFileNames();
errors.remove("excel_file");
String tempFile = null;
for (File f : theFiles) {
// File f = multi.getFile(name);
if (f == null || f.getName() == null) {
logger.debug("file is empty.");
Validator.addError(errors, "excel_file", resword.getString("you_have_to_provide_spreadsheet"));
session.setAttribute("version", version);
return tempFile;
} else if (f.getName().indexOf(".xls") < 0 && f.getName().indexOf(".XLS") < 0) {
logger.debug("file name:" + f.getName());
Validator.addError(errors, "excel_file", respage.getString("file_you_uploaded_not_seem_excel_spreadsheet"));
session.setAttribute("version", version);
return tempFile;
} else {
logger.debug("file name:" + f.getName());
tempFile = f.getName();
// create the inputstream here, so that it can be enclosed in a
// try/finally block and closed :: BWP, 06/08/2007
FileInputStream inStream = null;
FileInputStream inStreamClassic = null;
SpreadSheetTableRepeating htab = null;
SpreadSheetTableClassic sstc = null;
// create newCRFBean here
NewCRFBean nib = null;
try {
inStream = new FileInputStream(theDir + tempFile);
// *** now change the code here to generate sstable, tbh
// 06/07
htab = new SpreadSheetTableRepeating(inStream, ub, // FileInputStream(theDir + tempFile), ub,
version.getName(), locale, currentStudy.getId());
htab.setMeasurementUnitDao((MeasurementUnitDao) SpringServletAccess.getApplicationContext(context).getBean("measurementUnitDao"));
if (!htab.isRepeating()) {
inStreamClassic = new FileInputStream(theDir + tempFile);
sstc = new SpreadSheetTableClassic(inStreamClassic, ub, version.getName(), locale, currentStudy.getId());
sstc.setMeasurementUnitDao((MeasurementUnitDao) SpringServletAccess.getApplicationContext(context).getBean("measurementUnitDao"));
}
if (htab.isRepeating()) {
htab.setCrfId(version.getCrfId());
// not the best place for this but for now...
session.setAttribute("new_table", "y");
} else {
sstc.setCrfId(version.getCrfId());
}
if (htab.isRepeating()) {
nib = htab.toNewCRF(sm.getDataSource(), respage);
} else {
nib = sstc.toNewCRF(sm.getDataSource(), respage);
}
// bwp; 2/28/07; updated 6/11/07;
// This object is created to pull preview information out of
// the
// spreadsheet
HSSFWorkbook workbook = null;
FileInputStream inputStream = null;
try {
inputStream = new FileInputStream(theDir + tempFile);
workbook = new HSSFWorkbook(inputStream);
// Store the Sections, Items, Groups, and CRF name and
// version information
// so they can be displayed in a preview. The Map
// consists of the
// names "sections," "items," "groups," and "crf_info"
// as keys, each of which point
// to a Map containing data on those CRF sections.
// Check if it's the old template
Preview preview;
if (htab.isRepeating()) {
// the preview uses date formatting with default
// values in date fields: yyyy-MM-dd
preview = new SpreadsheetPreviewNw();
} else {
preview = new SpreadsheetPreview();
}
session.setAttribute("preview_crf", preview.createCrfMetaObject(workbook));
} catch (Exception exc) {
// opening the stream could
// throw FileNotFoundException
exc.printStackTrace();
String message = resword.getString("the_application_encountered_a_problem_uploading_CRF");
logger.debug(message + ": " + exc.getMessage());
this.addPageMessage(message);
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException io) {
// ignore this close()-related exception
}
}
}
ArrayList ibs = isItemSame(nib.getItems(), version);
if (!ibs.isEmpty()) {
ArrayList warnings = new ArrayList();
warnings.add(resexception.getString("you_may_not_modify_items"));
for (int i = 0; i < ibs.size(); i++) {
ItemBean ib = (ItemBean) ibs.get(i);
if (ib.getOwner().getId() == ub.getId()) {
warnings.add(resword.getString("the_item") + " '" + ib.getName() + "' " + resexception.getString("in_your_spreadsheet_already_exists") + ib.getDescription() + "), DATA_TYPE(" + ib.getDataType().getName() + "), UNITS(" + ib.getUnits() + "), " + resword.getString("and_or") + " PHI_STATUS(" + ib.isPhiStatus() + "). UNITS " + resword.getString("and") + " DATA_TYPE(PDATE to DATE) " + resexception.getString("will_not_be_changed_if") + " PHI, DESCRIPTION, DATA_TYPE from PDATE to DATE " + resexception.getString("will_be_changed_if_you_continue"));
} else {
warnings.add(resword.getString("the_item") + " '" + ib.getName() + "' " + resexception.getString("in_your_spreadsheet_already_exists") + ib.getDescription() + "), DATA_TYPE(" + ib.getDataType().getName() + "), UNITS(" + ib.getUnits() + "), " + resword.getString("and_or") + " PHI_STATUS(" + ib.isPhiStatus() + "). " + resexception.getString("these_field_cannot_be_modified_because_not_owner"));
}
request.setAttribute("warnings", warnings);
}
}
ItemBean ib = isResponseValid(nib.getItems(), version);
if (ib != null) {
nib.getErrors().add(resword.getString("the_item") + ": " + ib.getName() + " " + resexception.getString("in_your_spreadsheet_already_exits_in_DB"));
}
} catch (IOException io) {
logger.warn("Opening up the Excel file caused an error. the error message is: " + io.getMessage());
} finally {
if (inStream != null) {
try {
inStream.close();
} catch (IOException ioe) {
}
}
if (inStreamClassic != null) {
try {
inStreamClassic.close();
} catch (IOException ioe) {
}
}
}
// request.setAttribute("excelErrors", .getErrors());
session.setAttribute("excelErrors", nib.getErrors());
session.setAttribute("htmlTable", nib.getHtmlTable());
session.setAttribute("nib", nib);
}
}
return tempFile;
}
Aggregations