Esempio n. 1
0
 public String test(CodeFixture fixture, CodeScript codeScript) {
   try {
     Class<?> codeClass = Class.forName(codeScript.getCodeClassName());
     Entity<?> entity = (Entity<?>) codeClass.newInstance();
     PropertyUtils.getProperty(entity, codeScript.getAttr());
     if (null != fixture) {
       for (Iterator<?> iter = fixture.getParams().keySet().iterator(); iter.hasNext(); ) {
         String param = (String) iter.next();
         interpreter.set(param, fixture.getParams().get(param));
       }
       if (Strings.isNotEmpty(fixture.getScript())) {
         interpreter.eval(fixture.getScript());
       }
     }
     return gen(new CodeFixture(entity, codeScript.getScript()));
   } catch (Exception e) {
     return Throwables.getStackTrace(e);
   }
 }
Esempio n. 2
0
  /** 根据请求的实体,生成代码 */
  @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 (Strings.contains(script, SEQ)) {
      seqLength = Numbers.toInt(Strings.substringBetween(script, SEQ + "[", "]"));

      script =
          Strings.replace(
              script, SEQ + "[" + Strings.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",
            Strings.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);
      }
      synchronized (this) {
        int newSeqNo = 0;
        for (Iterator<String> iter = seqs.iterator(); iter.hasNext(); ) {
          String seqNo = iter.next();
          if (Numbers.toInt(seqNo) - newSeqNo >= 2) {
            break;
          } else {
            newSeqNo = Numbers.toInt(seqNo);
          }
        }
        newSeqNo++;
        String seqNo = String.valueOf(newSeqNo);
        if (0 != seqLength) {
          seqNo = Strings.repeat("0", seqLength - seqNo.length()) + newSeqNo;
        }
        code = Strings.replace(code, SEQ, seqNo);
      }
    }
    return code;
  }