Skip to content

Commit 3ab55de

Browse files
SilverYoChammoqui
authored andcommitted
Feature #13789: improving performances into blog loading data.
1 parent 88e6d26 commit 3ab55de

File tree

9 files changed

+423
-454
lines changed

9 files changed

+423
-454
lines changed

blog/blog-library/src/main/java/org/silverpeas/components/blog/dao/PostDAO.java

Lines changed: 48 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,37 @@
2525

2626
import org.apache.commons.lang3.time.FastDateFormat;
2727
import org.silverpeas.core.persistence.jdbc.DBUtil;
28+
import org.silverpeas.core.util.Pair;
2829

2930
import java.sql.Connection;
3031
import java.sql.PreparedStatement;
31-
import java.sql.ResultSet;
3232
import java.sql.SQLException;
3333
import java.text.ParseException;
34-
import java.util.ArrayList;
3534
import java.util.Collection;
3635
import java.util.Date;
37-
import java.util.List;
36+
import java.util.Map;
37+
38+
import static java.util.stream.Collectors.toList;
39+
import static java.util.stream.Collectors.toMap;
40+
import static org.silverpeas.core.persistence.jdbc.sql.JdbcSqlQuery.select;
41+
import static org.silverpeas.core.persistence.jdbc.sql.JdbcSqlQuery.streamBySplittingOn;
3842

