Adicionado classificaĆ§Ć£o do log conforme exemplo
[simdecs.git] / src / java / org / ufcspa / simdecs / bn / ui / FileUploadController.java
CommitLineData
c547eea0
MS
1/*
2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
4 */
5package org.ufcspa.simdecs.bn.ui;
6
7import java.io.*;
8import java.util.ArrayList;
9import java.util.logging.Level;
10import java.util.logging.Logger;
11import javax.annotation.Resource;
12import javax.faces.application.FacesMessage;
13import javax.faces.bean.ApplicationScoped;
14import javax.faces.bean.ManagedBean;
15import javax.faces.context.FacesContext;
16import javax.persistence.EntityManager;
17import javax.persistence.EntityManagerFactory;
18import javax.persistence.PersistenceUnit;
19import javax.transaction.UserTransaction;
20import javax.xml.bind.JAXBException;
21import org.primefaces.event.FileUploadEvent;
22import org.primefaces.model.UploadedFile;
23import org.ufcspa.simdecs.bn.entity.BayesianNetwork;
24import org.ufcspa.simdecs.bn.entity.Node;
25import unbbayes.io.exception.LoadException;
26import unbbayes.io.xmlbif.version6.XMLBIFIO;
27import unbbayes.prs.bn.ProbabilisticNetwork;
28
29/**
30 *
31 * @author mchelem
32 */
33@ManagedBean(name = "fileUploadController")
34@ApplicationScoped
35public class FileUploadController implements Serializable {
36 @Resource
37 private UserTransaction utx = null;
38 @PersistenceUnit(unitName = "simdecsEclipseLinkPU")
39 private EntityManagerFactory emf = null;
40 EntityManager em = null;
41
42 private static final Logger logger = Logger.getLogger("" + FileUploadController.class);
43
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());
50 }
51 logger.log(Level.INFO, "Uploaded file saved in: {0}", file.getAbsolutePath());
52 return file;
53 }
54
55 private void persist(BayesianNetwork bayesianNetwork){
56 em = emf.createEntityManager();
57 try {
58 utx.begin();
59 em.persist(bayesianNetwork);
60 utx.commit();
61 } catch (Exception e) {
62 try {
63 utx.rollback();
64 } catch (Exception ex) {
65 Logger.getLogger(FileUploadController.class.getName()).log(Level.SEVERE, null, ex);
66 }
67 Logger.getLogger(FileUploadController.class.getName()).log(Level.INFO, null, e);
68 }
69 logger.log(Level.INFO, "Bayesian Network {0} persisted.", bayesianNetwork.getName());
70
71 }
72
73
74 public void handleFileUpload(FileUploadEvent event) {
75 try {
76 File file = this.saveUploadedFile(event.getFile());
77 // required to run unbbayes gui classes on server
66652603 78 System.setProperty("java.awt.headless", "false");
c547eea0
MS
79
80 ProbabilisticNetwork bn = new ProbabilisticNetwork(null);
81 XMLBIFIO.loadXML(new File(file.getAbsolutePath()), bn);
82 BayesianNetwork bayesianNetwork = new BayesianNetwork();
83 bayesianNetwork.setName(bn.getName());
84
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);
90 nodes.add(node);
91 }
92 bayesianNetwork.setNodes(nodes);
93 persist(bayesianNetwork);
94
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);
103 }
104 }
105}
106