Java tutorial
/** * PureInfo Command * @(#)ShowNamedValueOptionsNewFunctionHandler.java 1.0 2007-10-8 * * 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.view.function; import java.util.Iterator; import java.util.List; import org.apache.commons.lang.ArrayUtils; import org.apache.log4j.Logger; import com.pureinfo.ark.content.ArkContentHelper; import com.pureinfo.ark.content.domain.IContentMgr; import com.pureinfo.common.namedvalue.NamedValueHelper; import com.pureinfo.common.namedvalue.model.INamedValue; import com.pureinfo.common.namedvalue.model.ListNamedValue; import com.pureinfo.common.namedvalue.model.TreeNamedValue; import com.pureinfo.dolphin.DolphinHelper; import com.pureinfo.dolphin.mapping.EntityMetadata; import com.pureinfo.dolphin.model.DolphinObject; import com.pureinfo.dolphin.model.IObjects; import com.pureinfo.dolphin.persister.IStatement; import com.pureinfo.dolphin.script.function.handler.FunctionHandlerUtil; import com.pureinfo.dolphinview.context.model.IDVContext; import com.pureinfo.dolphinview.parser.function.FunctionHandlerDVImplBase; import com.pureinfo.force.exception.PureException; import com.pureinfo.srm.view.function.PatternedStringRenderDolObjAdpter; public class ShowNamedValueOptionsNewFunctionHandler extends FunctionHandlerDVImplBase { private static Logger logger = Logger.getLogger(ShowNamedValueOptionsNewFunctionHandler.class); /** minimum number of arguments */ public final static int MIN_ARGS_NUM = 3; /** index of argument: value of named value */ private final static int ARG_VALUE = 0; /** index of argument: value type */ private final static int ARG_TYPE = 1; /** index of argument: multiple */ private final static int ARG_MULTIPLE = 2; private final static int ARG_SHOW_WHAT = 3; private final static int ARG_SHOW_HEAD = 4; //private final static int ARG_SHOW_INCLUDE = 5; private final static int ARG_FILTER = 6; private final static int VALUE_BIT = 1; private final static int NAME_BIT = 2; private final static int DESC_BIT = 4; private final static int ARG_NAME = 7; public Object perform(Object[] _args, IDVContext _context) throws PureException { FunctionHandlerUtil.validateArgsNum(_args, MIN_ARGS_NUM); String sValue = String.valueOf(_args[ARG_VALUE]); int nValueType = FunctionHandlerUtil.getIntArg(_args, ARG_TYPE, 0); boolean bMultiple = FunctionHandlerUtil.getBooleanArg(_args, ARG_MULTIPLE); Object oShowWhat = null; if (_args.length > ARG_SHOW_WHAT) { oShowWhat = _args[ARG_SHOW_WHAT]; } boolean showHead = true; if (_args.length > ARG_SHOW_HEAD) { showHead = FunctionHandlerUtil.getBooleanArg(_args, ARG_SHOW_HEAD); } String sFilter = null; if (_args.length > ARG_FILTER) { sFilter = (String) _args[ARG_FILTER]; } String sName = ""; if (_args.length > ARG_NAME) { sName = (String) _args[ARG_NAME]; } List nvs = getNamedValues(nValueType, sFilter); return doPerform(sValue, bMultiple, nvs, oShowWhat, showHead, sName); } /** * @param _nValueType * @param _sFilter * @return * @throws PureException */ public static List getNamedValues(int _nValueType, String _sFilter) throws PureException { if (_sFilter == null || _sFilter.trim().length() == 0) { return NamedValueHelper.getNamedValues(_nValueType, true); } boolean isTree = NamedValueHelper.isTreeValue(_nValueType); Class contentClass = isTree ? TreeNamedValue.class : ListNamedValue.class; EntityMetadata metadata = DolphinHelper.lookupEntityMetadataByName(contentClass.getName(), true); String sOrder = metadata.getRenderedOrder("this"); IContentMgr mgr = ArkContentHelper.getContentMgrOf(contentClass); String sStr = "SELECT {this.*} FROM {this} WHERE {this.active}=1 and {this.type} = ? and " + _sFilter; if (isTree) sStr += "and {this.level}=1 "; if (sOrder != null) { sStr += " order by " + sOrder; } IObjects nvs = null; IStatement stat = null; try { stat = mgr.createQuery(sStr, 0); stat.setInt(0, _nValueType); nvs = stat.executeQuery(); return nvs.toList(); } finally { DolphinHelper.clear(nvs, stat); } } /** * * @param _sValue * @param _bMultiple * @param _nValueType * @param _bShowHead * @param _sStyle * @param _sName * @return */ private String doPerform(String _sValue, boolean _bMultiple, List _nvs, Object _oShowWhat, boolean _bShowHead, String _sName) throws PureException { String[] arrValues = _sValue == null ? null : _sValue.split(NamedValueHelper.VALUE_SEPARATOR); StringBuffer sbuff = new StringBuffer(); try { if (!_bMultiple && _bShowHead) { sbuff.append("<option value=\"\"> -- --\n"); } for (Iterator iter = _nvs.iterator(); iter.hasNext();) { INamedValue element = (INamedValue) iter.next(); String sShow = rendShow(_oShowWhat, element); String sCode = element.getValue(); sbuff.append("<option ").append("value='").append(sCode).append('\''); if (arrValues != null && ArrayUtils.contains(arrValues, sCode)) { sbuff.append(" selected"); } sbuff.append('>').append(sShow).append("</option>\n"); } return sbuff.toString(); } finally { sbuff.setLength(0); if (_nvs != null) _nvs.clear(); } } /** * @param _oShowWhat * @param _nv * @return */ private String rendShow(Object _oShowWhat, INamedValue _nv) { if (_oShowWhat == null) { return _nv.getName(); } if (_oShowWhat instanceof Number) { return rendShow(((Number) _oShowWhat).intValue(), _nv); } String sShowWhat = _oShowWhat.toString(); if (sShowWhat.matches("[0-9]+")) { return rendShow(Integer.parseInt(sShowWhat), _nv); } return rendShow(sShowWhat, _nv); } private String rendShow(String _oShowWhat, INamedValue _nv) { PatternedStringRenderDolObjAdpter r = new PatternedStringRenderDolObjAdpter((DolphinObject) _nv); return r.render(_oShowWhat); } private String rendShow(int _nShowWhat, INamedValue element) { String sShow = ""; int i = 0; String[] strs = new String[3]; if ((VALUE_BIT & _nShowWhat) != 0) { strs[i++] = element.getValue(); } if ((NAME_BIT & _nShowWhat) != 0) { strs[i++] = element.getName(); } if ((DESC_BIT & _nShowWhat) != 0) { strs[i] = element.getDescription(); if (strs[i] == null) { strs[i] = ""; } i++; } for (int j = 0; j < i; j++) { if (j > 0) { sShow += " -- "; } sShow += strs[j]; } return sShow; } }