@@ -90,7 +90,7 @@ public long post( Map<String, String> attributes, List<File> attachments, Long m
9090
9191 String encoded_Text = new String (attributes .get ("Text" ).getBytes (), StandardCharsets .ISO_8859_1 ); // charset hell - this seems stupid but works
9292 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
9494 attributes .put ("Subject" , encoded_Subject );
9595
9696 Map <String , String > new_attributes = new HashMap <>(attributes );
@@ -235,19 +235,43 @@ public void delete( Long msgId ) throws LogbookException {
235235 * @param search_term
236236 *
237237 */
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 {
239239
240240 Map <String , Object > params = new HashMap <>();
241241 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+
244268 params .putAll ( search_term );
245269
246270 Map <String , Object > cookies = new HashMap <>();
247271 cookies .put ( "unm" , this .username );
248272 cookies .put ( "upwd" , this .password );
249273
250- Response <String > resp = Requests .get ( this . url )
274+ Response <String > resp = Requests .get ( requestUrl )
251275 .cookies (cookies )
252276 .params (params )
253277 .followRedirect (false )
@@ -257,12 +281,25 @@ public List<ElogEntry> search( Map<String, String> search_term ) throws LogbookE
257281 validateResponse ( resp );
258282
259283 List <ElogEntry > entries = new ArrayList <>();
284+ int totalCount = 0 ;
260285
261286 try {
262287 TagNode tagNode = new HtmlCleaner ().clean ( resp .body () );
263288 org .w3c .dom .Document doc = new DomSerializer ( new CleanerProperties ()).createDOM (tagNode );
264289 XPath xpath = XPathFactory .newInstance ().newXPath ();
265290 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+
266303 for ( int i = 0 ; i < msgIds .getLength (); i ++ ) {
267304 String msgIdStr = msgIds .item (i ).getNodeValue ();
268305 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
271308 throw new LogbookException ( "could not parse the elog response" , e );
272309 }
273310
274- return entries ;
311+ return ElogSearchResult . of ( entries , totalCount ) ;
275312 }
276313
277314
@@ -504,7 +541,10 @@ private long validateResponse( Response<String> resp ) throws LogbookException {
504541 } else {
505542 URI loc = URI .create ( location );
506543 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+ }
508548 }
509549 }
510550 if ( body .contains ("form name=form1" ) || body .contains ("type=password" ) ) {
0 commit comments