|
package com.ekexiu.portal.resResult;
import com.ekexiu.portal.dao.ProfessorDao;
import com.ekexiu.portal.service.KeyWordService;
import org.jfw.apt.annotation.Autowrie;
import org.jfw.apt.annotation.Nullable;
import org.jfw.apt.web.annotation.Path;
import org.jfw.apt.web.annotation.operate.Get;
import org.jfw.apt.web.annotation.operate.Post;
import org.jfw.apt.web.annotation.param.JdbcConn;
import org.jfw.apt.web.annotation.param.Upload;
import org.jfw.util.ListUtil;
import org.jfw.util.PageQueryResult;
import org.jfw.util.StringUtil;
import org.jfw.util.web.fileupload.Item;
import org.jfw.util.web.fileupload.UploadItemIterator;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
/**
* Created by TT on 2019/07/10.
*/
@Path("/resResult")
public class ResearchResultService {
private static final AtomicInteger FN_IDX = new AtomicInteger(1);
private File imgPath;
private int imgMaxWidth = 70;
@Autowrie
private ResearcherDao researcherDao;
@Autowrie
private ResearchResultDao researchResultDao;
@Autowrie
private ResResultKeyWordDao resResultKeyWordDao;
@Autowrie
private ProfessorDao professorDao;
@Autowrie
private KeyWordService keyWordService;
public ResearcherDao getResearcherDao() {
return researcherDao;
}
public void setResearcherDao(ResearcherDao researcherDao) {
this.researcherDao = researcherDao;
}
public ResearchResultDao getResearchResultDao() {
return researchResultDao;
}
public void setResearchResultDao(ResearchResultDao researchResultDao) {
this.researchResultDao = researchResultDao;
}
public ResResultKeyWordDao getResResultKeyWordDao() {
return resResultKeyWordDao;
}
public void setResResultKeyWordDao(ResResultKeyWordDao resResultKeyWordDao) {
this.resResultKeyWordDao = resResultKeyWordDao;
}
public ProfessorDao getProfessorDao() {
return professorDao;
}
public void setProfessorDao(ProfessorDao professorDao) {
this.professorDao = professorDao;
}
public KeyWordService getKeyWordService() {
return keyWordService;
}
public void setKeyWordService(KeyWordService keyWordService) {
this.keyWordService = keyWordService;
}
public static AtomicInteger getFnIdx() {
return FN_IDX;
}
public File getImgPath() {
return imgPath;
}
public void setImgPath(File imgPath) {
this.imgPath = imgPath;
}
public int getImgMaxWidth() {
return imgMaxWidth;
}
public void setImgMaxWidth(int imgMaxWidth) {
this.imgMaxWidth = imgMaxWidth;
}
public String insert(Connection con, ResearchResult researchResult, String[] researchers) throws SQLException {
String id = StringUtil.buildUUID();
researchResult.setId(id);
refreshSearchers(con, id, researchers);
if (researchResult.getStatus().equals("1")) {
String subject = researchResult.getSubject();
if (subject == null) {
subject = "";
}
String industry = researchResult.getIndustry();
if (industry == null)
industry = "";
String[] kws = null;
kws = ListUtil.splitTrimExcludeEmpty(subject + "," + industry, ',').toArray(new String[0]);
this.keyWordService.refreshResearchResult(con, id, kws);
}
this.researchResultDao.insert(con, researchResult);
return id;
}
public boolean update(Connection con, ResearchResult researchResult, String[] researchers) throws SQLException {
String id = researchResult.getId();
if (researchResultDao.update(con, researchResult)>0) {
refreshSearchers(con, id, researchers);
if (researchResult.getStatus().equals("1")) {
String subject = researchResult.getSubject();
if (subject == null) {
subject = "";
}
String industry = researchResult.getIndustry();
if (industry == null)
industry = "";
String[] kws = null;
kws = ListUtil.splitTrimExcludeEmpty(subject + "," + industry, ',').toArray(new String[0]);
this.keyWordService.refreshResearchResult(con, id, kws);
}else {
this.keyWordService.refreshResearchResult(con, id, null);
}
return true;
}
return false;
}
@Post
@Path("/draft")
public String draft(@JdbcConn(true) Connection con, ResearchResult researchResult, @Nullable String[] researchers) throws SQLException, IOException {
researchResult.setStatus("2");
return insert(con, researchResult, researchers);
}
@Post
@Path("/publish")
public String publish(@JdbcConn(true) Connection con, ResearchResult researchResult, @Nullable String[] researchers) throws SQLException, IOException {
researchResult.setStatus("1");
return insert(con, researchResult, researchers);
}
@Post
@Path("/draft/update")
public boolean updateDraft(@JdbcConn(true) Connection con, ResearchResult researchResult, @Nullable String[] researchers) throws SQLException, IOException {
researchResult.setStatus("2");
return update(con, researchResult,researchers);
}
@Post
@Path("/publish/update")
public boolean updatePublish(@JdbcConn(true) Connection con, ResearchResult researchResult, @Nullable String[] researchers) throws SQLException, IOException {
researchResult.setStatus("1");
return update(con, researchResult, researchers);
}
@Get
@Path("/researcher")
public List<Researcher> queryResearchers(@JdbcConn Connection con, String id) throws SQLException {
return this.researcherDao.query(con, id);
}
@Get
@Path("/qo")
public ResearchResult query(@JdbcConn Connection con, String id) throws SQLException {
return this.researchResultDao.query(con, id);
}
@Post
@Path("/delete")
public void delete(@JdbcConn(true) Connection con, String id) throws SQLException {
this.researchResultDao.updateStatus(con, id);
this.keyWordService.refreshResearchResult(con, id, null);
}
@Get
@Path("/pq")
public PageQueryResult<ResearchResult> pageQuery(@JdbcConn Connection con, @Nullable String[] status, @Nullable String key, int pageSize, int pageNo) throws SQLException {
return researchResultDao.pageQuery(con, status, key == null ? null : "%" + key + "%", pageSize, pageNo);
}
@Get
@Path("/pq/researcher")
public PageQueryResult<ResearchResult> pqByResearcher(@JdbcConn Connection con,@Nullable String[] status, String id,int pageSize,int pageNo)throws SQLException {
return researchResultDao.pageQueryByResearcher(con, status, id, pageSize, pageNo);
}
@Get
@Path("/pq/creator")
public PageQueryResult<ResearchResult> pqByCreator(@JdbcConn Connection con,@Nullable String[] status, String id,int pageSize,int pageNo)throws SQLException {
return researchResultDao.pageQueryByCreator(con, status, id, pageSize, pageNo);
}
@Get
@Path("/count/researcher")
public long count(@JdbcConn Connection con,String id)throws SQLException {
return researchResultDao.countPublish(con, id);
}
@Post
@Path("/incPageViews")
public void incPageViews(@JdbcConn(true) Connection con,String id)throws SQLException{
this.researchResultDao.incPageViews(con, id);
}
@Get
@Path("/qm")
public List<ResearchResult> query(@JdbcConn Connection con, String[] id)throws SQLException {
return researchResultDao.query(con, id);
}
/**
* 上传文件
*
* @param it
* @return
* @throws Exception
*/
@Path("/upload")
@Post
public List<UploadFile> upload(@Upload UploadItemIterator it) throws Exception {
List<UploadFile> ret = new LinkedList<>();
try {
while (it.hasNext()) {
Item item = it.next();
if (!item.isFormField()) {
String name = normalizeFileName(item.getName());
int index = name.lastIndexOf('.');
String ext = index >= 0 ? name.substring(index + 1) : "";
ext = ext.trim();
long fileLen = 0;
int len = 0;
UploadFile uf = this.buildTargetFile(ext);
uf.setName(name);
File file = uf.getFn();
ret.add(uf);
byte[] buf = new byte[8092];
OutputStream of = new FileOutputStream(file);
try {
InputStream in = item.getInputStream();
try {
while ((len = in.read(buf)) >= 0) {
if (len > 0) {
of.write(buf, 0, len);
fileLen += len;
}
}
uf.setSize(fileLen);
} finally {
in.close();
}
} finally {
of.close();
}
}
}
return ret;
} catch (Exception e) {
this.remove(ret);
throw e;
} finally {
if (it != null)
it.clean();
}
}
private void remove(List<UploadFile> list) {
for (UploadFile file : list) {
if (file.fn != null)
file.fn.delete();
}
}
private UploadFile buildTargetFile(String ext) {
StringBuilder sb = new StringBuilder();
sb.append(System.currentTimeMillis() / (1000 * 60 * 60 * 24));
File path = new File(this.imgPath, sb.toString());
if (!path.exists()) {
synchronized (this) {
if (!path.mkdirs())
throw new RuntimeException("mkdir error[" + path + "]");
FN_IDX.set(1);
}
}
File file;
do {
String fn = FN_IDX.toString();
if (ext.isEmpty()) {
file = new File(path, fn);
} else {
file = new File(path, fn + "." + ext);
}
FN_IDX.incrementAndGet();
} while (file.exists());
sb.append("/").append(file.getName());
UploadFile uf = new UploadFile();
uf.setFn(file);
uf.setUri("/" + sb.toString());
return uf;
}
private String normalizeFileName(String fn) {
int index = fn.indexOf('\\');
if (index >= 0) {
fn = fn.substring(fn.lastIndexOf('\\') + 1);
}
index = fn.indexOf('/');
if (index >= 0) {
fn = fn.substring(fn.lastIndexOf('/') + 1);
}
if (fn.length() == 0)
throw new RuntimeException("invalid filename in Multipart/data request");
return fn;
}
public static class UploadFile {
private String name;
private String uri;
private long size;
private transient File fn;
public File getFn() {
return fn;
}
public void setFn(File fn) {
this.fn = fn;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUri() {
return uri;
}
public void setUri(String uri) {
this.uri = uri;
}
public long getSize() {
return size;
}
public void setSize(long size) {
this.size = size;
}
}
public void refreshSearchers(Connection con, String id, String[] researchers) throws SQLException {
this.researcherDao.delete(con, id);
if (researchers != null && researchers.length > 0) {
Researcher[] res = new Researcher[researchers.length];
for (int i = 0; i < researchers.length; ++i) {
Researcher re = new Researcher();
re.setId(id);
re.setProfessorId(researchers[i].substring(0, 32));
re.setName(researchers[i].substring(33));
res[i] = re;
}
this.researcherDao.insert(con, res);
}
}
}
|