1.1.14.5.1.1.8. fejezet, SelectOneMenu

DataTable-ben "row editing" szerkesztési módba Not valid type üzenetet kapunk egy legördülő menünél, ha a menü elemeket alkotó osztálynak nincs az equals és a hashCode metódusa.

<p:column headerText="jelleg" style="width:30%;">
  <p:cellEditor>
    <f:facet name="output">
      <h:outputText value="#{acc.accessible_type.typename}" />
    </f:facet>
    <f:facet name="input">
      <p:selectOneMenu id="accTypeSelect" style="width:100%;"
        value="#{acc.accessible_type}"> <!-- #{currentAccessibleType} must have equals and hashCode impl and a converter -->
        <f:selectItems value="#{clientsManager.accessibleTypes}" var="accType" itemLabel="#{accType.typename}" itemValue="#{accType}" />
          <f:converter converterId="accTypeConv"/>
      </p:selectOneMenu>
    </f:facet>
  </p:cellEditor>
</p:column>

ClientManager.java

    public void onEditAcc(RowEditEvent event) {
        Accessible accessible = (Accessible) event.getObject();
        FacesMessage msg = new FacesMessage("Elérhetőség átszerkesztve", accessible.getAccessibleValue());
        clientsService.persistAccessible(accessible);
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }

Accessible.java

@Entity
@Table(name="accessible")
@NamedQuery(name="Accessible.findAll", query="SELECT a FROM Accessible a")
public class Accessible implements Serializable {
 
    private static final long serialVersionUID = -236080079995839428L;
 
    private Integer id;
    private String accessibleValue;
    private AccessibleType accessible_type;
 
	public Accessible() {
	}
 
 
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	@Column(unique=true, nullable=false)
	public Integer getId() {
		return this.id;
	}
 
	public void setId(Integer id) {
		this.id = id;
	}
 
 
	@Column(name="accessible_value")
	public String getAccessibleValue() {
		return this.accessibleValue;
	}
 
	public void setAccessibleValue(String accessibleValue) {
		this.accessibleValue = accessibleValue;
	}
 
 
	@ManyToOne(fetch=FetchType.EAGER)
	@JoinColumn(name="accessible_type_id")
	public AccessibleType getAccessible_type() {
		return this.accessible_type;
	}
 
	public void setAccessible_type(AccessibleType accessible_type) {
		this.accessible_type = accessible_type;
	}
 
 
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((id == null) ? 0 : id.hashCode());
        return result;
    }
 
 
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (!(obj instanceof Accessible))
            return false;
        Accessible other = (Accessible) obj;
        if (id == null) {
            if (other.id != null)
                return false;
        } else if (!id.equals(other.id))
            return false;
        return true;
    }
 
}

AccessibleType.java

@Entity
@Table(name="accessibletype")
@NamedQuery(name="AccessibleType.findAll", query="SELECT a FROM AccessibleType a")
public class AccessibleType implements Serializable {
 
    private static final long serialVersionUID = -1819079521899925869L;
 
    private Integer id;
	private String typename;
 
	public AccessibleType() {
	}
 
 
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	@Column(unique=true, nullable=false)
	public Integer getId() {
		return this.id;
	}
 
	public void setId(Integer id) {
		this.id = id;
	}
 
 
	@Column
	@Basic
	public String getTypename() {
		return this.typename;
	}
 
	public void setTypename(String typename) {
		this.typename = typename;
	}
 
 
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((id == null) ? 0 : id.hashCode());
        return result;
    }
 
 
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (!(obj instanceof AccessibleType))
            return false;
        AccessibleType other = (AccessibleType) obj;
        if (id == null) {
            if (other.id != null)
                return false;
        } else if (!id.equals(other.id))
            return false;
        return true;
    }
 
}

AccTypeConverter.java

public class accTypeConverter implements Converter {
 
    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        if (value == null) {
            return null;
        } else {
            AccessibleType result = null;
            try {
                Integer id = Integer.parseInt(value);
                InitialContext ic = new InitialContext();
                Object service = ic
                        .lookup("java:/global/accessadmin/ClientsService");
                if ((service != null) && (service instanceof ClientsService)) {
                    System.out.println("service:" + service);
                    result = ((ClientsService) service).findAccessibleType(id);
                } else {
                    System.out.println("service is null");
                }
            } catch (NumberFormatException ex) {
 
            } catch (Throwable t) {
                System.out.println("service lookup failed");
            }
 
            return result;
        }
    }
 
    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        AccessibleType accType = (AccessibleType) value;
        if (accType != null)
            return String.valueOf(accType.getId());
        else
            return null;
    }
 
}