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

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

Introduction

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

Prototype

public static void setProperty(Object bean, String name, Object value)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException 

Source Link

Document

Set the value of the specified property of the specified bean, no matter which property reference format is used, with no type conversions.

For more details see PropertyUtilsBean.

Usage

From source file:us.mn.state.health.lims.city.action.CityAction.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 City.
    // If there is a parameter present, we should bring up an existing
    // City to edit.
    String id = request.getParameter(ID);

    String forward = FWD_SUCCESS;
    request.setAttribute(RECORD_FROZEN_EDIT_DISABLED_KEY, "false");
    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);//  www .  j  ava 2s .c  o m

    Dictionary dictionary = new Dictionary();
    DictionaryDAO dictionaryDAO = new DictionaryDAOImpl();

    isNew = (id == null) || "0".equals(id);

    if (!isNew) {
        dictionary.setId(id);
        dictionaryDAO.getData(dictionary);

    } else {
        // this is a new city; default isActive to 'Y'
        dictionary.setIsActive(YES);
    }

    if (dictionary.getId() != null && !dictionary.getId().equals("0")) {
        request.setAttribute(ID, dictionary.getId());
    }
    PropertyUtils.copyProperties(form, dictionary);
    PropertyUtils.setProperty(form, "cityName", dictionary.getDictEntry());

    return mapping.findForward(forward);
}

