0

我正在使用带有 jdk 版本 6 和 primefaces 3.4.1 的 netbeans 7.2.1。我已经创建了一个数据库,并从数据库中创建了实体,然后从实体中创建了 JSF 页面。

我现在有一个 jsf 托管 bean,它通过从数据库中提取用户并根据他们的用户名是否包含在某个字符串中来连接它们来为程序创建一个思维导图。

思维导图中有一个双击工具,我想用它来更新数据库,但由于某种原因它不起作用。我没有收到错误消息,只是没有任何变化。

这是我试图用来实现更新功能的:

package jpa.session;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import jpa.entities.Enginetable;


@Stateless
public class EnginetableFacade extends AbstractFacade<Enginetable> {
    @PersistenceContext(unitName = "Networks0.75PU")
    private EntityManager em;


    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public EnginetableFacade() {
        super(Enginetable.class);
    }        

    public Enginetable setUserdetails( Enginetable user ) {
        try 
        {        
            return em.merge(user); 
        } 

        catch (NoResultException nre) 
        {
            return null;
        }
    }

}

But it doesn't work. Note that I have no problem accessing the database but haven't included the code here.

This is the relevant method:

    public void onNodeDblselect(SelectEvent event) {
        this.user.setConnections( this.user.getConnections() + "-" + this.selectedNode.getLabel() );
        this.selectedNode = (MindmapNode) event.getObject();
        this.ejbFacade.setUserdetails( this.user );
    }

This is the call from the xhtml

    <p:mindmap value="#{mmBean.root}" style="width:100%;height:600px">
        <p:ajax event="dblselect" listener="#{mmBean.onNodeDblselect}" update="output" />
    </p:mindmap>    

Please, any advice would be fantastic.

This is the full code for the bean:

package jsf;

import java.io.Serializable;
import java.sql.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ViewScoped;
import javax.inject.Named;
import javax.persistence.Version;
import jpa.entities.Enginetable;
import jpa.session.EnginetableFacade;
import org.primefaces.event.SelectEvent;
import org.primefaces.model.mindmap.DefaultMindmapNode;
import org.primefaces.model.mindmap.MindmapNode;

/**
 *
 * @author AOL
 */
@Named(value = "mmBean")
@ViewScoped
public class mmBean implements Serializable{

    @EJB
    private jpa.session.EnginetableFacade ejbFacade;
    private MindmapNode root;

    private MindmapNode selectedNode;

    PreparedStatement pst = null;

    private List<Enginetable> elements;
    private List<Enginetable> filteredElements;
    private List<Enginetable> userlist;


    private Enginetable selectedElement;
    private Enginetable[] selectedElements; 



    private Enginetable current;

    private Enginetable nodeentity = new Enginetable();
    private Enginetable user = new Enginetable();
    private String connections;
    private String[] strConnections;
    private String[] userConn;
    private List<String> indirectConn = new ArrayList<String>();
    private String[]  iConn;

    private Object rootlabel;

    //ADDED BY AODHÁN
    Connection con = null;
    Statement st = null;
    ResultSet rs = null;

    String url = "jdbc:mysql://localhost:8080/networkdb";
    String theuser = "Aodhan";
    String password = "Cavelcade1988";


    /**
     * Creates a new instance of MmBean
     */
    public mmBean() {
    }

    @PostConstruct
    public void init() {

        this.userlist = this.getFullList();
        Enginetable entity;

        //remoteuser=null;

        // filteredElements = new ArrayList<Enginetable>();
        this.user = this.userlist.get(0);

        this.userConn = this.user.getConnections().split("-");

        root = new DefaultMindmapNode(user.getStartnode(), "Google", "006666", false);


        this.rootlabel = this.root.getLabel();
        for( int i = 0 ; i < this.userlist.size() ; i++){

                    entity = this.userlist.get(i);

                    if ( !rootlabel.equals(entity.getStartnode() ) && Arrays.asList(this.userConn).contains( entity.getStartnode() ) ) {
                        connections = entity.getConnections();
                        iConn = connections.split("-");
                        boolean addAll;
                        addAll = this.indirectConn.addAll(Arrays.asList(iConn));
                    }

        }

        for( int i = 0 ; i < this.userlist.size() ; i++){

                    entity = this.userlist.get(i);

                    if ( !rootlabel.equals(entity.getStartnode() ) ) {


                if ( Arrays.asList(this.userConn).contains( entity.getStartnode() ) )  { 

                                root.addNode(new DefaultMindmapNode(entity.getStartnode() , "Malicious Software: ", "33FF00"));

                            } 



                                else if ( !Arrays.asList(this.userConn).contains( entity.getStartnode() ) && this.indirectConn.contains( entity.getStartnode() ) ){

                                    root.addNode(new DefaultMindmapNode(entity.getStartnode() , "Malicious Software: ", "00FFFF"));

                                } 


                else{

                                    root.addNode(new DefaultMindmapNode( entity.getStartnode() , "Malicious Software: ", "CC0000"));

                }

            }

        }

    }

    public MindmapNode getRoot() {
        return root;
    }

    public MindmapNode getSelectedNode() {
        return selectedNode;
    }
    public void setSelectedNode(MindmapNode selectedNode) {
        this.selectedNode = selectedNode;
    }

    public void onNodeSelect(SelectEvent event) {
        MindmapNode node = (MindmapNode) event.getObject();


            for( int i = 0 ; i < this.userlist.size() ; i++){
                    Enginetable entity;
                    Object label = node.getLabel();

                    entity = userlist.get(i);
                    if ( entity.getStartnode() == label ){
                        this.nodeentity = entity;
                    }

            }
    //load children of select node and add via node.addNode(childNode);
    //populate if not already loaded
            if(node.getChildren().isEmpty()) {
               this.setChildNodes( node ); 
            }
        }


    public void onNodeDblselect(SelectEvent event) {
        this.user.setConnections( this.user.getConnections() + "-" + this.selectedNode.getLabel() );
        this.selectedNode = (MindmapNode) event.getObject();
        this.ejbFacade.setUserdetails( this.user );
    }

    public void setUserlist(List<Enginetable> elements) {
        this.userlist = elements;
    }

    public List<Enginetable> getUserlist() {
        return this.userlist;
    }

    public void setUser(Enginetable element) {
        this.user = element;
    }

    public Enginetable getUser() {
        return this.user;
    }

    public void setFilteredElements(List<Enginetable> filteredElements) {
        //this.filteredElements = filteredElements;
        this.elements=filteredElements;
    }

    public List<Enginetable> getFilteredElements() {
        return filteredElements;
    }

    public void setSelectedElements(Enginetable[] selectedElements) {
        this.selectedElements = selectedElements;
    }

    public Enginetable[] getSelectedElements() {
        return selectedElements;
    }

    public void setSelectedElement(Enginetable selectedElement) {
        this.selectedElement = selectedElement;
    }

    public Enginetable getSelectedElement() {
        return selectedElement;
    }

    private EnginetableFacade getFacade() {
        return ejbFacade;
    }  

    // ADDED BY AODHÁN

    public List<Enginetable> getListByUsername(String user){
       return this.ejbFacade.getEnginetableByUsername(user);
    } 

    public List<Enginetable> getListByUserID( int id ){
       return this.ejbFacade.getEnginetableByUserid(id);
    } 

    public List<Enginetable> getFullList(){
       return this.ejbFacade.getFullEnginetable();
    } 

    public Enginetable setUserdetails( Enginetable user ){
       return this.ejbFacade.setUserdetails( user );
    } 

    public void setChildNodes( MindmapNode node ){

        Object label = node.getLabel();
        this.rootlabel = this.root.getLabel();
        this.strConnections = nodeentity.getConnections().split("-");

        for( int i = 0 ; i < this.userlist.size() ; i++){

            Enginetable entity;

            entity = this.userlist.get(i);


            if ( !label.equals(entity.getStartnode() ) && !this.rootlabel.equals( entity.getStartnode() ) ) {

                if ( Arrays.asList(this.userConn).contains( entity.getStartnode() ) && !Arrays.asList(this.strConnections).contains( entity.getStartnode() ) ){

                    node.addNode(new DefaultMindmapNode(entity.getStartnode() , "Malicious Software: ", "33FF00"));

                }   

                else if ( !Arrays.asList(this.userConn).contains( entity.getStartnode() ) && this.indirectConn.contains( entity.getStartnode() ) ){

                    node.addNode(new DefaultMindmapNode(entity.getStartnode() , "Malicious Software: ", "00FFFF"));

                } 

                else if ( Arrays.asList(this.userConn).contains( entity.getStartnode() ) && Arrays.asList(this.strConnections).contains( entity.getStartnode() ) ){

                    node.addNode(new DefaultMindmapNode(entity.getStartnode() , "Malicious Software: ", "0066FF"));

                }

        else if ( !Arrays.asList(this.userConn).contains( entity.getStartnode() ) && Arrays.asList(this.strConnections).contains( entity.getStartnode() ) ){

                    node.addNode(new DefaultMindmapNode(entity.getStartnode() , "Malicious Software: ", "FFCC00"));

                } 

                else if ( !Arrays.asList(this.userConn).contains( entity.getStartnode() ) && !Arrays.asList(this.strConnections).contains( entity.getStartnode() ) ){

                    node.addNode(new DefaultMindmapNode(entity.getStartnode() , "Malicious Software: ", "CC0000"));

                } 

            }

        }
    }
}
4

1 回答 1

0

I realised what the problem was, and it was twofold, it seems.

  public void onNodeDblselect(SelectEvent event) {
        this.user.setConnections( this.user.getConnections() + "-" + this.selectedNode.getLabel() );
        this.selectedNode = (MindmapNode) event.getObject();
        this.ejbFacade.setUserdetails( this.user );
    }

I was calling this.selectedNode before it was defined, here.

public Enginetable setUserdetails( Enginetable user ) {
    try 
    {        
        return em.merge(user); 
    }  

    catch (NoResultException nre) 
    {
        return null;
    }
}

It doesn't like this return statement, so I broke it up.

public Enginetable setUserdetails( Enginetable user ) {
    try 
    {        
        em.merge(user); 
        em.flush();
        return user;
    } 

    catch (NoResultException nre) 
    {
        return null;
    }
}

I will have to make the page update, but that is a html problem.

于 2012-11-24T14:14:39.267 回答