浏览代码

--add queryPage();update insert();update queryOne();

zzy.zhiyuan.foxmail 8 年之前
父节点
当前提交
140379f3ae

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

1
package com.ekexiu.portal.dao;
1
package com.ekexiu.portal.dao;
2

2

3
import java.sql.Connection;
3
import java.sql.Connection;
4
import java.sql.PreparedStatement;
5
import java.sql.ResultSet;
4
import java.sql.SQLException;
6
import java.sql.SQLException;
7
import java.util.ArrayList;
8
import java.util.Collections;
5
import java.util.List;
9
import java.util.List;
6

10

7
import org.jfw.apt.annotation.Nullable;
11
import org.jfw.apt.annotation.Nullable;
16
import org.jfw.apt.orm.annotation.dao.method.operator.Update;
20
import org.jfw.apt.orm.annotation.dao.method.operator.Update;
17
import org.jfw.apt.orm.annotation.dao.method.operator.UpdateWith;
21
import org.jfw.apt.orm.annotation.dao.method.operator.UpdateWith;
18
import org.jfw.apt.orm.annotation.dao.param.Set;
22
import org.jfw.apt.orm.annotation.dao.param.Set;
23
import org.jfw.util.PageQueryResult;
19

24

20
import com.ekexiu.portal.po.Article;
25
import com.ekexiu.portal.po.Article;
26
import com.ekexiu.portal.pojo.FindInfo;
21

27

22
@DAO
28
@DAO
23
public abstract class ArticleDao {
29
public abstract class ArticleDao {
48
	@OrderBy(" ORDER BY MODIFY_TIME DESC ")
54
	@OrderBy(" ORDER BY MODIFY_TIME DESC ")
49
	public abstract List<Article> queryPro(Connection con, String professorId) throws SQLException;
55
	public abstract List<Article> queryPro(Connection con, String professorId) throws SQLException;
50
	
56
	
57
	@SelectList
58
	@OrderBy(" ORDER BY MODIFY_TIME DESC")
59
	public abstract List<Article> queryOrg(Connection con, String orgId) throws SQLException;
60
	
51
	@SelectOne
61
	@SelectOne
52
	@Nullable
62
	@Nullable
53
	public abstract Article queryOne(Connection con, String articleId) throws SQLException;
63
	public abstract Article queryOne(Connection con, String articleId) throws SQLException;
64
	
65
	public PageQueryResult<FindInfo> queryPage(Connection con,int pageSize,int pageNo) throws SQLException{
66
        int total = 0;
67
        PageQueryResult<FindInfo> queryResult = new PageQueryResult<FindInfo>();
68
        String sql = null;
69
        sql = "SELECT SUM(NUM) FROM (SELECT COUNT(1) NUM FROM ARTICLE UNION ALL SELECT COUNT(1) NUM FROM RESOURCE) TBL";
70
        PreparedStatement ps = con.prepareStatement(sql);
71
        try{
72
        	queryResult.setPageSize(pageSize);
73
            ResultSet rs = ps.executeQuery();
74
            try{
75
                rs.next();
76
                total = rs.getInt(1);
77
            }finally{
78
                try{rs.close();}catch(Exception e1){}
79
            }
80
        }finally{
81
            try{ps.close();}catch(Exception e2){}
82
        }
83
        queryResult.setTotal(total);
84
        if(0== total){
85
        	queryResult.setPageNo(1);
86
        	queryResult.setData(Collections.<FindInfo>emptyList());
87
            return queryResult;
88
        }
89
        boolean firstPage = (1 == pageNo);
90
        if(firstPage){
91
        	queryResult.setPageNo(1);
92
        	sql = "SELECT ARTICLE_ID,ARTICLE_TITLE,ARTICLE_IMG,ARTICLE_TYPE,CREATE_TIME,ORG_ID,PROFESSOR_ID FROM ARTICLE"
93
                	+ " UNION ALL SELECT RESOURCE_ID,RESOURCE_NAME,NULL,'3',CREATE_TIME,NULL,PROFESSOR_ID FROM RESOURCE";
94
            sql = sql + " ORDER BY CREATE_TIME DESC ";
95
            sql = sql + " LIMIT " + pageSize;
96
        }else{
97
            int pageNum = total / pageSize;
98
            if(total % pageSize != 0){
99
                ++pageNum;
100
            }
101
            if(pageNo > pageNum){
102
                pageNo = pageNum;
103
            }
104
            queryResult.setPageNo(pageNo);
105
            --pageNo;
106
            int offset = (pageNo * pageSize);
107
            sql = "SELECT ARTICLE_ID,ARTICLE_TITLE,ARTICLE_IMG,ARTICLE_TYPE,CREATE_TIME,ORG_ID,PROFESSOR_ID FROM ARTICLE"
108
                	+ " UNION ALL SELECT RESOURCE_ID,RESOURCE_NAME,NULL,'3',CREATE_TIME,NULL,PROFESSOR_ID FROM RESOURCE";
109
            sql = sql + " ORDER BY CREATE_TIME DESC ";
110
            sql = sql + " LIMIT " + pageSize+ " OFFSET " + offset;
111
        }
112
        ps = con.prepareStatement(sql);
113
        try{
114
            ResultSet rs = ps.executeQuery();
115
            try{
116
                List<FindInfo> findInfos = new ArrayList<FindInfo>();
117
                queryResult.setData(findInfos);
118
                int size = 0;
119
                while((size<pageSize) && rs.next()){
120
                    ++size;
121
                    FindInfo findInfo =  new FindInfo();
122
                    findInfo.setId(rs.getString(1));
123
                    findInfo.setName(rs.getString(2));
124
                    String image = rs.getString(3);
125
                    if(rs.wasNull()){
126
                    	image = null;
127
                    }
128
                    findInfo.setImage(image);
129
                    findInfo.setType(rs.getString(4));
130
                    findInfo.setCreateTime(rs.getString(5));
131
                    String orgId = rs.getString(6);
132
                    if(rs.wasNull()){
133
                    	findInfo.setOwner(rs.getString(7));
134
                    }else{
135
                    	findInfo.setOwner(orgId);
136
                    }
137
                    findInfos.add(findInfo);
138
                }
139
                return queryResult;
140
            }finally{
141
                try{rs.close();}catch(Exception e3){}
142
            }
143
        }finally{
144
            try{ps.close();}catch(Exception e4){}
145
        }
146
    }
54

147

55
}
148
}

+ 18 - 1
src/main/java/com/ekexiu/portal/po/Article.java

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

8

8
import com.ekexiu.portal.basepo.CreateTimeSupported;
9
import com.ekexiu.portal.basepo.CreateTimeSupported;
21
	private String createTime;
22
	private String createTime;
22
	private String modifyTime;
23
	private String modifyTime;
23
	private String articleImg;
24
	private String articleImg;
25
	private String orgId;
26
	private String articleType;
24
	private EditProfessor professor;
27
	private EditProfessor professor;
25
	
28
	
26
	@Column(DE.id_32)
29
	@Column(DE.id_32)
30
	public void setArticleId(String articleId) {
33
	public void setArticleId(String articleId) {
31
		this.articleId = articleId;
34
		this.articleId = articleId;
32
	}
35
	}
33
	@Column(DE.id_32)
36
	@Column(handlerClass=FixLenStringHandler.class,dbType="CHAR(32)",nullable=true,renewable=false)
34
	public String getProfessorId() {
37
	public String getProfessorId() {
35
		return professorId;
38
		return professorId;
36
	}
39
	}
90
	public void setProfessor(EditProfessor professor) {
93
	public void setProfessor(EditProfessor professor) {
91
		this.professor = professor;
94
		this.professor = professor;
92
	}
95
	}
96
	@Column(handlerClass=FixLenStringHandler.class,dbType="CHAR(32)",nullable=true,renewable=false)
97
	public String getOrgId() {
98
		return orgId;
99
	}
100
	public void setOrgId(String orgId) {
101
		this.orgId = orgId;
102
	}
103
	@Column(handlerClass=FixLenStringHandler.class,dbType="CHAR(1)",nullable=true,renewable=false)
104
	public String getArticleType() {
105
		return articleType;
106
	}
107
	public void setArticleType(String articleType) {
108
		this.articleType = articleType;
109
	}
93

110

94
}
111
}

+ 47 - 0
src/main/java/com/ekexiu/portal/pojo/FindInfo.java

1
package com.ekexiu.portal.pojo;
2

3
public class FindInfo {
4
	private String id;
5
	private String name;
6
	private String createTime;
7
	private String owner;
8
	private String type;
9
	private String image;
10
	public String getId() {
11
		return id;
12
	}
13
	public void setId(String id) {
14
		this.id = id;
15
	}
16
	public String getName() {
17
		return name;
18
	}
19
	public void setName(String name) {
20
		this.name = name;
21
	}
22
	public String getCreateTime() {
23
		return createTime;
24
	}
25
	public void setCreateTime(String createTime) {
26
		this.createTime = createTime;
27
	}
28
	public String getOwner() {
29
		return owner;
30
	}
31
	public void setOwner(String owner) {
32
		this.owner = owner;
33
	}
34
	public String getType() {
35
		return type;
36
	}
37
	public void setType(String type) {
38
		this.type = type;
39
	}
40
	public String getImage() {
41
		return image;
42
	}
43
	public void setImage(String image) {
44
		this.image = image;
45
	}
46
	
47
}

+ 60 - 6
src/main/java/com/ekexiu/portal/service/ArticleService.java

14
import java.util.List;
14
import java.util.List;
15

15

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

27

26
import com.ekexiu.portal.dao.ArticleDao;
28
import com.ekexiu.portal.dao.ArticleDao;
29
import com.ekexiu.portal.dao.LeaveWordDao;
30
import com.ekexiu.portal.dao.OrgDao;
27
import com.ekexiu.portal.dao.ProfessorDao;
31
import com.ekexiu.portal.dao.ProfessorDao;
28
import com.ekexiu.portal.dao.ResearchAreaDao;
32
import com.ekexiu.portal.dao.ResearchAreaDao;
29
import com.ekexiu.portal.dao.ResourceDao;
33
import com.ekexiu.portal.dao.ResourceDao;
30
import com.ekexiu.portal.po.Article;
34
import com.ekexiu.portal.po.Article;
35
import com.ekexiu.portal.pojo.EditOrganization;
31
import com.ekexiu.portal.pojo.EditProfessor;
36
import com.ekexiu.portal.pojo.EditProfessor;
37
import com.ekexiu.portal.pojo.FindInfo;
32

38

33
@Path("/article")
39
@Path("/article")
34
public class ArticleService {
40
public class ArticleService {
47
	private ImageService imageService;
53
	private ImageService imageService;
48
	@Autowrie
54
	@Autowrie
49
	private ResearchAreaDao researchAreaDao;
55
	private ResearchAreaDao researchAreaDao;
56
	@Autowrie
57
	private LeaveWordDao leaveWordDao;
58
	@Autowrie
59
	private OrgDao orgDao;
50

60

51
	public String getDateFormat() {
61
	public String getDateFormat() {
52
		return dateFormat;
62
		return dateFormat;
120
		this.researchAreaDao = researchAreaDao;
130
		this.researchAreaDao = researchAreaDao;
121
	}
131
	}
122

132

133
	public LeaveWordDao getLeaveWordDao() {
134
		return leaveWordDao;
135
	}
136

137
	public void setLeaveWordDao(LeaveWordDao leaveWordDao) {
138
		this.leaveWordDao = leaveWordDao;
139
	}
140

141
	public OrgDao getOrgDao() {
142
		return orgDao;
143
	}
144

145
	public void setOrgDao(OrgDao orgDao) {
146
		this.orgDao = orgDao;
147
	}
148

123
	private byte[] resImage(byte[] src, int maxLen) throws IOException {
149
	private byte[] resImage(byte[] src, int maxLen) throws IOException {
124
		ByteArrayInputStream in = new ByteArrayInputStream(src);
150
		ByteArrayInputStream in = new ByteArrayInputStream(src);
125
		ByteArrayOutputStream out = new ByteArrayOutputStream();
151
		ByteArrayOutputStream out = new ByteArrayOutputStream();
172
			article.setArticleImg(this.createDate()+"/"+articleId+"."+JPG);
198
			article.setArticleImg(this.createDate()+"/"+articleId+"."+JPG);
173
		}
199
		}
174
		article.setArticleId(articleId);
200
		article.setArticleId(articleId);
201
		if(article.getProfessorId() != null && article.getOrgId() == null){
202
			article.setArticleType("1");
203
		}else if(article.getProfessorId() == null && article.getOrgId() != null){
204
			article.setArticleType("2");
205
		}else{
206
			throw new JfwBaseException(-1, "错误参数:文章发布者ID");
207
		}
175
		this.articleDao.insert(con, article);
208
		this.articleDao.insert(con, article);
176
		return articleId;
209
		return articleId;
177
	}
210
	}
