/** * @(#)Spr001aSmpCalculationForm * Copyright (c) 2005, B.C. Ministry of Forests * All rights reserved. */ package ca.bc.gov.mof.spar.web.form; /******************************************************** Form Class: Spr001aSmpCalculationForm Purpose: Revision History Person Date Comments ----------------- --------- ----------------------- Pangaea June 22 2005 Created *******************************************************/ import java.util.Iterator; import java.util.Hashtable; import javax.servlet.http.HttpServletRequest; import org.apache.struts.action.ActionMessage; import org.apache.struts.action.ActionErrors; import org.apache.struts.action.ActionMapping; import ca.bc.gov.mof.sil.common.validators.FloatFieldValidator; import ca.bc.gov.mof.sil.common.validators.FloatRangeValidator; import ca.bc.gov.mof.sil.common.validators.IntegerFieldValidator; import ca.bc.gov.mof.sil.common.validators.RequiredFieldValidator; import ca.bc.gov.mof.sil.common.validators.DecimalPlacesValidator; import ca.bc.gov.mof.sil.common.web.frame.SilUser; import ca.bc.gov.mof.sil.http.HttpErrorHandler; import ca.bc.gov.mof.spar.data.beans.Spr001aSmpCalculationBean; import ca.bc.gov.mof.spar.data.beans.Spr001aSmpCalculationResultArrayElement; import ca.bc.gov.mof.spar.data.validators.WrappedValidator; import ca.bc.gov.mof.spar.web.frame.SparForm; import ca.bc.gov.mof.spar.web.frame.SparUser; import ca.bc.gov.mof.sil.webade.db.DataBean; import ca.bc.gov.mof.sil.common.util.StringUtils; import ca.bc.gov.mof.sil.webade.db.ui.ErrorHandler; import ca.bc.gov.mof.sil.webade.db.ui.ErrorMessage; import ca.bc.gov.mof.sil.webade.db.util.Trace; import ca.bc.gov.mof.sil.webade.db.validators.FieldValidator; import ca.bc.gov.mof.sil.webade.db.validators.ValidatingForm; public class Spr001aSmpCalculationForm extends SparForm implements ValidatingForm { private static final Trace trace = new Trace( Spr001aSmpCalculationForm.class ); // Member variables private Spr001aSmpCalculationBean dataBean = new Spr001aSmpCalculationBean(); private String searchRowIndex = ""; //Used for Client Display Field Implementation(s) private SilUser user() { return super.getUser(); } /** * Contains the forward to use when returning using the back button. * This is populated with the searchRequest.requestor in the action class. */ private String backForward = ""; /** * Gets the forward to go to when invoking a back action * @return the forward to go to when invoking a back action */ public String getBackForward() { return backForward; } /** * Sets the forward to go to when invoking a back action * @param backForward the forward to use to go back */ public void setBackForward(String backForward) { if (backForward == null) { this.backForward = ""; } else { this.backForward = backForward.trim(); } } /** * @return Returns the searchRowIndex. */ public String getSearchRowIndex() { return searchRowIndex; } /** * @param searchRowIndex The searchRowIndex to set. */ public void setSearchRowIndex(String searchRowIndex) { this.searchRowIndex = searchRowIndex; } /** * Contains the name of the field that is to be populated by the search response. This is required * because there are two orchard id fields on the this screen. */ private String searchField = ""; /** * Gets the field currently being searched. * @return the field currently being searched */ public String getSearchField() { return searchField; } /** * Sets the field currently being searched. */ public void setSearchField(String searchField) { if (searchField == null) { this.searchField = ""; } else { this.searchField = searchField.trim(); } } boolean validatorsInitialized = false; // Constructor public Spr001aSmpCalculationForm () {} public void initFromRequest(HttpServletRequest request) { super.initFromRequest(request); // Read the array parameters from the screen accumulateParentTreeInformation(request); if (!validatorsInitialized) { // Add validators validatorsInitialized = true; } } public DataBean getBean() { return dataBean; } public void setBean( DataBean bean ) { dataBean = (Spr001aSmpCalculationBean)bean; } /** * Implemented from ValidatingForm interface. */ public DataBean getFormBean() { return dataBean; } /** * Gets/Sets the instance of the bean for the page */ public Spr001aSmpCalculationBean getDataBean() { return dataBean; } public void setDataBean(Spr001aSmpCalculationBean dataBean) { this.dataBean = dataBean; } /** * Clear form member variables. */ public void clear () { this.dataBean = new Spr001aSmpCalculationBean(); } public void clearDataBean () { this.clear(); } /** * Performs the validation on the form. */ public ActionErrors validate(ActionMapping actionMapping, HttpServletRequest request) { // Call the form's validators and validate the parent tree information super.validate(actionMapping, request); this.validateParentTreeInformation(actionMapping, request); // Get the errors from the session and return them ErrorHandler eh = HttpErrorHandler.get(request.getSession()); return eh.getErrors(); } /** * Force formatting on data entry fields. */ public void forceFormats() { } /* Parent Tree Information related methods (Validation and Accumulation) */ /** * Wraps the messages coming from the validators to display the parent tree number */ private class Spr001aWrappedValidator extends WrappedValidator { private String fieldName = null; private ValidatingForm form = null; public Spr001aWrappedValidator(ValidatingForm form, FieldValidator validator) { super(form, validator, "spar.web.error.usr.spr001a.parentTreeInformationValidationWrapper", new String[]{""}); // Needed for the validate. this.form = form; this.fieldName = "parent_tree_number"; } public ErrorMessage validate() { // Set the message parameter getParameters()[0] = this.getPropertyValue(form.getBean(), fieldName); return super.validate(); } } /** * Form used for the parent tree validators */ private class ParentTreeInformationForm extends SparForm implements ValidatingForm { // Field validators private RequiredFieldValidator parentTreeNumberRequired = new RequiredFieldValidator(this, "parent_tree_number", "Parent Tree Number"); private IntegerFieldValidator parentTreeNumberNumeric = new IntegerFieldValidator(this, "parent_tree_number", "Parent Tree Number", false); private RequiredFieldValidator volumeRequired = new RequiredFieldValidator(this, "amount_of_material", "Volume (ml)"); private IntegerFieldValidator volumeNumeric = new IntegerFieldValidator(this, "amount_of_material", "Volume (ml)", false); private FieldValidator validateParentTreeNumber = new FieldValidator() { public ErrorMessage validate() { ErrorMessage error = null; if(StringUtils.getInstance().hasAValue(bean.getAmount_of_material())) { error = parentTreeNumberRequired.validate(); } if(error == null) { error = parentTreeNumberNumeric.validate(); } return error; } }; private FieldValidator validateVolume = new FieldValidator() { public ErrorMessage validate() { ErrorMessage error = null; if(StringUtils.getInstance().hasAValue(bean.getParent_tree_number())) { error = volumeRequired.validate(); } if(error == null) { error = volumeNumeric.validate(); } return error; } }; public ParentTreeInformationForm() { // Initialize the validators addValidator("Save", new Spr001aWrappedValidator(this, validateParentTreeNumber)); addValidator("Save", new Spr001aWrappedValidator(this, validateVolume)); } private Spr001aSmpCalculationResultArrayElement bean = null; private String userAction = ""; public DataBean getBean() { return bean; } public void setUserAction(String userAction) { this.userAction = userAction; } public String getUserAction() { return userAction; } public void clear() { // Do Nothing } public void setBean(DataBean bean) { this.bean = (Spr001aSmpCalculationResultArrayElement) bean; } }; private ParentTreeInformationForm parentTreeInformationForm = new ParentTreeInformationForm(); /** * Validates the parent tree information contained within the bean and adds * any failures to the passed errors. * */ private void validateParentTreeInformation(ActionMapping mapping, HttpServletRequest request) { Spr001aSmpCalculationResultArrayElement element = null; parentTreeInformationForm.setUserAction(userAction); Hashtable table = new Hashtable(); ErrorHandler eh = HttpErrorHandler.get(request.getSession()); ActionErrors errors = eh.getErrors(); // Validate each of the beans Iterator iterator = dataBean.getP_result().iterator(); while (iterator.hasNext()) { element = (Spr001aSmpCalculationResultArrayElement) iterator.next(); parentTreeInformationForm.setBean(element); if(StringUtils.getInstance().hasAValue(element.getParent_tree_number())) { if(table.get(element.getParent_tree_number()) != null) { errors.add("duplicate PT", new ActionMessage("spar.web.error.usr.spr001a.parentTreeInformationValidationWrapper", "has a duplicate entry", element.getParent_tree_number())); } else { table.put(element.getParent_tree_number(), element.getParent_tree_number()); } } // If the item should be saved, validate it if (!Spr001aSmpCalculationForm.parentTreeInformationIsBlank(element)) { parentTreeInformationForm.validate(mapping, request); } } } /** * Accumulates parent tree information into p_results. * * @param request */ private void accumulateParentTreeInformation(HttpServletRequest request) { int index = 0; // Each item in the result set will have a p_results_N.p_parent_tree_number // field on the screen while (request.getParameter("p_result_" + index + ".parent_tree_number") != null) { Spr001aSmpCalculationResultArrayElement element = new Spr001aSmpCalculationResultArrayElement(); // Populate the element element.setParent_tree_id(request.getParameter("p_result_" + index + ".parent_tree_id")); element.setParent_tree_number(request.getParameter("p_result_" + index + ".parent_tree_number")); element.setAmount_of_material(request.getParameter("p_result_" + index + ".amount_of_material")); // If the parent tree number has changed, don't keep the derived values if (request.getParameter("p_result_" + index + ".old_parent_tree_number")!= null && request.getParameter("p_result_" + index + ".old_parent_tree_number").equals(element.getParent_tree_number())) { element.setG_estimated_ind(request.getParameter("p_result_" + index + ".g_estimated_ind")); element.setD_estimated_ind(request.getParameter("p_result_" + index + ".d_estimated_ind")); element.setR_estimated_ind(request.getParameter("p_result_" + index + ".r_estimated_ind")); element.setM_estimated_ind(request.getParameter("p_result_" + index + ".m_estimated_ind")); element.setUntested_ind(request.getParameter("p_result_" + index + ".untested_ind")); element.setProportion(request.getParameter("p_result_" + index + ".proportion")); element.setBv_g(request.getParameter("p_result_" + index + ".bv_g")); element.setBv_d(request.getParameter("p_result_" + index + ".bv_d")); element.setBv_r(request.getParameter("p_result_" + index + ".bv_r")); element.setBv_m(request.getParameter("p_result_" + index + ".bv_m")); element.setCv_g(request.getParameter("p_result_" + index + ".cv_g")); element.setCv_d(request.getParameter("p_result_" + index + ".cv_d")); element.setCv_r(request.getParameter("p_result_" + index + ".cv_r")); element.setCv_m(request.getParameter("p_result_" + index + ".cv_m")); element.setElevation(request.getParameter("p_result_" + index + ".elevation")); element.setLatitude_degrees(request.getParameter("p_result_" + index + ".latitude_degrees")); element.setLatitude_minutes(request.getParameter("p_result_" + index + ".latitude_minutes")); element.setLatitude_seconds(request.getParameter("p_result_" + index + ".latitude_seconds")); element.setLongitude_degrees(request.getParameter("p_result_" + index + ".longitude_degrees")); element.setLongitude_minutes(request.getParameter("p_result_" + index + ".longitude_minutes")); element.setLongitude_seconds(request.getParameter("p_result_" + index + ".longitude_seconds")); element.setWeighted_bv_g(request.getParameter("p_result_" + index + ".weighted_bv_g")); element.setWeighted_bv_r(request.getParameter("p_result_" + index + ".weighted_bv_r")); element.setWeighted_elevation(request.getParameter("p_result_" + index + ".weighted_elevation")); element.setWeighted_latitude(request.getParameter("p_result_" + index + ".weighted_latitude")); element.setWeighted_longitude(request.getParameter("p_result_" + index + ".weighted_longitude")); element.setRevision_count(request.getParameter("p_result_" + index + ".revision_count")); element.setGeography_error_ind(request.getParameter("p_result_" + index + ".geography_error_ind")); } dataBean.getP_result().set(index, element); index++; } } /** * Returns true if the passed parent tree information element contains all blanks * @param element Element to checked * @return True if the element should be saved, false if not */ public static boolean parentTreeInformationIsBlank(Spr001aSmpCalculationResultArrayElement element) { boolean blank = element.getParent_tree_number().equals("") && element.getAmount_of_material().equals(""); return blank; } /** * Returns true if the passed parent tree information element should be saved * @param element Element to checked * @return True if the element should be saved, false if not */ public static boolean parentTreeInformationSavable(Spr001aSmpCalculationResultArrayElement element) { boolean saveable = (!element.getParent_tree_number().equals("") && !element.getAmount_of_material().equals("")) || !element.getRevision_count().equals(""); return saveable; } /* Security Methods */ /** * Determines if the user is allowed to save. */ public boolean getSaveEnabled() { SparUser sparUser = (SparUser) user(); // If the record is pending, anybody can save if (dataBean.getP_seedlot_status_code().equals("PND")) { return true; } // If the seedlot is cancelled, or incomplete it is not updateable else if (dataBean.getP_seedlot_status_code().equals("CAN") || dataBean.getP_seedlot_status_code().equals("INC")) { return false; } // If the seedlot is approved, complete, submitted, or expired only users who are SPR_TREE_CENTRE_ADMIN can update it else if (dataBean.getP_seedlot_status_code().equals("EXP") || dataBean.getP_seedlot_status_code().equals("APP") || dataBean.getP_seedlot_status_code().equals("COM") || dataBean.getP_seedlot_status_code().equals("SUB")) { if (sparUser.getIsTreeSeedCentreAdmin()) { return true; } else { return false; } } // Assume the user is allowed to save return true; } /** * Returns true if bv_* are to be displayed instead of cv_*. * * @return true if bv_* are to be displayed instead of cv_*. */ public boolean getDisplayBv() { if (dataBean.getP_result().size() > 0) { Spr001aSmpCalculationResultArrayElement element = (Spr001aSmpCalculationResultArrayElement) dataBean.getP_result().get(0); // If any of the Bv_ fields have a value then bv will be displayed. if (!element.getBv_g().equals("") || !element.getBv_d().equals("") || !element.getBv_r().equals("") || !element.getBv_m().equals("")) { return true; } else { return false; } } else { // Assume that BV are displayed return true; } } }