2 Replies Latest reply on Jan 25, 2010 1:44 AM by Hunter Stern

    how to do multiple form submits that create (not modify) new objects every time

    Hunter Stern Newbie
      I have a typical data entry form.  I would like the form to create a new object I can persist every time I click submit.  Currently it seems that I can only modify the object that I have just created.  Is there a way to do this.  My code is below.

      //bean
      |@Entity
      @Table(name = "dream", catalog = "dream")
      @Name("dream")
      public class Dream implements java.io.Serializable {

           private Integer dreamId;
           private User user;
              private Set<DreamAttribute> tempDreamAttributes = new HashSet<DreamAttribute>(0);
           private Set<DreamAttribute> dreamAttributes = new HashSet<DreamAttribute>(0);

              private List<String>attributesForRegister =
                      new ArrayList<String>(0);
              private transient int attributeCounter = 0;


           public Dream() {
           }

           public Dream(User user) {
                this.user = user;
           }
           public Dream(User user, Set<DreamAttribute> dreamAttributes) {
                this.user = user;
                this.dreamAttributes = dreamAttributes;
           }

           @Id
           @GeneratedValue(strategy = IDENTITY)
           @Column(name = "dream_id", unique = true, nullable = false)
           public Integer getDreamId() {
                return this.dreamId;
           }

           public void setDreamId(Integer dreamId) {
                this.dreamId = dreamId;
           }

           @ManyToOne(fetch = FetchType.LAZY)
           @JoinColumn(name = "user_id", nullable = false)
           @NotNull
           public User getUser() {
                return this.user;
           }

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

           @OneToMany(fetch = FetchType.LAZY, mappedBy = "dream", cascade = {CascadeType.ALL})
           public Set<DreamAttribute> getDreamAttributes() {
                return this.dreamAttributes;
           }

           public void setDreamAttributes(Set<DreamAttribute> dreamAttributes) {
                this.dreamAttributes = dreamAttributes;
           }

              public void putAttributeId(String attributeId) {



                  System.out.println("in putatttr id!!!");
                  boolean alreadyExists = false;

                  for (String attId : attributesForRegister) {
                      if (attId.equals(attributeId)) {
                          alreadyExists = true;
                          break;
                      }
                  }


                  if (!alreadyExists) attributesForRegister.add(attributeId);
              }

              public boolean attributeAlreadyExistsAndUpdate(String attributeId, String value) {

                  System.out.println("attributeid: " + attributeId);
                  System.out.println("value : " + value);

                  for (DreamAttribute ua : dreamAttributes) {
                      if (ua.getDreamAttributeLookup().getAttributeId().intValue() ==
                              Integer.valueOf(attributeId)) {

                                  System.out.println("found attribute for :" + value);
                          ua.setValue(value);

                          return true;
                      }

                  }

                  return false;
              }

              @Transient
              public String getValue() {
                  return null;
              }

              @Transient
              public void setValue(String value) {

                  System.out.println("in setValue!!!!!");

                  if (attributeAlreadyExistsAndUpdate(attributesForRegister.get(attributeCounter), value)) {

                      ++attributeCounter;
                      System.out.println("attribute already existws~!!!!!!!");
                      return;
                  }


                  /*
                  System.out.println("dAREAWREAERAEERA:"+attributesForRegister.get(attributeCounter));
                  System.out.println("dAREAWREAERAEERA:"+ this.getDreamId());

                   *
                   */
                 
                  DreamAttribute u = new DreamAttribute();
                  u.setDream(this);

                 
                  u.setId(new DreamAttributeId(
                          new Integer(0),//temporary value
                          Integer.valueOf(attributesForRegister.get(attributeCounter))));

                  DreamAttributeLookup uu = new DreamAttributeLookup();
                  uu.setAttributeId(Integer.valueOf(attributesForRegister.get(attributeCounter)));

                  u.setDreamAttributeLookup(uu);
                  u.setValue(value);


                  System.out.println("dream attribute aDD!!!!!value: " + value);

                  //UserAttributeLookup is in incomplete state!!!!!!!
                  //must load it from db at some point
                  tempDreamAttributes.add(u);
                  ++attributeCounter;

              }

              public void resetAttributeIds() {
                  attributesForRegister.clear();
                  attributeCounter = 0;
              }

              @Transient
              public void setDreamAttributeDreamIds(Integer dreamId) {

                  for (DreamAttribute da : tempDreamAttributes) {

                      da.getId().setDreamId(dreamId);
                  }

                  dreamAttributes.addAll(tempDreamAttributes);
              }

              @Transient
              public String getDreamAttributeValueByName(String name) {

                  for (DreamAttribute d : getDreamAttributes()) {

       
                      if (d.getDreamAttributeLookup().getAttributeName().equals(name)) {
                          return d.getValue();
                      }
                  }

                  return null;
              }


              /*
              @Override
              public Object clone() throws CloneNotSupportedException {

                  return super.clone();

              }
               *
               */
        
      }

      My action method:

      public boolean save()
          {
             

              System.out.println("dream id: is " + dream.getDreamId());
              //check to see if same name
              if (dreamNameExists(user, dream.getDreamAttributeValueByName("name"))) {

                  statusMessages.add(
                          "You already had a dream named " +
                          dream.getDreamAttributeValueByName("name"));

                  dream.resetAttributeIds();

                  return true;
              }


              //System.out.println("in svae!");
              dream.resetAttributeIds();


              /*
              DreamUser dreamUser = new DreamUser(
                      dream, user);
               *
               */

              dream.setUser(user);


              user.getDreams().add(dream);

              entityManager.persist(dream);

              dream.setDreamAttributeDreamIds(dream.getDreamId());
              loadDreamAttributeLookupsIntoDream(dream);

              entityManager.flush();
             

              statusMessages.add("You successfully entered your dream!");

              return true;

          }

      My xhtml form:

          <h:form id="dreamEntryForm" styleClass="edit">
          <h1>Enter Your Dreams #{user.getUserAttributeValueByName("First Name")}!</h1>






      <h:dataTable var="_da" value="#{dreamAttributeLookupList.resultList}">

      <h:column>




      <s:decorate id="attributeValue" template="layout/edit.xhtml">
      <ui:define name="label">#{_da.attributeName}</ui:define>

      <hs:dreamLoopingInputText required="true" id="DynamicAttribute" rendered="#{_da.attributeType.attributeTypeId == 1}" value="#{dream.value}" validator="#{dream.putAttributeId(_da.attributeId)}" />

      <h:inputTextarea required="true" id="DynamicAttribute2" value="#{dream.value}" rendered="#{_da.attributeType.attributeTypeId == 4}" validator="#{dream.putAttributeId(_da.attributeId)}" />

      </s:decorate>



      </h:column>

      </h:dataTable>

         
          <h:commandButton id="enter" value="save your dream!"
                                     action="#{dreamSaver.save}"/>



          </h:form>






      ``

      |