Search in sources :

Example 6 with DynaBean

use of org.apache.commons.beanutils.DynaBean in project sonarqube by SonarSource.

the class FormBeanConfig method canReuse.

/**
     * <p>Checks if the given <code>ActionForm</code> instance is suitable for
     * use as an alternative to calling this <code>FormBeanConfig</code>
     * instance's <code>createActionForm</code> method.</p>
     *
     * @param form an existing form instance that may be reused.
     * @return true if the given form can be reused as the form for this
     *         config.
     */
public boolean canReuse(ActionForm form) {
    if (form != null) {
        if (this.getDynamic()) {
            String className = ((DynaBean) form).getDynaClass().getName();
            if (className.equals(this.getName())) {
                log.debug("Can reuse existing instance (dynamic)");
                return (true);
            }
        } else {
            try {
                // check if the form's class is compatible with the class
                //      we're configured for
                Class formClass = form.getClass();
                if (form instanceof BeanValidatorForm) {
                    BeanValidatorForm beanValidatorForm = (BeanValidatorForm) form;
                    if (beanValidatorForm.getInstance() instanceof DynaBean) {
                        String formName = beanValidatorForm.getStrutsConfigFormName();
                        if (getName().equals(formName)) {
                            log.debug("Can reuse existing instance (BeanValidatorForm)");
                            return true;
                        } else {
                            return false;
                        }
                    }
                    formClass = beanValidatorForm.getInstance().getClass();
                }
                Class configClass = ClassUtils.getApplicationClass(this.getType());
                if (configClass.isAssignableFrom(formClass)) {
                    log.debug("Can reuse existing instance (non-dynamic)");
                    return (true);
                }
            } catch (Exception e) {
                log.debug("Error testing existing instance for reusability; just create a new instance", e);
            }
        }
    }
    return false;
}
Also used : DynaBean(org.apache.commons.beanutils.DynaBean) DynaActionFormClass(org.apache.struts.action.DynaActionFormClass) MutableDynaClass(org.apache.commons.beanutils.MutableDynaClass) BeanValidatorForm(org.apache.struts.validator.BeanValidatorForm) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 7 with DynaBean

use of org.apache.commons.beanutils.DynaBean in project jaffa-framework by jaffa-projects.

the class ExcelExportService method generateExcel.

