Skip to content

Commit f53d6ed

Browse files
committed
Add GroupImgPost and It's DAO.
1 parent fcdac91 commit f53d6ed

File tree

21 files changed

+291
-62
lines changed

21 files changed

+291
-62
lines changed

.idea/encodings.xml

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dumps/Dump20200415.sql

Lines changed: 38 additions & 6 deletions
Large diffs are not rendered by default.
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package com.louay.projects.model.chains.communications.group;
2+
3+
import java.sql.Blob;
4+
import java.sql.Timestamp;
5+
import java.util.Objects;
6+
7+
public class GroupImgPost {
8+
Long idPost;
9+
String idGroup;
10+
String username;
11+
java.sql.Blob image;
12+
String fileName;
13+
java.sql.Timestamp dateUpload;
14+
15+
public GroupImgPost() {
16+
}
17+
18+
public Long getIdPost() {
19+
return idPost;
20+
}
21+
22+
public void setIdPost(Long idPost) {
23+
this.idPost = idPost;
24+
}
25+
26+
public String getIdGroup() {
27+
return idGroup;
28+
}
29+
30+
public void setIdGroup(String idGroup) {
31+
this.idGroup = idGroup;
32+
}
33+
34+
public String getUsername() {
35+
return username;
36+
}
37+
38+
public void setUsername(String username) {
39+
this.username = username;
40+
}
41+
42+
public Blob getImage() {
43+
return image;
44+
}
45+
46+
public void setImage(Blob image) {
47+
this.image = image;
48+
}
49+
50+
public String getFileName() {
51+
return fileName;
52+
}
53+
54+
public void setFileName(String fileName) {
55+
this.fileName = fileName;
56+
}
57+
58+
public Timestamp getDateUpload() {
59+
return dateUpload;
60+
}
61+
62+
public void setDateUpload(Timestamp dateUpload) {
63+
this.dateUpload = dateUpload;
64+
}
65+
66+
@Override
67+
public boolean equals(Object o) {
68+
if (this == o) return true;
69+
if (!(o instanceof GroupImgPost)) return false;
70+
GroupImgPost that = (GroupImgPost) o;
71+
return getIdPost().compareTo(that.getIdPost()) == 0;
72+
}
73+
74+
@Override
75+
public int hashCode() {
76+
return Objects.hash(getIdPost());
77+
}
78+
79+
@Override
80+
public String toString() {
81+
return "GroupImgPost{" +
82+
"idPost=" + idPost +
83+
", idGroup='" + idGroup + '\'' +
84+
", username='" + username + '\'' +
85+
", image=" + image +
86+
", fileName='" + fileName + '\'' +
87+
", dateUpload=" + dateUpload +
88+
'}';
89+
}
90+
}

src/main/java/com/louay/projects/model/dao/DeleteGroupDAO.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.louay.projects.model.dao;
22

