2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
5 package org.ufcspa.simdecs.bn.ui;
8 import java.util.ArrayList;
9 import java.util.logging.Level;
10 import java.util.logging.Logger;
11 import javax.annotation.Resource;
12 import javax.faces.application.FacesMessage;
13 import javax.faces.bean.ApplicationScoped;
14 import javax.faces.bean.ManagedBean;
15 import javax.faces.context.FacesContext;
16 import javax.persistence.EntityManager;
17 import javax.persistence.EntityManagerFactory;
18 import javax.persistence.PersistenceUnit;
19 import javax.transaction.UserTransaction;
20 import javax.xml.bind.JAXBException;
21 import org.primefaces.event.FileUploadEvent;
22 import org.primefaces.model.UploadedFile;
23 import org.ufcspa.simdecs.bn.entity.BayesianNetwork;
24 import org.ufcspa.simdecs.bn.entity.Node;
25 import unbbayes.io.exception.LoadException;
26 import unbbayes.io.xmlbif.version6.XMLBIFIO;
27 import unbbayes.prs.bn.ProbabilisticNetwork;
33 @ManagedBean(name = "fileUploadController")
35 public class FileUploadController implements Serializable {
37 private UserTransaction utx = null;
38 @PersistenceUnit(unitName = "simdecsEclipseLinkPU")
39 private EntityManagerFactory emf = null;
40 EntityManager em = null;
42 private static final Logger logger = Logger.getLogger("" + FileUploadController.class);
44 private File saveUploadedFile(UploadedFile uploadedFile) throws IOException{
45 InputStream in = new BufferedInputStream(uploadedFile.getInputstream());
46 File file = new File(uploadedFile.getFileName());
47 FileOutputStream fout = new FileOutputStream(file);
48 while(in.available() != 0){
49 fout.write(in.read());
51 logger.log(Level.INFO, "Uploaded file saved in: {0}", file.getAbsolutePath());
55 private void persist(BayesianNetwork bayesianNetwork){
56 em = emf.createEntityManager();
59 em.persist(bayesianNetwork);
61 } catch (Exception e) {
64 } catch (Exception ex) {
65 Logger.getLogger(FileUploadController.class.getName()).log(Level.SEVERE, null, ex);
67 Logger.getLogger(FileUploadController.class.getName()).log(Level.INFO, null, e);
69 logger.log(Level.INFO, "Bayesian Network {0} persisted.", bayesianNetwork.getName());
74 public void handleFileUpload(FileUploadEvent event) {
76 File file = this.saveUploadedFile(event.getFile());
77 // required to run unbbayes gui classes on server
78 System.setProperty("java.awt.headless", "false");
80 ProbabilisticNetwork bn = new ProbabilisticNetwork(null);
81 XMLBIFIO.loadXML(new File(file.getAbsolutePath()), bn);
82 BayesianNetwork bayesianNetwork = new BayesianNetwork();
83 bayesianNetwork.setName(bn.getName());
85 ArrayList<Node> nodes = new ArrayList<Node>();
86 for (unbbayes.prs.Node prs_node: bn.getNodes()){
87 Node node = new Node();
88 node.setName(prs_node.getName());
89 node.setBayesianNetwork(bayesianNetwork);
92 bayesianNetwork.setNodes(nodes);
93 persist(bayesianNetwork);
95 FacesMessage msg = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded.");
96 FacesContext.getCurrentInstance().addMessage(null, msg);
97 } catch (LoadException ex) {
98 Logger.getLogger(FileUploadController.class.getName()).log(Level.SEVERE, null, ex);
99 } catch (IOException ex) {
100 Logger.getLogger(FileUploadController.class.getName()).log(Level.SEVERE, null, ex);
101 } catch (JAXBException ex) {
102 Logger.getLogger(FileUploadController.class.getName()).log(Level.SEVERE, null, ex);