1 Reply Latest reply on Jul 27, 2004 11:51 AM by millerm1

    Please help - CMR field is not populating

    soubhratra

      Hi all,
      I am facing a problem regarding CMR fields in my CMP ejbs.

      I have 2 CMPs.
      1. user
      2.Invitation

      Invitation CMP is realted to User CMP in 2 ways.

      User (sender) >> Invitation 1 - 1 mapping
      Invitation >> User (receiver) 1 - 1 mapping

      so my invitaion table contains two fields - sender and receiver.

      I have a session facade -InvitationManager (IM) - which has a method sendInvitation();
      This method takes Invitation DTO as parameter from client.
      Invitaion DTo has two member variables - one is sender and another is a collection of emails of intended receivers of the invitation.

      The method takes each receiver from the collection and calls another method (createInvitationEJB) iteratively. For each iterative call to createInvitationEJB() method one Invitation CMP is being created.


      problem. -- Suppose there are 3 receivers.
      So createInvitationEJB() method will be called thrice. in invitation table in the database the sender field is populating only in the last iteration. the previous iterations setting this field to null.

      If there is only one receiver, then it is populating correctly.

      I am sending the code also. Please suggest me if u find any clue.

      -------------
      code
      ------------

      public void sendInvitation(InvitationDTO invite)
      throws BackendException,NoSuchInvitationException,NoSuchUserException,IllegalArgumentException
      {
      UserLocal senderLocal = null;
      UserLocal receiverLocal = null;
      String invitationType = null;
      if(null == invite)
      {
      throw new IllegalArgumentException("The argument InvitationDTO is null");
      }
      UserDTO senderDTO = invite.getFromUser();
      System.out.println("Sender name" + senderDTO.getFirstName() + " email : " + senderDTO.getEmail());
      try
      {
      senderLocal = getUserLocalHome().findByPrimaryKey(senderDTO.getID());
      }
      catch(FinderException fe)
      {
      throw new NoSuchUserException("unable to find the sender", fe);
      }
      Collection receiverEmailList = invite.getToUsers();

      if(receiverEmailList != null)
      {
      Iterator iterator = receiverEmailList.iterator();
      while(iterator.hasNext())
      {
      // create a invitation from each email and send a email to each
      String email = (String) iterator.next();
      //construct UserLocal (receiver) from email
      try
      {
      receiverLocal = getUserLocalHome().findByEmailAddress(email);
      invitationType = "internal";
      }
      catch(FinderException fe)
      {
      try
      {
      receiverLocal = getUserLocalHome().create(PrimaryKeyGenerator.generatePrimaryKey());
      invitationType = "external";
      }
      catch(CreateException ce)
      {
      throw new BackendException("can not create UserLocal", ce);
      }
      }
      InvitationLocal invitationLocal = createInvitationEJB(senderLocal, receiverLocal, invite); UserLocal user1 = invitationLocal.getSender();
      System.out.println("in main sender is : " + user1.getPrimaryKey().toString());
      sendInvitationMail(senderLocal, receiverLocal, invitationLocal, invitationType);
      }
      }
      }


      private InvitationLocal createInvitationEJB(UserLocal senderLocal, UserLocal receiverLocal, InvitationDTO invite) throws BackendException
      {
      try
      {
      invitationLocal = getInvitationLocalHome().create(PrimaryKeyGenerator.generatePrimaryKey());
      }
      catch(CreateException ce)
      {
      throw new BackendException("Unable to create Invitation with this id", ce);
      }

      // 2) copy all the detail from the DTO to the EJB
      if(invite.getStatus() == InvitationDTO.ACCEPTED)
      {
      invitationLocal.setAccepted(new Boolean(true));
      }
      else
      {
      invitationLocal.setAccepted(new Boolean(false));
      }
      System.out.println("argument Sender : " + senderLocal.getPrimaryKey().toString());
      invitationLocal.setSender(senderLocal); invitationLocal.setReceiver(receiverLocal);
      invitationLocal.setReadStatus(new Boolean(invite.isRead()));
      // invitationLocal.setRemoved(); unable to determine the source to get this data
      invitationLocal.setSubject(invite.getSubject());
      invitationLocal.setBody(invite.getBody());
      invitationLocal.setCreateDate(invite.getDate());
      UserLocal user1 = invitationLocal.getSender();
      System.out.println("getting sender from the invitation : " + user1.getPrimaryKey().toString());
      return invitationLocal;
      }

        • 1. Re: Please help - CMR field is not populating

          I think your problem is that you defined the relations to be 1-1 which means that each user can only be the sender of one invitation and each user can only be the recevier of one invitation. I think you want:
          User (sender) >> Invitation 1 - n mapping
          Invitation >> User (receiver) n - 1 mapping

          Hope this helps,
          Mark