From source file:us.mn.state.health.lims.codeelementtype.action.CodeElementTypeAction.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 CodeElementType.
    // If there is a parameter present, we should bring up an existing
    // CodeElementType 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);//  w  w w .  j a va2  s. c  o m

    CodeElementType codeElementType = new CodeElementType();

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

        codeElementType.setId(id);
        CodeElementTypeDAO codeElementTypeDAO = new CodeElementTypeDAOImpl();
        codeElementTypeDAO.getData(codeElementType);

        // initialize referenceTableId
        //bugzilla 2571 go through ReferenceTablesDAO to get reference tables info
        if (codeElementType.getReferenceTables() != null) {
            codeElementType.setReferenceTableId(codeElementType.getReferenceTables().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 codeElementTypes = codeElementTypeDAO.getNextCodeElementTypeRecord(codeElementType.getText());
        if (codeElementTypes.size() > 0) {
            // enable next button
            request.setAttribute(NEXT_DISABLED, "false");
        }
        //bugzilla 1427 pass in name not id
        codeElementTypes = codeElementTypeDAO.getPreviousCodeElementTypeRecord(codeElementType.getText());
        if (codeElementTypes.size() > 0) {
            // enable next button
            request.setAttribute(PREVIOUS_DISABLED, "false");
        }
        // end of logic to enable next or previous button
    } else { // this is a new codeElementType

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

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

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

    //bugzilla 2571 go through ReferenceTablesDAO to get reference tables info
    ReferenceTablesDAO referenceTablesDAO = new ReferenceTablesDAOImpl();
    List referenceTableList = referenceTablesDAO.getAllReferenceTablesForHl7Encoding();
    PropertyUtils.setProperty(form, "referenceTableList", referenceTableList);

    return mapping.findForward(forward);
}

From source file:us.mn.state.health.lims.codeelementtype.action.CodeElementTypeUpdateAction.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 CodeElementType.
    // If there is a parameter present, we should bring up an existing
    // CodeElementType 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;/*ww  w  .j  av  a  2s  .  com*/
    } else {
        isNew = false;
    }

    BaseActionForm dynaForm = (BaseActionForm) form;

    // server-side validation (validation.xml)
    ActionMessages errors = dynaForm.validate(mapping, request);
    if (errors != null && errors.size() > 0) {
        // System.out.println("Server side validation errors "
        // + errors.toString());
        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);
    CodeElementType codeElementType = new CodeElementType();
    //get sysUserId from login module
    UserSessionData usd = (UserSessionData) request.getSession().getAttribute(USER_SESSION_DATA);
    String sysUserId = String.valueOf(usd.getSystemUserId());
    codeElementType.setSysUserId(sysUserId);
    org.hibernate.Transaction tx = HibernateUtil.getSession().beginTransaction();

    String referenceTableId = (String) dynaForm.get("referenceTableId");
    //bugzilla 2571 go through ReferenceTablesDAO to get reference tables info
    List refTables = new ArrayList();
    if (dynaForm.get("referenceTableList") != null) {
        refTables = (List) dynaForm.get("referenceTableList");
    } else {
        //bugzilla 2571 go through ReferenceTablesDAO to get reference tables info
        ReferenceTablesDAO referenceTablesDAO = new ReferenceTablesDAOImpl();
        refTables = referenceTablesDAO.getAllReferenceTablesForHl7Encoding();
    }

    //bugzilla 2571 go through ReferenceTablesDAO to get reference tables info
    ReferenceTables referenceTables = null;

    //bugzilla 2571 go through ReferenceTablesDAO to get reference tables info
    for (int i = 0; i < refTables.size(); i++) {
        ReferenceTables refTbl = (ReferenceTables) refTables.get(i);
        if (refTbl.getId().equals(referenceTableId)) {
            referenceTables = refTbl;
            break;
        }
    }

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

    // set the referenceTable
    //bugzilla 2571 go through ReferenceTablesDAO to get reference tables info
    codeElementType.setReferenceTables(referenceTables);

    try {

        CodeElementTypeDAO codeElementTypeDAO = new CodeElementTypeDAOImpl();

        if (!isNew) {
            // UPDATE

            codeElementTypeDAO.updateData(codeElementType);

        } else {
            // INSERT

            codeElementTypeDAO.insertData(codeElementType);
        }
        tx.commit();
    } catch (LIMSRuntimeException lre) {
        //bugzilla 2154
        LogEvent.logError("CodeElementTypeUpdateAction", "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 = "codeelementtype.codeElementType";
                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(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, codeElementType);

    // need to repopulate in case of FWD_FAIL?
    //bugzilla 2571 go through ReferenceTablesDAO to get reference tables info
    PropertyUtils.setProperty(dynaForm, "referenceTableList", refTables);

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

    if (codeElementType.getId() != null && !codeElementType.getId().equals("0")) {
        request.setAttribute(ID, codeElementType.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.codeelementxref.action.CodeElementXrefAction.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 CodeElementXref.
    // If there is a parameter present, we should bring up an existing
    // CodeElementXref to edit.
    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);// ww w.  ja  v  a  2  s  . com

    MessageOrganizationDAO messageOrganizationDAO = new MessageOrganizationDAOImpl();
    List messageOrganizations = messageOrganizationDAO.getAllMessageOrganizations();
    PropertyUtils.setProperty(form, "messageOrganizations", messageOrganizations);

    CodeElementTypeDAO codeElementTypeDAO = new CodeElementTypeDAOImpl();
    List codeElementTypes = codeElementTypeDAO.getAllCodeElementTypes();
    PropertyUtils.setProperty(form, "codeElementTypes", codeElementTypes);

    PropertyUtils.setProperty(form, "localCodeElements", new ArrayList());
    PropertyUtils.setProperty(form, "receiverCodeElements", new ArrayList());
    PropertyUtils.setProperty(form, "codeElementXrefs", new ArrayList());

    return mapping.findForward(forward);
}

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

protected ActionForward performAction(ActionMapping mapping, ActionForm form, HttpServletRequest request,
        HttpServletResponse response) throws Exception {

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

    DynaActionForm dynaForm = (DynaActionForm) form;

    String selectedMessageOrganizationId = null;
    String selectedCodeElementTypeId = null;
    selectedMessageOrganizationId = (String) dynaForm.get("selectedMessageOrganizationId");
    selectedCodeElementTypeId = (String) dynaForm.getString("selectedCodeElementTypeId");

    if (StringUtil.isNullorNill(selectedMessageOrganizationId)) {
        selectedMessageOrganizationId = (String) request.getParameter("messageOrganizationId");
    }//from w w w.ja  v a  2 s  . c  o m

    if (StringUtil.isNullorNill(selectedCodeElementTypeId)) {
        selectedCodeElementTypeId = (String) request.getParameter("codeElementTypeId");
    }

    // initialize the form
    dynaForm.initialize(mapping);

    CodeElementXrefDAO codeElementXrefDAO = new CodeElementXrefDAOImpl();
    MessageOrganizationDAO messageOrganizationDAO = new MessageOrganizationDAOImpl();
    ReceiverCodeElementDAO receiverCodeElementDAO = new ReceiverCodeElementDAOImpl();
    EnumDAO enumDAO = new EnumDAOImpl();

    // populate form from valueholder

    List messageOrganizations = messageOrganizationDAO.getAllMessageOrganizations();
    PropertyUtils.setProperty(form, "messageOrganizations", messageOrganizations);

    CodeElementTypeDAO codeElementTypeDAO = new CodeElementTypeDAOImpl();
    List codeElementTypes = codeElementTypeDAO.getAllCodeElementTypes();
    PropertyUtils.setProperty(form, "codeElementTypes", codeElementTypes);

    PropertyUtils.setProperty(form, "selectedMessageOrganizationId", selectedMessageOrganizationId);
    PropertyUtils.setProperty(form, "selectedCodeElementTypeId", selectedCodeElementTypeId);

    ReceiverCodeElement receiverCodeElement = new ReceiverCodeElement();
    MessageOrganization mo = new MessageOrganization();
    if (!StringUtil.isNullorNill(selectedMessageOrganizationId)) {
        mo.setId(selectedMessageOrganizationId);
        messageOrganizationDAO.getData(mo);
    }

    CodeElementType cet = new CodeElementType();
    if (!StringUtil.isNullorNill(selectedCodeElementTypeId)) {
        cet.setId(selectedCodeElementTypeId);
        codeElementTypeDAO.getData(cet);
    }

    List linkedCodeElements = new ArrayList();
    CodeElementXref codeElementXref = new CodeElementXref();
    if (mo != null && !StringUtil.isNullorNill(mo.getId()) && cet != null
            && !StringUtil.isNullorNill(cet.getId())) {
        codeElementXref.setMessageOrganization(mo);
        codeElementXref.setCodeElementType(cet);
        linkedCodeElements = codeElementXrefDAO
                .getCodeElementXrefsByReceiverOrganizationAndCodeElementType(codeElementXref);
    }

    //for each codeElementXref fill in the vh for localCodeElement
    for (int i = 0; i < linkedCodeElements.size(); i++) {
        CodeElementXref codeElXref = (CodeElementXref) linkedCodeElements.get(i);
        String localID = codeElXref.getSelectedLocalCodeElementId();
        CodeElementType codeElType = (CodeElementType) codeElXref.getCodeElementType();
        if (codeElType != null && !StringUtil.isNullorNill(codeElType.getId())) {

            EnumValueItem evi = null;
            if (codeElType != null && !StringUtil.isNullorNill(codeElType.getId())) {
                evi = enumDAO.getEnumValueItem(EnumDAOImpl
                        //bugzilla 2571 go through ReferenceTablesDAO to get reference tables info
                        .getTableValueholderName(codeElType.getReferenceTables().getName()),
                        codeElXref.getSelectedLocalCodeElementId());
                codeElXref.setLocalCodeElement(evi);
            }

        }

    }
    Collections.sort(linkedCodeElements,
            CodeElementXrefLocalCodeElementNameComparator.LOCAL_CODE_ELEMENT_NAME_COMPARATOR);
    PropertyUtils.setProperty(form, "codeElementXrefs", linkedCodeElements);

    List receiverCodeElementsNotLinked = new ArrayList();
    if (mo != null && !StringUtil.isNullorNill(mo.getId()) && cet != null
            && !StringUtil.isNullorNill(cet.getId())) {
        receiverCodeElement.setMessageOrganization(mo);
        receiverCodeElement.setCodeElementType(cet);
        receiverCodeElementsNotLinked = receiverCodeElementDAO
                .getReceiverCodeElementsByMessageOrganizationAndCodeElementType(receiverCodeElement, false);
    }
    PropertyUtils.setProperty(form, "receiverCodeElements", receiverCodeElementsNotLinked);

    List localCodeElementsNotLinked = new ArrayList();
    if (cet != null && !StringUtil.isNullorNill(cet.getId())) {

        localCodeElementsNotLinked = enumDAO.getEnumObjForHL7(EnumDAOImpl.getTableValueholderName(cet
                //bugzilla 2571 go through ReferenceTablesDAO to get reference tables info
                .getReferenceTables().getName()), false);
    }
    Collections.sort(localCodeElementsNotLinked, EnumComparator.NAME_COMPARATOR);
    PropertyUtils.setProperty(form, "localCodeElements", localCodeElementsNotLinked);

    ActionError error = null;
    ActionMessages errors = dynaForm.validate(mapping, request);

    if (localCodeElementsNotLinked.size() == 0 && linkedCodeElements.size() == 0) {
        error = new ActionError("codeelementxref.validation.nolocalcodes", null, null);
        errors.add(ActionMessages.GLOBAL_MESSAGE, error);
    }

    if (receiverCodeElementsNotLinked.size() == 0 && linkedCodeElements.size() == 0) {
        error = new ActionError("codeelementxref.validation.noreceivercodes", null, null);
        errors.add(ActionMessages.GLOBAL_MESSAGE, error);
    }

    if (errors != null && errors.size() > 0) {
        saveErrors(request, errors);
        request.setAttribute(Globals.ERROR_KEY, errors);
        return mapping.findForward(FWD_FAIL);
    }

    return mapping.findForward(forward);
}

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

protected void setDictionaryList(BaseActionForm dynaForm, String propertyName, String category,
        boolean sortById)
        throws LIMSRuntimeException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {

    List<IdValuePair> conditionList = new ArrayList<IdValuePair>();
    DictionaryDAO dictionaryDAO = new DictionaryDAOImpl();
    // The category is by local_abbrev
    List<Dictionary> conditionDictionaryList = dictionaryDAO.getDictionaryEntrysByCategory(category);

    Collections.sort(conditionDictionaryList, new Comparator<Dictionary>() {
        @Override//from ww  w.ja va  2s .  c o  m
        public int compare(Dictionary o1, Dictionary o2) {
            return (int) (Long.parseLong(o1.getId()) - Long.parseLong(o2.getId()));
        }
    });

    for (Dictionary dictionary : conditionDictionaryList) {
        conditionList.add(new IdValuePair(dictionary.getId(), dictionary.getLocalizedName()));
    }

    PropertyUtils.setProperty(dynaForm, propertyName, conditionList);
}

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

protected ActionForward performAction(ActionMapping mapping, ActionForm form, HttpServletRequest request,
        HttpServletResponse response) throws Exception {

    String forward = FWD_SUCCESS;

    DynaActionForm dynaForm = (DynaActionForm) form;

    int action = -1;
    if (request.getParameter("paging") != null) {
        action = Integer.parseInt((String) request.getParameter("paging"));
    }//  ww  w  .  j a va2 s .  c o  m
    List menuList = null;

    try {
        switch (action) {
        case PREVIOUS:
            menuList = doPreviousPage(mapping, form, request, response);
            break;
        case NEXT:
            menuList = doNextPage(mapping, form, request, response);
            break;
        default:
            menuList = doNone(mapping, form, request, response);
        }
    } catch (Exception e) {
        LogEvent.logError("BaseMenuAction", "performAction()", e.toString());
        forward = FWD_FAIL;
    }

    dynaForm.initialize(mapping);

    PropertyUtils.setProperty(dynaForm, "menuList", menuList);

    request.setAttribute(DEACTIVATE_DISABLED, getDeactivateDisabled());
    request.setAttribute(ADD_DISABLED, getAddDisabled());
    request.setAttribute(EDIT_DISABLED, getEditDisabled());

    String[] selectedIDs = new String[5];

    PropertyUtils.setProperty(dynaForm, "selectedIDs", selectedIDs);

    return mapping.findForward(forward);
}

From source file:us.mn.state.health.lims.common.util.WebTestInfoAction.java

@Override
protected ActionForward performAction(ActionMapping mapping, ActionForm form, HttpServletRequest request,
        HttpServletResponse response) throws Exception {

    String xmlWad = getWebTestXmlWad();

    PropertyUtils.setProperty((DynaActionForm) form, "xmlWad", xmlWad);

    return mapping.findForward(IActionConstants.FWD_SUCCESS);
}

From source file:us.mn.state.health.lims.county.action.CountyAction.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 County.
    // If there is a parameter present, we should bring up an existing
    // County 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");

    DynaActionForm dynaForm = (DynaActionForm) form;

    // initialize the form
    dynaForm.initialize(mapping);/*  w  w  w  . j av a2  s .  co  m*/

    County county = new County();
    //System.out.println("I am in CountyAction and this is id " + id);
    if ((id != null) && (!"0".equals(id))) { // this is an existing
        // county

        county.setId(id);
        CountyDAO countyDAO = new CountyDAOImpl();
        countyDAO.getData(county);

        // initialize sysUserId
        if (county.getRegion() != null) {
            county.setRegionId(county.getRegion().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 counties = countyDAO.getNextCountyRecord(county.getCounty());
        if (counties.size() > 0) {
            // enable next button
            request.setAttribute(NEXT_DISABLED, "false");
        }
        //bugzilla 1427 pass in name not id
        counties = countyDAO.getPreviousCountyRecord(county.getCounty());
        if (counties.size() > 0) {
            // enable next button
            request.setAttribute(PREVIOUS_DISABLED, "false");
        }
        // end of logic to enable next or previous button

    } else { // this is a new county

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

    }

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

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

    RegionDAO regionDAO = new RegionDAOImpl();

    List regions = regionDAO.getAllRegions();

    PropertyUtils.setProperty(form, "regions", regions);

    //System.out.println("I am in CountyAction this is forward " + forward);
    return mapping.findForward(forward);
}

From source file:us.mn.state.health.lims.county.action.CountyUpdateAction.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 County.
    // If there is a parameter present, we should bring up an existing
    // County 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  a 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");

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

    String regionId = (String) dynaForm.get("regionId");

    List regions = new ArrayList();
    if (dynaForm.get("regions") != null) {
        regions = (List) dynaForm.get("regions");
    } else {
        RegionDAO regionDAO = new RegionDAOImpl();
        regions = regionDAO.getAllRegions();
    }

    Region region = null;

    for (int i = 0; i < regions.size(); i++) {
        Region reg = (Region) regions.get(i);
        if (reg.getId().equals(regionId)) {
            region = reg;
            break;
        }
    }

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

    // set the region
    county.setRegion(region);

    try {

        CountyDAO countyDAO = new CountyDAOImpl();

        if (!isNew) {
            // UPDATE
            countyDAO.updateData(county);
        } else {
            // INSERT
            countyDAO.insertData(county);
        }
        tx.commit();
    } catch (LIMSRuntimeException lre) {
        //bugzilla 2154
        LogEvent.logError("CountyUpdateAction", "performAction()", lre.toString());
        tx.rollback();
        errors = new ActionMessages();
        //bugzilla 1482
        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 = "county.county";
                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, county);

    // need to repopulate in case of FWD_FAIL?
    PropertyUtils.setProperty(dynaForm, "regions", regions);

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

    if (county.getId() != null && !county.getId().equals("0")) {
        request.setAttribute(ID, county.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);

}