use of org.akaza.openclinica.dao.extract.ArchivedDatasetFileDAO in project OpenClinica by OpenClinica.
the class GenerateExtractFileService method createFile.
public int createFile(String zipName, ArrayList names, String dir, ArrayList contents, DatasetBean datasetBean, long time, ExportFormatBean efb, boolean saveToDB, UserAccountBean userBean) {
ArchivedDatasetFileBean fbFinal = new ArchivedDatasetFileBean();
// >> tbh #4915
zipName = zipName.replaceAll(" ", "_");
fbFinal.setId(0);
BufferedWriter w = null;
try {
File complete = new File(dir);
if (!complete.isDirectory()) {
complete.mkdirs();
}
int totalSize = 0;
ZipOutputStream z = new ZipOutputStream(new FileOutputStream(new File(complete, zipName + ".zip")));
FileInputStream is = null;
for (int i = 0; i < names.size(); i++) {
String name = (String) names.get(i);
// >> tbh #4915
name = name.replaceAll(" ", "_");
String content = (String) contents.get(i);
File newFile = new File(complete, name);
// totalSize = totalSize + (int)newFile.length();
newFile.setLastModified(System.currentTimeMillis());
w = new BufferedWriter(new FileWriter(newFile));
w.write(content);
w.close();
logger.info("finished writing the text file...");
// now, we write the file to the zip file
is = new FileInputStream(newFile);
logger.info("created zip output stream...");
z.putNextEntry(new java.util.zip.ZipEntry(name));
int bytesRead;
byte[] buff = new byte[512];
while ((bytesRead = is.read(buff)) != -1) {
z.write(buff, 0, bytesRead);
totalSize += 512;
}
z.closeEntry();
//A. Hamid. 4910
is.close();
if (CoreResources.getField("dataset_file_delete").equalsIgnoreCase("true") || CoreResources.getField("dataset_file_delete").equals("")) {
newFile.delete();
}
}
logger.info("writing buffer...");
// }
z.flush();
z.finish();
z.close();
if (is != null) {
try {
is.close();
} catch (java.io.IOException ie) {
ie.printStackTrace();
}
}
logger.info("finished zipping up file...");
// set up the zip to go into the database
if (saveToDB) {
ArchivedDatasetFileBean fb = new ArchivedDatasetFileBean();
fb.setName(zipName + ".zip");
fb.setFileReference(dir + zipName + ".zip");
// current location of the file on the system
fb.setFileSize(totalSize);
// set the above to compressed size?
fb.setRunTime((int) time);
// need to set this in milliseconds, get it passed from above
// methods?
fb.setDatasetId(datasetBean.getId());
fb.setExportFormatBean(efb);
fb.setExportFormatId(efb.getId());
fb.setOwner(userBean);
fb.setOwnerId(userBean.getId());
fb.setDateCreated(new Date(System.currentTimeMillis()));
boolean write = true;
ArchivedDatasetFileDAO asdfDAO = new ArchivedDatasetFileDAO(ds);
if (write) {
fbFinal = (ArchivedDatasetFileBean) asdfDAO.create(fb);
logger.info("Created ADSFile!: " + fbFinal.getId() + " for " + zipName + ".zip");
} else {
logger.info("duplicate found: " + fb.getName());
}
}
// created in database!
} catch (Exception e) {
logger.warn(e.getMessage());
e.printStackTrace();
} finally {
if (w != null)
try {
w.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return fbFinal.getId();
}
use of org.akaza.openclinica.dao.extract.ArchivedDatasetFileDAO in project OpenClinica by OpenClinica.
the class GenerateExtractFileService method createFile.
public int createFile(String name, String dir, String content, DatasetBean datasetBean, long time, ExportFormatBean efb, boolean saveToDB, UserAccountBean userBean) {
ArchivedDatasetFileBean fbFinal = new ArchivedDatasetFileBean();
// >> tbh 04/2010 #4915 replace all names' spaces with underscores
name = name.replaceAll(" ", "_");
fbFinal.setId(0);
try {
File complete = new File(dir);
if (!complete.isDirectory()) {
complete.mkdirs();
}
File newFile = new File(complete, name);
newFile.setLastModified(System.currentTimeMillis());
BufferedWriter w = new BufferedWriter(new FileWriter(newFile));
w.write(content);
w.close();
logger.info("finished writing the text file...");
// now, we write the file to the zip file
FileInputStream is = new FileInputStream(newFile);
ZipOutputStream z = new ZipOutputStream(new FileOutputStream(new File(complete, name + ".zip")));
logger.info("created zip output stream...");
// we write over the content no matter what
// we then check to make sure there are no duplicates
// TODO need to change the above -- save all content!
// z.write(content);
z.putNextEntry(new java.util.zip.ZipEntry(name));
// int length = (int) newFile.length();
int bytesRead;
byte[] buff = new byte[512];
// while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
while ((bytesRead = is.read(buff)) != -1) {
z.write(buff, 0, bytesRead);
}
logger.info("writing buffer...");
// }
z.closeEntry();
z.finish();
// w2.close();
if (is != null) {
try {
is.close();
} catch (java.io.IOException ie) {
ie.printStackTrace();
}
}
logger.info("finished zipping up file...");
// set up the zip to go into the database
if (saveToDB) {
ArchivedDatasetFileBean fb = new ArchivedDatasetFileBean();
fb.setName(name + ".zip");
// logger.info("ODM filename: " + name + ".zip");
fb.setFileReference(dir + name + ".zip");
// logger.info("ODM fileReference: " + dir + name + ".zip");
// current location of the file on the system
fb.setFileSize((int) newFile.length());
// logger.info("ODM setFileSize: " + (int)newFile.length() );
// set the above to compressed size?
fb.setRunTime((int) time);
// logger.info("ODM setRunTime: " + (int)time );
// need to set this in milliseconds, get it passed from above
// methods?
fb.setDatasetId(datasetBean.getId());
// logger.info("ODM setDatasetid: " + ds.getId() );
fb.setExportFormatBean(efb);
// logger.info("ODM setExportFormatBean: success" );
fb.setExportFormatId(efb.getId());
// logger.info("ODM setExportFormatId: " + efb.getId());
fb.setOwner(userBean);
// logger.info("ODM setOwner: " + sm.getUserBean());
fb.setOwnerId(userBean.getId());
// logger.info("ODM setOwnerId: " + sm.getUserBean().getId() );
fb.setDateCreated(new Date(System.currentTimeMillis()));
boolean write = true;
ArchivedDatasetFileDAO asdfDAO = new ArchivedDatasetFileDAO(ds);
// eliminating all checks so that we create multiple files, tbh 6-7
if (write) {
fbFinal = (ArchivedDatasetFileBean) asdfDAO.create(fb);
} else {
logger.info("duplicate found: " + fb.getName());
}
}
// created in database!
} catch (Exception e) {
logger.error("-- exception thrown at createFile: " + e.getMessage());
e.printStackTrace();
}
return fbFinal.getId();
}
use of org.akaza.openclinica.dao.extract.ArchivedDatasetFileDAO in project OpenClinica by OpenClinica.
the class ShowFileServlet method processRequest.
@Override
public void processRequest() throws Exception {
FormProcessor fp = new FormProcessor(request);
int fileId = fp.getInt("fileId");
int dsId = fp.getInt("datasetId");
DatasetDAO dsdao = new DatasetDAO(sm.getDataSource());
DatasetBean db = (DatasetBean) dsdao.findByPK(dsId);
ArchivedDatasetFileDAO asdfdao = new ArchivedDatasetFileDAO(sm.getDataSource());
ArchivedDatasetFileBean asdfBean = (ArchivedDatasetFileBean) asdfdao.findByPK(fileId);
ArrayList newFileList = new ArrayList();
newFileList.add(asdfBean);
// request.setAttribute("filelist",newFileList);
ArrayList filterRows = ArchivedDatasetFileRow.generateRowsFromBeans(newFileList);
EntityBeanTable table = fp.getEntityBeanTable();
String[] columns = { resword.getString("file_name"), resword.getString("run_time"), resword.getString("file_size"), resword.getString("created_date"), resword.getString("created_by") };
table.setColumns(new ArrayList(Arrays.asList(columns)));
table.hideColumnLink(0);
table.hideColumnLink(1);
table.hideColumnLink(2);
table.hideColumnLink(3);
table.hideColumnLink(4);
// table.setQuery("ExportDataset?datasetId=" +db.getId(), new
// HashMap());
// trying to continue...
// session.setAttribute("newDataset",db);
request.setAttribute("dataset", db);
request.setAttribute("file", asdfBean);
table.setRows(filterRows);
table.computeDisplay();
request.setAttribute("table", table);
Page finalTarget = Page.EXPORT_DATA_CUSTOM;
finalTarget.setFileName("/WEB-INF/jsp/extract/generateMetadataFile.jsp");
forwardPage(finalTarget);
}
use of org.akaza.openclinica.dao.extract.ArchivedDatasetFileDAO in project OpenClinica by OpenClinica.
the class AccessFileServlet method processRequest.
@Override
public void processRequest() throws Exception {
FormProcessor fp = new FormProcessor(request);
int fileId = fp.getInt("fileId");
ArchivedDatasetFileDAO asdfdao = new ArchivedDatasetFileDAO(sm.getDataSource());
DatasetDAO dsDao = new DatasetDAO(sm.getDataSource());
ArchivedDatasetFileBean asdfBean = (ArchivedDatasetFileBean) asdfdao.findByPK(fileId);
StudyDAO studyDao = new StudyDAO(sm.getDataSource());
DatasetBean dsBean = (DatasetBean) dsDao.findByPK(asdfBean.getDatasetId());
int parentId = currentStudy.getParentStudyId();
if (//Logged in at study level
parentId == 0) {
StudyBean studyBean = (StudyBean) studyDao.findByPK(dsBean.getStudyId());
//parent id of dataset created
parentId = studyBean.getParentStudyId();
}
//logic: is parentId of the dataset created not equal to currentstudy? or is current study
if (parentId != currentStudy.getId())
if (dsBean.getStudyId() != currentStudy.getId()) {
addPageMessage(respage.getString("no_have_correct_privilege_current_study") + respage.getString("change_study_contact_sysadmin"));
// TODO
throw new InsufficientPermissionException(Page.MENU_SERVLET, resexception.getString("not_allowed_access_extract_data_servlet"), "1");
}
// asdfBean.setWebPath(WEB_DIR+
// asdfBean.getDatasetId()+
// "/"+
// asdfBean.getName());
Page finalTarget = Page.EXPORT_DATA_CUSTOM;
/*
* if (asdfBean.getExportFormatId() ==
* ExportFormatBean.EXCELFILE.getId()) { //
* response.setContentType("application/octet-stream");
* response.setHeader("Content-Disposition", "attachment; filename=" +
* asdfBean.getName()); logger.info("found file name: "+
* finalTarget.getFileName()); //
* finalTarget.setFileName(asdfBean.getWebPath()); finalTarget =
* Page.GENERATE_EXCEL_DATASET; } else {
*/
logger.debug("found file reference: " + asdfBean.getFileReference() + " and file name: " + asdfBean.getName());
if (asdfBean.getFileReference().endsWith(".zip")) {
response.setHeader("Content-disposition", "attachment; filename=\"" + asdfBean.getName() + "\";");
response.setContentType("application/zip");
// response.setContentType("application/download");
} else if (asdfBean.getFileReference().endsWith(".pdf")) {
response.setHeader("Content-disposition", "attachment; filename=\"" + asdfBean.getName() + "\";");
response.setContentType("application/pdf");
// response.setContentType("application/download; application/pdf");
} else if (asdfBean.getFileReference().endsWith(".csv")) {
response.setHeader("Content-disposition", "attachment; filename=\"" + asdfBean.getName() + "\";");
response.setContentType("text/csv");
// response.setContentType("application/download; text/csv");
} else if (asdfBean.getFileReference().endsWith(".xml")) {
response.setHeader("Content-disposition", "attachment; filename=\"" + asdfBean.getName() + "\";");
response.setContentType("text/xml");
// response.setContentType("application/download; text/xml");
} else if (asdfBean.getFileReference().endsWith(".html")) {
response.setHeader("Content-disposition", "filename=\"" + asdfBean.getName() + "\";");
response.setContentType("text/html; charset=utf-8");
} else {
// response.setContentType("text/plain");
// to ensure backwards compatability to text files shown on server
// not needed anymore? tbh 10/2010
}
finalTarget.setFileName("/WEB-INF/jsp/extract/generatedFileDataset.jsp");
// }
// finalTarget.setFileName(asdfBean.getWebPath());
request.setAttribute("generate", asdfBean.getFileReference());
response.setHeader("Pragma", "public");
forwardPage(finalTarget);
}
use of org.akaza.openclinica.dao.extract.ArchivedDatasetFileDAO in project OpenClinica by OpenClinica.
the class SecureController method decodeLINKURL.
private String decodeLINKURL(String successMsg, Integer datasetId) {
ArchivedDatasetFileDAO asdfDAO = new ArchivedDatasetFileDAO(sm.getDataSource());
ArrayList<ArchivedDatasetFileBean> fileBeans = asdfDAO.findByDatasetId(datasetId);
successMsg = successMsg.replace("$linkURL", "<a href=\"" + CoreResources.getField("sysURL.base") + "AccessFile?fileId=" + fileBeans.get(0).getId() + "\">here </a>");
return successMsg;
}
Aggregations