Last active
January 7, 2018 17:17
-
-
Save BenArunski/8e8183113744f258915fcfb2203cf7f4 to your computer and use it in GitHub Desktop.
JSF Lookup SelectItem cheat sheet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package us.ne.state.nsc.npacs.lookupbean; | |
import org.springframework.data.jpa.repository.JpaRepository; | |
import us.ne.state.nsc.npacs.lookup.NpacsLookup; | |
import javax.faces.application.FacesMessage; | |
import javax.faces.component.UIComponent; | |
import javax.faces.context.FacesContext; | |
import javax.faces.convert.Converter; | |
import javax.faces.convert.ConverterException; | |
import static org.apache.commons.lang.StringUtils.isNotEmpty; | |
public abstract class LookupConverterTemplate implements Converter { | |
/** | |
* subclass should provide repo, probably as a ManagedProperty | |
*/ | |
protected JpaRepository<? extends NpacsLookup, Integer> repo; | |
public void setRepo(JpaRepository<? extends NpacsLookup, Integer> repo) { | |
this.repo = repo; | |
} | |
@Override | |
public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String value) { | |
if (isNotEmpty(value)) { | |
try { | |
int id = Integer.valueOf(value); | |
return repo.findOne(id); | |
} catch (NumberFormatException e) { | |
throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Conversion Error", "Not a valid Lookup.")); | |
} | |
} else { | |
return null; | |
} | |
} | |
@Override | |
public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object object) { | |
if (object != null && NpacsLookup.class.isAssignableFrom(object.getClass())) { | |
NpacsLookup lookup = (NpacsLookup) object; | |
Object id = lookup.getLookupCd(); | |
if(id == null) | |
id = lookup.getStrLookupCd(); | |
return String.valueOf(id); | |
} else { | |
return null; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protected List<SelectItem> getSelectItemObjectList(List<? extends NpacsLookup> lookupList) { | |
return getSelectItemObjectList(lookupList, true, false, true, true, true); | |
} | |
protected List<SelectItem> getSelectItemList(List<? extends NpacsLookup> lookupList, boolean toIncludeBlankEntry, | |
boolean toAbbreviateDescription, boolean activeOnly, boolean toSort, boolean useObjects) { | |
List<SelectItem> result = new ArrayList<>(); | |
for (NpacsLookup lookup : lookupList) { | |
if (activeOnly && !lookup.getActive()) | |
continue; | |
if(lookup.getLookupCd() == null && lookup.getStrLookupCd() == null) | |
continue; | |
boolean isIntegerLookupCd = lookup.getLookupCd() != null; | |
if (toIncludeBlankEntry) { | |
toIncludeBlankEntry = false; | |
Object emptyValue; | |
if (useObjects || lookup instanceof AssessmentLookup) { | |
emptyValue = null; | |
} else if (isIntegerLookupCd ) { | |
emptyValue = 0; | |
} else { | |
emptyValue = ""; | |
} | |
result.add(new SelectItem(emptyValue, "")); | |
} | |
String description = lookup.getDescription(); | |
if (toAbbreviateDescription) { | |
description = StringUtils.abbreviate(description, 40); | |
} | |
Object lookupCd = isIntegerLookupCd ? lookup.getLookupCd() : lookup.getStrLookupCd(); | |
Object selectItemValue = useObjects ? lookup : lookupCd; | |
result.add(new SelectItem(selectItemValue, description)); | |
} | |
if (toSort) { | |
sort(result, "label", true); | |
} | |
return result; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Entity | |
@Table(name = "victim") | |
public class Victim extends Npacs { | |
private VictimRaceEthnicityLookup victimRaceEthnicityLookup; | |
@ManyToOne(targetEntity = VictimRaceEthnicityLookup.class, fetch = FetchType.LAZY) | |
@JoinColumn(name = "fk_Victim_Race_Ethnicity_Cd") | |
public VictimRaceEthnicityLookup getVictimRaceEthnicityLookup() { | |
return victimRaceEthnicityLookup; | |
} | |
public void setVictimRaceEthnicityLookup(VictimRaceEthnicityLookup victimRaceEthnicityLookup) { | |
this.victimRaceEthnicityLookup = victimRaceEthnicityLookup; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@ManagedBean | |
@SessionScoped | |
public class VictimBean extends NpacsPageBean { | |
@ManagedProperty(value = "#{lookupService}") | |
private ILookupService lookupService; | |
private Victim victim; | |
private List<SelectItem> victimRaceEthnicityList; | |
public void buildSelectItems() { | |
if(victimRaceEthnicityList == null) { | |
victimRaceEthnicityList = getSelectItemObjectList(lookupService.getVictimRaceEthnicityLookupList()); | |
} | |
} | |
public List<SelectItem> getVictimRaceEthnicityList() { | |
return victimRaceEthnicityList; | |
} | |
public void setVictimRaceEthnicityList(List<SelectItem> victimRaceEthnicityList) { | |
this.victimRaceEthnicityList = victimRaceEthnicityList; | |
} | |
public Victim getVictim() { | |
return victim; | |
} | |
public void setVictim(Victim victim) { | |
this.victim = victim; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
<!-- Notice that the value attribute is a business object type, not an Integer (ID) | |
type. This REQUIRES a converter. It also means we don't have to worry about | |
keeping an ID in synch with a @Transient description anymore. | |
Tried adding separate getter/setters to Victim, one for the Lookup object and | |
one for its foreign key value, but Hibernate REFUSES to update both in the same | |
transaction. | |
Notice that the converter attribute uses #{} syntax instead of the typical "" | |
because this converter is a ManagedBean (necessary for ManagedProperty injection) | |
--> | |
<h:panelGroup rendered="#{victimBean.displayVictimRaceEthnicity}"> | |
<h:selectOneMenu id="victimRaceEthnicity" required="true" requiredMessage="#{npacsMessages.requiredEntry}" | |
value="#{victimBean.victim.victimRaceEthnicityLookup}" | |
converter="#{victimRaceEthnicityConverter}"> | |
<f:selectItems id="victimRaceEthnicityList" value="#{victimBean.victimRaceEthnicityList}"/> | |
</h:selectOneMenu> <h:message for="victimRaceEthnicity" styleClass="errorMessage"/> | |
</h:panelGroup> | |
... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package us.ne.state.nsc.npacs.lookupbean; | |
import us.ne.state.nsc.npacs.court.VictimRaceEthnicityLookupRepo; | |
import javax.faces.bean.ManagedBean; | |
import javax.faces.bean.ManagedProperty; | |
import javax.faces.bean.SessionScoped; | |
@ManagedBean(name = "victimRaceEthnicityConverter") | |
@SessionScoped | |
public class VictimRaceEthnicityConverter extends LookupConverterTemplate { | |
@ManagedProperty(value = "#{victimRaceEthnicityLookupRepo}") | |
private VictimRaceEthnicityLookupRepo repo; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Entity | |
public class VictimRaceEthnicityLookup extends NpacsLookup { | |
private static final long serialVersionUID = 1L; | |
public VictimRaceEthnicityLookup() { | |
super(); | |
} | |
public VictimRaceEthnicityLookup(Integer lookupCd, String description) { | |
super(lookupCd, description); | |
} | |
@Id | |
@Column(name = "pk_Victim_Race_Ethnicity_Cd") | |
@GeneratedValue(strategy = GenerationType.IDENTITY) | |
@Override | |
public Integer getLookupCd() { | |
return lookupCd; | |
} | |
@Column(name = "victim_Race_Ethnicity_Descr") | |
public String getDescription() { | |
return description; | |
} | |
// MUST OVERRIDE EQUALS AND HASHCODE so that JSF can secretly track the list in the background (security measure) | |
@Override | |
public boolean equals(Object object) { | |
return (object instanceof VictimRaceEthnicityLookup) && (lookupCd != null) | |
? lookupCd.equals(((VictimRaceEthnicityLookup) object).lookupCd) | |
: (object == this); | |
} | |
@Override | |
public int hashCode() { | |
return (lookupCd != null) | |
? (VictimRaceEthnicityLookup.class.hashCode() + lookupCd.hashCode()) | |
: super.hashCode(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment