Java tutorial
/* * Copyright (c) 2007 NTT DATA Corporation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package jp.terasoluna.fw.web.struts.reset; import java.util.Iterator; import java.util.Map; import java.util.Set; import java.util.Map.Entry; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import jp.terasoluna.fw.beans.IndexedBeanWrapper; import jp.terasoluna.fw.beans.JXPathIndexedBeanWrapperImpl; import jp.terasoluna.fw.util.BeanUtil; import jp.terasoluna.fw.util.PropertyAccessException; import jp.terasoluna.fw.web.RequestUtil; import jp.terasoluna.fw.web.struts.ModuleUtil; import jp.terasoluna.fw.web.struts.form.FormEx; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.struts.action.ActionMapping; /** * ftHgZbgNX?B * * <p> * TERASOLUNAftHgp Resetter NX?B<br> * NX * {@link jp.terasoluna.fw.web.struts.plugins.ResetterPlugIn} * p?AXML t@C?? * tH?[tB?[hZbg?s?B<br> * ANVtH?[reset()?\bhg?AZbg?? * StrutsCtTCN?s?BZbg??`FbN{bNX * WI{^l?v?peB?I?A * Ip???B<br> * ?P?tB?[h????ArWlX?WbN?A * fl?X?s?BStrutsCtTCNZbg?s?A * NGXgp??[^v?peB?x??B<br> * <br> * Zbg???tB?[hlnull???A^?A * null?Al?X?s?BnullOObject^tB?[hnulll?A * boolean^false?Av~eBu^tB?[h0??s?B * DynaValidatorActionFormExgp???A * struts-config.xmlL?q?lZbg?A??B<br> * Zbg@\?A ?w?Zbg???x y ?wwZbg@\?x * ??AwZbg@\?Az List IuWFNg * CZbg\?B<br> * ZbgCNGXgp??[^ "startIndex", * "endIndex"L?[i[Kv?B * </p> * * <strong>gp@</strong><br> * tH?[Zbg`t@C(reset.xml)ANV * Zbg?tB?[h??B <code><pre> * <reset> * <action path="/resetAction"> * <property-reset name="field1" /> * <property-reset name="field2" select="true" /> * </action> * </reset> * </pre></code> ?L????A/resetAction.do * NGXgp??[^tH?[fOtH?[ * wtB?[hNA?B<br> * NA?tB?[h property-reset ^O name ?w?A * "field1" * "field2" ?B<br> * "field2" ?A select ? true ?A * NGXgp?^"startIndex" ?` "endIndex" * wCfbNXtB?[hl null * ??B<br> * ?A{NXlXgv?peBZbg?B * ANVtH?[lXgv?peBZbg???A * v?peB?u.?vA?Q?gp?B * ?AlXgv?peBZbg???B<br><br> * <strong>lXgv?peBZbg@</strong><br> * <code><pre> * public class MyActionForm extends ValidatorActionFormEx { * private List<Row> rows = null; * public void setRows(List<Row> rows) { * this.rows = rows; * } * public List<Row> getRows() { * return this.rows; * } * private Map<String, String> map = new HashMap(); * //? * } * public class Row { * private String value = null; * public void setValue(String value) { * this.value = value; * } * public String getValue() { * return this.value; * } * } * </pre></code> * ?LMyActionForm?RowNXXgevaluev?peB?A * Map^mapv?peB?ufield?vL?[l * Zbg??reset.xmlL?q?B * <code><pre> * <reset> * <action path="/resetAction"> * <property-reset name="rows.value" /> * <property-reset name="map(field)" /> * </action> * </reset> * </pre></code> * ?Q?v?peBL?q?BvfzList?? * t?[??[NIF?ASvfZbg?B * ?v?peBl?Aselect?truew?A * wZbg\?B * * @see jp.terasoluna.fw.web.struts.plugins.ResetterPlugIn * @see jp.terasoluna.fw.web.struts.reset.ActionReset * @see jp.terasoluna.fw.web.struts.reset.FieldReset * @see jp.terasoluna.fw.web.struts.reset.ResetterResources * @see jp.terasoluna.fw.web.struts.form.FormEx * @see jp.terasoluna.fw.web.struts.form.DynaValidatorActionFormEx * @see jp.terasoluna.fw.web.struts.form.ValidatorActionFormEx * */ public class ResetterImpl implements Resetter { /** * ?ONX */ private static Log log = LogFactory.getLog(ResetterImpl.class); /** * tH?[tB?[hlZbg?s?B * * @param form * ANVtH?[ * @param mapping * }bsO? * @param request * NGXg? * * @see jp.terasoluna.fw.web.struts.plugins.ResetterPlugIn * @see jp.terasoluna.fw.web.struts.reset.ActionReset * @see jp.terasoluna.fw.web.struts.reset.FieldReset * @see jp.terasoluna.fw.web.struts.reset.ResetterResources * @see jp.terasoluna.fw.web.struts.form.FormEx * @see jp.terasoluna.fw.web.struts.form.DynaValidatorActionFormEx * @see jp.terasoluna.fw.web.struts.form.ValidatorActionFormEx */ public void reset(FormEx form, ActionMapping mapping, HttpServletRequest request) { // form,mappingnull??B // NullPointer??l if (log.isDebugEnabled()) { log.debug(mapping.getName() + " reset() called."); } ActionReset reset = getActionReset(mapping, request); if (reset == null) { return; } Iterator<String> it = reset.getFieldNames(); IndexedBeanWrapper wrapper = new JXPathIndexedBeanWrapperImpl(form); while (it.hasNext()) { // tB?[h String fieldName = it.next(); if (log.isDebugEnabled()) { log.debug("reset[" + fieldName + "]"); } Map<String, Object> propMap = wrapper.getIndexedPropertyValues(fieldName); // selectw??wZbg // A?AtB?[hz?AList???Zbg?s if (reset.isSelectField(fieldName) && propMap.size() > 1) { resetSelectField(form, propMap, request); } else { for (Entry<String, Object> e : propMap.entrySet()) { resetValue(form, e); } } } } /** * ANVtH?[wv?peBZbg?B * v?peB^boolean^?ABoolean^??false??B * v~eBu^bp?[^???A0??B * v?peB^bp?[^OObject^??null??B<br> * ??A?entrynulln?B * * @param form ?NGXggpANVtH?[ * @param entry Zbg?v?peB?lGg * @throws PropertyAccessException v?peBl?s?? */ protected void resetValue(FormEx form, Entry<String, Object> entry) { if (log.isDebugEnabled()) { log.debug("resetValue(" + form + ", " + entry.getKey() + ") called."); } String propName = entry.getKey(); try { Object value = entry.getValue(); if (value == null) { return; } Class type = null; type = value.getClass(); if (type != null) { // ^???B if (type == Boolean.TYPE || type == Boolean.class) { BeanUtil.setBeanProperty(form, propName, Boolean.FALSE); } else if (type == Byte.TYPE || type == Byte.class) { BeanUtil.setBeanProperty(form, propName, new Byte((byte) 0)); } else if (type == Character.TYPE || type == Character.class) { BeanUtil.setBeanProperty(form, propName, new Character((char) 0)); } else if (type == Double.TYPE || type == Double.class) { BeanUtil.setBeanProperty(form, propName, new Double(0.0)); } else if (type == Float.TYPE || type == Float.class) { BeanUtil.setBeanProperty(form, propName, new Float((float) 0.0)); } else if (type == Integer.TYPE || type == Integer.class) { BeanUtil.setBeanProperty(form, propName, new Integer(0)); } else if (type == Long.TYPE || type == Long.class) { BeanUtil.setBeanProperty(form, propName, new Long(0)); } else if (type == Short.TYPE || type == Short.class) { BeanUtil.setBeanProperty(form, propName, new Short((short) 0)); } else { // v~eBu^?Abp?[^??null? BeanUtil.setBeanProperty(form, propName, null); } } } catch (PropertyAccessException e) { log.error("cannot access property " + form + "." + propName, e); } } /** * ResetterResources CX^X?B * * @param request NGXg? * @return ResetterResources CX^X * @see jp.terasoluna.fw.web.struts.reset.ActionReset * @see jp.terasoluna.fw.web.struts.reset.FieldReset */ protected ResetterResources getResetterResources(HttpServletRequest request) { // Prefix String prefix = ModuleUtil.getPrefix(request); // ServletContext ServletContext application = RequestUtil.getServletContext(request); // ResetterResource ResetterResources resources = (ResetterResources) application .getAttribute(ResetterResources.RESETTER_RESOURCES_KEY + prefix); return resources; } /** * ANVpXR ActionReset CX^X?B * * @param mapping ?NGXg}bsO? * @param request NGXg? * @return ActionReset CX^X * @see jp.terasoluna.fw.web.struts.reset.FieldReset * @see jp.terasoluna.fw.web.struts.reset.ResetterResources */ protected ActionReset getActionReset(ActionMapping mapping, HttpServletRequest request) { ResetterResources resources = getResetterResources(request); if (resources == null) { if (log.isDebugEnabled()) { log.debug("ResetterResources is null."); } return null; } ActionReset reset = resources.get(mapping.getPath()); if (reset == null) { if (log.isDebugEnabled()) { log.debug("action path:" + mapping.getPath() + " is not specified to reset."); } } return reset; } /** * ItB?[hZbg?s?B * * select ? true ? NGXgp?^ * "startIndex" ?` "endIndex" * wCfbNXtB?[hl null ??B<br> * NGXgp?^ "startIndex", "endIndex" * SvfZbg?s?B<br> * ?AtB?[hl^z^?AList ^????A * ?Zbg???s?B * * @param form ?NGXggpANVtH?[ * @param propMap v?peB?lMap * @param request NGXg? * * @see jp.terasoluna.fw.web.struts.reset.ActionReset * @see jp.terasoluna.fw.web.struts.reset.FieldReset * @see jp.terasoluna.fw.web.struts.reset.ResetterResources */ protected void resetSelectField(FormEx form, Map<String, Object> propMap, HttpServletRequest request) { if (log.isDebugEnabled()) { log.debug("resetSelectField() called."); } int startIndex = 0; int size = propMap.size(); int endIndex = size; try { String startIndexStr = request.getParameter("startIndex"); String endIndexStr = request.getParameter("endIndex"); if (startIndexStr != null) { startIndex = Integer.parseInt(startIndexStr); } if (endIndexStr != null) { endIndex = Integer.parseInt(endIndexStr); } if (log.isDebugEnabled()) { log.debug("startIndex = [" + startIndex + "]"); log.debug("endIndex = [" + endIndex + "]"); } } catch (NumberFormatException e) { log.error("startIndex or endIndex is not Number.", e); return; } // Zbg???s?B Set<Map.Entry<String, Object>> set = propMap.entrySet(); int index = 0; for (Entry<String, Object> e : set) { if (index >= startIndex && startIndex < size && index <= endIndex && index < size) { resetValue(form, e); } index++; } } }