Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature #13789: improving performances into blog loading data. #849

Merged
merged 1 commit into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,37 @@

import org.apache.commons.lang3.time.FastDateFormat;
import org.silverpeas.core.persistence.jdbc.DBUtil;
import org.silverpeas.core.util.Pair;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;

import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toMap;
import static org.silverpeas.core.persistence.jdbc.sql.JdbcSqlQuery.select;
import static org.silverpeas.core.persistence.jdbc.sql.JdbcSqlQuery.streamBySplittingOn;

public class PostDAO {

private static final FastDateFormat FORMATTER = FastDateFormat.getInstance("yyyy/MM/dd");
private static final String BLOG_POST_TABLE_NAME = "SC_Blog_Post";
private static final String PUB_ID = "pubId";
private static final String DATE_EVENT = "dateEvent";
private static final String EVENT_PERIOD_CLAUSE = "dateEvent >= ? and dateEvent <= ?";
private static final String INSTANCE_ID_CLAUSE = "instanceId = ?";
private static final String DESC = " DESC";
private static final String ORDER_BY_DATE = DATE_EVENT + DESC;
private static final String[] ORDER_BY_DATE_AND_PUB_ID = {DATE_EVENT + DESC, PUB_ID + DESC};

private PostDAO () {
}

public static void createDateEvent(Connection con, String pubId, Date dateEvent,
public static void create(Connection con, String pubId, Date dateEvent,
String instanceId) throws SQLException {
// Création
PreparedStatement prepStmt = null;
Expand All @@ -62,28 +74,18 @@ public static void createDateEvent(Connection con, String pubId, Date dateEvent,
}
}

public static Date getDateEvent(Connection con, String pubId) throws SQLException {
// récupérer la date
String query = "select dateEvent from SC_Blog_Post where pubId = ? ";
Date dateEvent = new Date();
PreparedStatement prepStmt = null;
ResultSet rs = null;
try {
prepStmt = con.prepareStatement(query);
prepStmt.setInt(1, Integer.parseInt(pubId));
rs = prepStmt.executeQuery();
while (rs.next()) {
// recuperation de la date
dateEvent = new Date(Long.parseLong(rs.getString("dateEvent")));
}
} finally {
// fermeture
DBUtil.close(rs, prepStmt);
}
return dateEvent;
public static Map<String, Date> getEventDateIndexedByPost(final Collection<String> pubIds)
throws SQLException {
return streamBySplittingOn(pubIds,
idBatch -> select(PUB_ID + ", dateEvent")
.from(BLOG_POST_TABLE_NAME)
.where(PUB_ID)
.in(idBatch.stream().map(Integer::parseInt).collect(toList()))
.execute(r -> Pair.of(r.getString(1), new Date(Long.parseLong(r.getString(2))))))
.collect(toMap(Pair::getFirst, Pair::getSecond));
}

public static void deleteDateEvent(Connection con, String pubId) throws SQLException {
public static void delete(Connection con, String pubId) throws SQLException {
PreparedStatement prepStmt = null;
try {
String query = "delete from SC_Blog_Post where pubId = ? ";
Expand All @@ -96,7 +98,7 @@ public static void deleteDateEvent(Connection con, String pubId) throws SQLExcep
}
}

public static void updateDateEvent(Connection con, String pubId, Date dateEvent)
public static void update(Connection con, String pubId, Date dateEvent)
throws SQLException {
PreparedStatement prepStmt = null;
try {
Expand All @@ -113,76 +115,33 @@ public static void updateDateEvent(Connection con, String pubId, Date dateEvent)
}
}

public static Collection<String> getAllEvents(Connection con, String instanceId)
public static Collection<String> getAllPostIds(Connection con, String instanceId)
throws SQLException {
// récupérer les derniers posts par date d'évènement
List<String> listEvents = new ArrayList<>();
String query =
"select pubId from SC_Blog_Post where instanceId = ? order by dateEvent DESC, pubId DESC";
PreparedStatement prepStmt = null;
ResultSet rs = null;
try {
prepStmt = con.prepareStatement(query);
prepStmt.setString(1, instanceId);
rs = prepStmt.executeQuery();
while (rs.next()) {
String pubId = String.valueOf(rs.getInt("pubId"));
listEvents.add(pubId);
}
} finally {
// fermeture
DBUtil.close(rs, prepStmt);
}
return listEvents;
return select(PUB_ID)
.from(BLOG_POST_TABLE_NAME)
.where(INSTANCE_ID_CLAUSE, instanceId)
.orderBy(ORDER_BY_DATE_AND_PUB_ID)
.executeWith(con, r -> String.valueOf(r.getInt(PUB_ID)));
}

public static Collection<Date> getAllDateEvents(Connection con, String instanceId)
public static Collection<Date> getAllEventDates(Connection con, String instanceId)
throws SQLException {
ArrayList<Date> dateEvents = null;
String query =
"select dateEvent from SC_Blog_Post where instanceId = ? order by dateEvent DESC";
PreparedStatement prepStmt = null;
ResultSet rs = null;
try {
prepStmt = con.prepareStatement(query);
prepStmt.setString(1, instanceId);
rs = prepStmt.executeQuery();
dateEvents = new ArrayList<>();
while (rs.next()) {
dateEvents.add(new Date(Long.parseLong(rs.getString("dateEvent"))));
}
} finally {
// fermeture
DBUtil.close(rs, prepStmt);
}
return dateEvents;
return select("DISTINCT " + DATE_EVENT)
.from(BLOG_POST_TABLE_NAME)
.where(INSTANCE_ID_CLAUSE, instanceId)
.orderBy(ORDER_BY_DATE)
.executeWith(con, r -> new Date(Long.parseLong(r.getString(DATE_EVENT))));
}

public static Collection<String> getEventsByDates(Connection con, String instanceId,
public static Collection<String> getPostInRange(Connection con, String instanceId,
String beginDate, String endDate) throws SQLException, ParseException {
// récupérer les posts par date d'évènement entre 2 dates
ArrayList<String> listEvents = null;

String query =
"select pubId from SC_Blog_Post where instanceId = ? and dateEvent >= ? and dateEvent <= " +
"? order by dateEvent DESC";
PreparedStatement prepStmt = null;
ResultSet rs = null;
try {
prepStmt = con.prepareStatement(query);
prepStmt.setString(1, instanceId);
prepStmt.setString(2, Long.toString((FORMATTER.parse(beginDate)).getTime()));
prepStmt.setString(3, Long.toString((FORMATTER.parse(endDate)).getTime()));
rs = prepStmt.executeQuery();
listEvents = new ArrayList<>();
while (rs.next()) {
String pubId = "" + rs.getInt("pubId");
listEvents.add(pubId);
}
} finally {
// fermeture
DBUtil.close(rs, prepStmt);
}
return listEvents;
return select(PUB_ID)
.from(BLOG_POST_TABLE_NAME)
.where(INSTANCE_ID_CLAUSE, instanceId)
.and(EVENT_PERIOD_CLAUSE,
Long.toString(FORMATTER.parse(beginDate).getTime()),
Long.toString(FORMATTER.parse(endDate).getTime()))
.orderBy(ORDER_BY_DATE_AND_PUB_ID)
.executeWith(con, r -> String.valueOf(r.getInt(PUB_ID)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Copyright (C) 2000 - 2024 Silverpeas
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* As a special exception to the terms and conditions of version 3.0 of
* the GPL, you may redistribute this Program in connection with Free/Libre
* Open Source Software ("FLOSS") applications as described in Silverpeas's
* FLOSS exception. You should have received a copy of the text describing
* the FLOSS exception, and it is also available here:
* "https://www.silverpeas.org/legal/floss_exception.html"
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package org.silverpeas.components.blog.service;

import org.silverpeas.core.contribution.publication.model.PublicationDetail;

import java.util.Optional;
import java.util.function.Predicate;

/**
* Class allowing to define several filters for blog post fetching.
* @author silveryocha
*/
public class BlogFilters {
private final boolean getDraftIfNotCreator;
private int maxResult = 0;
private String creatorId = null;

/**
* Mandatory constructor.
* @param getDraftIfNotCreator true to get
*/
public BlogFilters(boolean getDraftIfNotCreator) {
this.getDraftIfNotCreator = getDraftIfNotCreator;
}

/**
* Sets the identifier of the creator.
* <p>
* The creator is a registered Silverpeas user.
* </p>
* @param creatorId identifier of a user.
* @return itself.
*/
public BlogFilters withCreatorId(final String creatorId) {
this.creatorId = creatorId;
return this;
}

/**
* Sets the maximum result if result must be limited.
* @param maxResult positive integer to set a maximum result, 0 or negative means no limit.
* @return itself.
*/
public BlogFilters withMaxResult(final int maxResult) {
this.maxResult = maxResult;
return this;
}

/**
* Gets the predicate to apply on publication list.
* @return the {@link Predicate} instance.
*/
public Predicate<PublicationDetail> toPredicate() {
return p -> !p.isDraft() || getDraftIfNotCreator || p.getCreatorId().equals(creatorId);
}

/**
* Gets the maximum of results if any.
* @return an optional positive integer.
*/
public Optional<Integer> getMaxResult() {
return Optional.of(maxResult).filter(l -> l > 0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import org.silverpeas.core.util.ServiceProvider;

import java.util.Collection;
import java.util.Date;
import java.util.Optional;

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

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

void deletePost(String postId, String instanceId);
void deletePost(String instanceId, String postId);

Collection<PostDetail> getAllPosts(String instanceId);
Collection<PostDetail> getLastPosts(String instanceId, final BlogFilters filters);

Collection<PostDetail> getAllValidPosts(String instanceId, int nbReturned);
Collection<PostDetail> getAllPosts(String instanceId);

Date getDateEvent(String pubId);
Collection<PostDetail> getLastValidPosts(String instanceId, BlogFilters filters);

Collection<PostDetail> getPostsByCategory(String categoryId, String instanceId);
Collection<PostDetail> getPostsByCategory(String instanceId, String categoryId,
final BlogFilters filters);

Collection<PostDetail> getPostsByArchive(String beginDate, String endDate,
String instanceId);
Collection<PostDetail> getPostsByArchive(String instanceId, String beginDate, String endDate,
final BlogFilters filters);

Collection<PostDetail> getPostsByDate(String date, String instanceId);
Collection<PostDetail> getPostsByEventDate(String instanceId, String date,
final BlogFilters filters);

Collection<PostDetail> getResultSearch(String word, String userId, String instanceId);
Collection<PostDetail> getResultSearch(String instanceId, String word, String userId,
final BlogFilters filters);

void createCategory(final Category category);

void deleteCategory(String categoryId, String instanceId);
void deleteCategory(String instanceId, String categoryId);

void updateCategory(final Category category);

Expand Down