/** * Imports the entities * * @return the SUCCESS result */ public String importCSV() throws Exception { File file = this.getUpload(); CsvListReader reader = new CsvListReader(new FileReader(file), CsvPreference.EXCEL_PREFERENCE); int failedNum = 0; int successfulNum = 0; try { final String[] header = reader.getCSVHeader(true); List<String> line = new ArrayList<String>(); Map<String, String> failedMsg = new HashMap<String, String>(); while ((line = reader.read()) != null) { Map<String, String> row = new HashMap<String, String>(); for (int i = 0; i < line.size(); i++) { row.put(header[i], line.get(i)); } TargetList targetList = new TargetList(); try { String id = row.get(getText("entity.id.label")); if (!CommonUtil.isNullOrEmpty(id)) { targetList.setId(Integer.parseInt(id)); } targetList.setName(CommonUtil.fromNullToEmpty(row.get(getText("entity.name.label")))); targetList.setDescription( CommonUtil.fromNullToEmpty(row.get(getText("entity.description.label")))); targetList.setNotes(CommonUtil.fromNullToEmpty(row.get(getText("entity.notes.label")))); String assignedToID = row.get(getText("entity.assigned_to_id.label")); if (CommonUtil.isNullOrEmpty(assignedToID)) { targetList.setAssigned_to(null); } else { User assignedTo = userService.getEntityById(User.class, Integer.parseInt(assignedToID)); targetList.setAssigned_to(assignedTo); } baseService.makePersistent(targetList); successfulNum++; } catch (Exception e) { failedNum++; String Name = CommonUtil.fromNullToEmpty(targetList.getName()); failedMsg.put(Name, e.getMessage()); } } this.setFailedMsg(failedMsg); this.setFailedNum(failedNum); this.setSuccessfulNum(successfulNum); this.setTotalNum(successfulNum + failedNum); } finally { reader.close(); } return SUCCESS; }
/** * Copies the selected entities * * @return the SUCCESS result */ public String copy() throws Exception { UserUtil.permissionCheck("create_targetList"); if (this.getSeleteIDs() != null) { String[] ids = seleteIDs.split(","); for (int i = 0; i < ids.length; i++) { String copyid = ids[i]; TargetList oriRecord = baseService.getEntityById(TargetList.class, Integer.valueOf(copyid)); TargetList targetListRecord = oriRecord.clone(); targetListRecord.setId(null); this.getbaseService().makePersistent(targetListRecord); } } return SUCCESS; }