自定义文件编号如:字母+年份+序列号(如:B2020123)
自定义文件编码规则
package ext.customize;
import wt.rule.algorithm.RuleAlgorithm;
import wt.util.WTException;
import wt.inf.container.WTContainerRef;
import wt.pds.oracle81.OracleDataSource;
import java.util.TimeZone;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.*;
import java.util.*;
/*
* 自定义编号(eg:B2020123)B+年份+三位序号
*/
public class CustomNumberRule implements RuleAlgorithm {
public Object calculate(Object args[], WTContainerRef container) throws WTException {
Calendar gmtlocal = new GregorianCalendar(TimeZone.getTimeZone("GMT+8"));
SimpleDateFormat sf = new SimpleDateFormat("YYYY");
sf.setTimeZone(TimeZone.getTimeZone("GMT+8"));
String head = "A"+sf.format(gmtlocal.getTime());
String num = getDocumentNumber(head);
if (num.length() == 1) {
num = "00" + num;
} else if (num.length() == 2) {
num = "0" + num;
}
return head + num;
}
public static String getDocumentNumber(String head) {
int n = 0;
try {
Connection dbConnection = OracleDataSource.getOracleDataSource().getConnection();
String sql = String.format(
"SELECT WTDOCUMENTNUMBER FROM WTDOCUMENTMASTER WHERE WTDOCUMENTNUMBER LIKE '%s%%' ORDER BY WTDOCUMENTNUMBER DESC",
head);
PreparedStatement ps = dbConnection.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
n = Integer.parseInt(rs.getString(1).substring(5)) + 1;
} else {
n = 1;
}
rs.close();
ps.close();
dbConnection.close();
} catch (SQLException e) {
System.out.println("Please chekc the oracle table ReviewInfo was created or not!");
e.printStackTrace();
}
return n + "";
}
}
将编译好的文件放到对应的文件夹中,然后修改对象初始化规则
更新对象初始化规则文件,修改使用的算法,从:
<AttrValue id="number" algorithm="com.ptc.windchill.enterprise.revisionControlled.server.impl.NumberGenerator">
<Arg>{GEN:wt.enterprise.SequenceGenerator:WTPARTID_seq:10:0}</Arg>
</AttrValue>
修改为:
<AttrValue id="number" algorithm="ext.customize.customNumberRule">
<Arg>{GEN:wt.enterprise.SequenceGenerator:WTPARTID_seq:10:0}</Arg>
</AttrValue>
更新对象的对象初始化规则。