X-Git-Url: http://200.18.67.61/gitweb/?a=blobdiff_plain;f=src%2Fjava%2Forg%2Fufcspa%2Fsimdecs%2Fbn%2Fui%2FFileUploadController.java;fp=src%2Fjava%2Forg%2Fufcspa%2Fsimdecs%2Fbn%2Fui%2FFileUploadController.java;h=aa5f25e08b7b4769775c3d32ddf3345fb1d615d7;hb=c547eea0255390e5763eac8ffbca336a1acf5b41;hp=0000000000000000000000000000000000000000;hpb=5d05ee58812cfa8b58ab2dcf93a5d3a3bb71d6aa;p=simdecs.git diff --git a/src/java/org/ufcspa/simdecs/bn/ui/FileUploadController.java b/src/java/org/ufcspa/simdecs/bn/ui/FileUploadController.java new file mode 100644 index 0000000..aa5f25e --- /dev/null +++ b/src/java/org/ufcspa/simdecs/bn/ui/FileUploadController.java @@ -0,0 +1,106 @@ +/* + * 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); + } + } +} + \ No newline at end of file