Re: Date Type Converter strange behavior

2021-04-26 Thread M Huzaifah
Hii Yaser,

Thank you for reply. my problem solved by add:

 @TypeConversion(type = ConversionType.APPLICATION, key = "java.util.Date", 
converterClass = DateConverter.class)

Regards


> On 26 Apr 2021, at 14.36, Yasser Zamani  wrote:
> 
> Hi thanks for reaching out!
> 
> I don't remember and should check that your desired behavior used to work or 
> not but as a quick answer I think you're looking for Class wide conversion 
> [1] e.g. @TypeConversion(type = ConversionType.APPLICATION, property = 
> "java.util.Date", DateConverter.class)?
> 
> Regards.
> 
> [1] https://struts.apache.org/core-developers/type-conversion-annotation.html
> 
> On 2021/04/25 20:02:47, M Huzaifah  wrote: 
>> Hy guys,
>> 
>> i found somethin strange with StrutsTypeConverter.
>> let say i have a converter like this:
>> 
>> 
>> //-- begin DateConverter class ---
>> import java.text.ParseException;
>> import java.text.SimpleDateFormat;
>> import java.util.Date;
>> import java.util.Map;
>> 
>> import javax.annotation.PostConstruct;
>> 
>> import org.apache.commons.lang.ArrayUtils;
>> import org.apache.commons.lang.StringUtils;
>> import org.apache.struts2.util.StrutsTypeConverter;
>> 
>> public class DateConverter extends StrutsTypeConverter {
>>  
>>  private  String shortDateFormat;
>>  
>>  @PostConstruct
>>  public void loadFormat() {
>>  this.shortDateFormat = "dd/MM/";
>>  }
>> 
>>  @SuppressWarnings("unused")
>>  @Override
>>  public Object convertFromString(Map arg0, String[] arg1, Class arg2) {
>>  
>>  System.out.println("begin converter");
>> 
>>  if(ArrayUtils.isEmpty(arg1) || StringUtils.isEmpty(arg1[0])) {
>>  return null;
>>  }
>> 
>>  Date date = null;
>>  
>>  try {
>>  //cek kalo formatnya dd/MM/ hh:mm:ss
>>  if(arg1[0].contains(" ")) {
>>  this.shortDateFormat = "dd/MM/ hh:mm:ss";
>>  date = new 
>> SimpleDateFormat(shortDateFormat).parse(arg1[0]);
>>  }else {
>>  date = new 
>> SimpleDateFormat(shortDateFormat).parse(arg1[0]);
>>  }
>>  }catch (ParseException e) {
>>  System.out.println("DateConverter StrutsTypeConverter 
>> gagal parsing date;");
>>  e.printStackTrace();
>>  }
>> 
>>  System.out.println("end converter");
>>  return date;
>>  }
>> 
>>  @Override
>>  public String convertToString(Map arg0, Object arg1) {
>> 
>>  System.out.println("begin converter to string");
>>  if(arg1 instanceof Date) {
>>  System.out.println("end converter to string");
>>  return new 
>> SimpleDateFormat(shortDateFormat).format((Date) arg1);
>>  }
>> 
>>  System.out.println("end converter to string");
>>  return StringUtils.EMPTY;
>>  }
>> 
>>  
>>  
>> }
>> 
>> //-- end DateConverter class ---
>> 
>> 
>> 
>> and in my action class, i have 2 property. one of them is Date type. and 
>> other one is Pojo which one of that Pojo property have a Date type. so my 
>> action look like this
>> 
>> 
>> //-- begin MasterPatienAction class ---
>> 
>> @Namespace("/patient")
>> @ParentPackage("app-default")
>> @Conversion(conversions = {
>>  @TypeConversion(key = "birthDate", converterClass = 
>> DateConverter.class)
>> })
>> public class MasterPatientAction extends BaseAction {
>> 
>>  /**
>>   * 
>>   */
>>  private static final long serialVersionUID = 1L;
>> 
>>  private static transient Logger log = 
>> Logger.getLogger(MasterPatientAction.class);
>>  
>>  
>>  Patient patient;
>>  List listPatient;
>>  Date birthDate;
>>  
>>  @Action(value = "init", results = {
>>  @Result(name = "success", location = 
>> "/pages/pasient-main.jsp"),
>>  @Result(name = "error", location = 
>> "/pages/pasient-main.jsp"),
>>  @Result(name = "input", location = 
>> "/pages/pasient-main.jsp")
>>  })
>>  public String init() {
>>  log.info("begin execute method init");
>>  
>>  log.info("end execute method init");
>>  return SUCCESS;
>>  }
>> 
>>  @Action(value = "search", results = {
>>  @Result(name = "success", location = 
>> "/pages/pasient-main.jsp"),
>>  @Result(name = "error", location = 
>> "/pages/pasient-main.jsp"),
>>  @Result(name = "input", location = 
>> "/pages/pasient-main.jsp")
>>  })
>>  public String search() {
>>  log.info("begin execute method search");
>> 

Re: Date Type Converter strange behavior

2021-04-26 Thread Yasser Zamani
Hi thanks for reaching out!

I don't remember and should check that your desired behavior used to work or 
not but as a quick answer I think you're looking for Class wide conversion [1] 
e.g. @TypeConversion(type = ConversionType.APPLICATION, property = 
"java.util.Date", DateConverter.class)?

Regards.

[1] https://struts.apache.org/core-developers/type-conversion-annotation.html

On 2021/04/25 20:02:47, M Huzaifah  wrote: 
> Hy guys,
> 
> i found somethin strange with StrutsTypeConverter.
> let say i have a converter like this:
> 
> 
> //-- begin DateConverter class ---
> import java.text.ParseException;
> import java.text.SimpleDateFormat;
> import java.util.Date;
> import java.util.Map;
> 
> import javax.annotation.PostConstruct;
> 
> import org.apache.commons.lang.ArrayUtils;
> import org.apache.commons.lang.StringUtils;
> import org.apache.struts2.util.StrutsTypeConverter;
> 
> public class DateConverter extends StrutsTypeConverter {
>   
>   private  String shortDateFormat;
>   
>   @PostConstruct
>   public void loadFormat() {
>   this.shortDateFormat = "dd/MM/";
>   }
> 
>   @SuppressWarnings("unused")
>   @Override
>   public Object convertFromString(Map arg0, String[] arg1, Class arg2) {
>   
>   System.out.println("begin converter");
> 
>   if(ArrayUtils.isEmpty(arg1) || StringUtils.isEmpty(arg1[0])) {
>   return null;
>   }
> 
>   Date date = null;
>   
>   try {
>   //cek kalo formatnya dd/MM/ hh:mm:ss
>   if(arg1[0].contains(" ")) {
>   this.shortDateFormat = "dd/MM/ hh:mm:ss";
>   date = new 
> SimpleDateFormat(shortDateFormat).parse(arg1[0]);
>   }else {
>   date = new 
> SimpleDateFormat(shortDateFormat).parse(arg1[0]);
>   }
>   }catch (ParseException e) {
>   System.out.println("DateConverter StrutsTypeConverter 
> gagal parsing date;");
>   e.printStackTrace();
>   }
> 
>   System.out.println("end converter");
>   return date;
>   }
> 
>   @Override
>   public String convertToString(Map arg0, Object arg1) {
> 
>   System.out.println("begin converter to string");
>   if(arg1 instanceof Date) {
>   System.out.println("end converter to string");
>   return new 
> SimpleDateFormat(shortDateFormat).format((Date) arg1);
>   }
> 
>   System.out.println("end converter to string");
>   return StringUtils.EMPTY;
>   }
> 
>   
>   
> }
> 
> //-- end DateConverter class ---
> 
> 
> 
> and in my action class, i have 2 property. one of them is Date type. and 
> other one is Pojo which one of that Pojo property have a Date type. so my 
> action look like this
> 
> 
> //-- begin MasterPatienAction class ---
> 
> @Namespace("/patient")
> @ParentPackage("app-default")
> @Conversion(conversions = {
>   @TypeConversion(key = "birthDate", converterClass = 
> DateConverter.class)
> })
> public class MasterPatientAction extends BaseAction {
> 
>   /**
>* 
>*/
>   private static final long serialVersionUID = 1L;
> 
>   private static transient Logger log = 
> Logger.getLogger(MasterPatientAction.class);
>   
>   
>   Patient patient;
>   List listPatient;
>   Date birthDate;
>   
>   @Action(value = "init", results = {
>   @Result(name = "success", location = 
> "/pages/pasient-main.jsp"),
>   @Result(name = "error", location = 
> "/pages/pasient-main.jsp"),
>   @Result(name = "input", location = 
> "/pages/pasient-main.jsp")
>   })
>   public String init() {
>   log.info("begin execute method init");
>   
>   log.info("end execute method init");
>   return SUCCESS;
>   }
> 
>   @Action(value = "search", results = {
>   @Result(name = "success", location = 
> "/pages/pasient-main.jsp"),
>   @Result(name = "error", location = 
> "/pages/pasient-main.jsp"),
>   @Result(name = "input", location = 
> "/pages/pasient-main.jsp")
>   })
>   public String search() {
>   log.info("begin execute method search");
>   
>   if(birthDate!=null) {
>   patient.setPatientBirthDate(birthDate);
>   }
>   
>   try {
>   listPasien = service.search(patient);
>   } catch (DnaException e) {
>   setMessageError("error caused

Date Type Converter strange behavior

2021-04-25 Thread M Huzaifah
Hy guys,

i found somethin strange with StrutsTypeConverter.
let say i have a converter like this:


//-- begin DateConverter class ---
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.util.StrutsTypeConverter;

public class DateConverter extends StrutsTypeConverter {

private  String shortDateFormat;

@PostConstruct
public void loadFormat() {
this.shortDateFormat = "dd/MM/";
}

@SuppressWarnings("unused")
@Override
public Object convertFromString(Map arg0, String[] arg1, Class arg2) {

System.out.println("begin converter");

if(ArrayUtils.isEmpty(arg1) || StringUtils.isEmpty(arg1[0])) {
return null;
}

Date date = null;

try {
//cek kalo formatnya dd/MM/ hh:mm:ss
if(arg1[0].contains(" ")) {
this.shortDateFormat = "dd/MM/ hh:mm:ss";
date = new 
SimpleDateFormat(shortDateFormat).parse(arg1[0]);
}else {
date = new 
SimpleDateFormat(shortDateFormat).parse(arg1[0]);
}
}catch (ParseException e) {
System.out.println("DateConverter StrutsTypeConverter 
gagal parsing date;");
e.printStackTrace();
}

System.out.println("end converter");
return date;
}

@Override
public String convertToString(Map arg0, Object arg1) {

System.out.println("begin converter to string");
if(arg1 instanceof Date) {
System.out.println("end converter to string");
return new 
SimpleDateFormat(shortDateFormat).format((Date) arg1);
}

System.out.println("end converter to string");
return StringUtils.EMPTY;
}



}

//-- end DateConverter class ---



and in my action class, i have 2 property. one of them is Date type. and other 
one is Pojo which one of that Pojo property have a Date type. so my action look 
like this


//-- begin MasterPatienAction class ---

@Namespace("/patient")
@ParentPackage("app-default")
@Conversion(conversions = {
@TypeConversion(key = "birthDate", converterClass = 
DateConverter.class)
})
public class MasterPatientAction extends BaseAction {

/**
 * 
 */
private static final long serialVersionUID = 1L;

private static transient Logger log = 
Logger.getLogger(MasterPatientAction.class);


Patient patient;
List listPatient;
Date birthDate;

@Action(value = "init", results = {
@Result(name = "success", location = 
"/pages/pasient-main.jsp"),
@Result(name = "error", location = 
"/pages/pasient-main.jsp"),
@Result(name = "input", location = 
"/pages/pasient-main.jsp")
})
public String init() {
log.info("begin execute method init");

log.info("end execute method init");
return SUCCESS;
}

@Action(value = "search", results = {
@Result(name = "success", location = 
"/pages/pasient-main.jsp"),
@Result(name = "error", location = 
"/pages/pasient-main.jsp"),
@Result(name = "input", location = 
"/pages/pasient-main.jsp")
})
public String search() {
log.info("begin execute method search");

if(birthDate!=null) {
patient.setPatientBirthDate(birthDate);
}

try {
listPasien = service.search(patient);
} catch (DnaException e) {
setMessageError("error caused : "+e.getMessage());
return ERROR;
}

log.info("end execute method search");
return SUCCESS;
}

//--setter getter
}


//-- end MasterPatienAction class ---


let say my pojo like this:


//-- begin Patient pojo class ---

public class Patient {
private integer patientId;
private String patientName;
private String patientCode;
private Date patientBirthDat