* @return
* @throws IOException
* @throws NotFoundException
*/
public String publishDoc(String docid, String indexid) throws IOException, NotFoundException {
Doc doc = docDAO.queryDocById(docid);
if(doc==null){
logger.error("文章" + docid + "不存在");
return null;
}
PushRecord pushRecord = new PushRecord();
if (StringUtils.isNotBlank(indexid)) {
pushRecord = pushRecordDAO.queryPushRecordById(indexid);
} else {//如果没有传indexid,获取文章的第一个推送记录
List<PushRecord> pushList = pushRecordDAO.queryPushRecordByDocid(docid);
if (CollectionUtils.isNotEmpty(pushList)) {
pushRecord = pushList.get(0);
}
}
String modelName = doc.getModelName();
Model model = modelService.getModelInfoByModelName(modelName);
if (model == null) {
logger.error("文章模板" + modelName + "不存在");
return null;
}
String docContent = velocityService.parseDoc(model.getContent(), doc, pushRecord);
String encoding = jcmsProperties.getOutFileCoding();
File file = JCMSConstant.createDocOutputFile(doc.getUrl());
if (file == null) {
throw new IOException("获取模板uri出错");
}
FileUtils.write(file, docContent, encoding);
//如果文章不是发布状态,设置文章状态
setDocStatus(docid, Doc.docStatus.publish);
//写日志
DocLog docLog = new DocLog();
docLog.setContent(DocLog.DOC_PUBLISH_COMMON);
docLog.setTime(new Date());
docLog.setUserName(doc.getLastModify());
return docContent;
}