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

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

Introduction

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

Prototype

public static void copyProperties(Object dest, Object orig)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException 

Source Link

Document

Copy property values from the "origin" bean to the "destination" bean for all cases where the property names are the same (even though the actual getter and setter methods might have been customized via BeanInfo classes).

For more details see PropertyUtilsBean.

Usage

From source file:us.mn.state.health.lims.action.action.ActionAction.java

protected ActionForward performAction(ActionMapping mapping, ActionForm form, HttpServletRequest request,
        HttpServletResponse response) throws Exception {
    // The first job is to determine if we are coming to this action with an
    // ID parameter in the request. If there is no parameter, we are
    // creating a new Action.
    // If there is a parameter present, we should bring up an existing
    // Action to edit.
    String id = request.getParameter(ID);

    String forward = FWD_SUCCESS;
    request.setAttribute(ALLOW_EDITS_KEY, "true");
    request.setAttribute(PREVIOUS_DISABLED, "true");
    request.setAttribute(NEXT_DISABLED, "true");

    DynaActionForm dynaForm = (DynaActionForm) form;

    // initialize the form
    dynaForm.initialize(mapping);/*from www.j  a  v  a 2 s .co  m*/

    Action action = new Action();

    if ((id != null) && (!"0".equals(id))) { // this is an existing
        // action

        action.setId(id);
        ActionDAO actionDAO = new ActionDAOImpl();
        actionDAO.getData(action);
        isNew = false; // this is to set correct page title

        // do we need to enable next or previous?
        //bugzilla 1427 pass in name not id
        List actions = actionDAO.getNextActionRecord(action.getId());
        if (actions.size() > 0) {
            // enable next button
            request.setAttribute(NEXT_DISABLED, "false");
        }
        //bugzilla 1427 pass in name not id
        actions = actionDAO.getPreviousActionRecord(action.getId());
        if (actions.size() > 0) {
            // enable next button
            request.setAttribute(PREVIOUS_DISABLED, "false");
        }
        // end of logic to enable next or previous button
    } else { // this is a new action
        isNew = true; // this is to set correct page title
    }

    if (action.getId() != null && !action.getId().equals("0")) {
        request.setAttribute(ID, action.getId());
    }

    // populate form from valueholder
    PropertyUtils.copyProperties(form, action);

    return mapping.findForward(forward);
}

From source file:us.mn.state.health.lims.action.action.ActionUpdateAction.java

