br.com.jarch.faces.ui.AddressUI Maven / Gradle / Ivy
package br.com.jarch.faces.ui;
import br.com.jarch.util.br.CepBean;
import br.com.jarch.util.br.CepService;
import br.com.jarch.core.model.IAddress;
import javax.enterprise.inject.spi.CDI;
import javax.faces.component.FacesComponent;
import javax.faces.component.UIInput;
import javax.faces.component.UINamingContainer;
import javax.faces.component.UISelectOne;
import javax.faces.context.FacesContext;
import java.io.IOException;
@FacesComponent("br.com.jarch.faces.ui.AddressUI")
public class AddressUI extends UIInput implements IAddress {
private UIInput zipCode;
private UIInput publicPlace;
private UIInput numberPublicPlace;
private UIInput complement;
private UIInput district;
private UIInput county;
private UIInput state;
private UISelectOne stateCombo;
@Override
public String getFamily() {
return UINamingContainer.COMPONENT_FAMILY;
}
@Override
public UIInput getZipCode() {
return zipCode;
}
@Override
public void setZipCode(UIInput zipCode) {
this.zipCode = zipCode;
}
@Override
public UIInput getPublicPlace() {
return publicPlace;
}
@Override
public void setPublicPlace(UIInput publicPlace) {
this.publicPlace = publicPlace;
}
@Override
public UIInput getNumberPublicPlace() {
return numberPublicPlace;
}
@Override
public void setNumberPublicPlace(UIInput numberPublicPlace) {
this.numberPublicPlace = numberPublicPlace;
}
@Override
public UIInput getComplement() {
return complement;
}
@Override
public void setComplement(UIInput complement) {
this.complement = complement;
}
@Override
public UIInput getDistrict() {
return district;
}
@Override
public void setDistrict(UIInput district) {
this.district = district;
}
@Override
public UIInput getCounty() {
return county;
}
@Override
public void setCounty(UIInput county) {
this.county = county;
}
@Override
public UIInput getState() {
return state;
}
@Override
public void setState(UIInput state) {
this.state = state;
}
public UISelectOne getStateCombo() {
return stateCombo;
}
public void setStateCombo(UISelectOne stateCombo) {
this.stateCombo = stateCombo;
}
public void searchCep() {
if (zipCode.getSubmittedValue() == null)
return;
CepBean cepBean = CDI.current().select(CepService.class).get().buscaCep(zipCode.getSubmittedValue().toString());
zipCode.setValue(cepBean.getCep());
publicPlace.setValue(cepBean.getLogradouro());
// numberPublicPlace.setValue("");
complement.setValue("");
district.setValue(cepBean.getBairro());
county.setValue(cepBean.getCidade());
state.setValue(cepBean.getEstado());
stateCombo.setValue(cepBean.getEstado());
IAddress value = getAttributeValue("value", null);
if (value == null)
value = new AddressUI();
value.setPublicPlace(publicPlace.getValue());
value.setNumberPublicPlace(numberPublicPlace.getValue());
value.setComplement(complement.getValue());
value.setCounty(county.getValue());
value.setDistrict(district.getValue());
value.setState(state.getValue());
value.setZipCode(zipCode.getValue());
getAttributes().put("value", value);
}
private T getAttributeValue(String key, T defaultValue) {
T value = (T) getAttributes().get(key);
return (value != null) ? value : defaultValue;
}
@Override
public void encodeBegin(FacesContext context) throws IOException {
IAddress value = getAttributeValue("value", null);
if (value == null || value.getZipCode() == null || value.getZipCode().toString().isBlank())
return;
zipCode.setValue(value.getZipCode());
publicPlace.setValue(value.getPublicPlace());
numberPublicPlace.setValue(value.getNumberPublicPlace());
complement.setValue(value.getComplement());
district.setValue(value.getDistrict());
county.setValue(value.getCounty());
state.setValue(value.getState());
stateCombo.setValue(value.getState());
super.encodeBegin(context);
}
@Override
public Object getSubmittedValue() {
IAddress address = (IAddress) getValue();
if (address == null)
return address;
address.setZipCode(zipCode.getValue());
address.setPublicPlace(publicPlace.getValue());
address.setNumberPublicPlace(numberPublicPlace.getValue());
address.setComplement(complement.getValue());
address.setDistrict(district.getValue());
address.setCounty(county.getValue());
address.setState(state.getValue() == null || state.getValue().toString().isEmpty() ? stateCombo.getValue() : state.getValue());
return address;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy