*/
@SuppressWarnings("unchecked")
public String gen(CodeFixture fixture) {
// 在必要时查找相应的生成脚本
String script = fixture.getScript();
CodeScript codeScript = null;
if (null == script) {
codeScript = getCodeScript(EntityUtils.getEntityClassName(fixture.getEntity().getClass()));
if (null == codeScript) { return null; }
script = codeScript.getScript();
try {
String code = (String) PropertyUtils.getProperty(fixture.getEntity(), codeScript.getAttr());
if (isValidCode(code)) { return code; }
} catch (Exception e) {
throw new RuntimeException(e);
}
}
int seqLength = -1;
// 替换自动代码生成中的seq[x]
if (StringUtils.contains(script, SEQ)) {
seqLength = NumberUtils.toInt(StringUtils.substringBetween(script, SEQ + "[", "]"));
script = StringUtils.replace(script,
SEQ + "[" + StringUtils.substringBetween(script, SEQ + "[", "]") + "]", SEQ);
}
fixture.setScript(script);
String code = super.gen(fixture);
List<String> seqs = CollectUtils.newArrayList();
if (-1 != seqLength) {
try {
OqlBuilder<?> builder = OqlBuilder.from(Class.forName(codeScript.getCodeClassName()),
"entity");
builder.select("select substr(entity." + codeScript.getAttr() + "," + (code.indexOf(SEQ) + 1)
+ "," + seqLength + ")");
builder.where(" entity." + codeScript.getAttr() + " like :codeExample",
StringUtils.replace(code, SEQ, "%"));
builder.where("length(entity." + codeScript.getAttr() + ")="
+ (code.length() - SEQ.length() + seqLength));
seqs = (List<String>) entityDao.search(builder);
Collections.sort(seqs);
} catch (Exception e) {
throw new RuntimeException(e);