protected ActionForward performAction(ActionMapping mapping, ActionForm form, HttpServletRequest request,
        HttpServletResponse response) throws Exception {
    // The first job is to determine if we are coming to this action with an
    // ID parameter in the request. If there is no parameter, we are
    // creating a new Action.
    // If there is a parameter present, we should bring up an existing
    // Action to edit.
    String forward = FWD_SUCCESS;
    request.setAttribute(ALLOW_EDITS_KEY, "true");
    request.setAttribute(PREVIOUS_DISABLED, "false");
    request.setAttribute(NEXT_DISABLED, "false");

    String id = request.getParameter(ID);

    if (StringUtil.isNullorNill(id) || "0".equals(id)) {
        isNew = true;/*from   w  w w  .ja  va 2 s. c  o m*/
    } else {
        isNew = false;
    }

    BaseActionForm dynaForm = (BaseActionForm) form;

    // server-side validation (validation.xml)
    ActionMessages errors = dynaForm.validate(mapping, request);
    if (errors != null && errors.size() > 0) {
        saveErrors(request, errors);
        // since we forward to jsp - not Action we don't need to repopulate
        // the lists here
        return mapping.findForward(FWD_FAIL);
    }

    String start = (String) request.getParameter("startingRecNo");
    String direction = (String) request.getParameter("direction");

    // System.out.println("This is ID from request " + id);
    Action action = new Action();
    //get sysUserId from login module
    UserSessionData usd = (UserSessionData) request.getSession().getAttribute(USER_SESSION_DATA);
    String sysUserId = String.valueOf(usd.getSystemUserId());

    action.setSysUserId(sysUserId);
    org.hibernate.Transaction tx = HibernateUtil.getSession().beginTransaction();

    // populate valueholder from form
    PropertyUtils.copyProperties(action, dynaForm);

    try {

        ActionDAO actionDAO = new ActionDAOImpl();

        if (!isNew) {
            // UPDATE

            actionDAO.updateData(action);
        } else {
            // INSERT

            actionDAO.insertData(action);
        }
        tx.commit();
    } catch (LIMSRuntimeException lre) {
        //bugzilla 2154
        LogEvent.logError("ActionUpdateAction", "performAction()", lre.toString());
        tx.rollback();
        errors = new ActionMessages();
        java.util.Locale locale = (java.util.Locale) request.getSession()
                .getAttribute("org.apache.struts.action.LOCALE");
        ActionError error = null;
        if (lre.getException() instanceof org.hibernate.StaleObjectStateException) {
            // how can I get popup instead of struts error at the top of
            // page?
            // ActionMessages errors = dynaForm.validate(mapping, request);
            error = new ActionError("errors.OptimisticLockException", null, null);
        } else {
            // bugzilla 1482
            if (lre.getException() instanceof LIMSDuplicateRecordException) {
                String messageKey = "action.action";
                String msg = ResourceLocator.getInstance().getMessageResources().getMessage(locale, messageKey);
                error = new ActionError("errors.DuplicateRecord", msg, null);

            } else {
                error = new ActionError("errors.UpdateException", null, null);
            }
        }
        errors.add(ActionMessages.GLOBAL_MESSAGE, error);
        saveErrors(request, errors);
        request.setAttribute(Globals.ERROR_KEY, errors);
        //bugzilla 1485: allow change and try updating again (enable save button)
        //request.setAttribute(IActionConstants.ALLOW_EDITS_KEY, "false");
        // disable previous and next
        request.setAttribute(PREVIOUS_DISABLED, "true");
        request.setAttribute(NEXT_DISABLED, "true");
        forward = FWD_FAIL;

    } finally {
        HibernateUtil.closeSession();
    }
    if (forward.equals(FWD_FAIL))
        return mapping.findForward(forward);

    // initialize the form
    dynaForm.initialize(mapping);
    // repopulate the form from valueholder
    PropertyUtils.copyProperties(dynaForm, action);

    if ("true".equalsIgnoreCase(request.getParameter("close"))) {
        forward = FWD_CLOSE;
    }

    if (action.getId() != null && !action.getId().equals("0")) {
        request.setAttribute(ID, action.getId());

    }

    //bugzilla 1400
    if (isNew)
        forward = FWD_SUCCESS_INSERT;
    //bugzilla 1467 added direction for redirect to NextPreviousAction
    return getForward(mapping.findForward(forward), id, start, direction);

}

From source file:us.mn.state.health.lims.action.daoimpl.ActionDAOImpl.java

public void getData(Action action) throws LIMSRuntimeException {
    try {/*w ww . j  a va 2  s  . co  m*/
        Action act = (Action) HibernateUtil.getSession().get(Action.class, action.getId());
        HibernateUtil.getSession().flush();
        HibernateUtil.getSession().clear();
        if (act != null) {
            PropertyUtils.copyProperties(action, act);
        } else {
            action.setId(null);
        }
    } catch (Exception e) {
        //bugzilla 2154
        LogEvent.logError("ActionDAOImpl", "getData()", e.toString());
        throw new LIMSRuntimeException("Error in Action getData()", e);
    }
}

From source file:us.mn.state.health.lims.analysis.action.AnalysisAction.java

