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