Example usage for org.apache.commons.beanutils PropertyUtils getMappedProperty

List of usage examples for org.apache.commons.beanutils PropertyUtils getMappedProperty

Introduction

In this page you can find the example usage for org.apache.commons.beanutils PropertyUtils getMappedProperty.

Prototype

public static Object getMappedProperty(Object bean, String name, String key)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException 

Source Link

Document

Return the value of the specified mapped property of the specified bean, with no type conversions.

For more details see PropertyUtilsBean.

Usage

From source file:org.broadleafcommerce.core.search.service.solr.I18nSolrSearchServiceExtensionHandler.java

@Override
public ExtensionResultStatusType addPropertyValues(Product product, Field field, FieldType fieldType,
        Map<String, Object> values, String propertyName, List<Locale> locales)
        throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {

    Set<String> processedLocaleCodes = new HashSet<String>();

    ExtensionResultStatusType result = ExtensionResultStatusType.NOT_HANDLED;
    if (field.getTranslatable()) {
        result = ExtensionResultStatusType.HANDLED;

        for (Locale locale : locales) {
            String localeCode = locale.getLocaleCode();
            if (!Boolean.TRUE.equals(locale.getUseCountryInSearchIndex())) {
                int pos = localeCode.indexOf("_");
                if (pos > 0) {
                    localeCode = localeCode.substring(0, pos);
                    if (processedLocaleCodes.contains(localeCode)) {
                        continue;
                    } else {
                        locale = localeService.findLocaleByCode(localeCode);
                    }/*from   ww  w  .ja  v a  2  s.c  o m*/
                }
            }

            processedLocaleCodes.add(localeCode);

            TranslationConsiderationContext.setTranslationConsiderationContext(getTranslationEnabled());
            TranslationConsiderationContext.setTranslationService(translationService);
            BroadleafRequestContext tempContext = BroadleafRequestContext.getBroadleafRequestContext();
            if (tempContext == null) {
                tempContext = new BroadleafRequestContext();
            }
            tempContext.setLocale(locale);
            BroadleafRequestContext.setBroadleafRequestContext(tempContext);

            final Object propertyValue;
            if (propertyName.contains(ATTR_MAP)) {
                propertyValue = PropertyUtils.getMappedProperty(product, ATTR_MAP,
                        propertyName.substring(ATTR_MAP.length() + 1));
            } else {
                propertyValue = PropertyUtils.getProperty(product, propertyName);
            }
            values.put(localeCode, propertyValue);
        }
    }
    return result;
}

From source file:org.oddjob.framework.WrapDynaBean.java

/**
 * Return the value of a mapped property with the specified name,
 * or <code>null</code> if there is no value for the specified key.
 *
 * @param name Name of the property whose value is to be retrieved
 * @param key Key of the value to be retrieved
 *
 * @exception IllegalArgumentException if there is no property
 *  of the specified name//from ww  w  . j a  v  a2  s  . c o  m
 * @exception IllegalArgumentException if the specified property
 *  exists, but is not mapped
 */
public Object get(String name, String key) {

    if (!dynaClass.isReadable(name)) {
        return null;
    }

    Object value = null;
    try {
        value = PropertyUtils.getMappedProperty(instance, name, key);
    } catch (Throwable t) {
        throw new IllegalArgumentException("Property '" + name + "' has no mapped read method");
    }
    return (value);

}

From source file:org.rti.zcore.dar.remote.ReportHelper.java

/**
 * Update report value and saves report to an Excel file.
 * @param identifier//  ww  w . j a  v a 2  s  .com
 * @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;
}