protected ActionForward performAction(ActionMapping mapping, ActionForm form, HttpServletRequest request,
        HttpServletResponse response) throws Exception {
    // The first job is to determine if we are coming to this action with an
    // ID parameter in the request. If there is no parameter, we are
    // creating a new Analysis.
    // If there is a parameter present, we should bring up an existing
    // Analysis to edit.

    String id = request.getParameter(ID);

    String forward = "success";
    request.setAttribute(ALLOW_EDITS_KEY, "true");
    request.setAttribute(PREVIOUS_DISABLED, "true");
    request.setAttribute(NEXT_DISABLED, "true");

    BaseActionForm dynaForm = (BaseActionForm) form;

    // initialize the form
    dynaForm.initialize(mapping);/*from  w w  w.  j ava  2  s  .c o m*/

    Analysis analysis = new Analysis();

    if ((id != null) && (!"0".equals(id))) { // this is an existing
        // analysis

        analysis.setId(id);
        AnalysisDAO analysisDAO = new AnalysisDAOImpl();
        analysisDAO.getData(analysis);

        // initialize testSectionName
        if (analysis.getTestSection() != null) {
            analysis.setTestSectionName(analysis.getTestSection().getTestSectionName());
        }

        // initialize testName
        if (analysis.getTest() != null) {
            analysis.setTestName(TestService.getLocalizedTestName(analysis.getTest()));
        }

        isNew = false; // this is to set correct page title

        // do we need to enable next or previous?
        List analyses = analysisDAO.getNextAnalysisRecord(analysis.getId());
        if (analyses.size() > 0) {
            // enable next button
            request.setAttribute(NEXT_DISABLED, "false");
        }
        analyses = analysisDAO.getPreviousAnalysisRecord(analysis.getId());
        if (analyses.size() > 0) {
            // enable next button
            request.setAttribute(PREVIOUS_DISABLED, "false");
        }
        // end of logic to enable next or previous button

    } else { // this is a new analysis

        isNew = true; // this is to set correct page title

    }

    if (analysis.getId() != null && !analysis.getId().equals("0")) {
        request.setAttribute(ID, analysis.getId());
    }

    // populate form from valueholder
    PropertyUtils.copyProperties(dynaForm, analysis);

    return mapping.findForward(forward);
}

From source file:us.mn.state.health.lims.analysis.action.AnalysisUpdateAction.java

