SelectOneMenu Entity Values
fmaredia May 14, 2010 11:41 PMSorry, this problem seems to have happened to many and I have tried everything I could possibly look at on this forum, but I must be missing something because I can't seem to get something very simple like this to work:
Regions.java
package com.at.hib.persistence;
// Generated Aug 15, 2008 3:28:57 PM by Hibernate Tools 3.1.0.beta5
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.jboss.seam.annotations.Name;
/**
* Regions generated by hbm2java
*/
@Entity
@Name("regions")
@Table(name = "regions", uniqueConstraints = {})
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Regions implements java.io.Serializable {
// Fields
private Integer id;
private String regions;
private String description;
private Set<ProductsRegions> productsRegionsSet = new HashSet<ProductsRegions>(0);
private Set<Countries> countriesSet = new HashSet<Countries>(0);
// Constructors
/** default constructor */
public Regions() {
}
/** full constructor */
public Regions(String regions, String description, Set<ProductsRegions> productsRegionsSet,
Set<Countries> countriesSet) {
this.regions = regions;
this.description = description;
this.productsRegionsSet = productsRegionsSet;
this.countriesSet = countriesSet;
}
// Property accessors
@Id
@GeneratedValue
@Column(name = "Regions_ID", unique = true, nullable = false, insertable = true, updatable = true)
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(name = "Regions", unique = false, nullable = true, insertable = true, updatable = true, length = 100)
public String getRegions() {
return this.regions;
}
public void setRegions(String regions) {
this.regions = regions;
}
@Column(name = "Description", unique = false, nullable = true, insertable = true, updatable = true)
public String getDescription() {
return this.description;
}
public void setDescription(String description) {
this.description = description;
}
@OneToMany(cascade = { }, fetch = FetchType.LAZY, mappedBy = "regions")
public Set<ProductsRegions> getProductsRegionsSet() {
return this.productsRegionsSet;
}
public void setProductsRegionsSet(Set<ProductsRegions> productsRegionsSet) {
this.productsRegionsSet = productsRegionsSet;
}
@OneToMany(cascade = { }, fetch = FetchType.LAZY, mappedBy = "regions")
public Set<Countries> getCountriesSet() {
return this.countriesSet;
}
public void setCountriesSet(Set<Countries> countriesSet) {
this.countriesSet = countriesSet;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || !(o.getClass() == this.getClass())) {
return false;
}
Regions other = (Regions) o;
if (getId() == null)
return false;
return getId().equals(other.getId());
}
@Override
public int hashCode() {
if (getId() != null) {
return getId().hashCode();
}
return super.hashCode();
}
}
Countries.java
package com.at.hib.persistence;
// Generated Aug 14, 2008 11:16:32 AM by Hibernate Tools 3.1.0.beta5
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.jboss.seam.annotations.Name;
/**
* Countries generated by hbm2java
*/
@Entity
@Name("countries")
@Table(name = "countries", uniqueConstraints = {})
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Countries implements java.io.Serializable {
// Fields
private Integer id;
private Regions regions;
private String countries;
private String description;
private String isocode;
private Set<Person> personSetByMCountryID = new HashSet<Person>(0);
private Set<PersonAddress> personAddressSet = new HashSet<PersonAddress>(0);
private Set<Cities> citiesSet = new HashSet<Cities>(0);
private Set<States> statesSet = new HashSet<States>(0);
private Set<Person> personSetByPassportCountryID = new HashSet<Person>(0);
private Set<Employee> employeeSetByCitizenCountryID = new HashSet<Employee>(0);
private Set<PaymentCcards> paymentCcardsSet = new HashSet<PaymentCcards>(0);
private Set<ProductsCountries> productsCountriesSet = new HashSet<ProductsCountries>(0);
private Set<Employee> employeeSetByMCountryID = new HashSet<Employee>(0);
private Set<Person> personSetByCitizenCountryID = new HashSet<Person>(0);
private Set<EmployeeEmergencyContact> employeeEmergencyContactSet = new HashSet<EmployeeEmergencyContact>(
0);
private Set<Person> personSetByPCountryID = new HashSet<Person>(0);
private Set<Employee> employeeSetByPCountryID = new HashSet<Employee>(0);
// Constructors
/** default constructor */
public Countries() {
}
public Countries(int id) {
this.id = new Integer(id);
}
/** full constructor */
public Countries(Regions regionsid, String countries, String description, Set<Person> personSet,
Set<PersonAddress> personAddressSet, Set<Cities> citiesSet, Set<States> statesSet,
Set<Person> personSet_1, Set<Employee> employeeSet, Set<PaymentCcards> paymentCcardsSet,
Set<ProductsCountries> productsCountriesSet, Set<Employee> employeeSet_1,
Set<Person> personSet_2,
Set<EmployeeEmergencyContact> employeeEmergencyContactSet, Set<Person> personSet_3,
Set<Employee> employeeSet_2) {
this.regions = regionsid;
this.countries = countries;
this.description = description;
this.personSetByMCountryID = personSet;
this.personAddressSet = personAddressSet;
this.citiesSet = citiesSet;
this.statesSet = statesSet;
this.personSetByPassportCountryID = personSet_1;
this.employeeSetByCitizenCountryID = employeeSet;
this.paymentCcardsSet = paymentCcardsSet;
this.productsCountriesSet = productsCountriesSet;
this.employeeSetByMCountryID = employeeSet_1;
this.personSetByCitizenCountryID = personSet_2;
this.employeeEmergencyContactSet = employeeEmergencyContactSet;
this.personSetByPCountryID = personSet_3;
this.employeeSetByPCountryID = employeeSet_2;
}
// Property accessors
@Id
@GeneratedValue
@Column(name = "Countries_ID", unique = true, nullable = false, insertable = true, updatable = true)
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
@ManyToOne(cascade = {}, fetch = FetchType.EAGER)
@JoinColumn(name = "regions_id", unique = false, nullable = true, insertable = true, updatable = true)
public Regions getRegions() {
return this.regions;
}
public void setRegions(Regions regionsid) {
this.regions = regionsid;
}
@Column(name = "Countries", unique = false, nullable = true, insertable = true, updatable = true, length = 100)
public String getCountries() {
return this.countries;
}
public void setCountries(String countries) {
this.countries = countries;
}
@Column(name = "Description", unique = false, nullable = true, insertable = true, updatable = true)
public String getDescription() {
return this.description;
}
public void setDescription(String description) {
this.description = description;
}
@Column(name = "isocode", unique = false, nullable = true, insertable = true, updatable = true, length = 10)
public String getIsocode() {
return this.isocode;
}
public void setIsocode(String isocode) {
this.isocode = isocode;
}
@OneToMany(cascade = { }, fetch = FetchType.LAZY, mappedBy = "MCountryID")
public Set<Person> getPersonSetByMCountryID() {
return this.personSetByMCountryID;
}
public void setPersonSetByMCountryID(Set<Person> personSet) {
this.personSetByMCountryID = personSet;
}
@OneToMany(cascade = { }, fetch = FetchType.LAZY, mappedBy = "country")
public Set<PersonAddress> getPersonAddressSet() {
return this.personAddressSet;
}
public void setPersonAddressSet(Set<PersonAddress> personAddressSet) {
this.personAddressSet = personAddressSet;
}
@OneToMany(cascade = { }, fetch = FetchType.LAZY, mappedBy = "countries")
public Set<Cities> getCitiesSet() {
return this.citiesSet;
}
public void setCitiesSet(Set<Cities> citiesSet) {
this.citiesSet = citiesSet;
}
@OneToMany(cascade = { }, fetch = FetchType.LAZY, mappedBy = "countryID")
public Set<States> getStatesSet() {
return this.statesSet;
}
public void setStatesSet(Set<States> statesSet) {
this.statesSet = statesSet;
}
@OneToMany(cascade = { }, fetch = FetchType.LAZY, mappedBy = "passportCountryID")
public Set<Person> getPersonSetByPassportCountryID() {
return this.personSetByPassportCountryID;
}
public void setPersonSetByPassportCountryID(Set<Person> personSet_1) {
this.personSetByPassportCountryID = personSet_1;
}
@OneToMany(cascade = { }, fetch = FetchType.LAZY, mappedBy = "citizenshipCountryID")
public Set<Employee> getEmployeeSetByCitizenCountryID() {
return this.employeeSetByCitizenCountryID;
}
public void setEmployeeSetByCitizenCountryID(Set<Employee> employeeSet) {
this.employeeSetByCitizenCountryID = employeeSet;
}
@OneToMany(cascade = { }, fetch = FetchType.LAZY, mappedBy = "country")
public Set<PaymentCcards> getPaymentCcardsSet() {
return this.paymentCcardsSet;
}
public void setPaymentCcardsSet(Set<PaymentCcards> paymentCcardsSet) {
this.paymentCcardsSet = paymentCcardsSet;
}
@OneToMany(cascade = { }, fetch = FetchType.LAZY, mappedBy = "countries")
public Set<ProductsCountries> getProductsCountriesSet() {
return this.productsCountriesSet;
}
public void setProductsCountriesSet(Set<ProductsCountries> productsCountriesSet) {
this.productsCountriesSet = productsCountriesSet;
}
@OneToMany(cascade = { }, fetch = FetchType.LAZY, mappedBy = "MCountryID")
public Set<Employee> getEmployeeSetByMCountryID() {
return this.employeeSetByMCountryID;
}
public void setEmployeeSetByMCountryID(Set<Employee> employeeSet_1) {
this.employeeSetByMCountryID = employeeSet_1;
}
@OneToMany(cascade = { }, fetch = FetchType.LAZY, mappedBy = "citizenCountryID")
public Set<Person> getPersonSetByCitizenCountryID() {
return this.personSetByCitizenCountryID;
}
public void setPersonSetByCitizenCountryID(Set<Person> personSet_2) {
this.personSetByCitizenCountryID = personSet_2;
}
@OneToMany(cascade = { }, fetch = FetchType.LAZY, mappedBy = "countryID")
public Set<EmployeeEmergencyContact> getEmployeeEmergencyContactSet() {
return this.employeeEmergencyContactSet;
}
public void setEmployeeEmergencyContactSet(Set<EmployeeEmergencyContact> employeeEmergencyContactSet) {
this.employeeEmergencyContactSet = employeeEmergencyContactSet;
}
@OneToMany(cascade = { }, fetch = FetchType.LAZY, mappedBy = "PCountryID")
public Set<Person> getPersonSetByPCountryID() {
return this.personSetByPCountryID;
}
public void setPersonSetByPCountryID(Set<Person> personSet_3) {
this.personSetByPCountryID = personSet_3;
}
@OneToMany(cascade = { }, fetch = FetchType.LAZY, mappedBy = "PCountryID")
public Set<Employee> getEmployeeSetByPCountryID() {
return this.employeeSetByPCountryID;
}
public void setEmployeeSetByPCountryID(Set<Employee> employeeSet_2) {
this.employeeSetByPCountryID = employeeSet_2;
}
}
ManageCountriesBean.java
package com.at.cms;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.ejb.Remove;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Factory;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.datamodel.DataModel;
import org.jboss.seam.faces.FacesMessages;
import org.richfaces.model.selection.SimpleSelection;
import com.at.hib.persistence.Countries;
import com.at.hib.persistence.Regions;
@Stateful
@Scope(ScopeType.SESSION)
@Name("manageCountries")
public class ManageCountriesBean implements Serializable, ManageCountries {
@In(required=false)
private Countries country;
@DataModel
private List<Countries> countrieslist = null;
@DataModel
private List<Regions> regionslist = null;
private List<Countries> selectedcountries = new ArrayList<Countries>();
private SimpleSelection selection = new SimpleSelection();
@PersistenceContext
private EntityManager em;
public void addCountry() {
if (country.getDescription() != null && country.getDescription().trim().length() > 0) {
em.persist(country);
}
else {
FacesMessages.instance().add("Country #{country.countries} needs a description");
}
}
public void modifyCountry() {
}
@Factory("countrieslist")
public void findCountrieslist() {
countrieslist = em.createQuery("select c from Countries c order by c.countries").getResultList();
}
@Factory("regionslist")
public void findRegionslist() {
if (regionslist == null)
regionslist = em.createQuery("select r from Regions r order by r.regions").getResultList();
}
public SimpleSelection getSelection() {
return selection;
}
public void setSelection(SimpleSelection selection) {
this.selection = selection;
}
public String takeCountry() {
System.out.println("Inside TakeCountry");
getSelectedcountries().clear();
if (getSelection().isSelectAll()) {
System.out.println("inside isSelect All");
getSelectedcountries().addAll(countrieslist);
}
else {
System.out.println("inside else");
Iterator<Object> iterator = getSelection().getKeys();
System.out.println("Selection Size: " + getSelection().size());
while (iterator.hasNext()) {
Object key = iterator.next();
System.out.println("Key: " + key);
Countries mycountry = countrieslist.get((Integer) key);
if (mycountry != null)
System.out.println("Country Selected: " + mycountry.getCountries());
getSelectedcountries().add(countrieslist.get((Integer) key));
}
if (getSelectedcountries() != null && !getSelectedcountries().isEmpty()) {
for (Countries myc : getSelectedcountries())
System.out.println("forloop: " + myc.getCountries());
}
else
System.out.println("getSelectedCountries is empty");
}
return null;
}
public List<Countries> getSelectedcountries() {
if (selectedcountries != null && !selectedcountries.isEmpty()) {
for (Countries myc : selectedcountries)
System.out.println("getSelectedcountries forloop: " + myc.getCountries());
}
else
System.out.println("getSelectedCountries is empty");
return selectedcountries;
}
public void setSelectedcountries(List<Countries> selectedcountries) {
this.selectedcountries = selectedcountries;
}
@Remove
public void destroy() {}
}
toursmanageCountries.xhtml
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition 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"
template="layout/template.xhtml">
<style>
.active-row {
background-color: #FFEBDA;
}
</style>
<ui:define name="body">
<rich:panel>
<f:facet name="header">toursmanageCountries</f:facet>
<h:form id="tourmanageCountriesForm">
<s:validateAll>
Region: <h:selectOneMenu value="#{countries.regions}" required="true">
<s:selectItems var="_regions" value="#{tregionslist}" label="#{_regions.regions}" noSelectionLabel="Please Select"/>
<s:convertEntity/>
</h:selectOneMenu><br/>
Country Name: <h:inputText value="#{countries.countries}" required="true" /><br/>
ISO Code: <h:inputText value="#{countries.isocode}" required="true" /><br/>
Description: <h:inputText value="#{countries.description}" /><br/>
<h:commandButton id="toursmanageCountries" value="Add Country"
action="#{toursmanageCountries.addCountry}"/>
</s:validateAll>
</h:form>
<hr/>
</rich:panel>
<h:form>
<script type="text/javascript">
var row;
</script>
<rich:contextMenu attached="false" id="menu" submitMode="ajax" oncollapse="row.style.backgroundColor='#{a4jSkin.tableBackgroundColor}'">
<rich:menuItem value="Edit Record" ajaxSingle="true"
oncomplete="#{rich:component('editPanel')}.show()"
actionListener="#{toursmanageCountries.fetchCurrentRow}">
<a4j:actionparam name="cid" value="{Id}" />
<a4j:actionparam name="row" value="{currentRow}" />
</rich:menuItem>
<rich:menuItem value="Remove Record" ajaxSingle="true"
oncomplete="#{rich:component('deletePanel')}.show()"
actionListener="#{toursmanageCountries.fetchCurrentRow}">
<a4j:actionparam name="cid" value="{Id}" />
<a4j:actionparam name="row" value="{currentRow}" />
</rich:menuItem>
</rich:contextMenu>
<a4j:region>
<rich:dataTable value="#{tcountrieslist}"
var="record" rowKeyVar="row"
ajaxKeys="#{toursmanageCountries.keys}" id="table"
height="200px"
onRowContextMenu="if (row) row.style.backgroundColor='#{a4jSkin.tableBackgroundColor}';
this.style.backgroundColor='#F1F1F1'; row=this;
#{rich:component('menu')}.show(event,{Id:'#{record.id}', currentRow:'#{row}'});
return false;">
<f:facet name="header">
<h:outputText value="Edit/Remove Countries" />
</f:facet>
<rich:column>
<f:facet name="header">
<h:outputText value="Country" />
</f:facet>
<h:outputText value="#{record.countries}" id="countries" />
</rich:column>
<rich:column>
<f:facet name="header">
<h:outputText value="Region" />
</f:facet>
<h:outputText value="#{record.regions.regions}" id="regions" />
</rich:column>
<rich:column>
<f:facet name="header">
<h:outputText value="ISOCode" />
</f:facet>
<h:outputText value="#{record.isocode}" id="isocode" />
</rich:column>
<rich:column>
<f:facet name="header">
Actions
</f:facet>
<a4j:commandLink ajaxSingle="true" id="editlink"
oncomplete="#{rich:component('editPanel')}.show()">
<h:graphicImage value="/img/icons/edit.gif" style="border:0" />
<f:setPropertyActionListener value="#{record}"
target="#{toursmanageCountries.currentItem}" />
<f:setPropertyActionListener value="#{row}"
target="#{toursmanageCountries.currentRow}" />
</a4j:commandLink>
<rich:toolTip for="editlink" value="Edit" />
<a4j:commandLink ajaxSingle="true" id="deletelink"
oncomplete="#{rich:component('deletePanel')}.show()">
<h:graphicImage value="/img/icons/delete.gif" style="border:0" />
<f:setPropertyActionListener value="#{row}"
target="#{toursmanageCountries.currentRow}" />
</a4j:commandLink>
<rich:toolTip for="deletelink" value="Delete" />
</rich:column>
<f:facet name="footer">
<rich:datascroller renderIfSinglePage="false" maxPages="10" />
</f:facet>
</rich:dataTable>
</a4j:region>
</h:form>
<rich:modalPanel id="editPanel" autosized="true" width="450">
<f:facet name="header">
<h:outputText value="Edit Current Car" />
</f:facet>
<f:facet name="controls">
<h:panelGroup>
<h:graphicImage value="/img/modal/close.png" id="hidelink"
styleClass="hidelink" />
<rich:componentControl for="editPanel" attachTo="hidelink"
operation="hide" event="onclick" />
</h:panelGroup>
</f:facet>
<h:form>
<rich:messages style="color:red;"></rich:messages>
<h:panelGrid columns="1">
<a4j:outputPanel ajaxRendered="true">
<h:panelGrid columns="2">
<h:outputText value="Country Name" />
<h:inputText value="#{toursmanageCountries.currentItem.countries}" />
<h:outputText value="Region" />
<h:selectOneMenu value="#{toursmanageCountries.currentItem.regions}">
<s:selectItems var="_regions" value="#{tregionslist}" label="#{_regions.regions}" noSelectionLabel="Please Select"/>
<s:convertEntity/>
</h:selectOneMenu>
<h:outputText value="ISO Code" />
<h:inputText value="#{toursmanageCountries.currentItem.isocode}" />
<h:outputText value="Description" />
<h:inputTextarea value="#{toursmanageCountries.currentItem.description}" rows="5" cols="45" />
</h:panelGrid>
<rich:message showSummary="true" showDetail="false" for="price" />
</a4j:outputPanel>
<a4j:commandButton value="Update Country"
action="#{toursmanageCountries.modifyCountry}"
reRender="countries, region, isocode"
oncomplete="if (#{facesContext.maximumSeverity==null}) #{rich:component('editPanel')}.hide();" />
</h:panelGrid>
</h:form>
</rich:modalPanel>
<rich:modalPanel id="deletePanel" autosized="true" width="200">
<f:facet name="header">
<h:outputText value="Delete this country from list?"
style="padding-right:15px;" />
</f:facet>
<f:facet name="controls">
<h:panelGroup>
<h:graphicImage value="/img/modal/close.png"
styleClass="hidelink" id="hidelink2" />
<rich:componentControl for="deletePanel" attachTo="hidelink2"
operation="hide" event="onclick" />
</h:panelGroup>
</f:facet>
<h:form>
<table width="100%">
<tbody>
<tr>
<td align="center" width="50%"><a4j:commandButton value="Yes"
ajaxSingle="true" action="#{toursmanageCountries.removeCountry}"
oncomplete="#{rich:component('deletePanel')}.hide();"
reRender="table" /></td>
<td align="center" width="50%"><a4j:commandButton
value="Cancel"
onclick="#{rich:component('deletePanel')}.hide();return false;" />
</td>
</tr>
</tbody>
</table>
</h:form>
</rich:modalPanel>
<a4j:status onstart="#{rich:component('wait')}.show()"
onstop="#{rich:component('wait')}.hide()" />
<rich:modalPanel id="wait" autosized="true" width="200" height="120"
moveable="false" resizeable="false">
<f:facet name="header">
<h:outputText value="Processing" />
</f:facet>
<h:outputText value="Wait Please..." />
</rich:modalPanel>
<rich:messages></rich:messages>
</ui:define>
</ui:composition>
As you can see the selectOneMenu is pretty simple as it relates to the regions:
Region: <h:selectOneMenu value="#{countries.regions}" required="true">
<s:selectItems var="_regions" value="#{tregionslist}" label="#{_regions.regions}" noSelectionLabel="Please Select"/>
<s:convertEntity/>
</h:selectOneMenu><br/>
The HTML Outputted is incorrect as the values are incremented from 0 to 10 as such:
Region:
<select name="tourmanageCountriesForm:j_id17" size="1"> <option value="org.jboss.seam.ui.NoSelectionConverter.noSelectionValue">Please Select</option>
<option value="0">Africa</option>
<option value="1" selected="selected">Antarctica</option>
<option value="2">Asia</option>
<option value="3">Central America</option>
<option value="4">Eastern Europe</option>
<option value="5">Europe</option>
<option value="6">Middle East</option>
<option value="7">North America</option>
<option value="8">Scandinavia</option>
<option value="9">South America</option>
<option value="10">South Pacific</option>
</select><br />
It should have values of :
11 - Africa
12 - Antarctica
13 - Asia
14 - Central America
15 - Eastern Europe
21 - Europe
16 - Middle East
17 - North America
18 - Scandinavia
19 - South America
20 - South Pacific
Obviously this creates a problem where I get a 'value is not valid' error.
Could some one please help me out or point me to the right direction. Thank you.
Regions.java
package com.at.hib.persistence;
// Generated Aug 15, 2008 3:28:57 PM by Hibernate Tools 3.1.0.beta5
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.jboss.seam.annotations.Name;
/**
* Regions generated by hbm2java
*/
@Entity
@Name("regions")
@Table(name = "regions", uniqueConstraints = {})
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Regions implements java.io.Serializable {
// Fields
private Integer id;
private String regions;
private String description;
private Set<ProductsRegions> productsRegionsSet = new HashSet<ProductsRegions>(0);
private Set<Countries> countriesSet = new HashSet<Countries>(0);
// Constructors
/** default constructor */
public Regions() {
}
/** full constructor */
public Regions(String regions, String description, Set<ProductsRegions> productsRegionsSet,
Set<Countries> countriesSet) {
this.regions = regions;
this.description = description;
this.productsRegionsSet = productsRegionsSet;
this.countriesSet = countriesSet;
}
// Property accessors
@Id
@GeneratedValue
@Column(name = "Regions_ID", unique = true, nullable = false, insertable = true, updatable = true)
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(name = "Regions", unique = false, nullable = true, insertable = true, updatable = true, length = 100)
public String getRegions() {
return this.regions;
}
public void setRegions(String regions) {
this.regions = regions;
}
@Column(name = "Description", unique = false, nullable = true, insertable = true, updatable = true)
public String getDescription() {
return this.description;
}
public void setDescription(String description) {
this.description = description;
}
@OneToMany(cascade = { }, fetch = FetchType.LAZY, mappedBy = "regions")
public Set<ProductsRegions> getProductsRegionsSet() {
return this.productsRegionsSet;
}
public void setProductsRegionsSet(Set<ProductsRegions> productsRegionsSet) {
this.productsRegionsSet = productsRegionsSet;
}
@OneToMany(cascade = { }, fetch = FetchType.LAZY, mappedBy = "regions")
public Set<Countries> getCountriesSet() {
return this.countriesSet;
}
public void setCountriesSet(Set<Countries> countriesSet) {
this.countriesSet = countriesSet;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || !(o.getClass() == this.getClass())) {
return false;
}
Regions other = (Regions) o;
if (getId() == null)
return false;
return getId().equals(other.getId());
}
@Override
public int hashCode() {
if (getId() != null) {
return getId().hashCode();
}
return super.hashCode();
}
}
Countries.java
package com.at.hib.persistence;
// Generated Aug 14, 2008 11:16:32 AM by Hibernate Tools 3.1.0.beta5
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.jboss.seam.annotations.Name;
/**
* Countries generated by hbm2java
*/
@Entity
@Name("countries")
@Table(name = "countries", uniqueConstraints = {})
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Countries implements java.io.Serializable {
// Fields
private Integer id;
private Regions regions;
private String countries;
private String description;
private String isocode;
private Set<Person> personSetByMCountryID = new HashSet<Person>(0);
private Set<PersonAddress> personAddressSet = new HashSet<PersonAddress>(0);
private Set<Cities> citiesSet = new HashSet<Cities>(0);
private Set<States> statesSet = new HashSet<States>(0);
private Set<Person> personSetByPassportCountryID = new HashSet<Person>(0);
private Set<Employee> employeeSetByCitizenCountryID = new HashSet<Employee>(0);
private Set<PaymentCcards> paymentCcardsSet = new HashSet<PaymentCcards>(0);
private Set<ProductsCountries> productsCountriesSet = new HashSet<ProductsCountries>(0);
private Set<Employee> employeeSetByMCountryID = new HashSet<Employee>(0);
private Set<Person> personSetByCitizenCountryID = new HashSet<Person>(0);
private Set<EmployeeEmergencyContact> employeeEmergencyContactSet = new HashSet<EmployeeEmergencyContact>(
0);
private Set<Person> personSetByPCountryID = new HashSet<Person>(0);
private Set<Employee> employeeSetByPCountryID = new HashSet<Employee>(0);
// Constructors
/** default constructor */
public Countries() {
}
public Countries(int id) {
this.id = new Integer(id);
}
/** full constructor */
public Countries(Regions regionsid, String countries, String description, Set<Person> personSet,
Set<PersonAddress> personAddressSet, Set<Cities> citiesSet, Set<States> statesSet,
Set<Person> personSet_1, Set<Employee> employeeSet, Set<PaymentCcards> paymentCcardsSet,
Set<ProductsCountries> productsCountriesSet, Set<Employee> employeeSet_1,
Set<Person> personSet_2,
Set<EmployeeEmergencyContact> employeeEmergencyContactSet, Set<Person> personSet_3,
Set<Employee> employeeSet_2) {
this.regions = regionsid;
this.countries = countries;
this.description = description;
this.personSetByMCountryID = personSet;
this.personAddressSet = personAddressSet;
this.citiesSet = citiesSet;
this.statesSet = statesSet;
this.personSetByPassportCountryID = personSet_1;
this.employeeSetByCitizenCountryID = employeeSet;
this.paymentCcardsSet = paymentCcardsSet;
this.productsCountriesSet = productsCountriesSet;
this.employeeSetByMCountryID = employeeSet_1;
this.personSetByCitizenCountryID = personSet_2;
this.employeeEmergencyContactSet = employeeEmergencyContactSet;
this.personSetByPCountryID = personSet_3;
this.employeeSetByPCountryID = employeeSet_2;
}
// Property accessors
@Id
@GeneratedValue
@Column(name = "Countries_ID", unique = true, nullable = false, insertable = true, updatable = true)
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
@ManyToOne(cascade = {}, fetch = FetchType.EAGER)
@JoinColumn(name = "regions_id", unique = false, nullable = true, insertable = true, updatable = true)
public Regions getRegions() {
return this.regions;
}
public void setRegions(Regions regionsid) {
this.regions = regionsid;
}
@Column(name = "Countries", unique = false, nullable = true, insertable = true, updatable = true, length = 100)
public String getCountries() {
return this.countries;
}
public void setCountries(String countries) {
this.countries = countries;
}
@Column(name = "Description", unique = false, nullable = true, insertable = true, updatable = true)
public String getDescription() {
return this.description;
}
public void setDescription(String description) {
this.description = description;
}
@Column(name = "isocode", unique = false, nullable = true, insertable = true, updatable = true, length = 10)
public String getIsocode() {
return this.isocode;
}
public void setIsocode(String isocode) {
this.isocode = isocode;
}
@OneToMany(cascade = { }, fetch = FetchType.LAZY, mappedBy = "MCountryID")
public Set<Person> getPersonSetByMCountryID() {
return this.personSetByMCountryID;
}
public void setPersonSetByMCountryID(Set<Person> personSet) {
this.personSetByMCountryID = personSet;
}
@OneToMany(cascade = { }, fetch = FetchType.LAZY, mappedBy = "country")
public Set<PersonAddress> getPersonAddressSet() {
return this.personAddressSet;
}
public void setPersonAddressSet(Set<PersonAddress> personAddressSet) {
this.personAddressSet = personAddressSet;
}
@OneToMany(cascade = { }, fetch = FetchType.LAZY, mappedBy = "countries")
public Set<Cities> getCitiesSet() {
return this.citiesSet;
}
public void setCitiesSet(Set<Cities> citiesSet) {
this.citiesSet = citiesSet;
}
@OneToMany(cascade = { }, fetch = FetchType.LAZY, mappedBy = "countryID")
public Set<States> getStatesSet() {
return this.statesSet;
}
public void setStatesSet(Set<States> statesSet) {
this.statesSet = statesSet;
}
@OneToMany(cascade = { }, fetch = FetchType.LAZY, mappedBy = "passportCountryID")
public Set<Person> getPersonSetByPassportCountryID() {
return this.personSetByPassportCountryID;
}
public void setPersonSetByPassportCountryID(Set<Person> personSet_1) {
this.personSetByPassportCountryID = personSet_1;
}
@OneToMany(cascade = { }, fetch = FetchType.LAZY, mappedBy = "citizenshipCountryID")
public Set<Employee> getEmployeeSetByCitizenCountryID() {
return this.employeeSetByCitizenCountryID;
}
public void setEmployeeSetByCitizenCountryID(Set<Employee> employeeSet) {
this.employeeSetByCitizenCountryID = employeeSet;
}
@OneToMany(cascade = { }, fetch = FetchType.LAZY, mappedBy = "country")
public Set<PaymentCcards> getPaymentCcardsSet() {
return this.paymentCcardsSet;
}
public void setPaymentCcardsSet(Set<PaymentCcards> paymentCcardsSet) {
this.paymentCcardsSet = paymentCcardsSet;
}
@OneToMany(cascade = { }, fetch = FetchType.LAZY, mappedBy = "countries")
public Set<ProductsCountries> getProductsCountriesSet() {
return this.productsCountriesSet;
}
public void setProductsCountriesSet(Set<ProductsCountries> productsCountriesSet) {
this.productsCountriesSet = productsCountriesSet;
}
@OneToMany(cascade = { }, fetch = FetchType.LAZY, mappedBy = "MCountryID")
public Set<Employee> getEmployeeSetByMCountryID() {
return this.employeeSetByMCountryID;
}
public void setEmployeeSetByMCountryID(Set<Employee> employeeSet_1) {
this.employeeSetByMCountryID = employeeSet_1;
}
@OneToMany(cascade = { }, fetch = FetchType.LAZY, mappedBy = "citizenCountryID")
public Set<Person> getPersonSetByCitizenCountryID() {
return this.personSetByCitizenCountryID;
}
public void setPersonSetByCitizenCountryID(Set<Person> personSet_2) {
this.personSetByCitizenCountryID = personSet_2;
}
@OneToMany(cascade = { }, fetch = FetchType.LAZY, mappedBy = "countryID")
public Set<EmployeeEmergencyContact> getEmployeeEmergencyContactSet() {
return this.employeeEmergencyContactSet;
}
public void setEmployeeEmergencyContactSet(Set<EmployeeEmergencyContact> employeeEmergencyContactSet) {
this.employeeEmergencyContactSet = employeeEmergencyContactSet;
}
@OneToMany(cascade = { }, fetch = FetchType.LAZY, mappedBy = "PCountryID")
public Set<Person> getPersonSetByPCountryID() {
return this.personSetByPCountryID;
}
public void setPersonSetByPCountryID(Set<Person> personSet_3) {
this.personSetByPCountryID = personSet_3;
}
@OneToMany(cascade = { }, fetch = FetchType.LAZY, mappedBy = "PCountryID")
public Set<Employee> getEmployeeSetByPCountryID() {
return this.employeeSetByPCountryID;
}
public void setEmployeeSetByPCountryID(Set<Employee> employeeSet_2) {
this.employeeSetByPCountryID = employeeSet_2;
}
}
ManageCountriesBean.java
package com.at.cms;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.ejb.Remove;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Factory;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.datamodel.DataModel;
import org.jboss.seam.faces.FacesMessages;
import org.richfaces.model.selection.SimpleSelection;
import com.at.hib.persistence.Countries;
import com.at.hib.persistence.Regions;
@Stateful
@Scope(ScopeType.SESSION)
@Name("manageCountries")
public class ManageCountriesBean implements Serializable, ManageCountries {
@In(required=false)
private Countries country;
@DataModel
private List<Countries> countrieslist = null;
@DataModel
private List<Regions> regionslist = null;
private List<Countries> selectedcountries = new ArrayList<Countries>();
private SimpleSelection selection = new SimpleSelection();
@PersistenceContext
private EntityManager em;
public void addCountry() {
if (country.getDescription() != null && country.getDescription().trim().length() > 0) {
em.persist(country);
}
else {
FacesMessages.instance().add("Country #{country.countries} needs a description");
}
}
public void modifyCountry() {
}
@Factory("countrieslist")
public void findCountrieslist() {
countrieslist = em.createQuery("select c from Countries c order by c.countries").getResultList();
}
@Factory("regionslist")
public void findRegionslist() {
if (regionslist == null)
regionslist = em.createQuery("select r from Regions r order by r.regions").getResultList();
}
public SimpleSelection getSelection() {
return selection;
}
public void setSelection(SimpleSelection selection) {
this.selection = selection;
}
public String takeCountry() {
System.out.println("Inside TakeCountry");
getSelectedcountries().clear();
if (getSelection().isSelectAll()) {
System.out.println("inside isSelect All");
getSelectedcountries().addAll(countrieslist);
}
else {
System.out.println("inside else");
Iterator<Object> iterator = getSelection().getKeys();
System.out.println("Selection Size: " + getSelection().size());
while (iterator.hasNext()) {
Object key = iterator.next();
System.out.println("Key: " + key);
Countries mycountry = countrieslist.get((Integer) key);
if (mycountry != null)
System.out.println("Country Selected: " + mycountry.getCountries());
getSelectedcountries().add(countrieslist.get((Integer) key));
}
if (getSelectedcountries() != null && !getSelectedcountries().isEmpty()) {
for (Countries myc : getSelectedcountries())
System.out.println("forloop: " + myc.getCountries());
}
else
System.out.println("getSelectedCountries is empty");
}
return null;
}
public List<Countries> getSelectedcountries() {
if (selectedcountries != null && !selectedcountries.isEmpty()) {
for (Countries myc : selectedcountries)
System.out.println("getSelectedcountries forloop: " + myc.getCountries());
}
else
System.out.println("getSelectedCountries is empty");
return selectedcountries;
}
public void setSelectedcountries(List<Countries> selectedcountries) {
this.selectedcountries = selectedcountries;
}
@Remove
public void destroy() {}
}
toursmanageCountries.xhtml
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition 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"
template="layout/template.xhtml">
<style>
.active-row {
background-color: #FFEBDA;
}
</style>
<ui:define name="body">
<rich:panel>
<f:facet name="header">toursmanageCountries</f:facet>
<h:form id="tourmanageCountriesForm">
<s:validateAll>
Region: <h:selectOneMenu value="#{countries.regions}" required="true">
<s:selectItems var="_regions" value="#{tregionslist}" label="#{_regions.regions}" noSelectionLabel="Please Select"/>
<s:convertEntity/>
</h:selectOneMenu><br/>
Country Name: <h:inputText value="#{countries.countries}" required="true" /><br/>
ISO Code: <h:inputText value="#{countries.isocode}" required="true" /><br/>
Description: <h:inputText value="#{countries.description}" /><br/>
<h:commandButton id="toursmanageCountries" value="Add Country"
action="#{toursmanageCountries.addCountry}"/>
</s:validateAll>
</h:form>
<hr/>
</rich:panel>
<h:form>
<script type="text/javascript">
var row;
</script>
<rich:contextMenu attached="false" id="menu" submitMode="ajax" oncollapse="row.style.backgroundColor='#{a4jSkin.tableBackgroundColor}'">
<rich:menuItem value="Edit Record" ajaxSingle="true"
oncomplete="#{rich:component('editPanel')}.show()"
actionListener="#{toursmanageCountries.fetchCurrentRow}">
<a4j:actionparam name="cid" value="{Id}" />
<a4j:actionparam name="row" value="{currentRow}" />
</rich:menuItem>
<rich:menuItem value="Remove Record" ajaxSingle="true"
oncomplete="#{rich:component('deletePanel')}.show()"
actionListener="#{toursmanageCountries.fetchCurrentRow}">
<a4j:actionparam name="cid" value="{Id}" />
<a4j:actionparam name="row" value="{currentRow}" />
</rich:menuItem>
</rich:contextMenu>
<a4j:region>
<rich:dataTable value="#{tcountrieslist}"
var="record" rowKeyVar="row"
ajaxKeys="#{toursmanageCountries.keys}" id="table"
height="200px"
onRowContextMenu="if (row) row.style.backgroundColor='#{a4jSkin.tableBackgroundColor}';
this.style.backgroundColor='#F1F1F1'; row=this;
#{rich:component('menu')}.show(event,{Id:'#{record.id}', currentRow:'#{row}'});
return false;">
<f:facet name="header">
<h:outputText value="Edit/Remove Countries" />
</f:facet>
<rich:column>
<f:facet name="header">
<h:outputText value="Country" />
</f:facet>
<h:outputText value="#{record.countries}" id="countries" />
</rich:column>
<rich:column>
<f:facet name="header">
<h:outputText value="Region" />
</f:facet>
<h:outputText value="#{record.regions.regions}" id="regions" />
</rich:column>
<rich:column>
<f:facet name="header">
<h:outputText value="ISOCode" />
</f:facet>
<h:outputText value="#{record.isocode}" id="isocode" />
</rich:column>
<rich:column>
<f:facet name="header">
Actions
</f:facet>
<a4j:commandLink ajaxSingle="true" id="editlink"
oncomplete="#{rich:component('editPanel')}.show()">
<h:graphicImage value="/img/icons/edit.gif" style="border:0" />
<f:setPropertyActionListener value="#{record}"
target="#{toursmanageCountries.currentItem}" />
<f:setPropertyActionListener value="#{row}"
target="#{toursmanageCountries.currentRow}" />
</a4j:commandLink>
<rich:toolTip for="editlink" value="Edit" />
<a4j:commandLink ajaxSingle="true" id="deletelink"
oncomplete="#{rich:component('deletePanel')}.show()">
<h:graphicImage value="/img/icons/delete.gif" style="border:0" />
<f:setPropertyActionListener value="#{row}"
target="#{toursmanageCountries.currentRow}" />
</a4j:commandLink>
<rich:toolTip for="deletelink" value="Delete" />
</rich:column>
<f:facet name="footer">
<rich:datascroller renderIfSinglePage="false" maxPages="10" />
</f:facet>
</rich:dataTable>
</a4j:region>
</h:form>
<rich:modalPanel id="editPanel" autosized="true" width="450">
<f:facet name="header">
<h:outputText value="Edit Current Car" />
</f:facet>
<f:facet name="controls">
<h:panelGroup>
<h:graphicImage value="/img/modal/close.png" id="hidelink"
styleClass="hidelink" />
<rich:componentControl for="editPanel" attachTo="hidelink"
operation="hide" event="onclick" />
</h:panelGroup>
</f:facet>
<h:form>
<rich:messages style="color:red;"></rich:messages>
<h:panelGrid columns="1">
<a4j:outputPanel ajaxRendered="true">
<h:panelGrid columns="2">
<h:outputText value="Country Name" />
<h:inputText value="#{toursmanageCountries.currentItem.countries}" />
<h:outputText value="Region" />
<h:selectOneMenu value="#{toursmanageCountries.currentItem.regions}">
<s:selectItems var="_regions" value="#{tregionslist}" label="#{_regions.regions}" noSelectionLabel="Please Select"/>
<s:convertEntity/>
</h:selectOneMenu>
<h:outputText value="ISO Code" />
<h:inputText value="#{toursmanageCountries.currentItem.isocode}" />
<h:outputText value="Description" />
<h:inputTextarea value="#{toursmanageCountries.currentItem.description}" rows="5" cols="45" />
</h:panelGrid>
<rich:message showSummary="true" showDetail="false" for="price" />
</a4j:outputPanel>
<a4j:commandButton value="Update Country"
action="#{toursmanageCountries.modifyCountry}"
reRender="countries, region, isocode"
oncomplete="if (#{facesContext.maximumSeverity==null}) #{rich:component('editPanel')}.hide();" />
</h:panelGrid>
</h:form>
</rich:modalPanel>
<rich:modalPanel id="deletePanel" autosized="true" width="200">
<f:facet name="header">
<h:outputText value="Delete this country from list?"
style="padding-right:15px;" />
</f:facet>
<f:facet name="controls">
<h:panelGroup>
<h:graphicImage value="/img/modal/close.png"
styleClass="hidelink" id="hidelink2" />
<rich:componentControl for="deletePanel" attachTo="hidelink2"
operation="hide" event="onclick" />
</h:panelGroup>
</f:facet>
<h:form>
<table width="100%">
<tbody>
<tr>
<td align="center" width="50%"><a4j:commandButton value="Yes"
ajaxSingle="true" action="#{toursmanageCountries.removeCountry}"
oncomplete="#{rich:component('deletePanel')}.hide();"
reRender="table" /></td>
<td align="center" width="50%"><a4j:commandButton
value="Cancel"
onclick="#{rich:component('deletePanel')}.hide();return false;" />
</td>
</tr>
</tbody>
</table>
</h:form>
</rich:modalPanel>
<a4j:status onstart="#{rich:component('wait')}.show()"
onstop="#{rich:component('wait')}.hide()" />
<rich:modalPanel id="wait" autosized="true" width="200" height="120"
moveable="false" resizeable="false">
<f:facet name="header">
<h:outputText value="Processing" />
</f:facet>
<h:outputText value="Wait Please..." />
</rich:modalPanel>
<rich:messages></rich:messages>
</ui:define>
</ui:composition>
As you can see the selectOneMenu is pretty simple as it relates to the regions:
Region: <h:selectOneMenu value="#{countries.regions}" required="true">
<s:selectItems var="_regions" value="#{tregionslist}" label="#{_regions.regions}" noSelectionLabel="Please Select"/>
<s:convertEntity/>
</h:selectOneMenu><br/>
The HTML Outputted is incorrect as the values are incremented from 0 to 10 as such:
Region:
<select name="tourmanageCountriesForm:j_id17" size="1"> <option value="org.jboss.seam.ui.NoSelectionConverter.noSelectionValue">Please Select</option>
<option value="0">Africa</option>
<option value="1" selected="selected">Antarctica</option>
<option value="2">Asia</option>
<option value="3">Central America</option>
<option value="4">Eastern Europe</option>
<option value="5">Europe</option>
<option value="6">Middle East</option>
<option value="7">North America</option>
<option value="8">Scandinavia</option>
<option value="9">South America</option>
<option value="10">South Pacific</option>
</select><br />
It should have values of :
11 - Africa
12 - Antarctica
13 - Asia
14 - Central America
15 - Eastern Europe
21 - Europe
16 - Middle East
17 - North America
18 - Scandinavia
19 - South America
20 - South Pacific
Obviously this creates a problem where I get a 'value is not valid' error.
Could some one please help me out or point me to the right direction. Thank you.