@@ -90,7 +90,7 @@ public long post( Map<String, String> attributes, List<File> attachments, Long m
90
90
91
91
String encoded_Text = new String (attributes .get ("Text" ).getBytes (), StandardCharsets .ISO_8859_1 ); // charset hell - this seems stupid but works
92
92
attributes .put ("Text" , encoded_Text );
93
- String encoded_Subject = new String (attributes .get ("Text " ).getBytes (), StandardCharsets .UTF_8 ); // charset hell - this seems stupid but works
93
+ String encoded_Subject = new String (attributes .get ("Subject " ).getBytes (), StandardCharsets .UTF_8 ); // charset hell - this seems stupid but works
94
94
attributes .put ("Subject" , encoded_Subject );
95
95
96
96
Map <String , String > new_attributes = new HashMap <>(attributes );
@@ -235,19 +235,43 @@ public void delete( Long msgId ) throws LogbookException {
235
235
* @param search_term
236
236
*
237
237
*/
238
- public List < ElogEntry > search ( Map <String , String > search_term ) throws LogbookException {
238
+ public ElogSearchResult search ( Map <String , String > search_term , Integer from , Integer size ) throws LogbookException {
239
239
240
240
Map <String , Object > params = new HashMap <>();
241
241
params .put ( "mode" , "summary" );
242
- params .put ( "reverse" , "1" );
243
- params .put ( "npp" , 20 ); // number of returned results...
242
+
243
+ // Sort by date
244
+ if (search_term .containsKey ("sort" )) {
245
+ String sortDirection = search_term .get ("sort" );
246
+ if ("up" .equalsIgnoreCase (sortDirection )) {
247
+ params .put ( "reverse" , "0" );
248
+ } else if ("down" .equalsIgnoreCase (sortDirection )) {
249
+ params .put ( "reverse" , "1" );
250
+ }
251
+ search_term .remove ("sort" );
252
+ } else {
253
+ params .put ( "reverse" , "1" );
254
+ }
255
+
256
+ // Pagination parameters
257
+ String requestUrl = this .url ;
258
+ if (from != null && size != null ) {
259
+ if (size != 0 ) {
260
+ int page = from / size + 1 ;
261
+ requestUrl = String .format ("%spage%d" , requestUrl , page );
262
+ params .put ( "npp" , size );
263
+ }
264
+ } else {
265
+ params .put ( "npp" , 20 ); // number of returned results...
266
+ }
267
+
244
268
params .putAll ( search_term );
245
269
246
270
Map <String , Object > cookies = new HashMap <>();
247
271
cookies .put ( "unm" , this .username );
248
272
cookies .put ( "upwd" , this .password );
249
273
250
- Response <String > resp = Requests .get ( this . url )
274
+ Response <String > resp = Requests .get ( requestUrl )
251
275
.cookies (cookies )
252
276
.params (params )
253
277
.followRedirect (false )
@@ -257,12 +281,25 @@ public List<ElogEntry> search( Map<String, String> search_term ) throws LogbookE
257
281
validateResponse ( resp );
258
282
259
283
List <ElogEntry > entries = new ArrayList <>();
284
+ int totalCount = 0 ;
260
285
261
286
try {
262
287
TagNode tagNode = new HtmlCleaner ().clean ( resp .body () );
263
288
org .w3c .dom .Document doc = new DomSerializer ( new CleanerProperties ()).createDOM (tagNode );
264
289
XPath xpath = XPathFactory .newInstance ().newXPath ();
265
290
NodeList msgIds = (NodeList ) xpath .evaluate ( "(//tr/td[@class=\" list1\" or @class=\" list2\" ][1])/a/@href" , doc , XPathConstants .NODESET );
291
+
292
+ // Extract the number of all entries
293
+ String expression = "//b[contains(., 'Entries')]" ;
294
+ String result = (String ) xpath .evaluate (expression , doc , XPathConstants .STRING );
295
+ if (result != null && !result .isEmpty ()) {
296
+ java .util .regex .Pattern pattern = java .util .regex .Pattern .compile ("(\\ d+)\\ s+Entries" );
297
+ java .util .regex .Matcher matcher = pattern .matcher (result );
298
+ if (matcher .find ()) {
299
+ totalCount = Integer .parseInt (matcher .group (1 ));
300
+ }
301
+ }
302
+
266
303
for ( int i = 0 ; i < msgIds .getLength (); i ++ ) {
267
304
String msgIdStr = msgIds .item (i ).getNodeValue ();
268
305
entries .add ( read ( Long .valueOf ( msgIdStr .substring ( msgIdStr .lastIndexOf ('/' ) + 1 ) )));
@@ -271,7 +308,7 @@ public List<ElogEntry> search( Map<String, String> search_term ) throws LogbookE
271
308
throw new LogbookException ( "could not parse the elog response" , e );
272
309
}
273
310
274
- return entries ;
311
+ return ElogSearchResult . of ( entries , totalCount ) ;
275
312
}
276
313
277
314
@@ -504,7 +541,10 @@ private long validateResponse( Response<String> resp ) throws LogbookException {
504
541
} else {
505
542
URI loc = URI .create ( location );
506
543
String [] path = loc .getPath ().split ("/" );
507
- msgId = Integer .parseInt ( path [ path .length -1 ] );
544
+ String msgIdStr = path [ path .length -1 ];
545
+ if (msgIdStr != null && msgIdStr .matches ("\\ d+" )) {
546
+ msgId = Integer .parseInt ( msgIdStr );
547
+ }
508
548
}
509
549
}
510
550
if ( body .contains ("form name=form1" ) || body .contains ("type=password" ) ) {
0 commit comments