private List<FileReadAttributeBean> processFileReadBeans( String _projectName, String _sampleName, List<FileReadAttributeBean> loadingList) throws Exception { List<FileReadAttributeBean> processedList = new ArrayList<FileReadAttributeBean>(); for (FileReadAttributeBean fBean : loadingList) { if (fBean.getProjectName() == null || eventName.equals(Constants.EVENT_PROJECT_REGISTRATION)) { fBean.setProjectName(_projectName); } if (fBean.getSampleName() == null) { fBean.setSampleName(_sampleName); } // handle file uploads if (fBean.getUpload() != null && fBean.getUploadFileName() != null && !fBean.getUploadFileName().isEmpty()) { fileStoragePath = fileStoragePath + (fileStoragePath.endsWith(File.separator) ? "" : File.separator); String originalFileName = fBean.getUploadFileName(); String fileDirectoryPathProject = _projectName.replaceAll(" ", "_"); // project folder String fileDirectoryPathSample = fileDirectoryPathProject + File.separator + (_sampleName != null && !_sampleName.isEmpty() ? _sampleName.replaceAll(" ", "_") : "project"); // sample folder String fileDirectoryPath = fileDirectoryPathSample + File.separator + CommonTool.convertTimestampToDate(new Date()); // date folder String fileName = originalFileName.substring(0, originalFileName.indexOf(".")) + "_" + System.currentTimeMillis() + originalFileName.substring( originalFileName.indexOf( ".")); // append "_" + current time in milliseconds to file name File fileDirectory = new File(fileStoragePath + fileDirectoryPath); if (!fileDirectory.exists() || !fileDirectory.isDirectory()) { fileDirectory.mkdirs(); } File theFile = new File(fileDirectory.getPath() + File.separator + fileName); FileUtils.copyFile(fBean.getUpload(), theFile); if (theFile.exists() && theFile.isFile() && theFile.canRead()) { fBean.getUpload().delete(); fBean.setAttributeValue(fileDirectoryPath + File.separator + fileName); if (loadedFiles == null) { loadedFiles = new ArrayList<String>(); } loadedFiles.add(fBean.getAttributeValue()); } } if (!fBean.getAttributeName().equals("0") && fBean.getAttributeValue() != null && !fBean.getAttributeValue().equals("0") && !fBean.getAttributeValue().isEmpty()) { processedList.add(fBean); } } return processedList; }
public String execute() { String rtnVal = SUCCESS; UserTransaction tx = null; try { sampleName = sampleName != null && sampleName.equals("0") ? null : sampleName; if (jobType != null) { boolean isProjectRegistration = eventName.equals(Constants.EVENT_PROJECT_REGISTRATION); boolean isSampleRegistration = eventName.equals(Constants.EVENT_SAMPLE_REGISTRATION); if (projectName == null || projectName.equals("0") || eventName == null || eventName.equals("0")) throw new Exception("Project or Event type is not selected."); if (jobType.equals("insert")) { // loads single event tx = (UserTransaction) new InitialContext().lookup("java:comp/UserTransaction"); tx.begin(); psewt.loadAll( null, this.createMultiLoadParameter(projectName, loadingProject, loadingSample, beanList)); this.reset(); } else if (jobType.equals("grid")) { // loads multiple events from grid view tx = (UserTransaction) new InitialContext().lookup("java:comp/UserTransaction"); tx.begin(); for (GridBean gBean : gridList) { if (gBean != null) { if (isProjectRegistration && gBean.getProjectName() != null && gBean.getProjectPublic() != null) { loadingProject = new Project(); loadingProject.setProjectName(gBean.getProjectName()); loadingProject.setIsPublic(Integer.valueOf(gBean.getProjectPublic())); } else if (isSampleRegistration && gBean.getSampleName() != null && gBean.getSamplePublic() != null) { loadingSample = new Sample(); loadingSample.setSampleName(gBean.getSampleName()); loadingSample.setParentSampleName(gBean.getParentSampleName()); loadingSample.setIsPublic(Integer.valueOf(gBean.getSamplePublic())); } else { if (gBean.getSampleName() != null) { this.sampleName = gBean.getSampleName(); } } List<FileReadAttributeBean> fBeanList = gBean.getBeanList(); if (fBeanList != null && fBeanList.size() > 0) { psewt.loadAll( null, this.createMultiLoadParameter( projectName, loadingProject, loadingSample, fBeanList)); } } } this.reset(); } else if (jobType.equals("file")) { // loads data from a CSV file to grid view if (!this.uploadFile.canRead()) { throw new Exception("Error in reading the file."); } else { try { CSVReader reader = new CSVReader(new FileReader(this.uploadFile)); int lineCount = 0; List<String> columns = new ArrayList<String>(); String currProjectName = null; gridList = new ArrayList<GridBean>(); boolean hasSampleName = false; String[] line; while ((line = reader.readNext()) != null) { if (lineCount != 1) { if (lineCount == 0) { Collections.addAll(columns, line); hasSampleName = columns.indexOf("SampleName") >= 0; } else { int colIndex = 0; currProjectName = line[colIndex++]; if (!isProjectRegistration && !currProjectName.equals(this.projectName)) { throw new Exception(MULTIPLE_SUBJECT_IN_FILE_MESSAGE); } GridBean gBean = new GridBean(); gBean.setProjectName(currProjectName); if (hasSampleName) { gBean.setSampleName(line[(colIndex++)]); } if (isProjectRegistration) { gBean.setProjectName(currProjectName); gBean.setProjectPublic(line[(colIndex++)]); } else if (isSampleRegistration) { gBean.setParentSampleName(line[(colIndex++)]); gBean.setSamplePublic(line[(colIndex++)]); } gBean.setBeanList(new ArrayList<FileReadAttributeBean>()); for (; colIndex < columns.size(); colIndex++) { FileReadAttributeBean fBean = new FileReadAttributeBean(); fBean.setProjectName( isProjectRegistration ? currProjectName : this.projectName); fBean.setAttributeName(columns.get(colIndex)); fBean.setAttributeValue(line[colIndex]); gBean.getBeanList().add(fBean); } this.gridList.add(gBean); } } lineCount++; } jobType = "grid"; } catch (Exception ex) { throw ex; } } } else if (jobType.equals("template")) { // download template List<EventMetaAttribute> emaList = readPersister.getEventMetaAttributes(projectName, eventName); /* * removing the sanity check on sample requirement since multiple sample support is in action * by hkim 5/2/13 ModelValidator validator = new ModelValidator(); validator.validateEventTemplateSanity(emaList, projectName, sampleName, eventName); */ TemplatePreProcessingUtils cvsUtils = new TemplatePreProcessingUtils(); String templateType = jobType.substring(jobType.indexOf("_") + 1); downloadStream = cvsUtils.buildFileContent(templateType, emaList, projectName, sampleName, eventName); downloadContentType = templateType.equals("c") ? "csv" : "vnd.ms-excel"; rtnVal = Constants.FILE_DOWNLOAD_MSG; } } } catch (Exception ex) { logger.error("Exception in EventLoader : " + ex.toString()); ex.printStackTrace(); if (ex.getClass() == ForbiddenResourceException.class) { addActionError(Constants.DENIED_USER_EDIT_MESSAGE); return Constants.FORBIDDEN_ACTION_RESPONSE; } else if (ex.getClass() == ForbiddenResourceException.class) { addActionError(Constants.DENIED_USER_EDIT_MESSAGE); return LOGIN; } else if (ex.getClass() == ParseException.class) addActionError(Constants.INVALID_DATE_MESSAGE); else { addActionError(ex.toString()); } // deletes uploaded files in event of error if (loadedFiles != null && loadedFiles.size() > 0) { for (String filePath : loadedFiles) { File tempFile = new File(fileStoragePath + filePath); if (tempFile.exists()) tempFile.delete(); } } try { if (tx != null) tx.rollback(); } catch (SystemException se) { addActionError(se.toString()); } rtnVal = ERROR; } finally { try { // get project list for the drop down box List<String> projectNameList = new ArrayList<String>(); if (projectNames == null || projectNames.equals("")) { projectNameList.add("ALL"); } else if (projectNames.contains(",")) { projectNameList.addAll(Arrays.asList(projectNames.split(","))); } else { projectNameList.add(projectNames); } projectList = readPersister.getProjects(projectNameList); if (tx != null && tx.getStatus() != Status.STATUS_NO_TRANSACTION) { tx.commit(); } if (jobType != null && jobType.equals("grid") && this.uploadFile != null) { this.uploadFile.delete(); } } catch (Exception ex) { ex.printStackTrace(); } } return rtnVal; }