package com.fr.function.autonum; import java.util.HashMap; import java.util.Map; import com.fr.script.AbstractFunction; import com.fr.script.Calculator; import com.fr.stable.Primitive; /** * 自动编号 * * @author Wu Yujie * @version 1.0.0 */ public class SN extends AbstractFunction { private static final long serialVersionUID = 7021270725442437393L; public static final Object SYNCEXECUTE = new Object(); @SuppressWarnings("unchecked") @Override public Object run(Object[] args) { Object param = null; Calculator calculator = getCalculator(); Object objAtt = calculator.getAttribute(SYNCEXECUTE); Object objMap; if ((objAtt instanceof Map)) { objMap = (Map<?, ?>)objAtt; } else { objMap = new HashMap<Object, Object>(); calculator.setAttribute(SYNCEXECUTE, objMap); } Integer executimes; //执行次数 synchronized (SYNCEXECUTE) { Object objnumber = ((Map<?, ?>)objMap).get(param); if ((objnumber instanceof Integer)) { executimes = new Integer(((Integer)objnumber).intValue() + 1); } else { executimes = new Integer(1); } ((Map<Object, Integer>) objMap).put(param, executimes); } //自动编号数据源==>无,每次运行报表时,在整个报表执行过程中,编号从1开始自动递增 if (args == null || args.length < 1) { return snKey(null,false,executimes,""); } else if(args.length == 1) { String keyformat = args[0].toString(); return snKey(null,false,executimes,keyformat); } //自动编号数据源==>数据库连接,默认最大已用值 +1 else if(args.length == 4) { String connection = args[0].toString(); //数据连接名称 String table = args[1].toString(); //数据库表名称 Object field = args[2]; //自动编号(字符串)所在字段名(或列号) Object[] getKeyParam = {connection,table,field}; String keyformat = args[3].toString(); //自动编号格式 return snKey(getKeyParam,false,executimes,keyformat); } //自动编号数据源==>数据库连接 else if(args.length == 5) { String connection = args[0].toString(); //数据连接名称 String table = args[1].toString(); //数据库表名称 Object field = args[2]; //自动编号(字符串)所在字段名(或列号) Object[] getKeyParam = {connection,table,field}; String keyformat = args[3].toString(); //自动编号格式 boolean minUnused = Boolean.parseBoolean(args[4].toString()); //true:最小未使用值;false:最大已用值 +1 return snKey(getKeyParam,minUnused,executimes,keyformat); } return Primitive.ERROR_NAME; } private static String snKey(Object[] getKeyParam,boolean minUnused,Integer executimes,String... keyforamt){ SNCore snKey = new SNCore(getKeyParam,minUnused,keyforamt); return snKey.creatNextKey(executimes); } } |