protected ActionForward performAction(ActionMapping mapping, ActionForm form, HttpServletRequest request,
        HttpServletResponse response) throws Exception {
    // The first job is to determine if we are coming to this action with an
    // ID parameter in the request. If there is no parameter, we are
    // creating a new Analysis.
    // If there is a parameter present, we should bring up an existing
    // Analysis to edit.
    String forward = FWD_SUCCESS;
    request.setAttribute(ALLOW_EDITS_KEY, "true");
    request.setAttribute(PREVIOUS_DISABLED, "false");
    request.setAttribute(NEXT_DISABLED, "false");

    String id = request.getParameter(ID);

    if (StringUtil.isNullorNill(id) || "0".equals(id)) {
        isNew = true;// w  ww . j a  v  a2s. c o  m
    } else {
        isNew = false;
    }

    BaseActionForm dynaForm = (BaseActionForm) form;

    // server-side validation (validation.xml)
    ActionMessages errors = dynaForm.validate(mapping, request);

    try {
        errors = validateAll(request, errors, dynaForm);
    } catch (Exception e) {
        //bugzilla 2154
        LogEvent.logError("AnalysisUpdateAction", "performAction()", e.toString());
        ActionError error = new ActionError("errors.ValidationException", null, null);
        errors.add(ActionMessages.GLOBAL_MESSAGE, error);
    }

    if (errors != null && errors.size() > 0) {
        saveErrors(request, errors);
        // since we forward to jsp - not Action we don't need to repopulate
        // the lists here
        return mapping.findForward(FWD_FAIL);
    }

    String start = (String) request.getParameter("startingRecNo");
    String direction = (String) request.getParameter("direction");

    Analysis analysis = new Analysis();
    //get sysUserId from login module
    UserSessionData usd = (UserSessionData) request.getSession().getAttribute(USER_SESSION_DATA);
    String sysUserId = String.valueOf(usd.getSystemUserId());
    analysis.setSysUserId(sysUserId);
    org.hibernate.Transaction tx = HibernateUtil.getSession().beginTransaction();

    // create TestSection and Test valueholder
    TestSection testSection = new TestSection();
    String testSectionName = (String) dynaForm.get("testSectionName");
    testSection.setTestSectionName(testSectionName);

    TestSectionDAO testSectionDAO = new TestSectionDAOImpl();
    TestSection ts = testSectionDAO.getTestSectionByName(testSection);

    Test test = new Test();
    String testName = (String) dynaForm.get("testName");
    test.setTestName(testName);

    TestDAO testDAO = new TestDAOImpl();
    Test t = testDAO.getTestByName(test);

    // populate valueholder from form
    PropertyUtils.copyProperties(analysis, dynaForm);

    analysis.setTestSection(ts);
    analysis.setTest(t);
    //bugzilla 1942
    analysis.setIsReportable(t.getIsReportable());

    try {

        AnalysisDAO analysisDAO = new AnalysisDAOImpl();

        if (!isNew) {
            // UPDATE
            analysisDAO.updateData(analysis);
            if (FWD_NEXT.equals(direction)) {
                List analyses = analysisDAO.getNextAnalysisRecord(analysis.getId());
                if (analyses != null && analyses.size() > 0) {
                    analysis = (Analysis) analyses.get(0);
                    analysisDAO.getData(analysis);
                    if (analyses.size() < 2) {
                        // disable next button
                        request.setAttribute(NEXT_DISABLED, "true");
                    }
                    id = analysis.getId();
                } else {
                    // disable next button
                    request.setAttribute(NEXT_DISABLED, "true");
                }
                forward = FWD_NEXT;
            }

            if (FWD_PREVIOUS.equals(direction)) {
                List analyses = analysisDAO.getPreviousAnalysisRecord(analysis.getId());
                if (analyses != null && analyses.size() > 0) {
                    analysis = (Analysis) analyses.get(0);
                    analysisDAO.getData(analysis);
                    if (analyses.size() < 2) {
                        // disable previous button
                        request.setAttribute(PREVIOUS_DISABLED, "true");
                    }
                    id = analysis.getId();
                } else {
                    // disable previous button
                    request.setAttribute(PREVIOUS_DISABLED, "true");
                }
                forward = FWD_PREVIOUS;
            }
        } else {
            // INSERT
            //bugzilla 2013 added duplicateCheck parameter
            analysisDAO.insertData(analysis, false);
        }
        tx.commit();
    } catch (LIMSRuntimeException lre) {
        //bugzilla 2154
        LogEvent.logError("AnalysisUpdateAction", "performAction()", lre.toString());
        tx.rollback();
        errors = new ActionMessages();
        ActionError error = null;
        if (lre.getException() instanceof org.hibernate.StaleObjectStateException) {
            // how can I get popup instead of struts error at the top of
            // page?
            // ActionMessages errors = dynaForm.validate(mapping, request);
            error = new ActionError("errors.OptimisticLockException", null, null);
        } else {
            error = new ActionError("errors.UpdateException", null, null);
        }
        errors.add(ActionMessages.GLOBAL_MESSAGE, error);
        saveErrors(request, errors);
        request.setAttribute(Globals.ERROR_KEY, errors);
        request.setAttribute(ALLOW_EDITS_KEY, "false");
        request.setAttribute(PREVIOUS_DISABLED, "true");
        request.setAttribute(NEXT_DISABLED, "true");

        forward = FWD_FAIL;

    } finally {
        HibernateUtil.closeSession();
    }
    if (forward.equals(FWD_FAIL))
        return mapping.findForward(forward);

    // initialize the form
    dynaForm.initialize(mapping);
    // repopulate the form from valueholder
    PropertyUtils.copyProperties(dynaForm, analysis);

    if ("true".equalsIgnoreCase(request.getParameter("close"))) {
        forward = FWD_CLOSE;
    }

    if (analysis.getId() != null && !analysis.getId().equals("0")) {
        request.setAttribute(ID, analysis.getId());

    }

    //bugzilla 1400
    if (isNew)
        forward = FWD_SUCCESS_INSERT;
    return getForward(mapping.findForward(forward), id, start);

}

