public void saveTpl(Template template) {
   Template oldTpl = this.templateDao.loadByCodeAndId(template.getCode(), template.getId());
   if (oldTpl != null) {
     oldTpl.setStatus(BCConstants.STATUS_DISABLED);
     this.templateDao.save(oldTpl);
   }
   this.templateDao.save(template);
 }
  public InputStream getInputStream(String code) {
    Template tpl = loadByCode(code);
    if (tpl == null) {
      logger.warn("没有找到编码为'" + code + "'的模板!");
      return null;
    }

    return tpl.getInputStream();
  }
 @Override
 public void delete(Serializable id) {
   Template entity = this.templateDao.load(id);
   this.operateLogService.saveWorkLog(
       Template.class.getSimpleName(),
       entity.getId().toString(),
       "删除模板:" + entity.getSubject(),
       null,
       OperateLog.OPERATE_DELETE);
   super.delete(id);
 }
  public Map<String, Object> getMapParams(Long id, Map<String, Object> mapFormatSql) {
    Template tpl = this.templateDao.load(id);
    if (tpl == null) throw new CoreException("template is not exists");

    // 取参数集合
    Set<TemplateParam> tplps = tpl.getParams();
    // 没配置模板参数处理
    if (tplps == null || tplps.isEmpty()) return null;

    Map<String, Object> formattedMap = new HashMap<String, Object>();
    Map<String, Object> tempMap;
    // 遍历模板参数集合获取格式化的替换参数
    for (TemplateParam tp : tplps) {
      tempMap = templateParamService.getMapParams(tp, mapFormatSql);
      if (tempMap != null) formattedMap.putAll(tempMap);
    }
    return formattedMap.isEmpty() ? null : formattedMap;
  }
