Package org.bigk.invoices.converters

Source Code of org.bigk.invoices.converters.DateConverter

package org.bigk.invoices.converters;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.util.StrutsTypeConverter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.conversion.TypeConversionException;

public abstract class DateConverter extends StrutsTypeConverter {

  public static final Logger logger = LoggerFactory.getLogger(DateConverter.class);
 
  @SuppressWarnings("rawtypes")
  @Override
  public Object convertFromString(Map map, String[] as, Class class1) {
    if (logger.isDebugEnabled()) {
      logger.debug("convertFromString(Map map=" + map + ", String[] as=" + as + ", Class class1=" + class1 + ") - start");
    }
   
    if (as == null || as.length != 1)
      return performFallbackConversion(map, as, class1);

    Date res = null;
    String mask = null;
    if (StringUtils.isNotEmpty(as[0])) {
      try {
        ActionSupport action =
          (ActionSupport) ActionContext.getContext().getActionInvocation().getAction();

        // najpierw RegEx
        String regEx = this.getRegEx(action);
        if (StringUtils.isNotEmpty(regEx)) {
          if (!Pattern.matches(regEx, as[0])) {
            logger.warn("convertFromString(Map, String[], Class) - " + as[0] + " nie pasuje do RegEx-a:: " + regEx);
            throw new TypeConversionException(as[0] + " nie pasuje do RegEx-a:: " + regEx);
          }
        }
       
        mask = this.getMask(action);
        DateFormat df = new SimpleDateFormat(mask);
        res = df.parse(as[0]);

        if (logger.isDebugEnabled())
          logger.debug("convertFromString(Map, String[], Class) - " + as[0] + " -> " + res);
       
      } catch (ParseException ex) {
        logger.warn("convertFromString(Map, String[], Class) - " + as[0] + ", mask: " + mask);
        throw new TypeConversionException("No nie da sie tego przekonwertowac ciolku!", ex);
      }
    }

    if (logger.isDebugEnabled()) {
      logger.debug("convertFromString(Map, String[], Class) - end - return value=" + res);
    }
    return res;
  }

  @SuppressWarnings("rawtypes")
  @Override
  public String convertToString(Map map, Object obj) {
    if (logger.isDebugEnabled()) {
      logger.debug("convertToString(Map map=" + map + ", Object obj=" + obj + ") - start");
    }
   
    String res = obj != null ? obj.toString() : StringUtils.EMPTY;
    if (obj != null && obj instanceof Date) {
     
      ActionSupport action =
        (ActionSupport) ActionContext.getContext().getActionInvocation().getAction();

      String mask = this.getMask(action);
      DateFormat df = new SimpleDateFormat(mask);
      res = df.format((Date) obj);
    }
   
    if (logger.isDebugEnabled()) {
      logger.debug("convertToString(Map, Object) - end - return value=" + res);
    }
    return res;
  }
 
  protected abstract String getMask(ActionSupport action);
 
  protected String getRegEx(ActionSupport action) {
    return null;
  }   
}
TOP

Related Classes of org.bigk.invoices.converters.DateConverter

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.