1 Reply Latest reply on Jul 22, 2011 1:50 PM by ruthlesset

    Exception inserting or deleteing a new record

    ruthlesset
      Setup SEAM 2.2.1 & jboss 6.0.0

      I have a Department that is assigned to different Roles. Each role can have 3 different Actions.
      There are 3 actions: E (edit), V (view), H (hide). Also each role will have a default Action which is indicated by default_ind which will have the value 'Y'.

      The 3 tables are as follows

      M_Dept (recid Number(5), name VarChar2(50))

      M_Role (recid Number(5), dept_recid NUMBER(5), name VarChar2(50))

      Action (recid Number(5), role_recid NUMBER(5), action VarChar2(1), default_ind VarChar2(1))


      I am able to update the Dept and Role without any issue. The issue I currently have is adding a new record or deleting an existing record into the Action table.

      The page role_edit.xhtml allows the user to edit the roles as well as add or remove any action associated with it. There are 3 checkboxes for each Action. If a checkbox is checked then a new record should be created in the Action table for that role. If a checkbox is unchecked, then that record should be deleted from the Action table. I have the partial code below:

      I have no issues changing the existing default actions or the role names, but when I try to Add or Remove an existing Action I get an exception. Below is the exception thrown when I try to add a Action.

      Exception during request processing:
      Caused by javax.servlet.ServletException with message: "javax.el.ELException: /administration/role_edit.xhtml @133,76 value="#{_role.actionEdit}": java.lang.NullPointerException"
      Caused by javax.faces.component.UpdateModelException with message: "javax.el.ELException: /administration/role_edit.xhtml @133,76 value="#{_role.actionEdit}": java.lang.NullPointerException"
      Caused by javax.el.ELException with message: "/administration/role_edit.xhtml @133,76 value="#{_role.actionEdit}": java.lang.NullPointerException"
      Caused by java.lang.NullPointerException with message: ""

      ------------------------------------------------------------------------------------
      role_edit.xhtml:

      <tbody>
      <ui:repeat value="#{mDeptHome.instance.MRoleList}" var="_role">
      <tr>
      <td>
           <h:inputText id="roleName" required="true" size="40" maxlength="50" value="#{_role.name}" styleClass="w50">
                <a:support event="onblur" reRender="roleNameField" bypassUpdates="true" ajaxSingle="true"/>
           </h:inputText>
      </td>
      <td>
      <fieldset id="action1_1" class="chkLst"><a href="#" title="Available Actions">All Items</a><dl>
      <dd>
      <h:outputLabel for="action1_1_1">
      <h:selectBooleanCheckbox id="action1_1_1" value="#{_role.actionView}"></h:selectBooleanCheckbox> View</h:outputLabel>
      </dd>
      <dd>
      <h:outputLabel for="action1_1_2">
      <h:selectBooleanCheckbox id="action1_1_2" value="#{_role.actionEdit}" /> Edit</h:outputLabel>
      </dd>
      <dd>
      <h:outputLabel for="action1_1_3">
      <h:selectBooleanCheckbox id="action1_1_3" value="#{_role.actionHide}" /> Hide</h:outputLabel>
      </dd>
      </dl></fieldset><!-- .chkLst -->
      </td>
      <td>
           <h:selectOneMenu id="defaultAction" required="false" value="#{_role.defaultAction}">
                <s:selectItems var="_item" value="#{_context.getValidStatusList()}" itemValue="#{_item.action}" label="#{_item.getActionLabel()}"/>
           </h:selectOneMenu>
      </td>
      </tr>
      </ui:repeat>
      </tbody>

      ------------------------------------------------------------------------------------

      The classes under the model directory below:

      -----------------------------------------------------------------
      model\MDept.java

      package com.domain.cco.model;

      import java.util.ArrayList;
      import java.util.Date;
      import java.util.HashSet;
      import java.util.Iterator;
      import java.util.List;
      import java.util.Set;

      import javax.persistence.Column;
      import javax.persistence.Entity;
      import javax.persistence.EntityManager;
      import javax.persistence.FetchType;
      import javax.persistence.GeneratedValue;
      import javax.persistence.GenerationType;
      import javax.persistence.Id;
      import javax.persistence.JoinColumn;
      import javax.persistence.ManyToOne;
      import javax.persistence.OneToMany;
      import javax.persistence.SequenceGenerator;
      import javax.persistence.Table;
      import javax.persistence.Temporal;
      import javax.persistence.TemporalType;
      import javax.persistence.Transient;

      import org.hibernate.validator.Length;
      import org.hibernate.validator.NotNull;
      import org.jboss.seam.Component;
      import org.jboss.seam.annotations.Transactional;

      @Entity
      @Table(name = "M_Dept")
      @SequenceGenerator(name = "deptSeq", sequenceName = "M_DEPT_SEQ")
      public class MDept implements java.io.Serializable {

           private static final long serialVersionUID = 1L;
           private Integer recid;
           private String name;
           private Set<MRole> roles = new HashSet<MRole>(0);

           public MDept() {
           }

           public MDept(Integer recid, String name) {
                this.recid = recid;
                this.name = name;
           }

           public MDept(Integer recid, String name, Set<MRole> roles) {
                this.recid = recid;
                this.name = name;
                this.roles = roles;
           }

           @Id
           @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "deptSeq")
           @Column(name = "RECID", unique = true, nullable = false, precision = 5, scale = 0)
           public Integer getRecid() {
                return this.recid;
           }

           public void setRecid(Integer recid) {
                this.recid = recid;
           }

           @Column(name = "NAME", nullable = false, length = 50)
           @NotNull
           @Length(max = 50)
           public String getName() {
                return this.name;
           }

           public void setName(String name) {
                this.name = name;
           }

           @OneToMany(fetch = FetchType.LAZY, mappedBy = "MDept")
           public Set<MRole> getRoles() {
                return this.roles;
           }

           public void setRoles(Set<MRole> roles) {
                this.roles = roles;
           }

      }
      ------------------------------------------------------------------------------------------------------
      model\MRole.java

      package com.domain.cco.model;

      import java.util.ArrayList;
      import java.util.Date;
      import java.util.HashSet;
      import java.util.Iterator;
      import java.util.List;
      import java.util.Set;

      import javax.persistence.Column;
      import javax.persistence.Entity;
      import javax.persistence.EntityManager;
      import javax.persistence.FetchType;
      import javax.persistence.GeneratedValue;
      import javax.persistence.GenerationType;
      import javax.persistence.Id;
      import javax.persistence.JoinColumn;
      import javax.persistence.ManyToOne;
      import javax.persistence.OneToMany;
      import javax.persistence.SequenceGenerator;
      import javax.persistence.Table;
      import javax.persistence.Temporal;
      import javax.persistence.TemporalType;
      import javax.persistence.Transient;

      import org.hibernate.validator.Length;
      import org.hibernate.validator.NotNull;
      import org.jboss.seam.Component;
      import org.jboss.seam.annotations.Transactional;

      @Entity
      @Table(name = "M_Role")
      @SequenceGenerator(name = "roleSeq", sequenceName = "M_ROLE_SEQ")
      public class MRole implements Comparable<MRole>, java.io.Serializable {

           private static final long serialVersionUID = 1L;
           private Integer recid;
           private String name;
           private MDept MDept
           private Set<Action> actions = new HashSet<Action>(0);

           public MRole() {
           }

           public MRole(Integer recid, MDept MDept, String name) {
                this.recid = recid;
                this.MDept = MDept;
                this.name = name;
           }

           public MRole(Integer recid, MDept MDept, String name, Set<Action> actions) {
                this.recid = recid;
                this.MDept = MDept;
                this.name = name;
                this.actions = actions;
           }

           @Id
           @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "roleSeq")
           @Column(name = "RECID", unique = true, nullable = false, precision = 5, scale = 0)
           public Integer getRecid() {
                return this.recid;
           }

           public void setRecid(Integer recid) {
                this.recid = recid;
           }

           @ManyToOne(fetch = FetchType.LAZY)
           @JoinColumn(name = "DEPT_RECID", nullable = false)
           @NotNull
           public MDept getMDept() {
                return this.MDept;
           }

           public void setMDept(MDept MDept) {
                this.MDept = MDept;
           }

           @Column(name = "NAME", nullable = false, length = 50)
           @NotNull
           @Length(max = 50)
           public String getName() {
                return this.name;
           }

           public void setName(String name) {
                this.name = name;
           }

           @OneToMany(fetch = FetchType.LAZY, mappedBy = "MRole")
           public Set<Action> getActions() {
                return this.actions;
           }

           public void setActions(Set<Action> actions) {
                this.actions = actions;
           }

           @Override
           public int compareTo(MRole mRole) {
               int result = this.displaySeq.compareTo(mRole.getName());
               return result == 0 ? this.displaySeq.compareTo(((MRole) mRole).getName()) : result;
           }


           @Transient
           public List<Action> getValidStatusList() {
                List<Action> list = new ArrayList<Action>(this.actions);
                return list;
          }

           @Transient
           private Boolean isActionSelected(String action)
           {
                Iterator<Action> iter = this.getActions().iterator();
                while (iter.hasNext()) {
                     if (iter.next().getAction().equalsIgnoreCase(action)) {
                          return true;
                     }
                }
                return false;
           }

           @Transient
           @Transactional
           private void addAction(String action)
           {
                EntityManager entityManager = (EntityManager) Component.getInstance("em");
                Action action = new Action();
                action.setAction(action);
                action.setDefaultInd("N");
                action.setMRole(this);
                entityManager.persist(action);
                entityManager.flush();
                this.actions.add(action);
           }

           @Transient
           @Transactional
           private void removeAction(String action)
           {
                EntityManager entityManager = (EntityManager) Component.getInstance("em");
                Action action;
                Iterator<Action> iter = this.getActions().iterator();
                while (iter.hasNext()) {
                     action = iter.next();
                     if (action.getAction().equalsIgnoreCase(action)) {
                          this.actions.remove(action);
                          entityManager.remove(action);
                          entityManager.flush();
                          //iter.remove();
                          return;
                     }
                }
           }

           @Transient
           public Boolean getActionEdit()
           {
                return this.isActionSelected("E");
           }

           public void setActionEdit(Boolean editable)
           {
                if (editable) {
                     if (!this.getActionEdit()) {
                          this.addAction("E");
                     }               
                } else {
                     this.removeAction("E");
                }
           }

           @Transient
           public Boolean getActionView()
           {
                return isActionSelected("V");
           }

           public void setActionView(Boolean viewable)
           {
                if (viewable) {
                     if (!this.getActionView()) {
                          this.addAction("V");
                     }               
                } else {
                     this.removeAction("V");
                }
           }

           @Transient
           public Boolean getActionHide()
           {
                return isActionSelected("H");
           }

           public void setActionHide(Boolean hidden)
           {
                if (hidden) {
                     if (!this.getActionHide()) {
                          this.addAction("H");
                     }               
                } else {
                     this.removeAction("H");
                }
           }

           @Transient
           public String getDefaultAction()
           {
                String defaultAction = "";
                Iterator<Action> iter = this.getActions().iterator();
                while (iter.hasNext()) {
                     Action action = iter.next();
                     if (action.getDefaultInd().equalsIgnoreCase("Y")) {
                          defaultAction = action.getAction();
                     }
                }
                return defaultAction;
           }

           public void setDefaultAction(String action)
           {
                if (!this.getDefaultAction().equalsIgnoreCase(action)) { //Check if the default action has changed
                     if (action.equalsIgnoreCase("E")) { // Edit
                          if (!this.getActionEdit()) { // If the edit action is not enabled
                               this.addAction("E"); // Add the action
                          }
                          this.resetDefaultInd("E"); // Set the action to default
                     }
                     else if (action.equalsIgnoreCase("V")) { // View
                          if (!this.getActionView()) { // If the view action is not enabled
                               this.addAction("V"); // Add the action
                          }
                          this.resetDefaultInd("V"); // Set the action to default
                     }
                     else if (action.equalsIgnoreCase("H")) { // Hide
                          if (!this.getActionHide()) { // If the hide action is not enabled
                               this.addAction("H"); // Add the action
                          }
                          this.resetDefaultInd("H"); // Set the action to default
                     }
                }
           }

           @Transient
           private void resetDefaultInd(String action)
           {
                Iterator<Action> iter = this.getActions().iterator();
                while (iter.hasNext()) {
                     Action action = iter.next();
                     if (action.getAction().equalsIgnoreCase(action)) {
                          action.setDefaultInd("Y");
                     } else {
                          action.setDefaultInd("N");
                     }
                }
           }

      }
      ------------------------------------------------------------------------
      model\Action.java

      package com.domain.cco.model;

      import java.util.Date;
      import javax.persistence.Column;
      import javax.persistence.Entity;
      import javax.persistence.FetchType;
      import javax.persistence.GeneratedValue;
      import javax.persistence.GenerationType;
      import javax.persistence.Id;
      import javax.persistence.JoinColumn;
      import javax.persistence.ManyToOne;
      import javax.persistence.SequenceGenerator;
      import javax.persistence.Table;
      import javax.persistence.Temporal;
      import javax.persistence.TemporalType;
      import javax.persistence.Transient;

      import org.hibernate.validator.Length;
      import org.hibernate.validator.NotNull;

      @Entity
      @Table(name = "ACTION")
      @SequenceGenerator(name = "actionSeq", sequenceName = "ACTION_SEQ")
      public class Action implements java.io.Serializable {

           private static final long serialVersionUID = 1L;
           private Integer recid;
           private MRole MRole;
           private String action;
           private String defaultInd;

           public Action() {
           }

           public Action(Integer recid, MRole MRole, String action) {
                this.recid = recid;
                this.MRole = MRole;
                this.action = action;
           }

           public Action(Integer recid, MRole MRole, String action, String defaultInd) {
                this.recid = recid;
                this.MRole = MRole;
                this.action = action;
                this.defaultInd = defaultInd;
           }

           @Id
           @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "actionSeq")
           @Column(name = "RECID", unique = true, nullable = false, precision = 5, scale = 0)
           public Integer getRecid() {
                return this.recid;
           }

           public void setRecid(Integer recid) {
                this.recid = recid;
           }

           @ManyToOne(fetch = FetchType.LAZY)
           @JoinColumn(name = "ROLE_RECID", nullable = false)
           @NotNull
           public MRole getMRole() {
                return this.MRole;
           }

           public void setMRole(MRole MRole) {
                this.MRole = MRole;
           }

           @Column(name = "ACTION", nullable = false, length = 1)
           @NotNull
           @Length(max = 1)
           public String getAction() {
                return this.action;
           }

           public void setAction(String action) {
                this.action = action;
           }

           @Column(name = "DEFAULT_IND", length = 1)
           @Length(max = 1)
           public String getDefaultInd() {
                return this.defaultInd;
           }

           public void setDefaultInd(String defaultInd) {
                this.defaultInd = defaultInd;
           }

           @Transient
           public String getActionLabel()
           {
                String label = "";
                if (this.action.equalsIgnoreCase("E"))
                     label = "Edit";
                else if (this.action.equalsIgnoreCase("V"))
                     label = "View";
                else if (this.action.equalsIgnoreCase("H"))
                     label = "Hide";
                return label;
           }

      }
      -------------------------------------------------------------------------------

      I am new to SEAM and am not sure if this is the right way to do it or if there is a better way. ANy help will be appreciated.
        • 1. Re: Exception inserting or deleteing a new record
          ruthlesset
          I got it working, but I'm not sure how good the solution is. Added this code to MRoleHome.java
          ----------------------------------------------------------------------
               @Transactional
               public void addAction(String action)
               {
                    Action action = new Action();
                    action.setAction(action);
                    action.setDefaultInd("N");
                    action.setMRole(instance);
                    attachInstanceToEntityManager();
                    entityManager.persist(action);
                    instance.getActions().add(action);
               }

               @Transactional
               public void removeAction(String action)
               {
                    Action action;
                    Iterator<Action> iter = this.getActions().iterator();
                    while (iter.hasNext()) {
                         action= iter.next();
                         if (action.getAction().equalsIgnoreCase(action)) {
                              instance.getActions().remove(action);
                              attachInstanceToEntityManager();
                              entityManager.remove(action);
                              return;
                         }
                    }
               }


               private void attachInstanceToEntityManager() {
                    if (!super.isManaged() && getInstance().getRecid() != null) {
                         setInstance(getEntityManager().merge(getInstance()));
                    }
               }

          -------------------------------------------------------------------------------------

          Updated the functions under MRole.java

          --------------------------------------------------------------------------------------------

               @Transient
               private void addAction(String action)
               {
                    MRoleHome roleHome = (MRoleHome)Component.getInstance(MRoleHome.class);
                    roleHome.setId(this.recid);
                    roleHome.getInstance();
                    roleHome.addAction(action);
               }

               @Transient
               private void removeAction(String action)
               {
                    MRoleHome roleHome= (MRoleHome)Component.getInstance(MRoleHome.class);
                    roleHome.setId(this.recid);
                    roleHome.getInstance();
                    roleHome.removeAction(action);
               }

          --------------------------------------------------------------------------------------------