001    /*
002     * Copyright 2011 The Kuali Foundation.
003     * 
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     * 
008     * http://www.opensource.org/licenses/ecl2.php
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.kfs.module.purap.util;
017    
018    import java.math.BigDecimal;
019    import java.sql.Date;
020    import java.text.ParseException;
021    import java.text.SimpleDateFormat;
022    import java.util.Calendar;
023    
024    import org.apache.commons.lang.StringUtils;
025    import org.apache.commons.lang.time.DateUtils;
026    import org.apache.log4j.Logger;
027    import org.kuali.kfs.module.purap.PurapConstants;
028    import org.kuali.kfs.sys.context.SpringContext;
029    import org.kuali.rice.kns.service.DateTimeService;
030    
031    public class ElectronicInvoiceUtils {
032        
033        private final static Logger LOG = Logger.getLogger(ElectronicInvoiceUtils.class);
034        
035        public static Date getDate(String invoiceDateString){
036            
037            boolean formatInvalid = true;
038            String formattedDateString = "";
039            String stringToParse = null;
040            
041            if (StringUtils.isNotEmpty(invoiceDateString)) {
042    
043                String dateToConvert = null;
044                // get a copy of given date with 0's for all numbers to check format
045                formattedDateString = invoiceDateString.replaceAll("\\d", "0");
046    
047                if (PurApDateFormatUtils.getFormattingString(PurapConstants.NamedDateFormats.CXML_DATE_FORMAT).equals(formattedDateString)) {
048                    // Date is in 0000-00-00 format
049                    formatInvalid = false;
050                    stringToParse = invoiceDateString;
051                }
052                else if (PurApDateFormatUtils.getFormattingString(PurapConstants.NamedDateFormats.KUALI_DATE_FORMAT).equals(formattedDateString)) {
053                    try {
054                        java.util.Date javaDate = SpringContext.getBean(DateTimeService.class).convertToDate(invoiceDateString);
055                        return org.kuali.rice.kns.util.DateUtils.convertToSqlDate(javaDate);
056                    }
057                    catch (ParseException e) {
058                        return null;
059                    }
060                }
061                else if (PurApDateFormatUtils.getFormattingString(PurapConstants.NamedDateFormats.CXML_DATE_FORMAT).length() != formattedDateString.length()) {
062                    // strings are not the same length... must parse down given string from cXML for validation
063                    formattedDateString = formattedDateString.substring(0, PurApDateFormatUtils.getFormattingString(PurapConstants.NamedDateFormats.CXML_DATE_FORMAT).length());
064                    // strings should now be same length
065                    if (PurApDateFormatUtils.getFormattingString(PurapConstants.NamedDateFormats.CXML_DATE_FORMAT).equals(formattedDateString)) {
066                        // if strings are equal we can process date
067                        formatInvalid = false;
068                        stringToParse = invoiceDateString.substring(0, PurApDateFormatUtils.getFormattingString(PurapConstants.NamedDateFormats.CXML_DATE_FORMAT).length());
069                    }
070                    else {
071                        // strings are same size and both only use 0 characters so date is invalid
072                    }
073                }
074                else {
075                    /*
076                     * strings are of same length but are not equal this can only occur if date separators are invalid so we have an
077                     * invalid format
078                     */
079                }
080            }
081            
082            if (formatInvalid) {
083                return null;
084            }
085            else {
086                // try to parse date
087                SimpleDateFormat sdf = PurApDateFormatUtils.getSimpleDateFormat(PurapConstants.NamedDateFormats.CXML_SIMPLE_DATE_FORMAT);
088                try {
089                    return org.kuali.rice.kns.util.DateUtils.convertToSqlDate(sdf.parse(stringToParse));
090                }
091                catch (ParseException e) {
092                    return null;
093                }
094            }
095            
096        }
097        
098        public static String getDateDisplayText(java.util.Date date) {
099            Calendar c = Calendar.getInstance();
100            c.setTime(date);
101            // we add one to the month below because January = 0, February = 1, March = 2, and so on
102            String monthPart = (c.get(Calendar.MONTH) + 1) + "";
103            String dayPart = c.get(Calendar.DATE) + "";
104            if (monthPart.length() == 1){
105                monthPart = "0" + monthPart;
106            }
107            
108            if (dayPart.length() == 1){
109                dayPart = "0" + dayPart;
110            }
111            
112            String useDate =  monthPart + "/" + dayPart + "/" + c.get(Calendar.YEAR);
113            String actualDate = (date != null) ? date.toString() : "empty given date";
114            return useDate;
115        }
116        
117        public static String stripSplChars(String data){
118            if (data != null){
119                StringBuffer result = new StringBuffer();
120                for (int i = 0; i < data.length(); i++) {
121                  if (Character.isLetterOrDigit(data.charAt(i))){
122                      result.append(data.charAt(i));
123                  }
124                }
125                return result.toString();
126            }else{
127                return null;
128            }
129        }
130        
131    }