Merge pull request #1365 from hapifhir/do-20230726-android-remove-timestamp

Remove usage of java.sql.TimeStamp
This commit is contained in:
Grahame Grieve 2023-07-27 22:00:04 +10:00 committed by GitHub
commit efa52981d5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 220 additions and 168 deletions

View File

@ -1,33 +1,33 @@
package org.hl7.fhir.utilities; package org.hl7.fhir.utilities;
/* /*
Copyright (c) 2011+, HL7, Inc. Copyright (c) 2011+, HL7, Inc.
All rights reserved. All rights reserved.
Redistribution and use in source and binary forms, with or without modification, Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met: are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this * Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer. list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, * Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution. and/or other materials provided with the distribution.
* Neither the name of HL7 nor the names of its contributors may be used to * Neither the name of HL7 nor the names of its contributors may be used to
endorse or promote products derived from this software without specific endorse or promote products derived from this software without specific
prior written permission. prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE. POSSIBILITY OF SUCH DAMAGE.
*/ */
@ -43,18 +43,14 @@ import java.io.OutputStream;
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
import java.io.Reader; import java.io.Reader;
import java.io.Writer; import java.io.Writer;
import java.sql.Timestamp;
import java.text.DateFormat; import java.text.DateFormat;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Collections; import java.time.ZonedDateTime;
import java.util.Date; import java.time.ZoneId;
import java.util.Iterator; import java.time.format.DateTimeFormatter;
import java.util.LinkedHashMap; import java.util.*;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Properties;
import java.util.Set;
/** /**
@ -74,34 +70,34 @@ public final class IniFile
/** Variable to denote the successful load operation. */ /** Variable to denote the successful load operation. */
@SuppressWarnings("unused") @SuppressWarnings("unused")
private boolean mblnLoaded = false; private boolean fileSuccessfullyLoaded = false;
/** Variable to hold the ini file name and full path */ /** Variable to hold the ini file name and full path */
private String mstrFile; private String filepath;
/** Variable to hold the sections in an ini file. */ /** Variable to hold the sections in an ini file. */
private LinkedHashMap<String, INISection> mhmapSections; private LinkedHashMap<String, INISection> iniSections;
/** Variable to hold environment variables **/ /** Variable to hold environment variables **/
private Properties mpropEnv; private Properties mpropEnv;
/** /**
* Create a IniFile object from the file named in the parameter. * Create a IniFile object from the file named in the parameter.
* @param pstrPathAndName The full path and name of the ini file to be used. * @param pathname The full path and name of the ini file to be used.
*/ */
public IniFile(String pstrPathAndName) public IniFile(String pathname)
{ {
this.mpropEnv = getEnvVars(); this.mpropEnv = getEnvVars();
this.mhmapSections = new LinkedHashMap<String, INISection>(); this.iniSections = new LinkedHashMap<String, INISection>();
this.mstrFile = pstrPathAndName; this.filepath = pathname;
// Load the specified INI file. // Load the specified INI file.
if (checkFile(pstrPathAndName)) loadFile(); if (checkFile(pathname)) loadFile();
} }
public IniFile(InputStream stream) { public IniFile(InputStream stream) {
this.mpropEnv = getEnvVars(); this.mpropEnv = getEnvVars();
this.mhmapSections = new LinkedHashMap<String, INISection>(); this.iniSections = new LinkedHashMap<String, INISection>();
this.mstrFile = null; this.filepath = null;
// Load the specified INI file. // Load the specified INI file.
loadStream(stream); loadStream(stream);
} }
@ -115,7 +111,7 @@ public final class IniFile
*/ */
public String getFileName() public String getFileName()
{ {
return this.mstrFile; return this.filepath;
} }
/** /**
@ -130,7 +126,7 @@ public final class IniFile
INIProperty objProp = null; INIProperty objProp = null;
INISection objSec = null; INISection objSec = null;
objSec = (INISection) this.mhmapSections.get(pstrSection); objSec = (INISection) this.iniSections.get(pstrSection);
if (objSec != null) if (objSec != null)
{ {
objProp = objSec.getProperty(pstrProp); objProp = objSec.getProperty(pstrProp);
@ -166,7 +162,7 @@ public final class IniFile
INIProperty objProp = null; INIProperty objProp = null;
INISection objSec = null; INISection objSec = null;
objSec = (INISection) this.mhmapSections.get(pstrSection); objSec = (INISection) this.iniSections.get(pstrSection);
if (objSec != null) if (objSec != null)
{ {
objProp = objSec.getProperty(pstrProp); objProp = objSec.getProperty(pstrProp);
@ -198,7 +194,7 @@ public final class IniFile
INIProperty objProp = null; INIProperty objProp = null;
INISection objSec = null; INISection objSec = null;
objSec = (INISection) this.mhmapSections.get(pstrSection); objSec = (INISection) this.iniSections.get(pstrSection);
if (objSec != null) if (objSec != null)
{ {
objProp = objSec.getProperty(pstrProp); objProp = objSec.getProperty(pstrProp);
@ -235,7 +231,7 @@ public final class IniFile
INIProperty objProp = null; INIProperty objProp = null;
INISection objSec = null; INISection objSec = null;
objSec = (INISection) this.mhmapSections.get(pstrSection); objSec = (INISection) this.iniSections.get(pstrSection);
if (objSec != null) if (objSec != null)
{ {
objProp = objSec.getProperty(pstrProp); objProp = objSec.getProperty(pstrProp);
@ -272,7 +268,7 @@ public final class IniFile
INIProperty objProp = null; INIProperty objProp = null;
INISection objSec = null; INISection objSec = null;
objSec = (INISection) this.mhmapSections.get(pstrSection); objSec = (INISection) this.iniSections.get(pstrSection);
if (objSec != null) if (objSec != null)
{ {
objProp = objSec.getProperty(pstrProp); objProp = objSec.getProperty(pstrProp);
@ -310,7 +306,7 @@ public final class IniFile
INIProperty objProp = null; INIProperty objProp = null;
INISection objSec = null; INISection objSec = null;
objSec = (INISection) this.mhmapSections.get(pstrSection); objSec = (INISection) this.iniSections.get(pstrSection);
if (objSec != null) if (objSec != null)
{ {
objProp = objSec.getProperty(pstrProp); objProp = objSec.getProperty(pstrProp);
@ -340,46 +336,30 @@ public final class IniFile
/** /**
* Returns the specified date property from the specified section. * Returns the specified date property from the specified section.
* @param pstrSection the INI section name. * @param iniSectionName the INI section name.
* @param pstrProp the property to be retrieved. * @param propertyKey the property to be retrieved.
* @return the date property value. * @return the date property value.
*/ */
public Date getTimestampProperty(String pstrSection, String pstrProp) public Date getTimestampProperty(String iniSectionName, String propertyKey) {
{
Timestamp tsRet = null;
Date dtTmp = null;
String strVal = null;
DateFormat dtFmt = null;
INIProperty objProp = null;
INISection objSec = null;
objSec = (INISection) this.mhmapSections.get(pstrSection); Date dateToReturn = null;
if (objSec != null)
{ INISection iniSection = this.iniSections.get(iniSectionName);
objProp = objSec.getProperty(pstrProp); if (iniSection != null) {
try INIProperty objProp = iniSection.getProperty(propertyKey);
{ try {
if (objProp != null) strVal = objProp.getPropValue(); String propertyStringValue = null;
if (strVal != null) if (objProp != null) propertyStringValue = objProp.getPropValue();
{ if (propertyStringValue != null) {
dtFmt = new SimpleDateFormat(this.mstrDateFmt); DateTimeFormatter dtFmt = DateTimeFormatter.ofPattern(this.mstrTimeStampFmt).withZone(ZoneId.systemDefault());
dtTmp = dtFmt.parse(strVal);
tsRet = new Timestamp(dtTmp.getTime()); ZonedDateTime zonedDateTime = ZonedDateTime.parse(propertyStringValue, dtFmt);
} dateToReturn = Date.from(zonedDateTime.toInstant());
} }
catch (ParseException PExIgnore) } catch (IllegalArgumentException IAEx) {
{
}
catch (IllegalArgumentException IAEx)
{
}
finally
{
if (objProp != null) objProp = null;
}
objSec = null;
} }
return tsRet; }
return dateToReturn;
} }
/*------------------------------------------------------------------------------ /*------------------------------------------------------------------------------
@ -394,11 +374,11 @@ public final class IniFile
{ {
INISection objSec = null; INISection objSec = null;
objSec = (INISection) this.mhmapSections.get(pstrSection); objSec = (INISection) this.iniSections.get(pstrSection);
if (objSec == null) if (objSec == null)
{ {
objSec = new INISection(pstrSection); objSec = new INISection(pstrSection);
this.mhmapSections.put(pstrSection, objSec); this.iniSections.put(pstrSection, objSec);
} }
objSec.setSecComments(delRemChars(pstrComments)); objSec.setSecComments(delRemChars(pstrComments));
objSec = null; objSec = null;
@ -415,11 +395,11 @@ public final class IniFile
{ {
INISection objSec = null; INISection objSec = null;
objSec = (INISection) this.mhmapSections.get(pstrSection); objSec = (INISection) this.iniSections.get(pstrSection);
if (objSec == null) if (objSec == null)
{ {
objSec = new INISection(pstrSection); objSec = new INISection(pstrSection);
this.mhmapSections.put(pstrSection, objSec); this.iniSections.put(pstrSection, objSec);
} }
objSec.setProperty(pstrProp, pstrVal, pstrComments); objSec.setProperty(pstrProp, pstrVal, pstrComments);
} }
@ -435,11 +415,11 @@ public final class IniFile
{ {
INISection objSec = null; INISection objSec = null;
objSec = (INISection) this.mhmapSections.get(pstrSection); objSec = (INISection) this.iniSections.get(pstrSection);
if (objSec == null) if (objSec == null)
{ {
objSec = new INISection(pstrSection); objSec = new INISection(pstrSection);
this.mhmapSections.put(pstrSection, objSec); this.iniSections.put(pstrSection, objSec);
} }
if (pblnVal) if (pblnVal)
objSec.setProperty(pstrProp, "TRUE", pstrComments); objSec.setProperty(pstrProp, "TRUE", pstrComments);
@ -458,11 +438,11 @@ public final class IniFile
{ {
INISection objSec = null; INISection objSec = null;
objSec = (INISection) this.mhmapSections.get(pstrSection); objSec = (INISection) this.iniSections.get(pstrSection);
if (objSec == null) if (objSec == null)
{ {
objSec = new INISection(pstrSection); objSec = new INISection(pstrSection);
this.mhmapSections.put(pstrSection, objSec); this.iniSections.put(pstrSection, objSec);
} }
objSec.setProperty(pstrProp, Integer.toString(pintVal), pstrComments); objSec.setProperty(pstrProp, Integer.toString(pintVal), pstrComments);
} }
@ -478,11 +458,11 @@ public final class IniFile
{ {
INISection objSec = null; INISection objSec = null;
objSec = (INISection) this.mhmapSections.get(pstrSection); objSec = (INISection) this.iniSections.get(pstrSection);
if (objSec == null) if (objSec == null)
{ {
objSec = new INISection(pstrSection); objSec = new INISection(pstrSection);
this.mhmapSections.put(pstrSection, objSec); this.iniSections.put(pstrSection, objSec);
} }
objSec.setProperty(pstrProp, Long.toString(plngVal), pstrComments); objSec.setProperty(pstrProp, Long.toString(plngVal), pstrComments);
} }
@ -498,11 +478,11 @@ public final class IniFile
{ {
INISection objSec = null; INISection objSec = null;
objSec = (INISection) this.mhmapSections.get(pstrSection); objSec = (INISection) this.iniSections.get(pstrSection);
if (objSec == null) if (objSec == null)
{ {
objSec = new INISection(pstrSection); objSec = new INISection(pstrSection);
this.mhmapSections.put(pstrSection, objSec); this.iniSections.put(pstrSection, objSec);
} }
objSec.setProperty(pstrProp, Double.toString(pdblVal), pstrComments); objSec.setProperty(pstrProp, Double.toString(pdblVal), pstrComments);
} }
@ -518,11 +498,11 @@ public final class IniFile
{ {
INISection objSec = null; INISection objSec = null;
objSec = (INISection) this.mhmapSections.get(pstrSection); objSec = (INISection) this.iniSections.get(pstrSection);
if (objSec == null) if (objSec == null)
{ {
objSec = new INISection(pstrSection); objSec = new INISection(pstrSection);
this.mhmapSections.put(pstrSection, objSec); this.iniSections.put(pstrSection, objSec);
} }
objSec.setProperty(pstrProp, utilDateToStr(pdtVal, this.mstrDateFmt), objSec.setProperty(pstrProp, utilDateToStr(pdtVal, this.mstrDateFmt),
pstrComments); pstrComments);
@ -530,27 +510,27 @@ public final class IniFile
/** /**
* Sets the specified java.sql.Timestamp property. * Sets the specified java.sql.Timestamp property.
* @param pstrSection the INI section name. * @param iniSection the INI section name.
* @param pstrProp the property to be set. * @param propertyKey the property to be set.
* @param ptsVal the timestamp value to be persisted. * @param propertyValue the timestamp value to be persisted.
*/ */
public void setTimestampProperty(String pstrSection, String pstrProp, public void setTimestampProperty(String iniSection, String propertyKey,
Timestamp ptsVal, String pstrComments) ZonedDateTime propertyValue, String propertyComments)
{ {
INISection objSec = null; INISection objSec = null;
objSec = (INISection) this.mhmapSections.get(pstrSection); objSec = (INISection) this.iniSections.get(iniSection);
if (objSec == null) if (objSec == null)
{ {
objSec = new INISection(pstrSection); objSec = new INISection(iniSection);
this.mhmapSections.put(pstrSection, objSec); this.iniSections.put(iniSection, objSec);
} }
objSec.setProperty(pstrProp, timeToStr(ptsVal, this.mstrTimeStampFmt), objSec.setProperty(propertyKey, timeToString(propertyValue, this.mstrTimeStampFmt),
pstrComments); propertyComments);
} }
/** /**
* Sets the format to be used to interpreat date values. * Sets the format to be used to interpret date values.
* @param pstrDtFmt the format string * @param pstrDtFmt the format string
* @throws IllegalArgumentException if the if the given pattern is invalid * @throws IllegalArgumentException if the if the given pattern is invalid
*/ */
@ -578,7 +558,7 @@ public final class IniFile
------------------------------------------------------------------------------*/ ------------------------------------------------------------------------------*/
public int getTotalSections() public int getTotalSections()
{ {
return this.mhmapSections.size(); return this.iniSections.size();
} }
/** /**
@ -593,10 +573,10 @@ public final class IniFile
try try
{ {
if (this.mhmapSections.size() > 0) if (this.iniSections.size() > 0)
{ {
arrRet = new String[this.mhmapSections.size()]; arrRet = new String[this.iniSections.size()];
for (iter = this.mhmapSections.keySet().iterator();;iter.hasNext()) for (iter = this.iniSections.keySet().iterator();; iter.hasNext())
{ {
arrRet[iCntr] = (String) iter.next(); arrRet[iCntr] = (String) iter.next();
iCntr++; iCntr++;
@ -623,7 +603,7 @@ public final class IniFile
String[] arrRet = null; String[] arrRet = null;
INISection objSec = null; INISection objSec = null;
objSec = (INISection) this.mhmapSections.get(pstrSection); objSec = (INISection) this.iniSections.get(pstrSection);
if (objSec != null) if (objSec != null)
{ {
arrRet = objSec.getPropNames(); arrRet = objSec.getPropNames();
@ -642,7 +622,7 @@ public final class IniFile
Map<String, INIProperty> hmRet = null; Map<String, INIProperty> hmRet = null;
INISection objSec = null; INISection objSec = null;
objSec = (INISection) this.mhmapSections.get(pstrSection); objSec = (INISection) this.iniSections.get(pstrSection);
if (objSec != null) if (objSec != null)
{ {
hmRet = objSec.getProperties(); hmRet = objSec.getProperties();
@ -661,7 +641,7 @@ public final class IniFile
{ {
INISection objSec = null; INISection objSec = null;
objSec = (INISection) this.mhmapSections.get(pstrSection); objSec = (INISection) this.iniSections.get(pstrSection);
if (objSec != null) if (objSec != null)
{ {
objSec.removeProperty(pstrProp); objSec.removeProperty(pstrProp);
@ -675,8 +655,8 @@ public final class IniFile
*/ */
public void removeSection(String pstrSection) public void removeSection(String pstrSection)
{ {
if (this.mhmapSections.containsKey(pstrSection)) if (this.iniSections.containsKey(pstrSection))
this.mhmapSections.remove(pstrSection); this.iniSections.remove(pstrSection);
} }
/** /**
@ -696,15 +676,15 @@ public final class IniFile
try try
{ {
if (this.mhmapSections.size() == 0) return false; if (this.iniSections.size() == 0) return false;
objFile = new CSFile(this.mstrFile); objFile = new CSFile(this.filepath);
if (objFile.exists()) objFile.delete(); if (objFile.exists()) objFile.delete();
objWriter = new FileWriter(objFile); objWriter = new FileWriter(objFile);
itrSec = this.mhmapSections.keySet().iterator(); itrSec = this.iniSections.keySet().iterator();
while (itrSec.hasNext()) while (itrSec.hasNext())
{ {
strName = (String) itrSec.next(); strName = (String) itrSec.next();
objSec = (INISection) this.mhmapSections.get(strName); objSec = (INISection) this.iniSections.get(strName);
strTemp = objSec.toString(); strTemp = objSec.toString();
objWriter.write(strTemp); objWriter.write(strTemp);
objWriter.write("\r\n"); objWriter.write("\r\n");
@ -739,13 +719,13 @@ public final class IniFile
try try
{ {
if (this.mhmapSections.size() == 0) return false; if (this.iniSections.size() == 0) return false;
objWriter = new OutputStreamWriter(stream, "UTF-8"); objWriter = new OutputStreamWriter(stream, "UTF-8");
itrSec = this.mhmapSections.keySet().iterator(); itrSec = this.iniSections.keySet().iterator();
while (itrSec.hasNext()) while (itrSec.hasNext())
{ {
strName = (String) itrSec.next(); strName = (String) itrSec.next();
objSec = (INISection) this.mhmapSections.get(strName); objSec = (INISection) this.iniSections.get(strName);
strTemp = objSec.toString(); strTemp = objSec.toString();
objWriter.write(strTemp); objWriter.write(strTemp);
objWriter.write("\r\n"); objWriter.write("\r\n");
@ -891,7 +871,7 @@ public final class IniFile
{ {
// Section start reached create new section // Section start reached create new section
if (objSec != null) if (objSec != null)
this.mhmapSections.put(strSection.trim(), objSec); this.iniSections.put(strSection.trim(), objSec);
objSec = null; objSec = null;
strSection = strLine.substring(1, strLine.length() - 1); strSection = strLine.substring(1, strLine.length() - 1);
objSec = new INISection(strSection.trim(), strRemarks); objSec = new INISection(strSection.trim(), strRemarks);
@ -912,22 +892,22 @@ public final class IniFile
} }
} }
if (objSec != null) if (objSec != null)
this.mhmapSections.put(strSection.trim(), objSec); this.iniSections.put(strSection.trim(), objSec);
this.mblnLoaded = true; this.fileSuccessfullyLoaded = true;
} }
} }
} }
catch (FileNotFoundException FNFExIgnore) catch (FileNotFoundException FNFExIgnore)
{ {
this.mhmapSections.clear(); this.iniSections.clear();
} }
catch (IOException IOExIgnore) catch (IOException IOExIgnore)
{ {
this.mhmapSections.clear(); this.iniSections.clear();
} }
catch (NullPointerException NPExIgnore) catch (NullPointerException NPExIgnore)
{ {
this.mhmapSections.clear(); this.iniSections.clear();
} }
finally finally
{ {
@ -961,7 +941,7 @@ public final class IniFile
try try
{ {
objFRdr = new FileReader(this.mstrFile); objFRdr = new FileReader(this.filepath);
if (objFRdr != null) if (objFRdr != null)
{ {
objBRdr = new BufferedReader(objFRdr); objBRdr = new BufferedReader(objFRdr);
@ -991,7 +971,7 @@ public final class IniFile
{ {
// Section start reached create new section // Section start reached create new section
if (objSec != null) if (objSec != null)
this.mhmapSections.put(strSection.trim(), objSec); this.iniSections.put(strSection.trim(), objSec);
objSec = null; objSec = null;
strSection = strLine.substring(1, strLine.length() - 1); strSection = strLine.substring(1, strLine.length() - 1);
objSec = new INISection(strSection.trim(), strRemarks); objSec = new INISection(strSection.trim(), strRemarks);
@ -1012,22 +992,22 @@ public final class IniFile
} }
} }
if (objSec != null) if (objSec != null)
this.mhmapSections.put(strSection.trim(), objSec); this.iniSections.put(strSection.trim(), objSec);
this.mblnLoaded = true; this.fileSuccessfullyLoaded = true;
} }
} }
} }
catch (FileNotFoundException FNFExIgnore) catch (FileNotFoundException FNFExIgnore)
{ {
this.mhmapSections.clear(); this.iniSections.clear();
} }
catch (IOException IOExIgnore) catch (IOException IOExIgnore)
{ {
this.mhmapSections.clear(); this.iniSections.clear();
} }
catch (NullPointerException NPExIgnore) catch (NullPointerException NPExIgnore)
{ {
this.mhmapSections.clear(); this.iniSections.clear();
} }
finally finally
{ {
@ -1080,17 +1060,17 @@ public final class IniFile
/** /**
* Helper method to check the existance of a file. * Helper method to check the existance of a file.
* @param the full path and name of the file to be checked. * @param pathName the full path and name of the file to be checked.
* @return true if file exists, false otherwise. * @return true if file exists, false otherwise.
*/ */
private boolean checkFile(String pstrFile) private boolean checkFile(String pathName)
{ {
boolean blnRet = false; boolean blnRet = false;
File objFile = null; File objFile = null;
try try
{ {
objFile = new CSFile(pstrFile); objFile = new CSFile(pathName);
blnRet = (objFile.exists() && objFile.isFile()); blnRet = (objFile.exists() && objFile.isFile());
} }
catch (Exception e) catch (Exception e)
@ -1106,19 +1086,19 @@ public final class IniFile
/** /**
* Converts a java.util.date into String * Converts a java.util.date into String
* @param pd Date that need to be converted to String * @param date Date to be converted to String
* @param pstrFmt The date format pattern. * @param dateFormat The date format pattern.
* @return String * @return String
*/ */
private String utilDateToStr(Date pdt, String pstrFmt) private String utilDateToStr(Date date, String dateFormat)
{ {
String strRet = null; String strRet = null;
SimpleDateFormat dtFmt = null; SimpleDateFormat dtFmt = null;
try try
{ {
dtFmt = new SimpleDateFormat(pstrFmt); dtFmt = new SimpleDateFormat(dateFormat);
strRet = dtFmt.format(pdt); strRet = dtFmt.format(date);
} }
catch (Exception e) catch (Exception e)
{ {
@ -1135,34 +1115,33 @@ public final class IniFile
* Converts the given sql timestamp object to a string representation. The format * Converts the given sql timestamp object to a string representation. The format
* to be used is to be obtained from the configuration file. * to be used is to be obtained from the configuration file.
* *
* @param pobjTS the sql timestamp object to be converted. * @param zonedDateTime the sql timestamp object to be converted.
* @param pblnGMT If true formats the string using GMT timezone * @param timeFormat the time format used to store the zonedDateTime value.
* otherwise using local timezone.
* @return the formatted string representation of the timestamp. * @return the formatted string representation of the timestamp.
*/ */
private String timeToStr(Timestamp pobjTS, String pstrFmt) private String timeToString(ZonedDateTime zonedDateTime, String timeFormat)
{ {
String strRet = null; String stringValue = null;
SimpleDateFormat dtFmt = null; DateTimeFormatter dateTimeFormatter = null;
try try
{ {
dtFmt = new SimpleDateFormat(pstrFmt); dateTimeFormatter = DateTimeFormatter.ofPattern(timeFormat);
strRet = dtFmt.format(pobjTS); stringValue = dateTimeFormatter.format(zonedDateTime);
} }
catch (IllegalArgumentException iae) catch (IllegalArgumentException iae)
{ {
strRet = ""; stringValue = "";
} }
catch (NullPointerException npe) catch (NullPointerException npe)
{ {
strRet = ""; stringValue = "";
} }
finally finally
{ {
if (dtFmt != null) dtFmt = null; if (dateTimeFormatter != null) dateTimeFormatter = null;
} }
return strRet; return stringValue;
} }
/** /**

View File

@ -9,10 +9,11 @@ import java.io.InputStream;
import java.io.RandomAccessFile; import java.io.RandomAccessFile;
import java.nio.channels.FileChannel; import java.nio.channels.FileChannel;
import java.nio.channels.FileLock; import java.nio.channels.FileLock;
import java.sql.Timestamp;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.ZonedDateTime;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
@ -88,6 +89,8 @@ import org.slf4j.LoggerFactory;
public class FilesystemPackageCacheManager extends BasePackageCacheManager implements IPackageCacheManager { public class FilesystemPackageCacheManager extends BasePackageCacheManager implements IPackageCacheManager {
public static final String INI_TIMESTAMP_FORMAT = "yyyyMMddHHmmss";
public enum FilesystemPackageCacheMode { public enum FilesystemPackageCacheMode {
USER, SYSTEM, TESTING, CUSTOM USER, SYSTEM, TESTING, CUSTOM
} }
@ -486,8 +489,8 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple
Utilities.renameDirectory(tempDir, packRoot); Utilities.renameDirectory(tempDir, packRoot);
IniFile ini = new IniFile(Utilities.path(cacheFolder, "packages.ini")); IniFile ini = new IniFile(Utilities.path(cacheFolder, "packages.ini"));
ini.setTimeStampFormat("yyyyMMddhhmmss"); ini.setTimeStampFormat(INI_TIMESTAMP_FORMAT);
ini.setTimestampProperty("packages", id + "#" + v, Timestamp.from(Instant.now()), null); ini.setTimestampProperty("packages", id + "#" + v, ZonedDateTime.now(), null);
ini.setIntegerProperty("package-sizes", id + "#" + v, npm.getSize(), null); ini.setIntegerProperty("package-sizes", id + "#" + v, npm.getSize(), null);
ini.save(); ini.save();
if (progress) if (progress)

View File

@ -0,0 +1,70 @@
package org.hl7.fhir.utilities;
import org.junit.Test;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.text.ParseException;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class IniFileTest {
public static final String EXPECTED_TIMESTAMP_STRING = "1979-12-31 19:16:40";
public static final String DUMMY_SECTION = "dummy-section";
public static final String DUMMY_TIMESTAMP_KEY = "dummyTimestamp";
public static final String DUMMY_TIMESTAMP_COMMENT = "dummy comment";
public static final String INTEGER_ONLY_DATETIME_STRING = "19791231191640";
public static final String INTEGER_ONLY_DATE_TIME_FORMAT = "yyyyMMddHHmmss";
public ZonedDateTime getDummyInstant() throws ParseException {
DateTimeFormatter dtFmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").withZone(ZoneId.systemDefault());
return ZonedDateTime.parse(EXPECTED_TIMESTAMP_STRING,dtFmt);
}
@Test
public void testZonedDateTime() {
DateTimeFormatter dtFmt = DateTimeFormatter.ofPattern(INTEGER_ONLY_DATE_TIME_FORMAT);
LocalDateTime dateTime = LocalDateTime.parse(INTEGER_ONLY_DATETIME_STRING,dtFmt);
}
@Test
public void testSetTimestampPropertyDefaultFormat() throws IOException, ParseException {
Path testIni = Files.createTempFile("testIni", ".ini");
IniFile iniFile = new IniFile(testIni.toAbsolutePath().toString());
ZonedDateTime timestamp = getDummyInstant();
iniFile.setTimestampProperty(DUMMY_SECTION, DUMMY_TIMESTAMP_KEY, timestamp, DUMMY_TIMESTAMP_COMMENT);
Date date = iniFile.getTimestampProperty(DUMMY_SECTION, DUMMY_TIMESTAMP_KEY);
assertEquals(EXPECTED_TIMESTAMP_STRING, iniFile.getStringProperty(DUMMY_SECTION, DUMMY_TIMESTAMP_KEY));
assertEquals(Date.from(timestamp.toInstant()).getTime(), date.getTime());
}
@Test
public void voidTestSetTimeStampPropertyExplicitFormat() throws IOException, ParseException {
Path testIni = Files.createTempFile("testIni", ".ini");
IniFile iniFile = new IniFile(testIni.toAbsolutePath().toString());
ZonedDateTime timestamp = getDummyInstant();
iniFile.setTimeStampFormat(INTEGER_ONLY_DATE_TIME_FORMAT);
iniFile.setTimestampProperty(DUMMY_SECTION, DUMMY_TIMESTAMP_KEY, timestamp, DUMMY_TIMESTAMP_COMMENT);
Date date = iniFile.getTimestampProperty(DUMMY_SECTION, DUMMY_TIMESTAMP_KEY);
assertEquals(INTEGER_ONLY_DATETIME_STRING, iniFile.getStringProperty(DUMMY_SECTION, DUMMY_TIMESTAMP_KEY));
assertEquals(Date.from(timestamp.toInstant()).getTime(), date.getTime());
}
}