View Javadoc
1   package org.kuali.ole.sip2.sip2Server;
2   
3   import java.text.ParseException;
4   import java.text.SimpleDateFormat;
5   import java.util.Calendar;
6   import java.util.Date;
7   
8   /**
9    * This class offers methods that are needed for creating SIP2 messages.
10   *
11   * @author Gayathri A
12   */
13  public class MessageUtil {
14  
15      /**
16       * Computes the checksum of the given string.
17       *
18       * @param str transmission string
19       * @return checksum of the given string
20       */
21      public static String computeChecksum(String str) {
22          char[] message = str.toCharArray();
23          int checksum = 0;
24          int i = 0;
25          // Count the binary sum of the characters
26          while (i < str.length()) {
27              checksum += (int) message[i];
28              i++;
29          }
30          // Take the lower 16 bits of the total and
31          // perform 2's complement
32          checksum = -(checksum & 0xFFFF);
33          // Return the result represented by four hex digits
34          return Integer.toHexString(checksum).substring(4, 8).toUpperCase();
35      }
36  
37      /**
38       * Returns the current date and time in the format used in the
39       * SIP2 messages. The SIP2 format is "yyyyMMdd    HHmmss".
40       *
41       * @return current date and time in SIP2 format
42       */
43      public static String getSipDateTime() {
44          SimpleDateFormat simpleDf = new SimpleDateFormat("yyyyMMdd    HHmmss");
45          return simpleDf.format(new Date());
46      }
47  
48      /**
49       * Parses the date and time from the given SIP2 formatted string. The SIP2
50       * format is "yyyyMMdd    HHmmss".
51       *
52       * @param dateStr date and time in SIP2 format
53       * @return Date object
54       */
55      public static Date parseSipDateTime(String dateStr) {
56          SimpleDateFormat simpleDf = new SimpleDateFormat("yyyyMMdd    HHmmss");
57          try {
58              return simpleDf.parse(dateStr);
59          } catch (ParseException pe) {
60              return null;
61          }
62      }
63  
64      /**
65       * Converts the given date to a SIP2 formatted date/time string. The SIP2
66       * format is "yyyyMMdd    HHmmss".
67       *
68       * @param date date object to be converted
69       * @return SIP2 formatted date/time string
70       */
71      public static String toSipDateTime(Date date) {
72          SimpleDateFormat simpleDf = new SimpleDateFormat("yyyyMMdd    HHmmss");
73          return simpleDf.format(date);
74      }
75  
76      /**
77       * Creates a new date/time string presented in the SIP2 format
78       * "yyyyMMdd    HHmmss" by adding the given number of days and months
79       * to the current date. This method is usefull when setting an
80       * expiration date to a hold.
81       *
82       * @param days   number of days to be added to the current date
83       * @param months number of months to be added to the current date
84       * @return current date plus the given number of days and months in the
85       * SIP2 format "yyyyMMdd    HHmmss"
86       */
87      public static String createFutureDate(int days, int months) {
88          Calendar date = Calendar.getInstance();
89          date.setTime(new Date());
90          date.add(Calendar.DATE, days);
91          date.add(Calendar.MONTH, months);
92          return toSipDateTime(date.getTime());
93      }
94  }