Example usage for org.apache.commons.beanutils.converters IntegerConverter IntegerConverter

List of usage examples for org.apache.commons.beanutils.converters IntegerConverter IntegerConverter

Introduction

In this page you can find the example usage for org.apache.commons.beanutils.converters IntegerConverter IntegerConverter.

Prototype

public IntegerConverter(Object defaultValue) 

Source Link

Document

Create a Converter that will return the specified default value if a conversion error occurs.

Usage

From source file:com.tecapro.inventory.common.util.BeanUtil.java

/**
 * (non-Javadoc)/* ww  w.  jav a 2 s.c o  m*/
 * @see org.apache.commons.beanutils.BeanUtilsBean.populate(Object bean, Map properties) 
 *              throws IllegalAccessException, InvocationTargetException
 *
 * @param input
 * @param bean
 */
@SuppressWarnings("unchecked")
public void populate(Object input, Object bean) {
    try {
        beanUtil.setConvertUtil(convertUtil);
        beanUtil.setItemPropUtil(itemPropUtil);

        DoubleConverter dConverter = new DoubleConverter(null);
        ShortConverter sConverter = new ShortConverter(null);
        FloatConverter fConverter = new FloatConverter(null);
        LongConverter lConverter = new LongConverter(null);
        IntegerConverter iConverter = new IntegerConverter(null);

        beanUtil.getConvertUtils().register(dConverter, Double.class);
        beanUtil.getConvertUtils().register(sConverter, Short.class);
        beanUtil.getConvertUtils().register(fConverter, Float.class);
        beanUtil.getConvertUtils().register(lConverter, Long.class);
        beanUtil.getConvertUtils().register(iConverter, Integer.class);

        beanUtil.populate(bean, (Map<String, Object>) input);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

}

From source file:com.algoTrader.PropertySearch.java

/**
 * Initializes the converters to behave the way we want when converting values (we don't
 * want empty strings converted to zeros, like beanutils does by default)
 *//*  w  w w . j av a2  s .  c om*/
private void initializeConverters() {
    ConvertUtils.register(new LongConverter(null), Long.class);
    ConvertUtils.register(new IntegerConverter(null), Integer.class);
    ConvertUtils.register(new ShortConverter(null), Short.class);
    ConvertUtils.register(new CalendarConverter(), Calendar.class);
    ConvertUtils.register(new DateConverter(), Date.class);
}

From source file:com.sunchenbin.store.feilong.core.bean.ConvertUtil.java

/**
 * object?integer.//from w ww  .  jav a2s  . c  om
 * 
 * <p>
 * converted is missing or an error occurs converting the value,<span style="color:red">return null</span>
 * </p>
 *
 * @param toBeConvertedValue
 *            
 * @return the integer
 * @see org.apache.commons.beanutils.converters.IntegerConverter
 */
public static Integer toInteger(Object toBeConvertedValue) {
    IntegerConverter integerConverter = new IntegerConverter(null);
    return integerConverter.convert(Integer.class, toBeConvertedValue);
}

From source file:com.feilong.core.bean.ConvertUtil.java

/**
 *  <code>toBeConvertedValue</code> ?? {@link Integer},?? <code>defaultValue</code>.
 * /* w  w  w  .ja  v a2s  .  c  o m*/
 * <h3>:</h3>
 * 
 * <blockquote>
 * 
 * <pre class="code">
 * 
 * ConvertUtil.toInteger(null,1)                  = 1
 * ConvertUtil.toInteger("aaaa",1)                = 1
 * ConvertUtil.toInteger(8L,1)                    = 8
 * ConvertUtil.toInteger("8",1)                   = 8
 * ConvertUtil.toInteger(new BigDecimal("8"),1)   = 8
 * </pre>
 * 
 * </blockquote>
 * 
 * <p>
 * ???
 * </p>
 * 
 * <h3>:</h3>
 * 
 * <blockquote>
 * 
 * ?:
 * 
 * <pre class="code">
 * 
 * public static Integer getCurrentPageNo(HttpServletRequest request,String pageParamName){
 *     String pageNoString = RequestUtil.getParameter(request, pageParamName);
 *     try{
 *         int pageNo = Integer.parseInt(pageNoString);
 *         return pageNo;
 *     }catch (Exception e){
 *         LOGGER.error(e.getClass().getName(), e);
 *     }
 *     return 1; // ?? ? 1
 * }
 * 
 * </pre>
 * 
 * ??:
 * 
 * <pre class="code">
 * 
 * public static Integer getCurrentPageNo(HttpServletRequest request,String pageParamName){
 *     String pageNoString = RequestUtil.getParameter(request, pageParamName);
 *     return ConvertUtil.toInteger(pageNoString, 1);
 * }
 * </pre>
 * 
 * </blockquote>
 *
 * @param toBeConvertedValue
 *            
 * @param defaultValue
 *            
 * @return  <code>toBeConvertedValue</code> null, <code>defaultValue</code> <br>
 *         ???, <code>defaultValue</code>
 * @see org.apache.commons.beanutils.converters.IntegerConverter
 * @see org.apache.commons.lang3.ObjectUtils#defaultIfNull(Object, Object)
 * @since 1.6.1
 */
public static Integer toInteger(Object toBeConvertedValue, Integer defaultValue) {
    return new IntegerConverter(defaultValue).convert(Integer.class, toBeConvertedValue);
}

From source file:org.andromda.cartridges.database.DatabaseTemplateObject.java

/**
 * Creates a new DatabaseTemplateObject object.
 *///  w ww. j a  va 2  s  .com
public DatabaseTemplateObject() {

    // initialize converters we're using since we don't want to default to 0
    ConvertUtils.register(new LongConverter(null), java.lang.Long.class);
    ConvertUtils.register(new IntegerConverter(null), java.lang.Integer.class);
    ConvertUtils.register(new ShortConverter(null), java.lang.Short.class);

}

From source file:org.apache.struts.action.ActionServlet.java

/**
 * <p>Initialize other global characteristics of the controller
 * servlet.</p>/* www  .  j a  v a 2 s  . c o  m*/
 *
 * @throws ServletException if we cannot initialize these resources
 */
protected void initOther() throws ServletException {
    String value;

    value = getServletConfig().getInitParameter("config");

    if (value != null) {
        config = value;
    }

    // Backwards compatibility for form beans of Java wrapper classes
    // Set to true for strict Struts 1.0 compatibility
    value = getServletConfig().getInitParameter("convertNull");

    if ("true".equalsIgnoreCase(value) || "yes".equalsIgnoreCase(value) || "on".equalsIgnoreCase(value)
            || "y".equalsIgnoreCase(value) || "1".equalsIgnoreCase(value)) {
        convertNull = true;
    }

    if (convertNull) {
        ConvertUtils.deregister();
        ConvertUtils.register(new BigDecimalConverter(null), BigDecimal.class);
        ConvertUtils.register(new BigIntegerConverter(null), BigInteger.class);
        ConvertUtils.register(new BooleanConverter(null), Boolean.class);
        ConvertUtils.register(new ByteConverter(null), Byte.class);
        ConvertUtils.register(new CharacterConverter(null), Character.class);
        ConvertUtils.register(new DoubleConverter(null), Double.class);
        ConvertUtils.register(new FloatConverter(null), Float.class);
        ConvertUtils.register(new IntegerConverter(null), Integer.class);
        ConvertUtils.register(new LongConverter(null), Long.class);
        ConvertUtils.register(new ShortConverter(null), Short.class);
    }
}

From source file:org.beanfuse.struts2.action.helper.ParamHelper.java

public static void registerConverter() {
    ConvertUtils.register(new SqlDateConverter(), java.sql.Date.class);
    ConvertUtils.register(new DateConverter(), java.util.Date.class);
    ConvertUtils.register(new BooleanConverter(null), Boolean.class);
    ConvertUtils.register(new IntegerConverter(null), Integer.class);
    ConvertUtils.register(new LongConverter(null), Long.class);
    ConvertUtils.register(new FloatConverter(null), Float.class);
    ConvertUtils.register(new DoubleConverter(null), Double.class);
}

From source file:org.beangle.commons.converters.Converter.java

public static final ConvertUtilsBean getDefault() {
    ConvertUtilsBean convertUtils = new ConvertUtilsBean();
    convertUtils.register(new SqlDateConverter(), java.sql.Date.class);
    convertUtils.register(new DateConverter(), java.util.Date.class);
    convertUtils.register(new BooleanConverter(null), Boolean.class);
    convertUtils.register(new IntegerConverter(null), Integer.class);
    convertUtils.register(new LongConverter(null), Long.class);
    convertUtils.register(new FloatConverter(null), Float.class);
    convertUtils.register(new DoubleConverter(null), Double.class);
    return convertUtils;
}

From source file:org.ikasan.configurationService.service.ConfiguredResourceConfigurationService.java

/**
 * Constructor//from   www  . jav a 2 s. co m
 * 
 * @param staticConfigurationDao - used to update configuration outside a runtime transaction
 * @param dynamicConfigurationDao - used to update configuration at runtime within a transaction
 */
public ConfiguredResourceConfigurationService(ConfigurationDao staticConfigurationDao,
        ConfigurationDao dynamicConfigurationDao) {
    this.staticConfigurationDao = staticConfigurationDao;
    if (staticConfigurationDao == null) {
        throw new IllegalArgumentException("configurationDao cannot be 'null'");
    }
    this.dynamicConfigurationDao = dynamicConfigurationDao;
    if (dynamicConfigurationDao == null) {
        throw new IllegalArgumentException("dynamicConfigurationDao cannot be 'null'");
    }
    // override some default converters to ensure null is default assignments
    ConvertUtils.register(new IntegerConverter(null), Integer.class);
    ConvertUtils.register(new LongConverter(null), Long.class);
}

From source file:org.itracker.web.actions.project.AssignIssueAction.java

public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {

    ActionMessages errors = new ActionMessages();

    try {//from w  w w.java  2  s .c o  m
        IssueService issueService = ServletContextUtils.getItrackerServices().getIssueService();
        ProjectService projectService = ServletContextUtils.getItrackerServices().getProjectService();

        Integer defaultValue = -1;
        IntegerConverter converter = new IntegerConverter(defaultValue);
        Integer issueId = (Integer) converter.convert(Integer.class,
                (String) PropertyUtils.getSimpleProperty(form, "issueId"));
        Integer projectId = (Integer) converter.convert(Integer.class,
                (String) PropertyUtils.getSimpleProperty(form, "projectId"));
        Integer userId = (Integer) converter.convert(Integer.class,
                (String) PropertyUtils.getSimpleProperty(form, "userId"));

        HttpSession session = request.getSession(true);
        User currUser = (User) session.getAttribute(Constants.USER_KEY);
        Map<Integer, Set<PermissionType>> userPermissions = RequestHelper.getUserPermissions(session);
        Integer currUserId = currUser.getId();

        Project project = projectService.getProject(projectId);
        if (project == null) {
            return mapping.findForward("unauthorized");
        }

        if (!userId.equals(currUserId) && !UserUtilities.hasPermission(userPermissions, projectId,
                UserUtilities.PERMISSION_ASSIGN_OTHERS)) {
            return mapping.findForward("unauthorized");
        } else if (!UserUtilities.hasPermission(userPermissions, projectId,
                UserUtilities.PERMISSION_ASSIGN_SELF)) {
            return mapping.findForward("unauthorized");
        }

        if (project.getStatus() != Status.ACTIVE) {
            errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.projectlocked"));
        } else {
            issueService.assignIssue(issueId, userId, currUserId);
        }
    } catch (RuntimeException e) {
        log.warn("execute: caught exception", e);
        errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.system"));
    } catch (IllegalAccessException e) {
        log.warn("execute: caught exception", e);
        errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.system"));
    } catch (InvocationTargetException e) {
        log.warn("execute: caught exception", e);
        errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.system"));
    } catch (NoSuchMethodException e) {
        log.warn("execute: caught exception", e);
        errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.system"));
    }

    if (!errors.isEmpty()) {
        saveErrors(request, errors);
        saveToken(request);
    }
    return mapping.findForward("index");
}