📄 makenewslistimpl.java.svn-base
字号:
package com.suncms.service.freemarker;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.suncms.domain.Article;
import com.suncms.domain.Article_Class;
import com.suncms.domain.Index;
import com.suncms.domain.NewsFtl;
import com.suncms.domain.NewsList;
import com.suncms.domain.Vote_q;
import com.suncms.persistence.iface.ArticleClassDao;
import com.suncms.persistence.iface.ArticleDao;
import com.suncms.persistence.iface.VoteqDao;
import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;
import freemarker.template.TemplateException;
public class MakeNewsListImpl implements MakeNewsList {
private ArticleDao articleDao;
private ArticleClassDao articleclassDao;
private VoteqDao voteqDao;
private int perPage = 15;
public ArticleClassDao getArticleclassDao() {
return articleclassDao;
}
public VoteqDao getVoteqDao() {
return voteqDao;
}
public void setVoteqDao(VoteqDao voteqDao) {
this.voteqDao = voteqDao;
}
public void setArticleclassDao(ArticleClassDao articleclassDao) {
this.articleclassDao = articleclassDao;
}
public ArticleDao getArticleDao() {
return articleDao;
}
public void setArticleDao(ArticleDao articleDao) {
this.articleDao = articleDao;
}
public void make(NewsList nl, NewsFtl newsFtl) {
Configuration cfg = new Configuration();
String realPath = newsFtl.getRealPath();
String templatePath = realPath + "/WEB-INF/templates/news";
String cDateStr = "news/" + nl.getClass_id();
String path = realPath + "/" + cDateStr;
// String fileTimeName = PureUtil.getDateFormatStr("yyyyMMddhhmmss");
// String returnFileName = cDateStr + "/" + fileTimeName + filePostfix;
String fileName = "";
File newsDir = new File(path);
if (newsDir.exists()) {
fileName = path + "/" + nl.getFileName();
} else {
newsDir.mkdirs();
fileName = path + "/" + nl.getFileName();
}
try {
cfg.setDirectoryForTemplateLoading(new File(templatePath));
cfg.setObjectWrapper(new DefaultObjectWrapper());
Template newsTemplate = cfg.getTemplate(newsFtl.getTemplateName());
Map root = new HashMap();
root.put("news", nl);
Writer out = new OutputStreamWriter(new FileOutputStream(fileName));
try {
newsTemplate.process(root, out);
} catch (TemplateException e) {
e.printStackTrace();
}
out.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
public void makelist(Article_Class ac, NewsFtl newsFtl) {
try {
String filePostfix = ".html";
String classid=ac.getRow_id();
String claaname=ac.getClass_name();
String parclaaname=ac.getPar_class_name();
String Par_Id=ac.getPar_class_id();
List<Article> artlist = articleDao.getArticleByClass(classid);
List<Article_Class> aclist = articleclassDao.getArticleClassByParId(Par_Id);
//if(artlist==null || artlist.size()==0){
// return;
//}
int total = articleDao.getTotalCountByClass(classid);
int totalPage = (total + perPage - 1) / perPage;
int pageend = 0;
for (int i = 1; i <= totalPage; i++) {
List<Article> tmp = new ArrayList<Article>();
NewsList nl = new NewsList();
if (i == totalPage) {
pageend = total - (i - 1) * perPage;
} else {
pageend = perPage;
}
for (int j = (i - 1) * perPage; j < (i - 1) * perPage
+ pageend; j++) {
tmp.add(artlist.get(j));
}
nl.setArticleList(tmp);
nl.setClass_name(claaname);
nl.setClass_id(classid);
nl.setTotal(total);
nl.setPar_class_name(parclaaname);
String firstPage;
String prvPage;
String nextPage;
String lastPage;
String page;
String allPage;
String numPage;
if (i == 1) {
firstPage = "#";
prvPage = "#";
} else {
firstPage = classid + "_1" + filePostfix;
prvPage = classid + "_" + (i - 1) + filePostfix;
}
if (i == totalPage) {
nextPage = "#";
lastPage = "#";
} else {
nextPage = classid + "_" + (i + 1) + filePostfix;
lastPage = classid + "_" + totalPage + filePostfix;
}
nl.setFirstPage(firstPage);
nl.setPrvPage(prvPage);
nl.setNextPage(nextPage);
nl.setLastPage(lastPage);
nl.setPage(i);
nl.setAllPage(totalPage);
nl.setNumPage(perPage);
String fileName = classid + "_" + i + filePostfix;
nl.setFileName(fileName);
nl.setAcList(aclist);
make(nl, newsFtl);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void makechannel(String[] class_id, String[] class_name, NewsFtl newsFtl) {
try {
for (int k = 0; k < class_id.length; k++) {
String classid=class_id[k];
List tmp = articleclassDao.getArticleClassByParId(classid);
Article_Class ac = articleclassDao.getArticleClassById(classid);
if(tmp!=null && tmp.size()>0){
makeparlist(ac,newsFtl);
}else{
makelist(ac,newsFtl);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void makeparlist(Article_Class ac,NewsFtl newsFtl) {
Configuration cfg = new Configuration();
String realPath = newsFtl.getRealPath();
String templatePath = realPath + "/WEB-INF/templates/news";
String cDateStr = "news/" + ac.getRow_id();
String path = realPath + "/" + cDateStr;
// String fileTimeName = PureUtil.getDateFormatStr("yyyyMMddhhmmss");
// String returnFileName = cDateStr + "/" + fileTimeName + filePostfix;
String classid=ac.getRow_id();
String filePostfix = ".html";
String fileName = classid + "_1" + filePostfix;
File newsDir = new File(path);
if (newsDir.exists()) {
fileName = path + "/" + fileName;
} else {
newsDir.mkdirs();
fileName = path + "/" + fileName;
}
try {
cfg.setDirectoryForTemplateLoading(new File(templatePath));
cfg.setObjectWrapper(new DefaultObjectWrapper());
Template newsTemplate = cfg.getTemplate(newsFtl.getTemplateName2());
Map root = new HashMap();
List<Index> indexinfo = articleclassDao.getIndexInfoByPar(classid);
for(Index index:indexinfo){
List<Article> artlist = index.getArtlist();
int i=0;
while(i<5-artlist.size()){
Article art=new Article();
art.setArticle_title("");
art.setArticle_file("");
art.setArticle_content("");
artlist.add(art);
}
}
root.put("news", indexinfo);
root.put("ac", ac);
Writer out = new OutputStreamWriter(new FileOutputStream(fileName));
try {
newsTemplate.process(root, out);
} catch (TemplateException e) {
e.printStackTrace();
}
out.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
public void makeindex(NewsFtl newsFtl) {
Configuration cfg = new Configuration();
String realPath = newsFtl.getRealPath();
String templatePath = realPath + "/WEB-INF/templates/news";
//String cDateStr = "news/" + nl.getClass_id();
String path = realPath + "/" + "news";
// String fileTimeName = PureUtil.getDateFormatStr("yyyyMMddhhmmss");
// String returnFileName = cDateStr + "/" + fileTimeName + filePostfix;
String fileName = "";
String fileName1 = "";
File newsDir = new File(path);
if (newsDir.exists()) {
fileName = path + "/" + "index.html";
fileName1 = path + "/" + "top.html";
} else {
newsDir.mkdirs();
fileName = path + "/" + "index.html";
fileName1 = path + "/" + "top.html";
}
try {
cfg.setDirectoryForTemplateLoading(new File(templatePath));
cfg.setObjectWrapper(new DefaultObjectWrapper());
Template newsTemplate = cfg.getTemplate(newsFtl.getTemplateName());
Template newsTemplate1 = cfg.getTemplate(newsFtl.getTemplateName1());
Map root = new HashMap();
List<Index> indexinfo = articleclassDao.getIndexInfo();
for(Index index:indexinfo){
List<Article> artlist = index.getArtlist();
int i=0;
while(i<5-artlist.size()){
Article art=new Article();
art.setArticle_title("");
art.setArticle_file("");
art.setArticle_content("");
artlist.add(art);
}
}
root.put("news", indexinfo);
Writer out = new OutputStreamWriter(new FileOutputStream(fileName));
Writer out1 = new OutputStreamWriter(new FileOutputStream(fileName1));
try {
newsTemplate.process(root, out);
newsTemplate1.process(root, out1);
} catch (TemplateException e) {
e.printStackTrace();
}
out.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
public void makevote(NewsFtl newsFtl) {
Configuration cfg = new Configuration();
String realPath = newsFtl.getRealPath();
String templatePath = realPath + "/WEB-INF/templates/news";
//String cDateStr = "news/" + nl.getClass_id();
String path = realPath + "/" + "news";
// String fileTimeName = PureUtil.getDateFormatStr("yyyyMMddhhmmss");
// String returnFileName = cDateStr + "/" + fileTimeName + filePostfix;
String fileName = "";
File newsDir = new File(path);
if (newsDir.exists()) {
fileName = path + "/" + "vote.html";
} else {
newsDir.mkdirs();
fileName = path + "/" + "vote.html";
}
try {
cfg.setDirectoryForTemplateLoading(new File(templatePath));
cfg.setObjectWrapper(new DefaultObjectWrapper());
Template newsTemplate = cfg.getTemplate(newsFtl.getTemplateName());
Map root = new HashMap();
Vote_q vq=voteqDao.getVoteqByF();
/*
List<Index> indexinfo = articleclassDao.getIndexInfo();
for(Index index:indexinfo){
List<Article> artlist = index.getArtlist();
int i=0;
while(i<5-artlist.size()){
Article art=new Article();
art.setArticle_title("");
art.setArticle_file("");
art.setArticle_content("");
artlist.add(art);
}
}
*/
root.put("vote", vq);
Writer out = new OutputStreamWriter(new FileOutputStream(fileName));
try {
newsTemplate.process(root, out);
} catch (TemplateException e) {
e.printStackTrace();
}
out.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -