保存到数据库后如何在dataTable中显示记录?

时间:2018-09-13 17:44:39

标签: ajax jsf primefaces

在SQL Server中,我有一个带有一些表的架构(ade),其中两个表是:

picture of table Demandas and table Anotacoes

我正在使用Eclipselink ORM和Primefaces 6.2;我两个表都有CRUD,效果很好。

ListDemandas.jsf 中,我有一个数据表,用户可以在其中单击按钮来插入新的Demandas注册表。然后,出现一个对话框(位于 显示 CreateDemandas.jsf )以在某些字段中输入数据。单击保存按钮后,将隐藏该对话框并更新数据表,以显示此新的Demandas记录。使用 ListAnotacoes.jsf CreateAnotacoes.jsf 来对Anotacoes实体执行相同的操作。

我想要的是通过 ListDemandas.jsf 页插入Anotacoes注册表,并且在保存了新的Anotacoes注册表之后,它将立即显示在Demandas数据表中(在 ListDemandas.jsf 页中。

到目前为止,我实现了什么:我在 ListDemandas.jsf 中放置了一个按钮以调用对话框(位于 CreateAnotacoes.jsf 中)。在此类对话框中单击保存按钮后,该(Anotacoes)对话框将被隐藏,并且数据已正确保存在数据库中。

问题在于,此新的Anotacoes记录没有显示在Demandas数据表中,甚至没有如下更新:

<p:commandButton actionListener="#{anotacoesController.saveNew}" value="Save" update="display,:DemandasListForm:datalist,:DemandasListForm:DemandasDataTableContextMenu,:growl" oncomplete="handleSubmit(xhr,status,args,PF('AnotacaoCreateDialog'));" >

ListDemandas.jsf:

<h:form id="DemandasListForm">

    <p:panel id="PanelListForm" header="#{adeBundle.ListDemandasTitle}">

        <p:commandButton id="createButton"         value="Create Demanda"  update=":DemandasCreateForm" oncomplete="PF('DemandasCreateDialog').show()" actionListener="#{demandasController.prepareCreate}" />
        <p:commandButton id="createAnotacaoButton" value="Create Anotacao" update=":AnotacaoCreateForm, datalist" oncomplete="PF('AnotacaoCreateDialog').show()" />

        <p:dataTable id="datalist"
                     value="#{demandasController.items}"
                     lazy="false"
                     rowKey="#{item.id}"
                     var="item"
                     selection="#{demandasController.selected}"
                     widgetVar="demandasTable">

            <p:ajax event="rowSelect"   update="@form:createAnotacaoButton, @form:createButton" listener="#{demandasController.resetParents}"/>
            <p:ajax event="rowUnselect" update="@form:createAnotacaoButton, @form:createButton" listener="#{demandasController.resetParents}"/>
            <p:ajax event="contextMenu" update="@form:createAnotacaoButton, @form:createButton" listener="#{demandasController.resetParents}"/>

            <p:column id="idCod" >
                <f:facet name="header">
                    <h:outputText value="id"/>
                </f:facet>
                <h:outputText value="#{item.id}"/>
            </p:column>

            <!-- some columns ommited -->    

            <p:rowExpansion>
                <p:panelGrid>
                    <p:column>
                        <p:row>
                            <p:accordionPanel value="Mais informacoes da Demanda" multiple="true">
                                <p:tab title="Anotacoes}" >
                                    <p:dataList value="#{item.anotacoesCollection}" var="anotacoesCollectionItem" itemType="none" emptyMessage="-" >
                                        <h:outputText value="#{anotacoesCollectionItem.anotacao.toString()}" />
                                    </p:dataList>
                                </p:tab>
                            </p:accordionPanel>
                        </p:row>
                    </p:column>
                </p:panelGrid>
            </p:rowExpansion>
        </p:dataTable>
    </p:panel>
    <ui:include src="/WEB-INF/include/confirmation.xhtml"/>    
</h:form>

CreateAnotacao.jsf:

    <p:dialog id="AnotacaoCreateDlg" widgetVar="AnotacaoCreateDialog" modal="true" appendTo="@(body)" header="Create Anotacoes" >

    <h:form id="AnotacaoCreateForm" >

        <h:panelGroup id="display" >
            <p:panelGrid columns="2" columnClasses="column">

                <p:outputLabel value="CreateAnotacoesLabel_anotacao" for="anotacao" />
                <h:panelGroup>
                    <p:inputTextarea id="anotacao" value="#{anotacoesController.selected.anotacao}" title="CreateAnotacoesTitle_anotacao" />
                </h:panelGroup>

                <p:outputLabel value="CreateAnotacoesLabel_date" for="date" />
                <h:panelGroup>
                    <p:calendar id="date" value="#{demandasController.currentDate}" title="CreateAnotacoesTitle_date" />
                </h:panelGroup>

        <!-- some other input fields ommited -->                

            </p:panelGrid>
        </h:panelGroup>

        <p:commandButton actionListener="#{anotacoesController.saveNew}" value="Save" update="display, :DemandasListForm:datalist, :growl" oncomplete="handleSubmit(xhr,status,args,PF('AnotacaoCreateDialog'));" >
            <p:confirm header="Confirmation" message="Are you sure ?" />
        </p:commandButton>

        <p:commandButton value="Cancel" oncomplete="PF('AnotacaoCreateDialog').hide()" update="display" process="@this" immediate="true" resetValues="true"/>

    </h:form>
</p:dialog>

AbstractController.java类的摘录:

public abstract class AbstractController<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    private Class<T> itemClass;
    private T selected;
    private Collection<T> items;
    private List<T> filteredItems;

    private enum PersistAction {

        CREATE,
        DELETE,
        UPDATE
    }

    public AbstractController() {
    }

    public AbstractController(Class<T> itemClass) {
        this.itemClass = itemClass;
    }

    public void setSelected(T selected) {
        if (selected != null) {
            if (this.selected == null || !this.selected.equals(selected)) {
                this.selected = this.ejbFacade.findWithParents(selected);
                this.setChildrenEmptyFlags();
            }
        } else {
            this.selected = null;
        }
    }

    protected void setEmbeddableKeys() {
    }

    protected void initializeEmbeddableKey() {
    }

    public Collection<T> getItems() {
        if (itemClass.getSimpleName().equals("Demandas")) {
            if (sessionPrefixo == 9600) {
                items = this.ejbFacade.findAll();
            } else {
                items = this.ejbFacade.findAllVenceHojeByNomeUorPos(uorPosDiageJurisdiciona);
            }
        }
        return items;
    }

    public void save(ActionEvent event) {
        String msg = ResourceBundle.getBundle("/adeBundle").getString(itemClass.getSimpleName() + "Updated");
        persist(PersistAction.UPDATE, msg);

        if (!isValidationFailed()) {
            // Update the existing entity inside the item list
            List<T> itemList = refreshItem(this.selected, this.items);

            // If the original list has changed (it is a new object)
            if (this.items != itemList) {
                this.setItems(itemList);
            }
            // Also refresh the filteredItems list in case the user has filtered the DataTable
            if (filteredItems != null) {
                refreshItem(this.selected, this.filteredItems);
            }
        }
    }

    public void saveNew(ActionEvent event) {
        String msg = ResourceBundle.getBundle("/adeBundle").getString(itemClass.getSimpleName() + "Created");
        persist(PersistAction.CREATE, msg);

        if (!isValidationFailed()) {
            items = null; // Invalidate list of items to trigger re-query.
            lazyItems = null; // Invalidate list of lazy items to trigger re-query.
        }
    }

    private void persist(PersistAction persistAction, String successMessage) {
        if (selected != null) {
            this.setEmbeddableKeys();
            try {
                if (persistAction != PersistAction.DELETE) {
                    this.ejbFacade.edit(selected);
                } else {
                    this.ejbFacade.remove(selected);
                }
                this.setChildrenEmptyFlags();
                JsfUtil.addSuccessMessage(successMessage);
            } catch (EJBException ex) {
                Throwable cause = JsfUtil.getRootCause(ex.getCause());
                if (cause != null) {
                    if (cause instanceof ConstraintViolationException) {
                        ConstraintViolationException excp = (ConstraintViolationException) cause;
                        for (ConstraintViolation s : excp.getConstraintViolations()) {
                            JsfUtil.addErrorMessage(s.getMessage());
                        }
                    } else {
                        String msg = cause.getLocalizedMessage();
                        if (msg.length() > 0) {
                            JsfUtil.addErrorMessage(msg);
                        } else {
                            JsfUtil.addErrorMessage(ex, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
                        }
                    }
                }
            } catch (Exception ex) {
                Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
                JsfUtil.addErrorMessage(ex, ResourceBundle.getBundle("/adeBundle").getString("PersistenceErrorOccured"));
            }
        }
    }

    public T prepareCreate(ActionEvent event) {
        T newItem;
        try {
            newItem = itemClass.newInstance();
            this.selected = newItem;
            initializeEmbeddableKey();
            return newItem;
        } catch (InstantiationException | IllegalAccessException ex) {
            Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public boolean isValidationFailed() {
        return JsfUtil.isValidationFailed();
    }

    public String getComponentMessages(String clientComponent, String defaultMessage) {
        return JsfUtil.getComponentMessages(clientComponent, defaultMessage);
    }

    @PostConstruct
    public void initParams() {
        Object paramItems = FacesContext.getCurrentInstance().getExternalContext().getRequestMap().get(itemClass.getSimpleName() + "_items");
        if (paramItems != null) {
            setItems((Collection<T>) paramItems);
        }
    }

    private List<T> refreshItem(T item, Collection<T> items) {

        List<T> itemList;
        if (this.items instanceof List) {
            itemList = (List<T>) items;
        } else {
            itemList = new ArrayList<>(items);
        }
        int i = itemList.indexOf(item);
        if (i >= 0) {
            try {
                itemList.set(i, item);
            } catch (UnsupportedOperationException ex) {
                return refreshItem(item, new ArrayList<>(items));
            }
        }
        return itemList;
    }

    public AbstractFacade<T> getEjbFacade() {
        return ejbFacade;
    }

    public void setEjbFacade(AbstractFacade<T> ejbFacade) {
        this.ejbFacade = ejbFacade;
    }
}

AbstractFacade.java类的摘录:

[...]
public void edit(T entity) {
    getEntityManager().merge(entity);
}
[...]

如评论中所述,这是Demandas表的参考:

image for Demandas datatable id reference

请,有人可以帮我解决吗?

0 个答案:

没有答案