2 Replies Latest reply on Mar 25, 2010 9:15 PM by Efrain Farinango

    I want to do is a copy of DetalleCompra and persist both original and the copy

    Efrain Farinango Newbie




      Hello..... help

      I want to do is a copy of DetalleCompra and persist both original and the copy

      ..................................................................
      rich:modalPanel id="editarItem"
                       xmlns="http://www.w3.org/1999/xhtml"
                       xmlns:s="http://jboss.com/products/seam/taglib"
                       xmlns:ui="http://java.sun.com/jsf/facelets"
                       xmlns:f="http://java.sun.com/jsf/core"
                       xmlns:h="http://java.sun.com/jsf/html"
                       xmlns:rich="http://richfaces.org/rich"
                       xmlns:a4j="http://richfaces.org/a4j" resizeable="true" width="600" height="300">

          <f:facet name="header">
              <h:panelGroup>
                  <h:outputText value="Agregar Catalogo"/>
              </h:panelGroup>
          </f:facet>

          <f:facet name="controls">
              <h:panelGroup>
                  <h:graphicImage value="/img/close.png" id="hidelink"
                                  styleClass="hidelink" />
                  <rich:componentControl for="editarItem" attachTo="hidelink"
                                         operation="hide" event="onclick" />
              </h:panelGroup>
          </f:facet>
              <h:form id="guardar">
              <s:validateAll>

                      <rich:panel >
                      <f:facet name="header">Agregar Item</f:facet>
                      <h:panelGrid columns="3" rowClasses="prop" columnClasses="name,value">
                              <h:outputLabel for="items">cant</h:outputLabel>
                              <h:inputText value="#{detalleCompraHome.instance.dmsCantidad}" id="cant"/>
                                 <h:commandButton value="calcular" action="none" actionListener="#{detalleCompraHome.calculaporcentaje}"/>
                          <h:outputLabel for="itemd">Catalogo</h:outputLabel>
                          <h:inputText value="#{detalleCompraHome.instance.item}" id="itemd" converter="#{itemConverter}" style="text-transform:uppercase" size="30"/>
                          <rich:suggestionbox id="suggestionBoxId" for="itemd"
                                              suggestionAction="#{itemBodegaHome.findItem}" var="result"
                                              fetchValue="#{result.catalogo.eqcCodeqc}" tokens="," height="200" width="300" cellpadding="2"
                                              cellspacing="2" shadowOpacity="4" shadowDepth="4"
                                              minChars="4" rules="none" nothingLabel="No se encontro nada">
                              <h:column>
                                  <h:outputText value="#{result.catalogo.eqcCodeqc}"/>
                              </h:column>
                              <h:column>
                                  <h:outputText value="#{result.catalogo.eqcDescri}" />
                              </h:column>
                          </rich:suggestionbox>
                      </h:panelGrid>
                  </rich:panel>
              </s:validateAll>
              <div class="actionButtons">
                  <h:commandButton value="Guardar" action="#{detalleCompraHome.persist}" rendered="#{!detalleCompraHome.isManaged()}"/>
                  <h:commandButton value="Guardar" action="#{detalleCompraHome.update}" oncomplete = "('editarCatalogo').component.hide()" rendered="#{detalleCompraHome.isManaged()}"/>
                  <h:commandButton value="Regresar" action="cancelar" immediate="true"/>
              </div>
         </h:form>
      </rich:modalPanel>




      public class DetalleCompraHome extends EntityHome<DetalleCompra>
      .
      .
      .
      @Override
          public String persist()
          {

              this.getInstance().setDmsCantidad(this.getInstance().getDmsCantidad());
               this.getInstance().setItem(this.getInstance().getItem());
               System.out.println(this.getInstance().getDmsCantidad());
               System.out.println(this.getInstance().getItem());
                String s = super.persist();
               this.getInstance().setDmsCantidad(this.getInstance().getDmsCantidad());
               this.getInstance().setItem(this.getInstance().getItem());
               System.out.println(this.getInstance().getDmsCantidad());
               System.out.println(this.getInstance().getItem());
                String s1 = super.persist();
              return s+"D";
          }



      I do not have any idea
        • 1. Re: I want to do is a copy of DetalleCompra and persist both original and the copy
          Sergio Samayoa Newbie
          Sorry to be crude but... WHAT THE HELL YOU WANT TO ACCOMPLISH?

          Why do you want to persist a copy of the entity?

          Each entity should have its own id, what is the difference between the "original" and the "copy"?

          getInstance() points to ONE INSTANCE, the "correct" way coud be:

          1. Implement Object.clone() method (look for it in java API docs) which creates a new instance of your entity and copies all properties except the id one.
          2. Your persis() method would be something like:

          public String persist() {
            DetalleCompra clone = getInstance().clone();
            // set whatever the entity id property is
            // unless is auto generated
            clone.setId(...);
            // Persist both
            String s = super.persist();
            getEntityManager().persist(clone);
            return s;
          }

          Of course you should manage properly any exception which could occur while persisting clone entity.

          Regards.
          • 2. Re: I want to do is a copy of DetalleCompra and persist both original and the copy
            Efrain Farinango Newbie
            Hello Sergio
            It's not really a copy
            Let's see if I can explain

            It is a master  (cabeceraCompraHome)
            and detail (detalleCompraHome) application
            I add two details wing head
            I have to do modalpane (editarItem):
            <h:inputText value="#{detalleCompraHome.instance.dmsCantidad}" id="cant"/>
            <h:inputText value="#{detalleCompraHome.instance.dmsPorinv}" id="porinv"/>
            <h:inputText value="#{detalleCompraHome.instance.dmsPocons}" id="porcon"/>
            <h:inputText value="#{detalleCompraHome.instance.dmsPprecio}" id="precio"/>
            <h:inputText value="#{detalleCompraHome.instance.dmsSubtotal}" id="subtotal"/>

            Example fields or attributes
            1……………………..
            cant=100
            %_cons=10
            I%_inver=90
            Precio=10.0
            subtotal=100 $
            2 ……………………….
            cant=100
            %_cons=10
            I%_inver=90
            Precio=10.0
            subtotal=900 $


            looks like a copy, but not

            Sorry about my English, I do not practice

            Regards