package com.paic.wcm.search.admin.web.servlet;
import java.io.IOException;
import java.text.ParseException;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import com.paic.wcm.search.admin.service.SitesIndexService;
import com.paic.wcm.search.utils.SecurityUtil;
public class SitesIndexServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public SitesIndexServlet() {
super();
}
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doPost(req, resp);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String actionType = request.getParameter("actionType");
String user = (String) request.getSession().getAttribute("user");
/* if(!request.getSession().getAttribute("redom").equals(request.getParameter("redom"))){
request.getRequestDispatcher("/admin/sitesIndex.jsp").forward(
request, response);
return;
}*/
if ("insert".equals(actionType)) {
/*
* insert
*/
String link = StringUtils.trimToEmpty(request.getParameter("link"));
String title = StringUtils.trimToEmpty(request
.getParameter("title"));
String keywords = StringUtils.trimToEmpty(request
.getParameter("keywords"));
String desc = StringUtils.trimToEmpty(request.getParameter("desc"));
String content = StringUtils.trimToEmpty(request
.getParameter("content"));
String tags = StringUtils.trimToEmpty(request.getParameter("tags"));
String type = "";
String site = StringUtils.trimToEmpty(request.getParameter("site"));
// 拆分site和type
if (site.contains("|")) {
String[] strArray = site.split("\\|");
site = strArray[0];
type = strArray[1];
}
String dateStr = request.getParameter("date");
String hourStr = request.getParameter("hour");
String minuteStr = request.getParameter("minute");
// 解析时间
Date date = toDate(dateStr, hourStr, minuteStr);
SitesIndexService.insert(user ,link, title, desc, content, tags,
keywords, site, type , date , "site");
}else if("update".equals(actionType)){
String link = StringUtils.trimToEmpty(request.getParameter("link"));
String title = StringUtils.trimToEmpty(request
.getParameter("title"));
String keywords = StringUtils.trimToEmpty(request
.getParameter("keywords"));
String desc = StringUtils.trimToEmpty(request.getParameter("desc"));
String content = StringUtils.trimToEmpty(request
.getParameter("content"));
String tags = StringUtils.trimToEmpty(request.getParameter("tags"));
String type = "";
String site = StringUtils.trimToEmpty(request.getParameter("site"));
// 拆分site和type
if (site.contains("|")) {
String[] strArray = site.split("\\|");
site = strArray[0];
type = strArray[1];
}
String dateStr = request.getParameter("date");
String hourStr = request.getParameter("hour");
String minuteStr = request.getParameter("minute");
// 解析时间
Date date = toDate(dateStr, hourStr, minuteStr);
SitesIndexService.updateByLink(user ,link, title, keywords, desc, tags, content,
site, type, date , "site");
}else if ("delete".equals(actionType)) {
/*
* delete
*/
String link = StringUtils.trimToEmpty(request.getParameter("link"));
if(link!=null && !"".equals(link)){
SitesIndexService.deleteByLink(user ,link , "site");
}
}else if("deleteAll".equals(actionType)){
/*
* deleteAll
*/
String link = StringUtils.trimToEmpty(request.getParameter("link"));
if(link!=null && !"".equals(link)){
SitesIndexService.deleteAll(user ,link , "site");
}
}
String queryUrl = StringUtils.trimToEmpty(request
.getParameter("queryUrl"));
String pageIndexStr = request.getParameter("pageIndex");
String pageSizeStr = request.getParameter("pageSize");
if (StringUtils.isEmpty(pageIndexStr)
|| StringUtils.isEmpty(pageSizeStr)) {
request.getRequestDispatcher("/admin/sitesIndex.jsp").forward(
request, response);
return;
}
int pageIndex = Integer.valueOf(StringUtils.trimToEmpty(pageIndexStr));
int pageSize = Integer.valueOf(StringUtils.trimToEmpty(pageSizeStr));
if("".equals(queryUrl)){
request.getRequestDispatcher("/admin/sitesIndex.jsp").forward(
request, response);
return ;
}
SolrDocumentList resultList = SitesIndexService.query(
queryUrl, pageIndex, pageSize,"site");
SolrDocumentList resultLists = new SolrDocumentList();
SolrDocument solr;
for(SolrDocument s : resultList){
solr = s;
solr.setField("title", SecurityUtil.outputfilter((String)s.getFieldValue("title")));
solr.setField("keywords", SecurityUtil.outputfilter((String)s.getFieldValue("keywords")));
solr.setField("desc", SecurityUtil.outputfilter((String)s.getFieldValue("desc")));
solr.setField("tags", SecurityUtil.outputfilter((String)s.getFieldValue("tags")));
solr.setField("content", SecurityUtil.outputfilter((String)s.getFieldValue("content")));
resultLists.add(solr);
}
long count = resultList.getNumFound();
request.setAttribute("resultList", resultLists);
request.setAttribute("queryUrl", queryUrl);
request.setAttribute("pageCount", Math.ceil(count * 1.0 / pageSize));
request.setAttribute("pageIndex", pageIndex);
request.getRequestDispatcher("/admin/sitesIndex.jsp").forward(
request, response);
}
private Date toDate(String date, String hour, String minute) {
date = date + " " + hour + ":" + minute;
try {
return DateUtils.parseDate(date,
new String[] { "yyyy-MM-dd HH:mm" });
} catch (ParseException e) {
e.printStackTrace();
return new Date();
}
}
}
import java.io.IOException;
import java.text.ParseException;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import com.paic.wcm.search.admin.service.SitesIndexService;
import com.paic.wcm.search.utils.SecurityUtil;
public class SitesIndexServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public SitesIndexServlet() {
super();
}
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doPost(req, resp);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String actionType = request.getParameter("actionType");
String user = (String) request.getSession().getAttribute("user");
/* if(!request.getSession().getAttribute("redom").equals(request.getParameter("redom"))){
request.getRequestDispatcher("/admin/sitesIndex.jsp").forward(
request, response);
return;
}*/
if ("insert".equals(actionType)) {
/*
* insert
*/
String link = StringUtils.trimToEmpty(request.getParameter("link"));
String title = StringUtils.trimToEmpty(request
.getParameter("title"));
String keywords = StringUtils.trimToEmpty(request
.getParameter("keywords"));
String desc = StringUtils.trimToEmpty(request.getParameter("desc"));
String content = StringUtils.trimToEmpty(request
.getParameter("content"));
String tags = StringUtils.trimToEmpty(request.getParameter("tags"));
String type = "";
String site = StringUtils.trimToEmpty(request.getParameter("site"));
// 拆分site和type
if (site.contains("|")) {
String[] strArray = site.split("\\|");
site = strArray[0];
type = strArray[1];
}
String dateStr = request.getParameter("date");
String hourStr = request.getParameter("hour");
String minuteStr = request.getParameter("minute");
// 解析时间
Date date = toDate(dateStr, hourStr, minuteStr);
SitesIndexService.insert(user ,link, title, desc, content, tags,
keywords, site, type , date , "site");
}else if("update".equals(actionType)){
String link = StringUtils.trimToEmpty(request.getParameter("link"));
String title = StringUtils.trimToEmpty(request
.getParameter("title"));
String keywords = StringUtils.trimToEmpty(request
.getParameter("keywords"));
String desc = StringUtils.trimToEmpty(request.getParameter("desc"));
String content = StringUtils.trimToEmpty(request
.getParameter("content"));
String tags = StringUtils.trimToEmpty(request.getParameter("tags"));
String type = "";
String site = StringUtils.trimToEmpty(request.getParameter("site"));
// 拆分site和type
if (site.contains("|")) {
String[] strArray = site.split("\\|");
site = strArray[0];
type = strArray[1];
}
String dateStr = request.getParameter("date");
String hourStr = request.getParameter("hour");
String minuteStr = request.getParameter("minute");
// 解析时间
Date date = toDate(dateStr, hourStr, minuteStr);
SitesIndexService.updateByLink(user ,link, title, keywords, desc, tags, content,
site, type, date , "site");
}else if ("delete".equals(actionType)) {
/*
* delete
*/
String link = StringUtils.trimToEmpty(request.getParameter("link"));
if(link!=null && !"".equals(link)){
SitesIndexService.deleteByLink(user ,link , "site");
}
}else if("deleteAll".equals(actionType)){
/*
* deleteAll
*/
String link = StringUtils.trimToEmpty(request.getParameter("link"));
if(link!=null && !"".equals(link)){
SitesIndexService.deleteAll(user ,link , "site");
}
}
String queryUrl = StringUtils.trimToEmpty(request
.getParameter("queryUrl"));
String pageIndexStr = request.getParameter("pageIndex");
String pageSizeStr = request.getParameter("pageSize");
if (StringUtils.isEmpty(pageIndexStr)
|| StringUtils.isEmpty(pageSizeStr)) {
request.getRequestDispatcher("/admin/sitesIndex.jsp").forward(
request, response);
return;
}
int pageIndex = Integer.valueOf(StringUtils.trimToEmpty(pageIndexStr));
int pageSize = Integer.valueOf(StringUtils.trimToEmpty(pageSizeStr));
if("".equals(queryUrl)){
request.getRequestDispatcher("/admin/sitesIndex.jsp").forward(
request, response);
return ;
}
SolrDocumentList resultList = SitesIndexService.query(
queryUrl, pageIndex, pageSize,"site");
SolrDocumentList resultLists = new SolrDocumentList();
SolrDocument solr;
for(SolrDocument s : resultList){
solr = s;
solr.setField("title", SecurityUtil.outputfilter((String)s.getFieldValue("title")));
solr.setField("keywords", SecurityUtil.outputfilter((String)s.getFieldValue("keywords")));
solr.setField("desc", SecurityUtil.outputfilter((String)s.getFieldValue("desc")));
solr.setField("tags", SecurityUtil.outputfilter((String)s.getFieldValue("tags")));
solr.setField("content", SecurityUtil.outputfilter((String)s.getFieldValue("content")));
resultLists.add(solr);
}
long count = resultList.getNumFound();
request.setAttribute("resultList", resultLists);
request.setAttribute("queryUrl", queryUrl);
request.setAttribute("pageCount", Math.ceil(count * 1.0 / pageSize));
request.setAttribute("pageIndex", pageIndex);
request.getRequestDispatcher("/admin/sitesIndex.jsp").forward(
request, response);
}
private Date toDate(String date, String hour, String minute) {
date = date + " " + hour + ":" + minute;
try {
return DateUtils.parseDate(date,
new String[] { "yyyy-MM-dd HH:mm" });
} catch (ParseException e) {
e.printStackTrace();
return new Date();
}
}
}