View Javadoc
1   /*
2    * Copyright 2008 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.module.purap.util;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.apache.log4j.Logger;
20  import org.kuali.ole.module.purap.PurapConstants;
21  import org.kuali.ole.sys.context.SpringContext;
22  import org.kuali.rice.core.api.datetime.DateTimeService;
23  
24  import java.sql.Date;
25  import java.text.ParseException;
26  import java.text.SimpleDateFormat;
27  import java.util.Calendar;
28  
29  public class ElectronicInvoiceUtils {
30  
31      private final static Logger LOG = Logger.getLogger(ElectronicInvoiceUtils.class);
32  
33      public static Date getDate(String invoiceDateString) {
34  
35          boolean formatInvalid = true;
36          String formattedDateString = "";
37          String stringToParse = null;
38  
39          if (StringUtils.isNotEmpty(invoiceDateString)) {
40  
41              String dateToConvert = null;
42              // get a copy of given date with 0's for all numbers to check format
43              formattedDateString = invoiceDateString.replaceAll("\\d", "0");
44  
45              if (PurApDateFormatUtils.getFormattingString(PurapConstants.NamedDateFormats.CXML_DATE_FORMAT).equals(formattedDateString)) {
46                  // Date is in 0000-00-00 format
47                  formatInvalid = false;
48                  stringToParse = invoiceDateString;
49              } else if (PurApDateFormatUtils.getFormattingString(PurapConstants.NamedDateFormats.KUALI_DATE_FORMAT).equals(formattedDateString)) {
50                  try {
51                      java.util.Date javaDate = SpringContext.getBean(DateTimeService.class).convertToDate(invoiceDateString);
52                      return org.kuali.ole.sys.util.KfsDateUtils.convertToSqlDate(javaDate);
53                  } catch (ParseException e) {
54                      return null;
55                  }
56              } else if (PurApDateFormatUtils.getFormattingString(PurapConstants.NamedDateFormats.CXML_DATE_FORMAT).length() != formattedDateString.length()) {
57                  // strings are not the same length... must parse down given string from cXML for validation
58                  formattedDateString = formattedDateString.substring(0, PurApDateFormatUtils.getFormattingString(PurapConstants.NamedDateFormats.CXML_DATE_FORMAT).length());
59                  // strings should now be same length
60                  if (PurApDateFormatUtils.getFormattingString(PurapConstants.NamedDateFormats.CXML_DATE_FORMAT).equals(formattedDateString)) {
61                      // if strings are equal we can process date
62                      formatInvalid = false;
63                      stringToParse = invoiceDateString.substring(0, PurApDateFormatUtils.getFormattingString(PurapConstants.NamedDateFormats.CXML_DATE_FORMAT).length());
64                  } else {
65                      // strings are same size and both only use 0 characters so date is invalid
66                  }
67              } else {
68                  /*
69                   * strings are of same length but are not equal this can only occur if date separators are invalid so we have an
70                   * invalid format
71                   */
72              }
73          }
74  
75          if (formatInvalid) {
76              return null;
77          } else {
78              // try to parse date
79              SimpleDateFormat sdf = PurApDateFormatUtils.getSimpleDateFormat(PurapConstants.NamedDateFormats.CXML_SIMPLE_DATE_FORMAT);
80              try {
81                  return org.kuali.ole.sys.util.KfsDateUtils.convertToSqlDate(sdf.parse(stringToParse));
82              } catch (ParseException e) {
83                  return null;
84              }
85          }
86  
87      }
88  
89      public static String getDateDisplayText(java.util.Date date) {
90          Calendar c = Calendar.getInstance();
91          c.setTime(date);
92          // we add one to the month below because January = 0, February = 1, March = 2, and so on
93          String monthPart = (c.get(Calendar.MONTH) + 1) + "";
94          String dayPart = c.get(Calendar.DATE) + "";
95          if (monthPart.length() == 1) {
96              monthPart = "0" + monthPart;
97          }
98  
99          if (dayPart.length() == 1) {
100             dayPart = "0" + dayPart;
101         }
102 
103         String useDate = monthPart + "/" + dayPart + "/" + c.get(Calendar.YEAR);
104         String actualDate = (date != null) ? date.toString() : "empty given date";
105         return useDate;
106     }
107 
108     public static String stripSplChars(String data) {
109         if (data != null) {
110             StringBuffer result = new StringBuffer();
111             for (int i = 0; i < data.length(); i++) {
112                 if (Character.isLetterOrDigit(data.charAt(i))) {
113                     result.append(data.charAt(i));
114                 }
115             }
116             return result.toString();
117         } else {
118             return null;
119         }
120     }
121 
122 }