Java tutorial
/** * PureInfo Command * @(#)TreenamedValueQueryAction.java 1.0 2006-12-26 * * Copyright(c) 2004-2005, PureInfo Information Technology Corp. Ltd. * All rights reserved, see the license file. * * www.pureinfo.com.cn */ package com.pureinfo.common.namedvalue.action; import java.util.Iterator; import java.util.List; import org.apache.struts.action.ActionForward; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; import com.pureinfo.ark.interaction.ActionBase; import com.pureinfo.common.namedvalue.NamedValueHelper; import com.pureinfo.common.namedvalue.model.TreeNamedValue; import com.pureinfo.force.exception.PureException; /** * <P> * Created on 2006-12-26 16:41:22 <BR> * Last modified on 2006-12-26 * </P> * TODO describe TreenamedValueQueryAction here ... * * @author elmar.chen * @version 1.0, 2006-12-26 * @since Command 1.0 */ public class TreenamedValueQueryAction extends ActionBase { private static final int EXCEPTION_BADVALUE = -1; List[] m_nvs; String[] m_defaults; private int m_nType; private String m_sValue; private boolean m_bThisLevelOnly; /** * @see com.pureinfo.ark.interaction.ActionBase#executeAction() */ public ActionForward executeAction() throws PureException { m_nType = request.getRequiredInt("type", ""); m_sValue = request.getParameter("value", ""); m_bThisLevelOnly = "level".equals(request.getParameter("scope")); initValues(); makeDoc(); return null; } /** * */ private Document makeDoc() { Document doc = DocumentHelper.createDocument(); Element root = doc.addElement("tree-named-values"); for (int i = 0; i < m_defaults.length; i++) { if (m_defaults == null && m_nvs == null) { continue; } Element ele = root.addElement("values"); ele.addAttribute("level", "" + i + 1); ele.addAttribute("default", m_defaults[i] == null ? "" : m_defaults[i]); addValues(ele, m_nvs[i]); } return doc; } /** * @param _container * @param _values */ private void addValues(Element _container, List _values) { if (_values == null) return; for (Iterator iter = _values.iterator(); iter.hasNext();) { // TreeNamedValue nv = (TreeNamedValue) iter.next(); // Element ele = _container.addElement("value"); } } public void initValues() throws PureException { List nvsFirstLevel = NamedValueHelper.getTreeNamedValues(m_nType, true); TreeNamedValue nvFor = NamedValueHelper.getTreeNamedValue(m_nType, m_sValue, false); if (nvFor == null) { alloc(1); m_nvs[0] = nvsFirstLevel; m_defaults[0] = null; } else { int nForLevel = nvFor.getLevel(); if (nForLevel < 1) nForLevel = 1; alloc(nForLevel + 1); if (nForLevel == 1) { m_nvs[nForLevel - 1] = nvsFirstLevel; m_defaults[nForLevel - 1] = nvFor.getValue(); } else { List nvsSub = NamedValueHelper.getChildren(m_nType, nvFor.getValue(), true); m_nvs[nForLevel] = nvsSub; m_defaults[nForLevel - 1] = nvFor.getValue(); if (!m_bThisLevelOnly) { nForLevel = nvFor.getLevel(); while (true) { nvFor = nvFor.getParent(); if (nvFor == null) { break; } if (nvFor.getLevel() != nForLevel - 1) { throw new PureException(EXCEPTION_BADVALUE, "bad value"); } m_nvs[nForLevel - 1] = NamedValueHelper.getChildren(m_nType, nvFor.getValue(), true); ; m_defaults[nForLevel - 2] = nvFor.getValue(); nForLevel--; } m_nvs[0] = nvsFirstLevel; } } } } private void alloc(int _nSize) { m_nvs = new List[_nSize]; m_defaults = new String[_nSize]; } /** * Constructor * @param _sType * @param _sValue * @param _sThisLevelOnly */ public TreenamedValueQueryAction(int _sType, String _sValue, boolean _sThisLevelOnly) { super(); m_nType = _sType; m_sValue = _sValue; m_bThisLevelOnly = _sThisLevelOnly; } /** * Constructor * */ public TreenamedValueQueryAction() { } }