Java tutorial
/* * PayrollObjectUtility.java * * Copyright * (c) 2010 Walgreens Co. * All rights reserved. * * This software is the confidential and proprietary information of * Walgreens Co. ("Confidential Information"). You shall not disclose such * confidential information and shall use it only in accordance with the terms * of the license agreement you entered into with Walgreens Co. * * History: * Dec 01, 2011 acestoya */ package com.roadmap.common.util; import java.lang.reflect.Constructor; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Set; import java.util.Map.Entry; import org.apache.commons.beanutils.PropertyUtils; import org.apache.commons.lang3.StringUtils; import com.roadmap.common.constant.RoadmapConstants; import com.roadmap.vo.KeyValuePairVO; public class ObjectUtil { private static final String BOOLEAN = "boolean"; private static final String INTEGER = "Integer"; private static final String INT = "int"; private static final String LONG = "long"; /** * Return true if the specified property name identifies a readable * property on the specified bean; otherwise, return false. * * @param Object object to be examined * @param String property name to be evaluated * * @return boolean */ @SuppressWarnings("rawtypes") public static boolean isReadable(Object vo, String prop) { boolean retVal = false; try { if (vo instanceof Map) { retVal = vo != null && ((Map) vo).containsValue(prop); } else { retVal = PropertyUtils.isReadable(vo, prop); } } catch (Exception e) { //No need to handle } return retVal; } /** * Return true if the specified property name identifies a readable * property on the specified bean; otherwise, return false. * * @param Object object to be examined * @param String property name to be evaluated * * @return boolean */ @SuppressWarnings("rawtypes") public static boolean isReadable(Object vo, String[] props) { boolean retVal = false; try { for (String string : props) { retVal = PropertyUtils.isReadable(vo, string); if (!retVal) break; } } catch (Exception e) { //No need to handle } return retVal; } /** * Return true if the specified property name identifies a writeable * property on the specified bean; otherwise, return false. * * @param Object object to be examined * @param String property name to be evaluated * * @return boolean */ public static boolean isWriteable(Object vo, String prop) { boolean retVal = false; try { if (vo instanceof Map) { retVal = vo != null; } else { retVal = PropertyUtils.isWriteable(vo, prop); } } catch (Exception e) { //No need to handle } return retVal; } /** * Return the value of the specified simple property of the * specified bean, with no type conversions. * * @param Object object whose property is to be extracted * @param String name of the property to be extracted * * @return Object The property value */ @SuppressWarnings("rawtypes") public static Object getPropertyValue(Object vo, String prop) { Object retVal = null; try { if (vo instanceof Map) { if (vo != null) { retVal = ((Map) vo).get(prop); } } else { retVal = PropertyUtils.getSimpleProperty(vo, prop); } } catch (Exception e) { //No need to handle } return retVal; } /** * Return the value of the specified simple property of the * specified bean, with no type conversions. * * @param Object object whose property is to be extracted * @param String name of the property to be extracted * * @return Object The property value */ @SuppressWarnings("rawtypes") public static Object getConcatenatedPropertyStringValues(Object vo, String[] props, String sep) { Object retVal = null; ArrayList<String> stringList = new ArrayList<String>(); try { if (vo instanceof Map) { if (vo != null) { for (String prop : props) { retVal = ((Map) vo).get(prop); stringList.add(retVal.toString()); } } } else { for (String prop : props) { retVal = PropertyUtils.getSimpleProperty(vo, prop); stringList.add(retVal.toString()); } } } catch (Exception e) { //No need to handle } return StringUtils.join(stringList, sep); } /** * Set the value of the specified property of the specified bean, * no matter which property reference format is used, with no type conversions. * * @param Object object whose property is to be modified * @param String possibly indexed and/or nested name of the property to be modified * @param Object value to which this property is to be set */ @SuppressWarnings(value = { "unchecked", "rawtypes" }) public static void setPropertyValue(Object vo, String prop, Object value) { try { if (vo instanceof Map) { ((Map) vo).put(prop, value); } else { PropertyUtils.setProperty(vo, prop, value); } } catch (IllegalArgumentException iae) { try { Class cl = PropertyUtils.getPropertyType(vo, prop); String clName = cl.getName(); if (LONG.equalsIgnoreCase(clName)) { PropertyUtils.setProperty(vo, prop, Long.valueOf(RoadmapConstants.EMPTY_STRING + value)); } else if (INT.equalsIgnoreCase(clName) || INTEGER.equalsIgnoreCase(clName)) { PropertyUtils.setProperty(vo, prop, Integer.valueOf(RoadmapConstants.EMPTY_STRING + value)); } else if (BOOLEAN.equalsIgnoreCase(clName)) { PropertyUtils.setProperty(vo, prop, Boolean.valueOf(RoadmapConstants.EMPTY_STRING + value)); } else if (value != null && !value.getClass().getName().equals(clName)) { Constructor ctr = cl.getConstructor(value.getClass()); if (ctr != null) { PropertyUtils.setProperty(vo, prop, ctr.newInstance(value)); } } else { PropertyUtils.setProperty(vo, prop, value); } } catch (Exception e1) { // Supress errors // LoggingUtil.logError(logger, // "PayrollObjectUtility.setPropertyValue for object:" // + vo.getClass() + "|property:" + prop // + "|value:" + value, e1); } } catch (Exception e1) { // Supress errors // LoggingUtil.logError(logger, // "PayrollObjectUtility.setPropertyValue for object:" // + vo.getClass() + "|property:" + prop + "|value:" // + value, e1); } } /** * Obtains the JSON string from a map * * @param Map the map where the JSON string is stored * * @return String JSON string */ @SuppressWarnings({ "unchecked", "rawtypes" }) public static String getJSONFromMap(Map<String, List<? extends Object>> reportViewMap) { StringBuilder sb = new StringBuilder(); Set<Entry<String, List<?>>> reportViewSet = reportViewMap.entrySet(); int j = 0; for (Entry<String, List<?>> entry : reportViewSet) { if (j != 0) { sb.append(","); } sb.append("'" + entry.getKey() + "':["); List valList = reportViewMap.get(entry.getKey()); sb.append(getJSONFromList(valList)); sb.append("]"); j++; } return sb.toString(); } /** * Obtains the JSON string from a list * * @param List the list where the JSON string is stored * * @return String JSON string */ public static String getJSONFromList(List<? extends Object> componentList) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < componentList.size(); i++) { if (i != 0) { sb.append(","); } Object obj = componentList.get(i); if (obj instanceof KeyValuePairVO) { KeyValuePairVO vo = (KeyValuePairVO) obj; sb.append("'" + vo.getKey() + "=" + vo.getValue() + "'"); } else if (obj instanceof String) { sb.append("'" + obj + "'"); } } return sb.toString(); } }