public static Workbook generateExcel(QueryServiceConfig master, QueryServiceConfig child, String sheetName) throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
    Workbook wb = null;
    String legacyExport = (String) ContextManagerFactory.instance().getProperty("jaffa.widgets.exportToExcel.legacy");
    if (legacyExport != null && legacyExport.equals("T")) {
        wb = new HSSFWorkbook();
    } else {
        wb = new SXSSFWorkbook(100);
    }
    try {
        // Creating worksheet
        Sheet sheet = null;
        if (sheetName != null) {
            if (sheetName.length() > 31)
                sheetName = sheetName.substring(0, 31);
            char replaceChar = '_';
            sheetName = sheetName.replace('\u0003', replaceChar).replace(':', replaceChar).replace('/', replaceChar).replace("\\\\", Character.toString(replaceChar)).replace('?', replaceChar).replace('*', replaceChar).replace(']', replaceChar).replace('[', replaceChar);
            sheet = wb.createSheet(sheetName);
        } else
            sheet = wb.createSheet();
        // creating a custom palette for the workbook
        CellStyle style = wb.createCellStyle();
        style = wb.createCellStyle();
        // setting the foreground color to gray
        style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
        style.setFillPattern(CellStyle.SOLID_FOREGROUND);
        // Setting the border for the cells
        style.setBorderBottom(CellStyle.BORDER_THIN);
        style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
        style.setBorderLeft(CellStyle.BORDER_THIN);
        style.setLeftBorderColor(IndexedColors.GREEN.getIndex());
        style.setBorderRight(CellStyle.BORDER_THIN);
        style.setRightBorderColor(IndexedColors.BLACK.getIndex());
        style.setBorderTop(CellStyle.BORDER_THIN);
        style.setTopBorderColor(IndexedColors.BLACK.getIndex());
        style.setAlignment(CellStyle.ALIGN_CENTER);
        // setting font weight
        Font titleFont = wb.createFont();
        titleFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
        style.setFont(titleFont);
        int rowNum = 0;
        Row headerRow = sheet.createRow(rowNum);
        int colIndex = 0;
        for (Object o : master.getColumnModel()) {
            String columnTitle = (String) ((DynaBean) o).get("header");
            if (columnTitle == null || columnTitle.length() == 0)
                columnTitle = (String) ((DynaBean) o).get("mapping");
            headerRow.createCell(colIndex).setCellValue(columnTitle);
            Cell cell = headerRow.getCell(colIndex);
            cell.setCellStyle(style);
            sheet.autoSizeColumn(colIndex);
            colIndex += 1;
        }
        // Generate the Excel output by creating a simple HTML table
        if (child != null) {
            for (Object o : child.getColumnModel()) {
                String columnTitle = (String) ((DynaBean) o).get("header");
                if (columnTitle == null || columnTitle.length() == 0)
                    columnTitle = (String) ((DynaBean) o).get("mapping");
                headerRow.createCell(colIndex).setCellValue(columnTitle);
                Cell cell = headerRow.getCell(colIndex);
                cell.setCellStyle(style);
                sheet.autoSizeColumn(colIndex);
                colIndex += 1;
            }
        }
        // Invoke the query and obtain an array of Graph objects
        Object[] queryOutput = invokeQueryService(master.getCriteriaClassName(), master.getCriteriaObject(), master.getServiceClassName(), master.getServiceClassMethodName());
        // Add the data rows
        if (queryOutput != null) {
            for (Object row : queryOutput) {
                Object[] detailQueryOutput = new Object[0];
                if (child == null) {
                    rowNum += 1;
                    Row dataRow = sheet.createRow((short) rowNum);
                    int colNum = 0;
                    // extract the columns from master object
                    for (Object o : master.getColumnModel()) {
                        String mapping = (String) ((DynaBean) o).get("mapping");
                        Object value = null;
                        try {
                            value = PropertyUtils.getProperty(row, mapping);
                        } catch (Exception e) {
                            if (log.isDebugEnabled())
                                log.debug("Property not found: " + mapping, e);
                        }
                        dataRow.createCell(colNum).setCellValue(format(value, (DynaBean) o));
                        colNum += 1;
                    }
                } else {
                    // child is not null
                    // load the child rows
                    String detailCriteriaObject = child.getCriteriaObject();
                    for (int i = 0; i < child.getMasterKeyFieldNames().length; i++) {
                        String kfn = child.getMasterKeyFieldNames()[i];
                        try {
                            String keyValue = (String) PropertyUtils.getProperty(row, kfn);
                            detailCriteriaObject = detailCriteriaObject.replace("{" + i + "}", keyValue);
                        } catch (Exception e) {
                            if (log.isDebugEnabled())
                                log.debug("Key property not found: " + kfn, e);
                        }
                    }
                    detailQueryOutput = invokeQueryService(child.getCriteriaClassName(), detailCriteriaObject, child.getServiceClassName(), "query");
                    // add the child columns
                    if (detailQueryOutput != null && detailQueryOutput.length > 0) {
                        for (Object detailRow : detailQueryOutput) {
                            rowNum += 1;
                            Row dataRow = sheet.createRow((short) rowNum);
                            int colNum = 0;
                            // extract the columns from master object
                            for (Object obj : master.getColumnModel()) {
                                String masterMapping = (String) ((DynaBean) obj).get("mapping");
                                Object masterValue = null;
                                try {
                                    masterValue = PropertyUtils.getProperty(row, masterMapping);
                                } catch (Exception e) {
                                    if (log.isDebugEnabled())
                                        log.debug("Property not found: " + masterMapping, e);
                                }
                                dataRow.createCell(colNum).setCellValue(format(masterValue, (DynaBean) obj));
                                colNum += 1;
                            }
                            for (Object o : child.getColumnModel()) {
                                String mapping = (String) ((DynaBean) o).get("mapping");
                                Object value = null;
                                try {
                                    value = PropertyUtils.getProperty(detailRow, mapping);
                                } catch (Exception e) {
                                    if (log.isDebugEnabled())
                                        log.debug("Property not found in child result: " + mapping, e);
                                }
                                dataRow.createCell(colNum).setCellValue(format(value, (DynaBean) o));
                                colNum += 1;
                            }
                        }
                    }
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return wb;
}
Also used : SXSSFWorkbook(org.apache.poi.xssf.streaming.SXSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) Font(org.apache.poi.ss.usermodel.Font) InvocationTargetException(java.lang.reflect.InvocationTargetException) DynaBean(org.apache.commons.beanutils.DynaBean) SXSSFWorkbook(org.apache.poi.xssf.streaming.SXSSFWorkbook) JSONObject(net.sf.json.JSONObject) CellStyle(org.apache.poi.ss.usermodel.CellStyle) Row(org.apache.poi.ss.usermodel.Row) Sheet(org.apache.poi.ss.usermodel.Sheet) Cell(org.apache.poi.ss.usermodel.Cell)

Example 8 with DynaBean

use of org.apache.commons.beanutils.DynaBean in project sonar-java by SonarSource.

the class FormBeanConfig method createActionForm.

// --------------------------------------------------------- Public Methods
/**
 * <p>Create and return an <code>ActionForm</code> instance appropriate to
 * the information in this <code>FormBeanConfig</code>.</p>
 *
 * <p>Although this method is not formally deprecated yet, where possible,
 * the form which accepts an <code>ActionContext</code> as an argument is
 * preferred, to help sever direct dependencies on the Servlet API.  As
 * the ActionContext becomes more familiar in Struts, this method will
 * almost certainly be deprecated.</p>
 *
 * @param servlet The action servlet
 * @return ActionForm instance
 * @throws IllegalAccessException if the Class or the appropriate
 *                                constructor is not accessible
 * @throws InstantiationException if this Class represents an abstract
 *                                class, an array class, a primitive type,
 *                                or void; or if instantiation fails for
 *                                some other reason
 */
public ActionForm createActionForm(ActionServlet servlet) throws IllegalAccessException, InstantiationException {
    Object obj = null;
    // Create a new form bean instance
    if (getDynamic()) {
        obj = getDynaActionFormClass().newInstance();
    } else {
        obj = formBeanClass().newInstance();
    }
    ActionForm form = null;
    if (obj instanceof ActionForm) {
        form = (ActionForm) obj;
    } else {
        form = new BeanValidatorForm(obj);
    }
    form.setServlet(servlet);
    if (form instanceof DynaBean && ((DynaBean) form).getDynaClass() instanceof MutableDynaClass) {
        DynaBean dynaBean = (DynaBean) form;
        MutableDynaClass dynaClass = (MutableDynaClass) dynaBean.getDynaClass();
        // Add properties
        dynaClass.setRestricted(false);
        FormPropertyConfig[] props = findFormPropertyConfigs();
        for (int i = 0; i < props.length; i++) {
            dynaClass.add(props[i].getName(), props[i].getTypeClass());
            dynaBean.set(props[i].getName(), props[i].initial());
        }
        dynaClass.setRestricted(isRestricted());
    }
    if (form instanceof BeanValidatorForm) {
        ((BeanValidatorForm) form).initialize(this);
    }
    return form;
}
Also used : DynaBean(org.apache.commons.beanutils.DynaBean) ActionForm(org.apache.struts.action.ActionForm) DynaActionForm(org.apache.struts.action.DynaActionForm) BeanValidatorForm(org.apache.struts.validator.BeanValidatorForm) MutableDynaClass(org.apache.commons.beanutils.MutableDynaClass)

Example 9 with DynaBean

use of org.apache.commons.beanutils.DynaBean in project nimbus by nimbus-org.

the class DynaBeanJournalEditorService method processBlock.

protected boolean processBlock(EditorFinder finder, Object key, Object value, StringBuilder buf) {
    final DynaBean bean = (DynaBean) value;
    boolean isMake = false;
    if (isOutputDynaClass()) {
        makeDynaClassFormat(finder, key, bean, buf);
        isMake = true;
    }
    if (isOutputProperties()) {
        if (isMake) {
            buf.append(getLineSeparator());
        }
        makePropertiesFormat(finder, key, bean, buf);
        isMake = true;
    }
    return isMake;
}
Also used : DynaBean(org.apache.commons.beanutils.DynaBean)

Aggregations

DynaBean (org.apache.commons.beanutils.DynaBean)9 InvocationTargetException (java.lang.reflect.InvocationTargetException)5 MutableDynaClass (org.apache.commons.beanutils.MutableDynaClass)4 BeanValidatorForm (org.apache.struts.validator.BeanValidatorForm)4 JSONObject (net.sf.json.JSONObject)2 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)2 Cell (org.apache.poi.ss.usermodel.Cell)2 CellStyle (org.apache.poi.ss.usermodel.CellStyle)2 Font (org.apache.poi.ss.usermodel.Font)2 Row (org.apache.poi.ss.usermodel.Row)2 Sheet (org.apache.poi.ss.usermodel.Sheet)2 Workbook (org.apache.poi.ss.usermodel.Workbook)2 SXSSFWorkbook (org.apache.poi.xssf.streaming.SXSSFWorkbook)2 ActionForm (org.apache.struts.action.ActionForm)2 DynaActionForm (org.apache.struts.action.DynaActionForm)2 DynaActionFormClass (org.apache.struts.action.DynaActionFormClass)2 BeanInfo (java.beans.BeanInfo)1 IntrospectionException (java.beans.IntrospectionException)1 PropertyDescriptor (java.beans.PropertyDescriptor)1 Method (java.lang.reflect.Method)1