Esempio n. 1
0
 protected CodeScript getCodeScript(String entityName) {
   OqlBuilder<CodeScript> builder = OqlBuilder.from(CodeScript.class, "codeScript");
   builder.where("codeScript.codeClassName=:codeClassName", entityName);
   builder.cacheable();
   List<CodeScript> scripts = entityDao.search(builder);
   if (scripts.size() != 1) {
     return null;
   } else {
     return scripts.get(0);
   }
 }
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;
  }