Guest User

Untitled

a guest
Oct 17th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. import javax.faces.application.FacesMessage;
  2. import javax.faces.component.UIComponent;
  3. import javax.faces.context.FacesContext;
  4. import javax.faces.convert.Converter;
  5. import javax.faces.convert.ConverterException;
  6.  
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.stereotype.Component;
  9.  
  10. import com.jedlab.framework.spring.SpringUtil;
  11. import com.webportal.app.domain.TagEntity;
  12. import com.webportal.app.service.TagService;
  13.  
  14. /**
  15. * @author Omid Pourhadi
  16. *
  17. */
  18. @Component("tagConverter")
  19. public class TagConverter implements Converter
  20. {
  21.  
  22. @Autowired
  23. TagService tagService;
  24.  
  25.  
  26.  
  27. @Override
  28. public Object getAsObject(FacesContext context, UIComponent component, String value)
  29. {
  30. if (value == null || value.trim().equals("") || value.equals(SpringUtil.getMessage("Please_Select", null)))
  31. {
  32. return null;
  33. }
  34. else
  35. {
  36. try
  37. {
  38. Long tagId = Long.valueOf(value);
  39. return tagService.findById(TagEntity.class, tagId);
  40. }
  41. catch (NumberFormatException exception)
  42. {
  43. throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Conversion Error", "Not a valid Group"));
  44. }
  45. }
  46. }
  47.  
  48. @Override
  49. public String getAsString(FacesContext context, UIComponent component, Object value)
  50. {
  51. if (value instanceof TagEntity)
  52. {
  53. Long id = ((TagEntity) value).getId();
  54. if (id == null)
  55. return null;
  56. else
  57. {
  58. return id.toString();
  59. }
  60.  
  61. }
  62. else
  63. {
  64. if(value != null)
  65. return String.valueOf(value);
  66. return null;
  67. }
  68. }
  69.  
  70. }
Add Comment
Please, Sign In to add comment