Ejemplo n.º 5
0
  private void sendEmail(SubscribeEvent event, List<Actor> actors, OperateLog worklog) {
    // 工作日志的详细内容
    String worklog_content = worklog.getContent();
    Email email = new Email();
    worklog_content += "发送方式:邮件[";

    String emailUid = this.idGeneratorService.nexttvak(Email.ATTACH_TYPE);
    email.setUid(emailUid);
    email.setStatus(Email.STATUS_SENDED);
    email.setType(Email.TYPE_NEW);
    email.setFileDate(Calendar.getInstance());
    email.setSendDate(Calendar.getInstance());
    email.setSubject(event.getSubject());
    worklog_content += "邮件主题: " + event.getSubject();

    Map<String, Object> args = new HashMap<String, Object>();
    args.put("content", event.getContent());

    // 根据事件的CODE 加上 “-EMAIL”后缀 查找模板中是否有配置 自定义的模板
    Template custom = this.templateService.loadByCode(event.getCode() + "-EMAIL");

    if (custom != null) {
      email.setContent(this.templateService.format(custom.getCode(), args));
    } else { // 使用默认的邮件模板
      email.setContent(this.templateService.format("BC-EMAIL-SYSTEMAUTOFORWARD", args));
    }
    worklog_content += ",邮件内容: " + event.getContent();
    // 系统管理员发送
    Actor admin = this.actorService.loadByCode("admin");
    email.setSender(admin);

    if (event.getAttachs() != null) {
      for (Attach attach : event.getAttachs()) {
        // 复制附件
        this.attachService.doCopy(
            attach.getPtype(), attach.getPuid(), Email.ATTACH_TYPE, emailUid, true);
      }
    }

    // 设置发送人
    Set<EmailTo> emailTos = new HashSet<EmailTo>();
    worklog_content += ",邮件接收人:";
    EmailTo et;
    int i = 0;
    for (Actor a : actors) {
      et = new EmailTo();
      et.setEmail(email);
      et.setRead(false);
      et.setReceiver(a);
      // 正常发送
      et.setType(EmailTo.TYPE_TO);
      et.setOrderNo(i);
      emailTos.add(et);

      if (i > 0) worklog_content += "、";
      worklog_content += a.getName();
      i++;
    }
    worklog_content += "]";
    worklog.setContent(worklog_content);
    email.setTos(emailTos);

    this.emailService.save(email);
  }
  public String getContent(String code) {
    Template tpl = loadByCode(code);
    if (tpl == null) {
      logger.warn("没有找到编码为'" + code + "'的模板!");
      return null;
    }
    // 纯文本类型
    if (tpl.isPureText()) return tpl.getContentEx();
    // 附件的扩展名
    String extension = StringUtils.getFilenameExtension(tpl.getPath());
    if (tpl.getTemplateType().getCode().equals("word-docx") && extension.equals("docx"))
      return DocxUtils.loadText(tpl.getInputStream());

    if (tpl.getTemplateType().getCode().equals("xls") && extension.equals("xls"))
      return XlsUtils.loadText(tpl.getInputStream());

    if (tpl.getTemplateType().getCode().equals("xlsx") && extension.equals("xlsx"))
      return XlsxUtils.loadText(tpl.getInputStream());

    if (tpl.getTemplateType().getCode().equals("html") && extension.equals("html"))
      return TemplateUtils.loadText(tpl.getInputStream());

    logger.warn("文件后缀名:" + extension + ",未转换为字符串类型");
    return null;
  }
  public Attach getAttach(
      String subject,
      String code,
      Map<String, Object> args,
      String ptype,
      String puid,
      ActorHistory author,
      Map<String, Object> formatParamSql)
      throws Exception {
    Assert.hasText(subject, "subject is Empty");
    Assert.hasText(code, "code is Empty");
    Assert.hasText(ptype, "ptype is Empty");
    Assert.hasText(puid, "puid is Empty");

    Template template = this.templateDao.loadByCode(code);
    if (template == null) throw new CoreException("Template code:" + code + " not find entity!");

    if (args == null) args = new HashMap<String, Object>();

    if (author == null) {
      if (SystemContextHolder.get() == null) {
        author = this.actorHistoryService.loadByCode("admin");
      } else {
        author = SystemContextHolder.get().getUserHistory();
      }
    }

    Map<String, Object> args4Param = this.getMapParams(template.getId(), formatParamSql);

    if (args4Param != null)
      // 最终替换参数
      args.putAll(this.getMapParams(template.getId(), formatParamSql));

    // 生成附件
    Attach attach = new Attach();
    attach.setAuthor(author);
    attach.setFileDate(Calendar.getInstance());
    attach.setSubject(subject);
    attach.setAppPath(false);
    attach.setFormat(template.getTemplateType().getExtension());
    attach.setStatus(BCConstants.STATUS_ENABLED);
    attach.setPtype(ptype);
    attach.setPuid(puid);

    // 文件存储的相对路径(年月),避免超出目录内文件数的限制
    Calendar now = Calendar.getInstance();
    String datedir = new SimpleDateFormat("yyyyMM").format(now.getTime());

    // 要保存的物理文件
    String realpath; // 绝对路径名
    // uuid
    String fileName =
        UUID.randomUUID().toString().replace("-", "")
            + "."
            + template.getTemplateType().getExtension(); // 不含路径的文件名
    realpath = Attach.DATA_REAL_PATH + "/" + datedir + "/" + fileName;

    // 构建文件要保存到的目录
    File file = new File(realpath);
    if (!file.getParentFile().exists()) {
      if (logger.isInfoEnabled()) {
        logger.info("mkdir=" + file.getParentFile().getAbsolutePath());
      }
      file.getParentFile().mkdirs();
    }

    OutputStream out = new BufferedOutputStream(new FileOutputStream(file));

    this.formatTo(template.getCode(), args, out);

    // 设置附件大小
    attach.setSize(new File(realpath).length());

    // 设置附件相对路径
    attach.setPath(datedir + "/" + fileName);

    return this.attachService.save(attach);
  }
  @Override
  public Template save(Template entity) {
    String subject = "";
    String operate = OperateLog.OPERATE_CREATE;
    if (entity.isNew()) {
      subject = "新建模板:" + entity.getSubject();
    } else {
      subject = "更新模板:" + entity.getSubject();
      operate = OperateLog.OPERATE_UPDATE;
    }

    // 正常
    if (entity.getStatus() == BCConstants.STATUS_ENABLED) {
      Template oldTpl = this.templateDao.loadByCodeAndId(entity.getCode(), entity.getId());
      if (oldTpl != null) {
        this.operateLogService.saveWorkLog(
            Template.class.getSimpleName(),
            oldTpl.getId().toString(),
            "禁用旧模板:" + oldTpl.getSubject(),
            null,
            OperateLog.OPERATE_UPDATE);
        oldTpl.setStatus(BCConstants.STATUS_DISABLED);
        this.templateDao.save(oldTpl);
      }
    }
    entity = this.templateDao.save(entity);
    this.operateLogService.saveWorkLog(
        Template.class.getSimpleName(), entity.getId().toString(), subject, null, operate);
    return entity;
  }
  public void formatTo(String code, Map<String, Object> args, OutputStream out) {
    Template tpl = loadByCode(code);
    if (tpl == null) logger.warn("没有找到编码为'" + code + "'的模板!");
    if (tpl.isFormatted()) logger.warn("code=" + code + ",模板不可格式化。");

    // 纯文本类型
    if (tpl.isPureText()) {
      String source = this.getContent(code);
      String r = TemplateUtils.format(source, args);
      try {
        out.write(r.getBytes());
        out.flush();
      } catch (IOException e) {
        logger.warn("formatTo 写入数据到流错误:" + e.getMessage());
      } finally {
        try {
          out.close();
        } catch (IOException ex) {
        }
      }
    } else {
      InputStream is = tpl.getInputStream();

      // 附件的扩展名
      String extension = StringUtils.getFilenameExtension(tpl.getPath());

      if ("word-docx".equals(tpl.getTemplateType().getCode()) && "docx".equals(extension)) {
        XWPFDocument docx = DocxUtils.format(is, args);
        try {
          docx.write(out);
          out.flush();
        } catch (IOException e) {
          logger.warn("formatTo 写入数据到流错误:" + e.getMessage());
        } finally {
          try {
            is.close();
            out.close();
          } catch (IOException ex) {
          }
        }

      } else if ("xls".equals(tpl.getTemplateType().getCode()) && "xls".equals(extension)) {
        HSSFWorkbook xls = XlsUtils.format(is, args);
        try {
          xls.write(out);
          out.flush();
        } catch (IOException e) {
          e.printStackTrace();
          logger.warn("formatTo 写入数据到流错误:" + e.getMessage());
        } finally {
          try {
            is.close();
            out.close();
          } catch (IOException ex) {
          }
        }

      } else if ("xlsx".equals(tpl.getTemplateType().getCode())
          && "xlsx".equals(extension)) { // Excel2007+

        XSSFWorkbook xlsx = XlsxUtils.format(is, args);
        try {
          xlsx.write(out);
          out.flush();
        } catch (IOException e) {
          e.printStackTrace();
          logger.warn("formatTo 写入数据到流错误:" + e.getMessage());
        } finally {
          try {
            is.close();
            out.close();
          } catch (IOException ex) {
          }
        }

      } else if ("html".equals(tpl.getTemplateType().getCode())
          && "html".equals(extension)) { // html
        String source = FreeMarkerUtils.format(TemplateUtils.loadText(is), args);
        try {
          out.write(source.getBytes());
          out.flush();
        } catch (IOException e) {
          e.printStackTrace();
          logger.warn("formatTo 写入数据到流错误:" + e.getMessage());
        } finally {
          try {
            is.close();
            out.close();
          } catch (IOException ex) {
          }
        }
      } else {
        logger.warn("文件后缀名:" + extension + ",不能formatTo");
        try {
          is.close();
          out.close();
        } catch (IOException ex) {
        }
      }
    }
  }