193
		return this.articleDao.queryPro(con, professorId);
226
		return this.articleDao.queryPro(con, professorId);
194
	}
227
	}
195
	
228
	
229
	@Get
230
	@Path("/qaOrg")
231
	public List<Article> queryOrg(@JdbcConn Connection con,String orgId)throws SQLException{
232
		return this.articleDao.queryOrg(con, orgId);
233
	}
234
	
196
	@Get
235
	@Get
197
	@Path("/query")
236
	@Path("/query")
198
	public Article queryOne(@JdbcConn Connection con, String articleId) throws SQLException{
237
	public Article queryOne(@JdbcConn Connection con, String articleId) throws SQLException{
199
		Article article = this.articleDao.queryOne(con, articleId);
238
		Article article = this.articleDao.queryOne(con, articleId);
200
		if(article != null){
239
		if(article != null){
201
			EditProfessor professor = this.professorDao.queryBaseInfo(con, article.getProfessorId());
202
			if(professor != null){
203
				professor.setHasHeadImage(this.imageService.hasProfessorImage(professor.getId()));
204
				professor.setResearchAreas(this.researchAreaDao.query(con, professor.getId()));
205
				professor.setResources(this.resourceDao.queryPro(con, professor.getId()));
240
			if(article.getProfessorId() != null){
241
				EditProfessor professor = this.professorDao.queryBaseInfo(con, article.getProfessorId());
242
				if(professor != null){
243
					professor.setHasHeadImage(this.imageService.hasProfessorImage(professor.getId()));
244
					professor.setResearchAreas(this.researchAreaDao.query(con, professor.getId()));
245
					professor.setResources(this.resourceDao.queryPro(con, professor.getId()));
246
				}
247
				article.setProfessor(professor);
248
			}else if(article.getOrgId() != null){
249
				EditOrganization organization = this.orgDao.queryEditOrg(con, article.getOrgId());
250
				if(organization != null){
251
					organization.setHasOrgLogo(this.imageService.hasOrgLogo(organization.getId()));
252
				}
206
			}
253
			}
207
			article.setProfessor(professor);
208
		}
254
		}
209
		return article;
255
		return article;
210
	}
256
	}
211
	
257
	
258
	@Get
259
	@Path("/pqFind")
260
	public PageQueryResult<FindInfo> queryPage(@JdbcConn Connection con,@DefaultValue("10") int pageSize,@DefaultValue("1") int pageNo)throws SQLException{
261
		PageQueryResult<FindInfo> queryResult = this.articleDao.queryPage(con, pageSize, pageNo);
262
		return queryResult;
263
	}
264
	
212
	@Post
265
	@Post
213
	@Path("/delete")
266
	@Path("/delete")
214
	public void delete(@JdbcConn(true) Connection con, String articleId) throws SQLException{
267
	public void delete(@JdbcConn(true) Connection con, String articleId) throws SQLException{
215
		this.articleDao.delete(con, articleId);
268
		this.articleDao.delete(con, articleId);
269
		this.leaveWordDao.deleteArticle(con, articleId);
216
	}
270
	}
217

271

218
}
272
}