/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package org.ufcspa.simdecs.bn.ui; import java.io.*; import java.util.ArrayList; import java.util.logging.Level; import java.util.logging.Logger; import javax.annotation.Resource; import javax.faces.application.FacesMessage; import javax.faces.bean.ApplicationScoped; import javax.faces.bean.ManagedBean; import javax.faces.context.FacesContext; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.PersistenceUnit; import javax.transaction.UserTransaction; import javax.xml.bind.JAXBException; import org.primefaces.event.FileUploadEvent; import org.primefaces.model.UploadedFile; import org.ufcspa.simdecs.bn.entity.BayesianNetwork; import org.ufcspa.simdecs.bn.entity.Node; import unbbayes.io.exception.LoadException; import unbbayes.io.xmlbif.version6.XMLBIFIO; import unbbayes.prs.bn.ProbabilisticNetwork; /** * * @author mchelem */ @ManagedBean(name = "fileUploadController") @ApplicationScoped public class FileUploadController implements Serializable { @Resource private UserTransaction utx = null; @PersistenceUnit(unitName = "simdecsEclipseLinkPU") private EntityManagerFactory emf = null; EntityManager em = null; private static final Logger logger = Logger.getLogger("" + FileUploadController.class); private File saveUploadedFile(UploadedFile uploadedFile) throws IOException{ InputStream in = new BufferedInputStream(uploadedFile.getInputstream()); File file = new File(uploadedFile.getFileName()); FileOutputStream fout = new FileOutputStream(file); while(in.available() != 0){ fout.write(in.read()); } logger.log(Level.INFO, "Uploaded file saved in: {0}", file.getAbsolutePath()); return file; } private void persist(BayesianNetwork bayesianNetwork){ em = emf.createEntityManager(); try { utx.begin(); em.persist(bayesianNetwork); utx.commit(); } catch (Exception e) { try { utx.rollback(); } catch (Exception ex) { Logger.getLogger(FileUploadController.class.getName()).log(Level.SEVERE, null, ex); } Logger.getLogger(FileUploadController.class.getName()).log(Level.INFO, null, e); } logger.log(Level.INFO, "Bayesian Network {0} persisted.", bayesianNetwork.getName()); } public void handleFileUpload(FileUploadEvent event) { try { File file = this.saveUploadedFile(event.getFile()); // required to run unbbayes gui classes on server System.setProperty("java.awt.headless", "false"); ProbabilisticNetwork bn = new ProbabilisticNetwork(null); XMLBIFIO.loadXML(new File(file.getAbsolutePath()), bn); BayesianNetwork bayesianNetwork = new BayesianNetwork(); bayesianNetwork.setName(bn.getName()); ArrayList nodes = new ArrayList(); for (unbbayes.prs.Node prs_node: bn.getNodes()){ Node node = new Node(); node.setName(prs_node.getName()); node.setBayesianNetwork(bayesianNetwork); nodes.add(node); } bayesianNetwork.setNodes(nodes); persist(bayesianNetwork); FacesMessage msg = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded."); FacesContext.getCurrentInstance().addMessage(null, msg); } catch (LoadException ex) { Logger.getLogger(FileUploadController.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(FileUploadController.class.getName()).log(Level.SEVERE, null, ex); } catch (JAXBException ex) { Logger.getLogger(FileUploadController.class.getName()).log(Level.SEVERE, null, ex); } } }