Created
December 28, 2017 22:57
-
-
Save anonymous/b8ec5c28ef851429c192255f532ddabe to your computer and use it in GitHub Desktop.
This file contains 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 br.com.oftalmoprev.bean; | |
import java.io.Serializable; | |
import java.util.List; | |
import javax.annotation.PostConstruct; | |
import javax.faces.application.FacesMessage; | |
import javax.faces.context.FacesContext; | |
import javax.faces.view.ViewScoped; | |
import javax.inject.Inject; | |
import javax.inject.Named; | |
import org.primefaces.context.RequestContext; | |
import br.com.oftalmoprev.control.util.MessageUtil; | |
import br.com.oftalmoprev.model.Brand; | |
import br.com.oftalmoprev.service.BrandService; | |
@Named | |
@ViewScoped | |
public class BrandBean implements Serializable { | |
private static final long serialVersionUID = 9204486797916777869L; | |
@Inject | |
private BrandService brandService; | |
private Brand brand; | |
private List<Brand> brands; | |
@PostConstruct | |
public void init() { | |
clean(); | |
} | |
public void save() { | |
FacesContext ctx = FacesContext.getCurrentInstance(); | |
RequestContext req = RequestContext.getCurrentInstance(); | |
if (validate(ctx)) { | |
String message = ""; | |
try { | |
if (brand.getPk() == null) { | |
brandService.add(brand); | |
message = MessageUtil.getMessage("brand.add.success"); | |
} else { | |
brandService.edit(brand); | |
message = MessageUtil.getMessage("brand.edit.success"); | |
} | |
refreshBrands(); | |
ctx.addMessage("globalMessages", new FacesMessage(FacesMessage.SEVERITY_INFO, | |
MessageUtil.getMessage("global.success"), message)); | |
req.execute("PF('brandDialog').hide()"); | |
req.update("brandTableForm"); | |
req.update("globalMessages"); | |
} catch (Exception e) { | |
sendGenericErrorMessage(); | |
} | |
} | |
} | |
public void showAddDialog() { | |
clean(); | |
RequestContext req = RequestContext.getCurrentInstance(); | |
req.execute("PF('brandDialog').show()"); | |
req.update("brandForm"); | |
} | |
public void showEditDialog(Brand b) { | |
brand = b; | |
RequestContext req = RequestContext.getCurrentInstance(); | |
req.execute("PF('brandDialog').show()"); | |
req.update("brandForm"); | |
} | |
public void clean() { | |
brand = new Brand(); | |
} | |
public void refreshBrands() { | |
brands = brandService.getAll(); | |
} | |
public boolean validate(FacesContext ctx) { | |
boolean valid = true; | |
brand.setName(brand.getName().trim()); | |
if (brand.getName().isEmpty()) { | |
ctx.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, | |
MessageUtil.getMessage("global.error"), | |
MessageUtil.getMessage("brand.name.required"))); | |
valid = false; | |
} | |
return valid; | |
} | |
public void inactivate(Brand brand) { | |
try { | |
brandService.inactivate(brand); | |
} catch (Exception e) { | |
sendGenericErrorMessage(); | |
} | |
} | |
public void activate(Brand brand) { | |
try { | |
brandService.activate(brand); | |
} catch (Exception e) { | |
sendGenericErrorMessage(); | |
} | |
} | |
public void sendGenericErrorMessage() { | |
FacesContext ctx = FacesContext.getCurrentInstance(); | |
ctx.addMessage("globalMessages", new FacesMessage(FacesMessage.SEVERITY_ERROR, | |
MessageUtil.getMessage("global.error"), | |
MessageUtil.getMessage("error.message.support"))); | |
} | |
public Brand getBrand() { | |
return brand; | |
} | |
public List<Brand> getAllBrands() { | |
if (brands == null) { | |
refreshBrands(); | |
} | |
return brands; | |
} | |
public List<Brand> getAllActiveBrands() { | |
return brandService.getAllActive(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment