ソースを参照

--add Article.class

zzy.zhiyuan.foxmail 8 年 前
コミット
bd3adf5806

+ 47 - 0
src/main/java/com/ekexiu/portal/dao/ArticleDao.java

@ -0,0 +1,47 @@
1
package com.ekexiu.portal.dao;
2

3
import java.sql.Connection;
4
import java.sql.SQLException;
5
import java.util.List;
6

7
import org.jfw.apt.orm.annotation.dao.DAO;
8
import org.jfw.apt.orm.annotation.dao.method.From;
9
import org.jfw.apt.orm.annotation.dao.method.OrderBy;
10
import org.jfw.apt.orm.annotation.dao.method.operator.DeleteWith;
11
import org.jfw.apt.orm.annotation.dao.method.operator.Insert;
12
import org.jfw.apt.orm.annotation.dao.method.operator.SelectList;
13
import org.jfw.apt.orm.annotation.dao.method.operator.Update;
14
import org.jfw.apt.orm.annotation.dao.method.operator.UpdateWith;
15
import org.jfw.apt.orm.annotation.dao.param.Set;
16

17
import com.ekexiu.portal.po.Article;
18

19
@DAO
20
public abstract class ArticleDao {
21
	@Insert
22
	public abstract int insert(Connection con, Article article) throws SQLException;
23
	
24
	@Update
25
	public abstract int update(Connection con, Article article) throws SQLException;
26
	
27
	@UpdateWith
28
	@From(Article.class)
29
	public abstract int updateSubject(Connection con, @Set String subject, String articleId) throws SQLException;
30
	
31
	@UpdateWith
32
	@From(Article.class)
33
	public abstract int updateIndustry(Connection con, @Set String industry, String articleId) throws SQLException;
34
	
35
	@DeleteWith
36
	@From(Article.class)
37
	public abstract int delete(Connection con, String articleId) throws SQLException;
38
	
39
	@DeleteWith
40
	@From(Article.class)
41
	public abstract int deletePro(Connection con, String professorId) throws SQLException;
42
	
43
	@SelectList
44
	@OrderBy(" ORDER BY CREATE_TIME DESC ")
45
	public abstract List<Article> queryPro(Connection con, String professorId) throws SQLException;
46

47
}

+ 86 - 0
src/main/java/com/ekexiu/portal/po/Article.java

@ -0,0 +1,86 @@
1
package com.ekexiu.portal.po;
2

3
import org.jfw.apt.orm.annotation.entry.Column;
4
import org.jfw.apt.orm.annotation.entry.PrimaryKey;
5
import org.jfw.apt.orm.annotation.entry.Table;
6
import org.jfw.apt.orm.core.enums.DE;
7

8
import com.ekexiu.portal.basepo.CreateTimeSupported;
9
import com.ekexiu.portal.basepo.ModifyTimeSupported;
10

11
@Table
12
@PrimaryKey("articleId")
13
public class Article implements CreateTimeSupported, ModifyTimeSupported {
14
	private String articleId;
15
	private String professorId;
16
	private String articleTitle;
17
	private String articleContent;
18
	private String subject;
19
	private String industry;
20
	private String createTime;
21
	private String modifyTime;
22
	private String articleImg;
23
	
24
	@Column(DE.id_32)
25
	public String getArticleId() {
26
		return articleId;
27
	}
28
	public void setArticleId(String articleId) {
29
		this.articleId = articleId;
30
	}
31
	@Column(DE.id_32)
32
	public String getProfessorId() {
33
		return professorId;
34
	}
35
	public void setProfessorId(String professorId) {
36
		this.professorId = professorId;
37
	}
38
	@Column(value=DE.string_de,dbType="TEXT")
39
	public String getArticleTitle() {
40
		return articleTitle;
41
	}
42
	public void setArticleTitle(String articleTitle) {
43
		this.articleTitle = articleTitle;
44
	}
45
	@Column(value=DE.String_de,dbType="TEXT")
46
	public String getArticleContent() {
47
		return articleContent;
48
	}
49
	public void setArticleContent(String articleContent) {
50
		this.articleContent = articleContent;
51
	}
52
	@Column(value=DE.String_de,dbType="TEXT")
53
	public String getSubject() {
54
		return subject;
55
	}
56
	public void setSubject(String subject) {
57
		this.subject = subject;
58
	}
59
	@Column(value=DE.String_de,dbType="TEXT")
60
	public String getIndustry() {
61
		return industry;
62
	}
63
	public void setIndustry(String industry) {
64
		this.industry = industry;
65
	}
66
	public String getCreateTime() {
67
		return createTime;
68
	}
69
	public void setCreateTime(String createTime) {
70
		this.createTime = createTime;
71
	}
72
	public String getModifyTime() {
73
		return modifyTime;
74
	}
75
	public void setModifyTime(String modifyTime) {
76
		this.modifyTime = modifyTime;
77
	}
78
	@Column(DE.String_de)
79
	public String getArticleImg() {
80
		return articleImg;
81
	}
82
	public void setArticleImg(String articleImg) {
83
		this.articleImg = articleImg;
84
	}
85

86
}

+ 165 - 0
src/main/java/com/ekexiu/portal/service/ArticleService.java

@ -0,0 +1,165 @@
1
package com.ekexiu.portal.service;
2

3
import java.io.ByteArrayInputStream;
4
import java.io.ByteArrayOutputStream;
5
import java.io.File;
6
import java.io.FileInputStream;
7
import java.io.FileOutputStream;
8
import java.io.IOException;
9
import java.io.InputStream;
10
import java.sql.Connection;
11
import java.sql.SQLException;
12
import java.text.SimpleDateFormat;
13
import java.util.Date;
14
import java.util.List;
15

16
import org.jfw.apt.annotation.Autowrie;
17
import org.jfw.apt.annotation.Nullable;
18
import org.jfw.apt.web.annotation.Path;
19
import org.jfw.apt.web.annotation.operate.Delete;
20
import org.jfw.apt.web.annotation.operate.Get;
21
import org.jfw.apt.web.annotation.operate.Post;
22
import org.jfw.apt.web.annotation.param.JdbcConn;
23
import org.jfw.apt.web.annotation.param.PathVar;
24
import org.jfw.util.JpgUtil;
25
import org.jfw.util.StringUtil;
26
import org.jfw.util.exception.JfwBaseException;
27
import org.jfw.util.io.IoUtil;
28

29
import com.ekexiu.portal.dao.ArticleDao;
30
import com.ekexiu.portal.po.Article;
31

32
@Path("/article")
33
public class ArticleService {
34
	private File tmpPath;
35
	private File articlePath;
36
	private int artMaxLen=640;
37
	private static final String JPG = "jpg";
38
	@Autowrie
39
	private ArticleDao articleDao;
40

41
	public File getTmpPath() {
42
		return tmpPath;
43
	}
44

45
	public void setTmpPath(File tmpPath) {
46
		this.tmpPath = tmpPath;
47
	}
48

49
	public File getArticlePath() {
50
		return articlePath;
51
	}
52

53
	public void setArticlePath(File articlePath) {
54
		this.articlePath = articlePath;
55
	}
56

57
	public int getArtMaxLen() {
58
		return artMaxLen;
59
	}
60

61
	public void setArtMaxLen(int artMaxLen) {
62
		this.artMaxLen = artMaxLen;
63
	}
64

65
	public ArticleDao getArticleDao() {
66
		return articleDao;
67
	}
68

69
	public void setArticleDao(ArticleDao articleDao) {
70
		this.articleDao = articleDao;
71
	}
72
	
73
	private byte[] resImage(byte[] src, int maxLen) throws IOException {
74
		ByteArrayInputStream in = new ByteArrayInputStream(src);
75
		ByteArrayOutputStream out = new ByteArrayOutputStream();
76
		JpgUtil.scalingZoom(in, out, maxLen);
77
		out.flush();
78
		return out.toByteArray();
79
	}
80
	
81
	private byte[] readTmpFile(String fn) throws JfwBaseException {
82
		File file = new File(this.tmpPath, fn);
83
		if (!file.exists())
84
			throw new JfwBaseException(90, "resource image not exists");
85
		try {
86
			InputStream in = new FileInputStream(file);
87
			ByteArrayOutputStream out = new ByteArrayOutputStream();
88
			IoUtil.copy(in, out, true, true);
89
			return out.toByteArray();
90
		} catch (IOException e) {
91
			throw new JfwBaseException(91, "read temp resource image error", e);
92
		}
93
	}
94
	
95
	private void saveArtImg(String fn, String id) throws JfwBaseException, IOException{
96
		byte[] src = this.readTmpFile(fn);
97
		src = JpgUtil.read(src);
98
		byte[] shareResImage = this.resImage(src, this.artMaxLen);
99
		File savePath = this.createDate(this.articlePath);
100
		IoUtil.saveStream(new FileOutputStream(new File(savePath, id + "." + JPG)), src, true);
101
		IoUtil.saveStream(new FileOutputStream(new File(savePath, id + "_s." + JPG)), shareResImage, true);
102
	}
103
	
104
	private File createDate(File path){
105
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-DD");
106
		String date = df.format(new Date());
107
		String datePath = path + "/" +  date;
108
		File dateFile = new File(datePath);
109
		if (!dateFile.exists()) {
110
			// 创建日期目录
111
			dateFile.mkdir();
112
		}
113
		return dateFile;
114
	}
115
	
116
	@Post
117
	@Path
118
	public String insert(@JdbcConn(true) Connection con, Article article) 
119
			throws SQLException, IOException, JfwBaseException{
120
		String articleId = StringUtil.buildUUID();
121
		if(article.getArticleImg() != null){
122
			this.saveArtImg(article.getArticleImg(), articleId);
123
			article.setArticleImg(this.createDate(this.articlePath)+"/"+articleId+"."+JPG);
124
		}
125
		article.setArticleId(articleId);
126
		this.articleDao.insert(con, article);
127
		return articleId;
128
	}
129
	
130
	@Post
131
	@Path("/updateArt")
132
	public void update(@JdbcConn(true) Connection con, Article article) 
133
			throws SQLException, JfwBaseException, IOException{
134
		if(article.getArticleImg() != null){
135
			this.saveArtImg(article.getArticleImg(), article.getArticleId());
136
			article.setArticleImg(this.createDate(this.articlePath)+"/"+article.getArticleId()+"."+JPG);
137
		}
138
		this.articleDao.update(con, article);
139
	}
140
	
141
	@Post
142
	@Path("/subject")
143
	public void updateSubject(@JdbcConn(true) Connection con, @Nullable String subject, String articleId) throws SQLException{
144
		this.articleDao.updateSubject(con, subject, articleId);
145
	}
146
	
147
	@Post
148
	@Path("/industry")
149
	public void updateIndustry(@JdbcConn(true) Connection con, @Nullable String industry, String articleId) throws SQLException{
150
		this.articleDao.updateIndustry(con, industry, articleId);
151
	}
152
	
153
	@Get
154
	@Path("/qaPro")
155
	public List<Article> queryPro(@JdbcConn Connection con, String professorId) throws SQLException{
156
		return this.articleDao.queryPro(con, professorId);
157
	}
158
	
159
	@Delete
160
	@Path("/{articleId}")
161
	public void delete(@JdbcConn(true) Connection con, @PathVar String articleId) throws SQLException{
162
		this.articleDao.delete(con, articleId);
163
	}
164

165
}