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.function.handler.FunctionHandlerUtil; import com.pureinfo.dolphinview.context.model.IDVContext; 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> * ShowRelativesFunctionHandler: function handler for showRelatives() * * <pre> * <function name="showRelatives" class="com.pureinfo.dolphinview.function.dolphin.ShowRelativesFunctionHandler"> * <desc>Returns the property value of the relative objects.</desc> * <syntax>showRelatives(String _sAlias, String _sRelativeProperty, String _sDelim, [, String _sOrderBy[, int _nFrom[, int _nMaxSize]]])</syntax> * <sample> * 1. showRelatives("admin", "name", ", ") * 2. showRelatives("outlayCard", "code", ", ") * 3. showRelatives("outlayCard", "code", ", ", "{outlayCard.id} desc") * 4. showRelatives("outlayCard", "code", ", ", "{outlayCard.id} desc", 1) * 5. showRelatives("outlayCard", "code", ", ", "{outlayCard.id} desc", 2, 10) * </sample> * <param name="_sAlias">the relative alias</param> * <param name="_sRelativeProperty">the relative object property name to show </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 ShowRelativesFunctionHandler extends FunctionHandlerDVImplBase { //logger private Logger logger = Logger.getLogger(ShowRelativesFunctionHandler.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_PROPERTY_NAME = 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 */ public final static int ARG_POS_FROM = 4; 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 sProperty = (String) _args[ARG_PROPERTY_NAME]; 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 object 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 + "." + sProperty + "}"); } 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); } sbuff.append(object.getProperty(sProperty)); } return sbuff.toString(); } finally { DolphinHelper.clear(objs, query); if (sbuff != null) sbuff.setLength(0); if (params != null) params.clear(); } } }