seguinte pessoal,faço a o upload do excel…leio ele e gravo as info no banco, se logo em seguinte tento fazer mesma coisa de novo ele nao chega a nem entra no metodo do upload…
olhem o codigo
XHTML
<h:form id="form" prependId="false" enctype="multipart/form-data"
style="font-size: 10px">
<p:fileUpload id="uploader" fileUploadListener="#{ledgerMB.upload}" mode="advanced"
dragDropSupport="false" value="#{ledgerMB.uploadedFile}"
update="tabView, messages" />
</h:form>
Bean
@ViewScoped
public class LedgerMB implements Serializable {
public void upload(FileUploadEvent event) {
//
try {
if (!isXls(event)){
return;
}
DateFormat sdf = new SimpleDateFormat("HH:mm:ss");
Ledger obj;
VigenciaDAO daoVigencia = new VigenciaDAO();
Vigencia vigencia = daoVigencia.recuperarVigenciaAtiva();
String erro = null;
logger.info("Lendo arquivo: " + event.getFile().getFileName());
for (int i = 1; i < sh.getLastRowNum(); i++) {
Row row = sh.getRow(i);
if (row == null) {
break;
}
if (row.getCell(7) == null) {
break;
}else if (row.getCell(7).getStringCellValue().length()<1){
break;
}
obj = new Ledger();
try{
obj.setDocumentType(row.getCell(0) != null ? row.getCell(0).getStringCellValue() : null);
if (row.getCell(1)!=null && row.getCell(1).getCellType()!=Cell.CELL_TYPE_STRING) {
row.getCell(1).setCellType(Cell.CELL_TYPE_STRING);
}
obj.setRefDocument(row.getCell(1)!=null ? row.getCell(1).getStringCellValue():null);
obj.setHashId("" + obj.hashCode());
obj.setVigencia(vigencia);
obj.setStatus(Status.ATIVO);
listaTemp.add(obj);
logger.info("Concluída leitura da linha: " + row.getRowNum());
} catch (IllegalStateException e) {
erro = "Reading file error in row: " + row.getRowNum()+1 + "and Column: " + i+1 + ". Possible cause: " + e.getMessage();
logger.error(erro + "\n Arquivo:\t" + event.getFile().getFileName(), e);
break;
} catch (Exception e) {
erro = "Reading file error in row: " + row.getRowNum() +1 + ". Possible cause: " + e.getMessage();
logger.error(erro + "\n Arquivo:\t" + event.getFile().getFileName(), e);
break;
}
}
logger.info("Leitura de arquivo: " + event.getFile().getFileName() + " finalizada com " + listaTemp.size() + " registros.");
if (erro!=null){
MessagesUtil.exibeMensagem(erro , "Upload Error", false);
}
} catch (Exception e) {
logger.error(e.getMessage(), e);
MessagesUtil.exibeMensagem(e.getMessage(), "Upload Error", false);
}
}
//verifica se atende o padrao
public boolean isXls(FileUploadEvent event) throws IOException {
if (event == null || event.getFile() == null) {
MessagesUtil.exibeMensagem(
"Invalid file.",
"Error on loading file", false);
return false;
}
String contentType = event.getFile().getContentType();
if (!(contentType.equalsIgnoreCase(TYPE_XLS)
|| contentType.equalsIgnoreCase(TYPE_XLSX)|| contentType.equalsIgnoreCase(TYPE_UNIX))) {
MessagesUtil.exibeMensagem(
"Invalid file type.",
"Error on loading file", false);
return false;
}
switch (contentType) {
case TYPE_XLS:
case TYPE_UNIX:
HSSFWorkbook wb = new HSSFWorkbook(event.getFile().getInputstream());
sh = wb.getSheetAt(0);
break;
case TYPE_XLSX:
XSSFWorkbook sx = new XSSFWorkbook(event.getFile().getInputstream());
sh = sx.getSheetAt(0);
break;
default:
throw new InvalidFileNameException(contentType,
"Invalid file type.");
}
try {
if (validaExcel.compararLedger(sh.getRow(0))) {
MessagesUtil.exibeMensagem(
"Columns incompatibles with defined layout",
"Invalid Columns", false);
return false;
}
} catch (Exception e) {
logger.error(e.getMessage(), e);
return false;
}
return true;
}
public void limpar(ActionEvent event) {
listaTemp.clear();
setListaFilter(null);
setListaTempFilter(null);
setUploadedFile(null);
}
public void gravar(ActionEvent event) {
int i = dao.inserir(listaTemp);
if (i > 0) {
limpar(event);
carregarLista();
MessagesUtil.exibeMensagem(i + " row(s) inserted successfully.",
"Ledger", true);
if (lista == null || lista.isEmpty()) {
MessagesUtil.exibeMensagem("Error on loading data.", "", false);
}
} else {
MessagesUtil.exibeMensagem("No registry was inserted.",
"Data writing", false);
}
}
}