List of usage examples for org.apache.commons.beanutils PropertyUtils setMappedProperty
public static void setMappedProperty(Object bean, String name, String key, Object value) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException
Sets the value of the specified mapped property of the specified bean, with no type conversions.
For more details see PropertyUtilsBean
.
From source file:TasteOfThingsV1.java
public static void main(String args[]) throws Exception { prepareData();/*from w ww . j av a2 s . c o m*/ HashBag myBag = new HashBag(testMap.values()); System.err.println("How many Boxes? " + myBag.getCount("Boxes")); myBag.add("Boxes", 5); System.err.println("How many Boxes now? " + myBag.getCount("Boxes")); Method method = testBean.getClass().getDeclaredMethod("getTestMap", new Class[0]); HashMap reflectionMap = (HashMap)method.invoke(testBean, new Object[0]); System.err.println("The value of the 'squ' key using reflection: " + reflectionMap.get("squ")); String squ = BeanUtils.getMappedProperty(testBean, "testMap", "squ"); squ = StringUtils.capitalize(squ); PropertyUtils.setMappedProperty(testBean, "testMap", "squ", squ); System.err.println("The value of the 'squ' key is: " + BeanUtils.getMappedProperty(testBean, "testMap", "squ")); String box = (String)testMap.get("box"); String caps = Character.toTitleCase(box.charAt(0)) + box.substring(1, box.length()); System.err.println("Capitalizing boxes by Java: " + caps); }
From source file:org.apache.torque.dsfactory.AbstractDataSourceFactory.java
/** * Encapsulates setting configuration properties on * <code>DataSource</code> objects. * * @param property the property to read from the configuration * @param c the configuration to read the property from * @param ds the <code>DataSource</code> instance to write the property to * @throws Exception if anything goes wrong */// www .j a va2 s . c om protected void setProperty(String property, Configuration c, Object ds) throws Exception { if (c == null || c.isEmpty()) { return; } String key = property; Class<?> dsClass = ds.getClass(); int dot = property.indexOf('.'); try { if (dot > 0) { property = property.substring(0, dot); MappedPropertyDescriptor mappedPD = new MappedPropertyDescriptor(property, dsClass); Class<?> propertyType = mappedPD.getMappedPropertyType(); Configuration subProps = c.subset(property); // use reflection to set properties Iterator<?> j = subProps.getKeys(); while (j.hasNext()) { String subProp = (String) j.next(); String propVal = subProps.getString(subProp); Object value = ConvertUtils.convert(propVal, propertyType); PropertyUtils.setMappedProperty(ds, property, subProp, value); if (log.isDebugEnabled()) { log.debug( "setMappedProperty(" + ds + ", " + property + ", " + subProp + ", " + value + ")"); } } } else { if ("password".equals(key)) { // do not log value of password // for this, ConvertUtils.convert cannot be used // as it also logs the value of the converted property // so it is assumed here that the password is a String String value = c.getString(property); PropertyUtils.setSimpleProperty(ds, property, value); if (log.isDebugEnabled()) { log.debug("setSimpleProperty(" + ds + ", " + property + ", " + " (value not logged)" + ")"); } } else { Class<?> propertyType = PropertyUtils.getPropertyType(ds, property); Object value = ConvertUtils.convert(c.getString(property), propertyType); PropertyUtils.setSimpleProperty(ds, property, value); if (log.isDebugEnabled()) { log.debug("setSimpleProperty(" + ds + ", " + property + ", " + value + ")"); } } } } catch (RuntimeException e) { throw new TorqueRuntimeException("Runtime error setting property " + property, e); } catch (Exception e) { log.error("Property: " + property + " value: " + c.getString(key) + " is not supported by DataSource: " + ds.getClass().getName()); } }
From source file:org.oddjob.framework.WrapDynaBean.java
/** * Set the value of a mapped property with the specified name. * * @param name Name of the property whose value is to be set * @param key Key of the property to be set * @param value Value to which this property is to be set * * @exception ConversionException if the specified value cannot be * converted to the type required for this property * @exception IllegalArgumentException if there is no property * of the specified name//from w ww .j ava 2 s .co m * @exception IllegalArgumentException if the specified property * exists, but is not mapped */ public void set(String name, String key, Object value) { try { PropertyUtils.setMappedProperty(instance, name, key, value); } catch (Throwable t) { throw new IllegalArgumentException("Property '" + name + "' has no mapped write method"); } }
From source file:org.rti.zcore.dar.remote.ReportHelper.java
/** * Update report value and saves report to an Excel file. * @param identifier/*from ww w. j a v a2s .co m*/ * @param reportName * @param value * @param isFacilityReport * @return */ public static String updateReport(String identifier, String reportName, String value, Boolean isFacilityReport) throws FileNotFoundException { String result = ""; WebContext exec = WebContextFactory.get(); String username = null; SessionUtil zeprs_session = null; Site site = null; String siteAbbrev = null; try { username = exec.getHttpServletRequest().getUserPrincipal().getName(); } catch (NullPointerException e) { // unit testing - it's ok... username = "demo"; } HttpSession session = exec.getSession(); try { zeprs_session = (SessionUtil) session.getAttribute("zeprs_session"); } catch (Exception e) { // unit testing - it's ok... } try { ClientSettings clientSettings = zeprs_session.getClientSettings(); site = clientSettings.getSite(); siteAbbrev = site.getAbbreviation(); } catch (SessionUtil.AttributeNotFoundException e) { log.error(e); } catch (NullPointerException e) { // it's ok - unit testing siteAbbrev = "test"; } String parentField = null; String childField = null; if (identifier.equals("Save") || identifier.equals("SaveNext")) { } else { String[] identArray = identifier.split("\\."); parentField = identArray[0]; childField = identArray[1]; } Register report = null; String className = "org.rti.zcore.dar.report." + StringManipulation.fixClassname(reportName); Class clazz = null; try { clazz = Class.forName(className); } catch (ClassNotFoundException e) { log.error(e); } try { report = (Register) clazz.newInstance(); } catch (InstantiationException e) { log.error(e); } catch (IllegalAccessException e) { log.error(e); } if (identifier.equals("Save") || identifier.equals("SaveNext") || value != null) { Integer valueInt = null; report = SessionUtil.getInstance(session).getReports().get(reportName); String totalIdent = ""; String reportFileName = report.getReportFileName(); String pathXml = Constants.ARCHIVE_PATH + site.getAbbreviation() + "/reports/" + reportFileName + ".xml"; String pathExcel = Constants.ARCHIVE_PATH + site.getAbbreviation() + "/reports/" + reportFileName + ".xls"; String bdate = report.getBeginDate().toString(); String edate = report.getEndDate().toString(); String siteId = String.valueOf(report.getSiteId()); String jsessionId = session.getId(); String[] identifierArray = identifier.split("\\."); int len = identifierArray.length; String keyForMap = null; try { if (identifier.equals("Save")) { result = "Report saved at " + pathExcel; } else if (identifier.equals("SaveNext")) { String reportId = null; if (reportName.equals("CDRRArtReport")) { if ((isFacilityReport != null) && (isFacilityReport == Boolean.TRUE)) { reportId = "7"; result = "/dar/ChooseReportAction.do;jsessionid=" + jsessionId + "?bdate=" + bdate + "&edate=" + edate + "&siteId=" + siteId + "&report=" + reportId + "&isCombinedReport=1&isFacilityReport=1"; } else { // Skipping CDRROIReport reportId = "6"; result = "/dar/ChooseReportAction.do;jsessionid=" + jsessionId + "?bdate=" + bdate + "&edate=" + edate + "&siteId=" + siteId + "&report=" + reportId + "&isCombinedReport=1"; } } else if (reportName.equals("CDRROIReport")) { reportId = "6"; result = "/dar/ChooseReportAction.do;jsessionid=" + jsessionId + "?bdate=" + bdate + "&edate=" + edate + "&siteId=" + siteId + "&report=" + reportId + "&isCombinedReport=1&isFacilityReport=1"; } else { if ((isFacilityReport != null) && (isFacilityReport == Boolean.TRUE)) { result = "/dar/reports/combined/gen.do;jsessionid=" + jsessionId + "?isFacilityReport=1"; } else { result = "/dar/reports/combined/gen.do;jsessionid=" + jsessionId; } } } else { try { valueInt = Integer.valueOf(value); } catch (NumberFormatException e) { try { throw new PersistenceException( "This input field requires an integer value (e.g.: 55). You entered : " + value, e, false); } catch (PersistenceException e1) { return result = identifier + "=" + "Error:" + e1.getMessage(); } } if (identifier.contains("regimenReportMap")) { keyForMap = identifierArray[len - 1]; // should be newEstimatedArtPatients. String parentObjectName = identifier .replace("." + identifierArray[len - 2] + "." + keyForMap, ""); try { Object parent = null; parent = PropertyUtils.getNestedProperty(report, parentObjectName); PropertyUtils.setMappedProperty(parent, "regimenReportMap", keyForMap, valueInt); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } else if (identifier.contains("stockReportMap")) { keyForMap = identifierArray[len - 2]; //String propertyName = identifierArray[len-1]; // parentObjectName should be stockReportMap //String parentObjectName = identifier.replace("." + keyForMap + "." + childField, ""); try { PropertyUtils.setNestedProperty(report, identifier, valueInt); //Object parent = PropertyUtils.getNestedProperty(report, parentObjectName); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } else { PropertyUtils.setProperty(report, identifier, valueInt); } //valueInt = (Integer) PropertyUtils.getProperty(report, identifier); /*HashMap<String, Integer> regimenReportMap = report.get regimenReportMap.put(key, amount);*/ // CDRR (Stock) Reports if (parentField.equals("stockReportMap")) { String propertyField = identifierArray[2]; if (propertyField.equals("quantityRequiredNewPatients")) { String quantityRequiredResupplyIdent = identifier.replace(propertyField, "quantityRequiredResupply"); Integer quantityRequiredResupply = 0; if (PropertyUtils.getNestedProperty(report, quantityRequiredResupplyIdent) != null) { quantityRequiredResupply = (Integer) PropertyUtils.getNestedProperty(report, quantityRequiredResupplyIdent); } Integer totalQuantityRequired = quantityRequiredResupply + valueInt; String totalQuantityRequiredIdent = identifier.replace(propertyField, "totalQuantityRequired"); try { PropertyUtils.setNestedProperty(report, totalQuantityRequiredIdent, valueInt); } catch (Exception e) { e.printStackTrace(); } result = identifier + "=" + valueInt + ";" + totalQuantityRequiredIdent + "=" + totalQuantityRequired; } else { result = "0=0;" + identifier + "=" + valueInt; } // Regimen report } else if (parentField.equals("newEstimatedArtPatients")) { //String quantityRequiredResupplyIdent = "artRegimenReport." + childField; Integer quantityRequiredResupply = 0; /*if (PropertyUtils.getProperty(report, quantityRequiredResupplyIdent) != null) { quantityRequiredResupply = (Integer) PropertyUtils.getProperty(report, quantityRequiredResupplyIdent); }*/ if (PropertyUtils.getMappedProperty(report, "regimenReportMap", keyForMap) != null) { //value = regimenReportMap.get("regimen" + regimenCode); quantityRequiredResupply = (Integer) PropertyUtils.getMappedProperty(report, "regimenReportMap", keyForMap); } totalIdent = "totalEstimatedArtPatients." + childField + "." + keyForMap; Integer totalQuantityRequired = quantityRequiredResupply + valueInt; //String parentObjectName = identifier.replace("." + identifierArray[len-2] + "." + keyForMap, ""); //Object parent = PropertyUtils.getNestedProperty(report, parentObjectName); Object parent = PropertyUtils.getNestedProperty(report, "totalEstimatedArtPatients"); PropertyUtils.setMappedProperty(parent, "regimenReportMap", keyForMap, totalQuantityRequired); //PropertyUtils.setProperty(report, totalIdent, totalQuantityRequired); result = identifier + "=" + valueInt + ";" + totalIdent + "=" + totalQuantityRequired; } else if (parentField.equals("totalQuantityRequired")) { result = "0=0;" + identifier + "=" + valueInt; } } try { ReportOutput.outputReport(reportName, report, clazz, pathXml, pathExcel, null); } catch (FileNotFoundException e) { result = identifier + "=" + valueInt + ";" + totalIdent + "=" + "Error: the Excel file for this report is open. Please close."; } catch (IOException e) { log.debug(e); } catch (WrappedRuntimeException e) { log.error(e); //e.printStackTrace(); } catch (TransformerException e) { log.error(e); } } catch (IllegalAccessException e) { log.debug(e); } catch (InvocationTargetException e) { log.debug(e); } catch (NoSuchMethodException e) { log.debug(e); } //BeanUtils.setProperty(parent, childField, value); } else { result = identifier + "=" + "Error: No value entered."; } return result; }