3+
import com.louay.projects.model.chains.communications.group.GroupImgPost;
34
import com.louay.projects.model.chains.communications.group.GroupTextPost;
45
import com.louay.projects.model.chains.communications.group.GroupPicture;
56
import com.louay.projects.model.chains.groups.GroupsDetail;
@@ -13,6 +14,8 @@ public interface DeleteGroupDAO {
1314

1415
int deleteGroupTextPostByIdPost(GroupTextPost post);
1516

17+
int deleteGroupImgPostByIdPost(GroupImgPost post);
18+
1619
int deleteGroupInviteByIdGroupAndUsername(GroupInvite invite);
1720

1821
int deleteGroupInviteByIdGroupAndDate(GroupInvite invite);
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package com.louay.projects.model.dao;
22

3+
import com.louay.projects.model.chains.communications.group.GroupImgPost;
34
import com.louay.projects.model.chains.communications.group.GroupTextPost;
45
import com.louay.projects.model.chains.communications.group.GroupPicture;
56

67
public interface InsertGroupPostDAO {
78

89
Long insertGroupTextPost(GroupTextPost post);
910

11+
Long insertGroupImgPost(GroupImgPost post);
12+
1013
int insertGroupPicture(GroupPicture picture);
1114
}

src/main/java/com/louay/projects/model/dao/SelectGroupDAO.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.louay.projects.model.dao;
22

3+
import com.louay.projects.model.chains.communications.group.GroupImgPost;
34
import com.louay.projects.model.chains.communications.group.GroupTextPost;
45
import com.louay.projects.model.chains.communications.group.GroupPicture;
56
import com.louay.projects.model.chains.groups.GroupsDetail;
@@ -22,6 +23,8 @@ public interface SelectGroupDAO {
2223

2324
Collection<GroupTextPost> findGroupTextPostByUsernameAndIdGroup(GroupTextPost post);
2425

26+
Collection<GroupImgPost> findGroupImgPostByUsername(GroupImgPost post);
27+
2528
Map<Long, GroupInvite> findGroupInviteByIdGroup(GroupInvite invite);
2629

2730
Map<Long, GroupInvite> findGroupInviteByUsername(GroupInvite invite);
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package com.louay.projects.model.dao;
22

3+
import com.louay.projects.model.chains.communications.group.GroupImgPost;
34
import com.louay.projects.model.chains.communications.group.GroupTextPost;
45
import com.louay.projects.model.chains.communications.group.GroupPicture;
56

67
public interface UpdateGroupPostDAO {
78

89
int updateGroupTextPostByIdComment(GroupTextPost post);
910

11+
int updateGroupImgPostByIdComment(GroupImgPost post);
12+
1013
int updateGroupPictureByIdGroup(GroupPicture picture);
1114
}

src/main/java/com/louay/projects/model/dao/impl/GroupDAOImpl.java

Lines changed: 91 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.louay.projects.model.dao.impl;
22

3+
import com.louay.projects.model.chains.communications.group.GroupImgPost;
34
import com.louay.projects.model.chains.communications.group.GroupTextPost;
45
import com.louay.projects.model.chains.communications.group.GroupPicture;
56
import com.louay.projects.model.chains.groups.GroupsDetail;
@@ -75,6 +76,30 @@ public Long insertGroupTextPost(GroupTextPost post) {
7576
return post.getIdPost();
7677
}
7778

79+
@Override
80+
public Long insertGroupImgPost(GroupImgPost post) {
81+
try {
82+
ConnectionWrapper wrapper = this.pool.getConnection();
83+
PreparedStatement insert = wrapper.getConnection().prepareStatement("INSERT INTO `group_img_post`(`idGroup`, " +
84+
"`username`, `img`, `fileName`, `dateUpload`) VALUES (?, ?, ?, ?, ?);", Statement.RETURN_GENERATED_KEYS);
85+
insert.setString(1, post.getIdGroup());
86+
insert.setString(2, post.getUsername());
87+
insert.setBlob(3, post.getImage());
88+
insert.setString(4, post.getFileName());
89+
insert.setTimestamp(5, post.getDateUpload());
90+
insert.executeUpdate();
91+
92+
ResultSet resultSet = insert.getGeneratedKeys();
93+
if (resultSet.next()) {
94+
post.setIdPost(resultSet.getLong(1));
95+
}
96+
this.pool.release(wrapper);
97+
} catch (SQLException e) {
98+
System.out.println(e.getMessage());
99+
}
100+
return post.getIdPost();
101+
}
102+
78103
@Override
79104
public int insertGroupPicture(GroupPicture picture) {
80105
int result = 0;
@@ -151,6 +176,19 @@ public int updateGroupTextPostByIdComment(GroupTextPost post) {
151176
return result;
152177
}
153178

179+
@Override
180+
public int updateGroupImgPostByIdComment(GroupImgPost post) {
181+
int result = 0;
182+
try {
183+
result = this.pool.updateQuery("UPDATE `group_img_post` SET `idGroup` = ?, `username` = ?, " +
184+
"`img` = ?, `fileName` = ?, `dateUpload` = ? WHERE `idPost` = ?;", post.getIdGroup(), post.getUsername(),
185+
post.getImage(), post.getFileName(), post.getDateUpload(), post.getIdPost());
186+
} catch (SQLException e) {
187+
System.out.println(e.getMessage());
188+
}
189+
return result;
190+
}
191+
154192
@Override
155193
public int updateGroupPictureByIdGroup(GroupPicture picture) {
156194
int result = 0;
@@ -265,7 +303,7 @@ public void buildGroupTextPostContainer(ResultSet resultSet, Collection<GroupTex
265303
@Override
266304
public Collection<GroupTextPost> findGroupTextPostByIdPost(GroupTextPost post) {
267305
@SuppressWarnings(value = "unchecked")
268-
Collection<GroupTextPost> container = (Collection<GroupTextPost>) ac.getBean("groupCommentContainer");
306+
Collection<GroupTextPost> container = (Collection<GroupTextPost>) ac.getBean("groupTextPostContainer");
269307
try {
270308
ResultSet resultSet = this.pool.selectResult("SELECT * FROM `group_post` WHERE `idPost` = ? " +
271309
"ORDER BY `group_post`.`postDate` DESC;", post.getIdPost());
@@ -279,7 +317,7 @@ public Collection<GroupTextPost> findGroupTextPostByIdPost(GroupTextPost post) {
279317
@Override
280318
public Collection<GroupTextPost> findGroupTextPostByIdGroup(GroupTextPost post) {
281319
@SuppressWarnings(value = "unchecked")
282-
Collection<GroupTextPost> container = (Collection<GroupTextPost>) ac.getBean("groupCommentContainer");
320+
Collection<GroupTextPost> container = (Collection<GroupTextPost>) ac.getBean("groupTextPostContainer");
283321
try {
284322
ResultSet resultSet = this.pool.selectResult("SELECT * FROM `group_post` WHERE `idGroupe` = ? " +
285323
"ORDER BY `group_post`.`postDate` DESC;", post.getIdGroup());
@@ -293,7 +331,7 @@ public Collection<GroupTextPost> findGroupTextPostByIdGroup(GroupTextPost post)
293331
@Override
294332
public Collection<GroupTextPost> findGroupTextPostByUsername(GroupTextPost post) {
295333
@SuppressWarnings(value = "unchecked")
296-
Collection<GroupTextPost> container = (Collection<GroupTextPost>) ac.getBean("groupCommentContainer");
334+
Collection<GroupTextPost> container = (Collection<GroupTextPost>) ac.getBean("groupTextPostContainer");
297335
try {
298336
ResultSet resultSet = this.pool.selectResult("SELECT * FROM `group_post` WHERE `username` = ? " +
299337
"ORDER BY `group_post`.`postDate` DESC;", post.getUsername());
@@ -307,7 +345,7 @@ public Collection<GroupTextPost> findGroupTextPostByUsername(GroupTextPost post)
307345
@Override
308346
public Collection<GroupTextPost> findGroupTextPostByUsernameAndIdGroup(GroupTextPost post) {
309347
@SuppressWarnings(value = "unchecked")
310-
Collection<GroupTextPost> container = (Collection<GroupTextPost>) ac.getBean("groupCommentContainer");
348+
Collection<GroupTextPost> container = (Collection<GroupTextPost>) ac.getBean("groupTextPostContainer");
311349
try {
312350
ResultSet resultSet = this.pool.selectResult("SELECT * FROM `group_post` WHERE `username` = ? " +
313351
"AND `idGroupe` = ? ORDER BY `group_post`.`postDate` DESC;", post.getUsername(),
@@ -319,6 +357,45 @@ public Collection<GroupTextPost> findGroupTextPostByUsernameAndIdGroup(GroupText
319357
return container;
320358
}
321359

360+
private GroupImgPost buildGroupImgPost(ResultSet resultSet) {
361+
GroupImgPost post = ac.getBean(GroupImgPost.class);
362+
try {
363+
post.setIdPost(resultSet.getLong(1));
364+
post.setIdGroup(resultSet.getString(2));
365+
post.setUsername(resultSet.getString(3));
366+
post.setImage(resultSet.getBlob(4));
367+
post.setFileName(resultSet.getString(5));
368+
post.setDateUpload(resultSet.getTimestamp(6));
369+
} catch (SQLException e) {
370+
System.out.println(e.getMessage());
371+
}
372+
return post;
373+
}
374+
375+
public void buildGroupImgPostContainer(ResultSet resultSet, Collection<GroupImgPost> container) {
376+
try {
377+
while (resultSet.next()) {
378+
container.add(buildGroupImgPost(resultSet));
379+
}
380+
} catch (SQLException e) {
381+
System.out.println(e.getMessage());
382+
}
383+
}
384+
385+
@Override
386+
public Collection<GroupImgPost> findGroupImgPostByUsername(GroupImgPost post) {
387+
@SuppressWarnings(value = "unchecked")
388+
Collection<GroupImgPost> container = (Collection<GroupImgPost>) ac.getBean("groupImgPostContainer");
389+
try {
390+
ResultSet resultSet = this.pool.selectResult("SELECT * FROM `group_img_post` WHERE `username` = ? " +
391+
"ORDER BY `group_img_post`.`dateUpload` DESC;", post.getUsername());
392+
buildGroupImgPostContainer(resultSet, container);
393+
} catch (SQLException e) {
394+
System.out.println(e.getMessage());
395+
}
396+
return container;
397+
}
398+
322399
private GroupInvite buildGroupInvite(ResultSet resultSet) {
323400
GroupInvite invite = ac.getBean(GroupInvite.class);
324401
try {
@@ -536,6 +613,16 @@ public int deleteGroupTextPostByIdPost(GroupTextPost post) {
536613
return result;
537614
}
538615

616+
@Override
617+
public int deleteGroupImgPostByIdPost(GroupImgPost post) {
618+
int result = 0;
619+
try {
620+
result = this.pool.updateQuery("DELETE FROM `group_img_post` WHERE `idPost` = ?;", post.getIdPost());
621+
} catch (SQLException e) {
622+
System.out.println(e.getMessage());
623+
}
624+
return result; }
625+
539626
@Override
540627
public int deleteGroupInviteByIdGroupAndUsername(GroupInvite invite) {
541628
int result = 0;

src/main/java/com/louay/projects/model/factory/BeansFactory.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.louay.projects.model.chains.communications.AccountTextPost;
66
import com.louay.projects.model.chains.communications.AccountMessage;
77
import com.louay.projects.model.chains.communications.AccountPicture;
8+
import com.louay.projects.model.chains.communications.group.GroupImgPost;
89
import com.louay.projects.model.chains.communications.group.GroupTextPost;
910
import com.louay.projects.model.chains.communications.group.GroupPicture;
1011
import com.louay.projects.model.chains.groups.GroupsDetail;
@@ -160,12 +161,18 @@ public Collection<GroupsDetail> getGroupDetailContainer(){
160161
return new HashSet<>();
161162
}
162163

163-
@Bean(name = "groupCommentContainer")
164+
@Bean(name = "groupTextPostContainer")
164165
@Scope("prototype")
165166
public Collection<GroupTextPost> getGroupCommentContainer(){
166167
return new LinkedHashSet<>();
167168
}
168169

170+
@Bean(name = "groupImgPostContainer")
171+
@Scope("prototype")
172+
public Collection<GroupImgPost> getGroupImgPostContainer(){
173+
return new LinkedHashSet<>();
174+
}
175+
169176
@Bean(name = "groupPictureContainer")
170177
@Scope("prototype")
171178
public Collection<GroupPicture> getGroupPictureContainer(){

src/main/java/com/louay/projects/model/util/file/FileProcess.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public byte[] readAPicture(String path) throws IOException {
2020
if (file.canExecute()) {
2121
if (file.canRead()) {
2222
bytes = new byte[(int) file.length()];
23-
in = new BufferedInputStream(new FileInputStream(path), 2048);
23+
in = new BufferedInputStream(new FileInputStream(path));
2424
int i = 0;
2525
while (in.available() != 0) {
2626
bytes[i] = (byte) in.read();
@@ -40,9 +40,9 @@ public byte[] readAPicture(String path) throws IOException {
4040
}
4141

4242
public void writePicture(String path, byte[] bytes) throws IOException {
43-
OutputStream out = new BufferedOutputStream(new FileOutputStream(path), 2048);
43+
OutputStream out = new BufferedOutputStream(new FileOutputStream(path));
4444
out.write(bytes);
4545
out.flush();
4646
out.close();
4747
}
48-
}
48+
}

0 commit comments

Comments
 (0)