Java tutorial
/** * PureInfo Dolphin * @(#)GetRefPropertyFunctionHandler.java 1.0 Oct 19, 2005 * * Copyright(c) 2004-2005, PureInfo Information Technology Corp. Ltd. * All rights reserved, see the license file. * * www.pureinfo.com.cn */ package com.pureinfo.dolphinview.function.dolphin; import java.util.ArrayList; import java.util.List; import org.apache.commons.lang.StringUtils; import org.apache.log4j.Logger; import com.pureinfo.dolphin.DolphinHelper; import com.pureinfo.dolphin.context.LocalContextHelper; import com.pureinfo.dolphin.model.DolphinObject; import com.pureinfo.dolphin.model.IObjects; import com.pureinfo.dolphin.persister.ISession; import com.pureinfo.dolphin.persister.IStatement; import com.pureinfo.dolphin.persister.util.RelativeUtil; import com.pureinfo.dolphin.query.QueryFilter; import com.pureinfo.dolphin.script.execute.Executer; import com.pureinfo.dolphin.script.function.handler.FunctionHandlerUtil; import com.pureinfo.dolphin.script.lang.ScriptBlock; import com.pureinfo.dolphin.script.lang.ScriptReader; import com.pureinfo.dolphinview.context.model.IDVContext; import com.pureinfo.dolphinview.context.model.SimpleDVContext; import com.pureinfo.dolphinview.parser.function.FunctionHandlerDVImplBase; import com.pureinfo.force.exception.PureException; /** * <P> * Created on Oct 19, 2005 9:52:38 AM <BR> * Last modified on Oct 19, 2005 * </P> * ShowRelativesExFunctionHandler: function handler for showRelativesEx() * * <pre> * <function name="showRelativesEx" class="com.pureinfo.dolphinview.function.dolphin.ShowRelativesExFunctionHandler"> * <desc>Returns the property value of the relative objects.</desc> * <syntax>showRelativesEx(String _sAlias, String _sViewScript, String _sDelim, [, String _sOrderBy[, int _nFrom[, int _nMaxSize]]])</syntax> * <sample> * 1. showRelativesEx("admin", "[${#getProperty(\"id\")}]${#getProperty(\"name\")}", ", ") * 2. showRelativesEx("outlayCard", "[${#getProperty(\"code\")}]${#getProperty(\"name\")}", ", ") * 3. showRelativesEx("outlayCard", "[${#getProperty(\"code\")}]${#getProperty(\"name\")}", ", ", "{outlayCard.id} desc") * 4. showRelativesEx("outlayCard", "[${#getProperty(\"code\")}]${#getProperty(\"name\")}", ", ", "{outlayCard.id} desc", 1) * 5. showRelativesEx("outlayCard", "[${#getProperty(\"code\")}]${#getProperty(\"name\")}", ", ", "{outlayCard.id} desc", 2, 10) * </sample> * <param name="_sAlias">the relative alias</param> * <param name="_sViewScript">the view script for each relative</param> * <param name="_sDelim">the delimiter between relatives</param> * <param name="_sOrderBy">the order for relatives collection</param> * <param name="_nFrom">the first position of the relatives</param> * <param name="_nMaxSize">the total number of the relatives to show </param> * <return> the property value of the relative object.</return> * </function> * </pre> * * @author Why * @version 1.0, Oct 19, 2005 * @since Dolphin 1.0 */ public class ShowRelativesExFunctionHandler extends FunctionHandlerDVImplBase { //logger private Logger logger = Logger.getLogger(ShowRelativesExFunctionHandler.class.getName()); /** the minimum number of arguments */ public final static int MIN_ARG_NUM = 2; public final static int ARG_ALIAS = 0; public final static int ARG_VIEW_SCRIPT = 1; public final static int ARG_DELIM = 2; /** * function argument index: ordery by * @since 1.2 */ public final static int ARG_ORDERBY = 3; /** function argument index: from position in the relatives collection*/ public final static int ARG_POS_FROM = 4; /** function argument index: max size of relatives collection */ public final static int ARG_MAX_SIZE = 5; /** * @see com.pureinfo.dolphinview.parser.function.FunctionHandlerDVImplBase#perform(java.lang.Object[], * com.pureinfo.dolphinview.context.model.IDVContext) */ public Object perform(Object[] _args, IDVContext _context) throws PureException { FunctionHandlerUtil.validateArgsNum(_args, MIN_ARG_NUM); // 1. to read parameters String sAlias = (String) _args[ARG_ALIAS]; String sViewScript = (String) _args[ARG_VIEW_SCRIPT]; ScriptBlock viewScript = ScriptReader.readBlock(sViewScript); String sDelim = (String) _args[ARG_DELIM]; String sOrderBy; if (_args.length > ARG_ORDERBY) { sOrderBy = (String) _args[ARG_ORDERBY]; } else { sOrderBy = null; } int nFrom = 0; int nMaxSize = 0; if (_args.length > ARG_POS_FROM) { nFrom = FunctionHandlerUtil.getIntArg(_args, ARG_POS_FROM, 0); if (nFrom < 0) { nFrom = 0; } if (_args.length > ARG_MAX_SIZE) { nMaxSize = FunctionHandlerUtil.getIntArg(_args, ARG_MAX_SIZE, 0); if (nMaxSize < 0) { nMaxSize = 0; } } } // 2. to find the relative objects StringBuffer sbuff = null; List params = null; IStatement query = null; IObjects objs = null; QueryFilter filter = new QueryFilter(); try { // 2.1 to render query SQL DolphinObject object = _context.getObject(); Class relativeClass = RelativeUtil.renderFilter(object, sAlias, filter); if (StringUtils.isEmpty(filter.getSelect())) { filter.setSelect("{" + sAlias + ".*}"); } if (sOrderBy != null && (sOrderBy = sOrderBy.trim()).length() > 0) { filter.setOrder(sOrderBy); } // 2.2 to prepare query params = new ArrayList(); String strSQL = filter.toSQL(params); logger.debug("SQL: " + strSQL); // 2.3 to execute query ISession session = LocalContextHelper.currentSession(); query = session.createQuery(strSQL, relativeClass, nMaxSize > 0 ? nFrom + nMaxSize : 0); filter.registerEntitiesInQuery(query); query.setParameters(0, params); objs = query.executeQuery(false); if (nFrom > 0) { objs.skip(nFrom); } // 2.4 to render the view content sbuff = new StringBuffer(); while ((object = objs.next()) != null) { if (sbuff.length() > 0) { sbuff.append(sDelim); } SimpleDVContext context = new SimpleDVContext(object); context.setParent(_context); String sResult = Executer.execute(viewScript, context); sbuff.append(sResult); } return sbuff.toString(); } finally { DolphinHelper.clear(objs, query); if (sbuff != null) sbuff.setLength(0); if (params != null) params.clear(); } } }