Ejemplo n.º 10
0
  public Attach doGetAttachFromTemplate(Long id, String templateCode) throws IOException {
    TempDriver tempDriver = this.load(id);

    // 获取模板
    Template template = this.templateService.loadByCode(templateCode);
    if (template == null) {
      logger.error("模板不存在,返回null:code=" + templateCode);
      throw new CoreException("模板不存在,code=" + templateCode);
    }

    String ptype = TempDriver.ATTACH_TYPE;
    String puid = tempDriver.getUid();

    // 不能格式化
    if (!template.isFormatted()) {
      Attach attach = template.format2Attach(null, ptype, puid);
      this.attachService.save(attach);
      return attach;
    }

    // 声明格式化参数
    Map<String, Object> params = new HashMap<String, Object>();
    params = this.returnParam(tempDriver, params);

    // 修改信誉档案的显示
    String credit = params.get("credit").toString();
    if (!"".equals(credit)) {
      credit =
          credit.replace(
              "bc/attach/", SystemContextHolder.get().getAttr("htmlPageNamespace") + "/bc/attach/");
      credit = credit.replaceAll("<font size=\"\\d\">", "");
      credit = credit.replace("</font>", "");
      credit = credit.replace("padding-right:8px;", "");
      params.put("credit", credit);
    }

    params.put("sex", tempDriver.getSex() == 1 ? "男" : "女");

    params.put("age", DateUtils.getAge(tempDriver.getBirthdate()));

    // 出生日期格式化
    date4key("birthdate", tempDriver.getBirthdate(), params);
    // 驾驶证初领日期
    date4key("certDrivingFirstDate", tempDriver.getCertDrivingFirstDate(), params);
    // 驾驶证起效日期
    date4key("certDrivingStartDate", tempDriver.getCertDrivingStartDate(), params);
    // 驾驶证无效日期
    date4key("certDrivingEndDate", tempDriver.getCertDrivingEndDate(), params);

    // 根据模板参数获取的替换值
    Map<String, Object> mapFormatSql = new HashMap<String, Object>();
    mapFormatSql.put("id", id);
    Map<String, Object> mapParams = templateService.getMapParams(template.getId(), mapFormatSql);
    if (mapParams != null) params.putAll(mapParams);

    // 加载系统上下文属性
    params.put(SystemContext.class.getSimpleName(), SystemContextHolder.get());

    Attach attach = template.format2Attach(params, ptype, puid);
    return attach;
  }