From source file:us.mn.state.health.lims.analysis.daoimpl.AnalysisDAOImpl.java

public void getData(Analysis analysis) throws LIMSRuntimeException {

    try {//from   www .  j a v a 2s . com
        Analysis analysisClone = (Analysis) HibernateUtil.getSession().get(Analysis.class, analysis.getId());
        HibernateUtil.getSession().flush();
        HibernateUtil.getSession().clear();
        if (analysisClone != null) {
            PropertyUtils.copyProperties(analysis, analysisClone);
        } else {
            analysis.setId(null);
        }
    } catch (Exception e) {

        LogEvent.logError("AnalysisDAOImpl", "getData()", e.toString());
        throw new LIMSRuntimeException("Error in Analysis getData()", e);
    }
}

From source file:us.mn.state.health.lims.analysis.daoimpl.AnalysisDAOImpl.java

@SuppressWarnings({ "unchecked", "rawtypes" })
public void getMaxRevisionAnalysisBySampleAndTest(Analysis analysis) throws LIMSRuntimeException {

    try {//from   w  w w . j a va 2  s .co m
        Analysis anal = null;
        String sql = "from Analysis a where (a.sampleItem.id, a.test.id, a.revision) IN "
                + "(select b.sampleItem.id, b.test.id, max(b.revision) from Analysis b "
                + "group by b.sampleItem.id, b.test.id) " + "and a.sampleItem = :param "
                + "and a.status NOT IN (:param3) " + "and a.test = :param2";

        org.hibernate.Query query = HibernateUtil.getSession().createQuery(sql);
        query.setParameter("param", analysis.getSampleItem().getId());
        query.setParameter("param2", analysis.getTest().getId());
        List statusesToExclude = new ArrayList();
        statusesToExclude.add(SystemConfiguration.getInstance().getAnalysisStatusCanceled());
        query.setParameterList("param3", statusesToExclude);
        anal = (Analysis) query.uniqueResult();
        HibernateUtil.getSession().flush();
        HibernateUtil.getSession().clear();

        if (anal != null) {
            PropertyUtils.copyProperties(analysis, anal);
        } else {
            analysis.setId(null);
        }

    } catch (Exception e) {

        LogEvent.logError("AnalysisDAOImpl", "getMaxRevisionAnalysisBySampleAndTest()", e.toString());
        throw new LIMSRuntimeException("Error in Analysis getMaxRevisionAnalysisBySampleAndTest()", e);
    }

}

From source file:us.mn.state.health.lims.analysisqaevent.daoimpl.AnalysisQaEventDAOImpl.java

public void getData(AnalysisQaEvent analysisQaEvent) throws LIMSRuntimeException {
    try {/*from  w  w  w. j av a 2s  . com*/
        AnalysisQaEvent data = (AnalysisQaEvent) HibernateUtil.getSession().get(AnalysisQaEvent.class,
                analysisQaEvent.getId());
        HibernateUtil.getSession().flush();
        HibernateUtil.getSession().clear();
        if (data != null) {
            PropertyUtils.copyProperties(analysisQaEvent, data);
        } else {
            analysisQaEvent.setId(null);
        }
    } catch (Exception e) {
        //buzilla 2154
        LogEvent.logError("AnalysisQaEventDAOImpl", "getData()", e.toString());
        throw new LIMSRuntimeException("Error in AnalysisQaEvent getData()", e);
    }
}

From source file:us.mn.state.health.lims.analysisqaeventaction.daoimpl.AnalysisQaEventActionDAOImpl.java

