In my application when i try to upload from my excel, it need to upload the data from the scratch not append. but in my case it is appending the data. I m trying to clear the list Before I upload the data, but some problem my logic is not working.
here is my code of controller
// upload Question bank
@RequestMapping(value="questionupload", method = RequestMethod.POST)
public @ResponseBody String uploadQuestionBankList(MultipartHttpServletRequest request, HttpServletResponse response, @RequestParam Long moduleId, @RequestParam String langId, @RequestParam String topicId) throws TopicNotFound{
try {
List<QuestionBankDetails> list = new ArrayList<QuestionBankDetails>();
MultipartFile multipartFile = request.getFile("filename");
String [] fullNameWithExt=multipartFile.getOriginalFilename().toString().split(Pattern.quote("."));
if(fullNameWithExt[fullNameWithExt.length-1].equalsIgnoreCase("xlsx")){
XSSFWorkbook workbook=new XSSFWorkbook(multipartFile.getInputStream());
XSSFSheet sheet = workbook.getSheetAt(0);
Iterator<Row> rowIterator = sheet.iterator();
if(rowIterator.hasNext()){
rowIterator.next();
}
while(rowIterator.hasNext()){
Row row = rowIterator.next();
QuestionBankDetails questionMaster = new QuestionBankDetails();
Cell cell;
if(row.getCell(0)!=null){
cell = row.getCell(0);
cell.setCellType(Cell.CELL_TYPE_STRING);
String eid=null;
if(cell.getStringCellValue().trim().length()>0)
eid = cell.getStringCellValue();
if(eid.length()>0){
cell.setCellType(Cell.CELL_TYPE_STRING);
if(cell.getStringCellValue().trim().length()>0)
questionMaster.setQuestion(cell.getStringCellValue());
if(row.getCell(1)!=null){
cell = row.getCell(1);
cell.setCellType(Cell.CELL_TYPE_STRING);
if(cell.getStringCellValue().trim().length()>0)
questionMaster.setOpt1(cell.getStringCellValue());
}
if(row.getCell(2)!=null){
cell = row.getCell(2);
cell.setCellType(Cell.CELL_TYPE_STRING);
if(cell.getStringCellValue().trim().length()>0)
questionMaster.setOpt2(cell.getStringCellValue());
}
if(row.getCell(3)!=null){
cell = row.getCell(3);
cell.setCellType(Cell.CELL_TYPE_STRING);
if(cell.getStringCellValue().trim().length()>0)
questionMaster.setOpt3(cell.getStringCellValue());
}
if(row.getCell(4)!=null){
cell = row.getCell(4);
cell.setCellType(Cell.CELL_TYPE_STRING);
if(cell.getStringCellValue().trim().length()>0)
questionMaster.setOpt4(cell.getStringCellValue());
}
if(row.getCell(5)!=null){
cell = row.getCell(5);
cell.setCellType(Cell.CELL_TYPE_STRING);
if(cell.getStringCellValue().trim().length()>0)
questionMaster.setOpt5(cell.getStringCellValue());
}
if(row.getCell(6)!=null){
cell = row.getCell(6);
cell.setCellType(Cell.CELL_TYPE_STRING);
if(cell.getStringCellValue().trim().length()>0)
questionMaster.setCorrectAnswer(cell.getStringCellValue());
}
if(row.getCell(7)!=null){
cell = row.getCell(7);
cell.setCellType(Cell.CELL_TYPE_STRING);
if(cell.getStringCellValue().trim().length()>0)
questionMaster.setQuestionType(cell.getStringCellValue());
}
if(row.getCell(8)!=null){
cell = row.getCell(8);
cell.setCellType(Cell.CELL_TYPE_STRING);
if(cell.getStringCellValue().trim().length()>0)
questionMaster.setSectionId(cell.getStringCellValue());
}
if(row.getCell(9)!=null){
cell = row.getCell(9);
cell.setCellType(Cell.CELL_TYPE_STRING);
if(cell.getStringCellValue().trim().length()>0)
questionMaster.setLevel(cell.getStringCellValue());
}
questionMaster.setTopicId(topicId);
list.add(questionMaster);
}
}
}
} else if(fullNameWithExt[fullNameWithExt.length-1].equalsIgnoreCase("xls")){
Workbook workbook= WorkbookFactory.create(multipartFile.getInputStream());
Sheet sheet = workbook.getSheetAt(0);
Iterator<Row> rowIterator = sheet.iterator();
if(rowIterator.hasNext()){
rowIterator.next();
}
while(rowIterator.hasNext()){
Row row = rowIterator.next();
QuestionBankDetails questionMaster = new QuestionBankDetails();
Cell cell;
if(row.getCell(0)!=null){
cell = row.getCell(0);
cell.setCellType(Cell.CELL_TYPE_STRING);
String eid=null;
if(cell.getStringCellValue().trim().length()>0)
eid = cell.getStringCellValue();
if(eid.length()>0){
cell.setCellType(Cell.CELL_TYPE_STRING);
if(cell.getStringCellValue().trim().length()>0)
questionMaster.setQuestion(cell.getStringCellValue());
if(row.getCell(1)!=null){
cell = row.getCell(1);
cell.setCellType(Cell.CELL_TYPE_STRING);
if(cell.getStringCellValue().trim().length()>0)
questionMaster.setOpt1(cell.getStringCellValue());
}
if(row.getCell(2)!=null){
cell = row.getCell(2);
cell.setCellType(Cell.CELL_TYPE_STRING);
if(cell.getStringCellValue().trim().length()>0)
questionMaster.setOpt2(cell.getStringCellValue());
}
if(row.getCell(3)!=null){
cell = row.getCell(3);
cell.setCellType(Cell.CELL_TYPE_STRING);
if(cell.getStringCellValue().trim().length()>0)
questionMaster.setOpt3(cell.getStringCellValue());
}
if(row.getCell(4)!=null){
cell = row.getCell(4);
cell.setCellType(Cell.CELL_TYPE_STRING);
if(cell.getStringCellValue().trim().length()>0)
questionMaster.setOpt4(cell.getStringCellValue());
}
if(row.getCell(5)!=null){
cell = row.getCell(5);
cell.setCellType(Cell.CELL_TYPE_STRING);
if(cell.getStringCellValue().trim().length()>0)
questionMaster.setOpt5(cell.getStringCellValue());
}
if(row.getCell(6)!=null){
cell = row.getCell(6);
cell.setCellType(Cell.CELL_TYPE_STRING);
if(cell.getStringCellValue().trim().length()>0)
questionMaster.setCorrectAnswer(cell.getStringCellValue());
}
if(row.getCell(7)!=null){
cell = row.getCell(7);
cell.setCellType(Cell.CELL_TYPE_STRING);
if(cell.getStringCellValue().trim().length()>0)
questionMaster.setQuestionType(cell.getStringCellValue());
}
if(row.getCell(8)!=null){
cell = row.getCell(8);
cell.setCellType(Cell.CELL_TYPE_STRING);
if(cell.getStringCellValue().trim().length()>0)
questionMaster.setSectionId(cell.getStringCellValue());
}
if(row.getCell(9)!=null){
cell = row.getCell(9);
cell.setCellType(Cell.CELL_TYPE_STRING);
if(cell.getStringCellValue().trim().length()>0)
questionMaster.setLevel(cell.getStringCellValue());
}
questionMaster.setTopicId(topicId);
list.add(questionMaster);
}
}
}
}
moduleService.addListOfQuestionByModuleId(list, langId, moduleId);
return "1";
}catch (TopicNotFound e) {
return "Topic with'"+e.getMessage()+"'topic is not found";
} catch (Exception e) {
logger.error("Error while uploading the Question Bank List", e);
}
return "Some Problem While Adding";
}
This is my service implementation code
@Transactional(propagation=Propagation.REQUIRED)
public void addListOfQuestionByModuleId(
List<QuestionBankDetails> questionBankDetails, String langId, Long moduleId) throws ModuleNotFound, TopicNotFound, LanguageNotFound {
ModuleDetails moduleDetails = moduleDetailsDao.getModuleDetailsByMid(moduleId);
if(moduleDetails==null){
throw new ModuleNotFound();
}else{
LanguageDetails languageDetails = languageDetailsDao.getLanguageDetailsByLangId(langId);
if(languageDetails==null){
throw new LanguageNotFound();
}else {
if(moduleDetails.getLanguageDetails().contains(languageDetails)){
for (QuestionBankDetails currentQ : questionBankDetails) {
/* List<QBankMaster> bankMaster = qBankMasterDao.getQBankMastersByTopicId(currentQ.getTopicId()); // tried to clear the list before display
bankMaster.removeAll(bankMaster);*/
TopicDetails topicDetails = topicDetailsDao.getTopicDetailsByTopicId(new Long(currentQ.getTopicId()));
if(topicDetails!=null){
//check weather topic found is the topic of the current module
if(moduleDetails.getTopicDetails().contains(topicDetails)){
//Question master for Question
QBankMaster qm = new QBankMaster();
//Question type Master for the type of question
QtypeDetails qt = new QtypeDetails();
qt.setQtypeDesc(currentQ.getQuestionType());
qt.setActive(1);
//Section id is given take the Section Details from id given
qm.setActive(1);
qm.setAnswer(currentQ.getCorrectAnswer());
qm.setLevelMaster(levelMasterDao.getLevelByLevelCode(currentQ.getLevel()));
qm.setOption1(currentQ.getOpt1());
qm.setOption2(currentQ.getOpt2());
qm.setOption3(currentQ.getOpt3());
qm.setOption4(currentQ.getOpt4());
qm.setOption5(currentQ.getOpt5());
qm.setQtypeDetails(qt);
qm.setLanguageDetails(languageDetails);
qm.setQuestion(currentQ.getQuestion());
qm.setSectionDetails(sectionDao.getSectionDetailsBySectionCode(currentQ.getSectionId()));
qm.setTopicDetails(topicDetails);
qBankMasterDao.save(qm);
}else {
throw new TopicNotFound(currentQ.getTopicId());
}
}else {
throw new TopicNotFound(currentQ.getTopicId());
}
}
}else {
throw new LanguageNotFound();
}
}
}
}
Every time I upload I want my new data to be inserted and I want my old data to be cleared
Aucun commentaire:
Enregistrer un commentaire