3943
public class PostDAO {
4044

4145
private static final FastDateFormat FORMATTER = FastDateFormat.getInstance("yyyy/MM/dd");
46+
private static final String BLOG_POST_TABLE_NAME = "SC_Blog_Post";
47+
private static final String PUB_ID = "pubId";
48+
private static final String DATE_EVENT = "dateEvent";
49+
private static final String EVENT_PERIOD_CLAUSE = "dateEvent >= ? and dateEvent <= ?";
50+
private static final String INSTANCE_ID_CLAUSE = "instanceId = ?";
51+
private static final String DESC = " DESC";
52+
private static final String ORDER_BY_DATE = DATE_EVENT + DESC;
53+
private static final String[] ORDER_BY_DATE_AND_PUB_ID = {DATE_EVENT + DESC, PUB_ID + DESC};
4254

4355
private PostDAO () {
4456
}
4557

46-
public static void createDateEvent(Connection con, String pubId, Date dateEvent,
58+
public static void create(Connection con, String pubId, Date dateEvent,
4759
String instanceId) throws SQLException {
4860
// Création
4961
PreparedStatement prepStmt = null;
@@ -62,28 +74,18 @@ public static void createDateEvent(Connection con, String pubId, Date dateEvent,
6274
}
6375
}
6476

65-
public static Date getDateEvent(Connection con, String pubId) throws SQLException {
66-
// récupérer la date
67-
String query = "select dateEvent from SC_Blog_Post where pubId = ? ";
68-
Date dateEvent = new Date();
69-
PreparedStatement prepStmt = null;
70-
ResultSet rs = null;
71-
try {
72-
prepStmt = con.prepareStatement(query);
73-
prepStmt.setInt(1, Integer.parseInt(pubId));
74-
rs = prepStmt.executeQuery();
75-
while (rs.next()) {
76-
// recuperation de la date
77-
dateEvent = new Date(Long.parseLong(rs.getString("dateEvent")));
78-
}
79-
} finally {
80-
// fermeture
81-
DBUtil.close(rs, prepStmt);
82-
}
83-
return dateEvent;
77+
public static Map<String, Date> getEventDateIndexedByPost(final Collection<String> pubIds)
78+
throws SQLException {
79+
return streamBySplittingOn(pubIds,
80+
idBatch -> select(PUB_ID + ", dateEvent")
81+
.from(BLOG_POST_TABLE_NAME)
82+
.where(PUB_ID)
83+
.in(idBatch.stream().map(Integer::parseInt).collect(toList()))
84+
.execute(r -> Pair.of(r.getString(1), new Date(Long.parseLong(r.getString(2))))))
85+
.collect(toMap(Pair::getFirst, Pair::getSecond));
8486
}
8587

86-
public static void deleteDateEvent(Connection con, String pubId) throws SQLException {
88+
public static void delete(Connection con, String pubId) throws SQLException {
8789
PreparedStatement prepStmt = null;
8890
try {
8991
String query = "delete from SC_Blog_Post where pubId = ? ";
@@ -96,7 +98,7 @@ public static void deleteDateEvent(Connection con, String pubId) throws SQLExcep
9698
}
9799
}
98100

99-
public static void updateDateEvent(Connection con, String pubId, Date dateEvent)
101+
public static void update(Connection con, String pubId, Date dateEvent)
100102
throws SQLException {
101103
PreparedStatement prepStmt = null;
102104
try {
@@ -113,76 +115,33 @@ public static void updateDateEvent(Connection con, String pubId, Date dateEvent)
113115
}
114116
}
115117

116-
public static Collection<String> getAllEvents(Connection con, String instanceId)
118+
public static Collection<String> getAllPostIds(Connection con, String instanceId)
117119
throws SQLException {
118-
// récupérer les derniers posts par date d'évènement
119-
List<String> listEvents = new ArrayList<>();
120-
String query =
121-
"select pubId from SC_Blog_Post where instanceId = ? order by dateEvent DESC, pubId DESC";
122-
PreparedStatement prepStmt = null;
123-
ResultSet rs = null;
124-
try {
125-
prepStmt = con.prepareStatement(query);
126-
prepStmt.setString(1, instanceId);
127-
rs = prepStmt.executeQuery();
128-
while (rs.next()) {
129-
String pubId = String.valueOf(rs.getInt("pubId"));
130-
listEvents.add(pubId);
131-
}
132-
} finally {
133-
// fermeture
134-
DBUtil.close(rs, prepStmt);
135-
}
136-
return listEvents;
120+
return select(PUB_ID)
121+
.from(BLOG_POST_TABLE_NAME)
122+
.where(INSTANCE_ID_CLAUSE, instanceId)
123+
.orderBy(ORDER_BY_DATE_AND_PUB_ID)
124+
.executeWith(con, r -> String.valueOf(r.getInt(PUB_ID)));
137125
}
138126

139-
public static Collection<Date> getAllDateEvents(Connection con, String instanceId)
127+
public static Collection<Date> getAllEventDates(Connection con, String instanceId)
140128
throws SQLException {
141-
ArrayList<Date> dateEvents = null;
142-
String query =
143-
"select dateEvent from SC_Blog_Post where instanceId = ? order by dateEvent DESC";
144-
PreparedStatement prepStmt = null;
145-
ResultSet rs = null;
146-
try {
147-
prepStmt = con.prepareStatement(query);
148-
prepStmt.setString(1, instanceId);
149-
rs = prepStmt.executeQuery();
150-
dateEvents = new ArrayList<>();
151-
while (rs.next()) {
152-
dateEvents.add(new Date(Long.parseLong(rs.getString("dateEvent"))));
153-
}
154-
} finally {
155-
// fermeture
156-
DBUtil.close(rs, prepStmt);
157-
}
158-
return dateEvents;
129+
return select("DISTINCT " + DATE_EVENT)
130+
.from(BLOG_POST_TABLE_NAME)
131+
.where(INSTANCE_ID_CLAUSE, instanceId)
132+
.orderBy(ORDER_BY_DATE)
133+
.executeWith(con, r -> new Date(Long.parseLong(r.getString(DATE_EVENT))));
159134
}
160135

161-
public static Collection<String> getEventsByDates(Connection con, String instanceId,
136+
public static Collection<String> getPostInRange(Connection con, String instanceId,
162137
String beginDate, String endDate) throws SQLException, ParseException {
163-
// récupérer les posts par date d'évènement entre 2 dates
164-
ArrayList<String> listEvents = null;
165-
166-
String query =
167-
"select pubId from SC_Blog_Post where instanceId = ? and dateEvent >= ? and dateEvent <= " +
168-
"? order by dateEvent DESC";
169-
PreparedStatement prepStmt = null;
170-
ResultSet rs = null;
171-
try {
172-
prepStmt = con.prepareStatement(query);
173-
prepStmt.setString(1, instanceId);
174-
prepStmt.setString(2, Long.toString((FORMATTER.parse(beginDate)).getTime()));
175-
prepStmt.setString(3, Long.toString((FORMATTER.parse(endDate)).getTime()));
176-
rs = prepStmt.executeQuery();
177-
listEvents = new ArrayList<>();
178-
while (rs.next()) {
179-
String pubId = "" + rs.getInt("pubId");
180-
listEvents.add(pubId);
181-
}
182-
} finally {
183-
// fermeture
184-
DBUtil.close(rs, prepStmt);
185-
}
186-
return listEvents;
138+
return select(PUB_ID)
139+
.from(BLOG_POST_TABLE_NAME)
140+
.where(INSTANCE_ID_CLAUSE, instanceId)
141+
.and(EVENT_PERIOD_CLAUSE,
142+
Long.toString(FORMATTER.parse(beginDate).getTime()),
143+
Long.toString(FORMATTER.parse(endDate).getTime()))
144+
.orderBy(ORDER_BY_DATE_AND_PUB_ID)
145+
.executeWith(con, r -> String.valueOf(r.getInt(PUB_ID)));
187146
}
188147
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Copyright (C) 2000 - 2024 Silverpeas
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU Affero General Public License as
6+
* published by the Free Software Foundation, either version 3 of the
7+
* License, or (at your option) any later version.
8+
*
9+
* As a special exception to the terms and conditions of version 3.0 of
10+
* the GPL, you may redistribute this Program in connection with Free/Libre
11+
* Open Source Software ("FLOSS") applications as described in Silverpeas's
12+
* FLOSS exception. You should have received a copy of the text describing
13+
* the FLOSS exception, and it is also available here:
14+
* "https://www.silverpeas.org/legal/floss_exception.html"
15+
*
16+
* This program is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU Affero General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU Affero General Public License
22+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
23+
*/
24+
25+
package org.silverpeas.components.blog.service;
26+
27+
import org.silverpeas.core.contribution.publication.model.PublicationDetail;
28+
29+
import java.util.Optional;
30+
import java.util.function.Predicate;
31+
32+
/**
33+
* Class allowing to define several filters for blog post fetching.
34+
* @author silveryocha
35+
*/
36+
public class BlogFilters {
37+
private final boolean getDraftIfNotCreator;
38+
private int maxResult = 0;
39+
private String creatorId = null;
40+
41+
/**
42+
* Mandatory constructor.
43+
* @param getDraftIfNotCreator true to get
44+
*/
45+
public BlogFilters(boolean getDraftIfNotCreator) {
46+
this.getDraftIfNotCreator = getDraftIfNotCreator;
47+
}
48+
49+
/**
50+
* Sets the identifier of the creator.
51+
* <p>
52+
* The creator is a registered Silverpeas user.
53+
* </p>
54+
* @param creatorId identifier of a user.
55+
* @return itself.
56+
*/
57+
public BlogFilters withCreatorId(final String creatorId) {
58+
this.creatorId = creatorId;
59+
return this;
60+
}
61+
62+
/**
63+
* Sets the maximum result if result must be limited.
64+
* @param maxResult positive integer to set a maximum result, 0 or negative means no limit.
65+
* @return itself.
66+
*/
67+
public BlogFilters withMaxResult(final int maxResult) {
68+
this.maxResult = maxResult;
69+
return this;
70+
}
71+
72+
/**
73+
* Gets the predicate to apply on publication list.
74+
* @return the {@link Predicate} instance.
75+
*/
76+
public Predicate<PublicationDetail> toPredicate() {
77+
return p -> !p.isDraft() || getDraftIfNotCreator || p.getCreatorId().equals(creatorId);
78+
}
79+
80+
/**
81+
* Gets the maximum of results if any.
82+
* @return an optional positive integer.
83+
*/
84+
public Optional<Integer> getMaxResult() {
85+
return Optional.of(maxResult).filter(l -> l > 0);
86+
}
87+
}

blog/blog-library/src/main/java/org/silverpeas/components/blog/service/BlogService.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
import org.silverpeas.core.util.ServiceProvider;
3636

3737
import java.util.Collection;
38-
import java.util.Date;
3938
import java.util.Optional;
4039

4140
/**
@@ -57,26 +56,29 @@ static BlogService get() {
5756

5857
void updatePost(final PostDetail post, final PdcClassification classification);
5958

60-
void deletePost(String postId, String instanceId);
59+
void deletePost(String instanceId, String postId);
6160

62-
Collection<PostDetail> getAllPosts(String instanceId);
61+
Collection<PostDetail> getLastPosts(String instanceId, final BlogFilters filters);
6362

64-
Collection<PostDetail> getAllValidPosts(String instanceId, int nbReturned);
63+
Collection<PostDetail> getAllPosts(String instanceId);
6564

66-
Date getDateEvent(String pubId);
65+
Collection<PostDetail> getLastValidPosts(String instanceId, BlogFilters filters);
6766

68-
Collection<PostDetail> getPostsByCategory(String categoryId, String instanceId);
67+
Collection<PostDetail> getPostsByCategory(String instanceId, String categoryId,
68+
final BlogFilters filters);
6969

70-
Collection<PostDetail> getPostsByArchive(String beginDate, String endDate,
71-
String instanceId);
70+
Collection<PostDetail> getPostsByArchive(String instanceId, String beginDate, String endDate,
71+
final BlogFilters filters);
7272

73-
Collection<PostDetail> getPostsByDate(String date, String instanceId);
73+
Collection<PostDetail> getPostsByEventDate(String instanceId, String date,
74+
final BlogFilters filters);
7475

75-
Collection<PostDetail> getResultSearch(String word, String userId, String instanceId);
76+
Collection<PostDetail> getResultSearch(String instanceId, String word, String userId,
77+
final BlogFilters filters);
7678

7779
void createCategory(final Category category);
7880

79-
void deleteCategory(String categoryId, String instanceId);
81+
void deleteCategory(String instanceId, String categoryId);
8082

8183
void updateCategory(final Category category);
8284

0 commit comments

Comments
 (0)