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();
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) objMap).put(param, executimes);
}
//自动编号数据源==>无,每次运行报表时,在整个报表执行过程中,编号从1开始自动递增
if (args == null || args.length < 1)
{
return snKey(null,false,executimes,"");
}
else if(args.length == 1)
{
String keyformat = args.toString();
return snKey(null,false,executimes,keyformat);
}
//自动编号数据源==>数据库连接,默认最大已用值 +1
else if(args.length == 4)
{
String connection = args.toString(); //数据连接名称
String table = args.toString(); //数据库表名称
Object field = args; //自动编号(字符串)所在字段名(或列号)
Object getKeyParam = {connection,table,field};
String keyformat = args.toString(); //自动编号格式
return snKey(getKeyParam,false,executimes,keyformat);
}
//自动编号数据源==>数据库连接
else if(args.length == 5)
{
String connection = args.toString(); //数据连接名称
String table = args.toString(); //数据库表名称
Object field = args; //自动编号(字符串)所在字段名(或列号)
Object getKeyParam = {connection,table,field};
String keyformat = args.toString(); //自动编号格式
boolean minUnused = Boolean.parseBoolean(args.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);
}
}