25
25
26
26
import org .apache .commons .lang3 .time .FastDateFormat ;
27
27
import org .silverpeas .core .persistence .jdbc .DBUtil ;
28
+ import org .silverpeas .core .util .Pair ;
28
29
29
30
import java .sql .Connection ;
30
31
import java .sql .PreparedStatement ;
31
- import java .sql .ResultSet ;
32
32
import java .sql .SQLException ;
33
33
import java .text .ParseException ;
34
- import java .util .ArrayList ;
35
34
import java .util .Collection ;
36
35
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 ;
38
42
39
43
public class PostDAO {
40
44
41
45
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 };
42
54
43
55
private PostDAO () {
44
56
}
45
57
46
- public static void createDateEvent (Connection con , String pubId , Date dateEvent ,
58
+ public static void create (Connection con , String pubId , Date dateEvent ,
47
59
String instanceId ) throws SQLException {
48
60
// Création
49
61
PreparedStatement prepStmt = null ;
@@ -62,28 +74,18 @@ public static void createDateEvent(Connection con, String pubId, Date dateEvent,
62
74
}
63
75
}
64
76
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 ));
84
86
}
85
87
86
- public static void deleteDateEvent (Connection con , String pubId ) throws SQLException {
88
+ public static void delete (Connection con , String pubId ) throws SQLException {
87
89
PreparedStatement prepStmt = null ;
88
90
try {
89
91
String query = "delete from SC_Blog_Post where pubId = ? " ;
@@ -96,7 +98,7 @@ public static void deleteDateEvent(Connection con, String pubId) throws SQLExcep
96
98
}
97
99
}
98
100
99
- public static void updateDateEvent (Connection con , String pubId , Date dateEvent )
101
+ public static void update (Connection con , String pubId , Date dateEvent )
100
102
throws SQLException {
101
103
PreparedStatement prepStmt = null ;
102
104
try {
@@ -113,76 +115,33 @@ public static void updateDateEvent(Connection con, String pubId, Date dateEvent)
113
115
}
114
116
}
115
117
116
- public static Collection <String > getAllEvents (Connection con , String instanceId )
118
+ public static Collection <String > getAllPostIds (Connection con , String instanceId )
117
119
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 )));
137
125
}
138
126
139
- public static Collection <Date > getAllDateEvents (Connection con , String instanceId )
127
+ public static Collection <Date > getAllEventDates (Connection con , String instanceId )
140
128
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 ))));
159
134
}
160
135
161
- public static Collection <String > getEventsByDates (Connection con , String instanceId ,
136
+ public static Collection <String > getPostInRange (Connection con , String instanceId ,
162
137
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 )));
187
146
}
188
147
}
0 commit comments