Friday, August 31, 2012

JSF2 Byte Boolean Checkbox

A few days ago I needed to convert old Icefaces 1.8.2 (JSF1.2) project to Icefaces 3.1 (JSF2) and found a problem when converting Byte to Boolean for the checkboxes. Byte value was generated by Hibernate from database and that is the model that already exists so we really cannot change that. The option with JSF1.2 was to implement Byte to Boolean converter and override rendering kit to call it. JSF2 does not allow this anymore or it is not working (expression library only coerces String to Boolean). After some research, the only option was to automatically generate Boolean getters and setters for existing files. Since there is quite a bit of them spread through different projects, I needed an utility to do this. I ended up writing a small Ruby application for this job. Here is the code:

insert = "
      public void set_r1__b(Boolean _r2_){
        this._r2_ = _r2_!=null&&_r2_?new Byte(\"1\"):new Byte(\"0\");  

      public Boolean get_r1__b(){
        return new Byte(\"1\").equals(this._r2_);


  puts "Processing #{file}"
  f =,"r")
  lines = f.readlines

  result = []
  temp_file_buffer = []

  lines.each { |line| 
    m = /\s*private Byte (is.*)\;.*/.match(line) 
    if m then
      result << m[1]
    temp_file_buffer << line

  #remove last }
  temp_file_buffer.reverse_each { |line|
      if /\}\s*$/.match(line) then
        #better option would be to delete just 1 character } to avoid deleting }} or something else

  if result.length>0 then
    fw =,".rpl"),"w")


    result.each { |rl|
      upper = rl.dup
      lower = rl.dup
      upper[0] = upper[0].capitalize
      fw << insert.gsub(/_r1_/,upper).gsub(/_r2_/,lower)

    #insert closing bracket
    fw << "}\n"



This is by no means perfect code, but it did the job. If you have a better suggestion, please let me know!


No comments:

Post a Comment