public void getData(AnalysisQaEventAction analysisQaEventAction) throws LIMSRuntimeException {
    try {/*  w w w . j  a  v a2s . c  om*/
        AnalysisQaEventAction data = (AnalysisQaEventAction) HibernateUtil.getSession()
                .get(AnalysisQaEventAction.class, analysisQaEventAction.getId());
        HibernateUtil.getSession().flush();
        HibernateUtil.getSession().clear();
        if (data != null) {
            PropertyUtils.copyProperties(analysisQaEventAction, data);
        } else {
            analysisQaEventAction.setId(null);
        }
    } catch (Exception e) {
        //buzilla 2154
        LogEvent.logError("AnalysisQaEventActionDAOImpl", "getData()", e.toString());
        throw new LIMSRuntimeException("Error in AnalysisQaEventAction getData()", e);
    }
}

From source file:us.mn.state.health.lims.analyte.action.AnalyteAction.java

protected ActionForward performAction(ActionMapping mapping, ActionForm form, HttpServletRequest request,
        HttpServletResponse response) throws Exception {
    // The first job is to determine if we are coming to this action with an
    // ID parameter in the request. If there is no parameter, we are
    // creating a new Analyte.
    // If there is a parameter present, we should bring up an existing
    // Analyte to edit.
    String id = request.getParameter(ID);

    String forward = FWD_SUCCESS;
    request.setAttribute(ALLOW_EDITS_KEY, "true");
    request.setAttribute(PREVIOUS_DISABLED, "true");
    request.setAttribute(NEXT_DISABLED, "true");

    DynaActionForm dynaForm = (DynaActionForm) form;

    // initialize the form
    dynaForm.initialize(mapping);/*from www  .j av a  2  s.  c om*/

    Analyte analyte = new Analyte();

    if ((id != null) && (!"0".equals(id))) { // this is an existing
        // analyte

        analyte.setId(id);
        AnalyteDAO analyteDAO = new AnalyteDAOImpl();
        analyteDAO.getData(analyte);
        // initialize selectedAnalyteId
        if (analyte.getAnalyte() != null) {
            analyte.setSelectedAnalyteId(analyte.getAnalyte().getId());
        }
        isNew = false; // this is to set correct page title

        // do we need to enable next or previous?
        //bugzilla 1427 pass in name not id
        List analytes = analyteDAO.getNextAnalyteRecord(analyte.getAnalyteName());
        if (analytes.size() > 0) {
            // enable next button
            request.setAttribute(NEXT_DISABLED, "false");
        }
        //bugzilla 1427 pass in name not id
        analytes = analyteDAO.getPreviousAnalyteRecord(analyte.getAnalyteName());
        if (analytes.size() > 0) {
            // enable next button
            request.setAttribute(PREVIOUS_DISABLED, "false");
        }
        // end of logic to enable next or previous button
    } else { // this is a new analyte

        // default isActive to 'Y'
        analyte.setIsActive(YES);
        isNew = true; // this is to set correct page title
    }

    if (analyte.getId() != null && !analyte.getId().equals("0")) {
        request.setAttribute(ID, analyte.getId());
    }

    // populate form from valueholder
    PropertyUtils.copyProperties(form, analyte);

    AnalyteDAO analDAO = new AnalyteDAOImpl();

    List parentAnalytes = analDAO.getAllAnalytes();

    // set parentAnalyteName
    String parentAnalyteName = null;
    for (int i = 0; i < parentAnalytes.size(); i++) {
        Analyte parentAnalyte = (Analyte) parentAnalytes.get(i);
        if (parentAnalyte.getId().equals(analyte.getSelectedAnalyteId())) {
            parentAnalyteName = parentAnalyte.getAnalyteName();
        }
    }

    PropertyUtils.setProperty(form, "parentAnalytes", parentAnalytes);
    PropertyUtils.setProperty(form, "parentAnalyteName", parentAnalyteName);

    return mapping.findForward(forward);
}