Initial commit
This commit is contained in:
48
MDM-LCAG-JavaFunctions/.classpath
Normal file
48
MDM-LCAG-JavaFunctions/.classpath
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<classpath>
|
||||||
|
<classpathentry including="**/*.java" kind="src" output="target/classes" path="src/main/java">
|
||||||
|
<attributes>
|
||||||
|
<attribute name="optional" value="true"/>
|
||||||
|
<attribute name="maven.pomderived" value="true"/>
|
||||||
|
</attributes>
|
||||||
|
</classpathentry>
|
||||||
|
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
|
||||||
|
<attributes>
|
||||||
|
<attribute name="test" value="true"/>
|
||||||
|
<attribute name="optional" value="true"/>
|
||||||
|
<attribute name="maven.pomderived" value="true"/>
|
||||||
|
</attributes>
|
||||||
|
</classpathentry>
|
||||||
|
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
|
||||||
|
<attributes>
|
||||||
|
<attribute name="test" value="true"/>
|
||||||
|
<attribute name="maven.pomderived" value="true"/>
|
||||||
|
<attribute name="optional" value="true"/>
|
||||||
|
</attributes>
|
||||||
|
</classpathentry>
|
||||||
|
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-17">
|
||||||
|
<attributes>
|
||||||
|
<attribute name="maven.pomderived" value="true"/>
|
||||||
|
</attributes>
|
||||||
|
</classpathentry>
|
||||||
|
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/5"/>
|
||||||
|
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
|
||||||
|
<attributes>
|
||||||
|
<attribute name="maven.pomderived" value="true"/>
|
||||||
|
</attributes>
|
||||||
|
</classpathentry>
|
||||||
|
<classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.module.container"/>
|
||||||
|
<classpathentry kind="lib" path="lib/postgresql.jar"/>
|
||||||
|
<classpathentry kind="src" path="target/generated-sources/annotations">
|
||||||
|
<attributes>
|
||||||
|
<attribute name="optional" value="true"/>
|
||||||
|
</attributes>
|
||||||
|
</classpathentry>
|
||||||
|
<classpathentry kind="src" output="target/test-classes" path="target/generated-test-sources/test-annotations">
|
||||||
|
<attributes>
|
||||||
|
<attribute name="optional" value="true"/>
|
||||||
|
<attribute name="test" value="true"/>
|
||||||
|
</attributes>
|
||||||
|
</classpathentry>
|
||||||
|
<classpathentry kind="output" path="target/classes"/>
|
||||||
|
</classpath>
|
||||||
31
MDM-LCAG-JavaFunctions/.factorypath
Normal file
31
MDM-LCAG-JavaFunctions/.factorypath
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
<factorypath>
|
||||||
|
<factorypathentry kind="VARJAR" id="M2_REPO/org/dom4j/dom4j/2.1.4/dom4j-2.1.4.jar" enabled="true" runInBatchMode="false"/>
|
||||||
|
<factorypathentry kind="VARJAR" id="M2_REPO/commons-io/commons-io/2.16.0/commons-io-2.16.0.jar" enabled="true" runInBatchMode="false"/>
|
||||||
|
<factorypathentry kind="VARJAR" id="M2_REPO/org/json/json/20240303/json-20240303.jar" enabled="true" runInBatchMode="false"/>
|
||||||
|
<factorypathentry kind="VARJAR" id="M2_REPO/jaxen/jaxen/2.0.0/jaxen-2.0.0.jar" enabled="true" runInBatchMode="false"/>
|
||||||
|
<factorypathentry kind="VARJAR" id="M2_REPO/org/apache/logging/log4j/log4j-api/2.23.1/log4j-api-2.23.1.jar" enabled="true" runInBatchMode="false"/>
|
||||||
|
<factorypathentry kind="VARJAR" id="M2_REPO/org/apache/logging/log4j/log4j-core/2.23.1/log4j-core-2.23.1.jar" enabled="true" runInBatchMode="false"/>
|
||||||
|
<factorypathentry kind="VARJAR" id="M2_REPO/org/apache/commons/commons-lang3/3.12.0/commons-lang3-3.12.0.jar" enabled="true" runInBatchMode="false"/>
|
||||||
|
<factorypathentry kind="VARJAR" id="M2_REPO/org/apache/commons/commons-text/1.10.0/commons-text-1.10.0.jar" enabled="true" runInBatchMode="false"/>
|
||||||
|
<factorypathentry kind="VARJAR" id="M2_REPO/com/sun/xml/messaging/saaj/saaj-impl/3.0.3/saaj-impl-3.0.3.jar" enabled="true" runInBatchMode="false"/>
|
||||||
|
<factorypathentry kind="VARJAR" id="M2_REPO/jakarta/xml/soap/jakarta.xml.soap-api/3.0.1/jakarta.xml.soap-api-3.0.1.jar" enabled="true" runInBatchMode="false"/>
|
||||||
|
<factorypathentry kind="VARJAR" id="M2_REPO/org/jvnet/staxex/stax-ex/2.1.0/stax-ex-2.1.0.jar" enabled="true" runInBatchMode="false"/>
|
||||||
|
<factorypathentry kind="VARJAR" id="M2_REPO/jakarta/activation/jakarta.activation-api/2.1.2/jakarta.activation-api-2.1.2.jar" enabled="true" runInBatchMode="false"/>
|
||||||
|
<factorypathentry kind="VARJAR" id="M2_REPO/org/eclipse/angus/angus-activation/2.0.1/angus-activation-2.0.1.jar" enabled="true" runInBatchMode="false"/>
|
||||||
|
<factorypathentry kind="VARJAR" id="M2_REPO/com/jayway/jsonpath/json-path/2.9.0/json-path-2.9.0.jar" enabled="true" runInBatchMode="false"/>
|
||||||
|
<factorypathentry kind="VARJAR" id="M2_REPO/net/minidev/json-smart/2.5.0/json-smart-2.5.0.jar" enabled="true" runInBatchMode="false"/>
|
||||||
|
<factorypathentry kind="VARJAR" id="M2_REPO/net/minidev/accessors-smart/2.5.0/accessors-smart-2.5.0.jar" enabled="true" runInBatchMode="false"/>
|
||||||
|
<factorypathentry kind="VARJAR" id="M2_REPO/org/ow2/asm/asm/9.3/asm-9.3.jar" enabled="true" runInBatchMode="false"/>
|
||||||
|
<factorypathentry kind="VARJAR" id="M2_REPO/org/slf4j/slf4j-api/2.0.11/slf4j-api-2.0.11.jar" enabled="true" runInBatchMode="false"/>
|
||||||
|
<factorypathentry kind="VARJAR" id="M2_REPO/javax/xml/bind/jaxb-api-osgi/2.2.7/jaxb-api-osgi-2.2.7.jar" enabled="true" runInBatchMode="false"/>
|
||||||
|
<factorypathentry kind="VARJAR" id="M2_REPO/org/apache/poi/poi-ooxml/5.2.5/poi-ooxml-5.2.5.jar" enabled="true" runInBatchMode="false"/>
|
||||||
|
<factorypathentry kind="VARJAR" id="M2_REPO/org/apache/poi/poi/5.2.5/poi-5.2.5.jar" enabled="true" runInBatchMode="false"/>
|
||||||
|
<factorypathentry kind="VARJAR" id="M2_REPO/commons-codec/commons-codec/1.16.0/commons-codec-1.16.0.jar" enabled="true" runInBatchMode="false"/>
|
||||||
|
<factorypathentry kind="VARJAR" id="M2_REPO/org/apache/commons/commons-math3/3.6.1/commons-math3-3.6.1.jar" enabled="true" runInBatchMode="false"/>
|
||||||
|
<factorypathentry kind="VARJAR" id="M2_REPO/com/zaxxer/SparseBitSet/1.3/SparseBitSet-1.3.jar" enabled="true" runInBatchMode="false"/>
|
||||||
|
<factorypathentry kind="VARJAR" id="M2_REPO/org/apache/poi/poi-ooxml-lite/5.2.5/poi-ooxml-lite-5.2.5.jar" enabled="true" runInBatchMode="false"/>
|
||||||
|
<factorypathentry kind="VARJAR" id="M2_REPO/org/apache/xmlbeans/xmlbeans/5.2.0/xmlbeans-5.2.0.jar" enabled="true" runInBatchMode="false"/>
|
||||||
|
<factorypathentry kind="VARJAR" id="M2_REPO/org/apache/commons/commons-compress/1.25.0/commons-compress-1.25.0.jar" enabled="true" runInBatchMode="false"/>
|
||||||
|
<factorypathentry kind="VARJAR" id="M2_REPO/com/github/virtuald/curvesapi/1.08/curvesapi-1.08.jar" enabled="true" runInBatchMode="false"/>
|
||||||
|
<factorypathentry kind="VARJAR" id="M2_REPO/org/apache/commons/commons-collections4/4.4/commons-collections4-4.4.jar" enabled="true" runInBatchMode="false"/>
|
||||||
|
</factorypath>
|
||||||
7
MDM-LCAG-JavaFunctions/.gitignore
vendored
Normal file
7
MDM-LCAG-JavaFunctions/.gitignore
vendored
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
/.svn/
|
||||||
|
/target/
|
||||||
|
/bin/
|
||||||
|
**/*.class
|
||||||
|
/result.csv
|
||||||
|
/result.html
|
||||||
|
/result.xlsx
|
||||||
48
MDM-LCAG-JavaFunctions/.project
Normal file
48
MDM-LCAG-JavaFunctions/.project
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<projectDescription>
|
||||||
|
<name>MDM-LCAG-JavaFunctions</name>
|
||||||
|
<comment></comment>
|
||||||
|
<projects>
|
||||||
|
</projects>
|
||||||
|
<buildSpec>
|
||||||
|
<buildCommand>
|
||||||
|
<name>org.eclipse.wst.common.project.facet.core.builder</name>
|
||||||
|
<arguments>
|
||||||
|
</arguments>
|
||||||
|
</buildCommand>
|
||||||
|
<buildCommand>
|
||||||
|
<name>org.python.pydev.PyDevBuilder</name>
|
||||||
|
<arguments>
|
||||||
|
</arguments>
|
||||||
|
</buildCommand>
|
||||||
|
<buildCommand>
|
||||||
|
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||||
|
<arguments>
|
||||||
|
</arguments>
|
||||||
|
</buildCommand>
|
||||||
|
<buildCommand>
|
||||||
|
<name>org.eclipse.wst.validation.validationbuilder</name>
|
||||||
|
<arguments>
|
||||||
|
</arguments>
|
||||||
|
</buildCommand>
|
||||||
|
<buildCommand>
|
||||||
|
<name>org.eclipse.m2e.core.maven2Builder</name>
|
||||||
|
<arguments>
|
||||||
|
</arguments>
|
||||||
|
</buildCommand>
|
||||||
|
</buildSpec>
|
||||||
|
<natures>
|
||||||
|
<nature>org.eclipse.jem.workbench.JavaEMFNature</nature>
|
||||||
|
<nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>
|
||||||
|
<nature>org.eclipse.m2e.core.maven2Nature</nature>
|
||||||
|
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||||
|
<nature>org.python.pydev.pythonNature</nature>
|
||||||
|
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
|
||||||
|
</natures>
|
||||||
|
<variableList>
|
||||||
|
<variable>
|
||||||
|
<name>TALEND_STUDIO_HOME</name>
|
||||||
|
<value>%25USERPROFILE%25</value>
|
||||||
|
</variable>
|
||||||
|
</variableList>
|
||||||
|
</projectDescription>
|
||||||
5
MDM-LCAG-JavaFunctions/.pydevproject
Normal file
5
MDM-LCAG-JavaFunctions/.pydevproject
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||||
|
<?eclipse-pydev version="1.0"?><pydev_project>
|
||||||
|
<pydev_property name="org.python.pydev.PYTHON_PROJECT_INTERPRETER">Default</pydev_property>
|
||||||
|
<pydev_property name="org.python.pydev.PYTHON_PROJECT_VERSION">python interpreter</pydev_property>
|
||||||
|
</pydev_project>
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
eclipse.preferences.version=1
|
||||||
|
encoding//src/main/java/routines/StringUtil.java=UTF-8
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
eclipse.preferences.version=1
|
||||||
|
org.eclipse.jdt.apt.aptEnabled=true
|
||||||
|
org.eclipse.jdt.apt.genSrcDir=target\\generated-sources\\annotations
|
||||||
|
org.eclipse.jdt.apt.genTestSrcDir=target\\generated-test-sources\\test-annotations
|
||||||
17
MDM-LCAG-JavaFunctions/.settings/org.eclipse.jdt.core.prefs
Normal file
17
MDM-LCAG-JavaFunctions/.settings/org.eclipse.jdt.core.prefs
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
eclipse.preferences.version=1
|
||||||
|
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
|
||||||
|
org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate
|
||||||
|
org.eclipse.jdt.core.compiler.codegen.targetPlatform=17
|
||||||
|
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
|
||||||
|
org.eclipse.jdt.core.compiler.compliance=17
|
||||||
|
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
|
||||||
|
org.eclipse.jdt.core.compiler.debug.localVariable=generate
|
||||||
|
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
|
||||||
|
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
|
||||||
|
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
|
||||||
|
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
|
||||||
|
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
|
||||||
|
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
|
||||||
|
org.eclipse.jdt.core.compiler.processAnnotations=enabled
|
||||||
|
org.eclipse.jdt.core.compiler.release=enabled
|
||||||
|
org.eclipse.jdt.core.compiler.source=17
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?><project-modules id="moduleCoreId" project-version="1.5.0">
|
||||||
|
|
||||||
|
|
||||||
|
<wb-module deploy-name="MDM-LCAG-JavaFunctions">
|
||||||
|
|
||||||
|
|
||||||
|
<wb-resource deploy-path="/" source-path="/src/test/java"/>
|
||||||
|
|
||||||
|
|
||||||
|
<wb-resource deploy-path="/" source-path="/src/test/resources"/>
|
||||||
|
|
||||||
|
<wb-resource deploy-path="/" source-path="/src/main/java"/>
|
||||||
|
|
||||||
|
|
||||||
|
</wb-module>
|
||||||
|
|
||||||
|
|
||||||
|
</project-modules>
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<faceted-project>
|
||||||
|
<fixed facet="java"/>
|
||||||
|
<fixed facet="jst.utility"/>
|
||||||
|
<installed facet="jst.utility" version="1.0"/>
|
||||||
|
<installed facet="java" version="17"/>
|
||||||
|
</faceted-project>
|
||||||
21
MDM-LCAG-JavaFunctions/CommentExtractor.launch
Normal file
21
MDM-LCAG-JavaFunctions/CommentExtractor.launch
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||||
|
<launchConfiguration type="org.eclipse.jdt.launching.localJavaApplication">
|
||||||
|
<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_PATHS">
|
||||||
|
<listEntry value="/MDM-LCAG-JavaFunctions/src/main/java/application/CommentExtractor.java"/>
|
||||||
|
</listAttribute>
|
||||||
|
<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_TYPES">
|
||||||
|
<listEntry value="1"/>
|
||||||
|
</listAttribute>
|
||||||
|
<listAttribute key="org.eclipse.debug.ui.favoriteGroups">
|
||||||
|
<listEntry value="org.eclipse.debug.ui.launchGroup.run"/>
|
||||||
|
</listAttribute>
|
||||||
|
<booleanAttribute key="org.eclipse.jdt.launching.ATTR_ATTR_USE_ARGFILE" value="false"/>
|
||||||
|
<booleanAttribute key="org.eclipse.jdt.launching.ATTR_EXCLUDE_TEST_CODE" value="true"/>
|
||||||
|
<booleanAttribute key="org.eclipse.jdt.launching.ATTR_SHOW_CODEDETAILS_IN_EXCEPTION_MESSAGES" value="true"/>
|
||||||
|
<booleanAttribute key="org.eclipse.jdt.launching.ATTR_USE_CLASSPATH_ONLY_JAR" value="false"/>
|
||||||
|
<stringAttribute key="org.eclipse.jdt.launching.CLASSPATH_PROVIDER" value="org.eclipse.m2e.launchconfig.classpathProvider"/>
|
||||||
|
<stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value="application.CommentExtractor"/>
|
||||||
|
<stringAttribute key="org.eclipse.jdt.launching.MODULE_NAME" value="MDM-LCAG-JavaFunctions"/>
|
||||||
|
<stringAttribute key="org.eclipse.jdt.launching.PROJECT_ATTR" value="MDM-LCAG-JavaFunctions"/>
|
||||||
|
<stringAttribute key="org.eclipse.jdt.launching.SOURCE_PATH_PROVIDER" value="org.eclipse.m2e.launchconfig.sourcepathProvider"/>
|
||||||
|
</launchConfiguration>
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||||
|
<launchConfiguration type="org.eclipse.m2e.Maven2LaunchConfigurationType">
|
||||||
|
<booleanAttribute key="M2_DEBUG_OUTPUT" value="false"/>
|
||||||
|
<stringAttribute key="M2_GOALS" value="clean deploy"/>
|
||||||
|
<booleanAttribute key="M2_NON_RECURSIVE" value="false"/>
|
||||||
|
<booleanAttribute key="M2_OFFLINE" value="false"/>
|
||||||
|
<stringAttribute key="M2_PROFILES" value=""/>
|
||||||
|
<listAttribute key="M2_PROPERTIES"/>
|
||||||
|
<stringAttribute key="M2_RUNTIME" value="apache-maven"/>
|
||||||
|
<booleanAttribute key="M2_SKIP_TESTS" value="false"/>
|
||||||
|
<intAttribute key="M2_THREADS" value="1"/>
|
||||||
|
<booleanAttribute key="M2_UPDATE_SNAPSHOTS" value="false"/>
|
||||||
|
<stringAttribute key="M2_USER_SETTINGS" value=""/>
|
||||||
|
<booleanAttribute key="M2_WORKSPACE_RESOLUTION" value="false"/>
|
||||||
|
<listAttribute key="org.eclipse.debug.ui.favoriteGroups">
|
||||||
|
<listEntry value="org.eclipse.debug.ui.launchGroup.run"/>
|
||||||
|
</listAttribute>
|
||||||
|
<booleanAttribute key="org.eclipse.jdt.launching.ATTR_ATTR_USE_ARGFILE" value="false"/>
|
||||||
|
<booleanAttribute key="org.eclipse.jdt.launching.ATTR_SHOW_CODEDETAILS_IN_EXCEPTION_MESSAGES" value="true"/>
|
||||||
|
<booleanAttribute key="org.eclipse.jdt.launching.ATTR_USE_CLASSPATH_ONLY_JAR" value="false"/>
|
||||||
|
<stringAttribute key="org.eclipse.jdt.launching.WORKING_DIRECTORY" value="${project_loc:MDM-LCAG-JavaFunctions}"/>
|
||||||
|
</launchConfiguration>
|
||||||
3
MDM-LCAG-JavaFunctions/ReadMe-Content.txt
Normal file
3
MDM-LCAG-JavaFunctions/ReadMe-Content.txt
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
|
||||||
|
Folder lib: Contains Talend libraries needed to compile.
|
||||||
|
Copy Talend routines.system from deployed MDM job, e.g. from G:\Talend\Studio651\mdm\jobox\work\MDM_load_UI_City_1.1\lib\systemRoutines.jar
|
||||||
21
MDM-LCAG-JavaFunctions/ShowDevelopmentHints.launch
Normal file
21
MDM-LCAG-JavaFunctions/ShowDevelopmentHints.launch
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||||
|
<launchConfiguration type="org.eclipse.jdt.launching.localJavaApplication">
|
||||||
|
<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_PATHS">
|
||||||
|
<listEntry value="/MDM-LCAG-JavaFunctions/src/main/java/application/ShowDevelopmentHints.java"/>
|
||||||
|
</listAttribute>
|
||||||
|
<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_TYPES">
|
||||||
|
<listEntry value="1"/>
|
||||||
|
</listAttribute>
|
||||||
|
<listAttribute key="org.eclipse.debug.ui.favoriteGroups">
|
||||||
|
<listEntry value="org.eclipse.debug.ui.launchGroup.run"/>
|
||||||
|
</listAttribute>
|
||||||
|
<booleanAttribute key="org.eclipse.jdt.launching.ATTR_ATTR_USE_ARGFILE" value="false"/>
|
||||||
|
<booleanAttribute key="org.eclipse.jdt.launching.ATTR_EXCLUDE_TEST_CODE" value="true"/>
|
||||||
|
<booleanAttribute key="org.eclipse.jdt.launching.ATTR_SHOW_CODEDETAILS_IN_EXCEPTION_MESSAGES" value="true"/>
|
||||||
|
<booleanAttribute key="org.eclipse.jdt.launching.ATTR_USE_CLASSPATH_ONLY_JAR" value="false"/>
|
||||||
|
<stringAttribute key="org.eclipse.jdt.launching.CLASSPATH_PROVIDER" value="org.eclipse.m2e.launchconfig.classpathProvider"/>
|
||||||
|
<stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value="application.ShowDevelopmentHints"/>
|
||||||
|
<stringAttribute key="org.eclipse.jdt.launching.MODULE_NAME" value="MDM-LCAG-JavaFunctions"/>
|
||||||
|
<stringAttribute key="org.eclipse.jdt.launching.PROJECT_ATTR" value="MDM-LCAG-JavaFunctions"/>
|
||||||
|
<stringAttribute key="org.eclipse.jdt.launching.SOURCE_PATH_PROVIDER" value="org.eclipse.m2e.launchconfig.sourcepathProvider"/>
|
||||||
|
</launchConfiguration>
|
||||||
BIN
MDM-LCAG-JavaFunctions/lib/postgresql.jar
Normal file
BIN
MDM-LCAG-JavaFunctions/lib/postgresql.jar
Normal file
Binary file not shown.
2121
MDM-LCAG-JavaFunctions/log.html
Normal file
2121
MDM-LCAG-JavaFunctions/log.html
Normal file
File diff suppressed because one or more lines are too long
156
MDM-LCAG-JavaFunctions/output.xml
Normal file
156
MDM-LCAG-JavaFunctions/output.xml
Normal file
@@ -0,0 +1,156 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<robot generator="Robot 3.0.4 (Python 3.7.0 on win32)" generated="20181011 14:27:47.300">
|
||||||
|
<suite id="s1" name="Samples" source="Y:\eclipse-ws\samples">
|
||||||
|
<suite id="s1-s1" name="Src" source="Y:\eclipse-ws\samples\src">
|
||||||
|
<suite id="s1-s1-s1" name="Main" source="Y:\eclipse-ws\samples\src\main">
|
||||||
|
<suite id="s1-s1-s1-s1" name="Robotframework" source="Y:\eclipse-ws\samples\src\main\robotframework">
|
||||||
|
<suite id="s1-s1-s1-s1-s1" name="Station" source="Y:\eclipse-ws\samples\src\main\robotframework\station.robot">
|
||||||
|
<kw name="Connect To Database" library="DatabaseLibrary" type="setup">
|
||||||
|
<doc>Loads the DB API 2.0 module given `dbapiModuleName` then uses it to</doc>
|
||||||
|
<arguments>
|
||||||
|
<arg>pymysql</arg>
|
||||||
|
<arg>lcag_mdm_4_dev_master</arg>
|
||||||
|
<arg>centraldata</arg>
|
||||||
|
<arg>lcagdb2000$$</arg>
|
||||||
|
<arg>mdm-app01-dev</arg>
|
||||||
|
<arg>3306</arg>
|
||||||
|
</arguments>
|
||||||
|
<msg timestamp="20181011 14:27:47.880" level="INFO">Connecting using : pymysql.connect(db=lcag_mdm_4_dev_master, user=centraldata, passwd=lcagdb2000$$, host=mdm-app01-dev, port=3306, charset=None) </msg>
|
||||||
|
<status status="PASS" starttime="20181011 14:27:47.690" endtime="20181011 14:27:47.894"></status>
|
||||||
|
</kw>
|
||||||
|
<test id="s1-s1-s1-s1-s1-t1" name="Prepare Environment for Station">
|
||||||
|
<kw name="Exported Talend Job Should Run Successfully With RC" library="mdm_common">
|
||||||
|
<doc>ruft lokal einen TalendJob aufruft und gibt die JobinstanceId zurück.</doc>
|
||||||
|
<arguments>
|
||||||
|
<arg>${ICARGO_DELETE_JOB}</arg>
|
||||||
|
<arg>0.2.0-SNAPSHOT</arg>
|
||||||
|
<arg>0</arg>
|
||||||
|
<arg>True</arg>
|
||||||
|
</arguments>
|
||||||
|
<kw name="Get Current Date" library="DateTime">
|
||||||
|
<doc>Returns current local or UTC time with an optional increment.</doc>
|
||||||
|
<assign>
|
||||||
|
<var>${timestamp}</var>
|
||||||
|
</assign>
|
||||||
|
<msg timestamp="20181011 14:27:47.898" level="INFO">${timestamp} = 2018-10-11 14:27:47.897</msg>
|
||||||
|
<status status="PASS" starttime="20181011 14:27:47.897" endtime="20181011 14:27:47.898"></status>
|
||||||
|
</kw>
|
||||||
|
<kw name="Log To Console" library="BuiltIn">
|
||||||
|
<doc>Logs the given message to the console.</doc>
|
||||||
|
<arguments>
|
||||||
|
<arg>\nStarting Job ${jobName} on ${timestamp} ...</arg>
|
||||||
|
</arguments>
|
||||||
|
<status status="PASS" starttime="20181011 14:27:47.898" endtime="20181011 14:27:47.898"></status>
|
||||||
|
</kw>
|
||||||
|
<kw name="Run And Return Rc And Output" library="OperatingSystem">
|
||||||
|
<doc>Runs the given command in the system and returns the RC and output.</doc>
|
||||||
|
<arguments>
|
||||||
|
<arg>${JOB_PATH_NEXUS}/${jobName}-${revision}/${jobName}/${jobName}_run.bat</arg>
|
||||||
|
</arguments>
|
||||||
|
<assign>
|
||||||
|
<var>${returncode}</var>
|
||||||
|
<var>${result}</var>
|
||||||
|
</assign>
|
||||||
|
<msg timestamp="20181011 14:27:47.902" level="INFO">Running command 'E:/tools/robotframework/jobs/MDM_delete_AllStation-0.2.0-SNAPSHOT/MDM_delete_AllStation/MDM_delete_AllStation_run.bat 2>&1'.</msg>
|
||||||
|
<msg timestamp="20181011 14:27:47.910" level="INFO">${returncode} = 1</msg>
|
||||||
|
<msg timestamp="20181011 14:27:47.911" level="INFO">${result} = The system cannot find the drive specified.</msg>
|
||||||
|
<status status="PASS" starttime="20181011 14:27:47.899" endtime="20181011 14:27:47.911"></status>
|
||||||
|
</kw>
|
||||||
|
<kw name="Run Keyword If" library="BuiltIn">
|
||||||
|
<doc>Runs the given keyword with the given arguments, if ``condition`` is true.</doc>
|
||||||
|
<arguments>
|
||||||
|
<arg>${showLog}</arg>
|
||||||
|
<arg>Log To Console</arg>
|
||||||
|
<arg>>>>>>>>>>>>>\n${result}\n<<<<<<<<<<<<</arg>
|
||||||
|
</arguments>
|
||||||
|
<kw name="Log To Console" library="BuiltIn">
|
||||||
|
<doc>Logs the given message to the console.</doc>
|
||||||
|
<arguments>
|
||||||
|
<arg>>>>>>>>>>>>>\n${result}\n<<<<<<<<<<<<</arg>
|
||||||
|
</arguments>
|
||||||
|
<status status="PASS" starttime="20181011 14:27:47.913" endtime="20181011 14:27:47.913"></status>
|
||||||
|
</kw>
|
||||||
|
<status status="PASS" starttime="20181011 14:27:47.912" endtime="20181011 14:27:47.914"></status>
|
||||||
|
</kw>
|
||||||
|
<kw name="Should Be Equal As Integers" library="BuiltIn">
|
||||||
|
<doc>Fails if objects are unequal after converting them to integers.</doc>
|
||||||
|
<arguments>
|
||||||
|
<arg>${returncode}</arg>
|
||||||
|
<arg>${rc}</arg>
|
||||||
|
<arg>Job was not running successfully, finished with returncode ${returncode}</arg>
|
||||||
|
</arguments>
|
||||||
|
<msg timestamp="20181011 14:27:47.915" level="INFO">Argument types are:
|
||||||
|
<class 'int'>
|
||||||
|
<type 'unicode'></msg>
|
||||||
|
<msg timestamp="20181011 14:27:47.915" level="FAIL">Job was not running successfully, finished with returncode 1: 1 != 0</msg>
|
||||||
|
<status status="FAIL" starttime="20181011 14:27:47.914" endtime="20181011 14:27:47.916"></status>
|
||||||
|
</kw>
|
||||||
|
<status status="FAIL" starttime="20181011 14:27:47.896" endtime="20181011 14:27:47.916"></status>
|
||||||
|
</kw>
|
||||||
|
<status status="FAIL" starttime="20181011 14:27:47.894" endtime="20181011 14:27:47.917" critical="yes">Job was not running successfully, finished with returncode 1: 1 != 0</status>
|
||||||
|
</test>
|
||||||
|
<test id="s1-s1-s1-s1-s1-t2" name="Start Load Station">
|
||||||
|
<kw name="Create Directory" library="OperatingSystem">
|
||||||
|
<doc>Creates the specified directory.</doc>
|
||||||
|
<arguments>
|
||||||
|
<arg>${INPUT_PATH}/${ICARGO_LOAD_JOB}</arg>
|
||||||
|
</arguments>
|
||||||
|
<msg timestamp="20181011 14:27:47.919" level="INFO" html="yes">Directory '<a href="file://Y:\eclipse-ws\samples\$(MDM_HOME)\data\input\MDM_ICargo_load_Station">Y:\eclipse-ws\samples\$(MDM_HOME)\data\input\MDM_ICargo_load_Station</a>' already exists.</msg>
|
||||||
|
<status status="PASS" starttime="20181011 14:27:47.918" endtime="20181011 14:27:47.920"></status>
|
||||||
|
</kw>
|
||||||
|
<kw name="Copy File" library="OperatingSystem">
|
||||||
|
<doc>Copies the source file into the destination.</doc>
|
||||||
|
<arguments>
|
||||||
|
<arg>${FILE_PATH}/ICargo_Station_11Inserts.xml</arg>
|
||||||
|
<arg>${INPUT_PATH}/${ICARGO_LOAD_JOB}/ICargo_Station.xml</arg>
|
||||||
|
</arguments>
|
||||||
|
<msg timestamp="20181011 14:27:47.922" level="FAIL">Source file 'Y:\eclipse-ws\samples\main\robotframework\files\ICargo_Station_11Inserts.xml' does not exist.</msg>
|
||||||
|
<status status="FAIL" starttime="20181011 14:27:47.920" endtime="20181011 14:27:47.922"></status>
|
||||||
|
</kw>
|
||||||
|
<status status="FAIL" starttime="20181011 14:27:47.917" endtime="20181011 14:27:47.923" critical="yes">Source file 'Y:\eclipse-ws\samples\main\robotframework\files\ICargo_Station_11Inserts.xml' does not exist.</status>
|
||||||
|
</test>
|
||||||
|
<test id="s1-s1-s1-s1-s1-t3" name="Start Update Station">
|
||||||
|
<kw name="Copy File" library="OperatingSystem">
|
||||||
|
<doc>Copies the source file into the destination.</doc>
|
||||||
|
<arguments>
|
||||||
|
<arg>${FILE_PATH}/ICargo_Station_4Updates2Changes.xml</arg>
|
||||||
|
<arg>${INPUT_PATH}/${ICARGO_LOAD_JOB}/ICargo_Station.xml</arg>
|
||||||
|
</arguments>
|
||||||
|
<msg timestamp="20181011 14:27:47.927" level="FAIL">Source file 'Y:\eclipse-ws\samples\main\robotframework\files\ICargo_Station_4Updates2Changes.xml' does not exist.</msg>
|
||||||
|
<status status="FAIL" starttime="20181011 14:27:47.925" endtime="20181011 14:27:47.927"></status>
|
||||||
|
</kw>
|
||||||
|
<status status="FAIL" starttime="20181011 14:27:47.924" endtime="20181011 14:27:47.928" critical="yes">Source file 'Y:\eclipse-ws\samples\main\robotframework\files\ICargo_Station_4Updates2Changes.xml' does not exist.</status>
|
||||||
|
</test>
|
||||||
|
<kw name="Disconnect From Database" library="DatabaseLibrary" type="teardown">
|
||||||
|
<doc>Disconnects from the database.</doc>
|
||||||
|
<msg timestamp="20181011 14:27:47.929" level="INFO">Executing : Disconnect From Database</msg>
|
||||||
|
<status status="PASS" starttime="20181011 14:27:47.929" endtime="20181011 14:27:47.930"></status>
|
||||||
|
</kw>
|
||||||
|
<status status="FAIL" starttime="20181011 14:27:47.520" endtime="20181011 14:27:47.930"></status>
|
||||||
|
</suite>
|
||||||
|
<status status="FAIL" starttime="20181011 14:27:47.517" endtime="20181011 14:27:47.933"></status>
|
||||||
|
</suite>
|
||||||
|
<status status="FAIL" starttime="20181011 14:27:47.514" endtime="20181011 14:27:47.936"></status>
|
||||||
|
</suite>
|
||||||
|
<status status="FAIL" starttime="20181011 14:27:47.510" endtime="20181011 14:27:47.939"></status>
|
||||||
|
</suite>
|
||||||
|
<status status="FAIL" starttime="20181011 14:27:47.473" endtime="20181011 14:27:47.942"></status>
|
||||||
|
</suite>
|
||||||
|
<statistics>
|
||||||
|
<total>
|
||||||
|
<stat pass="0" fail="3">Critical Tests</stat>
|
||||||
|
<stat pass="0" fail="3">All Tests</stat>
|
||||||
|
</total>
|
||||||
|
<tag>
|
||||||
|
</tag>
|
||||||
|
<suite>
|
||||||
|
<stat pass="0" fail="3" id="s1" name="Samples">Samples</stat>
|
||||||
|
<stat pass="0" fail="3" id="s1-s1" name="Src">Samples.Src</stat>
|
||||||
|
<stat pass="0" fail="3" id="s1-s1-s1" name="Main">Samples.Src.Main</stat>
|
||||||
|
<stat pass="0" fail="3" id="s1-s1-s1-s1" name="Robotframework">Samples.Src.Main.Robotframework</stat>
|
||||||
|
<stat pass="0" fail="3" id="s1-s1-s1-s1-s1" name="Station">Samples.Src.Main.Robotframework.Station</stat>
|
||||||
|
</suite>
|
||||||
|
</statistics>
|
||||||
|
<errors>
|
||||||
|
</errors>
|
||||||
|
</robot>
|
||||||
210
MDM-LCAG-JavaFunctions/pom.xml
Normal file
210
MDM-LCAG-JavaFunctions/pom.xml
Normal file
@@ -0,0 +1,210 @@
|
|||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<groupId>lcag.mdm</groupId>
|
||||||
|
<artifactId>lcag-java-utils</artifactId>
|
||||||
|
<version>4.20.0</version> <!-- When updating here also update version in pom of main lufthansa project! -->
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
<name>LCAG Java Utitlities</name>
|
||||||
|
<url>http://maven.apache.org</url>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven-deploy-plugin.version>3.1.1</maven-deploy-plugin.version>
|
||||||
|
<maven-source-plugin.version>3.3.1</maven-source-plugin.version>
|
||||||
|
|
||||||
|
<com.sun.xml.messaging.saaj.version>3.0.3</com.sun.xml.messaging.saaj.version>
|
||||||
|
<jaxen.version>2.0.0</jaxen.version>
|
||||||
|
<org.apache.commons-io.version>2.16.0</org.apache.commons-io.version>
|
||||||
|
<org.apache.commons-text.version>1.10.0</org.apache.commons-text.version> <!-- Newer than 1.10 requires newer lang3 version, only possible when EBX upgrades CS modules to newer lang3 -->
|
||||||
|
<org.apache.commons-lang3.version>3.12.0</org.apache.commons-lang3.version>
|
||||||
|
<org.apache.logging.log4j.version>2.23.1</org.apache.logging.log4j.version>
|
||||||
|
<org.apache.poi.version>5.2.5</org.apache.poi.version>
|
||||||
|
<org.dom4j.version>2.1.4</org.dom4j.version>
|
||||||
|
<org.json.version>20240303</org.json.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<sourceDirectory>src/main/java</sourceDirectory>
|
||||||
|
<testSourceDirectory>src/test/java</testSourceDirectory>
|
||||||
|
<resources>
|
||||||
|
<resource>
|
||||||
|
<directory>src/main/java</directory>
|
||||||
|
<includes>
|
||||||
|
<include>**/*.java</include>
|
||||||
|
<include>**/*.gwt.xml</include>
|
||||||
|
</includes>
|
||||||
|
</resource>
|
||||||
|
</resources>
|
||||||
|
|
||||||
|
<pluginManagement>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<version>3.13.0</version>
|
||||||
|
<configuration>
|
||||||
|
<release>17</release>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-jar-plugin</artifactId>
|
||||||
|
<version>3.3.0</version>
|
||||||
|
<configuration>
|
||||||
|
<includes>
|
||||||
|
<include>**/*</include>
|
||||||
|
</includes>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-deploy-plugin</artifactId>
|
||||||
|
<version>${maven-deploy-plugin.version}</version>
|
||||||
|
<configuration>
|
||||||
|
<skip>true</skip>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-source-plugin</artifactId>
|
||||||
|
<version>${maven-source-plugin.version}</version>
|
||||||
|
<configuration>
|
||||||
|
<skipSource>true</skipSource>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-release-plugin</artifactId>
|
||||||
|
<version>3.0.1</version>
|
||||||
|
<configuration>
|
||||||
|
<tagNameFormat>v@{project.version}</tagNameFormat>
|
||||||
|
<autoVersionSubmodules>true</autoVersionSubmodules>
|
||||||
|
<releaseProfiles>releases</releaseProfiles>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</pluginManagement>
|
||||||
|
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.sonatype.plugins</groupId>
|
||||||
|
<artifactId>nexus-staging-maven-plugin</artifactId>
|
||||||
|
<version>1.6.13</version>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<id>default-deploy</id>
|
||||||
|
<phase>deploy</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>deploy</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
<configuration>
|
||||||
|
<serverId>lcag.nexus.releases</serverId>
|
||||||
|
<nexusUrl>https://mdm-dev.lcag.int.lhgroup.de/nexus3/</nexusUrl>
|
||||||
|
<skipStaging>true</skipStaging>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
<distributionManagement>
|
||||||
|
<snapshotRepository>
|
||||||
|
<id>lcag.nexus.snapshots</id>
|
||||||
|
<url>https://mdm-dev.lcag.int.lhgroup.de/nexus3/repository/lcag-snapshots/</url>
|
||||||
|
</snapshotRepository>
|
||||||
|
<repository>
|
||||||
|
<id>lcag.nexus.releases</id>
|
||||||
|
<url>https://mdm-dev.lcag.int.lhgroup.de/nexus3/repository/lcag-releases/</url>
|
||||||
|
</repository>
|
||||||
|
</distributionManagement>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.dom4j</groupId>
|
||||||
|
<artifactId>dom4j</artifactId>
|
||||||
|
<version>${org.dom4j.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>commons-io</groupId>
|
||||||
|
<artifactId>commons-io</artifactId>
|
||||||
|
<version>${org.apache.commons-io.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.json</groupId>
|
||||||
|
<artifactId>json</artifactId>
|
||||||
|
<version>${org.json.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>jaxen</groupId>
|
||||||
|
<artifactId>jaxen</artifactId>
|
||||||
|
<version>${jaxen.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.vladsch.flexmark</groupId>
|
||||||
|
<artifactId>flexmark-all</artifactId>
|
||||||
|
<version>0.64.8</version>
|
||||||
|
</dependency>
|
||||||
|
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-api -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.logging.log4j</groupId>
|
||||||
|
<artifactId>log4j-api</artifactId>
|
||||||
|
<version>${org.apache.logging.log4j.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.logging.log4j</groupId>
|
||||||
|
<artifactId>log4j-core</artifactId>
|
||||||
|
<version>${org.apache.logging.log4j.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
|
||||||
|
<!-- To split Strings by camal case -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.commons</groupId>
|
||||||
|
<artifactId>commons-lang3</artifactId>
|
||||||
|
<version>${org.apache.commons-lang3.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-text -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.commons</groupId>
|
||||||
|
<artifactId>commons-text</artifactId>
|
||||||
|
<version>${org.apache.commons-text.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.google.guava</groupId>
|
||||||
|
<artifactId>guava</artifactId>
|
||||||
|
<version>33.4.8-jre</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.sun.xml.messaging.saaj</groupId>
|
||||||
|
<artifactId>saaj-impl</artifactId>
|
||||||
|
<version>${com.sun.xml.messaging.saaj.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<!-- https://mvnrepository.com/artifact/com.jayway.jsonpath/json-path -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.jayway.jsonpath</groupId>
|
||||||
|
<artifactId>json-path</artifactId>
|
||||||
|
<version>2.9.0</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>javax.xml.bind</groupId>
|
||||||
|
<artifactId>jaxb-api-osgi</artifactId>
|
||||||
|
<version>2.2.7</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.poi</groupId>
|
||||||
|
<artifactId>poi-ooxml</artifactId>
|
||||||
|
<version>${org.apache.poi.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.jupiter</groupId>
|
||||||
|
<artifactId>junit-jupiter-api</artifactId>
|
||||||
|
<version>5.10.2</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
</project>
|
||||||
2420
MDM-LCAG-JavaFunctions/report.html
Normal file
2420
MDM-LCAG-JavaFunctions/report.html
Normal file
File diff suppressed because one or more lines are too long
@@ -0,0 +1,296 @@
|
|||||||
|
package application;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
import com.vladsch.flexmark.html2md.converter.FlexmarkHtmlConverter;
|
||||||
|
|
||||||
|
import de.lcag.common.AbstractTable.ListColumn;
|
||||||
|
import de.lcag.common.HashTable;
|
||||||
|
import routines.LcagFileTools;
|
||||||
|
import routines.LcagLogger;
|
||||||
|
|
||||||
|
public class CommentExtractor {
|
||||||
|
private static final String HOW_TO_COMMAND = "HOWTO_";
|
||||||
|
private static final String DOCUMENT_FLOW_COMMAND = "FLOW_";
|
||||||
|
private static final String TODO_COMMAND = "TODO_";
|
||||||
|
|
||||||
|
private static final String[] COMMAD_LIST = new String[] { HOW_TO_COMMAND, DOCUMENT_FLOW_COMMAND, TODO_COMMAND };
|
||||||
|
|
||||||
|
private static final String COL_TYPE = "Type";
|
||||||
|
private static final String COL_ID = "Id";
|
||||||
|
private static final String COL_KIND = "Kind";
|
||||||
|
private static final String COL_STEP = "StepNo";
|
||||||
|
private static final String COL_DESC = "Description;style=width: 85%";
|
||||||
|
|
||||||
|
private static final String COL_FILE = "File";
|
||||||
|
private static final String COL_LINE_NO = "LineNo";
|
||||||
|
|
||||||
|
public static final String JAVA_CODE_BASE_PATH = "/workspace/ebx_lufthansa/ebx_lufthansa-lib/src/main/java/com/lufthansa/ebx";
|
||||||
|
public static final String SQL_CODE_BASE_PATH = "/workspace/ebx_lufthansa/ebx_lufthansa-resources/src/main/sql";
|
||||||
|
public static final String XSD_CODE_BASE_PATH = "/workspace/MDM-Datamodel";
|
||||||
|
public static final String JAVA_CODE_BASE_FULLPATH = "V:/EBX" + JAVA_CODE_BASE_PATH;
|
||||||
|
public static final String SQL_CODE_BASE_FULLPATH = "V:/EBX" + SQL_CODE_BASE_PATH;
|
||||||
|
public static final String XSD_CODE_BASE_FULLPATH = "V:/EBX" + XSD_CODE_BASE_PATH;
|
||||||
|
|
||||||
|
public static final String CODE_BASE_GITHUB_DIR = "https://dev.azure.com/LCAGDevOps/MDM/_git/EBX?path=%s&version=GB%s&line=%d&lineEnd=%d&lineStartColumn=1&lineEndColumn=1&lineStyle=plain&_a=contents";
|
||||||
|
public static final String DOC_FILE = "V:/EBX/workspace/ebx_lufthansa/ebx_lufthansa-lib/HowToImplement";
|
||||||
|
|
||||||
|
private static HashTable resultTable = new HashTable(COL_TYPE, COL_ID, COL_KIND, COL_STEP, COL_DESC, COL_FILE,
|
||||||
|
COL_LINE_NO);
|
||||||
|
private static String gitBranchName = "main";
|
||||||
|
|
||||||
|
private static LcagLogger logger = LcagLogger.getLogger(CommentExtractor.class);
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
String rootDirPath;
|
||||||
|
|
||||||
|
logger.setJavaLogLevel(LcagLogger.LOG_TRACE);
|
||||||
|
if (args.length < 1) {
|
||||||
|
rootDirPath = JAVA_CODE_BASE_FULLPATH;
|
||||||
|
logger.info("Using default Root directory to scan code: %s", rootDirPath);
|
||||||
|
} else {
|
||||||
|
rootDirPath = args[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
gitBranchName = getCurrentGitBranch();
|
||||||
|
} catch (Exception e1) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e1.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
resultTable.setName("MDM EBX Coding HowTo");
|
||||||
|
|
||||||
|
try {
|
||||||
|
extractCodeExamples(rootDirPath);
|
||||||
|
|
||||||
|
ListColumn sortedResults = resultTable.sort(COL_ID, COL_STEP);
|
||||||
|
|
||||||
|
String txt = resultTable.asCSV(sortedResults);
|
||||||
|
String fileName = DOC_FILE + ".csv";
|
||||||
|
LcagFileTools.writeTextFile(fileName, txt);
|
||||||
|
|
||||||
|
writeHtmlFile(sortedResults);
|
||||||
|
|
||||||
|
System.out.println(String.format("Saved Code examples to file: %s", DOC_FILE));
|
||||||
|
} catch (IOException e) {
|
||||||
|
logger.error("Error reading file: %s", e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void writeHtmlFile(ListColumn sortedResults) throws IOException {
|
||||||
|
String fileName = DOC_FILE + ".html";
|
||||||
|
|
||||||
|
for (int i = 0; i < resultTable.lenght(); i++) {
|
||||||
|
int lineNo = (int) resultTable.getCellValue(i, COL_LINE_NO);
|
||||||
|
String filePath = (String) resultTable.getCellValue(i, COL_FILE);
|
||||||
|
String gitURL = String.format(CODE_BASE_GITHUB_DIR, filePath, gitBranchName, lineNo, lineNo + 1);
|
||||||
|
String lineNoURL = String.format("<html><a href=\"%s\">%d</a></html>", gitURL, lineNo);
|
||||||
|
|
||||||
|
if (filePath.endsWith(".java")) {
|
||||||
|
filePath = filePath.substring(JAVA_CODE_BASE_PATH.length() + 1);
|
||||||
|
} else if (filePath.endsWith(".sql")) {
|
||||||
|
filePath = filePath.substring(SQL_CODE_BASE_PATH.length() + 1);
|
||||||
|
} else if (filePath.endsWith(".xsd")) {
|
||||||
|
filePath = filePath.substring(XSD_CODE_BASE_PATH.length() + 1);
|
||||||
|
}
|
||||||
|
resultTable.setCellValue(i, COL_FILE, filePath);
|
||||||
|
resultTable.setCellValue(i, COL_LINE_NO, lineNoURL);
|
||||||
|
|
||||||
|
String kind = (String) resultTable.getCellValue(i, COL_KIND);
|
||||||
|
|
||||||
|
if (kind.equals("Title")) {
|
||||||
|
String disc = String.format("<html><b>%s</b></html>", (String) resultTable.getCellValue(i, COL_DESC));
|
||||||
|
resultTable.setCellValue(i, COL_DESC, disc);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
String txt = resultTable.asHTML(sortedResults);
|
||||||
|
|
||||||
|
LcagFileTools.writeTextFile(fileName, txt);
|
||||||
|
String md = FlexmarkHtmlConverter.builder().build().convert(txt);
|
||||||
|
String mdFileName = DOC_FILE + ".md";
|
||||||
|
LcagFileTools.writeTextFile(mdFileName, md);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getCurrentGitBranch() throws IOException, InterruptedException {
|
||||||
|
Process process = Runtime.getRuntime().exec("git rev-parse --abbrev-ref HEAD");
|
||||||
|
process.waitFor();
|
||||||
|
|
||||||
|
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
|
||||||
|
|
||||||
|
return reader.readLine();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void extractCodeExamples(String pRootDirPath) throws IOException {
|
||||||
|
List<File> fileList = LcagFileTools.findFilesWithExtension(pRootDirPath, ".java");
|
||||||
|
final List<File> sqlFileList = LcagFileTools.findFilesWithExtension(SQL_CODE_BASE_FULLPATH, ".sql");
|
||||||
|
final List<File> xsdFileList = LcagFileTools.findFilesWithExtension(XSD_CODE_BASE_FULLPATH, ".xsd");
|
||||||
|
|
||||||
|
fileList.addAll(xsdFileList);
|
||||||
|
fileList.addAll(sqlFileList);
|
||||||
|
|
||||||
|
for (File javaFile : fileList) {
|
||||||
|
String filename = javaFile.getName();
|
||||||
|
BufferedReader reader = new BufferedReader(new FileReader(javaFile));
|
||||||
|
StringBuilder currentComment = new StringBuilder();
|
||||||
|
boolean inMultilineComment = false;
|
||||||
|
boolean inSingleLineComment = false;
|
||||||
|
String line;
|
||||||
|
int lineNo = 0;
|
||||||
|
int commentStartLineNo = 0;
|
||||||
|
|
||||||
|
logger.trace("Processing file %s...", filename);
|
||||||
|
|
||||||
|
while ((line = reader.readLine()) != null) {
|
||||||
|
lineNo += 1;
|
||||||
|
line = line.trim();
|
||||||
|
|
||||||
|
final boolean singleLineComment = line.startsWith("//") /* java */ || line.startsWith("--") /* SQL */;
|
||||||
|
|
||||||
|
if (!inMultilineComment && singleLineComment) {
|
||||||
|
final String comment = line.substring(2).trim();
|
||||||
|
|
||||||
|
inSingleLineComment = true;
|
||||||
|
} else if (line.startsWith("/*") /* java */ || line.startsWith("<!--") /* xml */) {
|
||||||
|
final String comment = line.startsWith("/**") ? line.substring(3)
|
||||||
|
: (line.startsWith("<!--") ? line.substring(4) : line.substring(2));
|
||||||
|
|
||||||
|
commentStartLineNo = lineNo;
|
||||||
|
inMultilineComment = true;
|
||||||
|
currentComment.append(comment.trim());
|
||||||
|
}
|
||||||
|
|
||||||
|
final boolean isFirstLineInComment = lineNo == commentStartLineNo;
|
||||||
|
|
||||||
|
if (singleLineComment && inSingleLineComment) {
|
||||||
|
final String comment = line.substring(2).trim();
|
||||||
|
|
||||||
|
currentComment.append(" ").append(comment.trim());
|
||||||
|
} else if (!singleLineComment && inSingleLineComment) {
|
||||||
|
String comment = currentComment.toString().trim();
|
||||||
|
inSingleLineComment = false;
|
||||||
|
|
||||||
|
parseComment(comment, javaFile, lineNo);
|
||||||
|
currentComment.setLength(0);
|
||||||
|
} else if (inMultilineComment && (line.endsWith("*/") /* java */ || line.endsWith("-->") /* xml */ )) {
|
||||||
|
String comment = line.endsWith("*/") ? line.substring(0, line.length() - 2)
|
||||||
|
: line.substring(0, line.length() - 3);
|
||||||
|
|
||||||
|
if (isFirstLineInComment) {
|
||||||
|
comment = currentComment.toString().trim();
|
||||||
|
comment = comment.endsWith("*/") ? comment.substring(0, comment.length() - 2)
|
||||||
|
: comment.substring(0, comment.length() - 3);
|
||||||
|
} else {
|
||||||
|
currentComment.append(" ").append(comment.trim());
|
||||||
|
comment = currentComment.toString().trim();
|
||||||
|
}
|
||||||
|
|
||||||
|
parseComment(comment, javaFile, lineNo);
|
||||||
|
|
||||||
|
inMultilineComment = false;
|
||||||
|
commentStartLineNo = 0;
|
||||||
|
currentComment.setLength(0);
|
||||||
|
} else if (inMultilineComment && !isFirstLineInComment) {
|
||||||
|
if (line.startsWith("*"))
|
||||||
|
line = line.substring(1).trim();
|
||||||
|
currentComment.append(" ").append(line);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
reader.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void parseComment(String pComment, File pInFile, int pLineNo) {
|
||||||
|
String command = null;
|
||||||
|
|
||||||
|
String type = null;
|
||||||
|
String regEx = String.join("|", COMMAD_LIST);
|
||||||
|
Pattern pattern = Pattern.compile(regEx);
|
||||||
|
Matcher matcher = pattern.matcher(pComment);
|
||||||
|
|
||||||
|
List<String> cmdList = new ArrayList<>();
|
||||||
|
List<Integer> startPositionList = new ArrayList<>();
|
||||||
|
|
||||||
|
while (matcher.find()) {
|
||||||
|
final int start = matcher.start();
|
||||||
|
String cmd = matcher.group();
|
||||||
|
|
||||||
|
cmdList.add(cmd);
|
||||||
|
startPositionList.add(start);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (cmdList.isEmpty())
|
||||||
|
return;
|
||||||
|
|
||||||
|
for (int n = 0; n < cmdList.size(); n++) {
|
||||||
|
String cmd = cmdList.get(n);
|
||||||
|
int start = startPositionList.get(n);
|
||||||
|
int end = 0;
|
||||||
|
|
||||||
|
if (n == cmdList.size() - 1) {
|
||||||
|
end = pComment.length();
|
||||||
|
} else {
|
||||||
|
String nextCmd = (String) cmdList.get(n + 1);
|
||||||
|
end = startPositionList.get(n + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
start += cmd.length();
|
||||||
|
command = pComment.substring(start, end);
|
||||||
|
|
||||||
|
// FIXME: Problem if comment text also includes "_"
|
||||||
|
command = command.replace("_", ";");
|
||||||
|
|
||||||
|
command = command.replaceFirst(":", ";");
|
||||||
|
List<String> split = new ArrayList<>(Arrays.asList(command.split(";")));
|
||||||
|
|
||||||
|
if (split.size() < 2) {
|
||||||
|
logger.warn("Unexpected HowTo format '%s', should be '%s H<ID>: [Title:|<StepNo>.] <Description>",
|
||||||
|
pComment, cmd);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
type = cmd.substring(0, cmd.length() - 1);
|
||||||
|
|
||||||
|
String id = split.remove(0);
|
||||||
|
String kind = split.remove(0).trim();
|
||||||
|
String descr = String.join(" ", split).trim();
|
||||||
|
String absoluteSourceCodeFilePath = pInFile.getAbsolutePath();
|
||||||
|
|
||||||
|
absoluteSourceCodeFilePath = absoluteSourceCodeFilePath.replace("\\", "/");
|
||||||
|
absoluteSourceCodeFilePath = absoluteSourceCodeFilePath.replaceFirst("V:/EBX", "");
|
||||||
|
|
||||||
|
String stepNoStr = "00";
|
||||||
|
|
||||||
|
if (!"Title".equals(kind)) {
|
||||||
|
stepNoStr = kind.replace("Step", "");
|
||||||
|
kind = "Step";
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
int rowNo = resultTable.newRow();
|
||||||
|
|
||||||
|
resultTable.setCellValue(rowNo, COL_TYPE, type);
|
||||||
|
resultTable.setCellValue(rowNo, COL_ID, id);
|
||||||
|
resultTable.setCellValue(rowNo, COL_KIND, kind);
|
||||||
|
resultTable.setCellValue(rowNo, COL_STEP, stepNoStr);
|
||||||
|
resultTable.setCellValue(rowNo, COL_FILE, absoluteSourceCodeFilePath);
|
||||||
|
resultTable.setCellValue(rowNo, COL_LINE_NO, pLineNo);
|
||||||
|
resultTable.setCellValue(rowNo, COL_DESC, descr);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,101 @@
|
|||||||
|
package application;
|
||||||
|
|
||||||
|
import java.awt.Desktop;
|
||||||
|
import java.awt.GridLayout;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import javax.swing.JButton;
|
||||||
|
import javax.swing.JFileChooser;
|
||||||
|
import javax.swing.JFrame;
|
||||||
|
import javax.swing.JLabel;
|
||||||
|
import javax.swing.JMenu;
|
||||||
|
import javax.swing.JMenuBar;
|
||||||
|
import javax.swing.JMenuItem;
|
||||||
|
import javax.swing.JOptionPane;
|
||||||
|
import javax.swing.SwingUtilities;
|
||||||
|
import javax.swing.filechooser.FileNameExtensionFilter;
|
||||||
|
|
||||||
|
public class FileActions extends JFrame {
|
||||||
|
private JLabel file1Label, file2Label;
|
||||||
|
private JButton selectFile1, selectFile2;
|
||||||
|
private File file1, file2;
|
||||||
|
private JMenuBar menuBar;
|
||||||
|
private JMenu actionsMenu;
|
||||||
|
private JMenuItem openInExcelItem;
|
||||||
|
|
||||||
|
public FileActions() {
|
||||||
|
setTitle("Datei-Auswahl und Aktionen");
|
||||||
|
setSize(400, 200);
|
||||||
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
setLayout(new GridLayout(3, 2));
|
||||||
|
|
||||||
|
file1Label = new JLabel("Datei 1: Keine ausgew<65>hlt");
|
||||||
|
file2Label = new JLabel("Datei 2: Keine ausgew<65>hlt");
|
||||||
|
selectFile1 = new JButton("Datei 1 ausw<73>hlen");
|
||||||
|
selectFile2 = new JButton("Datei 2 ausw<73>hlen");
|
||||||
|
|
||||||
|
// File Chooser f<>r Datei 1 (nur Excel-Dateien)
|
||||||
|
selectFile1.addActionListener(e -> {
|
||||||
|
JFileChooser fileChooser = new JFileChooser();
|
||||||
|
FileNameExtensionFilter filter = new FileNameExtensionFilter("Excel-Dateien", "xls", "xlsx");
|
||||||
|
fileChooser.setFileFilter(filter);
|
||||||
|
if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
|
||||||
|
file1 = fileChooser.getSelectedFile();
|
||||||
|
file1Label.setText("Datei 1: " + file1.getName());
|
||||||
|
selectFile2.setEnabled(true);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// File Chooser f<>r Datei 2 (im selben Verzeichnis wie Datei 1, nur
|
||||||
|
// Excel-Dateien)
|
||||||
|
selectFile2.setEnabled(false);
|
||||||
|
selectFile2.addActionListener(e -> {
|
||||||
|
if (file1 != null) {
|
||||||
|
JFileChooser fileChooser = new JFileChooser(file1.getParentFile());
|
||||||
|
FileNameExtensionFilter filter = new FileNameExtensionFilter("Excel-Dateien", "xls", "xlsx");
|
||||||
|
fileChooser.setFileFilter(filter);
|
||||||
|
if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
|
||||||
|
file2 = fileChooser.getSelectedFile();
|
||||||
|
file2Label.setText("Datei 2: " + file2.getName());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
add(selectFile1);
|
||||||
|
add(file1Label);
|
||||||
|
add(selectFile2);
|
||||||
|
add(file2Label);
|
||||||
|
|
||||||
|
// Men<65> erstellen
|
||||||
|
menuBar = new JMenuBar();
|
||||||
|
actionsMenu = new JMenu("Aktionen");
|
||||||
|
openInExcelItem = new JMenuItem("Erste Datei in Excel <20>ffnen");
|
||||||
|
|
||||||
|
actionsMenu.add(openInExcelItem);
|
||||||
|
menuBar.add(actionsMenu);
|
||||||
|
setJMenuBar(menuBar);
|
||||||
|
|
||||||
|
// Aktion: Erste Datei in Excel <20>ffnen
|
||||||
|
openInExcelItem.addActionListener(e -> {
|
||||||
|
if (file1 != null) {
|
||||||
|
try {
|
||||||
|
Desktop.getDesktop().open(file1);
|
||||||
|
} catch (IOException ex) {
|
||||||
|
JOptionPane.showMessageDialog(this, "Fehler beim <20>ffnen der Datei in Excel.", "Fehler",
|
||||||
|
JOptionPane.ERROR_MESSAGE);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
JOptionPane.showMessageDialog(this, "Bitte w<>hle zuerst eine Datei aus!", "Fehler",
|
||||||
|
JOptionPane.ERROR_MESSAGE);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SwingUtilities.invokeLater(() -> {
|
||||||
|
FileActions gui = new FileActions();
|
||||||
|
gui.setVisible(true);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,155 @@
|
|||||||
|
package application;
|
||||||
|
|
||||||
|
import java.awt.Desktop;
|
||||||
|
import java.awt.GridLayout;
|
||||||
|
import java.awt.event.MouseAdapter;
|
||||||
|
import java.awt.event.MouseEvent;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import javax.swing.JButton;
|
||||||
|
import javax.swing.JFileChooser;
|
||||||
|
import javax.swing.JFrame;
|
||||||
|
import javax.swing.JLabel;
|
||||||
|
import javax.swing.JMenu;
|
||||||
|
import javax.swing.JMenuBar;
|
||||||
|
import javax.swing.JMenuItem;
|
||||||
|
import javax.swing.JOptionPane;
|
||||||
|
import javax.swing.JPopupMenu;
|
||||||
|
import javax.swing.SwingUtilities;
|
||||||
|
import javax.swing.filechooser.FileNameExtensionFilter;
|
||||||
|
|
||||||
|
public class FileActionsV2 extends JFrame {
|
||||||
|
private JLabel file1Label, file2Label;
|
||||||
|
private JButton selectFile1, selectFile2;
|
||||||
|
private File file1, file2;
|
||||||
|
private JMenuBar menuBar;
|
||||||
|
private JMenu actionsMenu;
|
||||||
|
private JMenuItem openInExcelItem;
|
||||||
|
private JPopupMenu contextMenuFile1, contextMenuFile2;
|
||||||
|
|
||||||
|
public FileActionsV2() {
|
||||||
|
setTitle("Datei-Auswahl und Aktionen");
|
||||||
|
setSize(400, 250);
|
||||||
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
setLayout(new GridLayout(3, 2));
|
||||||
|
|
||||||
|
file1Label = new JLabel("Datei 1: Keine ausgew<65>hlt");
|
||||||
|
file2Label = new JLabel("Datei 2: Keine ausgew<65>hlt");
|
||||||
|
selectFile1 = new JButton("Datei 1 ausw<73>hlen");
|
||||||
|
selectFile2 = new JButton("Datei 2 ausw<73>hlen");
|
||||||
|
|
||||||
|
// Kontextmen<65>s erstellen
|
||||||
|
contextMenuFile1 = new JPopupMenu();
|
||||||
|
contextMenuFile2 = new JPopupMenu();
|
||||||
|
JMenuItem renameFile1 = new JMenuItem("Datei umbenennen");
|
||||||
|
JMenuItem renameFile2 = new JMenuItem("Datei umbenennen");
|
||||||
|
contextMenuFile1.add(renameFile1);
|
||||||
|
contextMenuFile2.add(renameFile2);
|
||||||
|
|
||||||
|
// MouseListener f<>r Kontextmen<65> auf den Labels
|
||||||
|
file1Label.addMouseListener(new MouseAdapter() {
|
||||||
|
@Override
|
||||||
|
public void mouseClicked(MouseEvent e) {
|
||||||
|
if (SwingUtilities.isRightMouseButton(e) && file1 != null) {
|
||||||
|
contextMenuFile1.show(file1Label, e.getX(), e.getY());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
file2Label.addMouseListener(new MouseAdapter() {
|
||||||
|
@Override
|
||||||
|
public void mouseClicked(MouseEvent e) {
|
||||||
|
if (SwingUtilities.isRightMouseButton(e) && file2 != null) {
|
||||||
|
contextMenuFile2.show(file2Label, e.getX(), e.getY());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
renameFile1.addActionListener(e -> renameFile(file1, file1Label, "Datei 1"));
|
||||||
|
renameFile2.addActionListener(e -> renameFile(file2, file2Label, "Datei 2"));
|
||||||
|
|
||||||
|
// File Chooser f<>r Datei 1 (nur Excel-Dateien)
|
||||||
|
selectFile1.addActionListener(e -> {
|
||||||
|
JFileChooser fileChooser = new JFileChooser();
|
||||||
|
FileNameExtensionFilter filter = new FileNameExtensionFilter("Excel-Dateien", "xls", "xlsx");
|
||||||
|
fileChooser.setFileFilter(filter);
|
||||||
|
if (file1 != null)
|
||||||
|
fileChooser.setCurrentDirectory(file1.getParentFile());
|
||||||
|
if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
|
||||||
|
file1 = fileChooser.getSelectedFile();
|
||||||
|
file1Label.setText("Datei 1: " + file1.getName());
|
||||||
|
selectFile2.setEnabled(true);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// File Chooser f<>r Datei 2 (im selben Verzeichnis wie Datei 1, nur
|
||||||
|
// Excel-Dateien)
|
||||||
|
selectFile2.setEnabled(false);
|
||||||
|
selectFile2.addActionListener(e -> {
|
||||||
|
if (file1 != null) {
|
||||||
|
JFileChooser fileChooser = new JFileChooser(file1.getParentFile());
|
||||||
|
FileNameExtensionFilter filter = new FileNameExtensionFilter("Excel-Dateien", "xls", "xlsx");
|
||||||
|
fileChooser.setFileFilter(filter);
|
||||||
|
if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
|
||||||
|
file2 = fileChooser.getSelectedFile();
|
||||||
|
file2Label.setText("Datei 2: " + file2.getName());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
add(selectFile1);
|
||||||
|
add(file1Label);
|
||||||
|
add(selectFile2);
|
||||||
|
add(file2Label);
|
||||||
|
|
||||||
|
// Men<65> erstellen
|
||||||
|
menuBar = new JMenuBar();
|
||||||
|
actionsMenu = new JMenu("Aktionen");
|
||||||
|
openInExcelItem = new JMenuItem("Erste Datei in Excel <20>ffnen");
|
||||||
|
|
||||||
|
actionsMenu.add(openInExcelItem);
|
||||||
|
menuBar.add(actionsMenu);
|
||||||
|
setJMenuBar(menuBar);
|
||||||
|
|
||||||
|
// Aktion: Erste Datei in Excel <20>ffnen
|
||||||
|
openInExcelItem.addActionListener(e -> {
|
||||||
|
if (file1 != null) {
|
||||||
|
try {
|
||||||
|
Desktop.getDesktop().open(file1);
|
||||||
|
} catch (IOException ex) {
|
||||||
|
JOptionPane.showMessageDialog(this, "Fehler beim <20>ffnen der Datei in Excel.", "Fehler",
|
||||||
|
JOptionPane.ERROR_MESSAGE);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
JOptionPane.showMessageDialog(this, "Bitte w<>hle zuerst eine Datei aus!", "Fehler",
|
||||||
|
JOptionPane.ERROR_MESSAGE);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private void renameFile(File file, JLabel label, String prefix) {
|
||||||
|
if (file == null)
|
||||||
|
return;
|
||||||
|
String newName = JOptionPane.showInputDialog(this, "Neuer Dateiname:", file.getName());
|
||||||
|
if (newName != null && !newName.trim().isEmpty()) {
|
||||||
|
File newFile = new File(file.getParentFile(), newName);
|
||||||
|
if (file.renameTo(newFile)) {
|
||||||
|
file = newFile;
|
||||||
|
label.setText(prefix + ": " + newFile.getName());
|
||||||
|
if (prefix.equals("Datei 1"))
|
||||||
|
this.file1 = newFile;
|
||||||
|
else
|
||||||
|
this.file2 = newFile;
|
||||||
|
} else {
|
||||||
|
JOptionPane.showMessageDialog(this, "Umbenennung fehlgeschlagen!", "Fehler", JOptionPane.ERROR_MESSAGE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SwingUtilities.invokeLater(() -> {
|
||||||
|
FileActionsV2 gui = new FileActionsV2();
|
||||||
|
gui.setVisible(true);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,340 @@
|
|||||||
|
package application;
|
||||||
|
|
||||||
|
import java.awt.BorderLayout;
|
||||||
|
import java.awt.Desktop;
|
||||||
|
import java.awt.Dimension;
|
||||||
|
import java.awt.GridLayout;
|
||||||
|
import java.awt.event.ActionEvent;
|
||||||
|
import java.awt.event.KeyEvent;
|
||||||
|
import java.awt.event.MouseAdapter;
|
||||||
|
import java.awt.event.MouseEvent;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.StandardCopyOption;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import javax.naming.ConfigurationException;
|
||||||
|
import javax.swing.BorderFactory;
|
||||||
|
import javax.swing.JButton;
|
||||||
|
import javax.swing.JFileChooser;
|
||||||
|
import javax.swing.JFrame;
|
||||||
|
import javax.swing.JLabel;
|
||||||
|
import javax.swing.JMenu;
|
||||||
|
import javax.swing.JMenuBar;
|
||||||
|
import javax.swing.JMenuItem;
|
||||||
|
import javax.swing.JOptionPane;
|
||||||
|
import javax.swing.JPanel;
|
||||||
|
import javax.swing.JPopupMenu;
|
||||||
|
import javax.swing.KeyStroke;
|
||||||
|
import javax.swing.SwingUtilities;
|
||||||
|
import javax.swing.filechooser.FileNameExtensionFilter;
|
||||||
|
|
||||||
|
import de.lcag.common.ExcelWorkbook;
|
||||||
|
import de.lcag.common.LcagProperties;
|
||||||
|
import de.lcag.common.MappingTable;
|
||||||
|
import de.lcag.common.WorkbookComparisonResult;
|
||||||
|
import routines.LcagFileTools;
|
||||||
|
import routines.LcagStringTools;
|
||||||
|
|
||||||
|
public class FileActionsV3 extends JFrame {
|
||||||
|
private static final int FRAME_HEIGHT = 150;
|
||||||
|
private static String MASTER_FILE_LABEL = "Main Excel File";
|
||||||
|
private static String CONFIG_FILE_LABEL = "Configuration File";
|
||||||
|
|
||||||
|
private JLabel masterFileLabel, clientFileLabel, configFileLabel;
|
||||||
|
private JButton selectMasterFile, selectClientFile, selectConfigFile;
|
||||||
|
private Path masterFile, clientFile, configFile;
|
||||||
|
private JMenuBar menuBar;
|
||||||
|
private JMenu actionsMenu;
|
||||||
|
private JMenuItem mergeAction;
|
||||||
|
private JPopupMenu contextMenuMasterFile, contextMenuClientFile;
|
||||||
|
private File configFileDirectoryPath = null;
|
||||||
|
private LcagProperties config = null;
|
||||||
|
private File masterDirecory = null;
|
||||||
|
|
||||||
|
public FileActionsV3() {
|
||||||
|
setTitle("Compare 2 Excel Files");
|
||||||
|
setSize(800, FRAME_HEIGHT);
|
||||||
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
|
||||||
|
JPanel mainPanel = new JPanel(new BorderLayout());
|
||||||
|
JPanel leftButtonPanel = new JPanel(new GridLayout(3, 1, 5, 5));
|
||||||
|
JPanel rightTextPanel = new JPanel(new GridLayout(3, 1, 5, 5));
|
||||||
|
// setLayout(new GridLayout(4, 2));
|
||||||
|
|
||||||
|
leftButtonPanel.setPreferredSize(new Dimension(200, FRAME_HEIGHT)); // Fixed width
|
||||||
|
|
||||||
|
// Add an empty border to create a gap on the right side of leftPanel
|
||||||
|
leftButtonPanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 10)); // right gap: 10px
|
||||||
|
|
||||||
|
rightTextPanel.setPreferredSize(new Dimension(500, FRAME_HEIGHT));
|
||||||
|
mainPanel.add(leftButtonPanel, BorderLayout.WEST);
|
||||||
|
mainPanel.add(rightTextPanel, BorderLayout.EAST);
|
||||||
|
|
||||||
|
configFileLabel = new JLabel(String.format(" %s: Not selected", CONFIG_FILE_LABEL));
|
||||||
|
|
||||||
|
selectConfigFile = new JButton("Select " + CONFIG_FILE_LABEL);
|
||||||
|
leftButtonPanel.add(selectConfigFile);
|
||||||
|
rightTextPanel.add(configFileLabel);
|
||||||
|
|
||||||
|
// File Chooser for config file
|
||||||
|
selectConfigFile.setEnabled(true);
|
||||||
|
selectConfigFile.addActionListener(e -> {
|
||||||
|
if (!configFileDirectoryPath.isDirectory()) {
|
||||||
|
System.err.println(String.format("ERROR: Config directory %s does not exist",
|
||||||
|
configFileDirectoryPath.getAbsolutePath()));
|
||||||
|
System.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
JFileChooser fileChooser = new JFileChooser(configFileDirectoryPath);
|
||||||
|
FileNameExtensionFilter filter = new FileNameExtensionFilter("Config Files", "properties");
|
||||||
|
fileChooser.setFileFilter(filter);
|
||||||
|
if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
|
||||||
|
String clientDirString = null;
|
||||||
|
|
||||||
|
configFile = selectFile(fileChooser);
|
||||||
|
config = new LcagProperties(configFile.toString());
|
||||||
|
|
||||||
|
configFileLabel.setText(String.format(" %s: %s", CONFIG_FILE_LABEL, configFile));
|
||||||
|
|
||||||
|
masterFile = config.getOptionalFilePathProperty("main_file");
|
||||||
|
clientFile = config.getOptionalFilePathProperty("compare_file");
|
||||||
|
|
||||||
|
if (clientFile.isAbsolute()) {
|
||||||
|
clientDirString = clientFile.getParent().toString();
|
||||||
|
} else {
|
||||||
|
// client file is relative expect it is same directory as master
|
||||||
|
Path dirPath = masterFile.getParent();
|
||||||
|
|
||||||
|
clientDirString = "same directory as 1st file";
|
||||||
|
clientFile = dirPath.resolve(clientFile);
|
||||||
|
}
|
||||||
|
|
||||||
|
// After user selected a config file enable buttons to set files to compare (can
|
||||||
|
// overwrite files listed in config file):
|
||||||
|
selectMasterFile.setEnabled(true);
|
||||||
|
|
||||||
|
if (clientFile != null) {
|
||||||
|
String fileName = clientFile.getFileName().toString();
|
||||||
|
clientFileLabel
|
||||||
|
.setText(String.format("<html>Selected: %s<br>in: %s</html>", fileName, clientDirString));
|
||||||
|
selectClientFile.setEnabled(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (masterFile != null) {
|
||||||
|
String fileName = masterFile.getFileName().toString();
|
||||||
|
masterDirecory = new File(masterFile.getParent().toString());
|
||||||
|
masterFileLabel.setText(String.format("<html>Selected: %s<br>in: %s</html>", fileName,
|
||||||
|
masterFile.getParent().toString()));
|
||||||
|
selectClientFile.setEnabled(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Kontextmen<65>s erstellen
|
||||||
|
contextMenuMasterFile = new JPopupMenu();
|
||||||
|
contextMenuClientFile = new JPopupMenu();
|
||||||
|
JMenuItem renameFile1 = new JMenuItem("Rename File");
|
||||||
|
JMenuItem renameFile2 = new JMenuItem("Rename File");
|
||||||
|
contextMenuMasterFile.add(renameFile1);
|
||||||
|
contextMenuClientFile.add(renameFile2);
|
||||||
|
|
||||||
|
masterFileLabel = new JLabel(String.format(" %s: %s", MASTER_FILE_LABEL,
|
||||||
|
masterFile == null ? "Not selected" : masterFile.getFileName()));
|
||||||
|
|
||||||
|
selectMasterFile = new JButton("Select " + MASTER_FILE_LABEL);
|
||||||
|
leftButtonPanel.add(selectMasterFile);
|
||||||
|
rightTextPanel.add(masterFileLabel);
|
||||||
|
|
||||||
|
clientFileLabel = new JLabel(String.format(" %s: %s", "Excel to compare",
|
||||||
|
clientFile == null ? "Not selected" : clientFile.getFileName()));
|
||||||
|
|
||||||
|
selectClientFile = new JButton("Select Excel to Compare");
|
||||||
|
|
||||||
|
// MouseListener f<>r Kontextmen<65> auf den Labels
|
||||||
|
masterFileLabel.addMouseListener(new MouseAdapter() {
|
||||||
|
@Override
|
||||||
|
public void mouseClicked(MouseEvent e) {
|
||||||
|
if (SwingUtilities.isRightMouseButton(e) && masterFile != null) {
|
||||||
|
contextMenuMasterFile.show(masterFileLabel, e.getX(), e.getY());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
clientFileLabel.addMouseListener(new MouseAdapter() {
|
||||||
|
@Override
|
||||||
|
public void mouseClicked(MouseEvent e) {
|
||||||
|
if (SwingUtilities.isRightMouseButton(e) && clientFile != null) {
|
||||||
|
contextMenuClientFile.show(clientFileLabel, e.getX(), e.getY());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
leftButtonPanel.add(selectClientFile);
|
||||||
|
rightTextPanel.add(clientFileLabel);
|
||||||
|
|
||||||
|
renameFile1.addActionListener(e -> renameFile(masterFile, masterFileLabel, "Datei 1"));
|
||||||
|
renameFile2.addActionListener(e -> renameFile(clientFile, clientFileLabel, "Datei 2"));
|
||||||
|
|
||||||
|
// File Chooser f<>r Datei 1 (nur Excel-Dateien)
|
||||||
|
selectMasterFile.setEnabled(false);
|
||||||
|
selectMasterFile.addActionListener(e -> {
|
||||||
|
JFileChooser fileChooser = new JFileChooser();
|
||||||
|
FileNameExtensionFilter filter = new FileNameExtensionFilter("Excel-Files", "xls", "xlsx");
|
||||||
|
fileChooser.setFileFilter(filter);
|
||||||
|
if (masterFile != null) {
|
||||||
|
masterDirecory = new File(masterFile.getParent().toString());
|
||||||
|
fileChooser.setCurrentDirectory(masterDirecory);
|
||||||
|
}
|
||||||
|
if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
|
||||||
|
masterFile = selectFile(fileChooser);
|
||||||
|
masterDirecory = new File(masterFile.getParent().toString());
|
||||||
|
masterFileLabel.setText(String.format(" %s: %s", MASTER_FILE_LABEL, masterFile.getFileName()));
|
||||||
|
selectClientFile.setEnabled(true);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// File Chooser f<>r Datei 2 (im selben Verzeichnis wie Datei 1, nur
|
||||||
|
// Excel-Dateien)
|
||||||
|
selectClientFile.setEnabled(false);
|
||||||
|
selectClientFile.addActionListener(e -> {
|
||||||
|
if (masterDirecory != null) {
|
||||||
|
JFileChooser fileChooser = new JFileChooser(masterDirecory);
|
||||||
|
FileNameExtensionFilter filter = new FileNameExtensionFilter("Excel-Dateien", "xls", "xlsx");
|
||||||
|
fileChooser.setFileFilter(filter);
|
||||||
|
if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
|
||||||
|
clientFile = selectFile(fileChooser);
|
||||||
|
clientFileLabel.setText("Datei 2: " + clientFile.getFileName());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Men<65> erstellen
|
||||||
|
menuBar = new JMenuBar();
|
||||||
|
actionsMenu = new JMenu("Actions");
|
||||||
|
mergeAction = new JMenuItem("Merge 2nd into Main file");
|
||||||
|
|
||||||
|
mergeAction.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_M, ActionEvent.CTRL_MASK));
|
||||||
|
mergeAction.addActionListener(e -> {
|
||||||
|
System.out.println("Ctrl-C pressed on menu");
|
||||||
|
});
|
||||||
|
|
||||||
|
actionsMenu.add(mergeAction);
|
||||||
|
menuBar.add(actionsMenu);
|
||||||
|
setJMenuBar(menuBar);
|
||||||
|
|
||||||
|
// Aktion: Erste Datei in Excel <20>ffnen
|
||||||
|
mergeAction.addActionListener(e -> {
|
||||||
|
mergeFiles(config, masterFile, clientFile);
|
||||||
|
});
|
||||||
|
|
||||||
|
add(mainPanel);
|
||||||
|
pack();
|
||||||
|
setVisible(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Path selectFile(JFileChooser fileChooser) {
|
||||||
|
File result = fileChooser.getSelectedFile();
|
||||||
|
return Path.of(result.getAbsolutePath());
|
||||||
|
}
|
||||||
|
|
||||||
|
private void mergeFiles(LcagProperties pConfig, Path pMasterFilePath, Path pClientFilePath) {
|
||||||
|
if (pConfig != null) {
|
||||||
|
String keyFieldString = pConfig.getProperty("list_of_keys");
|
||||||
|
|
||||||
|
List<String> keyFields = LcagStringTools.asList(keyFieldString, ",\\s*");
|
||||||
|
|
||||||
|
if (pMasterFilePath != null && pClientFilePath != null)
|
||||||
|
try {
|
||||||
|
String masterFileName = pMasterFilePath.getFileName().toString();
|
||||||
|
Path resultFilePath = LcagFileTools.appendStringToFilePath(pMasterFilePath, "-merged");
|
||||||
|
String fieldMapping = pConfig.getProperty("column.mapping.main2compare");
|
||||||
|
|
||||||
|
Files.copy(pMasterFilePath, resultFilePath, StandardCopyOption.REPLACE_EXISTING);
|
||||||
|
|
||||||
|
ExcelWorkbook excel1 = new ExcelWorkbook(resultFilePath.toString());
|
||||||
|
ExcelWorkbook excel2 = new ExcelWorkbook(pClientFilePath.toString());
|
||||||
|
|
||||||
|
excel1.setKeyColumns(keyFields);
|
||||||
|
excel1.read();
|
||||||
|
excel2.setKeyColumns(keyFields);
|
||||||
|
excel2.read();
|
||||||
|
|
||||||
|
excel1.setOption(keyFieldString, FRAME_HEIGHT);
|
||||||
|
|
||||||
|
if (fieldMapping != null) {
|
||||||
|
MappingTable mappingTable = new MappingTable(fieldMapping, ";", "->");
|
||||||
|
|
||||||
|
excel2.setOption(mappingTable);
|
||||||
|
}
|
||||||
|
|
||||||
|
WorkbookComparisonResult compareResult = excel1.compareTo(excel2);
|
||||||
|
|
||||||
|
ExcelWorkbook integratedTable = (ExcelWorkbook) compareResult.integrateDifferencesIntoMaster();
|
||||||
|
|
||||||
|
integratedTable.save();
|
||||||
|
|
||||||
|
Desktop.getDesktop().open(resultFilePath.toFile());
|
||||||
|
} catch (IOException ex) {
|
||||||
|
JOptionPane.showMessageDialog(this, "Cannot open comparison result file.", "Error",
|
||||||
|
JOptionPane.ERROR_MESSAGE);
|
||||||
|
} catch (ConfigurationException e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
JOptionPane.showMessageDialog(this, "Pls select at least a config file!", "Error",
|
||||||
|
JOptionPane.ERROR_MESSAGE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Path getPath(LcagProperties config, String pProperty, File pMasterFile) {
|
||||||
|
Path result = null;
|
||||||
|
String filePathString = (pMasterFile == null) ? config.getProperty(pProperty) : pMasterFile.getAbsolutePath();
|
||||||
|
|
||||||
|
if (!LcagStringTools.isEmpty(filePathString)) {
|
||||||
|
result = Path.of(filePathString);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Path renameFile(Path pFile, JLabel label, String prefix) {
|
||||||
|
Path newPath = null;
|
||||||
|
if (pFile == null)
|
||||||
|
return newPath;
|
||||||
|
|
||||||
|
File file = new File(pFile.toString());
|
||||||
|
String newName = JOptionPane.showInputDialog(this, "Enter new name:", file.getName());
|
||||||
|
|
||||||
|
if (!LcagStringTools.isEmpty(newName)) {
|
||||||
|
newPath = pFile.getParent().resolve(newName);
|
||||||
|
|
||||||
|
File newFile = new File(newPath.toString());
|
||||||
|
if (file.renameTo(newFile)) {
|
||||||
|
file = newFile;
|
||||||
|
label.setText(prefix + ": " + newFile.getName());
|
||||||
|
if (prefix.equals("Datei 1"))
|
||||||
|
this.masterFile = newPath;
|
||||||
|
else
|
||||||
|
this.clientFile = newPath;
|
||||||
|
} else {
|
||||||
|
JOptionPane.showMessageDialog(this, "Umbenennung fehlgeschlagen!", "Fehler", JOptionPane.ERROR_MESSAGE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return newPath;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
String cfgFileDirectory = args.length < 1 ? System.getenv("_MY_DATA") + "/Tools" : args[1];
|
||||||
|
|
||||||
|
SwingUtilities.invokeLater(() -> {
|
||||||
|
FileActionsV3 gui = new FileActionsV3();
|
||||||
|
|
||||||
|
gui.configFileDirectoryPath = new File(cfgFileDirectory);
|
||||||
|
gui.setVisible(true);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,119 @@
|
|||||||
|
package application;
|
||||||
|
|
||||||
|
import java.awt.BorderLayout;
|
||||||
|
import java.awt.Desktop;
|
||||||
|
import java.awt.event.MouseAdapter;
|
||||||
|
import java.awt.event.MouseEvent;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.URI;
|
||||||
|
import java.net.URISyntaxException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import javax.swing.JFrame;
|
||||||
|
import javax.swing.JScrollPane;
|
||||||
|
import javax.swing.JTable;
|
||||||
|
import javax.swing.SwingUtilities;
|
||||||
|
import javax.swing.table.DefaultTableModel;
|
||||||
|
|
||||||
|
import de.lcag.common.CsvTable;
|
||||||
|
|
||||||
|
public class ShowDevelopmentHints extends JFrame {
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
private JTable table;
|
||||||
|
private DefaultTableModel model;
|
||||||
|
private String gitBranchName = null;
|
||||||
|
|
||||||
|
CsvTable hints = new CsvTable(CommentExtractor.DOC_FILE + ".csv");
|
||||||
|
Object[] columnNames = null;
|
||||||
|
List<Object[]> data = new ArrayList<>();
|
||||||
|
|
||||||
|
public ShowDevelopmentHints() throws Exception {
|
||||||
|
setTitle("Tabelle mit externem Programm");
|
||||||
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
setSize(400, 300);
|
||||||
|
setLocationRelativeTo(null);
|
||||||
|
|
||||||
|
gitBranchName = CommentExtractor.getCurrentGitBranch();
|
||||||
|
|
||||||
|
createDataTable();
|
||||||
|
|
||||||
|
// Hinzuf<75>gen des MouseListeners f<>r Zellklicks
|
||||||
|
table.addMouseListener(new MouseAdapter() {
|
||||||
|
@Override
|
||||||
|
public void mouseClicked(MouseEvent e) {
|
||||||
|
if (e.getClickCount() == 1) {
|
||||||
|
int row = table.rowAtPoint(e.getPoint());
|
||||||
|
int column = table.columnAtPoint(e.getPoint());
|
||||||
|
String colName = (String) columnNames[column];
|
||||||
|
|
||||||
|
if (row >= 0 && "LINENO".equals(colName)) {
|
||||||
|
String filePath = (String) table.getValueAt(row, column - 1);
|
||||||
|
int lineNo = Integer.parseInt((String) table.getValueAt(row, column));
|
||||||
|
String gitURL = String.format(CommentExtractor.CODE_BASE_GITHUB_DIR, filePath, gitBranchName,
|
||||||
|
lineNo, lineNo + 1);
|
||||||
|
|
||||||
|
runExternalProgram(gitURL);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
JScrollPane scrollPane = new JScrollPane(table);
|
||||||
|
add(scrollPane, BorderLayout.CENTER);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void createDataTable() throws IOException {
|
||||||
|
hints.read();
|
||||||
|
columnNames = hints.getColumnLabels().toArray();
|
||||||
|
|
||||||
|
final int noRows = hints.lenght();
|
||||||
|
|
||||||
|
model = new DefaultTableModel(columnNames, 0);
|
||||||
|
|
||||||
|
for (int i = 0; i < noRows; i++) {
|
||||||
|
List<Object> valuesInRow = new ArrayList<>();
|
||||||
|
String filePath = null;
|
||||||
|
|
||||||
|
for (Object col : columnNames) {
|
||||||
|
final Object cellValue = hints.getCellValue(i, (String) col);
|
||||||
|
|
||||||
|
valuesInRow.add(cellValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
model.addRow(valuesInRow.toArray());
|
||||||
|
}
|
||||||
|
|
||||||
|
table = new JTable(model);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void runExternalProgram(String value) {
|
||||||
|
// Does not work: Opens a new Eclipse.
|
||||||
|
// String prg = "eclipsec.exe -name Eclipse --launcher.openFile
|
||||||
|
// %USERPROFILE%\\workspace\\MyClass.java:50";
|
||||||
|
String prg = "notepad++ <filePath> -n50";
|
||||||
|
Desktop desktop = Desktop.getDesktop();
|
||||||
|
try {
|
||||||
|
// Open the URL in the default browser
|
||||||
|
desktop.browse(new URI(value));
|
||||||
|
} catch (IOException | URISyntaxException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SwingUtilities.invokeLater(() -> {
|
||||||
|
ShowDevelopmentHints frame;
|
||||||
|
try {
|
||||||
|
frame = new ShowDevelopmentHints();
|
||||||
|
frame.setVisible(true);
|
||||||
|
} catch (Exception e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,65 @@
|
|||||||
|
package application;
|
||||||
|
|
||||||
|
import java.awt.BorderLayout;
|
||||||
|
import java.awt.event.MouseAdapter;
|
||||||
|
import java.awt.event.MouseEvent;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import javax.swing.JFrame;
|
||||||
|
import javax.swing.JScrollPane;
|
||||||
|
import javax.swing.JTable;
|
||||||
|
import javax.swing.SwingUtilities;
|
||||||
|
import javax.swing.table.DefaultTableModel;
|
||||||
|
|
||||||
|
public class TableWithExternalProgram extends JFrame {
|
||||||
|
private JTable table;
|
||||||
|
private DefaultTableModel model;
|
||||||
|
|
||||||
|
public TableWithExternalProgram() {
|
||||||
|
setTitle("Tabelle mit externem Programm");
|
||||||
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
setSize(400, 300);
|
||||||
|
setLocationRelativeTo(null);
|
||||||
|
|
||||||
|
// Erstellen der Tabelle
|
||||||
|
String[] columnNames = { "Name", "Alter", "Beruf" };
|
||||||
|
Object[][] data = { { "John", 25, "Ingenieur" }, { "Alice", 30, "<EFBFBD>rztin" }, { "Bob", 35, "Lehrer" } };
|
||||||
|
|
||||||
|
model = new DefaultTableModel(data, columnNames);
|
||||||
|
table = new JTable(model);
|
||||||
|
|
||||||
|
// Hinzuf<75>gen des MouseListeners f<>r Zellklicks
|
||||||
|
table.addMouseListener(new MouseAdapter() {
|
||||||
|
@Override
|
||||||
|
public void mouseClicked(MouseEvent e) {
|
||||||
|
if (e.getClickCount() == 1) {
|
||||||
|
int row = table.rowAtPoint(e.getPoint());
|
||||||
|
int column = table.columnAtPoint(e.getPoint());
|
||||||
|
if (row >= 0 && column >= 0) {
|
||||||
|
String value = (String) table.getValueAt(row, column);
|
||||||
|
runExternalProgram(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
JScrollPane scrollPane = new JScrollPane(table);
|
||||||
|
add(scrollPane, BorderLayout.CENTER);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void runExternalProgram(String value) {
|
||||||
|
try {
|
||||||
|
// Beispiel: <20>ffnen der Systemeinstellungen
|
||||||
|
Runtime.getRuntime().exec("control.exe");
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SwingUtilities.invokeLater(() -> {
|
||||||
|
TableWithExternalProgram frame = new TableWithExternalProgram();
|
||||||
|
frame.setVisible(true);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
package application;
|
||||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,163 @@
|
|||||||
|
package de.lcag.common;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.URI;
|
||||||
|
import java.nio.charset.Charset;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.apache.commons.io.FileUtils;
|
||||||
|
|
||||||
|
import routines.LcagFileTools;
|
||||||
|
|
||||||
|
public class CsvTable extends HashTable implements Storage {
|
||||||
|
String filePath = null;
|
||||||
|
private char encloseBy = '"';
|
||||||
|
|
||||||
|
public CsvTable() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public CsvTable(String pTableName, String aFilePath) {
|
||||||
|
super(pTableName);
|
||||||
|
|
||||||
|
setFilePath(aFilePath);
|
||||||
|
}
|
||||||
|
|
||||||
|
public CsvTable(String aFilePath, AbstractTable pSrcTable) {
|
||||||
|
super(pSrcTable);
|
||||||
|
setFilePath(aFilePath);
|
||||||
|
}
|
||||||
|
|
||||||
|
public CsvTable(String aFilePath, String... pColumnNameList) {
|
||||||
|
super(pColumnNameList);
|
||||||
|
setFilePath(aFilePath);
|
||||||
|
}
|
||||||
|
|
||||||
|
private String setFilePath(String aFilePath) {
|
||||||
|
if (!aFilePath.toLowerCase().endsWith(".csv")) {
|
||||||
|
aFilePath += ".csv";
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.filePath = aFilePath;
|
||||||
|
}
|
||||||
|
|
||||||
|
public CsvTable(String aFilePath, Object pDefaultValue, SimpleDateFormat pDefaultDateFormat) {
|
||||||
|
super(pDefaultValue, pDefaultDateFormat);
|
||||||
|
setFilePath(aFilePath);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void saveAs(String pFilePath) throws IOException {
|
||||||
|
File aFile = new File(pFilePath);
|
||||||
|
String filePath = aFile.getCanonicalPath();
|
||||||
|
String text = this.toString();
|
||||||
|
|
||||||
|
createDirectoriesInPath(aFile);
|
||||||
|
|
||||||
|
FileUtils.writeStringToFile(aFile, text, Charset.forName("UTF-8"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void save() throws IOException {
|
||||||
|
saveAs(this.filePath);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void append() throws IOException {
|
||||||
|
File aFile = new File(this.filePath);
|
||||||
|
URI uri = aFile.toURI();
|
||||||
|
|
||||||
|
if (aFile.exists()) {
|
||||||
|
String text = this.asCsvData();
|
||||||
|
|
||||||
|
FileUtils.writeStringToFile(aFile, text, Charset.forName("UTF-8"), true);
|
||||||
|
} else {
|
||||||
|
this.save();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void readColumNames() throws IOException {
|
||||||
|
String text = LcagFileTools.readTextFile(filePath);
|
||||||
|
String eol = this.getEol();
|
||||||
|
String headline = text.substring(0, text.indexOf(eol));
|
||||||
|
|
||||||
|
String[] cols = headline.split(getSeparator().trim());
|
||||||
|
int colNo = 0;
|
||||||
|
|
||||||
|
for (String col : cols) {
|
||||||
|
col = col.trim();
|
||||||
|
|
||||||
|
this.addColumn(col);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void read() throws IOException {
|
||||||
|
String text = LcagFileTools.readTextFile(filePath);
|
||||||
|
parseString(text);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void read(String text) {
|
||||||
|
parseString(text);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void parseString(String text) {
|
||||||
|
int rowNo = -1;
|
||||||
|
boolean firstIteration = true;
|
||||||
|
String eol = this.getEol();
|
||||||
|
String sep = getSeparator().trim();
|
||||||
|
List<Integer> columnNumbersToUse = new ArrayList<>();
|
||||||
|
|
||||||
|
for (String line : text.split(eol)) {
|
||||||
|
String[] cols = line.split(sep);
|
||||||
|
int colNo = 0;
|
||||||
|
|
||||||
|
if (!firstIteration) {
|
||||||
|
rowNo = this.newRow();
|
||||||
|
}
|
||||||
|
|
||||||
|
for (String cellValue : cols) {
|
||||||
|
cellValue = cellValue.trim();
|
||||||
|
|
||||||
|
cellValue = stripEncosingCharacter(cellValue);
|
||||||
|
|
||||||
|
if (firstIteration) {
|
||||||
|
if (useColumn(cellValue)) {
|
||||||
|
this.addColumn(cellValue);
|
||||||
|
columnNumbersToUse.add(colNo);
|
||||||
|
}
|
||||||
|
} else if (columnNumbersToUse.contains(colNo)) {
|
||||||
|
this.setCellValue(rowNo, colNo, cellValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
colNo += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
firstIteration = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* If a CSV cell is enclosed by a character, strip it off and return the result
|
||||||
|
*
|
||||||
|
* @param pValue
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
private String stripEncosingCharacter(String pValue) {
|
||||||
|
int n = pValue.length() - 1;
|
||||||
|
|
||||||
|
if (n > 0 && pValue.charAt(0) == this.encloseBy && pValue.charAt(n) == this.encloseBy) {
|
||||||
|
pValue = pValue.substring(1, n);
|
||||||
|
}
|
||||||
|
return pValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void createDirectoriesInPath(File aFile) {
|
||||||
|
File dir = aFile.getParentFile();
|
||||||
|
|
||||||
|
if (dir != null) {
|
||||||
|
dir.mkdirs();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,77 @@
|
|||||||
|
package de.lcag.common;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.DriverManager;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.ResultSetMetaData;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.sql.Statement;
|
||||||
|
|
||||||
|
public class DbTable extends HashTable {
|
||||||
|
private LcagProperties dbProperties = null;
|
||||||
|
|
||||||
|
public DbTable(LcagProperties pDbProperties) {
|
||||||
|
this.dbProperties = pDbProperties;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void append(LcagProperties dbProperties, String sqlQuery) {
|
||||||
|
String jdbcURL = dbProperties.getProperty("jdbcURL");
|
||||||
|
String dbusername = dbProperties.getProperty("username");
|
||||||
|
String dbpassword = dbProperties.getProperty("password");
|
||||||
|
|
||||||
|
// String excelFilePath = getFileName(tableName.concat("_Export"));
|
||||||
|
|
||||||
|
try (Connection connection = DriverManager.getConnection(jdbcURL, dbusername, dbpassword)) {
|
||||||
|
Statement statement = connection.createStatement();
|
||||||
|
|
||||||
|
ResultSet result = statement.executeQuery(sqlQuery);
|
||||||
|
|
||||||
|
writeHeaderLine(result);
|
||||||
|
writeDataLines(result);
|
||||||
|
|
||||||
|
statement.close();
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
log.error("Error while connecting to database: %s", e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void append(String sqlQuery) {
|
||||||
|
this.append(this.dbProperties, sqlQuery);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* write header line containing column names
|
||||||
|
*
|
||||||
|
* @param result
|
||||||
|
* @throws SQLException
|
||||||
|
*/
|
||||||
|
private void writeHeaderLine(ResultSet result) throws SQLException {
|
||||||
|
ResultSetMetaData metaData = result.getMetaData();
|
||||||
|
int numberOfColumns = metaData.getColumnCount();
|
||||||
|
|
||||||
|
for (int i = 1; i <= numberOfColumns; i++) {
|
||||||
|
String columnName = metaData.getColumnName(i);
|
||||||
|
|
||||||
|
this.addColumn(columnName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void writeDataLines(ResultSet result) throws SQLException {
|
||||||
|
ResultSetMetaData metaData = result.getMetaData();
|
||||||
|
int numberOfColumns = metaData.getColumnCount();
|
||||||
|
|
||||||
|
while (result.next()) {
|
||||||
|
int newRow = this.newRow();
|
||||||
|
|
||||||
|
for (int i = 1; i <= numberOfColumns; i++) {
|
||||||
|
Object valueObject = result.getObject(i);
|
||||||
|
|
||||||
|
this.setCellValue(newRow, i - 1, valueObject);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
package de.lcag.common;
|
||||||
|
|
||||||
|
public class DuplicateValueException extends Exception {
|
||||||
|
public DuplicateValueException() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public DuplicateValueException(String pText) {
|
||||||
|
super(pText);
|
||||||
|
}
|
||||||
|
|
||||||
|
public DuplicateValueException(String pText, Object... pArgs) {
|
||||||
|
this(String.format(pText, pArgs));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,172 @@
|
|||||||
|
package de.lcag.common;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import de.lcag.common.ExcelWorkbook.ExcelTable;
|
||||||
|
import routines.LcagFileTools;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class represents a sheet in an Excel Workbook file. By default, it has
|
||||||
|
* just one sheet. You add HashTables as additional sheets using the addSheet
|
||||||
|
* method. When saving this ExcelTable all its sheets are saved too.
|
||||||
|
*
|
||||||
|
* @author U401455
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class ExcelHashTable extends HashTable implements Storage {
|
||||||
|
private static final String EXCEL_DEFAULT_SHEET_NAME = "Sheet1";
|
||||||
|
private static final String EXCEL_DATETIME_FORMAT_STR = "yyyy-MM-dd HH:mm:ss";
|
||||||
|
private static final SimpleDateFormat EXCEL_DATETIME_FORMAT = new SimpleDateFormat(EXCEL_DATETIME_FORMAT_STR);
|
||||||
|
String filePath = null;
|
||||||
|
ExcelWorkbook workbook = new ExcelWorkbook();
|
||||||
|
|
||||||
|
Map<String, HashTable> excelSheets = new LinkedHashMap<>();
|
||||||
|
|
||||||
|
public ExcelHashTable(String aFilePath, Object pDefaultValue, SimpleDateFormat pDefaultDateFormat) {
|
||||||
|
super(pDefaultValue, pDefaultDateFormat);
|
||||||
|
|
||||||
|
resolveFilePath(aFilePath, new HashMap<String, Object>());
|
||||||
|
}
|
||||||
|
|
||||||
|
public ExcelHashTable(String aFilePath, String... pColumnNameList) {
|
||||||
|
super(pColumnNameList);
|
||||||
|
|
||||||
|
this.setDefaultDateFormat(EXCEL_DATETIME_FORMAT);
|
||||||
|
resolveFilePath(aFilePath, new HashMap<String, Object>());
|
||||||
|
}
|
||||||
|
|
||||||
|
public ExcelHashTable(String aFilePath, Map<String, Object> pParams) {
|
||||||
|
super(pParams);
|
||||||
|
|
||||||
|
SimpleDateFormat defaultDateFormat = (SimpleDateFormat) pParams.getOrDefault("DateFormat",
|
||||||
|
EXCEL_DATETIME_FORMAT);
|
||||||
|
|
||||||
|
this.setDefaultDateFormat(defaultDateFormat);
|
||||||
|
resolveFilePath(aFilePath, pParams);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ExcelHashTable(String aFilePath) {
|
||||||
|
this(aFilePath, null, EXCEL_DATETIME_FORMAT);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ExcelHashTable() {
|
||||||
|
this(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public HashTable getSheet(String pSheetName) {
|
||||||
|
return excelSheets.get(pSheetName);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addSheet(String pSheetName) {
|
||||||
|
this.excelSheets.put(pSheetName, this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addSheet(String pSheetName, HashTable pSheetTable) {
|
||||||
|
this.excelSheets.put(pSheetName, pSheetTable);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void saveAs(String pFilePath) throws IOException {
|
||||||
|
File aFile = new File(pFilePath);
|
||||||
|
|
||||||
|
if (excelSheets.isEmpty()) {
|
||||||
|
excelSheets.put(EXCEL_DEFAULT_SHEET_NAME, this);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (String sheetName : excelSheets.keySet()) {
|
||||||
|
HashTable aSheetTable = excelSheets.get(sheetName);
|
||||||
|
ExcelTable excelSheet = workbook.addSheet(sheetName);
|
||||||
|
|
||||||
|
Set<String> columnNames = aSheetTable.getColumnNames();
|
||||||
|
Object[] columnList = columnNames.toArray();
|
||||||
|
int numberOfColumns = columnNames.size();
|
||||||
|
|
||||||
|
int newRow = excelSheet.newRow();
|
||||||
|
|
||||||
|
// write header row
|
||||||
|
for (int i = 0; i < numberOfColumns; i++) {
|
||||||
|
String colName = (String) columnList[i];
|
||||||
|
|
||||||
|
excelSheet.setCellValue(newRow, colName, colName);
|
||||||
|
}
|
||||||
|
|
||||||
|
// write data
|
||||||
|
for (int rowNo = 0; rowNo < aSheetTable.lenght(); rowNo++) {
|
||||||
|
newRow = excelSheet.newRow();
|
||||||
|
|
||||||
|
for (int i = 0; i < numberOfColumns; i++) {
|
||||||
|
String colName = (String) columnList[i];
|
||||||
|
|
||||||
|
Object valueObject = aSheetTable.getCellValue(rowNo, colName);
|
||||||
|
|
||||||
|
if (valueObject == null) {
|
||||||
|
valueObject = aSheetTable.getDefaultValue();
|
||||||
|
|
||||||
|
if (valueObject == null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
excelSheet.setCellValue(rowNo, colName, valueObject);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
CsvTable.createDirectoriesInPath(aFile);
|
||||||
|
workbook.save();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void save() throws IOException {
|
||||||
|
saveAs(this.filePath);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void readColumNames() throws IOException {
|
||||||
|
String text = LcagFileTools.readTextFile(filePath);
|
||||||
|
String eol = this.getEol();
|
||||||
|
String headline = text.substring(0, text.indexOf(eol));
|
||||||
|
|
||||||
|
String[] cols = headline.split(getSeparator().trim());
|
||||||
|
|
||||||
|
for (String col : cols) {
|
||||||
|
col = col.trim();
|
||||||
|
|
||||||
|
this.addColumn(col);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void read() throws IOException {
|
||||||
|
|
||||||
|
workbook = new ExcelWorkbook(filePath);
|
||||||
|
// Workbook workbook =
|
||||||
|
// StreamingReader.builder().rowCacheSize(100).bufferSize(4096).open(inputStream);
|
||||||
|
|
||||||
|
Integer rowNoLimitInt = getOption(OPTION_LIMIT_NUMBER_ROWS);
|
||||||
|
int rowNoLimit = rowNoLimitInt == null ? -1 : rowNoLimitInt;
|
||||||
|
|
||||||
|
workbook.read();
|
||||||
|
|
||||||
|
int noRows = Math.min(workbook.lenght(), rowNoLimit);
|
||||||
|
Collection<String> columns = workbook.getColumnLabels();
|
||||||
|
|
||||||
|
for (int rowNo = 1; rowNo < noRows; rowNo++) {
|
||||||
|
for (String colName : columns) {
|
||||||
|
if (useColumn(colName)) {
|
||||||
|
Object value = workbook.getCellValue(rowNo, colName);
|
||||||
|
this.setCellValue(rowNo, colName, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
workbook.setKey(rowNo);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,64 @@
|
|||||||
|
package de.lcag.common;
|
||||||
|
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import routines.LcagGeneral;
|
||||||
|
|
||||||
|
public class GenericComparisonResultTable extends HashTable {
|
||||||
|
public static final String FILE_PATH_COLUMN = "FilePath";
|
||||||
|
public static final String SOURCE_ROW_NUMBER_COLUMN = "Src_RowNo";
|
||||||
|
public static final String COMPARE_ROW_NUMBER_COLUMN = "Cmp_RowNo";
|
||||||
|
public static final String COLUMN_NAME_COLUMN = "Col_Name";
|
||||||
|
public static final String SOURCE_VALUE_COLUMN = "Src_Value";
|
||||||
|
public static final String COMPARE_VALUE_COLUMN = "Cmp_Value";
|
||||||
|
public static final String SOURCE_COLUMN_NUMBER_COLUMN = "Src_ColNo";
|
||||||
|
public static final String COMPARE_COLUMN_NUMBER_COLUMN = "Cmp_ColNo";
|
||||||
|
|
||||||
|
private AbstractTable sourceTable = null;
|
||||||
|
private AbstractTable compareTable = null;
|
||||||
|
|
||||||
|
public GenericComparisonResultTable(AbstractTable pSrcTable, AbstractTable pCmpTable) {
|
||||||
|
super(KEY_COLUMN, SOURCE_ROW_NUMBER_COLUMN, COMPARE_ROW_NUMBER_COLUMN, COLUMN_NAME_COLUMN, SOURCE_VALUE_COLUMN,
|
||||||
|
COMPARE_VALUE_COLUMN, SOURCE_COLUMN_NUMBER_COLUMN, COMPARE_COLUMN_NUMBER_COLUMN);
|
||||||
|
|
||||||
|
this.sourceTable = pSrcTable;
|
||||||
|
this.compareTable = pCmpTable;
|
||||||
|
|
||||||
|
compareTables();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void compareTables() {
|
||||||
|
int noRows = sourceTable.lenght();
|
||||||
|
Set<String> columnNames = sourceTable.getColumnNames();
|
||||||
|
Set<String> compareColumnNames = compareTable.getColumnNames();
|
||||||
|
|
||||||
|
for (int srcRowNumber = 0; srcRowNumber < noRows; srcRowNumber++) {
|
||||||
|
int columnNumber = 0;
|
||||||
|
|
||||||
|
for (String columnName : columnNames) {
|
||||||
|
Object srcValue = sourceTable.getCellValue(srcRowNumber, columnName);
|
||||||
|
Object cmpValue = null;
|
||||||
|
|
||||||
|
if (compareColumnNames.contains(columnName)) {
|
||||||
|
cmpValue = compareTable.getCellValue(srcRowNumber, columnName);
|
||||||
|
}
|
||||||
|
|
||||||
|
String compareResult = LcagGeneral.compareValue(srcValue, cmpValue);
|
||||||
|
int n = this.newRow();
|
||||||
|
|
||||||
|
columnNumber += 1;
|
||||||
|
|
||||||
|
this.setCellValue(n, KEY_COLUMN, srcRowNumber);
|
||||||
|
this.setCellValue(n, SOURCE_ROW_NUMBER_COLUMN, srcRowNumber);
|
||||||
|
this.setCellValue(n, COMPARE_ROW_NUMBER_COLUMN, srcRowNumber);
|
||||||
|
this.setCellValue(n, COLUMN_NAME_COLUMN, columnName);
|
||||||
|
this.setCellValue(n, SOURCE_VALUE_COLUMN, srcValue);
|
||||||
|
this.setCellValue(n, COMPARE_VALUE_COLUMN, compareResult);
|
||||||
|
this.setCellValue(n, SOURCE_COLUMN_NUMBER_COLUMN, columnNumber);
|
||||||
|
this.setCellValue(n, COMPARE_COLUMN_NUMBER_COLUMN, columnNumber);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,52 @@
|
|||||||
|
package de.lcag.common;
|
||||||
|
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
public class GenericTable extends HashTable {
|
||||||
|
private static final String KEY_COLUMN = "Key";
|
||||||
|
private static final String ROW_NUMBER_COLUMN = "RowNo";
|
||||||
|
private static final String COLUMN_NUMBER_COLUMN = "ColNo";
|
||||||
|
private static final String COLUMN_NAME_COLUMN = "ColName";
|
||||||
|
private static final String COLUMN_VALUE_COLUMN = "ColValue";
|
||||||
|
AbstractTable sourceTable = null;
|
||||||
|
private char encloseBy = '"';
|
||||||
|
|
||||||
|
public GenericTable(AbstractTable pSrcTable) {
|
||||||
|
super(KEY_COLUMN, ROW_NUMBER_COLUMN, COLUMN_NUMBER_COLUMN, COLUMN_NAME_COLUMN, COLUMN_VALUE_COLUMN);
|
||||||
|
|
||||||
|
this.sourceTable = pSrcTable;
|
||||||
|
|
||||||
|
convertToGenericTable();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void convertToGenericTable() {
|
||||||
|
AbstractTable srcTable = this.sourceTable;
|
||||||
|
int noRows = srcTable.lenght();
|
||||||
|
Set<String> columnNames = srcTable.getColumnNames();
|
||||||
|
|
||||||
|
for (int srcRowNumber = 0; srcRowNumber < noRows; srcRowNumber++) {
|
||||||
|
int columnNumber = 0;
|
||||||
|
|
||||||
|
for (String columnName : columnNames) {
|
||||||
|
int n = this.newRow();
|
||||||
|
Object cellValue = srcTable.getCellValue(srcRowNumber, columnName);
|
||||||
|
|
||||||
|
columnNumber += 1;
|
||||||
|
|
||||||
|
this.setCellValue(n, KEY_COLUMN, srcRowNumber);
|
||||||
|
this.setCellValue(n, ROW_NUMBER_COLUMN, srcRowNumber);
|
||||||
|
this.setCellValue(n, COLUMN_NUMBER_COLUMN, columnNumber);
|
||||||
|
this.setCellValue(n, COLUMN_NAME_COLUMN, columnName);
|
||||||
|
this.setCellValue(n, COLUMN_VALUE_COLUMN, cellValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getKey(int pRowNo) {
|
||||||
|
String result = this.getCellValue(pRowNo, KEY_COLUMN).toString();
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,123 @@
|
|||||||
|
package de.lcag.common;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import javax.management.OperationsException;
|
||||||
|
|
||||||
|
import routines.LcagDateTime;
|
||||||
|
|
||||||
|
public class HashTable extends AbstractTable {
|
||||||
|
|
||||||
|
public static final String DEFAULT_TABLE_NAME = "Unnamed Table";
|
||||||
|
|
||||||
|
private String name = DEFAULT_TABLE_NAME;
|
||||||
|
|
||||||
|
public HashTable() {
|
||||||
|
super();
|
||||||
|
this.setDefaultDateFormat(LcagDateTime.DATETIME_FORMAT_ISO8601_UTC);
|
||||||
|
}
|
||||||
|
|
||||||
|
public HashTable(String... aColumnNameList) {
|
||||||
|
this();
|
||||||
|
|
||||||
|
this.addColumns(aColumnNameList);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Take over structure (column names) but no data from another table
|
||||||
|
*
|
||||||
|
* @param pSrcTable
|
||||||
|
*/
|
||||||
|
public HashTable(AbstractTable pSrcTable) {
|
||||||
|
super();
|
||||||
|
this.defaultValue = pSrcTable.defaultValue;
|
||||||
|
this.defaultString = pSrcTable.defaultString;
|
||||||
|
this.defaultDateFormat = pSrcTable.defaultDateFormat;
|
||||||
|
|
||||||
|
Set<String> columnNames = pSrcTable.getColumnNames();
|
||||||
|
this.addColumns(columnNames);
|
||||||
|
}
|
||||||
|
|
||||||
|
public HashTable(Object pDefaultValue, SimpleDateFormat pDefaultDateFormat) {
|
||||||
|
super();
|
||||||
|
this.defaultValue = pDefaultValue;
|
||||||
|
this.defaultString = String.valueOf((String) defaultValue); // results in default java "null"
|
||||||
|
|
||||||
|
this.setDefaultDateFormat(pDefaultDateFormat);
|
||||||
|
}
|
||||||
|
|
||||||
|
public HashTable(Map<String, Object> pParams) {
|
||||||
|
super();
|
||||||
|
SimpleDateFormat defaultDateFormat = (SimpleDateFormat) pParams.getOrDefault("DateFormat",
|
||||||
|
LcagDateTime.DATETIME_FORMAT_ISO8601_UTC);
|
||||||
|
Object defaultValue = pParams.get("DefaultValue");
|
||||||
|
|
||||||
|
this.setDefaultDateFormat(defaultDateFormat);
|
||||||
|
this.setDefaultValue(defaultValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
if (name != null) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCellValue(int row, int colNo, Object v) {
|
||||||
|
try {
|
||||||
|
Collection<String> colNames = this.getColumnLabels();
|
||||||
|
Object[] colNameList = colNames.toArray();
|
||||||
|
|
||||||
|
String colName = (String) colNameList[colNo];
|
||||||
|
|
||||||
|
this.setCellValue(row, colName, v);
|
||||||
|
} catch (IndexOutOfBoundsException e) {
|
||||||
|
int n = this.getColumnNames().size();
|
||||||
|
|
||||||
|
if (v == null || "".equals(v.toString().trim())) {
|
||||||
|
log.warn(
|
||||||
|
"setCellValue(row %d, col %d): Column number %d does not exist (max is %d) => ignored setting to empty or null value",
|
||||||
|
row, colNo, colNo, n - 1);
|
||||||
|
} else {
|
||||||
|
String msg = String.format(
|
||||||
|
"setCellValue(row %d, col %d): Column number %d does not exist (max is %d) => exception as cannot set to value '%s'",
|
||||||
|
row, colNo, colNo, n - 1, v.toString());
|
||||||
|
throw new IndexOutOfBoundsException(msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void register() throws OperationsException {
|
||||||
|
Table.register(name, this);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void integrateDiff(TableComparisonResult pDiff) throws IOException {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Path getFilePath() {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addRow(Object... values) {
|
||||||
|
int rowNo = this.newRow();
|
||||||
|
int colNo = 0;
|
||||||
|
|
||||||
|
for (Object value : values) {
|
||||||
|
this.setCellValue(rowNo, colNo++, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,157 @@
|
|||||||
|
package de.lcag.common;
|
||||||
|
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Properties;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import routines.LcagFileTools;
|
||||||
|
import routines.LcagLogger;
|
||||||
|
import routines.LcagStringTools;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Implements Java properties whose values can contain variables point to other property variables or operating system environment variables
|
||||||
|
* First, it checks if variable is defined in the property file itself. If not it checks if it can be resolved as system OS variable
|
||||||
|
* Variables are reference as ${varName} in the values.
|
||||||
|
*/
|
||||||
|
public class LcagProperties {
|
||||||
|
private static LcagLogger logger = LcagLogger.getLogger(LcagProperties.class);
|
||||||
|
|
||||||
|
// private Class<?> clazz = null;
|
||||||
|
private String fileName = null;
|
||||||
|
private Properties properties = null;
|
||||||
|
private String propFilePath = null;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Use this to locate the properties file via the class loader.
|
||||||
|
*/
|
||||||
|
// public LcagProperties(Class<?> pClass, String pFileName) {
|
||||||
|
// this.clazz = pClass;
|
||||||
|
// this.fileName = pFileName;
|
||||||
|
// }
|
||||||
|
|
||||||
|
public LcagProperties(String pPropFilePath, String pFileName) {
|
||||||
|
this.propFilePath = pPropFilePath;
|
||||||
|
this.fileName = pFileName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public LcagProperties(String pPropFilePath) {
|
||||||
|
this.fileName = LcagFileTools.getName(pPropFilePath);
|
||||||
|
this.propFilePath = LcagFileTools.getPath(pPropFilePath);
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getProperty(String pPropertyName, boolean pIsOptional) {
|
||||||
|
String result = null;
|
||||||
|
String formatPropPath = null;
|
||||||
|
|
||||||
|
load();
|
||||||
|
|
||||||
|
if (this.properties != null) {
|
||||||
|
result = this.properties.getProperty(pPropertyName);
|
||||||
|
|
||||||
|
if (result == null) {
|
||||||
|
if (pIsOptional) {
|
||||||
|
logger.debug("Property '%s' not defined in properties file %s", pPropertyName, this.fileName);
|
||||||
|
} else {
|
||||||
|
logger.error("Property '%s' not defined in properties file %s", pPropertyName, this.fileName);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
result = LcagStringTools.resolveVariables(result, this.properties);
|
||||||
|
result = LcagStringTools.resolveEnvironmentVariables(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<String, String> getProperties() {
|
||||||
|
Map<String, String> result = new HashMap<>();
|
||||||
|
|
||||||
|
load();
|
||||||
|
final Set<String> propertyNames = this.properties.stringPropertyNames();
|
||||||
|
|
||||||
|
for (String p : propertyNames) {
|
||||||
|
String value = this.getProperty(p);
|
||||||
|
|
||||||
|
result.put(p, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void load() {
|
||||||
|
String formatPropPath;
|
||||||
|
|
||||||
|
if (this.properties == null) {
|
||||||
|
try {
|
||||||
|
String filePath = this.propFilePath + this.fileName;
|
||||||
|
|
||||||
|
formatPropPath = LcagStringTools.resolveEnvironmentVariables(filePath);
|
||||||
|
logger.info("Properties file path =%s", formatPropPath);
|
||||||
|
InputStream input = new FileInputStream(formatPropPath);
|
||||||
|
|
||||||
|
this.properties = new Properties();
|
||||||
|
|
||||||
|
if (input == null)
|
||||||
|
logger.error("Sorry, unable to find property file %s", this.fileName);
|
||||||
|
|
||||||
|
this.properties.load(input);
|
||||||
|
} catch (IOException ex) {
|
||||||
|
logger.error("Error while reading properties file %s\n" + ex, this.fileName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getProperty(String pPropertyName) {
|
||||||
|
return getProperty(pPropertyName, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getOptionalProperty(String pPropertyName) {
|
||||||
|
return getProperty(pPropertyName, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Path getFilePathProperty(String pPropertyName) {
|
||||||
|
Path result = null;
|
||||||
|
String path = getProperty(pPropertyName);
|
||||||
|
|
||||||
|
if (LcagStringTools.isEmpty(path)) {
|
||||||
|
logger.error("File path cannot be an empty string");
|
||||||
|
} else {
|
||||||
|
result = Path.of(path);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Path getOptionalFilePathProperty(String pPropertyName) {
|
||||||
|
Path result = null;
|
||||||
|
String path = getOptionalProperty(pPropertyName);
|
||||||
|
|
||||||
|
if (!LcagStringTools.isEmpty(path))
|
||||||
|
result = Path.of(path);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getProperty(String pPropertyName, String pDefaultValue) {
|
||||||
|
String result = this.getProperty(pPropertyName, false);
|
||||||
|
|
||||||
|
if (result == null)
|
||||||
|
result = pDefaultValue;
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String resolveIn(String pText) {
|
||||||
|
String result = null;
|
||||||
|
|
||||||
|
final Map<String, String> resolvedProperties = this.getProperties();
|
||||||
|
result = LcagStringTools.resolveVariables(pText, resolvedProperties);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
package de.lcag.common;
|
||||||
|
|
||||||
|
public class ListAllFiles {
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,90 @@
|
|||||||
|
package de.lcag.common;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import javax.naming.ConfigurationException;
|
||||||
|
|
||||||
|
import org.apache.commons.collections4.map.HashedMap;
|
||||||
|
|
||||||
|
import routines.LcagStringTools;
|
||||||
|
|
||||||
|
public class MappingTable {
|
||||||
|
private static final String SOURCE_FIELD_NAME_COLUMN = "Src_FieldName";
|
||||||
|
private static final String EXISTING_FIELD_NAME_COLUMN = "Existing_FieldName";
|
||||||
|
|
||||||
|
private HashTable mappingTable = null;
|
||||||
|
private Map<String, String> columnMap = new HashedMap<>();
|
||||||
|
|
||||||
|
public MappingTable(String pMapping, String pPairSeperator, String pKeyValueSeperator)
|
||||||
|
throws ConfigurationException {
|
||||||
|
Map<String, Object> map = LcagStringTools.asMap(pMapping, pPairSeperator, pKeyValueSeperator);
|
||||||
|
HashTable table = new HashTable(SOURCE_FIELD_NAME_COLUMN, EXISTING_FIELD_NAME_COLUMN);
|
||||||
|
|
||||||
|
for (String key : map.keySet()) {
|
||||||
|
int rowNo = table.newRow();
|
||||||
|
Object value = map.get(key);
|
||||||
|
|
||||||
|
table.setCellValue(rowNo, SOURCE_FIELD_NAME_COLUMN, key);
|
||||||
|
table.setCellValue(rowNo, EXISTING_FIELD_NAME_COLUMN, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
createHashTable(table);
|
||||||
|
}
|
||||||
|
|
||||||
|
public MappingTable(HashTable pMappingTable) throws ConfigurationException {
|
||||||
|
createHashTable(pMappingTable);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void createHashTable(HashTable pMappingTable) throws ConfigurationException {
|
||||||
|
this.mappingTable = pMappingTable;
|
||||||
|
|
||||||
|
for (String colName : List.of(SOURCE_FIELD_NAME_COLUMN, EXISTING_FIELD_NAME_COLUMN)) {
|
||||||
|
if (!mappingTable.hasColumn(colName)) {
|
||||||
|
throw new ConfigurationException(
|
||||||
|
String.format("Invalid configuration table: No Column '%s' provided", colName));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < mappingTable.lenght(); i++) {
|
||||||
|
String srcKey = (String) mappingTable.getCellValue(i, SOURCE_FIELD_NAME_COLUMN);
|
||||||
|
String existingColumnName = (String) mappingTable.getCellValue(i, EXISTING_FIELD_NAME_COLUMN);
|
||||||
|
|
||||||
|
if (LcagStringTools.isEmpty(srcKey) || LcagStringTools.isEmpty(existingColumnName))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
String col = this.columnMap.get(srcKey);
|
||||||
|
|
||||||
|
if (col == null) {
|
||||||
|
this.columnMap.put(srcKey, existingColumnName);
|
||||||
|
} else if (!col.equals(existingColumnName)) {
|
||||||
|
throw new ConfigurationException(String.format(
|
||||||
|
"Invalid configuration in table, row %d: Column '%s' already mapped to '%s', cannot map to '%s'",
|
||||||
|
i, srcKey, col, existingColumnName));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getColumnLableFor(String pAliasName) {
|
||||||
|
String result = columnMap.get(pAliasName);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String asHTML() {
|
||||||
|
return mappingTable.asHTML();
|
||||||
|
}
|
||||||
|
|
||||||
|
public int lenght() {
|
||||||
|
return mappingTable.lenght();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String asCsvData() {
|
||||||
|
return mappingTable.asCsvData();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String asCSV() {
|
||||||
|
return mappingTable.asCSV();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,153 @@
|
|||||||
|
package de.lcag.common;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.TreeMap;
|
||||||
|
|
||||||
|
import org.apache.commons.collections4.map.ListOrderedMap;
|
||||||
|
|
||||||
|
public class PositionOrderedMap<K, V> extends ListOrderedMap<K, V> {
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
private int currentPosition = 0;
|
||||||
|
private HashMap<V, Integer> value2Position = new HashMap<>();
|
||||||
|
private TreeMap<Integer, K> position2Key = new TreeMap<>(new HashMap<>());
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public V put(K pKey, V pValue) {
|
||||||
|
if (value2Position.values().contains(currentPosition)) {
|
||||||
|
throw new IllegalArgumentException();
|
||||||
|
}
|
||||||
|
|
||||||
|
value2Position.put(pValue, currentPosition);
|
||||||
|
position2Key.put(currentPosition, pKey);
|
||||||
|
currentPosition += 1;
|
||||||
|
|
||||||
|
return super.put(pKey, pValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public V put(int pPosition, K pKey, V pValue) {
|
||||||
|
if (value2Position.values().contains(pPosition)) {
|
||||||
|
throw new IllegalArgumentException();
|
||||||
|
}
|
||||||
|
|
||||||
|
value2Position.put(pValue, pPosition);
|
||||||
|
position2Key.put(pPosition, pKey);
|
||||||
|
currentPosition += 1;
|
||||||
|
|
||||||
|
return super.put(pPosition, pKey, pValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getPositionOfValue(V pValue) {
|
||||||
|
return this.value2Position.get(pValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getPositionOfKey(K pKey) {
|
||||||
|
V value = this.get(pKey);
|
||||||
|
return this.value2Position.get(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getPositionOfLastElement() {
|
||||||
|
K key = this.lastKey();
|
||||||
|
V value = this.get(key);
|
||||||
|
|
||||||
|
return value2Position.get(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public K getKeyByPosition(int pPosition) {
|
||||||
|
return this.position2Key.get(pPosition);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer shiftPositionByKey(K pKey) {
|
||||||
|
V v = this.get(pKey);
|
||||||
|
|
||||||
|
return shiftPositionByValue(v);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer shiftPositionByValue(V v) {
|
||||||
|
Integer pos = this.value2Position.get(v) + 1;
|
||||||
|
|
||||||
|
return shiftPosition(pos);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer shiftPosition(int pPosition) {
|
||||||
|
K key = this.position2Key.get(pPosition);
|
||||||
|
|
||||||
|
if (key == null) {
|
||||||
|
throw new IllegalArgumentException("Position to shift not in use");
|
||||||
|
}
|
||||||
|
|
||||||
|
V value = this.get(key);
|
||||||
|
int resultNewPosition = pPosition + 1;
|
||||||
|
K keyAtNewPos = this.position2Key.get(resultNewPosition);
|
||||||
|
|
||||||
|
if (keyAtNewPos != null) {
|
||||||
|
throw new IllegalArgumentException("Target position already in use");
|
||||||
|
}
|
||||||
|
|
||||||
|
this.value2Position.put(value, resultNewPosition);
|
||||||
|
this.position2Key.remove(pPosition);
|
||||||
|
this.position2Key.put(resultNewPosition, key);
|
||||||
|
|
||||||
|
return resultNewPosition;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int shiftAllPositions(int pStartPosition) {
|
||||||
|
int lastPosition = this.getPositionOfLastElement();
|
||||||
|
int resultNoShiftedElements = 0;
|
||||||
|
|
||||||
|
System.out.println(this);
|
||||||
|
for (int i = lastPosition; i >= pStartPosition; i--) {
|
||||||
|
if (this.position2Key.get(i) == null)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
this.shiftPosition(i);
|
||||||
|
resultNoShiftedElements += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return resultNoShiftedElements;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public V remove(int pIndex) {
|
||||||
|
throw new UnsupportedOperationException("Not yet implemented");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public V remove(Object pObject) {
|
||||||
|
throw new UnsupportedOperationException("Not yet implemented");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean remove(Object pKey, Object pValue) {
|
||||||
|
throw new UnsupportedOperationException("Not yet implemented");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public V replace(Object pKey, Object pValue) {
|
||||||
|
throw new UnsupportedOperationException("Not yet implemented");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean replace(Object pKey, Object pOldValue, Object pNewValue) {
|
||||||
|
throw new UnsupportedOperationException("Not yet implemented");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
|
||||||
|
this.forEach((K key, V value) -> {
|
||||||
|
Integer v2p = this.value2Position.get(value);
|
||||||
|
K p2k = this.position2Key.get(v2p);
|
||||||
|
|
||||||
|
if (v2p == null)
|
||||||
|
v2p = -1;
|
||||||
|
|
||||||
|
sb.append(String.format("Index %d: Key %10s: value=%10s, v2p=%d, p2k=%s\n", this.indexOf(key),
|
||||||
|
key.toString(), value.toString(), v2p, p2k == null ? "(null)" : p2k.toString()));
|
||||||
|
});
|
||||||
|
|
||||||
|
String result = sb.toString();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
package de.lcag.common;
|
||||||
|
|
||||||
|
import java.time.Instant;
|
||||||
|
|
||||||
|
public class RuntimeTable extends CsvTable {
|
||||||
|
private Instant startTime = Instant.now();
|
||||||
|
|
||||||
|
public RuntimeTable(String aFilePath) {
|
||||||
|
super(aFilePath);
|
||||||
|
|
||||||
|
this.addColumns("StartTime", "Duration", "Comment");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addRecord(String aComment) {
|
||||||
|
int rowNo = this.lenght() + 1;
|
||||||
|
Instant endTime = Instant.now();
|
||||||
|
Long duration = endTime.toEpochMilli() - this.startTime.toEpochMilli();
|
||||||
|
|
||||||
|
this.setCellValue(rowNo, "StartTime", startTime);
|
||||||
|
this.setCellValue(rowNo, "Duration", duration);
|
||||||
|
this.setCellValue(rowNo, "Comment", aComment);
|
||||||
|
|
||||||
|
// Measure next item:
|
||||||
|
this.startTime = Instant.now();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,265 @@
|
|||||||
|
package de.lcag.common;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
import routines.LcagStringTools;
|
||||||
|
|
||||||
|
public class SelectStatement {
|
||||||
|
private static Pattern COND_PATTERN = Pattern
|
||||||
|
.compile("\\s*([\\w\\d_\\.]+)\\s*(=|<=|>=|!=)\\s*([\\w\\d\\-_'\\.]+\\s*)", Pattern.CASE_INSENSITIVE);
|
||||||
|
private static Pattern SELECT_PATTERN = Pattern.compile("^\\s*SELECT\\s+(.+?)\\s+FROM\\s+([\\w\\-_]+)(\\s+\\w+)?",
|
||||||
|
Pattern.CASE_INSENSITIVE);
|
||||||
|
private static Pattern AS_PATTERN = Pattern.compile("^\\s*(.+?)\\s+AS\\s+(\\w+)", Pattern.CASE_INSENSITIVE);
|
||||||
|
private static Pattern JOIN_PATTERN = Pattern.compile(
|
||||||
|
"(LEFT JOIN|JOIN)\\s+([\\w\\-_]+)(\\s+\\w+)?\\s*ON\\s+(.*)\\s*(WHERE|$)", Pattern.CASE_INSENSITIVE);
|
||||||
|
private static Pattern WHERE_PATTERN = Pattern.compile("WHERE\\s+(.*)", Pattern.CASE_INSENSITIVE);
|
||||||
|
private static Pattern BOOL_EXPRESSION_PATTERN = Pattern.compile("\\s*(.+)\\s+(AND|OR)\\s+(.+)\\s*",
|
||||||
|
Pattern.CASE_INSENSITIVE);
|
||||||
|
|
||||||
|
private Map<String, String> columns = new LinkedHashMap<>();
|
||||||
|
private Table fromTable = new Table();
|
||||||
|
private List<Join> joinOn;
|
||||||
|
private List<Condition> whereClause = new ArrayList<>();
|
||||||
|
|
||||||
|
class Table {
|
||||||
|
public String name;
|
||||||
|
public String var = null;;
|
||||||
|
|
||||||
|
public void setVariable(String group) {
|
||||||
|
if (group != null)
|
||||||
|
this.var = group.trim();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Condition {
|
||||||
|
public String arg1;
|
||||||
|
public String operator;
|
||||||
|
public Object arg2;
|
||||||
|
|
||||||
|
Condition(String arg1, String operator, Object arg2) {
|
||||||
|
this.arg1 = arg1;
|
||||||
|
this.operator = operator;
|
||||||
|
this.arg2 = arg2;
|
||||||
|
}
|
||||||
|
|
||||||
|
Condition(String pCondition) {
|
||||||
|
Matcher m = COND_PATTERN.matcher(pCondition);
|
||||||
|
|
||||||
|
if (m.find()) {
|
||||||
|
this.arg1 = m.group(1);
|
||||||
|
this.operator = m.group(2);
|
||||||
|
this.arg2 = convertToType(m.group(3));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public String toString() {
|
||||||
|
return String.format("%s %s %s", arg1, operator, arg2.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Join {
|
||||||
|
public Table table = new Table();
|
||||||
|
private List<Condition> conditions = new ArrayList<>();
|
||||||
|
|
||||||
|
Join(String pJoin) {
|
||||||
|
}
|
||||||
|
|
||||||
|
List<Join> getJoins(String pJoin) {
|
||||||
|
List<Join> result = new ArrayList<>();
|
||||||
|
Matcher m = JOIN_PATTERN.matcher(pJoin);
|
||||||
|
|
||||||
|
while (m.find()) {
|
||||||
|
int n = m.groupCount();
|
||||||
|
Matcher m2 = COND_PATTERN.matcher(m.group(n));
|
||||||
|
|
||||||
|
this.table.name = m.group(1);
|
||||||
|
|
||||||
|
if (n > 2) {
|
||||||
|
this.table.var = m.group(2);
|
||||||
|
}
|
||||||
|
|
||||||
|
extractConditions(this.conditions, m2);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public SelectStatement(String pSelect) {
|
||||||
|
Matcher m = SELECT_PATTERN.matcher(pSelect);
|
||||||
|
|
||||||
|
if (m.find()) {
|
||||||
|
int n = m.groupCount();
|
||||||
|
String s = m.group(1);
|
||||||
|
|
||||||
|
fromTable.name = m.group(2);
|
||||||
|
fromTable.setVariable(m.group(3));
|
||||||
|
|
||||||
|
if (fromTable.name == null) {
|
||||||
|
fromTable.name = m.group(2);
|
||||||
|
}
|
||||||
|
|
||||||
|
String tableVarUpper = (fromTable.var == null) ? "" : fromTable.var.trim().toUpperCase();
|
||||||
|
|
||||||
|
if ("".equals(tableVarUpper) || "WHERE".equals(tableVarUpper) || "JOIN".equals(tableVarUpper)) {
|
||||||
|
fromTable.var = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
extractColumnNames(s);
|
||||||
|
|
||||||
|
extractWhereConditions(pSelect);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public String toString() {
|
||||||
|
String result = "SELECT ";
|
||||||
|
List<String> aList = new ArrayList<>();
|
||||||
|
|
||||||
|
for (String targetName : columns.keySet()) {
|
||||||
|
String sourceName = columns.get(targetName);
|
||||||
|
|
||||||
|
if (targetName.equals(sourceName)) {
|
||||||
|
aList.add(sourceName);
|
||||||
|
} else {
|
||||||
|
aList.add(sourceName + " AS " + targetName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
result += LcagStringTools.joinIgnoreNull(" AND ", aList);
|
||||||
|
result += "FROM " + fromTable.name + (fromTable.var == null ? "" : "AS " + fromTable.var);
|
||||||
|
|
||||||
|
if (!whereClause.isEmpty()) {
|
||||||
|
result += "WHERE " + LcagStringTools.joinIgnoreNull(" AND ", whereClause);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void extractWhereConditions(String pWhere) {
|
||||||
|
Matcher m = WHERE_PATTERN.matcher(pWhere);
|
||||||
|
|
||||||
|
if (m.find()) {
|
||||||
|
List<Condition> whereCondList = this.whereClause;
|
||||||
|
extractConditions(whereCondList, m);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void extractConditions(List<Condition> pResultList, Matcher pMatcher) {
|
||||||
|
String clause = pMatcher.group(1);
|
||||||
|
boolean boolExprFound = false;
|
||||||
|
|
||||||
|
pMatcher = BOOL_EXPRESSION_PATTERN.matcher(clause);
|
||||||
|
|
||||||
|
while (pMatcher.find()) {
|
||||||
|
int n = pMatcher.groupCount();
|
||||||
|
|
||||||
|
boolExprFound = true;
|
||||||
|
|
||||||
|
if (n == 3) {
|
||||||
|
String expr1 = pMatcher.group(1);
|
||||||
|
String opr = pMatcher.group(2).toUpperCase();
|
||||||
|
String expr2 = pMatcher.group(3);
|
||||||
|
|
||||||
|
if ("AND".equals(opr)) {
|
||||||
|
Condition c1 = new Condition(expr1);
|
||||||
|
Condition c2 = new Condition(expr2);
|
||||||
|
|
||||||
|
pResultList.add(c1);
|
||||||
|
pResultList.add(c2);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!boolExprFound) {
|
||||||
|
Condition c1 = new Condition(clause);
|
||||||
|
pResultList.add(c1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Object convertToType(String pStringValue) {
|
||||||
|
Object result = pStringValue;
|
||||||
|
|
||||||
|
try {
|
||||||
|
Double d = Double.parseDouble(pStringValue);
|
||||||
|
final boolean isInt = (d % 1 == 0);
|
||||||
|
|
||||||
|
if (isInt) {
|
||||||
|
result = Integer.parseInt(pStringValue);
|
||||||
|
} else {
|
||||||
|
result = d;
|
||||||
|
}
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
int i1 = pStringValue.indexOf("'");
|
||||||
|
int i2 = pStringValue.lastIndexOf("'");
|
||||||
|
|
||||||
|
if (i1 >= 0 && i2 > i1) {
|
||||||
|
result = pStringValue.substring(i1 + 1, i2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void extractColumnNames(String colNameList) {
|
||||||
|
String[] parts = colNameList.split("\\s*,\\s*");
|
||||||
|
|
||||||
|
for (String p : parts) {
|
||||||
|
Matcher c = AS_PATTERN.matcher(p);
|
||||||
|
String targetColName = p;
|
||||||
|
String srcColName = p;
|
||||||
|
boolean doRename = c.find();
|
||||||
|
|
||||||
|
if (doRename) {
|
||||||
|
srcColName = c.group(1);
|
||||||
|
targetColName = c.group(2);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (fromTable.var != null && srcColName.startsWith(fromTable.var + ".")) {
|
||||||
|
// Column name is addressed by a variable and it matches the table variable of
|
||||||
|
// FROM clause
|
||||||
|
|
||||||
|
int n = fromTable.var.length();
|
||||||
|
|
||||||
|
if (!doRename) {
|
||||||
|
int n2 = srcColName.indexOf('.');
|
||||||
|
|
||||||
|
targetColName = n2 >= 0 ? srcColName.substring(n2 + 1) : srcColName;
|
||||||
|
}
|
||||||
|
|
||||||
|
columns.put(targetColName, srcColName);
|
||||||
|
} else if (fromTable.var == null && srcColName.indexOf('.') < 0) {
|
||||||
|
// Column name is NOT addressed by a variable, so take it
|
||||||
|
columns.put(targetColName, srcColName);
|
||||||
|
} else {
|
||||||
|
// Column name is addressed by a variable but it does NOT match the table
|
||||||
|
// variable of FROM clause => ignore it
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public final Map<String, String> getColumns() {
|
||||||
|
return columns;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getFromTable() {
|
||||||
|
return fromTable.name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getFromTableVariable() {
|
||||||
|
return fromTable.var;
|
||||||
|
}
|
||||||
|
|
||||||
|
public final List<Join> getJoinOn() {
|
||||||
|
return joinOn;
|
||||||
|
}
|
||||||
|
|
||||||
|
public final List<Condition> getWhereClause() {
|
||||||
|
return whereClause;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
package de.lcag.common;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public interface Storage {
|
||||||
|
void save() throws IOException;
|
||||||
|
|
||||||
|
void saveAs(String pFileName) throws IOException;
|
||||||
|
|
||||||
|
void read() throws IOException;
|
||||||
|
}
|
||||||
255
MDM-LCAG-JavaFunctions/src/main/java/de/lcag/common/Table.java
Normal file
255
MDM-LCAG-JavaFunctions/src/main/java/de/lcag/common/Table.java
Normal file
@@ -0,0 +1,255 @@
|
|||||||
|
package de.lcag.common;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import javax.management.OperationsException;
|
||||||
|
|
||||||
|
import de.lcag.common.SelectStatement.Condition;
|
||||||
|
import de.lcag.common.SelectStatement.Join;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Supports in-memory tables. Those with a name can be registered to keep them
|
||||||
|
* in memory within livetime of the JVM. Only registered table can be used
|
||||||
|
* Table.select calls. You have to delete a registered table explicitly to
|
||||||
|
* cleanup its memory. Memeory allcoation of unregistered tables is done by
|
||||||
|
* theGarbage Collector.
|
||||||
|
*
|
||||||
|
* @author U401455
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public interface Table {
|
||||||
|
public interface TableColumn {
|
||||||
|
public void addValue(Object v);
|
||||||
|
|
||||||
|
public <V> V getValue(int i);
|
||||||
|
|
||||||
|
public void setValue(int rowNo, Object v);
|
||||||
|
|
||||||
|
public int length();
|
||||||
|
|
||||||
|
public int findRow(Object pValue);
|
||||||
|
|
||||||
|
public String getStyle();
|
||||||
|
|
||||||
|
public void setStyle(String string);
|
||||||
|
|
||||||
|
public String asCSV(String pSeparator);
|
||||||
|
|
||||||
|
public int shift();
|
||||||
|
|
||||||
|
public int getPosition();
|
||||||
|
|
||||||
|
public Table getTable();
|
||||||
|
|
||||||
|
public void init(TableColumn clientColumn);
|
||||||
|
|
||||||
|
public void init(Object pValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static final String OPTION_ADD_SOURCE_COLUMN = "ADD_SOURCE_COLUMN";
|
||||||
|
public static final String OPTION_USE_MAPPING = "USE_MAPPING";
|
||||||
|
public static final String OPTION_NUMBER_ROWS = "NUMBER_ROWS";
|
||||||
|
public static final String OPTION_LIMIT_NUMBER_ROWS = "LIMIT_NUMBER_ROWS";
|
||||||
|
|
||||||
|
public static final String KEY_COLUMN = "_Key_";
|
||||||
|
public static final String ROW_NUMBER_COLUMN = "_RowNo_";
|
||||||
|
static Map<String, Table> tables = new HashMap<>();
|
||||||
|
|
||||||
|
TableColumn addColumn(String aName);
|
||||||
|
|
||||||
|
void addColumns(String... aNameList);
|
||||||
|
|
||||||
|
void addColumns(Collection<String> pNameList);
|
||||||
|
|
||||||
|
String getSeparator();
|
||||||
|
|
||||||
|
String getName();
|
||||||
|
|
||||||
|
boolean hasColumn(String pName);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return a list of column names.
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
Set<String> getColumnNames();
|
||||||
|
|
||||||
|
public int getColumnPositionByLabel(String pLabel);
|
||||||
|
|
||||||
|
public int getColumnPositionByName(String pName);
|
||||||
|
|
||||||
|
public String getColumnNameByPosition(int pPos);
|
||||||
|
|
||||||
|
public TableColumn getColumnByName(String colName);
|
||||||
|
|
||||||
|
public TableColumn getColumnByLabel(String pFormattedColName);
|
||||||
|
|
||||||
|
public int getNoColumns();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return a list of column labels (User-defined).
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
Collection<String> getColumnLabels();
|
||||||
|
|
||||||
|
void setCellValue(int row, String colName, Object v);
|
||||||
|
|
||||||
|
void setCellValues(int pMyRow, Table pSrcTable, Integer pSrcRow);
|
||||||
|
|
||||||
|
<V> V getCellValue(int row, String colName);
|
||||||
|
|
||||||
|
public String getKeyValue(int pRow);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Calculate and return a unique key for the table
|
||||||
|
*/
|
||||||
|
public String setUniqueKeyValue(int pRow);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Calculate and return a key for the table. The key can be non-unique
|
||||||
|
*/
|
||||||
|
public String setKeyValue(int pRow);
|
||||||
|
|
||||||
|
public Integer getRowByKey(String pKey);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Find the first row number where the given column equals the value.
|
||||||
|
*
|
||||||
|
* @param colName - Name of column to search
|
||||||
|
* @param v - Value to search for
|
||||||
|
* @return -1 if not found else row number of first match in that column
|
||||||
|
*/
|
||||||
|
int findRow(String colName, Object v);
|
||||||
|
|
||||||
|
public void integrateDiff(TableComparisonResult pDiff) throws IOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new table as the result of a full table join between this table and
|
||||||
|
* the one passed.
|
||||||
|
*
|
||||||
|
* @param pTable - table to join with
|
||||||
|
* @param pFieldMap - Join condition: The Map defines the mapping of ALL column
|
||||||
|
* names in pTable to those of this table that must match to
|
||||||
|
* join rows.
|
||||||
|
* @return - new table with all rows and columns that matched
|
||||||
|
*/
|
||||||
|
Table joinTable(Table pTable, Map<String, String> pFieldMap);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new table as the result of a full table join between this table and
|
||||||
|
* the one passed.
|
||||||
|
*
|
||||||
|
* @param ptargetTable - empty table store result (will be cleared before)
|
||||||
|
* @param pTableToJoin - table to join with
|
||||||
|
* @param pFieldMap - Join condition: The Map defines the mapping of ALL
|
||||||
|
* column names in pTable to those of this table that must
|
||||||
|
* match to join rows.
|
||||||
|
*/
|
||||||
|
void joinTable(Table pTargetTable, Table pTableToJoin, Map<String, String> pFieldMap);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a CSV list of all string values in the given column.
|
||||||
|
*
|
||||||
|
* @param pColName - label or name of column to aggregate
|
||||||
|
* @param pSeparator - separator to use
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
String asCSV(String pColName, String pSeparator);
|
||||||
|
|
||||||
|
void init();
|
||||||
|
|
||||||
|
int newRow();
|
||||||
|
|
||||||
|
String asHTML();
|
||||||
|
|
||||||
|
int lenght();
|
||||||
|
|
||||||
|
String asCsvData();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Print CSV as String with the upper cased column names. If you want the lables
|
||||||
|
* instead, then use toSring()
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
String asCSV();
|
||||||
|
|
||||||
|
public Path getFilePath();
|
||||||
|
|
||||||
|
public static Table get(String pTableName) {
|
||||||
|
return tables.get(pTableName.toUpperCase());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void register(String pTableName, Table pTable) throws OperationsException {
|
||||||
|
if (pTableName == null || pTable == null)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (Table.get(pTableName) == null) {
|
||||||
|
tables.put(pTableName.toUpperCase(), pTable);
|
||||||
|
} else {
|
||||||
|
throw new OperationsException(String.format("Cannot register Table '%s' (already registered)", pTableName));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Table delete(String pTableName) {
|
||||||
|
return tables.remove(pTableName.toUpperCase());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void deleteAll() {
|
||||||
|
for (String s : tables.keySet())
|
||||||
|
tables.remove(s);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Table selectFromTable(AbstractTable pSrcTable, String pSelectStatement) {
|
||||||
|
HashTable result = new HashTable();
|
||||||
|
SelectStatement stmt = new SelectStatement(pSelectStatement);
|
||||||
|
String wantedTableName = stmt.getFromTable();
|
||||||
|
List<Condition> whereClause = stmt.getWhereClause();
|
||||||
|
List<Join> joinOn = stmt.getJoinOn();
|
||||||
|
String srcTableName = pSrcTable.getName();
|
||||||
|
Map<String, String> columns = stmt.getColumns();
|
||||||
|
|
||||||
|
if (whereClause != null && !whereClause.isEmpty()) {
|
||||||
|
throw new UnsupportedOperationException(
|
||||||
|
String.format("WHERE clause not yet supported in %s", pSelectStatement));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (joinOn != null && !joinOn.isEmpty()) {
|
||||||
|
throw new UnsupportedOperationException(
|
||||||
|
String.format("JOIN operation not yet supported in %s", pSelectStatement));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (srcTableName != null && !srcTableName.toUpperCase().equals(wantedTableName.toUpperCase())) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (String colName : columns.keySet()) {
|
||||||
|
String srcColName = columns.get(colName);
|
||||||
|
final AbstractTable.TableColumn newColumn = pSrcTable.cloneColumn(srcColName);
|
||||||
|
|
||||||
|
result.addColumn(colName, newColumn);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Table select(String pSelectStatement) {
|
||||||
|
SelectStatement stmt = new SelectStatement(pSelectStatement);
|
||||||
|
String srcTableName = stmt.getFromTable();
|
||||||
|
HashTable srcTable = (HashTable) Table.get(srcTableName);
|
||||||
|
|
||||||
|
AbstractTable result = (AbstractTable) Table.selectFromTable(srcTable, pSelectStatement);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
int getFirstRowNo();
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,204 @@
|
|||||||
|
package de.lcag.common;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Table holds information for actions to take when integrating changes done in
|
||||||
|
* Client Table into a Master Table
|
||||||
|
*/
|
||||||
|
public class TableComparisonResult {
|
||||||
|
private Table masterTable;
|
||||||
|
private Table clientTable;
|
||||||
|
|
||||||
|
private Collection<String> commonColumns;
|
||||||
|
private Collection<String> columnsToAddToMaster;
|
||||||
|
private Collection<String> columnsToDeleteFromMaster;
|
||||||
|
private Collection<Integer> rowsToAddFromClient = new ArrayList<>();
|
||||||
|
private Collection<Integer> rowsToDeleteFromMaster = new ArrayList<>();
|
||||||
|
|
||||||
|
public int getTotalNumberOfComparedCells() {
|
||||||
|
return totalNumberOfComparedCells;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTotalNumberOfComparedCells(int totalNumberOfComparedCells) {
|
||||||
|
this.totalNumberOfComparedCells = totalNumberOfComparedCells;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getTotalNumberOfDifferentCells() {
|
||||||
|
return totalNumberOfDifferentCells;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getPercentEqual() {
|
||||||
|
double result = 0;
|
||||||
|
|
||||||
|
if (totalNumberOfComparedCells > 0) {
|
||||||
|
result = 1 - totalNumberOfDifferentCells * 1.0 / totalNumberOfComparedCells;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTotalNumberOfDifferentCells(int totalNumberOfDifferentCells) {
|
||||||
|
this.totalNumberOfDifferentCells = totalNumberOfDifferentCells;
|
||||||
|
}
|
||||||
|
|
||||||
|
private int totalNumberOfComparedCells = 0;
|
||||||
|
private int totalNumberOfDifferentCells = 0;
|
||||||
|
|
||||||
|
public class CellPair {
|
||||||
|
public String key;
|
||||||
|
public int clientRow;
|
||||||
|
public String masterColName, clientColName;
|
||||||
|
|
||||||
|
public CellPair(String pKey, String pMasterColName, int pClientRowNo, String pClientColName) {
|
||||||
|
this.key = pKey;
|
||||||
|
this.clientRow = pClientRowNo;
|
||||||
|
this.masterColName = pMasterColName;
|
||||||
|
this.clientColName = pClientColName;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private LinkedHashMap<String, List<CellPair>> cellsToChangeInMasterPerRow = new LinkedHashMap<>();
|
||||||
|
|
||||||
|
public TableComparisonResult(Table pMasterTable, Table pClientTable) {
|
||||||
|
this.masterTable = pMasterTable;
|
||||||
|
this.clientTable = pClientTable;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Table getMasterTable() {
|
||||||
|
return masterTable;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Table getClientTable() {
|
||||||
|
return clientTable;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Collection<String> getColumnsToAddToMaster() {
|
||||||
|
return columnsToAddToMaster;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Collection<String> getColumnsToDeleteFromMaster() {
|
||||||
|
return columnsToDeleteFromMaster;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Collection<Integer> getRowsToAddFromClient() {
|
||||||
|
return rowsToAddFromClient;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Collection<Integer> getRowsToDeleteFromMaster() {
|
||||||
|
return rowsToDeleteFromMaster;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<String, List<CellPair>> getCellsToChangeInMaster() {
|
||||||
|
return cellsToChangeInMasterPerRow;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addCellDifference(String pKey, int pMasterRowNo, String pMasterColName, int pCmpRowNo,
|
||||||
|
String pClientColName) {
|
||||||
|
CellPair cellPair = new CellPair(pKey, pMasterColName, pCmpRowNo, pClientColName);
|
||||||
|
List<CellPair> changesInRow = this.cellsToChangeInMasterPerRow.getOrDefault(pKey, new ArrayList<>());
|
||||||
|
|
||||||
|
changesInRow.add(cellPair);
|
||||||
|
|
||||||
|
if (changesInRow.size() == 1)
|
||||||
|
this.cellsToChangeInMasterPerRow.put(pKey, changesInRow);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Collection<String> getCommonColumns() {
|
||||||
|
return this.commonColumns;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCommonColumns(Collection<String> pCommonColNames) {
|
||||||
|
this.commonColumns = pCommonColNames;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setColumnsToAdd(Collection<String> pClientsOnlyColNames) {
|
||||||
|
this.columnsToAddToMaster = pClientsOnlyColNames;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setColumnsToDelete(Collection<String> pMastersOnlyColNames) {
|
||||||
|
this.columnsToDeleteFromMaster = pMastersOnlyColNames;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addRowToAdd(int pFromClientRowNo) {
|
||||||
|
this.rowsToAddFromClient.add(pFromClientRowNo);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addRowToDelete(int pFromMasterRowNo) {
|
||||||
|
this.rowsToDeleteFromMaster.add(pFromMasterRowNo);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void generateMergedExcelFile(String pFilePath) throws IOException {
|
||||||
|
ExcelWorkbook resultTable = new ExcelWorkbook(pFilePath);
|
||||||
|
|
||||||
|
resultTable.saveAs(pFilePath);
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean tablesAreInconsistent() {
|
||||||
|
boolean result = !cellsToChangeInMasterPerRow.isEmpty();
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean masterIncludesClientTable() {
|
||||||
|
boolean result = columnsToAddToMaster.isEmpty() && rowsToAddFromClient.isEmpty()
|
||||||
|
&& cellsToChangeInMasterPerRow.isEmpty();
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean tablesAreEqual() {
|
||||||
|
boolean result = masterIncludesClientTable() && columnsToDeleteFromMaster.isEmpty()
|
||||||
|
&& rowsToDeleteFromMaster.isEmpty();
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Table integrateDifferencesIntoMaster() throws IOException {
|
||||||
|
masterTable.integrateDiff(this);
|
||||||
|
|
||||||
|
return masterTable;
|
||||||
|
}
|
||||||
|
|
||||||
|
public HashTable getSummary() {
|
||||||
|
HashTable result = new HashTable("Compare Statistic Subject", "Count");
|
||||||
|
|
||||||
|
result.addRow("Rows only in master table", this.rowsToDeleteFromMaster.size());
|
||||||
|
result.addRow("Rows only in client table", this.rowsToAddFromClient.size());
|
||||||
|
|
||||||
|
List<Integer> numberOfRowsWithNumberOfChanges = new ArrayList<>();
|
||||||
|
for (int i = 1; i < masterTable.getNoColumns(); i++) {
|
||||||
|
numberOfRowsWithNumberOfChanges.add(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (List<CellPair> changeList : this.cellsToChangeInMasterPerRow.values()) {
|
||||||
|
int n = changeList.size();
|
||||||
|
int currentCount = numberOfRowsWithNumberOfChanges.get(n) + 1;
|
||||||
|
|
||||||
|
numberOfRowsWithNumberOfChanges.set(n, currentCount);
|
||||||
|
}
|
||||||
|
|
||||||
|
int noChanges = 0;
|
||||||
|
for (int n = 0; n < numberOfRowsWithNumberOfChanges.size(); n++) {
|
||||||
|
Integer noChangesInRow = numberOfRowsWithNumberOfChanges.get(n);
|
||||||
|
|
||||||
|
noChanges += noChangesInRow;
|
||||||
|
if (noChangesInRow > 0)
|
||||||
|
result.addRow(String.format("Rows with %d differences", n), noChangesInRow);
|
||||||
|
}
|
||||||
|
|
||||||
|
result.addRow("Total number of compared cells", this.totalNumberOfComparedCells);
|
||||||
|
result.addRow("Total number of different cells", this.totalNumberOfDifferentCells);
|
||||||
|
|
||||||
|
double percentEqual = this.getPercentEqual();
|
||||||
|
result.addRow("Percent equal", String.format("%.1f%%", percentEqual * 100.0));
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,192 @@
|
|||||||
|
package de.lcag.common;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
|
||||||
|
|
||||||
|
import de.lcag.common.TableComparisonResult.CellPair;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Table holds information for actions to take when integrating changes done in
|
||||||
|
* Client Table into a Master Table
|
||||||
|
*/
|
||||||
|
public class WorkbookComparisonResult {
|
||||||
|
private ExcelWorkbook masterTable;
|
||||||
|
private ExcelWorkbook clientTable;
|
||||||
|
|
||||||
|
private Collection<String> commonTables;
|
||||||
|
private Collection<String> tablesToAddToMaster;
|
||||||
|
private Collection<String> tablesToDeleteFromMaster;
|
||||||
|
private Map<String, TableComparisonResult> differencesPerTable = new LinkedHashMap<>();
|
||||||
|
|
||||||
|
public WorkbookComparisonResult(ExcelWorkbook pMasterTable, ExcelWorkbook pClientTable) {
|
||||||
|
this.masterTable = pMasterTable;
|
||||||
|
this.clientTable = pClientTable;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ExcelWorkbook getMasterWorkbook() {
|
||||||
|
return masterTable;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ExcelWorkbook getClientWorkbook() {
|
||||||
|
return clientTable;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Collection<String> getTablesToAddToMaster() {
|
||||||
|
return tablesToAddToMaster;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Collection<String> getTablesToDeleteFromMaster() {
|
||||||
|
return tablesToDeleteFromMaster;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<String, TableComparisonResult> getDifferencesPerTable() {
|
||||||
|
return differencesPerTable;
|
||||||
|
}
|
||||||
|
|
||||||
|
public TableComparisonResult getFirstTableDifferences() {
|
||||||
|
return differencesPerTable.values().iterator().next();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addTableDifference(String pTableName, TableComparisonResult pResult) {
|
||||||
|
this.differencesPerTable.put(pTableName, pResult);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Collection<String> getCommonTables() {
|
||||||
|
return this.commonTables;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCommonTables(Collection<String> pCommonTables) {
|
||||||
|
this.commonTables = pCommonTables;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTablesToAdd(Collection<String> pClientsOnlyTables) {
|
||||||
|
this.tablesToAddToMaster = pClientsOnlyTables;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTablesToDelete(Collection<String> pMastersOnlyTables) {
|
||||||
|
this.tablesToDeleteFromMaster = pMastersOnlyTables;
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean tablesAreInconsistent() {
|
||||||
|
boolean result = !differencesPerTable.isEmpty();
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean masterIncludesClientTables() {
|
||||||
|
boolean result = tablesToAddToMaster.isEmpty() && differencesPerTable.isEmpty();
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean tablesAreEquals() {
|
||||||
|
boolean result = masterIncludesClientTables() && tablesToDeleteFromMaster.isEmpty();
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ExcelWorkbook integrateDifferencesIntoMaster() throws IOException {
|
||||||
|
masterTable.integrateDifferences(this);
|
||||||
|
|
||||||
|
return masterTable;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void save(Path pFilePath) throws IOException, InvalidFormatException {
|
||||||
|
ExcelWorkbook wb = new ExcelWorkbook(pFilePath.toString(), "Files", "Instruction", "Object", "Name", "Key",
|
||||||
|
"OldValue", "NewValue");
|
||||||
|
|
||||||
|
int rowNo = wb.newRow();
|
||||||
|
|
||||||
|
wb.newRow();
|
||||||
|
|
||||||
|
wb.setCellValue(rowNo, "Files", masterTable.getFilePath().toString());
|
||||||
|
wb.setCellValue(rowNo + 1, "Files", clientTable.getFilePath().toString());
|
||||||
|
|
||||||
|
int n = rowNo;
|
||||||
|
for (String s : tablesToAddToMaster) {
|
||||||
|
wb.setCellValue(n, "Instruction", "add");
|
||||||
|
wb.setCellValue(n, "Object", "Table");
|
||||||
|
wb.setCellValue(n, "Name", s);
|
||||||
|
|
||||||
|
n = (n < 2) ? n + 1 : wb.newRow();
|
||||||
|
}
|
||||||
|
|
||||||
|
for (String s : tablesToDeleteFromMaster) {
|
||||||
|
wb.setCellValue(n, "Instruction", "delete");
|
||||||
|
wb.setCellValue(n, "Object", "Table");
|
||||||
|
wb.setCellValue(n, "Name", s);
|
||||||
|
|
||||||
|
n = (n < 2) ? n + 1 : wb.newRow();
|
||||||
|
}
|
||||||
|
|
||||||
|
for (String tableName : differencesPerTable.keySet()) {
|
||||||
|
TableComparisonResult tableComparisonResult = differencesPerTable.get(tableName);
|
||||||
|
|
||||||
|
for (String s : tableComparisonResult.getColumnsToAddToMaster()) {
|
||||||
|
wb.setCellValue(n, "Instruction", "add");
|
||||||
|
wb.setCellValue(n, "Object", "Column");
|
||||||
|
wb.setCellValue(n, "Name", s);
|
||||||
|
|
||||||
|
n = (n < 2) ? n + 1 : wb.newRow();
|
||||||
|
}
|
||||||
|
|
||||||
|
for (String s : tableComparisonResult.getColumnsToDeleteFromMaster()) {
|
||||||
|
wb.setCellValue(n, "Instruction", "delete");
|
||||||
|
wb.setCellValue(n, "Object", "Column");
|
||||||
|
wb.setCellValue(n, "Name", s);
|
||||||
|
|
||||||
|
n = (n < 2) ? n + 1 : wb.newRow();
|
||||||
|
}
|
||||||
|
|
||||||
|
for (Integer s : tableComparisonResult.getRowsToAddFromClient()) {
|
||||||
|
wb.setCellValue(n, "Instruction", "add");
|
||||||
|
wb.setCellValue(n, "Object", "Row");
|
||||||
|
wb.setCellValue(n, "Name", s);
|
||||||
|
|
||||||
|
n = (n < 2) ? n + 1 : wb.newRow();
|
||||||
|
}
|
||||||
|
|
||||||
|
for (Integer s : tableComparisonResult.getRowsToDeleteFromMaster()) {
|
||||||
|
wb.setCellValue(n, "Instruction", "delete");
|
||||||
|
wb.setCellValue(n, "Object", "Row");
|
||||||
|
wb.setCellValue(n, "Name", s);
|
||||||
|
|
||||||
|
n = (n < 2) ? n + 1 : wb.newRow();
|
||||||
|
}
|
||||||
|
|
||||||
|
Map<String, List<CellPair>> cellsToChangeInMaster = tableComparisonResult.getCellsToChangeInMaster();
|
||||||
|
Table masterTable2 = tableComparisonResult.getMasterTable();
|
||||||
|
Table clientTable2 = tableComparisonResult.getClientTable();
|
||||||
|
|
||||||
|
for (String s : cellsToChangeInMaster.keySet()) {
|
||||||
|
List<CellPair> diffList = cellsToChangeInMaster.get(s);
|
||||||
|
|
||||||
|
for (CellPair diff : diffList) {
|
||||||
|
wb.setCellValue(n, "Instruction", "update");
|
||||||
|
wb.setCellValue(n, "Object", "Cell");
|
||||||
|
wb.setCellValue(n, "Name", diff.masterColName);
|
||||||
|
wb.setCellValue(n, "Key", diff.key);
|
||||||
|
|
||||||
|
Integer masterRowNo = masterTable2.getRowByKey(diff.key);
|
||||||
|
Integer clientRowNo = clientTable2.getRowByKey(diff.key);
|
||||||
|
|
||||||
|
Object oldValue = masterTable2.getCellValue(masterRowNo, diff.masterColName);
|
||||||
|
Object newValue = clientTable2.getCellValue(clientRowNo, diff.clientColName);
|
||||||
|
|
||||||
|
wb.setCellValue(n, "OldValue", oldValue);
|
||||||
|
wb.setCellValue(n, "NewValue", newValue);
|
||||||
|
n = (n < 2) ? n + 1 : wb.newRow();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
wb.save();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,173 @@
|
|||||||
|
package de.lcag.common;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.dom4j.Document;
|
||||||
|
import org.dom4j.DocumentException;
|
||||||
|
import org.dom4j.Element;
|
||||||
|
import org.dom4j.Node;
|
||||||
|
|
||||||
|
import routines.LcagFileTools;
|
||||||
|
import routines.LcagXmlTools;
|
||||||
|
|
||||||
|
public class XmlTable extends HashTable implements Storage {
|
||||||
|
private String filePath = null;
|
||||||
|
private String startXPath = "/*";
|
||||||
|
|
||||||
|
public XmlTable() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public XmlTable(String pTableName, String aFilePath) {
|
||||||
|
super(pTableName);
|
||||||
|
|
||||||
|
setFilePath(aFilePath);
|
||||||
|
}
|
||||||
|
|
||||||
|
public XmlTable(String aFilePath, AbstractTable pSrcTable) {
|
||||||
|
super(pSrcTable);
|
||||||
|
setFilePath(aFilePath);
|
||||||
|
}
|
||||||
|
|
||||||
|
public XmlTable(String aFilePath, String... pColumnNameList) {
|
||||||
|
super(pColumnNameList);
|
||||||
|
setFilePath(aFilePath);
|
||||||
|
}
|
||||||
|
|
||||||
|
private String setFilePath(String aFilePath) {
|
||||||
|
if (!aFilePath.toLowerCase().endsWith(".xml")) {
|
||||||
|
aFilePath += ".xml";
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.filePath = aFilePath;
|
||||||
|
}
|
||||||
|
|
||||||
|
public XmlTable(String aFilePath, Object pDefaultValue, SimpleDateFormat pDefaultDateFormat) {
|
||||||
|
super(pDefaultValue, pDefaultDateFormat);
|
||||||
|
setFilePath(aFilePath);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void save() throws IOException {
|
||||||
|
saveAs(this.filePath);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void readColumNames() throws IOException {
|
||||||
|
String text = LcagFileTools.readTextFile(filePath);
|
||||||
|
String eol = this.getEol();
|
||||||
|
String headline = text.substring(0, text.indexOf(eol));
|
||||||
|
|
||||||
|
String[] cols = headline.split(getSeparator().trim());
|
||||||
|
int colNo = 0;
|
||||||
|
|
||||||
|
for (String col : cols) {
|
||||||
|
col = col.trim();
|
||||||
|
|
||||||
|
this.addColumn(col);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Read the XML file leaf elements. Drops all existing table data. Note: In case
|
||||||
|
* of duplicate tag names only the value of the last one survives
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void read() throws IOException {
|
||||||
|
init();
|
||||||
|
|
||||||
|
try {
|
||||||
|
Document document = LcagXmlTools.getDocument(this.filePath);
|
||||||
|
|
||||||
|
this.read(document);
|
||||||
|
} catch (DocumentException e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void read(Document pDocument) {
|
||||||
|
init();
|
||||||
|
convertToTable(pDocument);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* * Appends the content of the given XML to the existing table using the same
|
||||||
|
* settings as when creating the object.
|
||||||
|
*
|
||||||
|
* @param pXML - XML String or file path to use
|
||||||
|
* @param pSourceName - adds this name into the additional "_Source" column if
|
||||||
|
* OPTION_SOURCE_COLUMN is set add
|
||||||
|
*/
|
||||||
|
public void append(String pXML, String pSourceName) {
|
||||||
|
try {
|
||||||
|
Document document = LcagXmlTools.getDocument(pXML);
|
||||||
|
convertToTable(document, pSourceName);
|
||||||
|
} catch (DocumentException e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* * Appends the content of the given XML to the existing table using the same
|
||||||
|
* settings as when creating the object.
|
||||||
|
*
|
||||||
|
* @param pXML - XML String or file path to use
|
||||||
|
*/
|
||||||
|
public void append(String pXML) {
|
||||||
|
try {
|
||||||
|
Document document = LcagXmlTools.getDocument(pXML);
|
||||||
|
convertToTable(document);
|
||||||
|
} catch (DocumentException e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void readLeafElementFields(Node pStartElement, int pRowNo) {
|
||||||
|
List<Node> nodeList = LcagXmlTools.selectNodes(pStartElement, "//*[not(*)]");
|
||||||
|
|
||||||
|
for (Node aNode : nodeList) {
|
||||||
|
Element aElement = (Element) aNode;
|
||||||
|
String value = aElement.getTextTrim();
|
||||||
|
String columnName = aElement.getName();
|
||||||
|
List<Node> attributeList = LcagXmlTools.selectNodes(aNode, "attribute::*");
|
||||||
|
|
||||||
|
if (this.useColumn(columnName))
|
||||||
|
this.addCellValue(pRowNo, columnName, value);
|
||||||
|
|
||||||
|
for (Node aAttribute : attributeList) {
|
||||||
|
String attributeColumnName = columnName + "_" + aAttribute.getName();
|
||||||
|
value = aAttribute.getText();
|
||||||
|
|
||||||
|
if (this.useColumn(attributeColumnName))
|
||||||
|
this.addCellValue(pRowNo, attributeColumnName, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void convertToTable(Document pDocument, String pSourceName) {
|
||||||
|
int rowNo = -1;
|
||||||
|
List<Node> nodeList = LcagXmlTools.selectNodes(pDocument.getRootElement(), startXPath);
|
||||||
|
|
||||||
|
for (Node aNode : nodeList) {
|
||||||
|
rowNo = this.newRow();
|
||||||
|
|
||||||
|
if (optionIsSet(OPTION_ADD_SOURCE_COLUMN) && pSourceName != null) {
|
||||||
|
this.addCellValue(rowNo, "_Source_", pSourceName);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.readLeafElementFields(aNode, rowNo);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void convertToTable(Document pDocument) {
|
||||||
|
convertToTable(pDocument, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setStartXPath(String startXPath) {
|
||||||
|
this.startXPath = startXPath;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,298 @@
|
|||||||
|
package de.lcag.csv;
|
||||||
|
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.sql.Array;
|
||||||
|
import java.sql.Blob;
|
||||||
|
import java.sql.CallableStatement;
|
||||||
|
import java.sql.Clob;
|
||||||
|
import java.sql.DatabaseMetaData;
|
||||||
|
import java.sql.NClob;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.SQLClientInfoException;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.sql.SQLWarning;
|
||||||
|
import java.sql.SQLXML;
|
||||||
|
import java.sql.Savepoint;
|
||||||
|
import java.sql.Statement;
|
||||||
|
import java.sql.Struct;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Properties;
|
||||||
|
import java.util.concurrent.Executor;
|
||||||
|
|
||||||
|
public class CsvConnection implements java.sql.Connection {
|
||||||
|
private Path directory;
|
||||||
|
|
||||||
|
CsvConnection(Path directory) {
|
||||||
|
this.directory = directory;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Statement createStatement() throws SQLException {
|
||||||
|
return new CsvStatement(directory);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public PreparedStatement prepareStatement(String s) throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public CallableStatement prepareCall(String s) throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String nativeSQL(String s) throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setAutoCommit(boolean b) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean getAutoCommit() throws SQLException {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void commit() throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void rollback() throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void close() throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isClosed() throws SQLException {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public DatabaseMetaData getMetaData() throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setReadOnly(boolean b) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isReadOnly() throws SQLException {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setCatalog(String s) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getCatalog() throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setTransactionIsolation(int i) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getTransactionIsolation() throws SQLException {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public SQLWarning getWarnings() throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void clearWarnings() throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Statement createStatement(int i, int i1) throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public PreparedStatement prepareStatement(String s, int i, int i1) throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public CallableStatement prepareCall(String s, int i, int i1) throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Map<String, Class<?>> getTypeMap() throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setTypeMap(Map<String, Class<?>> map) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setHoldability(int i) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getHoldability() throws SQLException {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Savepoint setSavepoint() throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Savepoint setSavepoint(String s) throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void rollback(Savepoint savepoint) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void releaseSavepoint(Savepoint savepoint) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Statement createStatement(int i, int i1, int i2) throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public PreparedStatement prepareStatement(String s, int i, int i1, int i2) throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public CallableStatement prepareCall(String s, int i, int i1, int i2) throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public PreparedStatement prepareStatement(String s, int i) throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public PreparedStatement prepareStatement(String s, int[] ints) throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public PreparedStatement prepareStatement(String s, String[] strings) throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Clob createClob() throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Blob createBlob() throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public NClob createNClob() throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public SQLXML createSQLXML() throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isValid(int i) throws SQLException {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setClientInfo(String s, String s1) throws SQLClientInfoException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setClientInfo(Properties properties) throws SQLClientInfoException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getClientInfo(String s) throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Properties getClientInfo() throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Array createArrayOf(String s, Object[] objects) throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Struct createStruct(String s, Object[] objects) throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setSchema(String s) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getSchema() throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void abort(Executor executor) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setNetworkTimeout(Executor executor, int i) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getNetworkTimeout() throws SQLException {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <T> T unwrap(Class<T> aClass) throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isWrapperFor(Class<?> aClass) throws SQLException {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,88 @@
|
|||||||
|
package de.lcag.csv;
|
||||||
|
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.Driver;
|
||||||
|
import java.sql.DriverManager;
|
||||||
|
import java.sql.DriverPropertyInfo;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.sql.SQLFeatureNotSupportedException;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Properties;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
public class CsvDriver implements Driver {
|
||||||
|
private static final Driver INSTANCE = new CsvDriver();
|
||||||
|
private static Logger log = Logger.getLogger("CsvDriver");
|
||||||
|
private static boolean registered;
|
||||||
|
|
||||||
|
public CsvDriver() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Connection connect(String s, Properties properties) throws SQLException {
|
||||||
|
String[] parts = s.split(":");
|
||||||
|
|
||||||
|
if (parts.length < 2 || !parts[0].toLowerCase().equals("jdbc") || !parts[1].toLowerCase().equals("csv"))
|
||||||
|
return null;
|
||||||
|
|
||||||
|
String directory = Arrays.stream(parts).skip(2).collect(Collectors.joining(":"));
|
||||||
|
|
||||||
|
Path path = Paths.get(directory).toAbsolutePath();
|
||||||
|
|
||||||
|
if (!Files.isDirectory(path))
|
||||||
|
throw new SQLException("'" + path + "' is not a directory");
|
||||||
|
|
||||||
|
return new CsvConnection(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean acceptsURL(String s) throws SQLException {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public DriverPropertyInfo[] getPropertyInfo(String s, Properties properties) throws SQLException {
|
||||||
|
return new DriverPropertyInfo[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getMajorVersion() {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getMinorVersion() {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean jdbcCompliant() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Logger getParentLogger() throws SQLFeatureNotSupportedException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static synchronized Driver load() {
|
||||||
|
if (!registered) {
|
||||||
|
registered = true;
|
||||||
|
try {
|
||||||
|
DriverManager.registerDriver(INSTANCE);
|
||||||
|
} catch (SQLException throwables) {
|
||||||
|
throwables.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return INSTANCE;
|
||||||
|
}
|
||||||
|
|
||||||
|
static {
|
||||||
|
load();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,998 @@
|
|||||||
|
package de.lcag.csv;
|
||||||
|
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.Reader;
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.sql.Array;
|
||||||
|
import java.sql.Blob;
|
||||||
|
import java.sql.Clob;
|
||||||
|
import java.sql.Date;
|
||||||
|
import java.sql.NClob;
|
||||||
|
import java.sql.Ref;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.ResultSetMetaData;
|
||||||
|
import java.sql.RowId;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.sql.SQLWarning;
|
||||||
|
import java.sql.SQLXML;
|
||||||
|
import java.sql.Statement;
|
||||||
|
import java.sql.Time;
|
||||||
|
import java.sql.Timestamp;
|
||||||
|
import java.util.Calendar;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
public class CsvResultSet implements ResultSet {
|
||||||
|
private static final String FIELD_SEPARATOR = ";";
|
||||||
|
private Iterator<String> iterator;
|
||||||
|
private List<String> record = null;
|
||||||
|
|
||||||
|
public CsvResultSet(Stream<String> s) {
|
||||||
|
iterator = s.iterator();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean next() throws SQLException {
|
||||||
|
boolean retVal = iterator.hasNext();
|
||||||
|
record = null;
|
||||||
|
|
||||||
|
if (iterator.hasNext()) {
|
||||||
|
record = List.of(iterator.next().split(FIELD_SEPARATOR));
|
||||||
|
}
|
||||||
|
|
||||||
|
return retVal;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void close() throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean wasNull() throws SQLException {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getString(int i) throws SQLException {
|
||||||
|
return record.get(i - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean getBoolean(int i) throws SQLException {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public byte getByte(int i) throws SQLException {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public short getShort(int i) throws SQLException {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getInt(int i) throws SQLException {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public long getLong(int i) throws SQLException {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public float getFloat(int i) throws SQLException {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public double getDouble(int i) throws SQLException {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BigDecimal getBigDecimal(int i, int i1) throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public byte[] getBytes(int i) throws SQLException {
|
||||||
|
return new byte[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Date getDate(int i) throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Time getTime(int i) throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Timestamp getTimestamp(int i) throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public InputStream getAsciiStream(int i) throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public InputStream getUnicodeStream(int i) throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public InputStream getBinaryStream(int i) throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getString(String s) throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean getBoolean(String s) throws SQLException {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public byte getByte(String s) throws SQLException {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public short getShort(String s) throws SQLException {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getInt(String s) throws SQLException {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public long getLong(String s) throws SQLException {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public float getFloat(String s) throws SQLException {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public double getDouble(String s) throws SQLException {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BigDecimal getBigDecimal(String s, int i) throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public byte[] getBytes(String s) throws SQLException {
|
||||||
|
return new byte[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Date getDate(String s) throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Time getTime(String s) throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Timestamp getTimestamp(String s) throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public InputStream getAsciiStream(String s) throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public InputStream getUnicodeStream(String s) throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public InputStream getBinaryStream(String s) throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public SQLWarning getWarnings() throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void clearWarnings() throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getCursorName() throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ResultSetMetaData getMetaData() throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object getObject(int i) throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object getObject(String s) throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int findColumn(String s) throws SQLException {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Reader getCharacterStream(int i) throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Reader getCharacterStream(String s) throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BigDecimal getBigDecimal(int i) throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BigDecimal getBigDecimal(String s) throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isBeforeFirst() throws SQLException {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isAfterLast() throws SQLException {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isFirst() throws SQLException {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isLast() throws SQLException {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void beforeFirst() throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void afterLast() throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean first() throws SQLException {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean last() throws SQLException {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getRow() throws SQLException {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean absolute(int i) throws SQLException {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean relative(int i) throws SQLException {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean previous() throws SQLException {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setFetchDirection(int i) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getFetchDirection() throws SQLException {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setFetchSize(int i) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getFetchSize() throws SQLException {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getType() throws SQLException {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getConcurrency() throws SQLException {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean rowUpdated() throws SQLException {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean rowInserted() throws SQLException {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean rowDeleted() throws SQLException {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateNull(int i) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateBoolean(int i, boolean b) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateByte(int i, byte b) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateShort(int i, short i1) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateInt(int i, int i1) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateLong(int i, long l) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateFloat(int i, float v) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateDouble(int i, double v) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateBigDecimal(int i, BigDecimal bigDecimal) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateString(int i, String s) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateBytes(int i, byte[] bytes) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateDate(int i, Date date) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateTime(int i, Time time) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateTimestamp(int i, Timestamp timestamp) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateAsciiStream(int i, InputStream inputStream, int i1) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateBinaryStream(int i, InputStream inputStream, int i1) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateCharacterStream(int i, Reader reader, int i1) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateObject(int i, Object o, int i1) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateObject(int i, Object o) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateNull(String s) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateBoolean(String s, boolean b) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateByte(String s, byte b) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateShort(String s, short i) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateInt(String s, int i) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateLong(String s, long l) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateFloat(String s, float v) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateDouble(String s, double v) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateBigDecimal(String s, BigDecimal bigDecimal) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateString(String s, String s1) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateBytes(String s, byte[] bytes) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateDate(String s, Date date) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateTime(String s, Time time) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateTimestamp(String s, Timestamp timestamp) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateAsciiStream(String s, InputStream inputStream, int i) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateBinaryStream(String s, InputStream inputStream, int i) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateCharacterStream(String s, Reader reader, int i) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateObject(String s, Object o, int i) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateObject(String s, Object o) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void insertRow() throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateRow() throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void deleteRow() throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void refreshRow() throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void cancelRowUpdates() throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void moveToInsertRow() throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void moveToCurrentRow() throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Statement getStatement() throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object getObject(int i, Map<String, Class<?>> map) throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Ref getRef(int i) throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Blob getBlob(int i) throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Clob getClob(int i) throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Array getArray(int i) throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object getObject(String s, Map<String, Class<?>> map) throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Ref getRef(String s) throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Blob getBlob(String s) throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Clob getClob(String s) throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Array getArray(String s) throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Date getDate(int i, Calendar calendar) throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Date getDate(String s, Calendar calendar) throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Time getTime(int i, Calendar calendar) throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Time getTime(String s, Calendar calendar) throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Timestamp getTimestamp(int i, Calendar calendar) throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Timestamp getTimestamp(String s, Calendar calendar) throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public URL getURL(int i) throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public URL getURL(String s) throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateRef(int i, Ref ref) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateRef(String s, Ref ref) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateBlob(int i, Blob blob) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateBlob(String s, Blob blob) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateClob(int i, Clob clob) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateClob(String s, Clob clob) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateArray(int i, Array array) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateArray(String s, Array array) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public RowId getRowId(int i) throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public RowId getRowId(String s) throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateRowId(int i, RowId rowId) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateRowId(String s, RowId rowId) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getHoldability() throws SQLException {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isClosed() throws SQLException {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateNString(int i, String s) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateNString(String s, String s1) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateNClob(int i, NClob nClob) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateNClob(String s, NClob nClob) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public NClob getNClob(int i) throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public NClob getNClob(String s) throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public SQLXML getSQLXML(int i) throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public SQLXML getSQLXML(String s) throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateSQLXML(int i, SQLXML sqlxml) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateSQLXML(String s, SQLXML sqlxml) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getNString(int i) throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getNString(String s) throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Reader getNCharacterStream(int i) throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Reader getNCharacterStream(String s) throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateNCharacterStream(int i, Reader reader, long l) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateNCharacterStream(String s, Reader reader, long l) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateAsciiStream(int i, InputStream inputStream, long l) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateBinaryStream(int i, InputStream inputStream, long l) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateCharacterStream(int i, Reader reader, long l) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateAsciiStream(String s, InputStream inputStream, long l) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateBinaryStream(String s, InputStream inputStream, long l) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateCharacterStream(String s, Reader reader, long l) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateBlob(int i, InputStream inputStream, long l) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateBlob(String s, InputStream inputStream, long l) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateClob(int i, Reader reader, long l) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateClob(String s, Reader reader, long l) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateNClob(int i, Reader reader, long l) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateNClob(String s, Reader reader, long l) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateNCharacterStream(int i, Reader reader) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateNCharacterStream(String s, Reader reader) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateAsciiStream(int i, InputStream inputStream) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateBinaryStream(int i, InputStream inputStream) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateCharacterStream(int i, Reader reader) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateAsciiStream(String s, InputStream inputStream) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateBinaryStream(String s, InputStream inputStream) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateCharacterStream(String s, Reader reader) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateBlob(int i, InputStream inputStream) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateBlob(String s, InputStream inputStream) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateClob(int i, Reader reader) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateClob(String s, Reader reader) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateNClob(int i, Reader reader) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateNClob(String s, Reader reader) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <T> T getObject(int i, Class<T> aClass) throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <T> T getObject(String s, Class<T> aClass) throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <T> T unwrap(Class<T> aClass) throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isWrapperFor(Class<?> aClass) throws SQLException {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,260 @@
|
|||||||
|
package de.lcag.csv;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.sql.SQLWarning;
|
||||||
|
import java.sql.Statement;
|
||||||
|
|
||||||
|
public class CsvStatement implements Statement {
|
||||||
|
private Path directory;
|
||||||
|
|
||||||
|
CsvStatement(Path directory) {
|
||||||
|
this.directory = directory;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ResultSet executeQuery(String s) throws SQLException {
|
||||||
|
String fileName = getFileName(s);
|
||||||
|
|
||||||
|
Path filePath = directory.resolve(fileName);
|
||||||
|
|
||||||
|
if (!Files.exists(filePath))
|
||||||
|
throw new SQLException(filePath.toAbsolutePath().toString() + " does not exists");
|
||||||
|
|
||||||
|
try {
|
||||||
|
return new CsvResultSet(Files.lines(filePath));
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new SQLException("'" + filePath.toString() + "' file can not be read", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getFileName(String sqlExpression) throws SQLException {
|
||||||
|
sqlExpression = sqlExpression.trim();
|
||||||
|
if (sqlExpression.isEmpty()) throw new SQLException("Empty sql expression");
|
||||||
|
|
||||||
|
String[] parts = sqlExpression.split(" +");
|
||||||
|
String fileName = parts[parts.length - 1];
|
||||||
|
if (fileName.endsWith(";")) fileName = fileName.substring(0, fileName.length() - 1);
|
||||||
|
|
||||||
|
return fileName;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int executeUpdate(String s) throws SQLException {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void close() throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getMaxFieldSize() throws SQLException {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setMaxFieldSize(int i) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getMaxRows() throws SQLException {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setMaxRows(int i) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setEscapeProcessing(boolean b) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getQueryTimeout() throws SQLException {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setQueryTimeout(int i) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void cancel() throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public SQLWarning getWarnings() throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void clearWarnings() throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setCursorName(String s) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean execute(String s) throws SQLException {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ResultSet getResultSet() throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getUpdateCount() throws SQLException {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean getMoreResults() throws SQLException {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setFetchDirection(int i) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getFetchDirection() throws SQLException {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setFetchSize(int i) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getFetchSize() throws SQLException {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getResultSetConcurrency() throws SQLException {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getResultSetType() throws SQLException {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addBatch(String s) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void clearBatch() throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int[] executeBatch() throws SQLException {
|
||||||
|
return new int[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Connection getConnection() throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean getMoreResults(int i) throws SQLException {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ResultSet getGeneratedKeys() throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int executeUpdate(String s, int i) throws SQLException {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int executeUpdate(String s, int[] ints) throws SQLException {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int executeUpdate(String s, String[] strings) throws SQLException {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean execute(String s, int i) throws SQLException {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean execute(String s, int[] ints) throws SQLException {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean execute(String s, String[] strings) throws SQLException {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getResultSetHoldability() throws SQLException {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isClosed() throws SQLException {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setPoolable(boolean b) throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isPoolable() throws SQLException {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void closeOnCompletion() throws SQLException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isCloseOnCompletion() throws SQLException {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <T> T unwrap(Class<T> aClass) throws SQLException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isWrapperFor(Class<?> aClass) throws SQLException {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
|
||||||
|
/**
|
||||||
|
* Basic CSV JDBC driver. Not yet used: Select function too basic and too
|
||||||
|
* difficult to implement to just support a basic JOIN operation
|
||||||
|
*
|
||||||
|
* @since 4.0.9
|
||||||
|
* @author Dr. C. Ditze
|
||||||
|
* @version 4.0.9
|
||||||
|
* @source https://github.com/peterborkuti/csv-jdbc-driver
|
||||||
|
*/
|
||||||
|
package de.lcag.csv;
|
||||||
200
MDM-LCAG-JavaFunctions/src/main/java/routines/DQTechnical.java
Normal file
200
MDM-LCAG-JavaFunctions/src/main/java/routines/DQTechnical.java
Normal file
@@ -0,0 +1,200 @@
|
|||||||
|
// ============================================================================
|
||||||
|
//
|
||||||
|
// Copyright (C) 2006-2019 Talend Inc. - www.talend.com
|
||||||
|
//
|
||||||
|
// This source code is available under agreement available at
|
||||||
|
// %InstallDIR%\features\org.talend.rcp.branding.%PRODUCTNAME%\%PRODUCTNAME%license.txt
|
||||||
|
//
|
||||||
|
// You should have received a copy of the agreement
|
||||||
|
// along with this program; if not, write to Talend SA
|
||||||
|
// 9 rue Pages 92150 Suresnes, France
|
||||||
|
//
|
||||||
|
// ============================================================================
|
||||||
|
package routines;
|
||||||
|
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* created by talend on 2015-07-28 Detailled comment.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class DQTechnical {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* extractTitle: Returns the extracted title based on a list of titles provided
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} Data Quality
|
||||||
|
*
|
||||||
|
* {param} string("Mrs. Marty Smith") inStr: the string to be processed
|
||||||
|
*
|
||||||
|
* {param} int(1) trimSpaces: 0 or 1 to trim spaces or not
|
||||||
|
*
|
||||||
|
* {example} extractTitle(" Mrs. Marty Smith ", 1) # returns "Mrs."
|
||||||
|
*/
|
||||||
|
|
||||||
|
public static String extractTitle(String inStr, int trimSpaces) {
|
||||||
|
if (inStr == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
Boolean debuget = false;
|
||||||
|
String outStr = inStr;
|
||||||
|
|
||||||
|
Pattern p = Pattern
|
||||||
|
.compile("^\\s?((Mr|Mrs|Ms|Miss|Dr|Herr|Monsieur|Hr|Frau|A V M|Admiraal|Admiral|Air Cdre|Air Commodore|Air Marshal|Air Vice Marshal|Alderman|Alhaji|Ambassador|Baron|Barones|Brig|Brig Gen|Brig General|Brigadier|Brigadier General|Brother|Canon|Capt|Captain|Cardinal|Cdr|Chief|Cik|Cmdr|Col|Col Dr|Colonel|Commandant|Commander|Commissioner|Commodore|Comte|Comtessa|Congressman|Conseiller|Consul|Conte|Contessa|Corporal|Councillor|Count|Countess|Crown Prince|Crown Princess|Dame|Datin|Dato|Datuk|Datuk Seri|Deacon|Deaconess|Dean|Dhr|Dipl Ing|Doctor|Dott|Dott sa|Dr|Dr Ing|Dra|Drs|Embajador|Embajadora|En|Encik|Eng|Eur Ing|Exma|Sra|Exmo Sr|F|O|Father|First Lieutient|First Officer|Flt Lieut|Flying Officer|Fr|Frau|Fraulein|Fru|Gen|Generaal|General|Governor|Graaf|Gravin|Group Captain|Grp Capt|H E Dr|H H|H M|H R H|Hajah|Haji|Hajim|Her Highness|Her Majesty|Herr|High Chief|His Highness|His Holiness|His Majesty|Hon|Hr|Hra|Ing|Ir|Jonkheer|Judge|Justice|Khun Ying|Kolonel|Lady|Lcda|Lic|Lieut|Lieut Cdr|Lieut Col|Lieut Gen|Lord|M|M L|M R|Madame|Mademoiselle|Maj Gen|Major|Master|Mevrouw|Miss|Mlle|Mme|Monsieur|Monsignor|Mr|Mrs|Ms|Mstr|Nti|Pastor|President|Prince|Princess|Princesse|Prinses|Prof|Prof Dr|Prof Sir|Professor|Puan|Puan Sri|Rabbi|Rear Admiral|Rev|Rev Canon|Rev Dr|Rev Mother|Reverend|Rva|Senator|Sergeant|Sheikh|Sheikha|Sig|Sig na|Sig ra|Sir|Sister|Sqn Ldr|Sr|Sr D|Sra|Srta|Sultan|Tan Sri|Tan Sri Dato|Tengku|Teuku|Than Puying|The Hon Dr|The Hon Justice|The Hon Miss|The Hon Mr|The Hon Mrs|The Hon Ms|The Hon Sir|The Very Rev|Toh Puan|Tun|Vice Admiral|Viscount|Viscountess|Wg Cdr)\\.?\\s)(.*)$");
|
||||||
|
Matcher m = p.matcher(inStr);
|
||||||
|
|
||||||
|
if (m.find()) {
|
||||||
|
outStr = m.group(2);
|
||||||
|
if (trimSpaces == 1) {
|
||||||
|
TalendString.talendTrim(outStr, ' ', 0);
|
||||||
|
}
|
||||||
|
if (debuget) {
|
||||||
|
System.err.println("extracted title: \"" + outStr + "\"");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return outStr;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* removeTitle: Returns the string without the title
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} Data Quality
|
||||||
|
*
|
||||||
|
* {param} string("Mrs. Marty Smith") inStr: the string to be processed
|
||||||
|
*
|
||||||
|
* {param} int(1) trimSpaces: 0 or 1 to trim spaces or not
|
||||||
|
*
|
||||||
|
* {example} removeTitle(" Mrs. Marty Smith ", 1) # returns "Mrs."
|
||||||
|
*/
|
||||||
|
|
||||||
|
public static String removeTitle(String inStr, int trimSpaces) {
|
||||||
|
if (inStr == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
Boolean debuget = false;
|
||||||
|
String outStr = inStr;
|
||||||
|
|
||||||
|
Pattern p = Pattern
|
||||||
|
.compile("^\\s?((Mr|Mrs|Ms|Miss|Dr|Herr|Monsieur|Hr|Frau|A V M|Admiraal|Admiral|Air Cdre|Air Commodore|Air Marshal|Air Vice Marshal|Alderman|Alhaji|Ambassador|Baron|Barones|Brig|Brig Gen|Brig General|Brigadier|Brigadier General|Brother|Canon|Capt|Captain|Cardinal|Cdr|Chief|Cik|Cmdr|Col|Col Dr|Colonel|Commandant|Commander|Commissioner|Commodore|Comte|Comtessa|Congressman|Conseiller|Consul|Conte|Contessa|Corporal|Councillor|Count|Countess|Crown Prince|Crown Princess|Dame|Datin|Dato|Datuk|Datuk Seri|Deacon|Deaconess|Dean|Dhr|Dipl Ing|Doctor|Dott|Dott sa|Dr|Dr Ing|Dra|Drs|Embajador|Embajadora|En|Encik|Eng|Eur Ing|Exma|Sra|Exmo Sr|F|O|Father|First Lieutient|First Officer|Flt Lieut|Flying Officer|Fr|Frau|Fraulein|Fru|Gen|Generaal|General|Governor|Graaf|Gravin|Group Captain|Grp Capt|H E Dr|H H|H M|H R H|Hajah|Haji|Hajim|Her Highness|Her Majesty|Herr|High Chief|His Highness|His Holiness|His Majesty|Hon|Hr|Hra|Ing|Ir|Jonkheer|Judge|Justice|Khun Ying|Kolonel|Lady|Lcda|Lic|Lieut|Lieut Cdr|Lieut Col|Lieut Gen|Lord|M|M L|M R|Madame|Mademoiselle|Maj Gen|Major|Master|Mevrouw|Miss|Mlle|Mme|Monsieur|Monsignor|Mr|Mrs|Ms|Mstr|Nti|Pastor|President|Prince|Princess|Princesse|Prinses|Prof|Prof Dr|Prof Sir|Professor|Puan|Puan Sri|Rabbi|Rear Admiral|Rev|Rev Canon|Rev Dr|Rev Mother|Reverend|Rva|Senator|Sergeant|Sheikh|Sheikha|Sig|Sig na|Sig ra|Sir|Sister|Sqn Ldr|Sr|Sr D|Sra|Srta|Sultan|Tan Sri|Tan Sri Dato|Tengku|Teuku|Than Puying|The Hon Dr|The Hon Justice|The Hon Miss|The Hon Mr|The Hon Mrs|The Hon Ms|The Hon Sir|The Very Rev|Toh Puan|Tun|Vice Admiral|Viscount|Viscountess|Wg Cdr)\\.?\\s)(.*)$");
|
||||||
|
Matcher m = p.matcher(inStr);
|
||||||
|
|
||||||
|
if (m.find()) {
|
||||||
|
outStr = m.group(3);
|
||||||
|
if (trimSpaces == 1) {
|
||||||
|
TalendString.talendTrim(outStr, ' ', 0);
|
||||||
|
}
|
||||||
|
if (debuget) {
|
||||||
|
System.err.println("name with title removed: \"" + outStr + "\"");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return outStr;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* extractLastName: Returns the string without the title
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} Data Quality
|
||||||
|
*
|
||||||
|
* {param} string("Mrs. Marty Smith") inStr: the string to be processed
|
||||||
|
*
|
||||||
|
* {param} int(1) trimSpaces: 0 or 1 to trim spaces or not
|
||||||
|
*
|
||||||
|
* {example} extractLastName(" Mrs. Marty Smith ", 1) # returns "Smith"
|
||||||
|
*/
|
||||||
|
|
||||||
|
public static String extractLastName(String inStr, int trimSpaces) {
|
||||||
|
if (inStr == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
Boolean debuget = false;
|
||||||
|
String outStr = inStr;
|
||||||
|
|
||||||
|
Pattern p = Pattern.compile("\\s([a-zA-Z]+)$");
|
||||||
|
Matcher m = p.matcher(inStr);
|
||||||
|
|
||||||
|
if (m.find()) {
|
||||||
|
outStr = m.group(1);
|
||||||
|
if (trimSpaces == 1) {
|
||||||
|
TalendString.talendTrim(outStr, ' ', 0);
|
||||||
|
}
|
||||||
|
if (debuget) {
|
||||||
|
System.err.println("name with title removed \"" + outStr + "\"");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return outStr;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* removeNameSuffix: Returns the string without the suffix
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} Data Quality
|
||||||
|
*
|
||||||
|
* {param} string("Mr. Marty Smith III") inStr: the string to be processed
|
||||||
|
*
|
||||||
|
* {param} int(1) trimSpaces: 0 or 1 to trim spaces or not
|
||||||
|
*
|
||||||
|
* {example} removeNameSuffix(" Mr. Marty Smith III", 1) # returns "Mr. Marty Smith"
|
||||||
|
*/
|
||||||
|
|
||||||
|
public static String removeNameSuffix(String inStr, int trimSpaces) {
|
||||||
|
if (inStr == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
Boolean debuget = false;
|
||||||
|
String outStr = inStr;
|
||||||
|
|
||||||
|
Pattern p = Pattern.compile("^(.*)?\\s(Jr.|Sr.|Jr|Sr.|III|IV|V)$");
|
||||||
|
Matcher m = p.matcher(inStr);
|
||||||
|
|
||||||
|
if (m.find()) {
|
||||||
|
outStr = m.group(1);
|
||||||
|
if (trimSpaces == 1) {
|
||||||
|
TalendString.talendTrim(outStr, ' ', 0);
|
||||||
|
}
|
||||||
|
if (debuget) {
|
||||||
|
System.err.println("name with suffix removed \"" + outStr + "\"");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return outStr;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* isNameStringValid: Returns true or false if the string is a valid formed name: [Initial] Firstname [Initial]
|
||||||
|
* Lastname
|
||||||
|
*
|
||||||
|
* {talendTypes} Boolean
|
||||||
|
*
|
||||||
|
* {Category} Data Quality
|
||||||
|
*
|
||||||
|
* {param} string("Mrs. Marty Smith") inStr: the string to be processed
|
||||||
|
*
|
||||||
|
* {example} isNameStringValid("Mrs. Marty Smith ") # returns true
|
||||||
|
*/
|
||||||
|
|
||||||
|
public static boolean isNameStringValid(String inStr) {
|
||||||
|
if (inStr == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
Pattern p = Pattern.compile("^\\s?([A-Z].? )?[A-Z][a-z]* ([A-Z].? )?[A-Z][a-z]*\\s?$");
|
||||||
|
Matcher m = p.matcher(inStr);
|
||||||
|
|
||||||
|
if (m.matches()) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
1243
MDM-LCAG-JavaFunctions/src/main/java/routines/DataMasking.java
Normal file
1243
MDM-LCAG-JavaFunctions/src/main/java/routines/DataMasking.java
Normal file
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,79 @@
|
|||||||
|
// ============================================================================
|
||||||
|
//
|
||||||
|
// Copyright (c) 2006-2014, Talend Inc.
|
||||||
|
//
|
||||||
|
// This source code has been automatically generated by_Talend MDM Platform
|
||||||
|
// CodeGenerator version 7.3.1.20220805_1617-patch
|
||||||
|
// You can find more information about Talend products at www.talend.com.
|
||||||
|
// You may distribute this code under the terms of the GNU LGPL license
|
||||||
|
// http://www.gnu.org/licenses/lgpl.html).
|
||||||
|
//
|
||||||
|
// ============================================================================
|
||||||
|
package routines;
|
||||||
|
|
||||||
|
public class DataOperation {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts a numeric value to its ASCII character string equivalent.
|
||||||
|
*
|
||||||
|
* {talendTypes} char | Character
|
||||||
|
*
|
||||||
|
* {Category} DataOperation
|
||||||
|
*
|
||||||
|
* {param} int(1) i: numeric value
|
||||||
|
*
|
||||||
|
* {example} CHAR(1):int
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static char CHAR(int i) {
|
||||||
|
return Character.forDigit(i, 10);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts a decimal integer into its hexadecimal equivalent.
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} DataOperation
|
||||||
|
*
|
||||||
|
* {param} int(1) i:decimal integer
|
||||||
|
*
|
||||||
|
* {example} DTX(1)
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static String DTX(int i) {
|
||||||
|
return Integer.toHexString(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Rounds a number of type Double to a number of type Long with the precision specified in the PRECISION statement.
|
||||||
|
*
|
||||||
|
* {talendTypes} long | Long
|
||||||
|
*
|
||||||
|
* {Category} DataOperation
|
||||||
|
*
|
||||||
|
* {param} double (0.0) d:double number
|
||||||
|
*
|
||||||
|
* {example} FIX(3.14)
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static long FIX(double d) {
|
||||||
|
return Math.round(d);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts a hexadecimal string into its decimal equivalent.
|
||||||
|
*
|
||||||
|
* {talendTypes} int | Integer
|
||||||
|
*
|
||||||
|
* {Category} DataOperation
|
||||||
|
*
|
||||||
|
* {param} string ("0") text: hexadecimal string
|
||||||
|
*
|
||||||
|
* {example} XTD(\"1\")
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static int XTD(String text) {
|
||||||
|
return Integer.valueOf(text, 16);
|
||||||
|
}
|
||||||
|
}
|
||||||
111
MDM-LCAG-JavaFunctions/src/main/java/routines/DataQuality.java
Normal file
111
MDM-LCAG-JavaFunctions/src/main/java/routines/DataQuality.java
Normal file
@@ -0,0 +1,111 @@
|
|||||||
|
// ============================================================================
|
||||||
|
//
|
||||||
|
// Copyright (C) 2006-2019 Talend Inc. - www.talend.com
|
||||||
|
//
|
||||||
|
// This source code is available under agreement available at
|
||||||
|
// %InstallDIR%\features\org.talend.rcp.branding.%PRODUCTNAME%\%PRODUCTNAME%license.txt
|
||||||
|
//
|
||||||
|
// You should have received a copy of the agreement
|
||||||
|
// along with this program; if not, write to Talend SA
|
||||||
|
// 9 rue Pages 92150 Suresnes, France
|
||||||
|
//
|
||||||
|
// ============================================================================
|
||||||
|
package routines;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* created by talend on 2015-07-28 Detailled comment.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class DataQuality {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* getTitle: Returns the extracted title based on a list of titles provided
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} Data Quality
|
||||||
|
*
|
||||||
|
* {param} string("Mrs. Marty Smith") name: the string to be processed
|
||||||
|
*
|
||||||
|
* {example} getTitle(" Mrs. Marty Smith ", 1) # returns "Mrs."
|
||||||
|
*/
|
||||||
|
public static String getTitle(String name) {
|
||||||
|
return DQTechnical.extractTitle(name, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* getNameWithoutTitle: Returns the string without the title
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} Data Quality
|
||||||
|
*
|
||||||
|
* {param} string("Mrs. Marty Smith") name: the string to be processed
|
||||||
|
*
|
||||||
|
* {example} getNameWithoutTitle(" Mrs. Marty Smith ", 1) # returns "Marty Smith"
|
||||||
|
*/
|
||||||
|
public static String getNameWithoutTitle(String name) {
|
||||||
|
return DQTechnical.removeTitle(name, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* getNameWithoutSuffix: Returns the string without the suffix
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} Data Quality
|
||||||
|
*
|
||||||
|
* {param} string("Mr. Marty Smith III") name: the string to be processed
|
||||||
|
*
|
||||||
|
* {example} getNameWithoutSuffix(" Mr. Marty Smith III", 1) # returns "Mr. Marty Smith"
|
||||||
|
*/
|
||||||
|
public static String getNameWithoutSuffix(String name) {
|
||||||
|
return DQTechnical.removeNameSuffix(name, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* getLastName: Returns only LastName value from the string
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} Data Quality
|
||||||
|
*
|
||||||
|
* {param} string("Mrs. Marty Smith III") name: the string to be processed
|
||||||
|
*
|
||||||
|
* {example} getLastName(" Mrs. Marty Smith III", 1) # returns "Smith"
|
||||||
|
*/
|
||||||
|
public static String getLastName(String name) {
|
||||||
|
return DQTechnical.extractLastName(DQTechnical.removeNameSuffix(DQTechnical.removeTitle(name, 1), 1), 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* getCleansedLastName: Returns the string without the title and suffix
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} Data Quality
|
||||||
|
*
|
||||||
|
* {param} string("Mrs. Marty Smith III") name: the string to be processed
|
||||||
|
*
|
||||||
|
* {example} getCleansedLastName(" Mrs. Marty Smith III", 1) # returns "Marty Smith"
|
||||||
|
*/
|
||||||
|
public static String getCleansedLastName(String name) {
|
||||||
|
return DQTechnical.removeNameSuffix(DQTechnical.removeTitle(name, 1), 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* isValidName: Returns true or false if the string is a valid formed name: [Initial] Firstname [Initial] Lastname
|
||||||
|
*
|
||||||
|
* {talendTypes} Boolean
|
||||||
|
*
|
||||||
|
* {Category} Data Quality
|
||||||
|
*
|
||||||
|
* {param} string("Mrs. Marty Smith") name: the string to be processed
|
||||||
|
*
|
||||||
|
* {example} isValidName("Mrs. Marty Smith ") # returns true
|
||||||
|
*/
|
||||||
|
public static Boolean isValidName(String name) {
|
||||||
|
return DQTechnical.isNameStringValid(DQTechnical.removeNameSuffix(DQTechnical.removeTitle(name, 1), 1));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
// ============================================================================
|
||||||
|
//
|
||||||
|
// Copyright (C) 2006-2019 Talend Inc. - www.talend.com
|
||||||
|
//
|
||||||
|
// This source code is available under agreement available at
|
||||||
|
// %InstallDIR%\features\org.talend.rcp.branding.%PRODUCTNAME%\%PRODUCTNAME%license.txt
|
||||||
|
//
|
||||||
|
// You should have received a copy of the agreement
|
||||||
|
// along with this program; if not, write to Talend SA
|
||||||
|
// 9 rue Pages 92150 Suresnes, France
|
||||||
|
//
|
||||||
|
// ============================================================================
|
||||||
|
package routines;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Empty routine entry to include library dependencies for generated DQ routines, such as "antlr" and
|
||||||
|
* "dataquality.parser" library for routines generated by tStandardizeRow component.
|
||||||
|
*/
|
||||||
|
public class DataQualityDependencies {
|
||||||
|
/**
|
||||||
|
* intentionally left empty
|
||||||
|
*/
|
||||||
|
}
|
||||||
@@ -0,0 +1,190 @@
|
|||||||
|
// ============================================================================
|
||||||
|
//
|
||||||
|
// Copyright (C) 2006-2019 Talend Inc. - www.talend.com
|
||||||
|
//
|
||||||
|
// This source code is available under agreement available at
|
||||||
|
// %InstallDIR%\features\org.talend.rcp.branding.%PRODUCTNAME%\%PRODUCTNAME%license.txt
|
||||||
|
//
|
||||||
|
// You should have received a copy of the agreement
|
||||||
|
// along with this program; if not, write to Talend SA
|
||||||
|
// 9 rue Pages 92150 Suresnes, France
|
||||||
|
//
|
||||||
|
// ============================================================================
|
||||||
|
package routines;
|
||||||
|
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Data quality String handling routines.
|
||||||
|
*/
|
||||||
|
public class DqStringHandling {
|
||||||
|
|
||||||
|
private static final Pattern INITIALS = Pattern.compile("^[A-Z]([. &-/|][A-Z])*[. &-/|]?$"); //$NON-NLS-1$
|
||||||
|
|
||||||
|
/**
|
||||||
|
* containsOnlyInitials: return true if the given string only contains initials such as "A", "A.", "A.I." or
|
||||||
|
* "A.I.D.S",.
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* {talendTypes} Boolean
|
||||||
|
*
|
||||||
|
* {Category} User Defined
|
||||||
|
*
|
||||||
|
* {param} string("A.I.D.S") str: The string that will be checked for initials
|
||||||
|
*
|
||||||
|
* {example} containsOnlyInitials("A.I.D.S.") # true.
|
||||||
|
*/
|
||||||
|
public static boolean containsOnlyInitials(String str) {
|
||||||
|
if (str == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
Matcher matcher = INITIALS.matcher(str);
|
||||||
|
return (matcher.find());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* makeSafe: returns an empty string when the given string is null.
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} Data Quality
|
||||||
|
*
|
||||||
|
* {param} string("world") str: The string to make safe.
|
||||||
|
*
|
||||||
|
* {example} makeSafe("world") # world.
|
||||||
|
*/
|
||||||
|
public static String makeSafe(String str) {
|
||||||
|
return (str == null) ? "" : str; //$NON-NLS-1$
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* safeTrim: returns the trimmed string or the empty string when the string is null.
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} Data Quality
|
||||||
|
*
|
||||||
|
* {param} string("world") str: The string to trim (can be null).
|
||||||
|
*
|
||||||
|
* {example} safeTrim(" world ") # world.
|
||||||
|
*/
|
||||||
|
public static String safeTrim(String str) {
|
||||||
|
return makeSafe(str).trim();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* safeConcat: returns the concatenation of the trimmed strings. The separator character is used when none of the
|
||||||
|
* given strings is empty or null.
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} Data Quality
|
||||||
|
*
|
||||||
|
* {param} string("hello") str1: The string to concatenate (can be null).
|
||||||
|
*
|
||||||
|
* {param} string("world") str2: The string to concatenate (can be null).
|
||||||
|
*
|
||||||
|
* {param} char('|') separator: the separator to be used in the concatenation of the strings
|
||||||
|
*
|
||||||
|
* {example} safeConcat("hello "," world ", '!') # hello|world.
|
||||||
|
*/
|
||||||
|
public static String safeConcat(String str1, String str2, char separator) {
|
||||||
|
String safeStr1 = safeTrim(str1);
|
||||||
|
String safeStr2 = safeTrim(str2);
|
||||||
|
if (safeStr1.length() == 0) {
|
||||||
|
return safeStr2;
|
||||||
|
} // else
|
||||||
|
if (safeStr2.length() == 0) {
|
||||||
|
return safeStr1;
|
||||||
|
} // else
|
||||||
|
return safeStr1 + separator + safeStr2;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* safeConcatMerge: returns the concatenation of the trimmed and unique strings. The separator character is used
|
||||||
|
* when none of the given strings is empty or null. If two strings are the same, only one is concatenated. The order
|
||||||
|
* of the output does may be different from the input order.
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} Data Quality
|
||||||
|
*
|
||||||
|
* {param} char('|') separator: the separator to be used in the concatenation of the strings
|
||||||
|
*
|
||||||
|
* {param} strings("hello","world") strings: The string to concatenate (can be null).
|
||||||
|
*
|
||||||
|
* {example} safeConcat( '|', "hello "," world ", "world") # hello|world.
|
||||||
|
*/
|
||||||
|
public static String safeConcatMerge(char separator, String... strings) {
|
||||||
|
|
||||||
|
if (strings == null) {
|
||||||
|
return ""; //$NON-NLS-1$
|
||||||
|
}
|
||||||
|
|
||||||
|
HashSet<String> hs = new HashSet<String>();
|
||||||
|
String resultingString = ""; //$NON-NLS-1$
|
||||||
|
for (String string : strings) {
|
||||||
|
String safeString = safeTrim(string);
|
||||||
|
if (hs.add(safeString)) {
|
||||||
|
resultingString = safeConcat(resultingString, safeString, separator);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return resultingString;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* safeConcat: returns the concatenation of the trimmed strings. The separator character is used when none of the
|
||||||
|
* given strings is empty or null.
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} Data Quality
|
||||||
|
*
|
||||||
|
* {param} char("|") separator: the separator to be used in the concatenation of the strings
|
||||||
|
*
|
||||||
|
* {param} strings("hello","world") strings: The string to concatenate (can be null).
|
||||||
|
*
|
||||||
|
* {example} safeConcat( '|', "hello "," world ") # hello|world.
|
||||||
|
*/
|
||||||
|
public static String safeConcat(char separator, String... strings) {
|
||||||
|
if (strings == null) {
|
||||||
|
return ""; //$NON-NLS-1$
|
||||||
|
}
|
||||||
|
String resultingString = ""; //$NON-NLS-1$
|
||||||
|
for (String string : strings) {
|
||||||
|
String safeString = safeTrim(string);
|
||||||
|
resultingString = safeConcat(resultingString, safeString, separator);
|
||||||
|
}
|
||||||
|
return resultingString;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* validAscii: validate the ascii format or not
|
||||||
|
*
|
||||||
|
* {talendTypes} boolean
|
||||||
|
*
|
||||||
|
* {Category} User Defined
|
||||||
|
*
|
||||||
|
* {param} string("text") text: The text to validate.
|
||||||
|
*
|
||||||
|
* {example} validAscii("text") # true
|
||||||
|
*/
|
||||||
|
public static boolean validAscii(String text) {
|
||||||
|
boolean result = true;
|
||||||
|
for (int i = 0; i < text.length(); i++) {
|
||||||
|
int charCode = text.charAt(i);
|
||||||
|
if (charCode < 33 || charCode > 126) {
|
||||||
|
result = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
256
MDM-LCAG-JavaFunctions/src/main/java/routines/GeoUtil.java
Normal file
256
MDM-LCAG-JavaFunctions/src/main/java/routines/GeoUtil.java
Normal file
@@ -0,0 +1,256 @@
|
|||||||
|
package routines;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* user specification: the function's comment should contain keys as follows: 1. write about the function's comment.but
|
||||||
|
* it must be before the "{talendTypes}" key.
|
||||||
|
*
|
||||||
|
* 2. {talendTypes} 's value must be talend Type, it is required . its value should be one of: String, char | Character,
|
||||||
|
* long | Long, int | Integer, boolean | Boolean, byte | Byte, Date, double | Double, float | Float, Object, short |
|
||||||
|
* Short
|
||||||
|
*
|
||||||
|
* 3. {Category} define a category for the Function. it is required. its value is user-defined .
|
||||||
|
*
|
||||||
|
* 4. {param} 's format is: {param} <type>[(<default value or closed list values>)] <name>[ : <comment>]
|
||||||
|
*
|
||||||
|
* <type> 's value should be one of: string, int, list, double, object, boolean, long, char, date. <name>'s value is the
|
||||||
|
* Function's parameter name. the {param} is optional. so if you the Function without the parameters. the {param} don't
|
||||||
|
* added. you can have many parameters for the Function.
|
||||||
|
*
|
||||||
|
* 5. {example} gives a example for the Function. it is optional.
|
||||||
|
*/
|
||||||
|
public class GeoUtil {
|
||||||
|
|
||||||
|
private static LcagLogger log = LcagLogger.getLogger(LcagDateTime.class);
|
||||||
|
|
||||||
|
public static class GeoCoord {
|
||||||
|
public static char NOT_INITIALIZED = '-';
|
||||||
|
public int degrees;
|
||||||
|
public int minutes;
|
||||||
|
public double seconds;
|
||||||
|
public char direction;
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
GeoCoord coord = this.roundToSeconds();
|
||||||
|
String result = "";
|
||||||
|
|
||||||
|
if (coord.direction == 'E' || coord.direction == 'W') {
|
||||||
|
result = String.format("%03d.%02d.%02.0f%c", coord.degrees, coord.minutes, coord.seconds, coord.direction);
|
||||||
|
} else if (coord.direction == 'N' || coord.direction == 'S') {
|
||||||
|
result = String.format("%02d.%02d.%02.0f%c", coord.degrees, coord.minutes, coord.seconds, coord.direction);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public GeoCoord() { direction = NOT_INITIALIZED; }
|
||||||
|
|
||||||
|
public GeoCoord(Integer degrees, Integer minutes, Double seconds, String direction) {
|
||||||
|
this.direction = NOT_INITIALIZED;
|
||||||
|
|
||||||
|
if (degrees != null && minutes != null && seconds != null && "EWNS".contains(direction)) {
|
||||||
|
this.degrees = degrees;
|
||||||
|
this.minutes = minutes;
|
||||||
|
this.seconds = seconds;
|
||||||
|
this.direction = direction.substring(0,1).toUpperCase().charAt(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public GeoCoord(double aValue, boolean isLongitude) {
|
||||||
|
double f = Math.abs(aValue);
|
||||||
|
this.degrees = (int)Math.floor(f);
|
||||||
|
this.minutes = (int)Math.floor( (f * 60.0f) % 60.0f);
|
||||||
|
this.seconds = (f * 3600.0f) % 60.0f;
|
||||||
|
|
||||||
|
if (isLongitude) {
|
||||||
|
this.direction = (aValue < 0) ? 'W' : 'E';
|
||||||
|
} else {
|
||||||
|
this.direction = (aValue < 0) ? 'S' : 'N';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public GeoCoord(String aCoord) {
|
||||||
|
if (aCoord == null)
|
||||||
|
throw new IllegalArgumentException("null argument not allowed in GeoCoord constructor");
|
||||||
|
|
||||||
|
int len = aCoord.length();
|
||||||
|
String[] dms = aCoord.substring(0,len-1).split("[\\.,]");
|
||||||
|
|
||||||
|
this.degrees = Integer.parseInt(dms[0]);
|
||||||
|
this.minutes = Integer.parseInt(dms[1]);
|
||||||
|
this.seconds = (dms.length > 2) ? Double.parseDouble(dms[2]) : 0f;
|
||||||
|
|
||||||
|
this.direction = aCoord.substring(len - 1).toUpperCase().charAt(0);
|
||||||
|
|
||||||
|
if ("EWSN".indexOf(this.direction) < 0 || this.degrees < 0 || this.minutes < 0 || this.seconds < 0) {
|
||||||
|
throw new IllegalArgumentException(String.format("geo coordinate '%s'is not well-formed", aCoord));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public Double toDecimal() {
|
||||||
|
Double result = null;
|
||||||
|
|
||||||
|
if (this.direction != NOT_INITIALIZED) {
|
||||||
|
double res = new Double(this.degrees + this.minutes/60.0 + this.seconds / 3600.0);
|
||||||
|
|
||||||
|
switch(this.direction) {
|
||||||
|
case 'S':
|
||||||
|
case 'W':
|
||||||
|
res = -res;
|
||||||
|
break;
|
||||||
|
case 'E':
|
||||||
|
case 'N':
|
||||||
|
break;
|
||||||
|
default: throw new IllegalStateException("Ill geo direction: " + this.direction);
|
||||||
|
}
|
||||||
|
|
||||||
|
result = new Double(res);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public GeoCoord clone() {
|
||||||
|
GeoCoord result = new GeoCoord();
|
||||||
|
|
||||||
|
result.seconds = this.seconds;
|
||||||
|
result.minutes = this.minutes;
|
||||||
|
result.degrees = this.degrees;
|
||||||
|
result.direction = this.direction;
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return new Object where seconds are rounded up to full seconds
|
||||||
|
*/
|
||||||
|
public GeoCoord roundToSeconds() {
|
||||||
|
GeoCoord result = this.clone();
|
||||||
|
|
||||||
|
result.seconds = Math.round(this.seconds);
|
||||||
|
|
||||||
|
if (result.seconds == 60f) {
|
||||||
|
result.seconds = 0f;
|
||||||
|
|
||||||
|
result.minutes += 1;
|
||||||
|
if (result.minutes == 60) {
|
||||||
|
result.minutes = 0;
|
||||||
|
result.degrees += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* formatCoordinate: Formats a geo-location to DDD.MM.SSd where d is one of the directions 'E', 'W', 'N', 'S'
|
||||||
|
*
|
||||||
|
* @param aCoord - the coordinate to format (may be a longitude or a latitude)
|
||||||
|
*
|
||||||
|
* @return standard formatted geo position as String
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} LCAG
|
||||||
|
*
|
||||||
|
* {param} String(aCoord) aCoord: the coordinate to format (may be a longitude or a latitude)
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static String formatCoordinate(String aCoord)
|
||||||
|
{
|
||||||
|
String result = null;
|
||||||
|
|
||||||
|
if (aCoord != null) {
|
||||||
|
GeoCoord coord = new GeoCoord(aCoord);
|
||||||
|
result = coord.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* formatLongitude: Formats a geo-location to DDD.MM.SSd where d is one of the directions 'E', 'W', 'N', 'S'
|
||||||
|
*
|
||||||
|
* @param aCoord - the coordinate decimal value to format
|
||||||
|
*
|
||||||
|
* @return standard formatted geo position as String
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} LCAG
|
||||||
|
*
|
||||||
|
* {param} Double(aCoord) aCoord: the coordinate decimal value to format
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static String formatLongitude(Double aCoord)
|
||||||
|
{
|
||||||
|
String result = null;
|
||||||
|
|
||||||
|
if (aCoord != null) {
|
||||||
|
GeoCoord coord = new GeoCoord(aCoord, true);
|
||||||
|
result = coord.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* formatLatitude: Formats a geo-location to DDD.MM.SSd where d is one of the directions 'E', 'W', 'N', 'S'
|
||||||
|
*
|
||||||
|
* @param aCoord - the coordinate decimal value to format
|
||||||
|
*
|
||||||
|
* @return standard formatted geo position as String
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} LCAG
|
||||||
|
*
|
||||||
|
* {param} Double(aCoord) aCoord: the coordinate decimal value to format
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static String formatLatitude(Double aCoord)
|
||||||
|
{
|
||||||
|
String result = null;
|
||||||
|
|
||||||
|
if (aCoord != null) {
|
||||||
|
GeoCoord coord = new GeoCoord(aCoord, false);
|
||||||
|
result = coord.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* formatCoordinate: Formats a geo-location in MaD format to DDD.MM.SSd where d is one of the directions 'E', 'W', 'N', 'S'
|
||||||
|
*
|
||||||
|
* @param deg - degrees
|
||||||
|
* @param min - minutes
|
||||||
|
* @param direction - any direction 'E', 'W', 'N', 'S'
|
||||||
|
*
|
||||||
|
* @return standard formatted geo position as String
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} LCAG
|
||||||
|
*
|
||||||
|
* {param} Integer(deg) deg: degrees
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static String formatCoordinate(Integer deg, Integer min, String direction)
|
||||||
|
{
|
||||||
|
String result = null;
|
||||||
|
|
||||||
|
if (deg != null && min != null && direction != null) {
|
||||||
|
GeoCoord coord = new GeoCoord(deg.intValue(), min.intValue(), 0.0, direction);
|
||||||
|
result = coord.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
41
MDM-LCAG-JavaFunctions/src/main/java/routines/JMSCPTEST.java
Normal file
41
MDM-LCAG-JavaFunctions/src/main/java/routines/JMSCPTEST.java
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
package routines;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* user specification: the function's comment should contain keys as follows: 1. write about the function's comment.but
|
||||||
|
* it must be before the "{talendTypes}" key.
|
||||||
|
*
|
||||||
|
* 2. {talendTypes} 's value must be talend Type, it is required . its value should be one of: String, char | Character,
|
||||||
|
* long | Long, int | Integer, boolean | Boolean, byte | Byte, Date, double | Double, float | Float, Object, short |
|
||||||
|
* Short
|
||||||
|
*
|
||||||
|
* 3. {Category} define a category for the Function. it is required. its value is user-defined .
|
||||||
|
*
|
||||||
|
* 4. {param} 's format is: {param} <type>[(<default value or closed list values>)] <name>[ : <comment>]
|
||||||
|
*
|
||||||
|
* <type> 's value should be one of: string, int, list, double, object, boolean, long, char, date. <name>'s value is the
|
||||||
|
* Function's parameter name. the {param} is optional. so if you the Function without the parameters. the {param} don't
|
||||||
|
* added. you can have many parameters for the Function.
|
||||||
|
*
|
||||||
|
* 5. {example} gives a example for the Function. it is optional.
|
||||||
|
*/
|
||||||
|
public class JMSCPTEST {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* helloExample: not return value, only print "hello" + message.
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} User Defined
|
||||||
|
*
|
||||||
|
* {param} string("world") input: The string need to be printed.
|
||||||
|
*
|
||||||
|
* {example} helloExemple("world") # hello world !.
|
||||||
|
*/
|
||||||
|
public static void helloExample(String message) {
|
||||||
|
if (message == null) {
|
||||||
|
message = "World"; //$NON-NLS-1$
|
||||||
|
}
|
||||||
|
System.out.println("Hello " + message + " !"); //$NON-NLS-1$ //$NON-NLS-2$
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,255 @@
|
|||||||
|
package routines;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* user specification: the function's comment should contain keys as follows: 1. write about the function's comment.but
|
||||||
|
* it must be before the "{talendTypes}" key.
|
||||||
|
*
|
||||||
|
* 2. {talendTypes} 's value must be talend Type, it is required . its value should be one of: String, char | Character,
|
||||||
|
* long | Long, int | Integer, boolean | Boolean, byte | Byte, Date, double | Double, float | Float, Object, short |
|
||||||
|
* Short
|
||||||
|
*
|
||||||
|
* 3. {Category} define a category for the Function. it is required. its value is user-defined .
|
||||||
|
*
|
||||||
|
* 4. {param} 's format is: {param} <type>[(<default value or closed list values>)] <name>[ : <comment>]
|
||||||
|
*
|
||||||
|
* <type> 's value should be one of: string, int, list, double, object, boolean, long, char, date. <name>'s value is the
|
||||||
|
* Function's parameter name. the {param} is optional. so if you the Function without the parameters. the {param} don't
|
||||||
|
* added. you can have many parameters for the Function.
|
||||||
|
*
|
||||||
|
* 5. {example} gives a example for the Function. it is optional.
|
||||||
|
*/
|
||||||
|
public class GeoUtil {
|
||||||
|
|
||||||
|
private static LcagLogger log = LcagLogger.getLogger(LcagDateTime.class);
|
||||||
|
|
||||||
|
public static class GeoCoord {
|
||||||
|
public static char NOT_INITIALIZED = '-';
|
||||||
|
public int degrees;
|
||||||
|
public int minutes;
|
||||||
|
public double seconds;
|
||||||
|
public char direction;
|
||||||
|
|
||||||
|
private GeoCoord() { direction = NOT_INITIALIZED; }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
GeoCoord coord = this.roundToSeconds();
|
||||||
|
String result = "";
|
||||||
|
|
||||||
|
if (coord.direction == 'E' || coord.direction == 'W') {
|
||||||
|
result = String.format("%03d.%02d.%02.0f%c", coord.degrees, coord.minutes, coord.seconds, coord.direction);
|
||||||
|
} else if (coord.direction == 'N' || coord.direction == 'S') {
|
||||||
|
result = String.format("%02d.%02d.%02.0f%c", coord.degrees, coord.minutes, coord.seconds, coord.direction);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public GeoCoord(Integer degrees, Integer minutes, Double seconds, String direction) {
|
||||||
|
this.direction = NOT_INITIALIZED;
|
||||||
|
|
||||||
|
if (degrees != null && minutes != null && seconds != null && "EWNS".contains(direction)) {
|
||||||
|
this.degrees = degrees;
|
||||||
|
this.minutes = minutes;
|
||||||
|
this.seconds = seconds;
|
||||||
|
this.direction = direction.substring(0,1).toUpperCase().charAt(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public GeoCoord(double aValue, boolean isLongitude) {
|
||||||
|
double f = Math.abs(aValue);
|
||||||
|
this.degrees = (int)Math.floor(f);
|
||||||
|
this.minutes = (int)Math.floor( (f * 60.0f) % 60.0f);
|
||||||
|
this.seconds = (f * 3600.0f) % 60.0f;
|
||||||
|
|
||||||
|
if (isLongitude) {
|
||||||
|
this.direction = (aValue < 0) ? 'W' : 'E';
|
||||||
|
} else {
|
||||||
|
this.direction = (aValue < 0) ? 'S' : 'N';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public GeoCoord(String aCoord) {
|
||||||
|
if (aCoord == null)
|
||||||
|
throw new IllegalArgumentException("null argument not allowed in GeoCoord constructor");
|
||||||
|
|
||||||
|
int len = aCoord.length();
|
||||||
|
String[] dms = aCoord.substring(0,len-1).split("[\\.,]");
|
||||||
|
|
||||||
|
this.degrees = Integer.parseInt(dms[0]);
|
||||||
|
this.minutes = Integer.parseInt(dms[1]);
|
||||||
|
this.seconds = (dms.length > 2) ? Double.parseDouble(dms[2]) : 0f;
|
||||||
|
|
||||||
|
this.direction = aCoord.substring(len - 1).toUpperCase().charAt(0);
|
||||||
|
|
||||||
|
if ("EWSN".indexOf(this.direction) < 0 || this.degrees < 0 || this.minutes < 0 || this.seconds < 0) {
|
||||||
|
throw new IllegalArgumentException(String.format("geo coordinate '%s'is not well-formed", aCoord));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public Double toDecimal() {
|
||||||
|
Double result = null;
|
||||||
|
|
||||||
|
if (this.direction != NOT_INITIALIZED) {
|
||||||
|
double res = new Double(this.degrees + this.minutes/60.0 + this.seconds / 3600.0);
|
||||||
|
|
||||||
|
switch(this.direction) {
|
||||||
|
case 'S':
|
||||||
|
case 'W':
|
||||||
|
res = -res;
|
||||||
|
break;
|
||||||
|
case 'E':
|
||||||
|
case 'N':
|
||||||
|
break;
|
||||||
|
default: throw new IllegalStateException("Ill geo direction: " + this.direction);
|
||||||
|
}
|
||||||
|
|
||||||
|
result = new Double(res);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public GeoCoord clone() {
|
||||||
|
GeoCoord result = new GeoCoord();
|
||||||
|
|
||||||
|
result.seconds = this.seconds;
|
||||||
|
result.minutes = this.minutes;
|
||||||
|
result.degrees = this.degrees;
|
||||||
|
result.direction = this.direction;
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return new Object where seconds are rounded up to full seconds
|
||||||
|
*/
|
||||||
|
public GeoCoord roundToSeconds() {
|
||||||
|
GeoCoord result = this.clone();
|
||||||
|
|
||||||
|
result.seconds = Math.round(this.seconds);
|
||||||
|
|
||||||
|
if (result.seconds == 60f) {
|
||||||
|
result.seconds = 0f;
|
||||||
|
|
||||||
|
result.minutes += 1;
|
||||||
|
if (result.minutes == 60) {
|
||||||
|
result.minutes = 0;
|
||||||
|
result.degrees += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* formatCoordinate: Formats a geo-location to DDD.MM.SSd where d is one of the directions 'E', 'W', 'N', 'S'
|
||||||
|
*
|
||||||
|
* @param aCoord - the coordinate to format (may be a longitude or a latitude)
|
||||||
|
*
|
||||||
|
* @return standard formatted geo position as String
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} LCAG
|
||||||
|
*
|
||||||
|
* {param} String(aCoord) aCoord: the coordinate to format (may be a longitude or a latitude)
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static String formatCoordinate(String aCoord)
|
||||||
|
{
|
||||||
|
String result = null;
|
||||||
|
|
||||||
|
if (aCoord != null) {
|
||||||
|
GeoCoord coord = new GeoCoord(aCoord);
|
||||||
|
result = coord.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* formatLongitude: Formats a geo-location to DDD.MM.SSd where d is one of the directions 'E', 'W', 'N', 'S'
|
||||||
|
*
|
||||||
|
* @param aCoord - the coordinate decimal value to format
|
||||||
|
*
|
||||||
|
* @return standard formatted geo position as String
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} LCAG
|
||||||
|
*
|
||||||
|
* {param} Double(aCoord) aCoord: the coordinate decimal value to format
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static String formatLongitude(Double aCoord)
|
||||||
|
{
|
||||||
|
String result = null;
|
||||||
|
|
||||||
|
if (aCoord != null) {
|
||||||
|
GeoCoord coord = new GeoCoord(aCoord, true);
|
||||||
|
result = coord.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* formatLatitude: Formats a geo-location to DDD.MM.SSd where d is one of the directions 'E', 'W', 'N', 'S'
|
||||||
|
*
|
||||||
|
* @param aCoord - the coordinate decimal value to format
|
||||||
|
*
|
||||||
|
* @return standard formatted geo position as String
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} LCAG
|
||||||
|
*
|
||||||
|
* {param} Double(aCoord) aCoord: the coordinate decimal value to format
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static String formatLatitude(Double aCoord)
|
||||||
|
{
|
||||||
|
String result = null;
|
||||||
|
|
||||||
|
if (aCoord != null) {
|
||||||
|
GeoCoord coord = new GeoCoord(aCoord, false);
|
||||||
|
result = coord.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* formatCoordinate: Formats a geo-location in MaD format to DDD.MM.SSd where d is one of the directions 'E', 'W', 'N', 'S'
|
||||||
|
*
|
||||||
|
* @param deg - degrees
|
||||||
|
* @param min - minutes
|
||||||
|
* @param direction - any direction 'E', 'W', 'N', 'S'
|
||||||
|
*
|
||||||
|
* @return standard formatted geo position as String
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} LCAG
|
||||||
|
*
|
||||||
|
* {param} Integer(deg) deg: degrees
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static String formatCoordinate(Integer deg, Integer min, String direction)
|
||||||
|
{
|
||||||
|
String result = null;
|
||||||
|
|
||||||
|
if (deg != null && min != null && direction != null) {
|
||||||
|
GeoCoord coord = new GeoCoord(deg.intValue(), min.intValue(), 0.0, direction);
|
||||||
|
result = coord.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
package routines;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* user specification: the function's comment should contain keys as follows: 1. write about the function's comment.but
|
||||||
|
* it must be before the "{talendTypes}" key.
|
||||||
|
*
|
||||||
|
* 2. {talendTypes} 's value must be talend Type, it is required . its value should be one of: String, char | Character,
|
||||||
|
* long | Long, int | Integer, boolean | Boolean, byte | Byte, Date, double | Double, float | Float, Object, short |
|
||||||
|
* Short
|
||||||
|
*
|
||||||
|
* 3. {Category} define a category for the Function. it is required. its value is user-defined .
|
||||||
|
*
|
||||||
|
* 4. {param} 's format is: {param} <type>[(<default value or closed list values>)] <name>[ : <comment>]
|
||||||
|
*
|
||||||
|
* <type> 's value should be one of: string, int, list, double, object, boolean, long, char, date. <name>'s value is the
|
||||||
|
* Function's parameter name. the {param} is optional. so if you the Function without the parameters. the {param} don't
|
||||||
|
* added. you can have many parameters for the Function.
|
||||||
|
*
|
||||||
|
* 5. {example} gives a example for the Function. it is optional.
|
||||||
|
*/
|
||||||
|
public class LCAGXML2JSON {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* helloExample: not return value, only print "hello" + message.
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} User Defined
|
||||||
|
*
|
||||||
|
* {param} string("world") input: The string need to be printed.
|
||||||
|
*
|
||||||
|
* {example} helloExemple("world") # hello world !.
|
||||||
|
*/
|
||||||
|
public static void helloExample(String message) {
|
||||||
|
if (message == null) {
|
||||||
|
message = "World"; //$NON-NLS-1$
|
||||||
|
}
|
||||||
|
System.out.println("Hello " + message + " !"); //$NON-NLS-1$ //$NON-NLS-2$
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,87 @@
|
|||||||
|
package routines;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* user specification: the function's comment should contain keys as follows: 1. write about the function's comment.but
|
||||||
|
* it must be before the "{talendTypes}" key.
|
||||||
|
*
|
||||||
|
* 2. {talendTypes} 's value must be talend Type, it is required . its value should be one of: String, char | Character,
|
||||||
|
* long | Long, int | Integer, boolean | Boolean, byte | Byte, Date, double | Double, float | Float, Object, short |
|
||||||
|
* Short
|
||||||
|
*
|
||||||
|
* 3. {Category} define a category for the Function. it is required. its value is user-defined .
|
||||||
|
*
|
||||||
|
* 4. {param} 's format is: {param} <type>[(<default value or closed list values>)] <name>[ : <comment>]
|
||||||
|
*
|
||||||
|
* <type> 's value should be one of: string, int, list, double, object, boolean, long, char, date. <name>'s value is the
|
||||||
|
* Function's parameter name. the {param} is optional. so if you the Function without the parameters. the {param} don't
|
||||||
|
* added. you can have many parameters for the Function.
|
||||||
|
*
|
||||||
|
* 5. {example} gives a example for the Function. it is optional.
|
||||||
|
*/
|
||||||
|
public class LcagBusinessCode {
|
||||||
|
private static LcagLogger log = LcagLogger.getLogger(LcagDateTime.class);
|
||||||
|
private static Map<String, Pattern> LOCATION_PATTERN = null;
|
||||||
|
|
||||||
|
public LcagBusinessCode() {} { }
|
||||||
|
|
||||||
|
private static Map<String, Pattern> getLocationPatterns() {
|
||||||
|
if (LOCATION_PATTERN != null) return LOCATION_PATTERN;
|
||||||
|
|
||||||
|
LOCATION_PATTERN = new HashMap<String, Pattern>();
|
||||||
|
List<String>locPatternList = null;
|
||||||
|
int flags = Pattern.CASE_INSENSITIVE;
|
||||||
|
|
||||||
|
LOCATION_PATTERN.put("Airport", Pattern.compile(".*airport.*|.*apt$", flags));
|
||||||
|
LOCATION_PATTERN.put("Bus Station", Pattern.compile(".* bus .*|.*central.b*", flags));
|
||||||
|
LOCATION_PATTERN.put("Harbour", Pattern.compile(".* harbour.*", flags));
|
||||||
|
LOCATION_PATTERN.put("Heliport", Pattern.compile(".*heliport.*", flags));
|
||||||
|
LOCATION_PATTERN.put("Off-Line Point", Pattern.compile(".*off.line p.*|.*off$|.*off.l.{0,2}$", flags));
|
||||||
|
LOCATION_PATTERN.put("Rail Station", Pattern.compile(".* rail .*|.*central.r.*", flags));
|
||||||
|
//LOCATION_PATTERN.put("Miscellaneous", null);
|
||||||
|
|
||||||
|
return LOCATION_PATTERN;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* deriveLocationType: Parse a Station Name and figure out if it is an Airport, a Bus Station, etc..
|
||||||
|
*
|
||||||
|
* @param Original Station Name
|
||||||
|
* @return null if no location Type can be derived, otherwise a IATA Location Type
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} LCAG
|
||||||
|
*
|
||||||
|
* {param} String("aName") aName: Original Station Name
|
||||||
|
*
|
||||||
|
* {examples}
|
||||||
|
*/
|
||||||
|
public static String deriveLocationType(String aName)
|
||||||
|
{
|
||||||
|
// Function implemented because BE input often uses Airport but the name indicate a different location.
|
||||||
|
String result = null;
|
||||||
|
Map<String, Pattern> locPatternList = LcagBusinessCode.getLocationPatterns();
|
||||||
|
|
||||||
|
for (String locType : locPatternList.keySet()) {
|
||||||
|
Pattern pattern = locPatternList.get(locType);
|
||||||
|
|
||||||
|
if (pattern == null) continue;
|
||||||
|
|
||||||
|
if (pattern.matcher(aName).matches()) {
|
||||||
|
result = locType;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
package routines;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* user specification: the function's comment should contain keys as follows: 1. write about the function's comment.but
|
||||||
|
* it must be before the "{talendTypes}" key.
|
||||||
|
*
|
||||||
|
* 2. {talendTypes} 's value must be talend Type, it is required . its value should be one of: String, char | Character,
|
||||||
|
* long | Long, int | Integer, boolean | Boolean, byte | Byte, Date, double | Double, float | Float, Object, short |
|
||||||
|
* Short
|
||||||
|
*
|
||||||
|
* 3. {Category} define a category for the Function. it is required. its value is user-defined .
|
||||||
|
*
|
||||||
|
* 4. {param} 's format is: {param} <type>[(<default value or closed list values>)] <name>[ : <comment>]
|
||||||
|
*
|
||||||
|
* <type> 's value should be one of: string, int, list, double, object, boolean, long, char, date. <name>'s value is the
|
||||||
|
* Function's parameter name. the {param} is optional. so if you the Function without the parameters. the {param} don't
|
||||||
|
* added. you can have many parameters for the Function.
|
||||||
|
*
|
||||||
|
* 5. {example} gives a example for the Function. it is optional.
|
||||||
|
*/
|
||||||
|
public class LcagConstant {
|
||||||
|
|
||||||
|
/* Date Patterns */
|
||||||
|
public static final String FILE_TIMESTAMP_FORMAT="yyyy-MM-dd_HH-mm-ss-SSS";
|
||||||
|
|
||||||
|
/* LOG Level, sequence taken over from apache commons logging */
|
||||||
|
public static final int LOG_TRACE = 2;
|
||||||
|
public static final int LOG_DEBUG = 1;
|
||||||
|
public static final int LOG_INFO = 0;
|
||||||
|
public static final int LOG_WARN = -1;
|
||||||
|
public static final int LOG_ERROR = -2;
|
||||||
|
public static final int LOG_FATAL = -3;
|
||||||
|
}
|
||||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,467 @@
|
|||||||
|
package routines;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
import org.apache.commons.io.FilenameUtils;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* user specification: the function's comment should contain keys as follows: 1. write about the function's comment.but
|
||||||
|
* it must be before the "{talendTypes}" key.
|
||||||
|
*
|
||||||
|
* 2. {talendTypes} 's value must be talend Type, it is required . its value should be one of: String, char | Character,
|
||||||
|
* long | Long, int | Integer, boolean | Boolean, byte | Byte, Date, double | Double, float | Float, Object, short |
|
||||||
|
* Short
|
||||||
|
*
|
||||||
|
* 3. {Category} define a category for the Function. it is required. its value is user-defined .
|
||||||
|
*
|
||||||
|
* 4. {param} 's format is: {param} <type>[(<default value or closed list values>)] <name>[ : <comment>]
|
||||||
|
*
|
||||||
|
* <type> 's value should be one of: string, int, list, double, object, boolean, long, char, date. <name>'s value is the
|
||||||
|
* Function's parameter name. the {param} is optional. so if you the Function without the parameters. the {param} don't
|
||||||
|
* added. you can have many parameters for the Function.
|
||||||
|
*
|
||||||
|
* 5. {example} gives a example for the Function. it is optional.
|
||||||
|
*/
|
||||||
|
public class LcagFileTools
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
private static LcagLogger log = LcagLogger.getLogger(LcagFileTools.class);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* getName: Get only the name of the given file path (name with extension)
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} LCAG-File
|
||||||
|
*
|
||||||
|
* {param} string filePath: File path to use
|
||||||
|
*
|
||||||
|
* {example} getName("a/b/HelloWorld.txt") = "HelloWorld.txt"
|
||||||
|
*/
|
||||||
|
public static String getName(String filePath) {
|
||||||
|
String result = FilenameUtils.getName(filePath);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* getBaseName: Get the base name of the given file path (without extension)
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} LCAG-File
|
||||||
|
*
|
||||||
|
* {param} string filePath: File path to use
|
||||||
|
*
|
||||||
|
* {example} getName("a/b/HelloWorld.txt") = "HelloWorld"
|
||||||
|
*/
|
||||||
|
public static String getBaseName(String filePath) {
|
||||||
|
String result = FilenameUtils.getBaseName(filePath);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* getBaseName: Get the extension of the given file path.
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} LCAG-File
|
||||||
|
*
|
||||||
|
* {param} string filePath: File path to use
|
||||||
|
*
|
||||||
|
* {example} getName("a/b/HelloWorld.txt") = "txt"
|
||||||
|
*/
|
||||||
|
public static String getExtension(String filePath) {
|
||||||
|
String result = FilenameUtils.getExtension(filePath);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* getBaseName: Get the path of the given file path without the file name.
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} LCAG-File
|
||||||
|
*
|
||||||
|
* {param} string filePath: File path to use
|
||||||
|
*
|
||||||
|
* {example} getName("a/b/HelloWorld.txt") = "a/b";
|
||||||
|
*/
|
||||||
|
public static String getPath(String filePath) {
|
||||||
|
String result = FilenameUtils.getPath(filePath);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* readTextFile: Read a file as String
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} LCAG-File
|
||||||
|
*
|
||||||
|
* {param} string fileName: File to read
|
||||||
|
*
|
||||||
|
* {example} readTextFile("HelloWorld.txt")
|
||||||
|
*/
|
||||||
|
public static String readTextFile(String fileName) throws java.io.IOException {
|
||||||
|
String result = null;
|
||||||
|
File file = new File(fileName);
|
||||||
|
String fPath = file.getAbsolutePath();
|
||||||
|
|
||||||
|
java.nio.file.Path filePath = java.nio.file.Paths.get(fPath);
|
||||||
|
|
||||||
|
byte[] fileContent = java.nio.file.Files.readAllBytes(filePath);
|
||||||
|
|
||||||
|
result = new String(fileContent);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* createSameJobFileName: For a context.env_inputDirectory/fileName.ext create a new file name
|
||||||
|
* for the same job. The target in relative (see rePath) to the input directory: {targetDir}/fileName{preExtension}.ext
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} LCAG
|
||||||
|
*
|
||||||
|
* {param} String("srcFileName") input: source file name
|
||||||
|
* {param} String("targetDir") input: Target directory. use context.env_outputDir, context.env_tempDir, ...
|
||||||
|
* {param} String("preExtension") input: file name part to prepend before the file name extension
|
||||||
|
* {param} String("ext") input: use this extension instead of original one
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static String createSameJobFileName(String srcFileName, String targetDir, String preExtension, String ext)
|
||||||
|
{
|
||||||
|
if (srcFileName == null) return null;
|
||||||
|
if (preExtension == null) preExtension = "";
|
||||||
|
if (ext == null) ext = FilenameUtils.getExtension(srcFileName);
|
||||||
|
|
||||||
|
// get basename without extension
|
||||||
|
String baseName = FilenameUtils.getBaseName(srcFileName);
|
||||||
|
|
||||||
|
String result = String.format("%s/%s%s.%s", targetDir, baseName, preExtension, ext);
|
||||||
|
result = FilenameUtils.normalize(result);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* createSameJobFileName: For a context.env_inputDirectory/fileName.ext create a new file name
|
||||||
|
* for the same job. The target in relative (see rePath) to the input directory: {targetDir}/fileName{preExtension}.ext
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} LCAG
|
||||||
|
*
|
||||||
|
* {param} String("srcFileName") input: source file name
|
||||||
|
* {param} String("targetDir") input: Target directory. use context.env_outputDir, context.env_tempDir, ...
|
||||||
|
* {param} String("preExtension") input: file name part to prepend before the file name extension
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static String createSameJobFileName(String srcFileName, String targetDir, String preExtension)
|
||||||
|
{
|
||||||
|
if (srcFileName == null) return null;
|
||||||
|
|
||||||
|
String ext = FilenameUtils.getExtension(srcFileName);
|
||||||
|
|
||||||
|
String result = createSameJobFileName(srcFileName, targetDir, preExtension, ext);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* createJobFileName: For a context.env_inputDirectory/fileName.ext create a new file name
|
||||||
|
* as context.env_inputDirectory/../{jobName}/fileName{preExtension}.ext
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} LCAG
|
||||||
|
*
|
||||||
|
* {param} String("srcFileName") input: source file name
|
||||||
|
* {param} String("jobName") input: usually the 'jobName' variable in Talend Jobs
|
||||||
|
* {param} String("preExtension") input: file name part to prepend before the file name extension
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static String createJobFileName(String srcFileName, String jobName, String preExtension)
|
||||||
|
{
|
||||||
|
if (srcFileName == null) return null;
|
||||||
|
if (jobName == null) return srcFileName;
|
||||||
|
if (preExtension == null) preExtension = "";
|
||||||
|
|
||||||
|
File theFile = new File(srcFileName);
|
||||||
|
String dirName = theFile.getParent();
|
||||||
|
String baseName = FilenameUtils.getBaseName(srcFileName);
|
||||||
|
String ext = FilenameUtils.getExtension(srcFileName);
|
||||||
|
|
||||||
|
String result = String.format("%s/../%s/%s%s.%s", dirName, jobName, baseName, preExtension, ext);
|
||||||
|
result = FilenameUtils.normalize(result);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* createJobTempFileName: For a context.env_inputDirectory/fileName.ext create a new file name
|
||||||
|
* for the same jobs temp directory as context.env_inputDirectory/../{jobName}/fileName{preExtension}.ext
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} LCAG
|
||||||
|
*
|
||||||
|
* {param} String("srcFileName") input: source file name
|
||||||
|
* {param} String("jobName") input: usually the 'jobName' variable in Talend Jobs
|
||||||
|
* {param} String("preExtension") input: file name part to prepend before the file name extension
|
||||||
|
* {param} String("ext") input: use thzis extension instead of original one
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static String createJobTempFileName(String srcFileName, String preExtension, String ext)
|
||||||
|
{
|
||||||
|
String result = createSameJobFileName(srcFileName, "../../../temp", preExtension, ext);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* createInputDirFileName4OtherJob: For a context.env_inputDirectory/fileName.ext or context.env_inputDirectory/fileName.ext
|
||||||
|
* create a new file name for the input directory of the other given jobname as fileName{preExtension}.ext
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} LCAG-File
|
||||||
|
*
|
||||||
|
* {param} String("srcFileName") input: source file name
|
||||||
|
* {param} String("jobName") input: usually the 'jobName' variable in Talend Jobs
|
||||||
|
* {param} String("preExtension") input: file name part to prepend before the file name extension
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static String createInputDirFileName4OtherJob(String srcFileName, String jobName, String preExtension)
|
||||||
|
{
|
||||||
|
if (srcFileName == null) throw new IllegalArgumentException("File name argument must not be null");
|
||||||
|
|
||||||
|
if (jobName == null) return srcFileName;
|
||||||
|
if (preExtension == null) preExtension = "";
|
||||||
|
|
||||||
|
Path absPath = Paths.get(srcFileName).toAbsolutePath();
|
||||||
|
Path thisJobDir = absPath.getParent();
|
||||||
|
Path thisJobDirParent = thisJobDir.getParent();
|
||||||
|
String otherJobInputDir = null;
|
||||||
|
|
||||||
|
if (thisJobDirParent.endsWith("input")) {
|
||||||
|
otherJobInputDir = thisJobDirParent.toString();
|
||||||
|
} else if (thisJobDirParent.endsWith("temp")) {
|
||||||
|
otherJobInputDir = thisJobDirParent.getParent() + "/data/input/";
|
||||||
|
} else {
|
||||||
|
throw new UnsupportedOperationException("Source file name argument must be in env_inputDirectory or env_temDirectory");
|
||||||
|
}
|
||||||
|
|
||||||
|
String baseName = FilenameUtils.getBaseName(srcFileName);
|
||||||
|
String ext = FilenameUtils.getExtension(srcFileName);
|
||||||
|
String result = String.format("%s/%s/%s%s.%s", otherJobInputDir, jobName, baseName, preExtension, ext);
|
||||||
|
Path resultPath = Paths.get(result);
|
||||||
|
|
||||||
|
result = resultPath.normalize().toString().replace("\\", "/");
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* createJobTempFileName: For a context.env_inputDirectory/fileName.ext create a new file name
|
||||||
|
* for the same jobs temp directory as context.env_inputDirectory/../{jobName}/fileName{preExtension}
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} LCAG
|
||||||
|
*
|
||||||
|
* {param} String("srcFileName") input: source file name
|
||||||
|
* {param} String("preExtension") input: file name part to prepend before the file name extension
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
public static String createJobTempFileName(String srcFileName, String preExtension)
|
||||||
|
{
|
||||||
|
if (srcFileName == null) return null;
|
||||||
|
if (preExtension == null) preExtension = "";
|
||||||
|
|
||||||
|
File theFile = new File(srcFileName);
|
||||||
|
String dirName = theFile.getParent();
|
||||||
|
String jobName = FilenameUtils.getBaseName(dirName);
|
||||||
|
String baseName = FilenameUtils.getBaseName(srcFileName);
|
||||||
|
String ext = FilenameUtils.getExtension(srcFileName);
|
||||||
|
|
||||||
|
String result = String.format("%s/../../../temp/%s/%s%s.%s", dirName, jobName, baseName, preExtension, ext);
|
||||||
|
result = FilenameUtils.normalize(result);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* createTimestampedFileName: Used for receive jobs and export from DB Jobs, only.
|
||||||
|
*
|
||||||
|
* Creates a file name starting with a UTC timestamp for a given date. Drops
|
||||||
|
* leading directory path.
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} LCAG-File
|
||||||
|
*
|
||||||
|
* {param} String("aDate") input: Date to use for timestamp
|
||||||
|
* {param} String("srcFileName") input: source file name
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static String createTimestampedFileName(Date aDate, String srcFileName)
|
||||||
|
{
|
||||||
|
if ( aDate == null) {
|
||||||
|
aDate = TalendDate.getCurrentDate();
|
||||||
|
}
|
||||||
|
String srcFileBaseName = FilenameUtils.getName((String)srcFileName);
|
||||||
|
String tsString = TalendDate.formatDateInUTC(LcagDateTime.MDM_FILE_TIMESTAMP_FORMAT_STR, aDate);
|
||||||
|
|
||||||
|
String result = String.format("%s_%s", tsString, srcFileBaseName);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* createTimestampedFileName: Used for receive jobs and export from DB Jobs, only.
|
||||||
|
*
|
||||||
|
* Creates a file name starting with a current UTC timestamp.
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} LCAG
|
||||||
|
*
|
||||||
|
* {param} String("srcFileName") input: source file name
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static String createTimestampedFileName(String srcFileName)
|
||||||
|
{
|
||||||
|
|
||||||
|
String result = createTimestampedFileName(TalendDate.getCurrentDate(), srcFileName);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* getFileTimestampAsString: retrieves the MDM timestamp prefix of a given file name.
|
||||||
|
*
|
||||||
|
* {talendTypes} Object
|
||||||
|
*
|
||||||
|
* {Category} LCAG
|
||||||
|
*
|
||||||
|
* {param} String("srcFileName") input: file name with timestamp prefix
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static String getFileTimestampAsString(Object srcFileName) {
|
||||||
|
String result = null;
|
||||||
|
|
||||||
|
String srcFileBaseName = FilenameUtils.getBaseName((String)srcFileName);
|
||||||
|
|
||||||
|
result = srcFileBaseName.substring(0, LcagConstant.FILE_TIMESTAMP_FORMAT.length());
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* getFileTimestamp: retrieves the MDM data denoted by the MDM timestamp prefix of a given file name.
|
||||||
|
*
|
||||||
|
* {talendTypes} Date
|
||||||
|
*
|
||||||
|
* {Category} LCAG
|
||||||
|
*
|
||||||
|
* {param} String("srcFileName") input: file name with timestamp prefix
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static Date getFileTimestamp(String srcFileName)
|
||||||
|
{
|
||||||
|
Date timestamp = null;
|
||||||
|
int len = (srcFileName == null) ? 0 : srcFileName.length();
|
||||||
|
|
||||||
|
if (len > 0) {
|
||||||
|
String timestampString = getFileTimestampAsString(srcFileName);
|
||||||
|
timestamp = LcagDateTime.secureParseDate(LcagConstant.FILE_TIMESTAMP_FORMAT, timestampString);
|
||||||
|
}
|
||||||
|
|
||||||
|
return timestamp;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* createPrefixedFileName: Used for jobs, only.
|
||||||
|
*
|
||||||
|
* Creates a file name starting with a current UTC timestamp and a number. Create for receiving multiple messages:
|
||||||
|
* The file name should start with the context.env_jobTimestamp followed by a number n that defines the n-th message received
|
||||||
|
*
|
||||||
|
* Note: The prefix should identify the data flow of an incoming message throughout its entire processing by multiple jobs
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} LCAG-File
|
||||||
|
*
|
||||||
|
* {param} String("srcFileName") input: source file name
|
||||||
|
* {param} Date("d") input: Date to use, recommended: context.env_jobTimestamp
|
||||||
|
* {param} int("n") input: Number to make the prefix unique throughout all jobs
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static String createPrefixedFileName(String srcFileName, Date d, int n)
|
||||||
|
{
|
||||||
|
String prefix = LcagDateTime.MDM_FILE_TIMESTAMP_FORMAT.format(d) + String.format("-x%05d_", n);
|
||||||
|
String result = prefix + srcFileName;
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* getFileNamePrefix: Used to fetch the file name prefix created by function createPrefixedFileName.
|
||||||
|
* This prefix should identify the data flow of an incoming message throughout its entire processing by multiple jobs
|
||||||
|
*
|
||||||
|
* Note: This version supports the new prefix type (Timestamp prefix with number) as well as the old format (only Timestamp)
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} LCAG-File
|
||||||
|
*
|
||||||
|
* {param} String("srcFileName") input: source file name
|
||||||
|
*/
|
||||||
|
public static String getFileNamePrefix(String srcFileName)
|
||||||
|
{
|
||||||
|
String result = "";
|
||||||
|
|
||||||
|
if (srcFileName == null) throw new IllegalArgumentException("File name argument must not be null");
|
||||||
|
|
||||||
|
String srcFileBaseName = FilenameUtils.getBaseName((String)srcFileName);
|
||||||
|
|
||||||
|
if (srcFileBaseName.indexOf("-x") < 0) {
|
||||||
|
result = getFileTimestampAsString(srcFileName);
|
||||||
|
|
||||||
|
if (result == null) result = "";
|
||||||
|
} else {
|
||||||
|
// replace needed to correctly handle formats including 'T' or 'Z':
|
||||||
|
int prefixLength = LcagDateTime.MDM_FILE_TIMESTAMP_FORMAT_STR.replace("'", "").length() + String.format("-x%05d_", 0).length();
|
||||||
|
|
||||||
|
if (srcFileBaseName.length() >= prefixLength) {
|
||||||
|
result = srcFileBaseName.substring(0, prefixLength);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,406 @@
|
|||||||
|
package routines;
|
||||||
|
|
||||||
|
import java.lang.Thread;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* user specification: the function's comment should contain keys as follows: 1. write about the function's comment.but
|
||||||
|
* it must be before the "{talendTypes}" key.
|
||||||
|
*
|
||||||
|
* 2. {talendTypes} 's value must be talend Type, it is required . its value should be one of: String, char | Character,
|
||||||
|
* long | Long, int | Integer, boolean | Boolean, byte | Byte, Date, double | Double, float | Float, Object, short |
|
||||||
|
* Short
|
||||||
|
*
|
||||||
|
* 3. {Category} define a category for the Function. it is required. its value is user-defined .
|
||||||
|
*
|
||||||
|
* 4. {param} 's format is: {param} <type>[(<default value or closed list values>)] <name>[ : <comment>]
|
||||||
|
*
|
||||||
|
* <type> 's value should be one of: string, int, list, double, object, boolean, long, char, date. <name>'s value is the
|
||||||
|
* Function's parameter name. the {param} is optional. so if you the Function without the parameters. the {param} don't
|
||||||
|
* added. you can have many parameters for the Function.
|
||||||
|
*
|
||||||
|
* 5. {example} gives a example for the Function. it is optional.
|
||||||
|
*/
|
||||||
|
public class LcagGeneral {
|
||||||
|
private static LcagLogger log = LcagLogger.getLogger(LcagGeneral.class);
|
||||||
|
|
||||||
|
public LcagGeneral() {} { }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sleep some milli seconds
|
||||||
|
*
|
||||||
|
* {talendTypes} Void
|
||||||
|
*
|
||||||
|
* {Category} LCAG
|
||||||
|
*
|
||||||
|
* {param} String("milliSeconds") input: number of milliseconds to sleep
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static void sleep(long milliSeconds)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
Thread.sleep(milliSeconds, 0);
|
||||||
|
} catch (Exception e) {
|
||||||
|
;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* incrementCounter: Create a new named counter in a global HashMap (usually globalMap) and initialize to 0.
|
||||||
|
* Increments the counter if it already exists.
|
||||||
|
*
|
||||||
|
* {talendTypes} int
|
||||||
|
*
|
||||||
|
* {Category} LCAG
|
||||||
|
*
|
||||||
|
* {param} Map("aMap") input: Map to use to store counter value (usually globalMap)
|
||||||
|
* {param} String("aCounter") input: Name of counter
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static int incrementCounter(Map<String, Object> aMap, String aCounter)
|
||||||
|
{
|
||||||
|
Integer n = (Integer)aMap.getOrDefault(aCounter, 0);
|
||||||
|
|
||||||
|
n += 1;
|
||||||
|
if (n == 1)
|
||||||
|
log.debug("incrementCounter: Created new counter %s = %d", aCounter, n);
|
||||||
|
|
||||||
|
aMap.put(aCounter, n);
|
||||||
|
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* firstValue: Return the first parameter that is not null and not an empty String (or blanks-only).
|
||||||
|
*
|
||||||
|
* @param valueList variable number of parameter, all of them must have the same type
|
||||||
|
* @return first parameter that is not null (and for String not empty) or null if all parameters are null
|
||||||
|
*
|
||||||
|
* {talendTypes} Object
|
||||||
|
*
|
||||||
|
* {Category} LCAG
|
||||||
|
*
|
||||||
|
* {param} Object("valueList") valueList...: variable number of parameter, all of them must have the same type
|
||||||
|
*
|
||||||
|
* {examples} Assume String variables s1 = null, s2 = "", s3 = " ", s4 = "Hi friends"
|
||||||
|
* ->> firstValue(s1, s2, s3, s4, "Hello World") returns "Hi friends"
|
||||||
|
* ->> firstValue(s1, "Hello World") returns "Hello World"
|
||||||
|
* ->> firstValue(s1, s2) returns null
|
||||||
|
*/
|
||||||
|
public static<T> T firstValue(T... valueList)
|
||||||
|
{
|
||||||
|
T result = null;
|
||||||
|
|
||||||
|
// get the first non-null value into aValue
|
||||||
|
for (T obj: valueList) {
|
||||||
|
if (obj != null) {
|
||||||
|
if (obj instanceof String && obj.toString().trim().equals("")) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
result = obj;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* inSync: Used to check if a field in MDM is in-sync with another object. The second parameter is in-sync with the
|
||||||
|
* first one, if
|
||||||
|
* * first one is null (in-sync because it is safe to update that field)
|
||||||
|
* * or both are equal
|
||||||
|
*
|
||||||
|
* @param inMDM_Obj - first field to compare to (usually a MDM object field)
|
||||||
|
* @param withOtherObj - field to compare to first parameter
|
||||||
|
*
|
||||||
|
* @return true if first parameter is null or if both are equal (deeply checked)
|
||||||
|
*
|
||||||
|
* {talendTypes} Object
|
||||||
|
*
|
||||||
|
* {Category} LCAG
|
||||||
|
*
|
||||||
|
* {param} Object(inMDM_Obj) inMDM_Obj: first field to compare to (usually a MDM object field)
|
||||||
|
* {param} Object(withOtherObj) withOtherObj: field to compare to first parameter
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static boolean inSync(Object inMDM_Obj, Object withOtherObj)
|
||||||
|
{
|
||||||
|
boolean result = (inMDM_Obj == null) || java.util.Objects.deepEquals(inMDM_Obj, withOtherObj);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* inSyncIgnoreCase: Used to check if a String field in MDM is in-sync with another. The second parameter is in-sync with the
|
||||||
|
* first one, if
|
||||||
|
* * first one is null (in-sync because it is safe to update that field)
|
||||||
|
* * or both are equal ignoring case
|
||||||
|
*
|
||||||
|
* @param inMDM_Obj - first String to compare to (usually a MDM object field)
|
||||||
|
* @param withOtherObj - String to compare to first parameter
|
||||||
|
*
|
||||||
|
* @return true if first parameter is null or if both are equal
|
||||||
|
*
|
||||||
|
* {talendTypes} Object
|
||||||
|
*
|
||||||
|
* {Category} LCAG
|
||||||
|
*
|
||||||
|
* {param} String(inMDM_Obj) inMDM_Obj: first String to compare to (usually a MDM object field)
|
||||||
|
* {param} String(withOtherObj) withOtherObj: String to compare to first parameterr
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static boolean inSyncIgnoreCase(String inMDM_Obj, String withOtherObj)
|
||||||
|
{
|
||||||
|
boolean result = (inMDM_Obj == null) || inMDM_Obj.equalsIgnoreCase(withOtherObj);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int countChararcters(CharSequence s, String charList) {
|
||||||
|
int result = 0;
|
||||||
|
|
||||||
|
if (s == null || charList == null)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
int n = s.length();
|
||||||
|
|
||||||
|
for (int i=0; i < n; i++) {
|
||||||
|
CharSequence c = s.subSequence(i, i+1);
|
||||||
|
|
||||||
|
if ( charList.contains(c) )
|
||||||
|
result++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if string s1 and s2 are equal except white spaces in between. The result string indicates how to modify s1 to get s2
|
||||||
|
*
|
||||||
|
* @param s1
|
||||||
|
* @param s2
|
||||||
|
* @return String s2 if both differ excluding spaces. If both are equal excluding spaces:
|
||||||
|
* Return s2 where spaces are replaced by ' ' (if space is in s1 and s2) or '+' (space must be added to s2) and
|
||||||
|
* '-' (space must be deleted from s2)
|
||||||
|
*/
|
||||||
|
private static String compareWithSpaces(String s1, String s2, boolean ignoreCase) {
|
||||||
|
String result = "";
|
||||||
|
CharSequence s1Temp;
|
||||||
|
CharSequence s2Temp;
|
||||||
|
|
||||||
|
s1Temp = ignoreCase ? s1.toLowerCase() : s1;
|
||||||
|
s2Temp = ignoreCase ? s2.toLowerCase() : s2;
|
||||||
|
|
||||||
|
String s1NoBlancs = s1Temp.toString().replaceAll("[ \t\n]", "");
|
||||||
|
String s2NoBlancs = s2Temp.toString().replaceAll("[ \t\n]", "");
|
||||||
|
|
||||||
|
if (! s1NoBlancs.equals(s2NoBlancs) ) {
|
||||||
|
result = s2;
|
||||||
|
} else {
|
||||||
|
// both strings excluding spaces are equal
|
||||||
|
|
||||||
|
int n1 = s1.length();
|
||||||
|
int n2 = s2.length();
|
||||||
|
int nMin = Math.min(n1, n2);
|
||||||
|
|
||||||
|
int i = 0;
|
||||||
|
int j = 0;
|
||||||
|
|
||||||
|
while (i < nMin && j < nMin) {
|
||||||
|
char c1 = s1Temp.charAt(i);
|
||||||
|
char c2 = s2Temp.charAt(j);
|
||||||
|
|
||||||
|
if (c1 == c2) {
|
||||||
|
result += s1.charAt(i);
|
||||||
|
i++; j++;
|
||||||
|
} else if (c1 == ' ' && c2 != ' ') {
|
||||||
|
result += '+';
|
||||||
|
i++;
|
||||||
|
} else if (c2 == ' ' && c1 != ' ') {
|
||||||
|
result += '-';
|
||||||
|
j++;
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* compareEqual: Used to check if two parameters are equal. Returns
|
||||||
|
* * second parameter as string if not equal to the first one
|
||||||
|
* * "_n/a_" is the second is null or empty
|
||||||
|
* * "_equal_" if both are equal (deeply checked)
|
||||||
|
* * "_equalExceptCase_" if both are Strings that are equal except upper/lower case differences
|
||||||
|
*
|
||||||
|
* @param aValue - first field to compare to (usually a MDM object field)
|
||||||
|
* @param withOtherObj - field to compare to first parameter
|
||||||
|
*
|
||||||
|
* @return see above
|
||||||
|
*
|
||||||
|
* {talendTypes} Object
|
||||||
|
*
|
||||||
|
* {Category} LCAG
|
||||||
|
*
|
||||||
|
* {param} Object(inMDM_Obj) aValue: first field to compare to (usually a MDM object field)
|
||||||
|
* {param} Object(withOtherObj) withOtherObj: field to compare to first parameter
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static String compareEqual(Object aValue, Object withOtherObj)
|
||||||
|
{
|
||||||
|
String result = "?error?";
|
||||||
|
SimpleDateFormat df = LcagDateTime.MDM_DATE_FORMAT;
|
||||||
|
|
||||||
|
String s1 = (aValue == null) ? "" : aValue.toString().trim();
|
||||||
|
String s2 = (withOtherObj == null) ? "" : withOtherObj.toString().trim();
|
||||||
|
|
||||||
|
// Convert dates to mdm standard format:
|
||||||
|
if (aValue != null && (aValue instanceof Date)) s1 = df.format((Date)aValue);
|
||||||
|
if (withOtherObj != null && (withOtherObj instanceof Date)) s2 = df.format((Date)withOtherObj);
|
||||||
|
|
||||||
|
if (! "".equals(s1) && "".equals(s2)) {
|
||||||
|
result = "_n/a_";
|
||||||
|
} else if ("".equals(s1) && ! "".equals(s2)) {
|
||||||
|
result = s2;
|
||||||
|
} else if ( s1.equals(s2) ) {
|
||||||
|
result = "_equal_";
|
||||||
|
} else if (s1.equalsIgnoreCase(s2)) {
|
||||||
|
result = "_equalExceptCase_";
|
||||||
|
} else {
|
||||||
|
result = s2;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* compareValue: Used to check if two parameters are equal. Returns
|
||||||
|
* * second parameter as string if not equal to the first one
|
||||||
|
* * "_n/a_" is the second is null or empty
|
||||||
|
* * "_equal_" if both are equal (deeply checked)
|
||||||
|
* * "_equalExceptCase_" if both are Strings that are equal except upper/lower case differences
|
||||||
|
* * "_startsWith_*" if the first string starts with the second
|
||||||
|
* * "_startsWith_*" if the first string starts with the second
|
||||||
|
* * "_contains_*" if the first string contains the second
|
||||||
|
* * "_partOf_*" if the second string contains the first
|
||||||
|
*
|
||||||
|
* @param aValue - first field to compare to (usually a MDM object field)
|
||||||
|
* @param withOtherObj - field to compare to first parameter
|
||||||
|
*
|
||||||
|
* @return see above
|
||||||
|
*
|
||||||
|
* {talendTypes} Object
|
||||||
|
*
|
||||||
|
* {Category} LCAG
|
||||||
|
*
|
||||||
|
* {param} Object(inMDM_Obj) aValue: first field to compare to (usually a MDM object field)
|
||||||
|
* {param} Object(withOtherObj) withOtherObj: field to compare to first parameter
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static String compareValue(Object aValue, Object withOtherObj)
|
||||||
|
{
|
||||||
|
String result = "?error?";
|
||||||
|
SimpleDateFormat df = LcagDateTime.MDM_DATE_FORMAT;
|
||||||
|
|
||||||
|
String s1 = (aValue == null) ? "" : aValue.toString().trim();
|
||||||
|
String s2 = (withOtherObj == null) ? "" : withOtherObj.toString().trim();
|
||||||
|
|
||||||
|
// Convert dates to mdm standard format:
|
||||||
|
if (aValue != null && (aValue instanceof Date)) s1 = df.format((Date)aValue);
|
||||||
|
if (withOtherObj != null && (withOtherObj instanceof Date)) s2 = df.format((Date)withOtherObj);
|
||||||
|
|
||||||
|
if (! "".equals(s1) && "".equals(s2)) {
|
||||||
|
result = "_n/a_";
|
||||||
|
} else if ("".equals(s1) && ! "".equals(s2)) {
|
||||||
|
result = s2;
|
||||||
|
} else if ( s1.equals(s2) ) {
|
||||||
|
result = "_equal_";
|
||||||
|
} else if (s1.equalsIgnoreCase(s2)) {
|
||||||
|
result = "_equalExceptCase_";
|
||||||
|
} else {
|
||||||
|
result = s2;
|
||||||
|
|
||||||
|
if (s1.startsWith(s2)) {
|
||||||
|
result = "_startsWith_" + s2;
|
||||||
|
} else if (s1.endsWith(s2)) {
|
||||||
|
result = "_endsWith_" + s2;
|
||||||
|
} else if (s1.contains(s2)) {
|
||||||
|
result = "_contains_" + s2;
|
||||||
|
} else if (s2.contains(s1)) {
|
||||||
|
result = "_partOf_" + s2;
|
||||||
|
} else {
|
||||||
|
String correctionAdvise = LcagGeneral.compareWithSpaces(s1, s2, true);
|
||||||
|
|
||||||
|
if ( correctionAdvise.equals(s2) ) {
|
||||||
|
result = s2;
|
||||||
|
} else {
|
||||||
|
int n = countChararcters(s2, "+-");
|
||||||
|
int nc = countChararcters(correctionAdvise, "+-");
|
||||||
|
|
||||||
|
if (nc > n || correctionAdvise.length() < s2.length()) {
|
||||||
|
result = "_spacesDiffer_" + correctionAdvise;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* inSyncByPolicy: Used to check if a String field in MDM is in-sync with another with respect to a given compare policy.
|
||||||
|
* The second parameter is in-sync with the first one for
|
||||||
|
* * enriching policy (policy starts with '+'): if inMDM_Obj is null, otherwise the following rules apply
|
||||||
|
* * "case-sensitive" policy: both objects are equal in all values
|
||||||
|
* * "ignore-case" policy: the string representation of both objects is equal disregarding case of letters
|
||||||
|
* * "Levenstein" policy: not yet supported, throws exception
|
||||||
|
*
|
||||||
|
* @param inMDM_Obj - first String to compare to (usually a MDM object field)
|
||||||
|
* @param withOtherObj - String to compare to first parameter
|
||||||
|
* @param policy - Compare policy to use
|
||||||
|
*
|
||||||
|
* @return true if first two parameter are in-sync with respect to the policy given.
|
||||||
|
*
|
||||||
|
* {talendTypes} boolean
|
||||||
|
*
|
||||||
|
* {Category} LCAG
|
||||||
|
*
|
||||||
|
* {param} String(inMDM_Obj) inMDM_Obj: first String to compare to (usually a MDM object field)
|
||||||
|
* {param} String(withOtherObj) withOtherObj: String to compare to first parameter
|
||||||
|
* {param} String("policy") input: compare policy to use (case-sensitive, ignore-case, Levenstein (not yet supported))
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static boolean inSyncByPolicy(Object inMDM_Obj, Object withOtherObj, String policy)
|
||||||
|
{
|
||||||
|
if (policy == null) throw new IllegalArgumentException("Error: policy is not set (null)");
|
||||||
|
if (inMDM_Obj == null && withOtherObj == null) return true;
|
||||||
|
|
||||||
|
// Check enriching rule: If policy starts with '+' withOtherObject can safely set the inMDM_Obj if that one is null
|
||||||
|
if ((inMDM_Obj == null || "".equals(inMDM_Obj)) && policy.startsWith("+")) return true;
|
||||||
|
|
||||||
|
if (withOtherObj == null || inMDM_Obj == null) return false;
|
||||||
|
|
||||||
|
// after here, remove leading + from sync-rule
|
||||||
|
policy = policy.startsWith("+") ? policy.substring(1) : policy;
|
||||||
|
|
||||||
|
boolean result = false;
|
||||||
|
|
||||||
|
if ("case-sensitive".equals(policy)) {
|
||||||
|
result = java.util.Objects.deepEquals(inMDM_Obj, withOtherObj);
|
||||||
|
} else if ("ignore-case".equals(policy)) {
|
||||||
|
String inMDM = inMDM_Obj.toString();
|
||||||
|
String withOther = withOtherObj.toString();
|
||||||
|
|
||||||
|
result = inMDM.equalsIgnoreCase(withOther);
|
||||||
|
} else {
|
||||||
|
throw new IllegalArgumentException(String.format("Error: policy '%s' is not supported", policy));
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,325 @@
|
|||||||
|
package routines;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
|
import org.apache.log4j.Logger;
|
||||||
|
/**
|
||||||
|
* A wrapper for Log4J for simple use in tJava, tJavaRow components.
|
||||||
|
* It maps LCAG log levels to Apache Log levels so that all trace and debug message map to 'info'
|
||||||
|
* in Apache (other are mapped 1:1). This avoids messing up LCAG business log output
|
||||||
|
* with Talend internal og messages in all Talend jobs.
|
||||||
|
*
|
||||||
|
* Change History:
|
||||||
|
* V3.5.1: Full Redesign supporting LOG_LEVEL strings and restore
|
||||||
|
*
|
||||||
|
* @author U401455
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class LcagLogger {
|
||||||
|
public enum LOG_TYPE {
|
||||||
|
LOG_JOB,
|
||||||
|
LOG_JAVA,
|
||||||
|
LOG_ANY
|
||||||
|
}
|
||||||
|
|
||||||
|
private Logger loggerV1 = null; // for backward compatibility using Log4j1
|
||||||
|
private org.apache.logging.log4j.Logger loggerV2 = null; // to use log4j2
|
||||||
|
|
||||||
|
private LOG_TYPE logType = LOG_TYPE.LOG_JAVA;
|
||||||
|
|
||||||
|
|
||||||
|
/* LOG Level, sequence taken over from apache commons logging */
|
||||||
|
public static final int LOG_TRACE = 2;
|
||||||
|
public static final int LOG_DEBUG = 1;
|
||||||
|
public static final int LOG_INFO = 0;
|
||||||
|
public static final int LOG_WARN = -1;
|
||||||
|
public static final int LOG_ERROR = -2;
|
||||||
|
public static final int LOG_FATAL = -3;
|
||||||
|
public static final int LOG_OFF = -99;
|
||||||
|
public static final int ILLEGAL_LEVEL = -999;
|
||||||
|
|
||||||
|
private static HashMap<Integer, String> int2Str = initInt2Str();
|
||||||
|
private static HashMap<String, Integer> str2Int = initStr2Int();
|
||||||
|
|
||||||
|
private static int minJavaLevel = LOG_WARN; // By default only log Java warnings or more critical
|
||||||
|
private static int minJobLevel = LOG_TRACE;
|
||||||
|
|
||||||
|
// backup values to use in restore LogLevel
|
||||||
|
private static int lastJavaLevel = minJavaLevel;
|
||||||
|
private static int lastJobLevel = minJobLevel;
|
||||||
|
|
||||||
|
private static final LcagLogger myLogger = getLogger(LcagLogger.class);
|
||||||
|
|
||||||
|
private LcagLogger(Class<? extends Logger> aClass) {
|
||||||
|
loggerV1 = Logger.getLogger(aClass);
|
||||||
|
}
|
||||||
|
|
||||||
|
private LcagLogger(Object aObject) {
|
||||||
|
if (aObject instanceof org.apache.logging.log4j.spi.AbstractLogger) {
|
||||||
|
loggerV2= (org.apache.logging.log4j.Logger)aObject;
|
||||||
|
logType = LOG_TYPE.LOG_JOB;
|
||||||
|
} else if (aObject instanceof Logger) {
|
||||||
|
loggerV1 = (Logger)aObject;
|
||||||
|
logType = LOG_TYPE.LOG_JOB;
|
||||||
|
} else {
|
||||||
|
Class<?> clazz = aObject.getClass();
|
||||||
|
String className = clazz.getCanonicalName();
|
||||||
|
loggerV1 = Logger.getLogger(clazz);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static HashMap<Integer, String> initInt2Str() {
|
||||||
|
HashMap<Integer, String> result = new HashMap<Integer, String>();
|
||||||
|
|
||||||
|
result.put(LOG_TRACE, "TRACE");
|
||||||
|
result.put(LOG_DEBUG, "DEBUG");
|
||||||
|
result.put(LOG_INFO, "INFO");
|
||||||
|
result.put(LOG_WARN, "WARN");
|
||||||
|
result.put(LOG_ERROR, "ERROR");
|
||||||
|
result.put(LOG_FATAL, "FATAL");
|
||||||
|
result.put(LOG_OFF, "OFF");
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static HashMap<String, Integer> initStr2Int() {
|
||||||
|
HashMap<String, Integer> result = new HashMap<String, Integer>();
|
||||||
|
|
||||||
|
result.put("TRACE", LOG_TRACE);
|
||||||
|
result.put("DEBUG", LOG_DEBUG);
|
||||||
|
result.put("INFO", LOG_INFO);
|
||||||
|
result.put("WARN", LOG_WARN);
|
||||||
|
result.put("ERROR", LOG_ERROR);
|
||||||
|
result.put("FATAL", LOG_FATAL);
|
||||||
|
result.put("OFF", LOG_OFF);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set Lcag Log level for Java function.
|
||||||
|
* Note: This function must be be named setLevel, as this has side effects with Job ContextBean injection
|
||||||
|
* @param aLevel - Minimum log level to set.
|
||||||
|
*/
|
||||||
|
private static void setLogLevel(LOG_TYPE aType, String aTypeName, Object aLevel) {
|
||||||
|
Integer intLevel = asLevel(aLevel);
|
||||||
|
|
||||||
|
if (intLevel == null) {
|
||||||
|
myLogger.error("setLogLevel for %s with aLevel=null called => ignored", aTypeName);
|
||||||
|
} else {
|
||||||
|
if ( aType == LOG_TYPE.LOG_JAVA || aType == LOG_TYPE.LOG_ANY)
|
||||||
|
minJavaLevel = intLevel;
|
||||||
|
if ( aType == LOG_TYPE.LOG_JOB || aType == LOG_TYPE.LOG_ANY)
|
||||||
|
minJobLevel = intLevel;
|
||||||
|
|
||||||
|
if ( intLevel <= LOG_OFF ) {
|
||||||
|
System.err.println(String.format("WARNING: LcagLogger %s LOG LEVEL DISABLED (!)", aTypeName ));
|
||||||
|
} else {
|
||||||
|
System.out.println(String.format("LcagLogger: Info: %s LOG LEVEL set to '%s'", aTypeName, asString(intLevel)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int getJavaLogLevel() {
|
||||||
|
return minJavaLevel;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set Lcag Log level for Java function.
|
||||||
|
* Note: This function must be be named setLevel, as this has side effects with Job ContextBean injection
|
||||||
|
* @param aLevel - Minimum log level to set.
|
||||||
|
*/
|
||||||
|
public static void setJavaLogLevel(Object aLevel) {
|
||||||
|
setLogLevel(LOG_TYPE.LOG_JAVA, "JAVA", aLevel);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int getJobLogLevel() {
|
||||||
|
return minJobLevel;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set Lcag Log level for jobs.
|
||||||
|
* Note: This function must be be named setLevel, as this has side effects with Job ContextBean injection
|
||||||
|
* @param aLevel - Minimum log level to set.
|
||||||
|
*/
|
||||||
|
public static void setJobLogLevel(Object aLevel) {
|
||||||
|
setLogLevel(LOG_TYPE.LOG_JOB, "JOB", aLevel);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set Lcag Log level for jobs or Java function or both.
|
||||||
|
* Note: This function must be be named setLevel, as this has side effects with Job ContextBean injection
|
||||||
|
* @param aLevel - Minimum log level to set.
|
||||||
|
*/
|
||||||
|
public static void setAllLogLevel(Object aLevel) {
|
||||||
|
setJavaLogLevel(aLevel);
|
||||||
|
setJobLogLevel(aLevel);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set Lcag Log level for jobs or Java function or both.
|
||||||
|
* Note: This function must be be named setLevel, as this has side effects with Job ContextBean injection
|
||||||
|
* @param aLogType - Either LOG_TYPE.LOG_JAVA or LOG_TYPE.LOG_JOB or LOG_TYPE.LOG_ANY for both
|
||||||
|
* @param aLevel - Minimum log level to set.
|
||||||
|
* @deprecated Use setJobLogLevel, setJavaLogLevel or setAllLogLevel instead
|
||||||
|
*/
|
||||||
|
public static void setLogLevel(LOG_TYPE aLogType, Object aLevel) {
|
||||||
|
|
||||||
|
if ( aLogType == LOG_TYPE.LOG_JAVA) {
|
||||||
|
setJavaLogLevel(aLevel);
|
||||||
|
} else if ( aLogType == LOG_TYPE.LOG_JOB ) {
|
||||||
|
setJobLogLevel(aLevel);
|
||||||
|
} else if ( aLogType == LOG_TYPE.LOG_ANY ) {
|
||||||
|
setAllLogLevel(aLevel);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Restore loglevel to previous values and remember current values.
|
||||||
|
*/
|
||||||
|
public static void restoreLogLevels() {
|
||||||
|
int l = lastJavaLevel;
|
||||||
|
|
||||||
|
System.out.println(String.format("LcagLogger: Levels restored for Jobs set to %s, Java Code set to %s",
|
||||||
|
int2Str.getOrDefault(lastJobLevel, "OTHER"), int2Str.getOrDefault(lastJavaLevel, "OTHER"))
|
||||||
|
);
|
||||||
|
lastJavaLevel = minJavaLevel;
|
||||||
|
minJavaLevel = l;
|
||||||
|
|
||||||
|
l = lastJobLevel;
|
||||||
|
lastJobLevel = minJobLevel;
|
||||||
|
minJobLevel = l;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String asString(int aLogLevel) {
|
||||||
|
return int2Str.getOrDefault(aLogLevel, "OTHER");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Integer asLevel(Object aLogLevel) {
|
||||||
|
Integer result = null;
|
||||||
|
String errMsg = null;
|
||||||
|
|
||||||
|
if (aLogLevel != null) {
|
||||||
|
if (aLogLevel instanceof Integer) {
|
||||||
|
result = (Integer)aLogLevel;
|
||||||
|
} else if ( aLogLevel instanceof String) {
|
||||||
|
String logLevelStr = ((String)aLogLevel).trim().toUpperCase();
|
||||||
|
|
||||||
|
if ( "".equals(logLevelStr) ) {
|
||||||
|
result = null;
|
||||||
|
} else if (logLevelStr.startsWith("LOG_")) {
|
||||||
|
logLevelStr = logLevelStr.substring(4);
|
||||||
|
}
|
||||||
|
|
||||||
|
result = str2Int.get(logLevelStr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String format(String fmt, Object ...valueList) {
|
||||||
|
String result = String.format(fmt, valueList);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String doLog(int logLevel, String fmt, Object ...valueList) {
|
||||||
|
String logText = format(fmt, valueList);
|
||||||
|
String loggerName = loggerV2 == null ? loggerV1.getName() : loggerV2.getName();
|
||||||
|
String logPrefix = "";
|
||||||
|
int suffixPosition = loggerName.lastIndexOf(".");
|
||||||
|
String jobName = loggerName.substring(suffixPosition + 1);
|
||||||
|
|
||||||
|
boolean doIt = ( logType == LOG_TYPE.LOG_JOB && logLevel <= minJobLevel ) || ( logType == LOG_TYPE.LOG_JAVA && logLevel <= minJavaLevel );
|
||||||
|
|
||||||
|
if (! doIt)
|
||||||
|
return "";
|
||||||
|
|
||||||
|
logPrefix = "#" + asString(logLevel) + "#";
|
||||||
|
|
||||||
|
switch (logLevel) {
|
||||||
|
case LOG_INFO:
|
||||||
|
case LOG_DEBUG:
|
||||||
|
case LOG_TRACE:
|
||||||
|
if (loggerV1 != null) loggerV1.info(logPrefix + logText);
|
||||||
|
if (loggerV2 != null)
|
||||||
|
loggerV2.info(logPrefix + logText);
|
||||||
|
break;
|
||||||
|
case LOG_WARN:
|
||||||
|
if (loggerV1 != null) loggerV1.warn(logPrefix + logText);
|
||||||
|
if (loggerV2 != null)
|
||||||
|
loggerV2.warn(logPrefix + logText);
|
||||||
|
break;
|
||||||
|
case LOG_ERROR:
|
||||||
|
if (loggerV1 != null) loggerV1.error(logPrefix + logText);
|
||||||
|
if (loggerV2 != null)
|
||||||
|
loggerV2.error(logPrefix + logText);
|
||||||
|
break;
|
||||||
|
case LOG_FATAL:
|
||||||
|
if (loggerV1 != null) loggerV1.fatal(logPrefix + logText);
|
||||||
|
if (loggerV2 != null)
|
||||||
|
loggerV2.fatal(logPrefix + logText);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
//FIXME: logger.* does not produce output! drop next line after this is fixed.
|
||||||
|
if (logLevel > LOG_ERROR) {
|
||||||
|
System.out.println(logPrefix + jobName + " - " + logText);
|
||||||
|
} else {
|
||||||
|
System.err.println(logPrefix + jobName + " - " + logText);
|
||||||
|
}
|
||||||
|
|
||||||
|
return logText;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String trace(String fmt, Object ...valueList) {
|
||||||
|
String logText = doLog(LOG_TRACE, fmt, valueList);
|
||||||
|
|
||||||
|
return logText;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String debug(String fmt, Object ...valueList) {
|
||||||
|
String logText = doLog(LOG_DEBUG, fmt, valueList);
|
||||||
|
|
||||||
|
return logText;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String info(String fmt, Object ...valueList) {
|
||||||
|
String logText = doLog(LOG_INFO, fmt, valueList);
|
||||||
|
|
||||||
|
return logText;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String warn(String fmt, Object ...valueList) {
|
||||||
|
String logText = doLog(LOG_WARN, fmt, valueList);
|
||||||
|
|
||||||
|
return logText;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String error(String fmt, Object ...valueList) {
|
||||||
|
String logText = doLog(LOG_ERROR, fmt, valueList);
|
||||||
|
|
||||||
|
return logText;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String fatal(String fmt, Object ...valueList) {
|
||||||
|
String logText = doLog(LOG_FATAL, fmt, valueList);
|
||||||
|
|
||||||
|
return logText;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static LcagLogger getLogger(Logger aLogger) {
|
||||||
|
return new LcagLogger(aLogger);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static LcagLogger getLogger(Class aClass) {
|
||||||
|
return new LcagLogger(aClass);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static LcagLogger getLogger(Object aObject) {
|
||||||
|
return new LcagLogger(aObject);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,585 @@
|
|||||||
|
package routines;
|
||||||
|
|
||||||
|
/* LCAG String helper functions
|
||||||
|
*
|
||||||
|
* Change History:
|
||||||
|
* V3.5.0: Added new function isEmpty
|
||||||
|
*/
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Map.Entry;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Properties;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* user specification: the function's comment should contain keys as follows: 1. write about the function's comment.but
|
||||||
|
* it must be before the "{talendTypes}" key.
|
||||||
|
*
|
||||||
|
* 2. {talendTypes} 's value must be talend Type, it is required . its value should be one of: String, char | Character,
|
||||||
|
* long | Long, int | Integer, boolean | Boolean, byte | Byte, Date, double | Double, float | Float, Object, short |
|
||||||
|
* Short
|
||||||
|
*
|
||||||
|
* 3. {Category} define a category for the Function. it is required. its value is user-defined .
|
||||||
|
*
|
||||||
|
* 4. {param} 's format is: {param} <type>[(<default value or closed list values>)] <name>[ : <comment>]
|
||||||
|
*
|
||||||
|
* <type> 's value should be one of: string, int, list, double, object, boolean, long, char, date. <name>'s value is the
|
||||||
|
* Function's parameter name. the {param} is optional. so if you the Function without the parameters. the {param} don't
|
||||||
|
* added. you can have many parameters for the Function.
|
||||||
|
*
|
||||||
|
* 5. {example} gives a example for the Function. it is optional.
|
||||||
|
*/
|
||||||
|
public class LcagStringTools
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
private static LcagLogger log = LcagLogger.getLogger(LcagStringTools.class);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* isEmpty: Checks if a string representation of an object is null or some empty string
|
||||||
|
*
|
||||||
|
* {talendTypes} boolean
|
||||||
|
*
|
||||||
|
* {Category} LCAG-StringTools
|
||||||
|
*
|
||||||
|
* {param} Object("s") input: object to check
|
||||||
|
*/
|
||||||
|
public static boolean isEmpty(Object s)
|
||||||
|
{
|
||||||
|
boolean result = false;
|
||||||
|
|
||||||
|
if (s == null) {
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
String asString = s.toString().trim();
|
||||||
|
|
||||||
|
result = "".equals(asString) || "(null)".equals(asString) || "null".equals(asString);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* isEqualTo: Compares a String to another object
|
||||||
|
*
|
||||||
|
* {talendTypes} boolean
|
||||||
|
*
|
||||||
|
* {Category} LCAG-StringTools
|
||||||
|
*
|
||||||
|
* {param} String("s") input: string to compare with
|
||||||
|
* {param} int("obj") input: Object to compare to
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static boolean isEqualTo(String s, Object obj)
|
||||||
|
{
|
||||||
|
boolean result = false;
|
||||||
|
|
||||||
|
if (s == null && obj == null) {
|
||||||
|
return true;
|
||||||
|
} else if (s == null || obj == null) {
|
||||||
|
return false;
|
||||||
|
} else if (obj instanceof String || obj instanceof Integer || obj instanceof Long || obj instanceof Boolean){
|
||||||
|
String s2 = obj.toString();
|
||||||
|
|
||||||
|
result = s.equals(s2);
|
||||||
|
} else {
|
||||||
|
// Float, Double cause rounding problems, Date format problems when using toString()
|
||||||
|
throw new IllegalArgumentException(String.format("Comparison type '%s'not yet supported", obj.getClass().getName()));
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* isEqualToIgnoreCase: Compares a String to another object
|
||||||
|
*
|
||||||
|
* {talendTypes} boolean
|
||||||
|
*
|
||||||
|
* {Category} LCAG-StringTools
|
||||||
|
*
|
||||||
|
* {param} String("s") input: string to compare with
|
||||||
|
* {param} int("obj") input: Object to compare to
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static boolean isEqualToIgnoreCase(String s, Object obj)
|
||||||
|
{
|
||||||
|
boolean result = false;
|
||||||
|
|
||||||
|
if (s == null && obj == null) {
|
||||||
|
return true;
|
||||||
|
} else if (s == null || obj == null) {
|
||||||
|
return false;
|
||||||
|
} else if (obj instanceof String || obj instanceof Integer || obj instanceof Long || obj instanceof Boolean){
|
||||||
|
String s2 = obj.toString();
|
||||||
|
|
||||||
|
result = s.equalsIgnoreCase(s2);
|
||||||
|
} else {
|
||||||
|
// Float, Double cause rounding problems, Date format problems when using toString()
|
||||||
|
throw new IllegalArgumentException(String.format("Comparison type '%s'not yet supported", obj.getClass().getName()));
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert an object to a non null String
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} LCAG-StringTools
|
||||||
|
*
|
||||||
|
* {param} Object anObj : input: Object to turn into String
|
||||||
|
* {returns} String representation using toString or empty string
|
||||||
|
* {example} toString(null) = "", toString(new Double(9.9)) = "9.9"
|
||||||
|
*/
|
||||||
|
public static String toString(Object anObj) {
|
||||||
|
String result = anObj == null ? "" : anObj.toString();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert a CSV string into a Java String List
|
||||||
|
*
|
||||||
|
* {talendTypes} List<String>
|
||||||
|
*
|
||||||
|
* {Category} LCAG-StringTools
|
||||||
|
*
|
||||||
|
* {param} String csv : input: text to split into a list
|
||||||
|
* {param} String regExSeparator : input: Character or regular exression string to use for splitting
|
||||||
|
* {returns} List of strings
|
||||||
|
* {example} assembleMDS_DateTime(aDate, "23:59") returns next day 00:00 of aDate
|
||||||
|
*/
|
||||||
|
public static List<String> asList(String csv, String regExSeparator) {
|
||||||
|
List<String> result = new ArrayList<>();
|
||||||
|
|
||||||
|
result = Arrays.asList( csv.split(regExSeparator) );
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Joins an object as a result String. Usually the parameter is a List<>. The functions joins multiple elements
|
||||||
|
* using the delimiter passed. It replaces null values by the nullString argument unless it is null itself.
|
||||||
|
* If nullString argument is null the function skips all null values from the stringList argument
|
||||||
|
*
|
||||||
|
* @param delimiter - separstor of strings to use
|
||||||
|
* @param nullString - replacement of null values (if null skips null values)
|
||||||
|
* @param stringList - any object or List<>. Elements are converted to String using toString()
|
||||||
|
* @return - String joined by delimiter from all arguments.
|
||||||
|
*/
|
||||||
|
private static String join(String delimiter, String nullString, Object stringList) {
|
||||||
|
List<Object> objList = new ArrayList<Object>();
|
||||||
|
List<String> txtList = new ArrayList<String>();
|
||||||
|
|
||||||
|
if (stringList instanceof Collection) {
|
||||||
|
objList = new java.util.ArrayList<>((Collection<?>)stringList);
|
||||||
|
} else {
|
||||||
|
objList.add(stringList);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (Object o : objList) {
|
||||||
|
if ( o != null ) {
|
||||||
|
txtList.add(o.toString());
|
||||||
|
} else if (nullString != null) {
|
||||||
|
txtList.add(nullString);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
String result = String.join(delimiter, txtList);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Joins an object as a result String. Usually the parameter is a List<>. The functions joins multiple elements
|
||||||
|
* using the delimiter passed. It skips null values!
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} LCAG-StringTools
|
||||||
|
*
|
||||||
|
* {param} String stringList : input: List of Objects to join
|
||||||
|
* {returns} All elements joined by delimiter.
|
||||||
|
* {example} join( "/", { "a", null, "b" } ) = "a/b"
|
||||||
|
*/
|
||||||
|
public static String joinIgnoreNull(String delimiter, Object stringList) {
|
||||||
|
return join(delimiter, null, stringList);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Joins an object as a result String. Usually the parameter is a List<>. The functions joins multiple elements
|
||||||
|
* using the delimiter passed. Uses nullString argument to represent null values to keep same number of elements.
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} LCAG-StringTools
|
||||||
|
*
|
||||||
|
* {param} String stringList : input: List of Objects to join
|
||||||
|
* {returns} All elements joined by delimiter.
|
||||||
|
* {example} join( "; ", "n/a", { "a", null, "b" } ) = "a; n/a; b"
|
||||||
|
*/
|
||||||
|
public static String joinAll(String delimiter, String nullString, Object stringList) {
|
||||||
|
return join(delimiter, nullString, stringList);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Joins all arguments as a result string using the delimiter to seperate the arguments.
|
||||||
|
* Ignores null values in the list.
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} LCAG-StringTools
|
||||||
|
*
|
||||||
|
* {param} String stringList : input: List of Objects to join
|
||||||
|
* {returns} All elements joined by delimiter.
|
||||||
|
* {example} join(", ", "a", "b", "c") = "a, b, c
|
||||||
|
* join( "; ", "a", 1, 1.2, null, true ) = "a; 1; 1.2; true"
|
||||||
|
*/
|
||||||
|
public static String joinIgnoreNull(String delimiter, Object... stringList) {
|
||||||
|
if (stringList == null) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
List<Object> aList = Arrays.asList(stringList);
|
||||||
|
|
||||||
|
String result = join(delimiter, null, aList);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Joins all arguments as a result string using the delimiter to separate the arguments.
|
||||||
|
* Uses nullString argument to represent null values to keep same number of elements.
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} LCAG-StringTools
|
||||||
|
*
|
||||||
|
* {param} String stringList : input: List of Objects to join
|
||||||
|
* {returns} All elements joined by delimiter.
|
||||||
|
* {example} join(", ", "n/a", "a", "b", "c") = "a, b, c
|
||||||
|
* join( "; ", "n/a", "a", 1, 1.2, null, true ) = "a; 1; 1.2; n/a; true"
|
||||||
|
*/
|
||||||
|
public static String joinAll(String delimiter, String nullString, Object... stringList) {
|
||||||
|
if (stringList == null) {
|
||||||
|
return nullString;
|
||||||
|
}
|
||||||
|
|
||||||
|
List<Object> aList = Arrays.asList(stringList);
|
||||||
|
|
||||||
|
String result = join(delimiter, nullString, aList);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* fillBlanksUpcase: add blanks to given Strings for a fixed length
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} LCAG-StringTools
|
||||||
|
*
|
||||||
|
* {param} String("s") input: string to fill with blanks
|
||||||
|
* {param} int("n") input: max number of blanks to add
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static String fillBlanksUpcase(String s, int n)
|
||||||
|
{
|
||||||
|
if (s == null) return null;
|
||||||
|
else return String.format("%1$-" + n + "s", s.toUpperCase());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* splitString: add blanks to given Strings for a fixed length
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} LCAG-StringTools
|
||||||
|
*
|
||||||
|
* {param} String("s") input: string to tokenize
|
||||||
|
* {param} String("delim") input: delimiter string
|
||||||
|
* {param} int("n") input: token indicator/counter starting with 0
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static String splitString(String s, String delim, int n)
|
||||||
|
{
|
||||||
|
if (s == null) return null;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* s != null */
|
||||||
|
String result = null;
|
||||||
|
String[] tokens = s.split(delim);
|
||||||
|
if (tokens != null && tokens.length > n) result = tokens[n];
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* selectCaseSensitiveIfEqual: In case both String parameters are equal ignoring case return the one
|
||||||
|
* that is case-sensitive. In all other cases always return the first parameter that is not null or empty.
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} LCAG-StringTools
|
||||||
|
*
|
||||||
|
* {param} String("s1") input: first string to evaluate
|
||||||
|
* {param} String("s2") input: second string to evaluate
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static String selectCaseSensitiveIfEqual(String s1, String s2)
|
||||||
|
{
|
||||||
|
if (s1 == null) return s2;
|
||||||
|
if (s2 == null) return s1;
|
||||||
|
|
||||||
|
s1 = s1.trim();
|
||||||
|
s2 = s2.trim();
|
||||||
|
|
||||||
|
String result = s1;
|
||||||
|
|
||||||
|
int n1 = s1.length();
|
||||||
|
int n2 = s2.length();
|
||||||
|
|
||||||
|
if (
|
||||||
|
n1 == n2 && n1 > 1 && s1.equalsIgnoreCase(s2) &&
|
||||||
|
Character.isUpperCase(s1.charAt(1)) && Character.isLowerCase(s2.charAt(1)) ) {
|
||||||
|
|
||||||
|
// both are equal ignoring case but s2 is likely case-sensitive whereas as s1 is not. So use s2:
|
||||||
|
result = s2;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* selectBestOf: Selects the String with the most available information according to the rules:
|
||||||
|
* 1) if first parameter is null then return the second.
|
||||||
|
* 2) if one string start with the other return the longer string (after trimming)
|
||||||
|
* 3) if both are of same length but differ in content ignoring case, return the first one
|
||||||
|
* 4) if both are equal when ignoring case, return the one that also contains lower-case letters (tested by 2nd char)
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} LCAG-StringTools
|
||||||
|
*
|
||||||
|
* {param} String("s1") input: first string to evaluate
|
||||||
|
* {param} String("s2") input: second string to evaluate
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static String selectBestOf(String s1, String s2)
|
||||||
|
{
|
||||||
|
if (s1 == null) return s2;
|
||||||
|
if (s2 == null) return s1;
|
||||||
|
|
||||||
|
s1 = s1.trim();
|
||||||
|
s2 = s2.trim();
|
||||||
|
|
||||||
|
String result = s1;
|
||||||
|
String s1CaseIgnored = s1.toLowerCase();
|
||||||
|
String s2CaseIgnored = s2.toLowerCase();
|
||||||
|
|
||||||
|
int n1 = s1.length();
|
||||||
|
int n2 = s2.length();
|
||||||
|
|
||||||
|
if (n2 > n1 && s2CaseIgnored.startsWith(s1CaseIgnored)) {
|
||||||
|
result = s2;
|
||||||
|
} else if (
|
||||||
|
n1 == n2 && n1 > 1 && s1.equalsIgnoreCase(s2) &&
|
||||||
|
Character.isUpperCase(s1.charAt(1)) && Character.isLowerCase(s2.charAt(1)) ) {
|
||||||
|
|
||||||
|
// both are equal ignoring case but s2 is likely case-sensitive whereas as s1 is not. So use s2:
|
||||||
|
result = s2;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* applyChangeRule: applies the given change rule to the first two parameters and returs the corresponding result.
|
||||||
|
* Rules:
|
||||||
|
* *) in general: if the first string s1 is null then apply the rule to the second string s2
|
||||||
|
* 1) "to-upper-case":
|
||||||
|
* 2) "to-lower-case":
|
||||||
|
* 3) "prefer case-sensitive": if both string are equal except case, use the one that is case-sensitive
|
||||||
|
* 4) "select best-of"; if one string start with the other return the longer string (after trimming)
|
||||||
|
* 5) null:
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} LCAG-StringTools
|
||||||
|
*
|
||||||
|
* {param} String("s1") input: first string to evaluate
|
||||||
|
* {param} String("s2") input: second string to evaluate
|
||||||
|
* {param} String("changeRule") input: rule used to change
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static String applyChangeRule(String s1, String s2, String changeRule) {
|
||||||
|
String result;
|
||||||
|
|
||||||
|
if (s1 == null) {
|
||||||
|
s1 = s2;
|
||||||
|
}
|
||||||
|
|
||||||
|
result = s1;
|
||||||
|
|
||||||
|
if (changeRule == null) {
|
||||||
|
|
||||||
|
} else if ("prefer case-sensitive".equals(changeRule)) {
|
||||||
|
result = selectCaseSensitiveIfEqual( s1, s2);
|
||||||
|
} else if ("to-upper-case".equals(changeRule)) {
|
||||||
|
result = s1.toUpperCase();
|
||||||
|
} else if ("to-lower-case".equals(changeRule)) {
|
||||||
|
result = s1.toLowerCase();
|
||||||
|
} else if ("select best-of".equals(changeRule)) {
|
||||||
|
result = selectBestOf(s1,s2);
|
||||||
|
}
|
||||||
|
|
||||||
|
log.debug(String.format("applyChangeRule(%s, %s, %s) = %s", s1, s2, changeRule, result));
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* resolveVariables: Replaces all variables ${VAR} in the txt argument by the value defined in the map.
|
||||||
|
* If the variable is not defined the function will not change this placeholder.
|
||||||
|
*
|
||||||
|
* Example:
|
||||||
|
* resolveVariables("This is ${MDM_Cargo_MDM_DataContainer}", globalMap) = "This is MDM_4_DEV_4_TST1_REG"
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} LCAG-String
|
||||||
|
*
|
||||||
|
* {param} String("txt") input: string to substitutes
|
||||||
|
* {param} Map("map") input: Map of strings to replace
|
||||||
|
*/
|
||||||
|
public static String resolveVariables(String txt, Map<? extends Object, ? extends Object> map) {
|
||||||
|
String result = "";
|
||||||
|
if (txt == null || map == null) return txt;
|
||||||
|
|
||||||
|
String[] varList = txt.split("\\$\\{"); // split txt by ${
|
||||||
|
|
||||||
|
|
||||||
|
for (String s: varList) {
|
||||||
|
int n = s.indexOf("}");
|
||||||
|
|
||||||
|
if (n > 0) {
|
||||||
|
// Var found to replace
|
||||||
|
String key = s.substring(0, n);
|
||||||
|
String format = null;
|
||||||
|
|
||||||
|
int formatIndex = key.indexOf("#");
|
||||||
|
|
||||||
|
if (formatIndex > 0) {
|
||||||
|
// There is a formatting instruction after the variable name => get name and format:
|
||||||
|
format = key.substring(formatIndex + 1);
|
||||||
|
key = key.substring(0, formatIndex);
|
||||||
|
}
|
||||||
|
|
||||||
|
Object value = map.get(key);
|
||||||
|
|
||||||
|
// handle reserved keys:
|
||||||
|
if ("now".equals(key)) { // current date/time
|
||||||
|
value = new Date();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (value != null) {
|
||||||
|
if (value instanceof Date && format != null) {
|
||||||
|
if ("milliseconds".equals(format)) {
|
||||||
|
Long v = ((Date)value).getTime();
|
||||||
|
|
||||||
|
value = v;
|
||||||
|
} else {
|
||||||
|
SimpleDateFormat f = new SimpleDateFormat(format);
|
||||||
|
|
||||||
|
value = f.format( (Date)value );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Replace env var by value
|
||||||
|
s = value.toString() + s.substring(n+1);
|
||||||
|
} else {
|
||||||
|
s = "${" + s;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
result += s;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* resolveVariables: Replaces all variables ${VAR} in the txt argument by the value defined in the map.
|
||||||
|
* If the variable is not defined the function will not change this placeholder.
|
||||||
|
*
|
||||||
|
* Example:
|
||||||
|
* resolveVariables("This is ${MDM_Cargo_MDM_DataContainer}", context) = "This is MDM_4_DEV_4_TST1_REG"
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} LCAG-String
|
||||||
|
*
|
||||||
|
* {param} String("txt") input: string to substitutes
|
||||||
|
* {param} HashTable("context") input: Talend Context in a job
|
||||||
|
*/
|
||||||
|
public static String resolveVariables(String txt, Properties context) {
|
||||||
|
Map<Object, Object> aMap = new HashMap<Object, Object>();
|
||||||
|
|
||||||
|
String result = "";
|
||||||
|
if (txt == null || context == null) return txt;
|
||||||
|
|
||||||
|
for (Entry<Object, Object> e: context.entrySet()) {
|
||||||
|
aMap.put(e.getKey(), e.getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
result = resolveVariables(txt, aMap);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* resolveEnvironmentVariables: Replaces all environment variables ${VAR} by its value.
|
||||||
|
* If the variable is not defined the function will not change this placeholder.
|
||||||
|
*
|
||||||
|
* Example:
|
||||||
|
* resolveEnvironmentVariables("This is ${OS}") = "This is Windows_NT"
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} LCAG-StringTools
|
||||||
|
*
|
||||||
|
* {param} String("txt") input: string to substitutes
|
||||||
|
*/
|
||||||
|
public static String resolveEnvironmentVariables(String txt) {
|
||||||
|
String result = "";
|
||||||
|
String[] varList = txt.split("\\$\\{"); // split txt by ${
|
||||||
|
|
||||||
|
for (String s: varList) {
|
||||||
|
int n = s.indexOf("}");
|
||||||
|
|
||||||
|
if (n > 0) {
|
||||||
|
// Env var found to replace
|
||||||
|
String key = s.substring(0, n);
|
||||||
|
String value = System.getenv(key);
|
||||||
|
|
||||||
|
if (value != null) {
|
||||||
|
// Replace env var by value
|
||||||
|
s = value + s.substring(n+1);
|
||||||
|
} else {
|
||||||
|
s = "${" + s;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
result += s;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,117 @@
|
|||||||
|
package routines;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* user specification: the function's comment should contain keys as follows: 1. write about the function's comment.but
|
||||||
|
* it must be before the "{talendTypes}" key.
|
||||||
|
*
|
||||||
|
* 2. {talendTypes} 's value must be talend Type, it is required . its value should be one of: String, char | Character,
|
||||||
|
* long | Long, int | Integer, boolean | Boolean, byte | Byte, Date, double | Double, float | Float, Object, short |
|
||||||
|
* Short
|
||||||
|
*
|
||||||
|
* 3. {Category} define a category for the Function. it is required. its value is user-defined .
|
||||||
|
*
|
||||||
|
* 4. {param} 's format is: {param} <type>[(<default value or closed list values>)] <name>[ : <comment>]
|
||||||
|
*
|
||||||
|
* <type> 's value should be one of: string, int, list, double, object, boolean, long, char, date. <name>'s value is the
|
||||||
|
* Function's parameter name. the {param} is optional. so if you the Function without the parameters. the {param} don't
|
||||||
|
* added. you can have many parameters for the Function.
|
||||||
|
*
|
||||||
|
* 5. {example} gives a example for the Function. it is optional.
|
||||||
|
*/
|
||||||
|
public class LcagTestTools {
|
||||||
|
|
||||||
|
private static LcagLogger log = LcagLogger.getLogger(LcagTestTools.class);
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* generateCode: Generate a sequence of codes from a given template @startCode.
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} LCAG-Test Tools
|
||||||
|
*
|
||||||
|
* {param} Object("startCode") input: Code to start with, e.g. TAA
|
||||||
|
* {param} Object("n") input: n-th code in generated sequence, e.g. TAC for n=2
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static String generateCode(String startCode, int n)
|
||||||
|
{
|
||||||
|
String result = startCode;
|
||||||
|
char[] resultAry = startCode.toCharArray();
|
||||||
|
|
||||||
|
for (int i = startCode.length()-1; i > 0; i -=1) {
|
||||||
|
char c = startCode.charAt(i);
|
||||||
|
int oldOrd = (c >= 'a') ? (int)c - (int)'a' : (int)c - (int)'A';
|
||||||
|
int newOrd = oldOrd + n;
|
||||||
|
|
||||||
|
int rest = newOrd % 26;
|
||||||
|
char newChar = (char)(c - oldOrd + rest);
|
||||||
|
|
||||||
|
n = newOrd / 26;
|
||||||
|
resultAry[i] = newChar;
|
||||||
|
|
||||||
|
if (n == 0) break;
|
||||||
|
}
|
||||||
|
|
||||||
|
result = String.valueOf(resultAry);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* compareToExpected: compares an expected result (first parameter) with the actual result (second paramter). Returns "OK" if both are equal and the actual value else.
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} LCAG-Test Tools
|
||||||
|
*
|
||||||
|
* {param} Object("expectedResult") input: expected result object
|
||||||
|
* {param} Object("ActualResult") input: actual result object
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static String compareToExpected(Object expectedResult, Object actualResult)
|
||||||
|
{
|
||||||
|
String result = "OK";
|
||||||
|
|
||||||
|
if (! java.util.Objects.equals(expectedResult, actualResult)) {
|
||||||
|
result = (actualResult == null) ? "n/a" : actualResult.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* compareToSimilarExpected: compares an expected String (first parameter) with an actual String (second parameter). Returns "OK" if both
|
||||||
|
* are equal excluding blancs and case and the actual value else.
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} LCAG-Test Tools
|
||||||
|
*
|
||||||
|
* {param} String("expectedResult") input: expected result object
|
||||||
|
* {param} String("ActualResult") input: actual result object
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static String compareToSimilarExpected(String expectedResult, String actualResult)
|
||||||
|
{
|
||||||
|
String result = "OK";
|
||||||
|
|
||||||
|
if (actualResult == null && expectedResult != null) {
|
||||||
|
result = "n/a";
|
||||||
|
} else if (actualResult != null && expectedResult == null) {
|
||||||
|
result = actualResult;
|
||||||
|
} else if (actualResult != null && expectedResult != null) {
|
||||||
|
String expectedSimilarResult = expectedResult.replaceAll(" ", "");
|
||||||
|
String actualSimilarResult = actualResult.replaceAll(" ", "");
|
||||||
|
|
||||||
|
if (! expectedSimilarResult.equalsIgnoreCase(actualSimilarResult)) {
|
||||||
|
result = actualResult;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,598 @@
|
|||||||
|
package routines;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.text.ParseException;
|
||||||
|
|
||||||
|
public class ValidityCheck {
|
||||||
|
private static LcagLogger log = LcagLogger.getLogger(ValidityCheck.class);
|
||||||
|
|
||||||
|
public enum CheckResult
|
||||||
|
{
|
||||||
|
TO_FROM_SWITCHED
|
||||||
|
,EXTENDS_BEFORE
|
||||||
|
,EXTENDS_AFTER
|
||||||
|
,OVERLAPS_WITH_START
|
||||||
|
,OVERLAPS_WITH_END
|
||||||
|
,CONTAINS
|
||||||
|
,HAS_GAP_BEFORE
|
||||||
|
,HAS_GAP_AFTER
|
||||||
|
,FIRSTINSERT
|
||||||
|
,EQUALS
|
||||||
|
,SHORTENED
|
||||||
|
,SETVALIDFROM
|
||||||
|
,UNKNOWN
|
||||||
|
}
|
||||||
|
|
||||||
|
public enum ResultInsertUpdate
|
||||||
|
{
|
||||||
|
INSERT
|
||||||
|
,UPDATE
|
||||||
|
,ERROR
|
||||||
|
}
|
||||||
|
|
||||||
|
public enum ResultValidityCheck
|
||||||
|
{
|
||||||
|
OK
|
||||||
|
,ERROR
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* validityCheckResult: Checks the timeline of the validities
|
||||||
|
*
|
||||||
|
* {talendTypes} Date
|
||||||
|
*
|
||||||
|
* {Category} ValidityCheck
|
||||||
|
*
|
||||||
|
* {param} Dated(Date existing_validFrom, Date existing_validTo, Date new_validFrom,Date new_validTo) input: Dates to check.
|
||||||
|
*
|
||||||
|
* {example} helloExemple(2018-03-02,2099-12-31,2013-01-01,2018-03-01) # EXTENDS_AFTER !.
|
||||||
|
*/
|
||||||
|
public static CheckResult timelineValidity(Date existing_validFrom, Date existing_validTo, Date new_validFrom,Date new_validTo){
|
||||||
|
|
||||||
|
|
||||||
|
CheckResult checkResult=CheckResult.UNKNOWN;
|
||||||
|
|
||||||
|
if (existing_validFrom == null && existing_validTo==null)
|
||||||
|
{
|
||||||
|
//First insert of a record -> no date exists
|
||||||
|
log.debug("CheckResult.FIRSTINSERT");
|
||||||
|
return CheckResult.FIRSTINSERT;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if (new_validFrom == null && new_validTo==null)
|
||||||
|
{
|
||||||
|
log.debug("existing_validFrom:" + existing_validFrom.toString());
|
||||||
|
log.debug("existing_validTo:" + existing_validTo.toString());
|
||||||
|
//No Gap (Related Check)
|
||||||
|
log.debug("CheckResult.HAS_GAP_AFTER1");
|
||||||
|
return CheckResult.HAS_GAP_AFTER;
|
||||||
|
}
|
||||||
|
|
||||||
|
//log.debug("diffDate(existing_validTo,constant.getFarPastDate()):" + String.valueOf(diffDate(existing_validTo,constant.getFarPastDate())));
|
||||||
|
//log.debug("diffDate(existing_validFrom,new_validFrom):" + String.valueOf(diffDate(existing_validTo,constant.getFarPastDate())));
|
||||||
|
log.debug("diffDate(existing_validTo,new_validTo):" + String.valueOf(diffDate(existing_validTo,new_validTo)));
|
||||||
|
|
||||||
|
|
||||||
|
if (diffDate(existing_validTo,constant.getFarFutureDate()) ==0 && diffDate(existing_validFrom,new_validFrom)==0 && diffDate(existing_validTo,new_validTo)<0)
|
||||||
|
{
|
||||||
|
// MDM *......* (2010 to 2099)
|
||||||
|
// Inc *...* (2010 to 2050)
|
||||||
|
|
||||||
|
log.debug("CheckResult.SHORTENED");
|
||||||
|
return CheckResult.SHORTENED;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (diffDate(existing_validFrom,constant.getFarPastDate()) ==0 && diffDate(existing_validTo,new_validTo)==0 && diffDate(existing_validFrom,new_validFrom)>0)
|
||||||
|
{
|
||||||
|
// MDM *......* (1970 to 2099)
|
||||||
|
// Inc *...* (2015 to 2099)
|
||||||
|
|
||||||
|
log.debug("CheckResult.SETVALIDFROM");
|
||||||
|
return CheckResult.SETVALIDFROM;
|
||||||
|
}
|
||||||
|
|
||||||
|
log.debug("existing_validFrom:" + existing_validFrom.toString());
|
||||||
|
log.debug("existing_validTo:" + existing_validTo.toString());
|
||||||
|
log.debug("new_validFrom:" + (new_validFrom.equals(null)?"NULL":new_validFrom.toString()));
|
||||||
|
log.debug("new_validTo:" + new_validTo.toString());
|
||||||
|
|
||||||
|
log.debug("diffDate(new_validFrom,new_validTo):" + String.valueOf(diffDate(new_validFrom,new_validTo)));
|
||||||
|
log.debug("diffDate(existing_validFrom,new_validFrom):" + String.valueOf(diffDate(existing_validFrom,new_validFrom)));
|
||||||
|
log.debug("diffDate(existing_validTo,new_validTo):" + String.valueOf(diffDate(existing_validTo,new_validTo)));
|
||||||
|
|
||||||
|
if (diffDate(new_validFrom,existing_validFrom)==0 && diffDate(new_validTo,existing_validTo)==0)
|
||||||
|
{
|
||||||
|
//First insert of a record -> no date exists
|
||||||
|
log.debug("CheckResult.EQUALS");
|
||||||
|
return CheckResult.EQUALS;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (diffDate(new_validFrom,new_validTo)<=0){
|
||||||
|
checkResult=CheckResult.TO_FROM_SWITCHED;
|
||||||
|
log.debug("CheckResult.TO_FROM_SWITCHED");
|
||||||
|
}else{
|
||||||
|
/*
|
||||||
|
* existing *-----*
|
||||||
|
* new *----*
|
||||||
|
*/
|
||||||
|
if (diffDate(existing_validTo,new_validFrom)==1){
|
||||||
|
checkResult=CheckResult.EXTENDS_AFTER;
|
||||||
|
log.debug("CheckResult.EXTENDS_AFTER");
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
* existing *------*
|
||||||
|
* new *----*
|
||||||
|
*/
|
||||||
|
if (diffDate(existing_validTo, new_validFrom)<0 &&
|
||||||
|
diffDate(existing_validTo, new_validTo)>0 ){
|
||||||
|
checkResult=CheckResult.OVERLAPS_WITH_END;
|
||||||
|
log.debug("CheckResult.OVERLAPS_WITH_END");
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
* existing *------*
|
||||||
|
* new *----*
|
||||||
|
*/
|
||||||
|
if (diffDate(existing_validTo,new_validFrom)>1){
|
||||||
|
checkResult=CheckResult.HAS_GAP_AFTER;
|
||||||
|
log.debug("CheckResult.HAS_GAP_AFTER");
|
||||||
|
log.debug("diffDate(existing_validTo,new_validFrom)" + String.valueOf(diffDate(existing_validTo,new_validFrom)));
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
* existing *------*
|
||||||
|
* new *----*
|
||||||
|
*/
|
||||||
|
if (diffDate(new_validTo, existing_validFrom)==1){
|
||||||
|
checkResult=CheckResult.EXTENDS_BEFORE;
|
||||||
|
log.debug("CheckResult.EXTENDS_BEFORE");
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
* existing *------*
|
||||||
|
* new *----*
|
||||||
|
*/
|
||||||
|
if (diffDate(existing_validFrom,new_validTo)<-1){
|
||||||
|
checkResult=CheckResult.HAS_GAP_BEFORE;
|
||||||
|
log.debug("CheckResult.HAS_GAP_BEFORE");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* existing *------*
|
||||||
|
* new -----*
|
||||||
|
log.debug("## contains");
|
||||||
|
log.debug("## diffDate("+existing_validFrom+","+new_validFrom+"):"+diffDate(existing_validFrom,new_validFrom));
|
||||||
|
log.debug("## diffDate("+existing_validTo+","+new_validTo+"):"+diffDate(existing_validTo,new_validTo));
|
||||||
|
*/
|
||||||
|
if (diffDate(existing_validFrom,new_validFrom) >0 &&
|
||||||
|
diffDate(existing_validTo,new_validTo)<=0 ){
|
||||||
|
checkResult=CheckResult.CONTAINS;
|
||||||
|
log.debug("CONTAINS ENTERED");
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
* existing *------*
|
||||||
|
* new *----*
|
||||||
|
|
||||||
|
log.debug("## OVERLAPS_WITH_START");
|
||||||
|
log.debug("## diffDate("+existing_validFrom+","+ new_validTo+"):"+diffDate(existing_validFrom, new_validTo));
|
||||||
|
log.debug("## diffDate("+existing_validFrom+","+ new_validFrom+"):"+diffDate(existing_validFrom, new_validFrom));
|
||||||
|
*/
|
||||||
|
if (diffDate(existing_validFrom, new_validTo)>0 &&
|
||||||
|
diffDate(existing_validFrom, new_validFrom)<0 ){
|
||||||
|
checkResult=CheckResult.OVERLAPS_WITH_START;
|
||||||
|
log.debug("OVERLAPS_WITH_START");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return checkResult;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private static long diffDate(Date d1, Date d2){
|
||||||
|
return TalendDate.diffDate(d2,d1,"dd",true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Date calc_ValidFrom(Date existing_validFrom, Date new_validFrom){
|
||||||
|
|
||||||
|
Date initValidFromDate = null;
|
||||||
|
Date dresult = null;
|
||||||
|
Date curDate = null;
|
||||||
|
|
||||||
|
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
|
||||||
|
Date date = new Date();
|
||||||
|
String strcurDate = dateFormat.format(date);
|
||||||
|
|
||||||
|
if (new_validFrom == null) new_validFrom = constant.getFarPastDate();
|
||||||
|
|
||||||
|
try {
|
||||||
|
curDate = dateFormat.parse(strcurDate);
|
||||||
|
log.debug("curDate:"+curDate.toString());
|
||||||
|
}
|
||||||
|
catch (ParseException e)
|
||||||
|
{}
|
||||||
|
|
||||||
|
log.debug("calc_ValidFrom(existing_validFrom = %s, new_validFrom = %s) called", existing_validFrom, new_validFrom);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
SimpleDateFormat textFormat = new SimpleDateFormat("yyyy-MM-dd");
|
||||||
|
String paramDateAsString = "1970-01-01";
|
||||||
|
initValidFromDate = textFormat.parse(paramDateAsString);
|
||||||
|
|
||||||
|
if ( TalendDate.diffDate(existing_validFrom,initValidFromDate,"dd") == 0)
|
||||||
|
{
|
||||||
|
log.debug("calc_ValidFrom: ValidFrom in MDM is DefaultDate");
|
||||||
|
//MDM enthält default Wert
|
||||||
|
if ( TalendDate.diffDate(new_validFrom,initValidFromDate,"dd") == 0)
|
||||||
|
{
|
||||||
|
log.debug("calc_ValidFrom: ValidFrom in msg is DefaultDate => use Default date");
|
||||||
|
//auch Message enthält default Wert
|
||||||
|
dresult = initValidFromDate;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
log.debug("calc_ValidFrom: ValidFrom in msg is NOT DefaultDate => use msg date");
|
||||||
|
dresult = new_validFrom;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
log.debug("calc_ValidFrom: ValidFrom in MDM is NOT DefaultDate");
|
||||||
|
|
||||||
|
//MDM enthält KEINE default Wert
|
||||||
|
if ( TalendDate.diffDate(new_validFrom,initValidFromDate,"dd") == 0)
|
||||||
|
{
|
||||||
|
log.debug("calc_ValidFrom: ValidFrom in msg is DefaultDate => use MDM date");
|
||||||
|
// Message enthält default Wert
|
||||||
|
dresult = existing_validFrom;
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if ( TalendDate.diffDate(new_validFrom,curDate,"dd") < 0)
|
||||||
|
{
|
||||||
|
//log.debug("calc_ValidFrom: ValidFrom in msg is in PAST => use MDM date");
|
||||||
|
dresult = existing_validFrom;
|
||||||
|
|
||||||
|
// 2021-02-21: deactivate condition to allow Update in past
|
||||||
|
log.debug("calc_ValidFrom: ValidFrom in msg is in PAST => use Msg date (changed 2021-02-21)");
|
||||||
|
dresult = new_validFrom;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
log.debug("calc_ValidFrom: ValidFrom in msg is NOT in PAST => use Msg date");
|
||||||
|
dresult = new_validFrom;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
catch (ParseException e) {
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
log.debug("calc_ValidFrom: Returning %s", dresult);
|
||||||
|
return dresult;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Date calc_ValidTo(Date existing_validTo, Date new_validTo){
|
||||||
|
|
||||||
|
Date initValidToDate = null;
|
||||||
|
Date dresult = null;
|
||||||
|
|
||||||
|
if (new_validTo == null) new_validTo = constant.getFarFutureDate();
|
||||||
|
|
||||||
|
log.debug("calc_ValidTo(existing_validTo = %s, new_validTo = %s) called", existing_validTo, new_validTo);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
SimpleDateFormat textFormat = new SimpleDateFormat("yyyy-MM-dd");
|
||||||
|
String paramDateAsString = "2099-12-31";
|
||||||
|
initValidToDate = textFormat.parse(paramDateAsString);
|
||||||
|
|
||||||
|
if ( TalendDate.diffDate(existing_validTo,initValidToDate,"dd") == 0)
|
||||||
|
{
|
||||||
|
log.debug("calc_ValidTo: ValidTo in MDM is DefaultDate");
|
||||||
|
//MDM enthält default Wert
|
||||||
|
if ( TalendDate.diffDate(new_validTo,initValidToDate,"dd") == 0)
|
||||||
|
{
|
||||||
|
log.debug("calc_ValidTo: ValidTo in Msg is also DefaultDate => use Default Date");
|
||||||
|
//auch Message enthält default Wert
|
||||||
|
dresult = initValidToDate;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
log.debug("calc_ValidTo: but ValidTo in Msg is NOT DefaultDate => use Msg Date");
|
||||||
|
dresult = new_validTo;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
log.debug("calc_ValidTo: ValidTo in MDM is NOT DefaultDate");
|
||||||
|
|
||||||
|
//MDM enthält KEINE default Wert
|
||||||
|
if ( TalendDate.diffDate(new_validTo,initValidToDate,"dd") == 0)
|
||||||
|
{
|
||||||
|
log.debug("calc_ValidTo: and ValidTo in Msg is DefaultDate => use MDM Date");
|
||||||
|
// Message enthält default Wert
|
||||||
|
dresult = existing_validTo;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
log.debug("calc_ValidTo: and ValidTo in Msg is also NOT DefaultDate => use Msg Date");
|
||||||
|
dresult = new_validTo;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
catch (ParseException e) {
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
log.debug("calc_ValidTo: Returning %s", dresult);
|
||||||
|
return dresult;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ResultInsertUpdate DetermineIfInsOrUpd(Date existing_validFrom, Date existing_validTo, Date new_validFrom,Date new_validTo, String ValidityResult, String isNew)
|
||||||
|
{
|
||||||
|
|
||||||
|
//ONLY IF MDM is not set to default and Values from Incoming Message is NOT set to defaults
|
||||||
|
// log.debug("Enter DetermineIfInsOrUpd");
|
||||||
|
// log.debug("existing_validFrom:" + existing_validFrom.toString());
|
||||||
|
// log.debug("existing_validTo:" + existing_validTo.toString());
|
||||||
|
// log.debug("new_validFrom:" + (new_validFrom.equals(null)?"NULL":new_validFrom.toString()));
|
||||||
|
// log.debug("new_validTo:" + new_validTo.toString());
|
||||||
|
log.debug("DetermineIfInsOrUpd: checking ValidityResult:" + ValidityResult);
|
||||||
|
|
||||||
|
ResultInsertUpdate rInsUpd= ResultInsertUpdate.ERROR;
|
||||||
|
|
||||||
|
if (ValidityResult.contains("EXTENDS_AFTER") || ValidityResult.contains("EXTENDS_BEFORE") || ValidityResult.contains("HAS_GAP_AFTER") || ValidityResult.contains("HAS_GAP_BEFORE") || ValidityResult.contains("TO_FROM_SWITCHED") || (ValidityResult.contains("SETVALIDFROM") && "new".equals(isNew)) )
|
||||||
|
{
|
||||||
|
//First insert of a record -> no date exists
|
||||||
|
log.debug("ResultInsertUpdate.INSERT");
|
||||||
|
return ResultInsertUpdate.INSERT;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ValidityResult.contains("EQUALS") || ValidityResult.contains("SHORTENED"))
|
||||||
|
{
|
||||||
|
//First insert of a record -> no date exists
|
||||||
|
log.debug("ResultInsertUpdate.EQUALS");
|
||||||
|
return ResultInsertUpdate.UPDATE;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ValidityResult.contains("OVERLAPS_WITH_START") || ValidityResult.contains("OVERLAPS_WITH_END") || ValidityResult.contains("CONTAINS"))
|
||||||
|
{
|
||||||
|
//First insert of a record -> no date exists
|
||||||
|
log.debug("ResultInsertUpdate.ERROR");
|
||||||
|
return ResultInsertUpdate.ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
return rInsUpd;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String DetermineIfValidityCheckOK(Date existing_validFrom, Date existing_validTo, Date new_validFrom,Date new_validTo, String ValidityResult, String OPType)
|
||||||
|
{
|
||||||
|
|
||||||
|
//ONLY IF MDM is not set to default and Values from Incoming Message is NOT set to defaults
|
||||||
|
// log.debug("Enter DetermineIfValidityCheckOK");
|
||||||
|
// log.debug("existing_validFrom:" + existing_validFrom.toString());
|
||||||
|
// log.debug("existing_validTo:" + existing_validTo.toString());
|
||||||
|
// log.debug("new_validFrom:" + (new_validFrom.equals(null)?"NULL":new_validFrom.toString()));
|
||||||
|
// log.debug("new_validTo:" + new_validTo.toString());
|
||||||
|
// log.debug("ValidityResult:" + ValidityResult);
|
||||||
|
|
||||||
|
String rValChck= "OPType";
|
||||||
|
|
||||||
|
if ("INSERT".equals(OPType))
|
||||||
|
{
|
||||||
|
if (existing_validFrom == null && existing_validTo== null)
|
||||||
|
{
|
||||||
|
if ("FIRSTINSERT".equals(ValidityResult))
|
||||||
|
{
|
||||||
|
log.debug("CheckResult.FIRSTINSERT");
|
||||||
|
return "OK";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
log.debug("!FIRSTINSERT");
|
||||||
|
return "ERROR";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (ValidityResult.contains("EXTENDS_AFTER") || ValidityResult.contains("EXTENDS_BEFORE") || ValidityResult.contains("HAS_GAP_AFTER") || ValidityResult.contains("HAS_GAP_BEFORE") || ValidityResult.contains("TO_FROM_SWITCHED"))
|
||||||
|
{ //for inserts
|
||||||
|
log.debug("EXTENDS_AFTER....");
|
||||||
|
return "OK";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{ //for inserts
|
||||||
|
log.debug("!EXTENDS_AFTER....");
|
||||||
|
return "ERROR";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if ("UPDATE".equals(OPType))
|
||||||
|
{
|
||||||
|
if
|
||||||
|
(
|
||||||
|
(TalendDate.diffDate(existing_validFrom,constant.getFarPastDate(),"dd") == 0) &&
|
||||||
|
(TalendDate.diffDate(existing_validTo,constant.getFarFutureDate(),"dd") == 0)
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//log.debug("MDM Default Validities");
|
||||||
|
|
||||||
|
if ((diffDate(existing_validFrom,constant.getFarPastDate()) ==0 && diffDate(existing_validTo,new_validTo)==0 && diffDate(existing_validFrom,new_validFrom)>0) || ValidityResult.contains("SETVALIDFROM"))
|
||||||
|
{
|
||||||
|
// MDM *......* (1970 to 2099)
|
||||||
|
// Inc *...* (2015 to 2099)
|
||||||
|
|
||||||
|
log.debug("CheckResult.SETVALIDFROM");
|
||||||
|
return "OK";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ValidityResult.contains("CONTAINS") || ValidityResult.contains("EQUALS") || ValidityResult.contains("SHORTENED"))
|
||||||
|
{
|
||||||
|
log.debug("CONTAINS, EQUALS, SHORTENED");
|
||||||
|
return "OK";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
log.debug("!UPDATE....");
|
||||||
|
return "ERROR";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (ValidityResult.contains("EQUALS") || ValidityResult.contains("SHORTENED"))
|
||||||
|
{
|
||||||
|
log.debug("SHORTENED....");
|
||||||
|
return "OK";
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//MDM has default Validities
|
||||||
|
if
|
||||||
|
(
|
||||||
|
(TalendDate.diffDate(existing_validFrom,constant.getFarPastDate(),"dd") == 0) &&
|
||||||
|
(TalendDate.diffDate(existing_validTo,constant.getFarFutureDate(),"dd") == 0)
|
||||||
|
)
|
||||||
|
{
|
||||||
|
log.debug("MDM Default Validities");
|
||||||
|
|
||||||
|
if (ValidityResult.contains("UPDATE") || ValidityResult.contains("EQUALS"))
|
||||||
|
{
|
||||||
|
return "OK";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return "ERROR";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
//MDM element has no default validities
|
||||||
|
if (ValidityResult.contains("EXTENDS_AFTER") || ValidityResult.contains("EXTENDS_BEFORE") || ValidityResult.contains("HAS_GAP_AFTER") || ValidityResult.contains("HAS_GAP_BEFORE"))
|
||||||
|
{ //for inserts
|
||||||
|
return "OK";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ValidityResult.contains("EQUALS") || ValidityResult.contains("SHORTENED") || ValidityResult.contains("TO_FROM_SWITCHED"))
|
||||||
|
{ //for updates
|
||||||
|
return "OK";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ValidityResult.contains("OVERLAPS_WITH_START") || ValidityResult.contains("OVERLAPS_WITH_END") )
|
||||||
|
{ //for updates
|
||||||
|
return "ERROR";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
return rValChck;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String DetermineIfValidityCheckOK_ALT(Date existing_validFrom, Date existing_validTo, Date new_validFrom,Date new_validTo, String ValidityResult)
|
||||||
|
{
|
||||||
|
|
||||||
|
//ONLY IF MDM is not set to default and Values from Incoming Message is NOT set to defaults
|
||||||
|
log.debug("Enter DetermineIfValidityCheckOK");
|
||||||
|
log.debug("existing_validFrom:" + existing_validFrom.toString());
|
||||||
|
log.debug("existing_validTo:" + existing_validTo.toString());
|
||||||
|
log.debug("new_validFrom:" + (new_validFrom.equals(null)?"NULL":new_validFrom.toString()));
|
||||||
|
log.debug("new_validTo:" + new_validTo.toString());
|
||||||
|
log.debug("ValidityResult:" + ValidityResult);
|
||||||
|
|
||||||
|
String rValChck= "ERROR";
|
||||||
|
|
||||||
|
if (existing_validFrom == null && existing_validTo== null)
|
||||||
|
{
|
||||||
|
//log.debug("CheckResult.FIRSTINSERT");
|
||||||
|
return "OK";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//MDM has default Validities
|
||||||
|
if
|
||||||
|
(
|
||||||
|
(TalendDate.diffDate(existing_validFrom,constant.getFarPastDate(),"dd") == 0) &&
|
||||||
|
(TalendDate.diffDate(existing_validTo,constant.getFarFutureDate(),"dd") == 0)
|
||||||
|
)
|
||||||
|
{
|
||||||
|
log.debug("MDM Default Validities");
|
||||||
|
|
||||||
|
if (ValidityResult.contains("UPDATE") || ValidityResult.contains("EQUALS"))
|
||||||
|
{
|
||||||
|
return "OK";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return "ERROR";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
//MDM element has no default validities
|
||||||
|
if (ValidityResult.contains("EXTENDS_AFTER") || ValidityResult.contains("EXTENDS_BEFORE") || ValidityResult.contains("HAS_GAP_AFTER") || ValidityResult.contains("HAS_GAP_BEFORE"))
|
||||||
|
{ //for inserts
|
||||||
|
return "OK";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ValidityResult.contains("EQUALS") || ValidityResult.contains("SHORTENED") || ValidityResult.contains("TO_FROM_SWITCHED"))
|
||||||
|
{ //for updates
|
||||||
|
return "OK";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ValidityResult.contains("OVERLAPS_WITH_START") || ValidityResult.contains("OVERLAPS_WITH_END") )
|
||||||
|
{ //for updates
|
||||||
|
return "ERROR";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
return rValChck;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,87 @@
|
|||||||
|
package routines;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* user specification: the function's comment should contain keys as follows: 1. write about the function's comment.but
|
||||||
|
* it must be before the "{talendTypes}" key.
|
||||||
|
*
|
||||||
|
* 2. {talendTypes} 's value must be talend Type, it is required . its value should be one of: String, char | Character,
|
||||||
|
* long | Long, int | Integer, boolean | Boolean, byte | Byte, Date, double | Double, float | Float, Object, short |
|
||||||
|
* Short
|
||||||
|
*
|
||||||
|
* 3. {Category} define a category for the Function. it is required. its value is user-defined .
|
||||||
|
*
|
||||||
|
* 4. {param} 's format is: {param} <type>[(<default value or closed list values>)] <name>[ : <comment>]
|
||||||
|
*
|
||||||
|
* <type> 's value should be one of: string, int, list, double, object, boolean, long, char, date. <name>'s value is the
|
||||||
|
* Function's parameter name. the {param} is optional. so if you the Function without the parameters. the {param} don't
|
||||||
|
* added. you can have many parameters for the Function.
|
||||||
|
*
|
||||||
|
* 5. {example} gives a example for the Function. it is optional.
|
||||||
|
*/
|
||||||
|
public class LcagBusinessCode {
|
||||||
|
private static LcagLogger log = LcagLogger.getLogger(LcagDateTime.class);
|
||||||
|
private static Map<String, Pattern> LOCATION_PATTERN = null;
|
||||||
|
|
||||||
|
public LcagBusinessCode() {} { }
|
||||||
|
|
||||||
|
private static Map<String, Pattern> getLocationPatterns() {
|
||||||
|
if (LOCATION_PATTERN != null) return LOCATION_PATTERN;
|
||||||
|
|
||||||
|
LOCATION_PATTERN = new HashMap<String, Pattern>();
|
||||||
|
List<String>locPatternList = null;
|
||||||
|
int flags = Pattern.CASE_INSENSITIVE;
|
||||||
|
|
||||||
|
LOCATION_PATTERN.put("Airport", Pattern.compile(".*airport.*|.*apt$", flags));
|
||||||
|
LOCATION_PATTERN.put("Bus Station", Pattern.compile(".* bus .*|.*central.b*", flags));
|
||||||
|
LOCATION_PATTERN.put("Harbour", Pattern.compile(".* harbour.*", flags));
|
||||||
|
LOCATION_PATTERN.put("Heliport", Pattern.compile(".*heliport.*", flags));
|
||||||
|
LOCATION_PATTERN.put("Off-Line Point", Pattern.compile(".*off.line p.*|.*off$|.*off.l.{0,2}$", flags));
|
||||||
|
LOCATION_PATTERN.put("Rail Station", Pattern.compile(".* rail .*|.*central.r.*", flags));
|
||||||
|
//LOCATION_PATTERN.put("Miscellaneous", null);
|
||||||
|
|
||||||
|
return LOCATION_PATTERN;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* deriveLocationType: Parse a Station Name and figure out if it is an Airport, a Bus Station, etc..
|
||||||
|
*
|
||||||
|
* @param Original Station Name
|
||||||
|
* @return null if no location Type can be derived, otherwise a IATA Location Type
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} LCAG
|
||||||
|
*
|
||||||
|
* {param} String("aName") aName: Original Station Name
|
||||||
|
*
|
||||||
|
* {examples}
|
||||||
|
*/
|
||||||
|
public static String deriveLocationType(String aName)
|
||||||
|
{
|
||||||
|
// Function implemented because BE input often uses Airport but the name indicate a different location.
|
||||||
|
String result = null;
|
||||||
|
Map<String, Pattern> locPatternList = LcagBusinessCode.getLocationPatterns();
|
||||||
|
|
||||||
|
for (String locType : locPatternList.keySet()) {
|
||||||
|
Pattern pattern = locPatternList.get(locType);
|
||||||
|
|
||||||
|
if (pattern == null) continue;
|
||||||
|
|
||||||
|
if (pattern.matcher(aName).matches()) {
|
||||||
|
result = locType;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
package routines;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* user specification: the function's comment should contain keys as follows: 1. write about the function's comment.but
|
||||||
|
* it must be before the "{talendTypes}" key.
|
||||||
|
*
|
||||||
|
* 2. {talendTypes} 's value must be talend Type, it is required . its value should be one of: String, char | Character,
|
||||||
|
* long | Long, int | Integer, boolean | Boolean, byte | Byte, Date, double | Double, float | Float, Object, short |
|
||||||
|
* Short
|
||||||
|
*
|
||||||
|
* 3. {Category} define a category for the Function. it is required. its value is user-defined .
|
||||||
|
*
|
||||||
|
* 4. {param} 's format is: {param} <type>[(<default value or closed list values>)] <name>[ : <comment>]
|
||||||
|
*
|
||||||
|
* <type> 's value should be one of: string, int, list, double, object, boolean, long, char, date. <name>'s value is the
|
||||||
|
* Function's parameter name. the {param} is optional. so if you the Function without the parameters. the {param} don't
|
||||||
|
* added. you can have many parameters for the Function.
|
||||||
|
*
|
||||||
|
* 5. {example} gives a example for the Function. it is optional.
|
||||||
|
*/
|
||||||
|
public class LcagConstant {
|
||||||
|
|
||||||
|
/* Date Patterns */
|
||||||
|
public static final String FILE_TIMESTAMP_FORMAT="yyyy-MM-dd_HH-mm-ss-SSS";
|
||||||
|
|
||||||
|
/* LOG Level, sequence taken over from apache commons logging */
|
||||||
|
public static final int LOG_TRACE = 2;
|
||||||
|
public static final int LOG_DEBUG = 1;
|
||||||
|
public static final int LOG_INFO = 0;
|
||||||
|
public static final int LOG_WARN = -1;
|
||||||
|
public static final int LOG_ERROR = -2;
|
||||||
|
public static final int LOG_FATAL = -3;
|
||||||
|
}
|
||||||
1063
MDM-LCAG-JavaFunctions/src/main/java/routines/LcagDateTime.java
Normal file
1063
MDM-LCAG-JavaFunctions/src/main/java/routines/LcagDateTime.java
Normal file
File diff suppressed because it is too large
Load Diff
424
MDM-LCAG-JavaFunctions/src/main/java/routines/LcagFileTools.java
Normal file
424
MDM-LCAG-JavaFunctions/src/main/java/routines/LcagFileTools.java
Normal file
@@ -0,0 +1,424 @@
|
|||||||
|
package routines;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.nio.charset.Charset;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import org.apache.commons.io.FileUtils;
|
||||||
|
import org.apache.commons.io.FilenameUtils;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* user specification: the function's comment should contain keys as follows: 1. write about the function's comment.but
|
||||||
|
* it must be before the "{talendTypes}" key.
|
||||||
|
*
|
||||||
|
* 2. {talendTypes} 's value must be talend Type, it is required . its value should be one of: String, char | Character,
|
||||||
|
* long | Long, int | Integer, boolean | Boolean, byte | Byte, Date, double | Double, float | Float, Object, short |
|
||||||
|
* Short
|
||||||
|
*
|
||||||
|
* 3. {Category} define a category for the Function. it is required. its value is user-defined .
|
||||||
|
*
|
||||||
|
* 4. {param} 's format is: {param} <type>[(<default value or closed list values>)] <name>[ : <comment>]
|
||||||
|
*
|
||||||
|
* <type> 's value should be one of: string, int, list, double, object, boolean, long, char, date. <name>'s value is the
|
||||||
|
* Function's parameter name. the {param} is optional. so if you the Function without the parameters. the {param} don't
|
||||||
|
* added. you can have many parameters for the Function.
|
||||||
|
*
|
||||||
|
* 5. {example} gives a example for the Function. it is optional.
|
||||||
|
*/
|
||||||
|
public class LcagFileTools {
|
||||||
|
|
||||||
|
private static LcagLogger log = LcagLogger.getLogger(LcagFileTools.class);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* getName: Get only the name of the given file path (name with extension)
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} LCAG-File
|
||||||
|
*
|
||||||
|
* {param} string filePath: File path to use
|
||||||
|
*
|
||||||
|
* {example} getName("a/b/HelloWorld.txt") = "HelloWorld.txt"
|
||||||
|
*/
|
||||||
|
public static String getName(String filePath) {
|
||||||
|
String result = FilenameUtils.getName(filePath);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* getBaseName: Get the base name of the given file path (without extension)
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} LCAG-File
|
||||||
|
*
|
||||||
|
* {param} string filePath: File path to use
|
||||||
|
*
|
||||||
|
* {example} getName("a/b/HelloWorld.txt") = "HelloWorld"
|
||||||
|
*/
|
||||||
|
public static String getBaseName(String filePath) {
|
||||||
|
String result = FilenameUtils.getBaseName(filePath);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* getBaseName: Get the extension of the given file path.
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} LCAG-File
|
||||||
|
*
|
||||||
|
* {param} string filePath: File path to use
|
||||||
|
*
|
||||||
|
* {example} getName("a/b/HelloWorld.txt") = "txt"
|
||||||
|
*/
|
||||||
|
public static String getExtension(String filePath) {
|
||||||
|
String result = FilenameUtils.getExtension(filePath);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* getBaseName: Get the path of the given file path without the file name.
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} LCAG-File
|
||||||
|
*
|
||||||
|
* {param} string filePath: File path to use
|
||||||
|
*
|
||||||
|
* {example} getName("a/b/HelloWorld.txt") = "a/b";
|
||||||
|
*/
|
||||||
|
public static String getPath(String filePath) {
|
||||||
|
String result = FilenameUtils.getFullPath(filePath);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* readTextFile: Read a file into a String. First it tries to lcate the given
|
||||||
|
* file path directly. If not found, it tries to locate the file path as a
|
||||||
|
* resource.
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} LCAG-File
|
||||||
|
*
|
||||||
|
* {param} string fileName: File path to read
|
||||||
|
*
|
||||||
|
* {example} readTextFile("HelloWorld.txt")
|
||||||
|
*/
|
||||||
|
public static String readTextFile(String fileName) throws java.io.IOException {
|
||||||
|
String result = null;
|
||||||
|
File file = new File(fileName);
|
||||||
|
String resourceilePath = getResourceFilePath(fileName);
|
||||||
|
|
||||||
|
if (file.canRead()) {
|
||||||
|
String fPath = file.getAbsolutePath();
|
||||||
|
java.nio.file.Path filePath = java.nio.file.Paths.get(fPath);
|
||||||
|
|
||||||
|
byte[] fileContent = java.nio.file.Files.readAllBytes(filePath);
|
||||||
|
|
||||||
|
result = new String(fileContent);
|
||||||
|
} else {
|
||||||
|
result = readResourceFile(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void writeTextFile(String pFilePath, String pText) throws java.io.IOException {
|
||||||
|
final File file = new File(pFilePath);
|
||||||
|
|
||||||
|
FileUtils.writeStringToFile(file, pText, Charset.forName("UTF-8"));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getResourceFilePath(String fileName) throws IOException {
|
||||||
|
String result = fileName;
|
||||||
|
|
||||||
|
if (!fileName.contains("/") && !fileName.contains("\\")) {
|
||||||
|
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
|
||||||
|
|
||||||
|
URL url = classLoader.getResource(fileName);
|
||||||
|
result = url.getPath();
|
||||||
|
|
||||||
|
if (result.startsWith("/")) {
|
||||||
|
// Drop staring / from paths like /C:/folder: Cuase file not found error when
|
||||||
|
// opening
|
||||||
|
result = result.substring(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String readResourceFile(String fileName) throws IOException {
|
||||||
|
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
|
||||||
|
String result = null;
|
||||||
|
|
||||||
|
InputStream is = classLoader.getResourceAsStream(fileName);
|
||||||
|
|
||||||
|
if (is == null) {
|
||||||
|
throw new IOException(String.format("readResourceFile: File %s not found", fileName));
|
||||||
|
}
|
||||||
|
|
||||||
|
InputStreamReader isr = new InputStreamReader(is);
|
||||||
|
BufferedReader reader = new BufferedReader(isr);
|
||||||
|
|
||||||
|
result = reader.lines().collect(Collectors.joining(System.lineSeparator()));
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* createSameJobFileName: For a context.env_inputDirectory/fileName.ext create a
|
||||||
|
* new file name for the same job. The target in relative (see rePath) to the
|
||||||
|
* input directory: {targetDir}/fileName{preExtension}.ext
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} LCAG
|
||||||
|
*
|
||||||
|
* {param} String("srcFileName") input: source file name {param}
|
||||||
|
* String("targetDir") input: Target directory. use context.env_outputDir,
|
||||||
|
* context.env_tempDir, ... {param} String("preExtension") input: file name part
|
||||||
|
* to prepend before the file name extension {param} String("ext") input: use
|
||||||
|
* this extension instead of original one
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static String createSameJobFileName(String srcFileName, String targetDir, String preExtension, String ext) {
|
||||||
|
if (srcFileName == null)
|
||||||
|
return null;
|
||||||
|
if (preExtension == null)
|
||||||
|
preExtension = "";
|
||||||
|
if (ext == null)
|
||||||
|
ext = FilenameUtils.getExtension(srcFileName);
|
||||||
|
|
||||||
|
// get basename without extension
|
||||||
|
String baseName = FilenameUtils.getBaseName(srcFileName);
|
||||||
|
|
||||||
|
String result = String.format("%s/%s%s.%s", targetDir, baseName, preExtension, ext);
|
||||||
|
result = FilenameUtils.normalize(result);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* createTimestampedFileName: Used for receive jobs and export from DB Jobs,
|
||||||
|
* only.
|
||||||
|
*
|
||||||
|
* Creates a file name starting with a UTC timestamp for a given date. Drops
|
||||||
|
* leading directory path.
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} LCAG-File
|
||||||
|
*
|
||||||
|
* {param} String("aDate") input: Date to use for timestamp {param}
|
||||||
|
* String("srcFileName") input: source file name
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static String createTimestampedFileName(Date aDate, String srcFileName) {
|
||||||
|
if (aDate == null) {
|
||||||
|
aDate = TalendDate.getCurrentDate();
|
||||||
|
}
|
||||||
|
String srcFileBaseName = FilenameUtils.getName((String) srcFileName);
|
||||||
|
String tsString = TalendDate.formatDateInUTC(LcagDateTime.MDM_FILE_TIMESTAMP_FORMAT_STR, aDate);
|
||||||
|
|
||||||
|
String result = String.format("%s_%s", tsString, srcFileBaseName);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* createTimestampedFileName: Used for receive jobs and export from DB Jobs,
|
||||||
|
* only.
|
||||||
|
*
|
||||||
|
* Creates a file name starting with a current UTC timestamp.
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} LCAG
|
||||||
|
*
|
||||||
|
* {param} String("srcFileName") input: source file name
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static String createTimestampedFileName(String srcFileName) {
|
||||||
|
|
||||||
|
String result = createTimestampedFileName(TalendDate.getCurrentDate(), srcFileName);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* getFileTimestampAsString: retrieves the MDM timestamp prefix of a given file
|
||||||
|
* name.
|
||||||
|
*
|
||||||
|
* {talendTypes} Object
|
||||||
|
*
|
||||||
|
* {Category} LCAG
|
||||||
|
*
|
||||||
|
* {param} String("srcFileName") input: file name with timestamp prefix
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static String getFileTimestampAsString(Object srcFileName) {
|
||||||
|
String result = null;
|
||||||
|
|
||||||
|
String srcFileBaseName = FilenameUtils.getBaseName((String) srcFileName);
|
||||||
|
int fnLength = srcFileBaseName.length();
|
||||||
|
int tsLength = LcagDateTime.MDM_FILE_TIMESTAMP_FORMAT_STR.replaceAll("'", "").length();
|
||||||
|
|
||||||
|
if (fnLength >= tsLength) {
|
||||||
|
result = srcFileBaseName.substring(0, tsLength);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* getFileTimestamp: retrieves the MDM data denoted by the MDM timestamp prefix
|
||||||
|
* of a given file name.
|
||||||
|
*
|
||||||
|
* {talendTypes} Date
|
||||||
|
*
|
||||||
|
* {Category} LCAG
|
||||||
|
*
|
||||||
|
* {param} String("srcFileName") input: file name with timestamp prefix
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static Date getFileTimestamp(String srcFileName) {
|
||||||
|
|
||||||
|
if (srcFileName == null)
|
||||||
|
throw new IllegalArgumentException("Filename argument may not be null");
|
||||||
|
|
||||||
|
Date timestamp = null;
|
||||||
|
int len = (srcFileName == null) ? 0 : srcFileName.length();
|
||||||
|
|
||||||
|
if (len > 0) {
|
||||||
|
String timestampString = getFileTimestampAsString(srcFileName);
|
||||||
|
timestamp = LcagDateTime.secureParseDate(LcagConstant.FILE_TIMESTAMP_FORMAT, timestampString);
|
||||||
|
}
|
||||||
|
|
||||||
|
return timestamp;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* createPrefixedFileName: Used for jobs, only.
|
||||||
|
*
|
||||||
|
* Creates a file name starting with a current UTC timestamp and a number.
|
||||||
|
* Create for receiving multiple messages: The file name should start with the
|
||||||
|
* context.env_jobTimestamp followed by a number n that defines the n-th message
|
||||||
|
* received
|
||||||
|
*
|
||||||
|
* Note: The prefix should identify the data flow of an incoming message
|
||||||
|
* throughout its entire processing by multiple jobs
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} LCAG-File
|
||||||
|
*
|
||||||
|
* {param} String("srcFileName") input: source file name {param} Date("d")
|
||||||
|
* input: Date to use, recommended: context.env_jobTimestamp {param} int("n")
|
||||||
|
* input: Number to make the prefix unique throughout all jobs
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static String createPrefixedFileName(String srcFileName, Date d, int n) {
|
||||||
|
String prefix = LcagDateTime.MDM_FILE_TIMESTAMP_FORMAT.format(d) + String.format("-x%05d_", n);
|
||||||
|
String result = prefix + srcFileName;
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Path appendStringToFilePath(Path pSrcFilePath, String pEndFileNameWith) {
|
||||||
|
String filePath = pSrcFilePath.toString();
|
||||||
|
String ext = "." + getExtension(filePath);
|
||||||
|
|
||||||
|
filePath = filePath.replace(ext, pEndFileNameWith + ext);
|
||||||
|
|
||||||
|
return Path.of(filePath);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* getFileNamePrefix: Used to fetch the file name prefix created by function
|
||||||
|
* createPrefixedFileName. This prefix should identify the data flow of an
|
||||||
|
* incoming message throughout its entire processing by multiple jobs
|
||||||
|
*
|
||||||
|
* Note: This version supports the new prefix type (Timestamp prefix with
|
||||||
|
* number) as well as the old format (only Timestamp)
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} LCAG-File
|
||||||
|
*
|
||||||
|
* {param} String("srcFileName") input: source file name
|
||||||
|
*/
|
||||||
|
public static String getFileNamePrefix(String srcFileName) {
|
||||||
|
String result = "";
|
||||||
|
|
||||||
|
if (srcFileName == null)
|
||||||
|
throw new IllegalArgumentException("File name argument must not be null");
|
||||||
|
|
||||||
|
String srcFileBaseName = FilenameUtils.getBaseName((String) srcFileName);
|
||||||
|
|
||||||
|
if (srcFileBaseName.indexOf("-x") < 0) {
|
||||||
|
result = getFileTimestampAsString(srcFileName);
|
||||||
|
|
||||||
|
if (result == null)
|
||||||
|
result = "";
|
||||||
|
} else {
|
||||||
|
// replace needed to correctly handle formats including 'T' or 'Z':
|
||||||
|
int prefixLength = LcagDateTime.MDM_FILE_TIMESTAMP_FORMAT_STR.replace("'", "").length()
|
||||||
|
+ String.format("-x%05d_", 0).length();
|
||||||
|
|
||||||
|
if (srcFileBaseName.length() >= prefixLength) {
|
||||||
|
result = srcFileBaseName.substring(0, prefixLength);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static List<File> findFilesWithExtension(String pDirectoryPath, String... pExtList) {
|
||||||
|
List<File> fileList = new ArrayList<>();
|
||||||
|
File directory = new File(pDirectoryPath);
|
||||||
|
|
||||||
|
findFilesWithExensionRecursive(directory, fileList, pExtList);
|
||||||
|
return fileList;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void findFilesWithExensionRecursive(File pDirectory, List<File> pFileList, String... pExtList) {
|
||||||
|
File[] files = pDirectory.listFiles();
|
||||||
|
if (files != null) {
|
||||||
|
for (File file : files) {
|
||||||
|
if (file.isDirectory()) {
|
||||||
|
findFilesWithExensionRecursive(file, pFileList, pExtList); // Recurse into sub directories
|
||||||
|
} else {
|
||||||
|
for (String ext : pExtList) {
|
||||||
|
if (file.getName().endsWith(ext)) {
|
||||||
|
pFileList.add(file);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
434
MDM-LCAG-JavaFunctions/src/main/java/routines/LcagGeneral.java
Normal file
434
MDM-LCAG-JavaFunctions/src/main/java/routines/LcagGeneral.java
Normal file
@@ -0,0 +1,434 @@
|
|||||||
|
package routines;
|
||||||
|
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* user specification: the function's comment should contain keys as follows: 1. write about the function's comment.but
|
||||||
|
* it must be before the "{talendTypes}" key.
|
||||||
|
*
|
||||||
|
* 2. {talendTypes} 's value must be talend Type, it is required . its value should be one of: String, char | Character,
|
||||||
|
* long | Long, int | Integer, boolean | Boolean, byte | Byte, Date, double | Double, float | Float, Object, short |
|
||||||
|
* Short
|
||||||
|
*
|
||||||
|
* 3. {Category} define a category for the Function. it is required. its value is user-defined .
|
||||||
|
*
|
||||||
|
* 4. {param} 's format is: {param} <type>[(<default value or closed list values>)] <name>[ : <comment>]
|
||||||
|
*
|
||||||
|
* <type> 's value should be one of: string, int, list, double, object, boolean, long, char, date. <name>'s value is the
|
||||||
|
* Function's parameter name. the {param} is optional. so if you the Function without the parameters. the {param} don't
|
||||||
|
* added. you can have many parameters for the Function.
|
||||||
|
*
|
||||||
|
* 5. {example} gives a example for the Function. it is optional.
|
||||||
|
*/
|
||||||
|
public class LcagGeneral {
|
||||||
|
public static final String IN_SYNC_POLICY_IGNORE_CASE = "ignore-case";
|
||||||
|
public static final String IN_SYNC_POLICY_CASE_SENSITIVE = "case-sensitive";
|
||||||
|
public static final String COMPARE_SPACES_DIFFER = "_spacesDiffer_";
|
||||||
|
public static final String COMPARE_PART_OF = "_partOf_";
|
||||||
|
public static final String COMPARE_CONTAINS = "_contains_";
|
||||||
|
public static final String COMPARE_ENDS_WITH = "_endsWith_";
|
||||||
|
public static final String COMPARE_STARTS_WITH = "_startsWith_";
|
||||||
|
public static final String COMPARE_ERROR = "?error?";
|
||||||
|
public static final String COMPARE_EQUAL_EXCEPT_CASE = "_equalExceptCase_";
|
||||||
|
public static final String COMPARE_EQUALS = "_equal_";
|
||||||
|
public static final String COMPARE_NOT_AVAILABLE = "_n/a_";
|
||||||
|
private static LcagLogger log = LcagLogger.getLogger(LcagGeneral.class);
|
||||||
|
|
||||||
|
public LcagGeneral() {
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sleep some milli seconds
|
||||||
|
*
|
||||||
|
* {talendTypes} Void
|
||||||
|
*
|
||||||
|
* {Category} LCAG
|
||||||
|
*
|
||||||
|
* {param} String("milliSeconds") input: number of milliseconds to sleep
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static void sleep(long milliSeconds) {
|
||||||
|
try {
|
||||||
|
Thread.sleep(milliSeconds, 0);
|
||||||
|
} catch (Exception e) {
|
||||||
|
;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* incrementCounter: Create a new named counter in a global HashMap (usually
|
||||||
|
* globalMap) and initialize to 0. Increments the counter if it already exists.
|
||||||
|
*
|
||||||
|
* {talendTypes} int
|
||||||
|
*
|
||||||
|
* {Category} LCAG
|
||||||
|
*
|
||||||
|
* {param} Map("aMap") input: Map to use to store counter value (usually
|
||||||
|
* globalMap) {param} String("aCounter") input: Name of counter
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static int incrementCounter(Map<String, Object> aMap, String aCounter) {
|
||||||
|
Integer n = (Integer) aMap.getOrDefault(aCounter, 0);
|
||||||
|
|
||||||
|
n += 1;
|
||||||
|
if (n == 1)
|
||||||
|
log.debug("incrementCounter: Created new counter %s = %d", aCounter, n);
|
||||||
|
|
||||||
|
aMap.put(aCounter, n);
|
||||||
|
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* firstValue: Return the first parameter that is not null and not an empty
|
||||||
|
* String (or blanks-only).
|
||||||
|
*
|
||||||
|
* @param valueList variable number of parameter, all of them must have the same
|
||||||
|
* type
|
||||||
|
* @return first parameter that is not null (and for String not empty) or null
|
||||||
|
* if all parameters are null
|
||||||
|
*
|
||||||
|
* {talendTypes} Object
|
||||||
|
*
|
||||||
|
* {Category} LCAG
|
||||||
|
*
|
||||||
|
* {param} Object("valueList") valueList...: variable number of
|
||||||
|
* parameter, all of them must have the same type
|
||||||
|
*
|
||||||
|
* {examples} Assume String variables s1 = null, s2 = "", s3 = " ", s4 =
|
||||||
|
* "Hi friends" ->> firstValue(s1, s2, s3, s4, "Hello World") returns
|
||||||
|
* "Hi friends" ->> firstValue(s1, "Hello World") returns "Hello World"
|
||||||
|
* ->> firstValue(s1, s2) returns null
|
||||||
|
*/
|
||||||
|
public static <T> T firstValue(T... valueList) {
|
||||||
|
T result = null;
|
||||||
|
|
||||||
|
// get the first non-null value into aValue
|
||||||
|
for (T obj : valueList) {
|
||||||
|
if (obj != null) {
|
||||||
|
if (obj instanceof String && obj.toString().trim().equals("")) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
result = obj;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* inSync: Used to check if a field in MDM is in-sync with another object. The
|
||||||
|
* second parameter is in-sync with the first one, if * first one is null
|
||||||
|
* (in-sync because it is safe to update that field) * or both are equal
|
||||||
|
*
|
||||||
|
* @param inMDM_Obj - first field to compare to (usually a MDM object field)
|
||||||
|
* @param withOtherObj - field to compare to first parameter
|
||||||
|
*
|
||||||
|
* @return true if first parameter is null or if both are equal (deeply checked)
|
||||||
|
*
|
||||||
|
* {talendTypes} Object
|
||||||
|
*
|
||||||
|
* {Category} LCAG
|
||||||
|
*
|
||||||
|
* {param} Object(inMDM_Obj) inMDM_Obj: first field to compare to
|
||||||
|
* (usually a MDM object field) {param} Object(withOtherObj)
|
||||||
|
* withOtherObj: field to compare to first parameter
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static boolean inSync(Object inMDM_Obj, Object withOtherObj) {
|
||||||
|
boolean result = (inMDM_Obj == null) || java.util.Objects.deepEquals(inMDM_Obj, withOtherObj);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* inSyncIgnoreCase: Used to check if a String field in MDM is in-sync with
|
||||||
|
* another. The second parameter is in-sync with the first one, if * first one
|
||||||
|
* is null (in-sync because it is safe to update that field) * or both are equal
|
||||||
|
* ignoring case
|
||||||
|
*
|
||||||
|
* @param inMDM_Obj - first String to compare to (usually a MDM object field)
|
||||||
|
* @param withOtherObj - String to compare to first parameter
|
||||||
|
*
|
||||||
|
* @return true if first parameter is null or if both are equal
|
||||||
|
*
|
||||||
|
* {talendTypes} Object
|
||||||
|
*
|
||||||
|
* {Category} LCAG
|
||||||
|
*
|
||||||
|
* {param} String(inMDM_Obj) inMDM_Obj: first String to compare to
|
||||||
|
* (usually a MDM object field) {param} String(withOtherObj)
|
||||||
|
* withOtherObj: String to compare to first parameterr
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static boolean inSyncIgnoreCase(String inMDM_Obj, String withOtherObj) {
|
||||||
|
boolean result = (inMDM_Obj == null) || inMDM_Obj.equalsIgnoreCase(withOtherObj);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int countChararcters(CharSequence s, String charList) {
|
||||||
|
int result = 0;
|
||||||
|
|
||||||
|
if (s == null || charList == null)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
int n = s.length();
|
||||||
|
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
CharSequence c = s.subSequence(i, i + 1);
|
||||||
|
|
||||||
|
if (charList.contains(c))
|
||||||
|
result++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if string s1 and s2 are equal except white spaces in between. The
|
||||||
|
* result string indicates how to modify s1 to get s2
|
||||||
|
*
|
||||||
|
* @param s1
|
||||||
|
* @param s2
|
||||||
|
* @return String s2 if both differ excluding spaces. If both are equal
|
||||||
|
* excluding spaces: Return s2 where spaces are replaced by ' ' (if
|
||||||
|
* space is in s1 and s2) or '+' (space must be added to s2) and '-'
|
||||||
|
* (space must be deleted from s2)
|
||||||
|
*/
|
||||||
|
private static String compareWithSpaces(String s1, String s2, boolean ignoreCase) {
|
||||||
|
String result = "";
|
||||||
|
CharSequence s1Temp;
|
||||||
|
CharSequence s2Temp;
|
||||||
|
|
||||||
|
s1Temp = ignoreCase ? s1.toLowerCase() : s1;
|
||||||
|
s2Temp = ignoreCase ? s2.toLowerCase() : s2;
|
||||||
|
|
||||||
|
String s1NoBlancs = s1Temp.toString().replaceAll("[ \t\n]", "");
|
||||||
|
String s2NoBlancs = s2Temp.toString().replaceAll("[ \t\n]", "");
|
||||||
|
|
||||||
|
if (!s1NoBlancs.equals(s2NoBlancs)) {
|
||||||
|
result = s2;
|
||||||
|
} else {
|
||||||
|
// both strings excluding spaces are equal
|
||||||
|
|
||||||
|
int n1 = s1.length();
|
||||||
|
int n2 = s2.length();
|
||||||
|
int nMin = Math.min(n1, n2);
|
||||||
|
|
||||||
|
int i = 0;
|
||||||
|
int j = 0;
|
||||||
|
|
||||||
|
while (i < nMin && j < nMin) {
|
||||||
|
char c1 = s1Temp.charAt(i);
|
||||||
|
char c2 = s2Temp.charAt(j);
|
||||||
|
|
||||||
|
if (c1 == c2) {
|
||||||
|
result += s1.charAt(i);
|
||||||
|
i++;
|
||||||
|
j++;
|
||||||
|
} else if (c1 == ' ' && c2 != ' ') {
|
||||||
|
result += '+';
|
||||||
|
i++;
|
||||||
|
} else if (c2 == ' ' && c1 != ' ') {
|
||||||
|
result += '-';
|
||||||
|
j++;
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* compareEqual: Used to check if two parameters are equal. Returns * second
|
||||||
|
* parameter as string if not equal to the first one * "_n/a_" is the second is
|
||||||
|
* null or empty * "_equal_" if both are equal (deeply checked) *
|
||||||
|
* "_equalExceptCase_" if both are Strings that are equal except upper/lower
|
||||||
|
* case differences
|
||||||
|
*
|
||||||
|
* @param aValue - first field to compare to (usually a MDM object field)
|
||||||
|
* @param withOtherObj - field to compare to first parameter
|
||||||
|
*
|
||||||
|
* @return see above
|
||||||
|
*
|
||||||
|
* {talendTypes} Object
|
||||||
|
*
|
||||||
|
* {Category} LCAG
|
||||||
|
*
|
||||||
|
* {param} Object(inMDM_Obj) aValue: first field to compare to (usually
|
||||||
|
* a MDM object field) {param} Object(withOtherObj) withOtherObj: field
|
||||||
|
* to compare to first parameter
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static String compareEqual(Object aValue, Object withOtherObj) {
|
||||||
|
String result = COMPARE_ERROR;
|
||||||
|
SimpleDateFormat df = LcagDateTime.MDM_DATE_FORMAT;
|
||||||
|
|
||||||
|
String s1 = (aValue == null) ? "" : aValue.toString().trim();
|
||||||
|
String s2 = (withOtherObj == null) ? "" : withOtherObj.toString().trim();
|
||||||
|
|
||||||
|
// Convert dates to mdm standard format:
|
||||||
|
if (aValue != null && (aValue instanceof Date))
|
||||||
|
s1 = df.format((Date) aValue);
|
||||||
|
if (withOtherObj != null && (withOtherObj instanceof Date))
|
||||||
|
s2 = df.format((Date) withOtherObj);
|
||||||
|
|
||||||
|
if (!"".equals(s1) && "".equals(s2)) {
|
||||||
|
result = COMPARE_NOT_AVAILABLE;
|
||||||
|
} else if ("".equals(s1) && !"".equals(s2)) {
|
||||||
|
result = s2;
|
||||||
|
} else if (s1.equals(s2)) {
|
||||||
|
result = COMPARE_EQUALS;
|
||||||
|
} else if (s1.equalsIgnoreCase(s2)) {
|
||||||
|
result = COMPARE_EQUAL_EXCEPT_CASE;
|
||||||
|
} else {
|
||||||
|
result = s2;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* compareValue: Used to check if two parameters are equal. Returns * second
|
||||||
|
* parameter as string if not equal to the first one * "_n/a_" is the second is
|
||||||
|
* null or empty * "_equal_" if both are equal (deeply checked) *
|
||||||
|
* "_equalExceptCase_" if both are Strings that are equal except upper/lower
|
||||||
|
* case differences * "_startsWith_*" if the first string starts with the second
|
||||||
|
* * "_startsWith_*" if the first string starts with the second * "_contains_*"
|
||||||
|
* if the first string contains the second * "_partOf_*" if the second string
|
||||||
|
* contains the first
|
||||||
|
*
|
||||||
|
* @param aValue - first field to compare to (usually a MDM object field)
|
||||||
|
* @param withOtherObj - field to compare to first parameter
|
||||||
|
*
|
||||||
|
* @return see above
|
||||||
|
*
|
||||||
|
* {talendTypes} Object
|
||||||
|
*
|
||||||
|
* {Category} LCAG
|
||||||
|
*
|
||||||
|
* {param} Object(inMDM_Obj) aValue: first field to compare to (usually
|
||||||
|
* a MDM object field) {param} Object(withOtherObj) withOtherObj: field
|
||||||
|
* to compare to first parameter
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static String compareValue(Object aValue, Object withOtherObj) {
|
||||||
|
String result = COMPARE_ERROR;
|
||||||
|
SimpleDateFormat df = LcagDateTime.MDM_DATE_FORMAT;
|
||||||
|
|
||||||
|
String s1 = (aValue == null) ? "" : aValue.toString().trim();
|
||||||
|
String s2 = (withOtherObj == null) ? "" : withOtherObj.toString().trim();
|
||||||
|
|
||||||
|
// Convert dates to mdm standard format:
|
||||||
|
if (aValue != null && (aValue instanceof Date))
|
||||||
|
s1 = df.format((Date) aValue);
|
||||||
|
if (withOtherObj != null && (withOtherObj instanceof Date))
|
||||||
|
s2 = df.format((Date) withOtherObj);
|
||||||
|
|
||||||
|
if (!"".equals(s1) && "".equals(s2)) {
|
||||||
|
result = COMPARE_NOT_AVAILABLE;
|
||||||
|
} else if ("".equals(s1) && !"".equals(s2)) {
|
||||||
|
result = s2;
|
||||||
|
} else if (s1.equals(s2)) {
|
||||||
|
result = COMPARE_EQUALS;
|
||||||
|
} else if (s1.equalsIgnoreCase(s2)) {
|
||||||
|
result = COMPARE_EQUAL_EXCEPT_CASE;
|
||||||
|
} else {
|
||||||
|
result = s2;
|
||||||
|
|
||||||
|
if (s1.startsWith(s2)) {
|
||||||
|
result = COMPARE_STARTS_WITH + s2;
|
||||||
|
} else if (s1.endsWith(s2)) {
|
||||||
|
result = COMPARE_ENDS_WITH + s2;
|
||||||
|
} else if (s1.contains(s2)) {
|
||||||
|
result = COMPARE_CONTAINS + s2;
|
||||||
|
} else if (s2.contains(s1)) {
|
||||||
|
result = COMPARE_PART_OF + s2;
|
||||||
|
} else {
|
||||||
|
String correctionAdvise = LcagGeneral.compareWithSpaces(s1, s2, true);
|
||||||
|
|
||||||
|
if (correctionAdvise.equals(s2)) {
|
||||||
|
result = s2;
|
||||||
|
} else {
|
||||||
|
int n = countChararcters(s2, "+-");
|
||||||
|
int nc = countChararcters(correctionAdvise, "+-");
|
||||||
|
|
||||||
|
if (nc > n || correctionAdvise.length() < s2.length()) {
|
||||||
|
result = COMPARE_SPACES_DIFFER + correctionAdvise;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* inSyncByPolicy: Used to check if a String field in MDM is in-sync with
|
||||||
|
* another with respect to a given compare policy. The second parameter is
|
||||||
|
* in-sync with the first one for * enriching policy (policy starts with '+'):
|
||||||
|
* if inMDM_Obj is null, otherwise the following rules apply * "case-sensitive"
|
||||||
|
* policy: both objects are equal in all values * "ignore-case" policy: the
|
||||||
|
* string representation of both objects is equal disregarding case of letters *
|
||||||
|
* "Levenstein" policy: not yet supported, throws exception
|
||||||
|
*
|
||||||
|
* @param inMDM_Obj - first String to compare to (usually a MDM object field)
|
||||||
|
* @param withOtherObj - String to compare to first parameter
|
||||||
|
* @param policy - Compare policy to use
|
||||||
|
*
|
||||||
|
* @return true if first two parameter are in-sync with respect to the policy
|
||||||
|
* given.
|
||||||
|
*
|
||||||
|
* {talendTypes} boolean
|
||||||
|
*
|
||||||
|
* {Category} LCAG
|
||||||
|
*
|
||||||
|
* {param} String(inMDM_Obj) inMDM_Obj: first String to compare to
|
||||||
|
* (usually a MDM object field) {param} String(withOtherObj)
|
||||||
|
* withOtherObj: String to compare to first parameter {param}
|
||||||
|
* String("policy") input: compare policy to use (case-sensitive,
|
||||||
|
* ignore-case, Levenstein (not yet supported))
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static boolean inSyncByPolicy(Object inMDM_Obj, Object withOtherObj, String policy) {
|
||||||
|
if (policy == null)
|
||||||
|
throw new IllegalArgumentException("Error: policy is not set (null)");
|
||||||
|
if (inMDM_Obj == null && withOtherObj == null)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
// Check enriching rule: If policy starts with '+' withOtherObject can safely
|
||||||
|
// set the inMDM_Obj if that one is null
|
||||||
|
if ((inMDM_Obj == null || "".equals(inMDM_Obj)) && policy.startsWith("+"))
|
||||||
|
return true;
|
||||||
|
|
||||||
|
if (withOtherObj == null || inMDM_Obj == null)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
// after here, remove leading + from sync-rule
|
||||||
|
policy = policy.startsWith("+") ? policy.substring(1) : policy;
|
||||||
|
|
||||||
|
boolean result = false;
|
||||||
|
|
||||||
|
if (IN_SYNC_POLICY_CASE_SENSITIVE.equals(policy)) {
|
||||||
|
result = java.util.Objects.deepEquals(inMDM_Obj, withOtherObj);
|
||||||
|
} else if (IN_SYNC_POLICY_IGNORE_CASE.equals(policy)) {
|
||||||
|
String inMDM = inMDM_Obj.toString();
|
||||||
|
String withOther = withOtherObj.toString();
|
||||||
|
|
||||||
|
result = inMDM.equalsIgnoreCase(withOther);
|
||||||
|
} else {
|
||||||
|
throw new IllegalArgumentException(String.format("Error: policy '%s' is not supported", policy));
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,60 @@
|
|||||||
|
package routines;
|
||||||
|
|
||||||
|
import org.json.JSONException;
|
||||||
|
import org.json.JSONObject;
|
||||||
|
import org.json.XML;
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* user specification: the function's comment should contain keys as follows: 1. write about the function's comment.but
|
||||||
|
* it must be before the "{talendTypes}" key.
|
||||||
|
*
|
||||||
|
* 2. {talendTypes} 's value must be talend Type, it is required . its value should be one of: String, char | Character,
|
||||||
|
* long | Long, int | Integer, boolean | Boolean, byte | Byte, Date, double | Double, float | Float, Object, short |
|
||||||
|
* Short
|
||||||
|
*
|
||||||
|
* 3. {Category} define a category for the Function. it is required. its value is user-defined .
|
||||||
|
*
|
||||||
|
* 4. {param} 's format is: {param} <type>[(<default value or closed list values>)] <name>[ : <comment>]
|
||||||
|
*
|
||||||
|
* <type> 's value should be one of: string, int, list, double, object, boolean, long, char, date. <name>'s value is the
|
||||||
|
* Function's parameter name. the {param} is optional. so if you the Function without the parameters. the {param} don't
|
||||||
|
* added. you can have many parameters for the Function.
|
||||||
|
*
|
||||||
|
* 5. {example} gives a example for the Function. it is optional.
|
||||||
|
*/
|
||||||
|
public class LcagJSONTools {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* helloExample: not return value, only print "hello" + message.
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} User Defined
|
||||||
|
*
|
||||||
|
* {param} string("world") input: The string need to be printed.
|
||||||
|
*
|
||||||
|
* {example} helloExemple("world") # hello world !.
|
||||||
|
*/
|
||||||
|
|
||||||
|
public static String xmlToJson(String xml){
|
||||||
|
String json ="";
|
||||||
|
try {
|
||||||
|
JSONObject xmlJSONObj = XML.toJSONObject(xml);
|
||||||
|
json = xmlJSONObj.toString();
|
||||||
|
} catch (JSONException je) {
|
||||||
|
System.out.println(je.toString());
|
||||||
|
}
|
||||||
|
return json;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void helloExample(String message) {
|
||||||
|
if (message == null) {
|
||||||
|
message = "World"; //$NON-NLS-1$
|
||||||
|
}
|
||||||
|
System.out.println("Hello " + message + " !"); //$NON-NLS-1$ //$NON-NLS-2$
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
324
MDM-LCAG-JavaFunctions/src/main/java/routines/LcagLogger.java
Normal file
324
MDM-LCAG-JavaFunctions/src/main/java/routines/LcagLogger.java
Normal file
@@ -0,0 +1,324 @@
|
|||||||
|
package routines;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A wrapper for Log4J for simple use in tJava, tJavaRow components. It maps
|
||||||
|
* LCAG log levels to Apache Log levels so that all trace and debug message map
|
||||||
|
* to 'info' in Apache (other are mapped 1:1). This avoids messing up LCAG
|
||||||
|
* business log output with Talend internal og messages in all Talend jobs.
|
||||||
|
*
|
||||||
|
* Change History: V3.5.1: Full Redesign supporting LOG_LEVEL strings and
|
||||||
|
* restore
|
||||||
|
*
|
||||||
|
* @author U401455
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class LcagLogger {
|
||||||
|
public enum LOG_TYPE {
|
||||||
|
LOG_JOB, LOG_JAVA, LOG_ANY
|
||||||
|
}
|
||||||
|
|
||||||
|
private org.apache.logging.log4j.Logger loggerV2 = null; // to use log4j2
|
||||||
|
|
||||||
|
private LOG_TYPE logType = LOG_TYPE.LOG_JAVA;
|
||||||
|
private String classToLogFor = "unknown";
|
||||||
|
String loggerName = loggerV2 == null ? "doLog: Error: loggerV2 not initialized" : loggerV2.getName();
|
||||||
|
String jobName = "";
|
||||||
|
|
||||||
|
/* LOG Level, sequence taken over from apache commons logging */
|
||||||
|
public static final int LOG_TRACE = 2;
|
||||||
|
public static final int LOG_DEBUG = 1;
|
||||||
|
public static final int LOG_INFO = 0;
|
||||||
|
public static final int LOG_WARN = -1;
|
||||||
|
public static final int LOG_ERROR = -2;
|
||||||
|
public static final int LOG_FATAL = -3;
|
||||||
|
public static final int LOG_OFF = -99;
|
||||||
|
public static final int ILLEGAL_LEVEL = -999;
|
||||||
|
|
||||||
|
private static HashMap<Integer, String> int2Str = initInt2Str();
|
||||||
|
private static HashMap<String, Integer> str2Int = initStr2Int();
|
||||||
|
|
||||||
|
private static int minJavaLevel = LOG_DEBUG; // By default only log Java warnings or more critical
|
||||||
|
private static int minJobLevel = LOG_TRACE;
|
||||||
|
|
||||||
|
// backup values to use in restore LogLevel
|
||||||
|
private static int lastJavaLevel = minJavaLevel;
|
||||||
|
private static int lastJobLevel = minJobLevel;
|
||||||
|
|
||||||
|
private static final LcagLogger myLogger = getLogger(LcagLogger.class);
|
||||||
|
|
||||||
|
private LcagLogger(Object aObject) {
|
||||||
|
int suffixPosition = loggerName.lastIndexOf(".");
|
||||||
|
|
||||||
|
if (aObject instanceof org.apache.logging.log4j.Logger) {
|
||||||
|
loggerV2 = (org.apache.logging.log4j.Logger) aObject;
|
||||||
|
logType = LOG_TYPE.LOG_JOB;
|
||||||
|
classToLogFor = "";
|
||||||
|
jobName = loggerName.substring(suffixPosition + 1);
|
||||||
|
} else if (aObject instanceof Class<?>) {
|
||||||
|
Class<?> clazz = (Class<?>)aObject;
|
||||||
|
|
||||||
|
classToLogFor = clazz.getCanonicalName();
|
||||||
|
loggerV2 = org.apache.logging.log4j.LogManager.getLogger(clazz);
|
||||||
|
} else {
|
||||||
|
Class<?> clazz = aObject.getClass();
|
||||||
|
|
||||||
|
classToLogFor = clazz.getCanonicalName();
|
||||||
|
loggerV2 = org.apache.logging.log4j.LogManager.getLogger(clazz);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static HashMap<Integer, String> initInt2Str() {
|
||||||
|
HashMap<Integer, String> result = new HashMap<Integer, String>();
|
||||||
|
|
||||||
|
result.put(LOG_TRACE, "TRACE");
|
||||||
|
result.put(LOG_DEBUG, "DEBUG");
|
||||||
|
result.put(LOG_INFO, "INFO");
|
||||||
|
result.put(LOG_WARN, "WARN");
|
||||||
|
result.put(LOG_ERROR, "ERROR");
|
||||||
|
result.put(LOG_FATAL, "FATAL");
|
||||||
|
result.put(LOG_OFF, "OFF");
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static HashMap<String, Integer> initStr2Int() {
|
||||||
|
HashMap<String, Integer> result = new HashMap<String, Integer>();
|
||||||
|
|
||||||
|
result.put("TRACE", LOG_TRACE);
|
||||||
|
result.put("DEBUG", LOG_DEBUG);
|
||||||
|
result.put("INFO", LOG_INFO);
|
||||||
|
result.put("WARN", LOG_WARN);
|
||||||
|
result.put("ERROR", LOG_ERROR);
|
||||||
|
result.put("FATAL", LOG_FATAL);
|
||||||
|
result.put("OFF", LOG_OFF);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set Lcag Log level for Java function. Note: This function must be be named
|
||||||
|
* setLevel, as this has side effects with Job ContextBean injection
|
||||||
|
*
|
||||||
|
* @param aLevel - Minimum log level to set.
|
||||||
|
*/
|
||||||
|
private static void setLogLevel(LOG_TYPE aType, String aTypeName, Object aLevel) {
|
||||||
|
Integer intLevel = asLevel(aLevel);
|
||||||
|
|
||||||
|
if (intLevel == null) {
|
||||||
|
myLogger.error("setLogLevel for %s with aLevel=null called => ignored", aTypeName);
|
||||||
|
} else {
|
||||||
|
if (aType == LOG_TYPE.LOG_JAVA || aType == LOG_TYPE.LOG_ANY)
|
||||||
|
minJavaLevel = intLevel;
|
||||||
|
if (aType == LOG_TYPE.LOG_JOB || aType == LOG_TYPE.LOG_ANY)
|
||||||
|
minJobLevel = intLevel;
|
||||||
|
|
||||||
|
if (intLevel <= LOG_OFF) {
|
||||||
|
System.err.println(String.format("WARNING: LcagLogger %s LOG LEVEL DISABLED (!)", aTypeName));
|
||||||
|
} else {
|
||||||
|
System.out.println(
|
||||||
|
String.format("LcagLogger: Info: %s LOG LEVEL set to '%s'", aTypeName, asString(intLevel)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int getJavaLogLevel() {
|
||||||
|
return minJavaLevel;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set Lcag Log level for Java function. Note: This function must be be named
|
||||||
|
* setLevel, as this has side effects with Job ContextBean injection
|
||||||
|
*
|
||||||
|
* @param aLevel - Minimum log level to set.
|
||||||
|
*/
|
||||||
|
public static void setJavaLogLevel(Object aLevel) {
|
||||||
|
setLogLevel(LOG_TYPE.LOG_JAVA, "JAVA", aLevel);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int getJobLogLevel() {
|
||||||
|
return minJobLevel;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set Lcag Log level for jobs. Note: This function must be be named setLevel,
|
||||||
|
* as this has side effects with Job ContextBean injection
|
||||||
|
*
|
||||||
|
* @param aLevel - Minimum log level to set.
|
||||||
|
*/
|
||||||
|
public static void setJobLogLevel(Object aLevel) {
|
||||||
|
setLogLevel(LOG_TYPE.LOG_JOB, "JOB", aLevel);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set Lcag Log level for jobs or Java function or both. Note: This function
|
||||||
|
* must be be named setLevel, as this has side effects with Job ContextBean
|
||||||
|
* injection
|
||||||
|
*
|
||||||
|
* @param aLevel - Minimum log level to set.
|
||||||
|
*/
|
||||||
|
public static void setAllLogLevel(Object aLevel) {
|
||||||
|
setJavaLogLevel(aLevel);
|
||||||
|
setJobLogLevel(aLevel);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set Lcag Log level for jobs or Java function or both. Note: This function
|
||||||
|
* must be be named setLevel, as this has side effects with Job ContextBean
|
||||||
|
* injection
|
||||||
|
*
|
||||||
|
* @param aLogType - Either LOG_TYPE.LOG_JAVA or LOG_TYPE.LOG_JOB or
|
||||||
|
* LOG_TYPE.LOG_ANY for both
|
||||||
|
* @param aLevel - Minimum log level to set.
|
||||||
|
* @deprecated Use setJobLogLevel, setJavaLogLevel or setAllLogLevel instead
|
||||||
|
*/
|
||||||
|
public static void setLogLevel(LOG_TYPE aLogType, Object aLevel) {
|
||||||
|
|
||||||
|
if (aLogType == LOG_TYPE.LOG_JAVA) {
|
||||||
|
setJavaLogLevel(aLevel);
|
||||||
|
} else if (aLogType == LOG_TYPE.LOG_JOB) {
|
||||||
|
setJobLogLevel(aLevel);
|
||||||
|
} else if (aLogType == LOG_TYPE.LOG_ANY) {
|
||||||
|
setAllLogLevel(aLevel);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Restore loglevel to previous values and remember current values.
|
||||||
|
*/
|
||||||
|
public static void restoreLogLevels() {
|
||||||
|
int l = lastJavaLevel;
|
||||||
|
|
||||||
|
System.out.println(String.format("LcagLogger: Levels restored for Jobs set to %s, Java Code set to %s",
|
||||||
|
int2Str.getOrDefault(lastJobLevel, "OTHER"), int2Str.getOrDefault(lastJavaLevel, "OTHER")));
|
||||||
|
lastJavaLevel = minJavaLevel;
|
||||||
|
minJavaLevel = l;
|
||||||
|
|
||||||
|
l = lastJobLevel;
|
||||||
|
lastJobLevel = minJobLevel;
|
||||||
|
minJobLevel = l;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String asString(int aLogLevel) {
|
||||||
|
return int2Str.getOrDefault(aLogLevel, "OTHER");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Integer asLevel(Object aLogLevel) {
|
||||||
|
Integer result = null;
|
||||||
|
String errMsg = null;
|
||||||
|
|
||||||
|
if (aLogLevel != null) {
|
||||||
|
if (aLogLevel instanceof Integer) {
|
||||||
|
result = (Integer) aLogLevel;
|
||||||
|
} else if (aLogLevel instanceof String) {
|
||||||
|
String logLevelStr = ((String) aLogLevel).trim().toUpperCase();
|
||||||
|
|
||||||
|
if ("".equals(logLevelStr)) {
|
||||||
|
result = null;
|
||||||
|
} else if (logLevelStr.startsWith("LOG_")) {
|
||||||
|
logLevelStr = logLevelStr.substring(4);
|
||||||
|
}
|
||||||
|
|
||||||
|
result = str2Int.get(logLevelStr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String format(String fmt, Object... valueList) {
|
||||||
|
String result = String.format(fmt, valueList);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String doLog(int logLevel, String fmt, Object... valueList) {
|
||||||
|
String logText = format(fmt, valueList);
|
||||||
|
String logPrefix = "";
|
||||||
|
|
||||||
|
boolean doIt = (logType == LOG_TYPE.LOG_JOB && logLevel <= minJobLevel)
|
||||||
|
|| (logType == LOG_TYPE.LOG_JAVA && logLevel <= minJavaLevel);
|
||||||
|
|
||||||
|
if (!doIt)
|
||||||
|
return "";
|
||||||
|
|
||||||
|
logPrefix = String.format("%s: #%s#", classToLogFor, asString(logLevel));
|
||||||
|
|
||||||
|
switch (logLevel) {
|
||||||
|
case LOG_INFO:
|
||||||
|
case LOG_DEBUG:
|
||||||
|
case LOG_TRACE:
|
||||||
|
if (loggerV2 != null)
|
||||||
|
loggerV2.info(logPrefix + logText);
|
||||||
|
break;
|
||||||
|
case LOG_WARN:
|
||||||
|
if (loggerV2 != null)
|
||||||
|
loggerV2.warn(logPrefix + logText);
|
||||||
|
break;
|
||||||
|
case LOG_ERROR:
|
||||||
|
if (loggerV2 != null)
|
||||||
|
loggerV2.error(logPrefix + logText);
|
||||||
|
break;
|
||||||
|
case LOG_FATAL:
|
||||||
|
if (loggerV2 != null)
|
||||||
|
loggerV2.fatal(logPrefix + logText);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// FIXME: logger.* does not produce output! drop next line after this is fixed.
|
||||||
|
if (logLevel > LOG_ERROR) {
|
||||||
|
System.out.println(logPrefix + jobName + " - " + logText);
|
||||||
|
} else {
|
||||||
|
System.err.println(logPrefix + jobName + " - " + logText);
|
||||||
|
}
|
||||||
|
|
||||||
|
return logText;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String trace(String fmt, Object... valueList) {
|
||||||
|
String logText = doLog(LOG_TRACE, fmt, valueList);
|
||||||
|
|
||||||
|
return logText;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String debug(String fmt, Object... valueList) {
|
||||||
|
String logText = doLog(LOG_DEBUG, fmt, valueList);
|
||||||
|
|
||||||
|
return logText;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String info(String fmt, Object... valueList) {
|
||||||
|
String logText = doLog(LOG_INFO, fmt, valueList);
|
||||||
|
|
||||||
|
return logText;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String warn(String fmt, Object... valueList) {
|
||||||
|
String logText = doLog(LOG_WARN, fmt, valueList);
|
||||||
|
|
||||||
|
return logText;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String error(String fmt, Object... valueList) {
|
||||||
|
String logText = doLog(LOG_ERROR, fmt, valueList);
|
||||||
|
|
||||||
|
return logText;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String fatal(String fmt, Object... valueList) {
|
||||||
|
String logText = doLog(LOG_FATAL, fmt, valueList);
|
||||||
|
|
||||||
|
return logText;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static LcagLogger getLogger(Class aClass) {
|
||||||
|
return new LcagLogger(aClass);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static LcagLogger getLogger(Object aObject) {
|
||||||
|
return new LcagLogger(aObject);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,894 @@
|
|||||||
|
package routines;
|
||||||
|
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
import java.text.ParseException;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
|
||||||
|
/* LCAG String helper functions
|
||||||
|
*
|
||||||
|
* Change History:
|
||||||
|
* V3.5.0: Added new function isEmpty
|
||||||
|
*/
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
|
import java.util.LinkedHashSet;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Map.Entry;
|
||||||
|
import java.util.Properties;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.regex.MatchResult;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.apache.commons.text.StringEscapeUtils;
|
||||||
|
|
||||||
|
import com.jayway.jsonpath.JsonPath;
|
||||||
|
|
||||||
|
import de.lcag.common.CsvTable;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* user specification: the function's comment should contain keys as follows: 1. write about the function's comment.but
|
||||||
|
* it must be before the "{talendTypes}" key.
|
||||||
|
*
|
||||||
|
* 2. {talendTypes} 's value must be talend Type, it is required . its value should be one of: String, char | Character,
|
||||||
|
* long | Long, int | Integer, boolean | Boolean, byte | Byte, Date, double | Double, float | Float, Object, short |
|
||||||
|
* Short
|
||||||
|
*
|
||||||
|
* 3. {Category} define a category for the Function. it is required. its value is user-defined .
|
||||||
|
*
|
||||||
|
* 4. {param} 's format is: {param} <type>[(<default value or closed list values>)] <name>[ : <comment>]
|
||||||
|
*
|
||||||
|
* <type> 's value should be one of: string, int, list, double, object, boolean, long, char, date. <name>'s value is the
|
||||||
|
* Function's parameter name. the {param} is optional. so if you the Function without the parameters. the {param} don't
|
||||||
|
* added. you can have many parameters for the Function.
|
||||||
|
*
|
||||||
|
* 5. {example} gives a example for the Function. it is optional.
|
||||||
|
*/
|
||||||
|
public class LcagStringTools {
|
||||||
|
public enum StringType {
|
||||||
|
CSV("CSV"), HTML("HTML"), JSON("JSON"), XML("XML");
|
||||||
|
|
||||||
|
String typeName;
|
||||||
|
|
||||||
|
StringType(String pType) {
|
||||||
|
this.typeName = pType;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return this.typeName;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Use this to format output of "," seperated lists (split shall use first
|
||||||
|
// character and then ignore the rest)
|
||||||
|
public static final String COMMA_DELIMITER_FORMATTER = ", ";
|
||||||
|
// Use this to format output of ";" seperated lists (split shall use first
|
||||||
|
// character and then ignore the rest)
|
||||||
|
public static final String SEMI_COLON_DELIMITER_FORMATTER = ", ";
|
||||||
|
public static final String EMPTY_STRING = "";
|
||||||
|
|
||||||
|
private static LcagLogger log = LcagLogger.getLogger(LcagStringTools.class);
|
||||||
|
private static final Map<Pattern, StringType> CONTENT_DETECTOR_PATTERN_MAP = new LinkedHashMap<>();
|
||||||
|
|
||||||
|
static {
|
||||||
|
// Order items by most specific first! ALL for HTML also match XML!
|
||||||
|
CONTENT_DETECTOR_PATTERN_MAP.put(
|
||||||
|
Pattern.compile("<(b|br|h\\d|i|ul|li|p|pre|td|th)?>", Pattern.DOTALL | Pattern.MULTILINE),
|
||||||
|
StringType.HTML);
|
||||||
|
CONTENT_DETECTOR_PATTERN_MAP.put(Pattern.compile("</.*?>", Pattern.DOTALL | Pattern.MULTILINE), StringType.XML);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* isEmpty: Checks if a string representation of an object is null or some empty
|
||||||
|
* string
|
||||||
|
*
|
||||||
|
* {talendTypes} boolean
|
||||||
|
*
|
||||||
|
* {Category} LCAG-StringTools
|
||||||
|
*
|
||||||
|
* {param} Object("s") input: object to check
|
||||||
|
*/
|
||||||
|
public static boolean isEmpty(Object s) {
|
||||||
|
boolean result = false;
|
||||||
|
|
||||||
|
if (s == null) {
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
String asString = s.toString().trim();
|
||||||
|
|
||||||
|
result = EMPTY_STRING.equals(asString) || "(null)".equals(asString) || "null".equals(asString);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* isEqualTo: Compares a String to another object
|
||||||
|
*
|
||||||
|
* {talendTypes} boolean
|
||||||
|
*
|
||||||
|
* {Category} LCAG-StringTools
|
||||||
|
*
|
||||||
|
* {param} String("s") input: string to compare with {param} int("obj") input:
|
||||||
|
* Object to compare to
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static boolean isEqualTo(String s, Object obj) {
|
||||||
|
boolean result = false;
|
||||||
|
|
||||||
|
if (s == null && obj == null) {
|
||||||
|
return true;
|
||||||
|
} else if (s == null || obj == null) {
|
||||||
|
return false;
|
||||||
|
} else if (obj instanceof String || obj instanceof Integer || obj instanceof Long || obj instanceof Boolean) {
|
||||||
|
String s2 = obj.toString();
|
||||||
|
|
||||||
|
result = s.equals(s2);
|
||||||
|
} else {
|
||||||
|
// Float, Double cause rounding problems, Date format problems when using
|
||||||
|
// toString()
|
||||||
|
throw new IllegalArgumentException(
|
||||||
|
String.format("Comparison type '%s'not yet supported", obj.getClass().getName()));
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* isEqualToIgnoreCase: Compares a String to another object
|
||||||
|
*
|
||||||
|
* {talendTypes} boolean
|
||||||
|
*
|
||||||
|
* {Category} LCAG-StringTools
|
||||||
|
*
|
||||||
|
* {param} String("s") input: string to compare with {param} int("obj") input:
|
||||||
|
* Object to compare to
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static boolean isEqualToIgnoreCase(String s, Object obj) {
|
||||||
|
boolean result = false;
|
||||||
|
|
||||||
|
if (s == null && obj == null) {
|
||||||
|
return true;
|
||||||
|
} else if (s == null || obj == null) {
|
||||||
|
return false;
|
||||||
|
} else if (obj instanceof String || obj instanceof Integer || obj instanceof Long || obj instanceof Boolean) {
|
||||||
|
String s2 = obj.toString();
|
||||||
|
|
||||||
|
result = s.equalsIgnoreCase(s2);
|
||||||
|
} else {
|
||||||
|
// Float, Double cause rounding problems, Date format problems when using
|
||||||
|
// toString()
|
||||||
|
throw new IllegalArgumentException(
|
||||||
|
String.format("Comparison type '%s'not yet supported", obj.getClass().getName()));
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert an object to a non null String
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} LCAG-StringTools
|
||||||
|
*
|
||||||
|
* {param} Object anObj : input: Object to turn into String {returns} String
|
||||||
|
* representation using toString or empty string {example} toString(null) = "",
|
||||||
|
* toString(new Double(9.9)) = "9.9"
|
||||||
|
*/
|
||||||
|
public static String toString(Object anObj) {
|
||||||
|
String result = anObj == null ? EMPTY_STRING : anObj.toString();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert a CSV string into a Java String List
|
||||||
|
*
|
||||||
|
* {talendTypes} List<String>
|
||||||
|
*
|
||||||
|
* {Category} LCAG-StringTools
|
||||||
|
*
|
||||||
|
* {param} String csv : input: text to split into a list {param} String
|
||||||
|
* regExSeparator : input: Character or regular exression string to use for
|
||||||
|
* splitting {returns} List of strings {example} assembleMDS_DateTime(aDate,
|
||||||
|
* "23:59") returns next day 00:00 of aDate
|
||||||
|
*/
|
||||||
|
public static List<String> asList(String csv, String regExSeparator) {
|
||||||
|
List<String> result = new ArrayList<>();
|
||||||
|
|
||||||
|
if (csv != null) {
|
||||||
|
List<String> aList = Arrays.asList(csv.split(regExSeparator));
|
||||||
|
result.addAll(aList);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String asHtml(String tagName, String text) {
|
||||||
|
return String.format("<%s>%s</%s>", tagName, StringEscapeUtils.escapeHtml4(text), tagName);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Joins an object as a result String. Usually the parameter is a List<>. The
|
||||||
|
* functions joins multiple elements using the delimiter passed. It replaces
|
||||||
|
* null values by the nullString argument unless it is null itself. If
|
||||||
|
* nullString argument is null the function skips all null values from the
|
||||||
|
* stringList argument
|
||||||
|
*
|
||||||
|
* @param delimiter - separstor of strings to use
|
||||||
|
* @param nullString - replacement of null values (if null skips null values)
|
||||||
|
* @param stringList - any object or List<>. Elements are converted to String
|
||||||
|
* using toString()
|
||||||
|
* @return - String joined by delimiter from all arguments.
|
||||||
|
*/
|
||||||
|
private static String join(String delimiter, String nullString, Object stringList) {
|
||||||
|
List<Object> objList = new ArrayList<Object>();
|
||||||
|
List<String> txtList = new ArrayList<String>();
|
||||||
|
|
||||||
|
if (stringList instanceof Collection) {
|
||||||
|
objList = new java.util.ArrayList<>((Collection<?>) stringList);
|
||||||
|
} else {
|
||||||
|
objList.add(stringList);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (Object o : objList) {
|
||||||
|
if (o != null) {
|
||||||
|
txtList.add(o.toString());
|
||||||
|
} else if (nullString != null) {
|
||||||
|
txtList.add(nullString);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
String result = String.join(delimiter, txtList);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Joins an object as a result String. Usually the parameter is a List<>. The
|
||||||
|
* functions joins multiple elements using the delimiter passed. It skips null
|
||||||
|
* values!
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} LCAG-StringTools
|
||||||
|
*
|
||||||
|
* {param} String stringList : input: List of Objects to join {returns} All
|
||||||
|
* elements joined by delimiter. {example} join( "/", { "a", null, "b" } ) =
|
||||||
|
* "a/b"
|
||||||
|
*/
|
||||||
|
public static String joinIgnoreNull(String delimiter, Object stringList) {
|
||||||
|
return join(delimiter, null, stringList);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Joins an object as a result String. Usually the parameter is a List<>. The
|
||||||
|
* functions joins multiple elements using the delimiter passed. Uses nullString
|
||||||
|
* argument to represent null values to keep same number of elements.
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} LCAG-StringTools
|
||||||
|
*
|
||||||
|
* {param} String stringList : input: List of Objects to join {returns} All
|
||||||
|
* elements joined by delimiter. {example} join( "; ", "n/a", { "a", null, "b" }
|
||||||
|
* ) = "a; n/a; b"
|
||||||
|
*/
|
||||||
|
public static String joinAll(String delimiter, String nullString, Object stringList) {
|
||||||
|
return join(delimiter, nullString, stringList);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Joins all arguments as a result string using the delimiter to seperate the
|
||||||
|
* arguments. Ignores null values in the list.
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} LCAG-StringTools
|
||||||
|
*
|
||||||
|
* {param} String stringList : input: List of Objects to join {returns} All
|
||||||
|
* elements joined by delimiter. {example} join(", ", "a", "b", "c") = "a, b, c
|
||||||
|
* join( "; ", "a", 1, 1.2, null, true ) = "a; 1; 1.2; true"
|
||||||
|
*/
|
||||||
|
public static String joinIgnoreNull(String delimiter, Object... stringList) {
|
||||||
|
if (stringList == null) {
|
||||||
|
return EMPTY_STRING;
|
||||||
|
}
|
||||||
|
|
||||||
|
List<Object> aList = Arrays.asList(stringList);
|
||||||
|
|
||||||
|
String result = join(delimiter, null, aList);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Joins all arguments as a result string using the delimiter to separate the
|
||||||
|
* arguments. Uses nullString argument to represent null values to keep same
|
||||||
|
* number of elements.
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} LCAG-StringTools
|
||||||
|
*
|
||||||
|
* {param} String stringList : input: List of Objects to join {returns} All
|
||||||
|
* elements joined by delimiter. {example} join(", ", "n/a", "a", "b", "c") =
|
||||||
|
* "a, b, c join( "; ", "n/a", "a", 1, 1.2, null, true ) = "a; 1; 1.2; n/a;
|
||||||
|
* true"
|
||||||
|
*/
|
||||||
|
public static String joinAll(String delimiter, String nullString, Object... stringList) {
|
||||||
|
if (stringList == null) {
|
||||||
|
return nullString;
|
||||||
|
}
|
||||||
|
|
||||||
|
List<Object> aList = Arrays.asList(stringList);
|
||||||
|
|
||||||
|
String result = join(delimiter, nullString, aList);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* fillBlanksUpcase: add blanks to given Strings for a fixed length
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} LCAG-StringTools
|
||||||
|
*
|
||||||
|
* {param} String("s") input: string to fill with blanks {param} int("n") input:
|
||||||
|
* max number of blanks to add
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static String fillBlanksUpcase(String s, int n) {
|
||||||
|
if (s == null)
|
||||||
|
return null;
|
||||||
|
else
|
||||||
|
return String.format("%1$-" + n + "s", s.toUpperCase());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* splitString: add blanks to given Strings for a fixed length
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} LCAG-StringTools
|
||||||
|
*
|
||||||
|
* {param} String("s") input: string to tokenize {param} String("delim") input:
|
||||||
|
* delimiter string {param} int("n") input: token indicator/counter starting
|
||||||
|
* with 0
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static String splitString(String s, String delim, int n) {
|
||||||
|
if (s == null)
|
||||||
|
return null;
|
||||||
|
else {
|
||||||
|
/* s != null */
|
||||||
|
String result = null;
|
||||||
|
String[] tokens = s.split(delim);
|
||||||
|
if (tokens != null && tokens.length > n)
|
||||||
|
result = tokens[n];
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* selectCaseSensitiveIfEqual: In case both String parameters are equal ignoring
|
||||||
|
* case return the one that is case-sensitive. In all other cases always return
|
||||||
|
* the first parameter that is not null or empty.
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} LCAG-StringTools
|
||||||
|
*
|
||||||
|
* {param} String("s1") input: first string to evaluate {param} String("s2")
|
||||||
|
* input: second string to evaluate
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static String selectCaseSensitiveIfEqual(String s1, String s2) {
|
||||||
|
if (s1 == null)
|
||||||
|
return s2;
|
||||||
|
if (s2 == null)
|
||||||
|
return s1;
|
||||||
|
|
||||||
|
s1 = s1.trim();
|
||||||
|
s2 = s2.trim();
|
||||||
|
|
||||||
|
String result = s1;
|
||||||
|
|
||||||
|
int n1 = s1.length();
|
||||||
|
int n2 = s2.length();
|
||||||
|
|
||||||
|
if (n1 == n2 && n1 > 1 && s1.equalsIgnoreCase(s2) && Character.isUpperCase(s1.charAt(1))
|
||||||
|
&& Character.isLowerCase(s2.charAt(1))) {
|
||||||
|
|
||||||
|
// both are equal ignoring case but s2 is likely case-sensitive whereas as s1 is
|
||||||
|
// not. So use s2:
|
||||||
|
result = s2;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* selectBestOf: Selects the String with the most available information
|
||||||
|
* according to the rules: 1) if first parameter is null then return the second.
|
||||||
|
* 2) if one string start with the other return the longer string (after
|
||||||
|
* trimming) 3) if both are of same length but differ in content ignoring case,
|
||||||
|
* return the first one 4) if both are equal when ignoring case, return the one
|
||||||
|
* that also contains lower-case letters (tested by 2nd char)
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} LCAG-StringTools
|
||||||
|
*
|
||||||
|
* {param} String("s1") input: first string to evaluate {param} String("s2")
|
||||||
|
* input: second string to evaluate
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static String selectBestOf(String s1, String s2) {
|
||||||
|
if (s1 == null)
|
||||||
|
return s2;
|
||||||
|
if (s2 == null)
|
||||||
|
return s1;
|
||||||
|
|
||||||
|
s1 = s1.trim();
|
||||||
|
s2 = s2.trim();
|
||||||
|
|
||||||
|
String result = s1;
|
||||||
|
String s1CaseIgnored = s1.toLowerCase();
|
||||||
|
String s2CaseIgnored = s2.toLowerCase();
|
||||||
|
|
||||||
|
int n1 = s1.length();
|
||||||
|
int n2 = s2.length();
|
||||||
|
|
||||||
|
if (n2 > n1 && s2CaseIgnored.startsWith(s1CaseIgnored)) {
|
||||||
|
result = s2;
|
||||||
|
} else if (n1 == n2 && n1 > 1 && s1.equalsIgnoreCase(s2) && Character.isUpperCase(s1.charAt(1))
|
||||||
|
&& Character.isLowerCase(s2.charAt(1))) {
|
||||||
|
|
||||||
|
// both are equal ignoring case but s2 is likely case-sensitive whereas as s1 is
|
||||||
|
// not. So use s2:
|
||||||
|
result = s2;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* applyChangeRule: applies the given change rule to the first two parameters
|
||||||
|
* and returs the corresponding result. Rules: *) in general: if the first
|
||||||
|
* string s1 is null then apply the rule to the second string s2 1)
|
||||||
|
* "to-upper-case": 2) "to-lower-case": 3) "prefer case-sensitive": if both
|
||||||
|
* string are equal except case, use the one that is case-sensitive 4) "select
|
||||||
|
* best-of"; if one string start with the other return the longer string (after
|
||||||
|
* trimming) 5) null:
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} LCAG-StringTools
|
||||||
|
*
|
||||||
|
* {param} String("s1") input: first string to evaluate {param} String("s2")
|
||||||
|
* input: second string to evaluate {param} String("changeRule") input: rule
|
||||||
|
* used to change
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static String applyChangeRule(String s1, String s2, String changeRule) {
|
||||||
|
String result;
|
||||||
|
|
||||||
|
if (s1 == null) {
|
||||||
|
s1 = s2;
|
||||||
|
}
|
||||||
|
|
||||||
|
result = s1;
|
||||||
|
|
||||||
|
if (changeRule == null) {
|
||||||
|
|
||||||
|
} else if ("prefer case-sensitive".equals(changeRule)) {
|
||||||
|
result = selectCaseSensitiveIfEqual(s1, s2);
|
||||||
|
} else if ("to-upper-case".equals(changeRule)) {
|
||||||
|
result = s1.toUpperCase();
|
||||||
|
} else if ("to-lower-case".equals(changeRule)) {
|
||||||
|
result = s1.toLowerCase();
|
||||||
|
} else if ("select best-of".equals(changeRule)) {
|
||||||
|
result = selectBestOf(s1, s2);
|
||||||
|
}
|
||||||
|
|
||||||
|
log.debug(String.format("applyChangeRule(%s, %s, %s) = %s", s1, s2, changeRule, result));
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getString(String text) {
|
||||||
|
String result = text;
|
||||||
|
// Check if the argument is a valid path to a file:
|
||||||
|
try {
|
||||||
|
Path path = Paths.get(text);
|
||||||
|
|
||||||
|
if (Files.isReadable(path)) {
|
||||||
|
// Yes, it is a file, use its content instead:
|
||||||
|
result = new String(Files.readAllBytes(path));
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static StringType getStringType(String text) {
|
||||||
|
StringType result = null;
|
||||||
|
|
||||||
|
text = text.strip();
|
||||||
|
|
||||||
|
int n = text.indexOf("\n");
|
||||||
|
String firstLine = n < 0 ? text : text.substring(0, n).trim();
|
||||||
|
|
||||||
|
if (firstLine.startsWith("<?xml")) {
|
||||||
|
result = StringType.XML;
|
||||||
|
} else if (text.startsWith("{") && text.endsWith("}")) {
|
||||||
|
result = StringType.JSON;
|
||||||
|
} else if (StringUtils.countMatches(firstLine, ';') > 0 && StringUtils.countMatches(text, '\n') > 1) {
|
||||||
|
result = StringType.CSV;
|
||||||
|
} else if (firstLine.startsWith("<!DOCTYPE html>") || firstLine.startsWith("<html>")) {
|
||||||
|
result = StringType.HTML;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (result == null) {
|
||||||
|
// Count tags in the text to find out the correct type
|
||||||
|
final int MIN_MATCHES_TO_DETECT = 2;
|
||||||
|
// For performance only count in 1K of text
|
||||||
|
final int MAX_TEXT_SCAN_SIZE = 1024;
|
||||||
|
|
||||||
|
int textLength = text.length();
|
||||||
|
if (textLength > MAX_TEXT_SCAN_SIZE)
|
||||||
|
text = text.substring(0, MAX_TEXT_SCAN_SIZE);
|
||||||
|
|
||||||
|
for (Pattern detector : CONTENT_DETECTOR_PATTERN_MAP.keySet()) {
|
||||||
|
Stream<MatchResult> results = detector.matcher(text).results();
|
||||||
|
long noMatches = results.count();
|
||||||
|
|
||||||
|
if (noMatches >= MIN_MATCHES_TO_DETECT) {
|
||||||
|
result = CONTENT_DETECTOR_PATTERN_MAP.get(detector);
|
||||||
|
|
||||||
|
log.debug("getStringType: text '%s...' detected as '%s' by pattern analysis", firstLine, result);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
log.debug("getStringType: text '%s...' detected as '%s' by start tag analysis", firstLine, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* resolveVariables: Replaces all variables ${VAR} in the txt argument by the
|
||||||
|
* value defined in the map. If the variable is not defined the function will
|
||||||
|
* not change this placeholder.
|
||||||
|
*
|
||||||
|
* Example: resolveVariables("This is ${MDM_Cargo_MDM_DataContainer}",
|
||||||
|
* globalMap) = "This is MDM_4_DEV_4_TST1_REG"
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} LCAG-String
|
||||||
|
*
|
||||||
|
* {param} String("txt") input: string to substitutes {param} Map("map") input:
|
||||||
|
* Map of strings to replace
|
||||||
|
*/
|
||||||
|
public static String resolveVariables(String txt, Map<? extends Object, ? extends Object> map) {
|
||||||
|
String result = EMPTY_STRING;
|
||||||
|
|
||||||
|
if (txt == null || map == null)
|
||||||
|
return txt;
|
||||||
|
|
||||||
|
// handle all allowed bracket pairs that may enclose a variabe name:
|
||||||
|
for (String bracket : new String[] { "{}", "()" }) {
|
||||||
|
char openBracket = bracket.charAt(0);
|
||||||
|
char closeBracket = bracket.charAt(1);
|
||||||
|
|
||||||
|
// handle all variables found:
|
||||||
|
result = _tryResolvingUsingBracketType(txt, map, openBracket, closeBracket);
|
||||||
|
|
||||||
|
// Only support one bracket type:
|
||||||
|
if (!EMPTY_STRING.equals(result))
|
||||||
|
break;
|
||||||
|
|
||||||
|
} // end of bracket type handling
|
||||||
|
|
||||||
|
if (EMPTY_STRING.equals(result))
|
||||||
|
result = txt;
|
||||||
|
|
||||||
|
return result;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String _tryResolvingUsingBracketType(String pText,
|
||||||
|
Map<? extends Object, ? extends Object> pVariableMap, char pOpenBracket, char pCloseBracket) {
|
||||||
|
|
||||||
|
String result = EMPTY_STRING;
|
||||||
|
String[] varList = pText.split("\\$\\" + pOpenBracket); // split txt by ${ or $(
|
||||||
|
|
||||||
|
if (varList.length == 1) {
|
||||||
|
// no bracket found, return incomming text
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (String s : varList) {
|
||||||
|
Object value = null;
|
||||||
|
|
||||||
|
if (EMPTY_STRING.equals(s))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
int n = s.indexOf(pCloseBracket);
|
||||||
|
|
||||||
|
if (n > 0) {
|
||||||
|
// Var found to replace
|
||||||
|
String key = s.substring(0, n);
|
||||||
|
String format = null;
|
||||||
|
String path = null; // for Json or xpath: /-separated list of attributes
|
||||||
|
|
||||||
|
int formatIndex = key.indexOf("#");
|
||||||
|
int pathIndex = key.indexOf("/");
|
||||||
|
|
||||||
|
if (formatIndex > 0) {
|
||||||
|
// There is a formatting instruction after the variable name => get name and
|
||||||
|
// format:
|
||||||
|
format = key.substring(formatIndex + 1);
|
||||||
|
key = key.substring(0, formatIndex);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pathIndex > 0) {
|
||||||
|
path = key.substring(pathIndex + 1);
|
||||||
|
key = key.substring(0, pathIndex);
|
||||||
|
}
|
||||||
|
|
||||||
|
value = pVariableMap.get(key);
|
||||||
|
|
||||||
|
// handle reserved keys:
|
||||||
|
if ("now".equals(key)) { // current date/time
|
||||||
|
value = new Date();
|
||||||
|
} else if ("today".equals(key)) {
|
||||||
|
value = new Date();
|
||||||
|
format = LcagDateTime.MDM_DATE_FORMAT_STR;
|
||||||
|
} else if ("tomorrow".equals(key)) {
|
||||||
|
value = LcagDateTime.addDays(new Date(), 1);
|
||||||
|
format = LcagDateTime.MDM_DATE_FORMAT_STR;
|
||||||
|
} else if ("yesterday".equals(key)) {
|
||||||
|
value = LcagDateTime.addDays(new Date(), -1);
|
||||||
|
format = LcagDateTime.MDM_DATE_FORMAT_STR;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (value != null) {
|
||||||
|
if (value instanceof Date) {
|
||||||
|
if (format == null) {
|
||||||
|
value = LcagDateTime.DATETIME_FORMAT_ISO8601_UTC_FILE.format((Date) value);
|
||||||
|
} else if ("milliseconds".equals(format)) {
|
||||||
|
Long v = ((Date) value).getTime();
|
||||||
|
|
||||||
|
value = v;
|
||||||
|
} else {
|
||||||
|
SimpleDateFormat f = new SimpleDateFormat(format);
|
||||||
|
|
||||||
|
value = f.format((Date) value);
|
||||||
|
}
|
||||||
|
} else if (value instanceof String) {
|
||||||
|
String stringValue = (String) value;
|
||||||
|
String text = getString(stringValue);
|
||||||
|
StringType textType = getStringType(text);
|
||||||
|
|
||||||
|
if (textType == StringType.JSON && pathIndex >= 0) {
|
||||||
|
String jsonPath = "." + path.replace('/', '.');
|
||||||
|
|
||||||
|
List<Object> itemsFound = JsonPath.parse((String) value).read(jsonPath);
|
||||||
|
// List<> uniqueResultList = new ArrayList<>();
|
||||||
|
|
||||||
|
if (itemsFound != null) {
|
||||||
|
Set<Object> uniqueValues = new LinkedHashSet<Object>(itemsFound);
|
||||||
|
|
||||||
|
text = joinIgnoreNull(COMMA_DELIMITER_FORMATTER, uniqueValues);
|
||||||
|
} else {
|
||||||
|
text = EMPTY_STRING;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (StringType.HTML.toString().equals(format)) {
|
||||||
|
String htmlText = EMPTY_STRING;
|
||||||
|
|
||||||
|
if (textType == StringType.CSV) {
|
||||||
|
CsvTable table = new CsvTable();
|
||||||
|
|
||||||
|
table.read(text);
|
||||||
|
htmlText = table.asHTML();
|
||||||
|
} else if (textType == StringType.HTML) {
|
||||||
|
htmlText = String.format(
|
||||||
|
"<b>ERROR: Converting %s tables into HTML not yet supported !!</b>", textType);
|
||||||
|
} else {
|
||||||
|
htmlText = StringEscapeUtils.escapeHtml4(text);
|
||||||
|
}
|
||||||
|
text = htmlText;
|
||||||
|
}
|
||||||
|
|
||||||
|
value = text;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// Replace env var by value
|
||||||
|
s = value.toString() + s.substring(n + 1);
|
||||||
|
} else { // value == null
|
||||||
|
s = "$" + pOpenBracket + s;
|
||||||
|
}
|
||||||
|
} // end of n > 0 (variable handling)
|
||||||
|
|
||||||
|
result += s;
|
||||||
|
} // end of bracket pair handling
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* resolveVariables: Replaces all standard variables in the txt argument by the
|
||||||
|
* values.
|
||||||
|
*
|
||||||
|
* Supports the following standard variables: ${now}, ${today}, ${tomorrow},
|
||||||
|
* ${yesterday}
|
||||||
|
*
|
||||||
|
* Examples: (if now is June, 23rd 2023 at 11:45:00 UTC): resolveVariables("This
|
||||||
|
* is ${today}") = "This is 2023-06-23" resolveVariables("This is ${tomorrow}")
|
||||||
|
* = "This is 2023-06-24" resolveVariables("This is ${now}") = "This is
|
||||||
|
* 2023-06-23T11-45-00Z" resolveVariables("This is ${now#HH:mm}") = "This is
|
||||||
|
* 11:45"
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} LCAG-String
|
||||||
|
*
|
||||||
|
* {param} String("txt") input: string to substitutes
|
||||||
|
*/
|
||||||
|
public static String resolveVariables(String txt) {
|
||||||
|
return resolveVariables(txt, new HashMap<String, String>());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* resolveVariables: Replaces all variables ${VAR} in the txt argument by the
|
||||||
|
* value defined in the map. If the variable is not defined the function will
|
||||||
|
* not change this placeholder.
|
||||||
|
*
|
||||||
|
* Example: resolveVariables("This is ${MDM_Cargo_MDM_DataContainer}", context)
|
||||||
|
* = "This is MDM_4_DEV_4_TST1_REG"
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} LCAG-String
|
||||||
|
*
|
||||||
|
* {param} String("txt") input: string to substitutes {param}
|
||||||
|
* HashTable("context") input: Talend Context in a job
|
||||||
|
*/
|
||||||
|
public static String resolveVariables(String txt, Properties context) {
|
||||||
|
Map<Object, Object> aMap = new HashMap<Object, Object>();
|
||||||
|
|
||||||
|
String result = EMPTY_STRING;
|
||||||
|
if (txt == null || context == null)
|
||||||
|
return txt;
|
||||||
|
|
||||||
|
for (Entry<Object, Object> e : context.entrySet()) {
|
||||||
|
aMap.put(e.getKey(), e.getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
result = resolveVariables(txt, aMap);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* resolveEnvironmentVariables: Replaces all environment variables ${VAR} by its
|
||||||
|
* value. If the variable is not defined the function will not change this
|
||||||
|
* placeholder.
|
||||||
|
*
|
||||||
|
* Example: resolveEnvironmentVariables("This is ${OS}") = "This is Windows_NT"
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} LCAG-StringTools
|
||||||
|
*
|
||||||
|
* {param} String("txt") input: string to substitutes
|
||||||
|
*/
|
||||||
|
public static String resolveEnvironmentVariables(String txt) {
|
||||||
|
String result = EMPTY_STRING;
|
||||||
|
String[] varList = txt.split("\\$\\{"); // split txt by ${
|
||||||
|
|
||||||
|
for (String s : varList) {
|
||||||
|
int n = s.indexOf("}");
|
||||||
|
|
||||||
|
if (n > 0) {
|
||||||
|
// Env var found to replace
|
||||||
|
String key = s.substring(0, n);
|
||||||
|
String value = System.getenv(key);
|
||||||
|
|
||||||
|
if (value != null) {
|
||||||
|
// Replace env var by value
|
||||||
|
s = value + s.substring(n + 1);
|
||||||
|
} else {
|
||||||
|
s = "${" + s;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
result += s;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static List<String> splitByCamelCase(String s) {
|
||||||
|
List<String> splitted = new ArrayList<String>(Arrays.asList(StringUtils.splitByCharacterTypeCamelCase(s)));
|
||||||
|
|
||||||
|
splitted.removeAll(Collections.singleton("_"));
|
||||||
|
|
||||||
|
return splitted;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> T convertToObject(String pType, String pStringValue, String pDateFormat) throws ParseException {
|
||||||
|
T result = null;
|
||||||
|
log.debug("LcagStringTools.convertToObject(type %s, value %s, format %s) starts", pType, pStringValue,
|
||||||
|
pDateFormat);
|
||||||
|
|
||||||
|
if (pStringValue == null || pStringValue.trim().equals(EMPTY_STRING)) {
|
||||||
|
result = null;
|
||||||
|
} else {
|
||||||
|
if ("String".equals(pType)) {
|
||||||
|
result = (T) pStringValue;
|
||||||
|
} else if ("Date".equals(pType)) {
|
||||||
|
if (pDateFormat != null) {
|
||||||
|
result = (T) LcagDateTime.toDate(pStringValue, pDateFormat);
|
||||||
|
} else {
|
||||||
|
result = (T) LcagDateTime.MDM_DATE_FORMAT.parse(pStringValue);
|
||||||
|
}
|
||||||
|
} else if ("Timestamp".equals(pType)) {
|
||||||
|
result = (T) LcagDateTime.MDM_DATETIME_FORMAT.parse(pStringValue);
|
||||||
|
} else if ("Integer".equals(pType)) {
|
||||||
|
Integer v = Integer.parseInt(pStringValue);
|
||||||
|
result = (T) v;
|
||||||
|
} else if ("Decimal".equals(pType)) {
|
||||||
|
pStringValue = pStringValue.replace(',', '.');
|
||||||
|
Double v = Double.parseDouble(pStringValue);
|
||||||
|
result = (T) BigDecimal.valueOf(v);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Map<String, Object> asMap(String fieldMapping, String pPairSeperator, String pKeyValueSeperator) {
|
||||||
|
// Create the HashMap to store the result
|
||||||
|
Map<String, Object> map = new HashMap<>();
|
||||||
|
|
||||||
|
// Split the string by ';' to get each key-value pair
|
||||||
|
String[] pairs = fieldMapping.split(pPairSeperator);
|
||||||
|
|
||||||
|
for (String pair : pairs) {
|
||||||
|
// Split each pair
|
||||||
|
String[] keyValue = pair.split(pKeyValueSeperator);
|
||||||
|
if (keyValue.length == 2) {
|
||||||
|
// Trim to remove any whitespace
|
||||||
|
map.put(keyValue[0].trim(), keyValue[1].trim());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
}
|
||||||
117
MDM-LCAG-JavaFunctions/src/main/java/routines/LcagTestTools.java
Normal file
117
MDM-LCAG-JavaFunctions/src/main/java/routines/LcagTestTools.java
Normal file
@@ -0,0 +1,117 @@
|
|||||||
|
package routines;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* user specification: the function's comment should contain keys as follows: 1. write about the function's comment.but
|
||||||
|
* it must be before the "{talendTypes}" key.
|
||||||
|
*
|
||||||
|
* 2. {talendTypes} 's value must be talend Type, it is required . its value should be one of: String, char | Character,
|
||||||
|
* long | Long, int | Integer, boolean | Boolean, byte | Byte, Date, double | Double, float | Float, Object, short |
|
||||||
|
* Short
|
||||||
|
*
|
||||||
|
* 3. {Category} define a category for the Function. it is required. its value is user-defined .
|
||||||
|
*
|
||||||
|
* 4. {param} 's format is: {param} <type>[(<default value or closed list values>)] <name>[ : <comment>]
|
||||||
|
*
|
||||||
|
* <type> 's value should be one of: string, int, list, double, object, boolean, long, char, date. <name>'s value is the
|
||||||
|
* Function's parameter name. the {param} is optional. so if you the Function without the parameters. the {param} don't
|
||||||
|
* added. you can have many parameters for the Function.
|
||||||
|
*
|
||||||
|
* 5. {example} gives a example for the Function. it is optional.
|
||||||
|
*/
|
||||||
|
public class LcagTestTools {
|
||||||
|
|
||||||
|
private static LcagLogger log = LcagLogger.getLogger(LcagTestTools.class);
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* generateCode: Generate a sequence of codes from a given template @startCode.
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} LCAG-Test Tools
|
||||||
|
*
|
||||||
|
* {param} Object("startCode") input: Code to start with, e.g. TAA
|
||||||
|
* {param} Object("n") input: n-th code in generated sequence, e.g. TAC for n=2
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static String generateCode(String startCode, int n)
|
||||||
|
{
|
||||||
|
String result = startCode;
|
||||||
|
char[] resultAry = startCode.toCharArray();
|
||||||
|
|
||||||
|
for (int i = startCode.length()-1; i > 0; i -=1) {
|
||||||
|
char c = startCode.charAt(i);
|
||||||
|
int oldOrd = (c >= 'a') ? (int)c - (int)'a' : (int)c - (int)'A';
|
||||||
|
int newOrd = oldOrd + n;
|
||||||
|
|
||||||
|
int rest = newOrd % 26;
|
||||||
|
char newChar = (char)(c - oldOrd + rest);
|
||||||
|
|
||||||
|
n = newOrd / 26;
|
||||||
|
resultAry[i] = newChar;
|
||||||
|
|
||||||
|
if (n == 0) break;
|
||||||
|
}
|
||||||
|
|
||||||
|
result = String.valueOf(resultAry);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* compareToExpected: compares an expected result (first parameter) with the actual result (second paramter). Returns "OK" if both are equal and the actual value else.
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} LCAG-Test Tools
|
||||||
|
*
|
||||||
|
* {param} Object("expectedResult") input: expected result object
|
||||||
|
* {param} Object("ActualResult") input: actual result object
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static String compareToExpected(Object expectedResult, Object actualResult)
|
||||||
|
{
|
||||||
|
String result = "OK";
|
||||||
|
|
||||||
|
if (! java.util.Objects.equals(expectedResult, actualResult)) {
|
||||||
|
result = (actualResult == null) ? "n/a" : actualResult.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* compareToSimilarExpected: compares an expected String (first parameter) with an actual String (second parameter). Returns "OK" if both
|
||||||
|
* are equal excluding blancs and case and the actual value else.
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} LCAG-Test Tools
|
||||||
|
*
|
||||||
|
* {param} String("expectedResult") input: expected result object
|
||||||
|
* {param} String("ActualResult") input: actual result object
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static String compareToSimilarExpected(String expectedResult, String actualResult)
|
||||||
|
{
|
||||||
|
String result = "OK";
|
||||||
|
|
||||||
|
if (actualResult == null && expectedResult != null) {
|
||||||
|
result = "n/a";
|
||||||
|
} else if (actualResult != null && expectedResult == null) {
|
||||||
|
result = actualResult;
|
||||||
|
} else if (actualResult != null && expectedResult != null) {
|
||||||
|
String expectedSimilarResult = expectedResult.replaceAll(" ", "");
|
||||||
|
String actualSimilarResult = actualResult.replaceAll(" ", "");
|
||||||
|
|
||||||
|
if (! expectedSimilarResult.equalsIgnoreCase(actualSimilarResult)) {
|
||||||
|
result = actualResult;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
2053
MDM-LCAG-JavaFunctions/src/main/java/routines/LcagXmlTools.java
Normal file
2053
MDM-LCAG-JavaFunctions/src/main/java/routines/LcagXmlTools.java
Normal file
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,46 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
package routines;
|
||||||
|
|
||||||
|
import java.util.EnumSet;
|
||||||
|
|
||||||
|
import org.dom4j.Document;
|
||||||
|
import org.dom4j.DocumentException;
|
||||||
|
|
||||||
|
import routines.LcagXmlTools4.UpdateMode;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author U401455
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class LcagXmlTools3_5 extends LcagXmlTools4 {
|
||||||
|
/**
|
||||||
|
* Same as diffXMLAndCreateChangeList. Only difference: Elements that are empty in the resultXML are not updated and not listed in the Change List.
|
||||||
|
*
|
||||||
|
* Note: Parameters of type Object allow the XML representations as String, File name, Talend Document, Dom4J Document or Dom4J Element.
|
||||||
|
*
|
||||||
|
* {talendTypes} Document
|
||||||
|
*
|
||||||
|
* {Category} LCAG-XML Tools
|
||||||
|
*
|
||||||
|
* {param} object resultXml: If not null it contains the XML with added Change List as Item list with path, newValue and oldValue Tags in tag Update
|
||||||
|
* If null, the functions only the change list as result
|
||||||
|
*
|
||||||
|
* {param} string resultInsertChangeListXp: XPath to compare to and where to insert the Change List calculated by this function
|
||||||
|
*
|
||||||
|
* {param} object existingXml: usually an XML representing an existing entity in MDM DB or a MDM UI XML
|
||||||
|
*
|
||||||
|
* {param} string excludeCSV: CSV list of pathes to exclude from comparison
|
||||||
|
*
|
||||||
|
* {example} findXMLUpdatesAndCreateChangeList(resultXml, "exchange/report/Update", existingXml, "Id;LastupdateTimeStamp")
|
||||||
|
*/
|
||||||
|
public static Document findXMLUpdatesAndCreateChangeList(Object resultXml, String resultInsertChangeListXp, final Object existingXml, String excludeCSV) throws DocumentException
|
||||||
|
{
|
||||||
|
Document result = null;
|
||||||
|
|
||||||
|
result = _xpathUpdate(resultXml, existingXml, EnumSet.of(UpdateMode.UPDATE, UpdateMode.DIFF_ONLY), excludeCSV, resultInsertChangeListXp);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
1413
MDM-LCAG-JavaFunctions/src/main/java/routines/LcagXmlTools4.java
Normal file
1413
MDM-LCAG-JavaFunctions/src/main/java/routines/LcagXmlTools4.java
Normal file
File diff suppressed because it is too large
Load Diff
538
MDM-LCAG-JavaFunctions/src/main/java/routines/MDM.java
Normal file
538
MDM-LCAG-JavaFunctions/src/main/java/routines/MDM.java
Normal file
@@ -0,0 +1,538 @@
|
|||||||
|
// ============================================================================
|
||||||
|
//
|
||||||
|
// Copyright (C) 2006-2019 Talend Inc. - www.talend.com
|
||||||
|
//
|
||||||
|
// This source code is available under agreement available at
|
||||||
|
// %InstallDIR%\features\org.talend.rcp.branding.%PRODUCTNAME%\%PRODUCTNAME%license.txt
|
||||||
|
//
|
||||||
|
// You should have received a copy of the agreement
|
||||||
|
// along with this program; if not, write to Talend SA
|
||||||
|
// 9 rue Pages 92150 Suresnes, France
|
||||||
|
//
|
||||||
|
// ============================================================================
|
||||||
|
package routines;
|
||||||
|
|
||||||
|
import java.io.StringReader;
|
||||||
|
import java.io.StringWriter;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
import javax.xml.XMLConstants;
|
||||||
|
import javax.xml.parsers.DocumentBuilder;
|
||||||
|
import javax.xml.parsers.DocumentBuilderFactory;
|
||||||
|
import javax.xml.transform.Transformer;
|
||||||
|
import javax.xml.transform.TransformerException;
|
||||||
|
import javax.xml.transform.TransformerFactory;
|
||||||
|
import javax.xml.transform.dom.DOMSource;
|
||||||
|
import javax.xml.transform.stream.StreamResult;
|
||||||
|
import javax.xml.xpath.XPath;
|
||||||
|
import javax.xml.xpath.XPathConstants;
|
||||||
|
import javax.xml.xpath.XPathExpression;
|
||||||
|
import javax.xml.xpath.XPathFactory;
|
||||||
|
|
||||||
|
import org.w3c.dom.Document;
|
||||||
|
import org.w3c.dom.Element;
|
||||||
|
import org.w3c.dom.Node;
|
||||||
|
import org.w3c.dom.NodeList;
|
||||||
|
import org.xml.sax.InputSource;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* user specification: the function's comment should contain keys as follows: 1. write about the function's comment.but
|
||||||
|
* it must be before the "{talendTypes}" key.
|
||||||
|
*
|
||||||
|
* 2. {talendTypes} 's value must be talend Type, it is required . its value should be one of: String, char | Character,
|
||||||
|
* long | Long, int | Integer, boolean | Boolean, byte | Byte, Date, double | Double, float | Float, Object, short |
|
||||||
|
* Short
|
||||||
|
*
|
||||||
|
* 3. {Category} define a category for the Function. it is required. its value is user-defined .
|
||||||
|
*
|
||||||
|
* 4. {param} 's format is: {param} <type>[(<default value or closed list values>)] <name>[ : <comment>]
|
||||||
|
*
|
||||||
|
* <type> 's value should be one of: string, int, list, double, object, boolean, long, char, date. <name>'s value is the
|
||||||
|
* Function's parameter name. the {param} is optional. so if you the Function without the parameters. the {param} don't
|
||||||
|
* added. you can have many parameters for the Function.
|
||||||
|
*
|
||||||
|
* 5. {example} gives a example for the Function. it is optional.
|
||||||
|
*/
|
||||||
|
public class MDM {
|
||||||
|
|
||||||
|
public static final String WS_CLIENT_CONNECTION_TIMEOUT_KEY = "javax.xml.ws.client.connectionTimeout"; //$NON-NLS-1$
|
||||||
|
|
||||||
|
public static final String WS_CLIENT_RECEIVE_TIMEOUT_KEY = "javax.xml.ws.client.receiveTimeout"; //$NON-NLS-1$
|
||||||
|
|
||||||
|
public static final String WS_CLIENT_CONNECTION_TIME_VALUE = "ws_client_connection_timeout"; //$NON-NLS-1$
|
||||||
|
|
||||||
|
public static final String WS_CLIENT_RECEIVE_TIME_VALUE = "ws_client_receive_timeout"; //$NON-NLS-1$
|
||||||
|
|
||||||
|
public static final String DISALLOW_DOCTYPE_DECL = "http://apache.org/xml/features/disallow-doctype-decl"; //$NON-NLS-1$
|
||||||
|
|
||||||
|
/**
|
||||||
|
* getFK: Return one of the FK component by position in a mangled FK (FKs are mangled in MDM to accommodate for
|
||||||
|
* compound keys)
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} MDM
|
||||||
|
*
|
||||||
|
* {param} string(FKs) mangledFK: original mangled FK.
|
||||||
|
*
|
||||||
|
* {param} int(0) pos: key position (starts at 0)
|
||||||
|
*
|
||||||
|
* {example} getFK(FKs,0) # 12345
|
||||||
|
*/
|
||||||
|
public static String getFK(String mangledFK, int pos) {
|
||||||
|
if (mangledFK == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
Pattern p = Pattern.compile("(\\[[^\\[\\]]*\\])"); //$NON-NLS-1$
|
||||||
|
Matcher m = p.matcher(mangledFK.trim());
|
||||||
|
int i = 0;
|
||||||
|
while (m.find()) {
|
||||||
|
if (i == pos) {
|
||||||
|
String targetValue = m.group(0);
|
||||||
|
return targetValue.substring(1, targetValue.length() - 1);
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* createFK: Returns the mangled FK string of a given key (FKs are mangled in MDM to accommodate for compound keys).
|
||||||
|
* Returns null if key is null.
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} MDM
|
||||||
|
*
|
||||||
|
* {param} string singleKey: original key.
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* {example} createFK("0") # return "[0]"
|
||||||
|
*/
|
||||||
|
public static String createFK(String singleKey) {
|
||||||
|
if (singleKey != null) {
|
||||||
|
return "[" + singleKey + "]"; //$NON-NLS-1$ //$NON-NLS-2$
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* createFK: Returns the mangled FK string of a given array of keys (FKs are mangled in MDM to accommodate for
|
||||||
|
* compound keys). Returns null if one of the keys is null.
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} MDM
|
||||||
|
*
|
||||||
|
* {param} string singleKey: original key array.
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* {example} createFK({"0","1"}) # return "[0][1]"
|
||||||
|
*/
|
||||||
|
public static String createFK(String[] keys) {
|
||||||
|
StringBuffer sb = new StringBuffer();
|
||||||
|
for (String key : keys) {
|
||||||
|
if (key == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
sb.append("[").append(key).append("]"); //$NON-NLS-1$ //$NON-NLS-2$
|
||||||
|
}
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get repeating element in xmlString according to the xpath & position
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} MDM
|
||||||
|
*
|
||||||
|
* {param} string(xml) xml: xml
|
||||||
|
*
|
||||||
|
* {param} string(xpath) xpath: xpath.
|
||||||
|
*
|
||||||
|
* {param} int(0) position: position.
|
||||||
|
*/
|
||||||
|
public static String getRepeatingElement(String xml, String xpath, int position) throws Exception {
|
||||||
|
|
||||||
|
Node node = parse(xml);
|
||||||
|
NodeList list = getNodeList(node, xpath, false);
|
||||||
|
for (int i = 0; i < list.getLength(); i++) {
|
||||||
|
if (i == position) {
|
||||||
|
Node n = list.item(i);
|
||||||
|
return n.getNodeValue();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* check repeating elements in xmlString according to xpath & text
|
||||||
|
*
|
||||||
|
* {talendTypes} Boolean
|
||||||
|
*
|
||||||
|
* {Category} MDM
|
||||||
|
*
|
||||||
|
* {param} string(xml) xml: xml.
|
||||||
|
*
|
||||||
|
* {param} string(xpath) xpath: xpath.
|
||||||
|
*
|
||||||
|
* {param} String(text) text: text.
|
||||||
|
*/
|
||||||
|
public static boolean hasRepeatingElement(String xml, String xpath, String text) throws Exception {
|
||||||
|
Node node = parse(xml);
|
||||||
|
NodeList list = getNodeList(node, xpath, false);
|
||||||
|
for (int i = 0; i < list.getLength(); i++) {
|
||||||
|
Node n = list.item(i);
|
||||||
|
if (n.getNodeValue().equals(text)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* list repeating elements in xmlString according to xpath & delimiter
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} MDM
|
||||||
|
*
|
||||||
|
* {param} string(xml) xml: xml.
|
||||||
|
*
|
||||||
|
* {param} string(xpath) xpath: xpath.
|
||||||
|
*
|
||||||
|
* {param} char(delimiter) delimiter: delimiter.
|
||||||
|
*/
|
||||||
|
public static String listRepeatingElement(String xml, String xpath, char delimiter) throws Exception {
|
||||||
|
Node node = parse(xml);
|
||||||
|
|
||||||
|
NodeList list = getNodeList(node, xpath, false);
|
||||||
|
StringBuffer sb = new StringBuffer();
|
||||||
|
for (int i = 0; i < list.getLength(); i++) {
|
||||||
|
Node n = list.item(i);
|
||||||
|
sb.append(n.getNodeValue());
|
||||||
|
if (i >= 0 && i < list.getLength() - 1) {
|
||||||
|
sb.append(delimiter);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* add repeating elements in xmlString according to xpath & text
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} MDM
|
||||||
|
*
|
||||||
|
* {param} string(xml) xml: xml
|
||||||
|
*
|
||||||
|
* {param} string(xpath) xpath: xpath
|
||||||
|
*
|
||||||
|
* {param} String(text) text: text
|
||||||
|
*/
|
||||||
|
public static String addRepeatingElement(String xml, String xpath, String text) throws Exception {
|
||||||
|
Node node = parse(xml);
|
||||||
|
|
||||||
|
int pos = xpath.lastIndexOf('/');
|
||||||
|
String name = xpath.substring(pos + 1);
|
||||||
|
String parentPath = xpath.substring(0, pos);
|
||||||
|
NodeList plist = getNodeList(node, parentPath, true);
|
||||||
|
if (plist.getLength() > 0) {
|
||||||
|
Element el = node.getOwnerDocument().createElement(name);
|
||||||
|
el.setTextContent(text);
|
||||||
|
plist.item(0).appendChild(el);
|
||||||
|
}
|
||||||
|
|
||||||
|
return nodeToString(node, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated Generate an <error code="X">msg</error> fragment
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} MDM
|
||||||
|
*
|
||||||
|
* {param} string msg: error message.
|
||||||
|
*
|
||||||
|
* {param} int(0) code: error code, (1:ERROR, 0:NORMAL)
|
||||||
|
*
|
||||||
|
* {example} genErrMsg("test message",0) # return <error code="0">test message</error>
|
||||||
|
*/
|
||||||
|
@Deprecated
|
||||||
|
public static String createReturnMessage(String msg, int code) {
|
||||||
|
return "<error code=\"" + code + "\">" + msg + "</error>"; //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate an <report><message type="X">msg</message><report> fragment
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} MDM
|
||||||
|
*
|
||||||
|
* {param} string msg: error message.
|
||||||
|
*
|
||||||
|
* {param} String(0) type: error code, (info|error)
|
||||||
|
*
|
||||||
|
* {example} genErrMsg("test message",0) # return <error code="0">test message</error>
|
||||||
|
*/
|
||||||
|
public static String createReturnMessage(String msg, String type) {
|
||||||
|
return "<report><message type=\"" + type + "\">" + msg + "</message></report>"; //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add or update an ISO variant to the multi-lingual text value
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} MDM
|
||||||
|
*
|
||||||
|
* {param} string(iso) iso: iso
|
||||||
|
*
|
||||||
|
* {param} string(value) value: value
|
||||||
|
*
|
||||||
|
* {param} string(rawValue) rawValue: rawValue
|
||||||
|
*
|
||||||
|
* {example} setLanguageVariant("EN","abc","[EN:ab][FR:ab_fr]") # return [EN:abc][FR:ab_fr]
|
||||||
|
*/
|
||||||
|
public static String setLanguageVariant(String iso, String value, String rawValue) {
|
||||||
|
|
||||||
|
return setLanguageVariant(iso, value, rawValue, "EN", true); //$NON-NLS-1$
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add or update an ISO variant to the multi-lingual text value with defaultIso and sort option (For the legacy
|
||||||
|
* value which do not follow the multi-lingual field syntax, it will be adapted to defaultIso)
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} MDM
|
||||||
|
*
|
||||||
|
* {param} string(iso) iso: iso
|
||||||
|
*
|
||||||
|
* {param} string(value) value: value
|
||||||
|
*
|
||||||
|
* {param} string(rawValue) rawValue: rawValue
|
||||||
|
*
|
||||||
|
* {param} string(defaultIso) defaultIso: defaultIso
|
||||||
|
*
|
||||||
|
* {param} string(sort) sort: sort
|
||||||
|
*
|
||||||
|
* {example} setLanguageVariant("FR","ab_fr","ab","EN", true) # return [EN:ab][FR:ab_fr]
|
||||||
|
*/
|
||||||
|
public static String setLanguageVariant(String iso, String value, String rawValue, String defaultIso, boolean sort) {
|
||||||
|
|
||||||
|
if (iso == null || value == null) {
|
||||||
|
throw new IllegalArgumentException();
|
||||||
|
}
|
||||||
|
|
||||||
|
iso = iso.toUpperCase();
|
||||||
|
|
||||||
|
Map<String, String> isoValues = new LinkedHashMap<String, String>();
|
||||||
|
|
||||||
|
if (rawValue == null || rawValue.trim().length() == 0) {
|
||||||
|
isoValues.put(iso, value);
|
||||||
|
} else {
|
||||||
|
|
||||||
|
Pattern p = Pattern.compile("\\[(\\w+)\\:([^\\[\\]]*?)\\]{1,}"); //$NON-NLS-1$
|
||||||
|
Matcher m = p.matcher(rawValue);
|
||||||
|
while (m.find()) {
|
||||||
|
isoValues.put(m.group(1).toUpperCase(), m.group(2));
|
||||||
|
}
|
||||||
|
|
||||||
|
// illegal/legacy raw value
|
||||||
|
if (isoValues.size() == 0) {
|
||||||
|
// throw new IllegalArgumentException();
|
||||||
|
if (defaultIso != null && defaultIso.trim().length() > 0) {
|
||||||
|
isoValues.put(defaultIso.toUpperCase(), rawValue);
|
||||||
|
} else {
|
||||||
|
isoValues.put("EN", rawValue); //$NON-NLS-1$
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
isoValues.put(iso, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
StringBuilder result = new StringBuilder();
|
||||||
|
if (isoValues.size() > 0) {
|
||||||
|
|
||||||
|
List<String> isoList = new ArrayList<String>(isoValues.keySet());
|
||||||
|
// sort
|
||||||
|
if (sort) {
|
||||||
|
Collections.sort(isoList);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (String string : isoList) {
|
||||||
|
String isoKey = string;
|
||||||
|
String isoValue = isoValues.get(isoKey);
|
||||||
|
result.append("[").append(isoKey).append(":").append(isoValue).append("]"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result.toString();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Give an ISO value from a multi-lingual text value, with default iso fallback
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} MDM
|
||||||
|
*
|
||||||
|
* {param} string(iso) iso: iso
|
||||||
|
*
|
||||||
|
* {param} string(defaultIso) defaultIso: Default iso used in case of fallback
|
||||||
|
*
|
||||||
|
* {param} string(rawValue) rawValue: rawValue
|
||||||
|
*
|
||||||
|
* {example} getLanguageVariant("DE","EN","[EN:ab][FR:ab_fr]") # return [EN:ab]
|
||||||
|
*/
|
||||||
|
public static String getLanguageVariant(String iso, String defaultIso, String rawValue) {
|
||||||
|
String requestedLanguageVariant = getLanguageVariant(iso, rawValue);
|
||||||
|
if (requestedLanguageVariant == null || "".equals(requestedLanguageVariant)) { //$NON-NLS-1$
|
||||||
|
// fallback to the default variant
|
||||||
|
return getLanguageVariant(defaultIso, rawValue);
|
||||||
|
} else {
|
||||||
|
return requestedLanguageVariant;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Give an ISO value from a multi-lingual text value
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} MDM
|
||||||
|
*
|
||||||
|
* {param} string(iso) iso: iso
|
||||||
|
*
|
||||||
|
* {param} string(rawValue) rawValue: rawValue
|
||||||
|
*
|
||||||
|
* {example} getLanguageVariant("FR","[EN:ab][FR:ab_fr]") # return ab_fr
|
||||||
|
*/
|
||||||
|
public static String getLanguageVariant(String iso, String rawValue) {
|
||||||
|
|
||||||
|
if (iso == null || rawValue == null) {
|
||||||
|
throw new IllegalArgumentException();
|
||||||
|
}
|
||||||
|
|
||||||
|
iso = iso.toUpperCase();
|
||||||
|
|
||||||
|
Map<String, String> isoValues = new HashMap<String, String>();
|
||||||
|
Pattern p = Pattern.compile("\\[(\\w+)\\:([^\\[\\]]*?)\\]{1,}"); //$NON-NLS-1$
|
||||||
|
Matcher m = p.matcher(rawValue);
|
||||||
|
while (m.find()) {
|
||||||
|
isoValues.put(m.group(1).toUpperCase(), m.group(2));
|
||||||
|
}
|
||||||
|
|
||||||
|
return isoValues.get(iso);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get webservice client timeout information from jvm param setting
|
||||||
|
*
|
||||||
|
* {talendTypes} int
|
||||||
|
*
|
||||||
|
* {Category} MDM
|
||||||
|
*
|
||||||
|
* {param} String property: timeout property
|
||||||
|
*
|
||||||
|
* {example} getWSClientTimeout(String property) # return 60000
|
||||||
|
*/
|
||||||
|
public static int getWSClientTimeout(String property) throws Exception {
|
||||||
|
int defaultTimeout = 60000;
|
||||||
|
String inputTimeout = System.getProperty(property);
|
||||||
|
if (inputTimeout != null) {
|
||||||
|
try {
|
||||||
|
int timeout = Integer.parseInt(inputTimeout);
|
||||||
|
if (timeout > 0) {
|
||||||
|
return timeout;
|
||||||
|
}
|
||||||
|
} catch (Exception exception) {
|
||||||
|
throw new RuntimeException("Webservice " + property + " property value '" + inputTimeout + "' is invalid", exception);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return defaultTimeout;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Utility methods
|
||||||
|
/**
|
||||||
|
* Get a nodelist from an xPath
|
||||||
|
*
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
private static NodeList getNodeList(Node contextNode, String xPath, boolean isParent) throws Exception {
|
||||||
|
if (!xPath.matches(".*@[^/\\]]+")) { //$NON-NLS-1$
|
||||||
|
if (!xPath.endsWith(")") && !isParent) { //$NON-NLS-1$
|
||||||
|
xPath += "/text()"; //$NON-NLS-1$
|
||||||
|
}
|
||||||
|
}
|
||||||
|
XPathFactory factory = XPathFactory.newInstance();
|
||||||
|
factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
|
||||||
|
XPath xpath = factory.newXPath();
|
||||||
|
XPathExpression expr = xpath.compile(xPath);
|
||||||
|
|
||||||
|
Object result = expr.evaluate(contextNode, XPathConstants.NODESET);
|
||||||
|
NodeList nodes = (NodeList) result;
|
||||||
|
return nodes;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* parse the xml
|
||||||
|
*
|
||||||
|
* @param xml
|
||||||
|
* @return
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
private static Node parse(String xml) throws Exception {
|
||||||
|
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
|
||||||
|
domFactory.setNamespaceAware(true); // never forget this!
|
||||||
|
domFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
|
||||||
|
domFactory.setFeature(DISALLOW_DOCTYPE_DECL, true);
|
||||||
|
DocumentBuilder builder = domFactory.newDocumentBuilder();
|
||||||
|
Document doc = builder.parse(new InputSource(new StringReader(xml)));
|
||||||
|
return doc.getDocumentElement();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generates an xml string from a node with or without the xml declaration (not pretty formatted)
|
||||||
|
*
|
||||||
|
* @param n the node
|
||||||
|
* @return the xml string
|
||||||
|
* @throws TransformerException
|
||||||
|
*/
|
||||||
|
private static String nodeToString(Node n, boolean omitXMLDeclaration) throws TransformerException {
|
||||||
|
StringWriter sw = new StringWriter();
|
||||||
|
TransformerFactory transFactory = TransformerFactory.newInstance();
|
||||||
|
transFactory.setFeature(javax.xml.XMLConstants.FEATURE_SECURE_PROCESSING, Boolean.TRUE);
|
||||||
|
Transformer transformer = transFactory.newTransformer();
|
||||||
|
if (omitXMLDeclaration) {
|
||||||
|
transformer.setOutputProperty("omit-xml-declaration", "yes"); //$NON-NLS-1$ //$NON-NLS-2$
|
||||||
|
} else {
|
||||||
|
transformer.setOutputProperty("omit-xml-declaration", "no"); //$NON-NLS-1$ //$NON-NLS-2$
|
||||||
|
}
|
||||||
|
transformer.setOutputProperty("indent", "yes"); //$NON-NLS-1$ //$NON-NLS-2$
|
||||||
|
transformer.transform(new DOMSource(n), new StreamResult(sw));
|
||||||
|
return sw.toString().replaceAll("\r\n", "\n"); //$NON-NLS-1$ //$NON-NLS-2$
|
||||||
|
}
|
||||||
|
}
|
||||||
573
MDM-LCAG-JavaFunctions/src/main/java/routines/Mathematical.java
Normal file
573
MDM-LCAG-JavaFunctions/src/main/java/routines/Mathematical.java
Normal file
@@ -0,0 +1,573 @@
|
|||||||
|
// ============================================================================
|
||||||
|
//
|
||||||
|
// Copyright (c) 2006-2014, Talend Inc.
|
||||||
|
//
|
||||||
|
// This source code has been automatically generated by_Talend MDM Platform
|
||||||
|
// CodeGenerator version 7.3.1.20220805_1617-patch
|
||||||
|
// You can find more information about Talend products at www.talend.com.
|
||||||
|
// You may distribute this code under the terms of the GNU LGPL license
|
||||||
|
// http://www.gnu.org/licenses/lgpl.html).
|
||||||
|
//
|
||||||
|
// ============================================================================
|
||||||
|
package routines;
|
||||||
|
|
||||||
|
import routines.system.RandomUtils;
|
||||||
|
|
||||||
|
public class Mathematical {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the absolute (positive) numeric value of an expression.
|
||||||
|
*
|
||||||
|
* {talendTypes} double | Double
|
||||||
|
*
|
||||||
|
* {Category} Mathematical
|
||||||
|
*
|
||||||
|
* {param} double(10)
|
||||||
|
*
|
||||||
|
* {example} ABS(-10) # 10
|
||||||
|
*/
|
||||||
|
public static double ABS(double a) {
|
||||||
|
return Math.abs(a);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculates the trigonometric arc-cosine of an expression.
|
||||||
|
*
|
||||||
|
* {talendTypes} double | Double
|
||||||
|
*
|
||||||
|
* {Category} Mathematical
|
||||||
|
*
|
||||||
|
* {param} double(0.15)
|
||||||
|
*
|
||||||
|
* {example} ACOS(0.15)
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static double ACOS(double a) {
|
||||||
|
return Math.acos(a);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculates the trigonometric arc-sine of an expression.
|
||||||
|
*
|
||||||
|
* {talendTypes} double | Double
|
||||||
|
*
|
||||||
|
* {Category} Mathematical
|
||||||
|
*
|
||||||
|
* {param} double(0.15)
|
||||||
|
*
|
||||||
|
* {example} ASIN(0.15)
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static double ASIN(double a) {
|
||||||
|
return Math.asin(a);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculates the trigonometric arctangent of an expression.
|
||||||
|
*
|
||||||
|
* {talendTypes} double | Double
|
||||||
|
*
|
||||||
|
* {Category} Mathematical
|
||||||
|
*
|
||||||
|
* {param} double(3.14)
|
||||||
|
*
|
||||||
|
* {example} ATAN(3.14)
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static double ATAN(double a) {
|
||||||
|
return Math.atan(a);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Performs a bitwise AND of two integers.
|
||||||
|
*
|
||||||
|
* {talendTypes} int | Integer
|
||||||
|
*
|
||||||
|
* {Category} Mathematical
|
||||||
|
*
|
||||||
|
* {param} int(1) a :integer
|
||||||
|
*
|
||||||
|
* {param} int(2) b :integer
|
||||||
|
*
|
||||||
|
* {example} BITAND(1,1)
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static int BITAND(int a, int b) {
|
||||||
|
return a & b;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Performs a bitwise NOT of a integers.
|
||||||
|
*
|
||||||
|
* {talendTypes} int | Integer
|
||||||
|
*
|
||||||
|
* {Category} Mathematical
|
||||||
|
*
|
||||||
|
* {param} int(10)
|
||||||
|
*
|
||||||
|
* {example} BITNOT(10)
|
||||||
|
*/
|
||||||
|
public static int BITNOT(int a) {
|
||||||
|
return ~a;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Performs a bitwise OR of two integers.
|
||||||
|
*
|
||||||
|
* {talendTypes} int | Integer
|
||||||
|
*
|
||||||
|
* {Category} Mathematical
|
||||||
|
*
|
||||||
|
* {param} int(10) a: integer
|
||||||
|
*
|
||||||
|
* {param} int(10) b: integer
|
||||||
|
*
|
||||||
|
* {example} BITOR(10,10)
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static int BITOR(int a, int b) {
|
||||||
|
return a | b;
|
||||||
|
}
|
||||||
|
|
||||||
|
// BITRESET( ) Resets one bit of an integer.
|
||||||
|
|
||||||
|
// BITSET( ) Sets one bit of an integer.
|
||||||
|
// BITTEST( ) Tests one bit of an integer.
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Performs a bitwise XOR of two integers.
|
||||||
|
*
|
||||||
|
* {talendTypes} int | Integer
|
||||||
|
*
|
||||||
|
* {Category} Mathematical
|
||||||
|
*
|
||||||
|
* {param} int(10) a: integer
|
||||||
|
*
|
||||||
|
* {param} int(10) b: integer
|
||||||
|
*
|
||||||
|
* {example} BITXOR(10,10)
|
||||||
|
*/
|
||||||
|
public static int BITXOR(int a, int b) {
|
||||||
|
return a ^ b;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculates the trigonometric cosine of an angle.
|
||||||
|
*
|
||||||
|
* {talendTypes} double | Double
|
||||||
|
*
|
||||||
|
* {Category} Mathematical
|
||||||
|
*
|
||||||
|
* {param} double(3.14)
|
||||||
|
*
|
||||||
|
* {example} COS(3.14)
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static double COS(double a) {
|
||||||
|
return Math.cos(a);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculates the hyperbolic cosine of an expression.
|
||||||
|
*
|
||||||
|
* {talendTypes} double | Double
|
||||||
|
*
|
||||||
|
* {Category} Mathematical
|
||||||
|
*
|
||||||
|
* {param} double(3.14)
|
||||||
|
*
|
||||||
|
* {example} COSH(3.14)
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static double COSH(double a) {
|
||||||
|
return Math.cosh(a);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Outputs the whole part of the real division of two real numbers.
|
||||||
|
*
|
||||||
|
* {talendTypes} double | Double
|
||||||
|
*
|
||||||
|
* {Category} Mathematical
|
||||||
|
*
|
||||||
|
* {param} double(3.14) a: real number
|
||||||
|
*
|
||||||
|
* {param} double(3.14) b: real number
|
||||||
|
*
|
||||||
|
* {example} DIV(3.14,3.14)
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static int DIV(double a, double b) {
|
||||||
|
return (int) (a / b);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculates the result of base 'e' raised to the power designated by the value of the expression.
|
||||||
|
*
|
||||||
|
* {talendTypes} double | Double
|
||||||
|
*
|
||||||
|
* {Category} Mathematical
|
||||||
|
*
|
||||||
|
* {param} double(3.14)
|
||||||
|
*
|
||||||
|
* {example} EXP(3.14)
|
||||||
|
*/
|
||||||
|
public static double EXP(double a) {
|
||||||
|
return Math.exp(a);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculates the integer numeric value of an expression.
|
||||||
|
*
|
||||||
|
* {talendTypes} int | Integer
|
||||||
|
*
|
||||||
|
* {Category} Mathematical
|
||||||
|
*
|
||||||
|
* {param} string("100")
|
||||||
|
*
|
||||||
|
* {example} INT(\"100\")
|
||||||
|
*/
|
||||||
|
public static int INT(String e) {
|
||||||
|
return Integer.valueOf(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
// FADD( ) Performs floating-point addition on two numeric values. This function is provided for compatibility with
|
||||||
|
// existing software.
|
||||||
|
// FDIV( ) Performs floating-point division on two numeric values.
|
||||||
|
//
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts a floating-point number to a string with a fixed precision. FFIX is provided for compatibility
|
||||||
|
* with existing software.
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} Mathematical
|
||||||
|
*
|
||||||
|
* {param} double(3.1415926) d: real number
|
||||||
|
*
|
||||||
|
* {param} int(2) precision: precision
|
||||||
|
*
|
||||||
|
* {example}FFIX(3.1415926,2)
|
||||||
|
*/
|
||||||
|
public static String FFIX(double d, int precision) {
|
||||||
|
double p = Math.pow(10, precision);
|
||||||
|
d = d * p;
|
||||||
|
d = Math.round(d) / p;
|
||||||
|
return Double.toString(d);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Rounds a number to a string with a precision of 14.
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} Mathematical
|
||||||
|
*
|
||||||
|
* {param} double(3.14)
|
||||||
|
*
|
||||||
|
* {example} FFLT(3.14)
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static String FFLT(double d) {
|
||||||
|
return Mathematical.FFIX(d, 14);
|
||||||
|
}
|
||||||
|
|
||||||
|
// FMUL( ) Performs floating-point multiplication on two numeric values. This function is provided for compatibility
|
||||||
|
// with existing software.
|
||||||
|
// FSUB( ) Performs floating-point subtraction on two numeric values.
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculates the natural logarithm of an expression in base 'e'.
|
||||||
|
*
|
||||||
|
* {talendTypes} double | Double
|
||||||
|
*
|
||||||
|
* {Category} Mathematical
|
||||||
|
*
|
||||||
|
* {param} double(3.14)
|
||||||
|
*
|
||||||
|
* {example} LN(3.14)
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static double LN(double a) {
|
||||||
|
return Math.log(a);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculates the modulo (the remainder) of two expressions.
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} Mathematical
|
||||||
|
*
|
||||||
|
* {param} double(3) a: double
|
||||||
|
*
|
||||||
|
* {param} double(2) b: double
|
||||||
|
*
|
||||||
|
* {example} MOD(3,2)
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static double MOD(double a, double b) {
|
||||||
|
return a % b;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Mathematical.MOD(3, 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the arithmetic additive inverse of the value of the argument.
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* {talendTypes} double | Double
|
||||||
|
*
|
||||||
|
* {Category} Mathematical
|
||||||
|
*
|
||||||
|
* {param} double(3.14)
|
||||||
|
*
|
||||||
|
* {example} NEG(3.14)
|
||||||
|
*/
|
||||||
|
public static double NEG(double a) {
|
||||||
|
return -1 * a;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns true (1) if the argument is a numeric data type; otherwise, returns false (0).
|
||||||
|
*
|
||||||
|
* {talendTypes} int | Integer
|
||||||
|
*
|
||||||
|
* {Category} Mathematical
|
||||||
|
*
|
||||||
|
* {param} string("1")
|
||||||
|
*
|
||||||
|
* {example} NUM(\"1\")
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static int NUM(String e) {
|
||||||
|
if (e.matches("\\d+")) { //$NON-NLS-1$
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// PWR( ) Calculates the value of an expression when raised to a specified
|
||||||
|
// power.
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts a numeric expression into a real number without loss of accuracy.
|
||||||
|
*
|
||||||
|
* {talendTypes} double | Double
|
||||||
|
*
|
||||||
|
* {Category} Mathematical
|
||||||
|
*
|
||||||
|
* {param} string("3.14")
|
||||||
|
*
|
||||||
|
* {example} REAL(\"3.14\")
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static double REAL(String e) {
|
||||||
|
return Double.valueOf(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
// REM( ) Calculates the value of the remainder after integer division is
|
||||||
|
// performed.
|
||||||
|
//
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generates a random number between zero and a specified number minus one.
|
||||||
|
*
|
||||||
|
* {talendTypes} double | Double
|
||||||
|
*
|
||||||
|
* {Category} Mathematical
|
||||||
|
*
|
||||||
|
* {param} double(3.14)
|
||||||
|
*
|
||||||
|
* {example} RND(3.14)
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static double RND(double a) {
|
||||||
|
return RandomUtils.random() * a;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds two string numbers and returns the result as a string number.
|
||||||
|
*
|
||||||
|
* {talendTypes} double | Double
|
||||||
|
*
|
||||||
|
* {Category} Mathematical
|
||||||
|
*
|
||||||
|
* {param} string("10") a: string number
|
||||||
|
*
|
||||||
|
* {param} string("10") b: string number
|
||||||
|
*
|
||||||
|
* {example} SADD(\"10\",\"10\")
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static double SADD(String a, String b) {
|
||||||
|
return Double.valueOf(a) + Double.valueOf(b);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Compares two string numbers.
|
||||||
|
*
|
||||||
|
* {talendTypes} int | Integer
|
||||||
|
*
|
||||||
|
* {Category} Mathematical
|
||||||
|
*
|
||||||
|
* {param} string("12") a: string
|
||||||
|
*
|
||||||
|
* {param} string("13") b: string
|
||||||
|
*
|
||||||
|
* {example} SCMP(\"12\",\"13\")
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static int SCMP(String a, String b) {
|
||||||
|
double da = Double.valueOf(a);
|
||||||
|
double db = Double.valueOf(b);
|
||||||
|
if (da > db) {
|
||||||
|
return 1;
|
||||||
|
} else if (da == db) {
|
||||||
|
return 0;
|
||||||
|
} else {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Outputs the quotient of the whole division of two integers.
|
||||||
|
*
|
||||||
|
* {talendTypes} int | Integer
|
||||||
|
*
|
||||||
|
* {Category} Mathematical
|
||||||
|
*
|
||||||
|
* {param} int(10) a: int
|
||||||
|
*
|
||||||
|
* {param} int(10) b: int
|
||||||
|
*
|
||||||
|
* {example} SDIV(10,20)
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static int SDIV(int a, int b) {
|
||||||
|
return (int) (a / b);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculates the trigonometric sine of an angle.
|
||||||
|
*
|
||||||
|
* {talendTypes} double | Double
|
||||||
|
*
|
||||||
|
* {Category} Mathematical
|
||||||
|
*
|
||||||
|
* {param} double(3.14)
|
||||||
|
*
|
||||||
|
* {example} SIN(3.14)
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static double SIN(double a) {
|
||||||
|
return Math.sin(a);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculates the hyperbolic sine of an expression.
|
||||||
|
*
|
||||||
|
* {talendTypes} double | Double
|
||||||
|
*
|
||||||
|
* {Category} Mathematical
|
||||||
|
*
|
||||||
|
* {param} double(3.14)
|
||||||
|
*
|
||||||
|
* {example} SINH(3.14)
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static double SINH(double a) {
|
||||||
|
return Math.sinh(a);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Multiplies two string numbers.
|
||||||
|
*
|
||||||
|
* {talendTypes} double | Double
|
||||||
|
*
|
||||||
|
* {Category} Mathematical
|
||||||
|
*
|
||||||
|
* {param} string("3.14") a: string
|
||||||
|
*
|
||||||
|
* {param} string("3.14") b: string
|
||||||
|
*
|
||||||
|
* {example} SMUL(\"3.14\",\"3.14\")
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static double SMUL(String a, String b) {
|
||||||
|
return Double.valueOf(a) * Double.valueOf(b);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculates the square root of a number.
|
||||||
|
*
|
||||||
|
* {talendTypes} double | Double
|
||||||
|
*
|
||||||
|
* {Category} Mathematical
|
||||||
|
*
|
||||||
|
* {param} double(4)
|
||||||
|
*
|
||||||
|
* {example} SQRT(4.0)
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static double SQRT(double a) {
|
||||||
|
return Math.sqrt(a);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Subtracts one string number from another and returns the result as a string number.
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} Mathematical
|
||||||
|
*
|
||||||
|
* {param} string(20) a: string
|
||||||
|
*
|
||||||
|
* {param} string(10) b: string
|
||||||
|
*
|
||||||
|
* {example} SSUB(\"20\",\"10\")
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static String SSUB(String a, String b) {
|
||||||
|
return Double.toString(Double.valueOf(a) - Double.valueOf(b));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculates the trigonometric tangent of an angle.
|
||||||
|
*
|
||||||
|
* {talendTypes} double | Double
|
||||||
|
*
|
||||||
|
* {Category} Mathematical
|
||||||
|
*
|
||||||
|
* {param} double(3.14) a: double
|
||||||
|
*
|
||||||
|
* {example} TAN(3.14)
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static double TAN(double a) {
|
||||||
|
return Math.tan(a);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculates the hyperbolic tangent of an expression.
|
||||||
|
*
|
||||||
|
* {talendTypes} double | Double
|
||||||
|
*
|
||||||
|
* {Category} Mathematical
|
||||||
|
*
|
||||||
|
* {param} double(3.14) a: double
|
||||||
|
*
|
||||||
|
* {example} TANH(3.14)
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static double TANH(double a) {
|
||||||
|
return Math.tanh(a);
|
||||||
|
}
|
||||||
|
}
|
||||||
675
MDM-LCAG-JavaFunctions/src/main/java/routines/NumberUtil.java
Normal file
675
MDM-LCAG-JavaFunctions/src/main/java/routines/NumberUtil.java
Normal file
@@ -0,0 +1,675 @@
|
|||||||
|
package routines;
|
||||||
|
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
import java.text.NumberFormat;
|
||||||
|
import java.util.Calendar;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.Locale;
|
||||||
|
import java.util.TimeZone;
|
||||||
|
|
||||||
|
public class NumberUtil {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* returns the first not null Integer
|
||||||
|
*
|
||||||
|
* {Category} NumberUtil
|
||||||
|
*
|
||||||
|
* {talendTypes} Integer
|
||||||
|
*
|
||||||
|
* {param} Interer(null,1,2) intnumbers: Integer.
|
||||||
|
*
|
||||||
|
* {example} coalesce(null,13,null,2) # 13
|
||||||
|
*/
|
||||||
|
public static Integer coalesce(Integer ...intnumbers) {
|
||||||
|
for (Integer i : intnumbers) {
|
||||||
|
if (i != null) {
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* returns the first not null Integer
|
||||||
|
*
|
||||||
|
* {Category} NumberUtil
|
||||||
|
*
|
||||||
|
* {talendTypes} Float
|
||||||
|
*
|
||||||
|
* {param} Float(null,1,2) floatnumbers: Float.
|
||||||
|
*
|
||||||
|
* {example} coalesce(null,13,null,2) # 13
|
||||||
|
*/
|
||||||
|
public static Float coalesce(Float ...floatnumbers) {
|
||||||
|
for (Float f : floatnumbers) {
|
||||||
|
if (f != null) {
|
||||||
|
return f;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* returns the first not null Integer
|
||||||
|
*
|
||||||
|
* {Category} NumberUtil
|
||||||
|
*
|
||||||
|
* {talendTypes} Float
|
||||||
|
*
|
||||||
|
* {param} Double(null,1,2) double numbers: Double.
|
||||||
|
*
|
||||||
|
* {example} coalesce(null,13,null,2) # 13
|
||||||
|
*/
|
||||||
|
public static Double coalesce(Double ...floatnumbers) {
|
||||||
|
for (Double f : floatnumbers) {
|
||||||
|
if (f != null) {
|
||||||
|
return f;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* returns the first not null Long
|
||||||
|
*
|
||||||
|
* {Category} NumberUtil
|
||||||
|
*
|
||||||
|
* {talendTypes} Long
|
||||||
|
*
|
||||||
|
* {param} Long(null,2,2) longnumbers: Long.
|
||||||
|
*
|
||||||
|
* {example} coalesce(null,2,0,2) # 13
|
||||||
|
*/
|
||||||
|
public static Long coalesce(Long ...longnumbers) {
|
||||||
|
for (Long i : longnumbers) {
|
||||||
|
if (i != null) {
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* returns the first not null BigDecimal
|
||||||
|
*
|
||||||
|
* {Category} NumberUtil
|
||||||
|
*
|
||||||
|
* {talendTypes} BigDecimal
|
||||||
|
*
|
||||||
|
* {param} BigDecimal(null,1,2) numbers: BigDecimal.
|
||||||
|
*
|
||||||
|
* {example} coalesce(null,new BigDecimal("13"),null) # new BigDecimal("13")
|
||||||
|
*/
|
||||||
|
public static BigDecimal coalesce(BigDecimal ...numbers) {
|
||||||
|
for (BigDecimal i : numbers) {
|
||||||
|
if (i != null) {
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* return long and accepts Long, Double and String. Null returns 0
|
||||||
|
*
|
||||||
|
* {Category} NumberUtil
|
||||||
|
*
|
||||||
|
* {talendTypes} long | Long
|
||||||
|
*
|
||||||
|
* {param} Long(123) input: long format.
|
||||||
|
*
|
||||||
|
* {example} getNullSafeLong("123") result: 123 ...
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static long getNullSafeLong(Object input) {
|
||||||
|
if (input == null) {
|
||||||
|
return 0;
|
||||||
|
} else if (input instanceof Long) {
|
||||||
|
Long i = (Long) input;
|
||||||
|
if (i == 0) {
|
||||||
|
return 0;
|
||||||
|
} else {
|
||||||
|
return i.longValue();
|
||||||
|
}
|
||||||
|
} else if (input instanceof Double) {
|
||||||
|
long n = ((Double)input).longValue();
|
||||||
|
return n;
|
||||||
|
} else if (input instanceof String) {
|
||||||
|
String s = (String) input;
|
||||||
|
if (s.isEmpty()) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return Long.parseLong((String) s);
|
||||||
|
} else {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* return integer and accepts Integer, Double and String. Null returns 0
|
||||||
|
*
|
||||||
|
* {Category} NumberUtil
|
||||||
|
*
|
||||||
|
* {talendTypes} int | Integer
|
||||||
|
*
|
||||||
|
* {param} Integer(123) input: float pointing format.
|
||||||
|
*
|
||||||
|
* {example} getNullSafeInt("123") result: 123 ...
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static int getNullSafeInt(Object input) {
|
||||||
|
if (input == null) {
|
||||||
|
return 0;
|
||||||
|
} else if (input instanceof Integer) {
|
||||||
|
Integer i = (Integer) input;
|
||||||
|
if (i == 0) {
|
||||||
|
return 0;
|
||||||
|
} else {
|
||||||
|
return i.intValue();
|
||||||
|
}
|
||||||
|
} else if (input instanceof Double) {
|
||||||
|
int n = ((Double)input).intValue();
|
||||||
|
return n;
|
||||||
|
} else if (input instanceof String) {
|
||||||
|
String s = (String) input;
|
||||||
|
if (s.isEmpty()) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return Integer.parseInt((String) s);
|
||||||
|
} else {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* return integer and accepts Integer and String. Null returns 0
|
||||||
|
*
|
||||||
|
* {Category} NumberUtil
|
||||||
|
*
|
||||||
|
* {talendTypes} int | Integer
|
||||||
|
*
|
||||||
|
* {param} Integer(123) input: float pointing format.
|
||||||
|
*
|
||||||
|
* {example} getNullSafeInt("123", "replacements) result: 123 ...
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static int getNullSafeInt(Object input, String ...toRemoveString) {
|
||||||
|
if (input instanceof Integer) {
|
||||||
|
Integer i = (Integer) input;
|
||||||
|
if (i == 0) {
|
||||||
|
return 0;
|
||||||
|
} else {
|
||||||
|
return i.intValue();
|
||||||
|
}
|
||||||
|
} else if (input instanceof String) {
|
||||||
|
String s = (String) input;
|
||||||
|
if (toRemoveString != null) {
|
||||||
|
for (String r : toRemoveString) {
|
||||||
|
s = s.replace(r, "");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (s == null || s.isEmpty()) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return Integer.parseInt((String) s);
|
||||||
|
} else {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* return double and accepts Double and String. Null returns 0
|
||||||
|
*
|
||||||
|
* {Category} NumberUtil
|
||||||
|
*
|
||||||
|
* {talendTypes} double | Double
|
||||||
|
*
|
||||||
|
* {param} Double(123) input: float pointing format.
|
||||||
|
*
|
||||||
|
* {example} getNullSafeDouble("123") result: 123 ...
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static double getNullSafeDouble(String input) {
|
||||||
|
return getNullSafeDouble(input, Locale.US);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* return double and accepts Double and String. Null returns 0
|
||||||
|
*
|
||||||
|
* {Category} NumberUtil
|
||||||
|
*
|
||||||
|
* {talendTypes} double | Double
|
||||||
|
*
|
||||||
|
* {param} Double(123) input: float pointing format.
|
||||||
|
*
|
||||||
|
* {param} Object(java.util.Local) locale: Locale.
|
||||||
|
*
|
||||||
|
* {example} getNullSafeDouble("123") result: 123 ...
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static double getNullSafeDouble(String input, Locale locale) {
|
||||||
|
if (input != null && input.isEmpty() == false) {
|
||||||
|
input = input.replace("%", "").replace('"', ' ').trim();
|
||||||
|
if (input == null || input.isEmpty()) {
|
||||||
|
return 0d;
|
||||||
|
}
|
||||||
|
NumberFormat nf = NumberFormat.getInstance(locale);
|
||||||
|
Number n = null;
|
||||||
|
try {
|
||||||
|
n = nf.parse(input);
|
||||||
|
} catch (Exception e) {
|
||||||
|
return 0d;
|
||||||
|
}
|
||||||
|
return n.doubleValue();
|
||||||
|
} else {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static double getNullSafeDouble(Number input) {
|
||||||
|
if (input != null) {
|
||||||
|
return input.doubleValue();
|
||||||
|
} else {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* return double and accepts Double and String. Null returns 0
|
||||||
|
*
|
||||||
|
* {Category} NumberUtil
|
||||||
|
*
|
||||||
|
* {talendTypes} double | Double
|
||||||
|
*
|
||||||
|
* {param} Double(123) input: float pointing format.
|
||||||
|
*
|
||||||
|
* {example} getNullSafeDouble("123", "toRemoveString) result: 123 ...
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static double getNullSafeDouble(String input, String ...toRemoveString) {
|
||||||
|
return getNullSafeDoubleLocal(input, Locale.US, toRemoveString);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* return double and accepts Double and String. Null returns 0
|
||||||
|
*
|
||||||
|
* {Category} NumberUtil
|
||||||
|
*
|
||||||
|
* {talendTypes} double | Double
|
||||||
|
*
|
||||||
|
* {param} Double(123) input: float pointing format.
|
||||||
|
*
|
||||||
|
* {param} Object(java.util.Local) locale: Locale.
|
||||||
|
*
|
||||||
|
* {example} getNullSafeDoubleLocal("123", "toRemoveString) result: 123 ...
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static double getNullSafeDoubleLocal(String input, Locale locale, String ...toRemoveString) {
|
||||||
|
if (input != null && input.isEmpty() == false) {
|
||||||
|
String s = (String) input;
|
||||||
|
if (toRemoveString != null) {
|
||||||
|
for (String r : toRemoveString) {
|
||||||
|
s = s.replace(r, "");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return getNullSafeDouble(s, locale);
|
||||||
|
} else {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* return true if the first parameter is in the list of the following parameters
|
||||||
|
*
|
||||||
|
* {Category} NumberUtil
|
||||||
|
*
|
||||||
|
* {talendTypes} boolean | Boolean
|
||||||
|
*
|
||||||
|
* {param} int(99) test: int test value.
|
||||||
|
*
|
||||||
|
* {param} int(2,5,99,120) possibleValues: one or more int.
|
||||||
|
*
|
||||||
|
* {example} in(25,2,4,25,66) result: true
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static boolean in(Integer test, int ... possibleValues) {
|
||||||
|
if (test != null) {
|
||||||
|
for (int v : possibleValues) {
|
||||||
|
if (test.intValue() == v) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* build the sum of given Integers regardless of null
|
||||||
|
*
|
||||||
|
* {Category} NumberUtil
|
||||||
|
*
|
||||||
|
* {talendTypes} int | Integer
|
||||||
|
*
|
||||||
|
* {param} int(2,5,99,120) integers: integers to sum.
|
||||||
|
*
|
||||||
|
* {example} sum(25,2,4,25,66) result: 8889
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static int sum(Integer ...integers) {
|
||||||
|
int sum = 0;
|
||||||
|
for (Integer i : integers) {
|
||||||
|
if (i != null) {
|
||||||
|
sum = sum + i.intValue();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return sum;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* build the division of given Doubles regardless of null
|
||||||
|
*
|
||||||
|
* {Category} NumberUtil
|
||||||
|
*
|
||||||
|
* {talendTypes} double | Double
|
||||||
|
*
|
||||||
|
* {param} double(2.5) dividend: dividend
|
||||||
|
*
|
||||||
|
* {param} double(4.3) quotient: quotient
|
||||||
|
*
|
||||||
|
* {example} div(2,4) result: 0.5
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static double div(Double dividend, Double quotient) {
|
||||||
|
if (dividend == null || quotient == null) {
|
||||||
|
return 0;
|
||||||
|
} else {
|
||||||
|
if (quotient.doubleValue() == 0) {
|
||||||
|
return 0;
|
||||||
|
} else {
|
||||||
|
return dividend.doubleValue() / quotient.doubleValue();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* build the division of given Doubles regardless of null
|
||||||
|
*
|
||||||
|
* {Category} NumberUtil
|
||||||
|
*
|
||||||
|
* {talendTypes} double | Double
|
||||||
|
*
|
||||||
|
* {param} double(2.5) dividend: dividend
|
||||||
|
*
|
||||||
|
* {param} int(4.3) quotient: quotient
|
||||||
|
*
|
||||||
|
* {example} div(2,4) result: 0.5
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static double div(Double dividend, Integer quotient) {
|
||||||
|
if (dividend == null || quotient == null) {
|
||||||
|
return 0;
|
||||||
|
} else {
|
||||||
|
if (quotient.doubleValue() == 0) {
|
||||||
|
return 0;
|
||||||
|
} else {
|
||||||
|
return dividend.doubleValue() / (double) quotient.intValue();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* build the division of given Doubles regardless of null
|
||||||
|
*
|
||||||
|
* {Category} NumberUtil
|
||||||
|
*
|
||||||
|
* {talendTypes} double | Double
|
||||||
|
*
|
||||||
|
* {param} double(2.5) dividend: dividend
|
||||||
|
*
|
||||||
|
* {param} int(4.3) quotient: quotient
|
||||||
|
*
|
||||||
|
* {example} div(2,4) result: 0.5
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static double div(Integer dividend, Integer quotient) {
|
||||||
|
if (dividend == null || quotient == null) {
|
||||||
|
return 0;
|
||||||
|
} else {
|
||||||
|
if (quotient.doubleValue() == 0) {
|
||||||
|
return 0;
|
||||||
|
} else {
|
||||||
|
return (double) dividend.intValue() / quotient.intValue();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* build the division of given Doubles regardless of null
|
||||||
|
*
|
||||||
|
* {Category} NumberUtil
|
||||||
|
*
|
||||||
|
* {talendTypes} double | Double
|
||||||
|
*
|
||||||
|
* {param} double(2.5) dividend: dividend
|
||||||
|
*
|
||||||
|
* {param} int(4.3) quotient: quotient
|
||||||
|
*
|
||||||
|
* {example} div(2,4) result: 0.5
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static double div(Integer dividend, Double quotient) {
|
||||||
|
if (dividend == null || quotient == null) {
|
||||||
|
return 0;
|
||||||
|
} else {
|
||||||
|
if (quotient.doubleValue() == 0) {
|
||||||
|
return 0;
|
||||||
|
} else {
|
||||||
|
return ((double) dividend.intValue()) / quotient.doubleValue();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* formats the number
|
||||||
|
*
|
||||||
|
* {Category} NumberUtil
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {param} double(2.5) number: number to format
|
||||||
|
*
|
||||||
|
* {example} numberToPercent2Scale(0.12345) result: 12,34%
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static String formatToPercent2Scale(Double number) {
|
||||||
|
if (number != null) {
|
||||||
|
return formatToString(roundScale2(number * 100d)) + "%";
|
||||||
|
} else {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* formats the number
|
||||||
|
*
|
||||||
|
* {Category} NumberUtil
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {param} double(2.5) number: number to format
|
||||||
|
*
|
||||||
|
* {example} numberToString(1234.12345) result: "1.234,12345"
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static String formatToString(Double number) {
|
||||||
|
if (number != null) {
|
||||||
|
NumberFormat nf = NumberFormat.getInstance(Locale.GERMANY);
|
||||||
|
return nf.format(number);
|
||||||
|
} else {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* formats the number
|
||||||
|
*
|
||||||
|
* {Category} NumberUtil
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {param} double(2.5) number: number to format
|
||||||
|
*
|
||||||
|
* {example} numberToStringRoundScale2(1234.12345) result: "1.234,12"
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static String formatToStringRoundScale2(Double number) {
|
||||||
|
if (number != null) {
|
||||||
|
NumberFormat nf = NumberFormat.getInstance(Locale.GERMANY);
|
||||||
|
return nf.format(roundScale2(number));
|
||||||
|
} else {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* formats the number
|
||||||
|
*
|
||||||
|
* {Category} NumberUtil
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {param} double(2.5) number: number to format
|
||||||
|
*
|
||||||
|
* {example} numberToString(1234) result: 1.234
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static String formatToString(Integer number) {
|
||||||
|
if (number != null) {
|
||||||
|
NumberFormat nf = NumberFormat.getInstance(Locale.GERMANY);
|
||||||
|
return nf.format(number);
|
||||||
|
} else {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* rounds the number to a value with 2 decimal digits
|
||||||
|
*
|
||||||
|
* {Category} NumberUtil
|
||||||
|
*
|
||||||
|
* {talendTypes} double | Double
|
||||||
|
*
|
||||||
|
* {param} double(2.5) number: number to format
|
||||||
|
*
|
||||||
|
* {example} numberToString(1234.12345) result: 1234.12
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static Double roundScale2(Double number) {
|
||||||
|
if (number != null) {
|
||||||
|
return Math.round(number * 100d) / 100d;
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* rounds the number to a value with 2 decimal digits
|
||||||
|
*
|
||||||
|
* {Category} NumberUtil
|
||||||
|
*
|
||||||
|
* {talendTypes} BigDecimal
|
||||||
|
*
|
||||||
|
* {param} BigDecimal(2.5) number: number to format
|
||||||
|
*
|
||||||
|
* {example} formatToString(new BigDecimal("1234.12345"), 6) result: 1234.123450
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static String formatToString(BigDecimal number, int scale) {
|
||||||
|
return number.setScale(scale, BigDecimal.ROUND_HALF_DOWN).toPlainString();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* return float and accepts Float and String. Null returns 0
|
||||||
|
*
|
||||||
|
* {Category} NumberUtil
|
||||||
|
*
|
||||||
|
* {talendTypes} float | Float
|
||||||
|
*
|
||||||
|
* {param} Float(123) input: float pointing format.
|
||||||
|
*
|
||||||
|
* {example} getNullSafeFloat("123.4") result: 123.4 ...
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static float getNullSafeFloat(Object input) {
|
||||||
|
if (input instanceof Float) {
|
||||||
|
Float i = (Float) input;
|
||||||
|
if (i == 0) {
|
||||||
|
return 0;
|
||||||
|
} else {
|
||||||
|
return i.floatValue();
|
||||||
|
}
|
||||||
|
} else if (input instanceof String) {
|
||||||
|
String s = (String) input;
|
||||||
|
if (s == null || s.isEmpty()) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return Float.parseFloat((String) s);
|
||||||
|
} else {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* returns the given text as integer or 0 otherwise
|
||||||
|
*
|
||||||
|
* {Category} NumberUtil
|
||||||
|
*
|
||||||
|
* {talendTypes} int | Integer
|
||||||
|
*
|
||||||
|
* {param} String("123") text: string.
|
||||||
|
*
|
||||||
|
* {example} parseNullSafeInt("AB54") result: 0
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static int parseNullSafeInt(String text) {
|
||||||
|
try {
|
||||||
|
return Integer.parseInt(text);
|
||||||
|
} catch (Exception e) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* returns the given text as long or 0 otherwise
|
||||||
|
*
|
||||||
|
* {Category} NumberUtil
|
||||||
|
*
|
||||||
|
* {talendTypes} long | Long
|
||||||
|
*
|
||||||
|
* {param} String("123") text: string.
|
||||||
|
*
|
||||||
|
* {example} parseNullSafeLong("AB54") result: 0
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static long parseNullSafeLong(String text) {
|
||||||
|
try {
|
||||||
|
return Long.parseLong(text);
|
||||||
|
} catch (Exception e) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* returns true, if elements contains at least one an element x so that lowerBound <= x <= upperBound
|
||||||
|
*
|
||||||
|
* @param elements elements to check
|
||||||
|
* @param lowerBound lower range bound
|
||||||
|
* @param upperBound upper range bound
|
||||||
|
* @return see above
|
||||||
|
*/
|
||||||
|
public static boolean collectionElementInRange(Iterable<Integer> elements, int lowerBound, int upperBound) {
|
||||||
|
for(Integer i : elements)
|
||||||
|
if(lowerBound <= i && i <= upperBound)
|
||||||
|
return true;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
139
MDM-LCAG-JavaFunctions/src/main/java/routines/Numeric.java
Normal file
139
MDM-LCAG-JavaFunctions/src/main/java/routines/Numeric.java
Normal file
@@ -0,0 +1,139 @@
|
|||||||
|
// ============================================================================
|
||||||
|
//
|
||||||
|
// Copyright (c) 2006-2014, Talend Inc.
|
||||||
|
//
|
||||||
|
// This source code has been automatically generated by_Talend MDM Platform
|
||||||
|
// CodeGenerator version 7.3.1.20220805_1617-patch
|
||||||
|
// You can find more information about Talend products at www.talend.com.
|
||||||
|
// You may distribute this code under the terms of the GNU LGPL license
|
||||||
|
// http://www.gnu.org/licenses/lgpl.html).
|
||||||
|
//
|
||||||
|
// ============================================================================
|
||||||
|
package routines;
|
||||||
|
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
|
import routines.system.RandomUtils;
|
||||||
|
|
||||||
|
public class Numeric {
|
||||||
|
|
||||||
|
private static final java.util.Map<String, Integer> seq_Hash = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* return an incremented numeric id
|
||||||
|
*
|
||||||
|
* {talendTypes} int | Integer
|
||||||
|
*
|
||||||
|
* {Category} Numeric
|
||||||
|
*
|
||||||
|
* {param} string("s1") sequence identifier
|
||||||
|
*
|
||||||
|
* {param} int(1) start value
|
||||||
|
*
|
||||||
|
* {param} int(1) step
|
||||||
|
*
|
||||||
|
* {example} sequence("s1", 1, 1) # 1, 2, 3, ...
|
||||||
|
*
|
||||||
|
* {example} sequence("s2", 100, -2) # 100, 98, 96, ...
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static Integer sequence(String seqName, int startValue, int step) {
|
||||||
|
return seq_Hash.compute(seqName,
|
||||||
|
(String k, Integer v) -> v == null ? startValue : v + step);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* create a sequence if not exists and put a new startValue
|
||||||
|
*
|
||||||
|
* {Category} Numeric
|
||||||
|
*
|
||||||
|
* {param} string("s1") sequence identifier
|
||||||
|
*
|
||||||
|
* {param} int(1) start value
|
||||||
|
*
|
||||||
|
* {example} sequence("s1", 1)
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
public static void resetSequence(String seqName, int startValue) {
|
||||||
|
seq_Hash.put(seqName, startValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* remove a sequence
|
||||||
|
*
|
||||||
|
* {Category} Numeric
|
||||||
|
*
|
||||||
|
* {param} string("s1") sequence identifier
|
||||||
|
*
|
||||||
|
* {example} sequence("s1")
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
public static void removeSequence(String seqName) {
|
||||||
|
seq_Hash.remove(seqName);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* return a random int between min and max
|
||||||
|
*
|
||||||
|
* {Category} Numeric
|
||||||
|
*
|
||||||
|
* {talendTypes} int | Integer
|
||||||
|
*
|
||||||
|
* {param} int(0) min value
|
||||||
|
*
|
||||||
|
* {param} int(100) max value
|
||||||
|
*
|
||||||
|
* {example} random(3, 10) # 7, 4, 8, ...
|
||||||
|
*
|
||||||
|
* {example} random(0, 100) # 93, 12, 83, ...
|
||||||
|
*
|
||||||
|
* @throws RuntimeException in case when max value is smaller than min value
|
||||||
|
*/
|
||||||
|
public static Integer random(Integer min, Integer max) {
|
||||||
|
if (max < min) {
|
||||||
|
throw new RuntimeException("Max value should be bigger than min value");
|
||||||
|
}
|
||||||
|
return ((Long) Math.round(min - 0.5 + (RandomUtils.random() * (max - min + 1)))).intValue();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* return numbers using an implied decimal format.
|
||||||
|
*
|
||||||
|
* {Category} Numeric
|
||||||
|
*
|
||||||
|
* {talendTypes} float | Float
|
||||||
|
*
|
||||||
|
* {param} String("9V99") format: float pointing format.
|
||||||
|
*
|
||||||
|
* {param} String("123") toConvert: read this value.
|
||||||
|
*
|
||||||
|
* {example} convertImpliedDecimalFormat("9V99", "123") result: 1.23 ...
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static Float convertImpliedDecimalFormat(String format, String toConvert) {
|
||||||
|
BigDecimal decimal = Numeric.convertString2BigDecimal(format,toConvert);
|
||||||
|
return new Float(decimal.doubleValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static BigDecimal convertString2BigDecimal(String format, String toConvert) {
|
||||||
|
long decimalPlace = 1;
|
||||||
|
int indexOf = format.indexOf('V');
|
||||||
|
if (indexOf > -1) {
|
||||||
|
boolean isV = false;
|
||||||
|
for (int i = 0; i < format.length(); i++) {
|
||||||
|
char charAt = format.charAt(i);
|
||||||
|
if (charAt == '9' && isV) {
|
||||||
|
decimalPlace = 10 * decimalPlace;
|
||||||
|
} else if (charAt == 'V') {
|
||||||
|
isV = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
BigDecimal decimal = new BigDecimal(toConvert);
|
||||||
|
decimal = decimal.divide(new BigDecimal(decimalPlace));
|
||||||
|
return decimal;
|
||||||
|
}
|
||||||
|
}
|
||||||
267
MDM-LCAG-JavaFunctions/src/main/java/routines/PasswordUtil.java
Normal file
267
MDM-LCAG-JavaFunctions/src/main/java/routines/PasswordUtil.java
Normal file
@@ -0,0 +1,267 @@
|
|||||||
|
package routines;
|
||||||
|
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.security.Key;
|
||||||
|
|
||||||
|
import javax.crypto.Cipher;
|
||||||
|
import javax.crypto.CipherInputStream;
|
||||||
|
import javax.crypto.CipherOutputStream;
|
||||||
|
import javax.crypto.KeyGenerator;
|
||||||
|
import javax.crypto.spec.SecretKeySpec;
|
||||||
|
|
||||||
|
//import sun.misc.BASE64Decoder;
|
||||||
|
//import sun.misc.BASE64Encoder;
|
||||||
|
import java.util.Base64;
|
||||||
|
|
||||||
|
public class PasswordUtil {
|
||||||
|
|
||||||
|
public static void uncryptContextPwds(java.util.Properties c) {
|
||||||
|
java.util.Enumeration<?> en = c.propertyNames();
|
||||||
|
String salt = c.getProperty("conf_rootDataDirectory");
|
||||||
|
String cryptedPwd = "";
|
||||||
|
while (en.hasMoreElements()) {
|
||||||
|
String key = (String) en.nextElement();
|
||||||
|
if (key.contains("Password")) {
|
||||||
|
// c.setProperty(key,PasswordUtil.unCrypt(c.getProperty(key)));
|
||||||
|
cryptedPwd = c.getProperty(key);
|
||||||
|
cryptedPwd = cryptedPwd.replace("@", "=");
|
||||||
|
c.setProperty(key, decrypt(cryptedPwd, salt));
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
publishContext(c);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void publishContext(java.util.Properties context) {
|
||||||
|
java.lang.reflect.Field[] declaredFields = context.getClass()
|
||||||
|
.getDeclaredFields();
|
||||||
|
for (java.lang.reflect.Field contextField : declaredFields) {
|
||||||
|
for (java.util.Map.Entry<?, ?> propertyEntry : context.entrySet()) {
|
||||||
|
if (isSecuredProperty(contextField.getName()) &&
|
||||||
|
contextField.getName().equals(propertyEntry.getKey())) {
|
||||||
|
try {
|
||||||
|
contextField.set(context, propertyEntry.getValue());
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (IllegalAccessException e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// crypt modul
|
||||||
|
private static String getSalt(String saltstr){
|
||||||
|
return (saltstr+"919ebba032181dacaa1849192").substring(0, 16);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String decrypt(String text, String saltstr) {
|
||||||
|
return decrypt("##nopropertyname##", text, saltstr);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static String decrypt(String propertyName, String text, String saltstr) {
|
||||||
|
String result = null;
|
||||||
|
saltstr=getSalt(saltstr);
|
||||||
|
try {
|
||||||
|
// zufaelligen Schluessel erzeugen
|
||||||
|
KeyGenerator keygen = KeyGenerator.getInstance("AES");
|
||||||
|
keygen.init(128);
|
||||||
|
// SecretKey aesKey = keygen.generateKey();
|
||||||
|
byte[] salt = saltstr.getBytes();
|
||||||
|
Key aesKey = new SecretKeySpec(salt, 0, 16, "AES");
|
||||||
|
|
||||||
|
// Klasse erzeugen
|
||||||
|
EasyCrypt ec = new EasyCrypt(aesKey, "AES");
|
||||||
|
|
||||||
|
// Text verschlüsseln
|
||||||
|
result = ec.decrypt(text);
|
||||||
|
} catch (Exception e) {
|
||||||
|
// e.printStackTrace();
|
||||||
|
// Im Fehlerfall soll keine Aenderung des Eingabestrings erfolgen und wir melden das an die Konsole
|
||||||
|
result = text;
|
||||||
|
if(!"##nopropertyname##".equals(propertyName)) System.err.println("Unsecured property found:'"+propertyName+"'");
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String encrypt(String text, String saltstr) {
|
||||||
|
String result = null;
|
||||||
|
saltstr=getSalt(saltstr);
|
||||||
|
try {
|
||||||
|
// zufaelligen Schluessel erzeugen
|
||||||
|
KeyGenerator keygen = KeyGenerator.getInstance("AES");
|
||||||
|
keygen.init(128);
|
||||||
|
// SecretKey aesKey = keygen.generateKey();
|
||||||
|
byte[] salt = saltstr.getBytes();
|
||||||
|
Key aesKey = new SecretKeySpec(salt, 0, 16, "AES");
|
||||||
|
// Klasse erzeugen
|
||||||
|
EasyCrypt ec = new EasyCrypt(aesKey, "AES");
|
||||||
|
|
||||||
|
// Text verschlüsseln
|
||||||
|
result = ec.encrypt(text);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean isSecuredProperty(String key) { //Extend this method to add more Patterns for Password properties
|
||||||
|
return key.contains("Password");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
private static class EasyCrypt {
|
||||||
|
|
||||||
|
private Key key = null;
|
||||||
|
private String verfahren = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Key
|
||||||
|
* verwendeter Schluessel
|
||||||
|
* @param verfahren
|
||||||
|
* bestimmt das verwendete Verschluesselungsverfahren "RSA",
|
||||||
|
* "AES", ....
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public EasyCrypt(Key k, String verfahren) throws Exception {
|
||||||
|
this.key = k;
|
||||||
|
this.verfahren = verfahren;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Verschluesselt einen Outputstream
|
||||||
|
*
|
||||||
|
* @param os
|
||||||
|
* Klartext-Outputstream
|
||||||
|
* @return verschluesselter Outputstream
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public OutputStream encryptOutputStream(OutputStream os) throws Exception {
|
||||||
|
// integritaet pruefen
|
||||||
|
valid();
|
||||||
|
|
||||||
|
// eigentliche Nachricht mit RSA verschluesseln
|
||||||
|
Cipher cipher = Cipher.getInstance(verfahren);
|
||||||
|
cipher.init(Cipher.ENCRYPT_MODE, key);
|
||||||
|
os = new CipherOutputStream(os, cipher);
|
||||||
|
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Entschluesselt einen Inputstream
|
||||||
|
*
|
||||||
|
* @param is
|
||||||
|
* verschluesselter Inputstream
|
||||||
|
* @return Klartext-Inputstream
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public InputStream decryptInputStream(InputStream is) throws Exception {
|
||||||
|
// integritaet pruefen
|
||||||
|
valid();
|
||||||
|
|
||||||
|
// Daten mit AES entschluesseln
|
||||||
|
Cipher cipher = Cipher.getInstance(verfahren);
|
||||||
|
cipher.init(Cipher.DECRYPT_MODE, key);
|
||||||
|
is = new CipherInputStream(is, cipher);
|
||||||
|
|
||||||
|
return is;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Verschluesselt einen Text in BASE64
|
||||||
|
*
|
||||||
|
* @param text
|
||||||
|
* Klartext
|
||||||
|
* @return BASE64 String
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public String encrypt(String text) throws Exception {
|
||||||
|
// integritaet pruefen
|
||||||
|
valid();
|
||||||
|
|
||||||
|
// Verschluesseln
|
||||||
|
Cipher cipher = Cipher.getInstance(verfahren);
|
||||||
|
cipher.init(Cipher.ENCRYPT_MODE, key);
|
||||||
|
byte[] encrypted = cipher.doFinal(text.getBytes());
|
||||||
|
|
||||||
|
// bytes zu Base64-String konvertieren
|
||||||
|
String geheim = Base64.getEncoder().encodeToString(encrypted);
|
||||||
|
//java.util.Base64.Encoder myEncoder = new java.util.Base64.Encoder();
|
||||||
|
//String geheim = myEncoder.encode(encrypted);
|
||||||
|
|
||||||
|
return geheim;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Entschluesselt einen BASE64 kodierten Text
|
||||||
|
*
|
||||||
|
* @param geheim
|
||||||
|
* BASE64 kodierter Text
|
||||||
|
* @return Klartext
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public String decrypt(String geheim) throws Exception {
|
||||||
|
// integritaet pruefen
|
||||||
|
valid();
|
||||||
|
|
||||||
|
// BASE64 String zu Byte-Array
|
||||||
|
//BASE64Decoder myDecoder = new BASE64Decoder();
|
||||||
|
//byte[] crypted = myDecoder.decodeBuffer(geheim);
|
||||||
|
byte[] crypted = Base64.getDecoder().decode(geheim);
|
||||||
|
|
||||||
|
// entschluesseln
|
||||||
|
Cipher cipher = Cipher.getInstance(verfahren);
|
||||||
|
cipher.init(Cipher.DECRYPT_MODE, key);
|
||||||
|
byte[] cipherData = cipher.doFinal(crypted);
|
||||||
|
return new String(cipherData);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ++++++++++++++++++++++++++++++
|
||||||
|
// Validierung
|
||||||
|
// ++++++++++++++++++++++++++++++
|
||||||
|
|
||||||
|
private boolean valid() throws Exception {
|
||||||
|
if (verfahren == null) {
|
||||||
|
throw new NullPointerException("Kein Verfahren angegeben!");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (key == null) {
|
||||||
|
throw new NullPointerException("Keinen Key angegeben!");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (verfahren.isEmpty()) {
|
||||||
|
throw new NullPointerException("Kein Verfahren angegeben!");
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ++++++++++++++++++++++++++++++
|
||||||
|
// Getter und Setter
|
||||||
|
// ++++++++++++++++++++++++++++++
|
||||||
|
|
||||||
|
public Key getKey() {
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setKey(Key key) {
|
||||||
|
this.key = key;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getVerfahren() {
|
||||||
|
return verfahren;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setVerfahren(String verfahren) {
|
||||||
|
this.verfahren = verfahren;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,68 @@
|
|||||||
|
// ============================================================================
|
||||||
|
//
|
||||||
|
// Copyright (c) 2006-2014, Talend Inc.
|
||||||
|
//
|
||||||
|
// This source code has been automatically generated by_Talend MDM Platform
|
||||||
|
// CodeGenerator version 7.3.1.20220805_1617-patch
|
||||||
|
// You can find more information about Talend products at www.talend.com.
|
||||||
|
// You may distribute this code under the terms of the GNU LGPL license
|
||||||
|
// http://www.gnu.org/licenses/lgpl.html).
|
||||||
|
//
|
||||||
|
// ============================================================================
|
||||||
|
package routines;
|
||||||
|
|
||||||
|
public class Relational {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Indicates when a variable is the null value.
|
||||||
|
*
|
||||||
|
* {talendTypes} boolean | Boolean
|
||||||
|
*
|
||||||
|
* {Category} Relational
|
||||||
|
*
|
||||||
|
* {param} Object(null)
|
||||||
|
*
|
||||||
|
* {example} ISNULL(null)
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static boolean ISNULL(Object variable) {
|
||||||
|
return variable == null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the complement of the logical value of an expression.
|
||||||
|
*
|
||||||
|
* {talendTypes} boolean | Boolean
|
||||||
|
*
|
||||||
|
* {Category} Relational
|
||||||
|
*
|
||||||
|
* {param} boolean(true)
|
||||||
|
*
|
||||||
|
* {example} NOT(false)
|
||||||
|
*/
|
||||||
|
public static boolean NOT(boolean expression) {
|
||||||
|
return !expression;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Indicates when a variable is the null value.
|
||||||
|
*
|
||||||
|
* @param value :the object need to judge.
|
||||||
|
*
|
||||||
|
* @return 1 if value == null, 0 if not.
|
||||||
|
*
|
||||||
|
* {Category} Relational
|
||||||
|
*
|
||||||
|
* {param} Object(null)
|
||||||
|
*
|
||||||
|
* {example} ISNULL(null)
|
||||||
|
*/
|
||||||
|
public static int isNull(Object value) {
|
||||||
|
if (value == null) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
184
MDM-LCAG-JavaFunctions/src/main/java/routines/SQLike.java
Normal file
184
MDM-LCAG-JavaFunctions/src/main/java/routines/SQLike.java
Normal file
@@ -0,0 +1,184 @@
|
|||||||
|
// ============================================================================
|
||||||
|
//
|
||||||
|
// Copyright (c) 2006-2014, Talend Inc.
|
||||||
|
//
|
||||||
|
// This source code has been automatically generated by_Talend MDM Platform
|
||||||
|
// CodeGenerator version 7.3.1.20220805_1617-patch
|
||||||
|
// You can find more information about Talend products at www.talend.com.
|
||||||
|
// You may distribute this code under the terms of the GNU LGPL license
|
||||||
|
// http://www.gnu.org/licenses/lgpl.html).
|
||||||
|
//
|
||||||
|
// ============================================================================
|
||||||
|
package routines;
|
||||||
|
|
||||||
|
public class SQLike {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get the part of String, reference MySQL: mid
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} SQLike
|
||||||
|
*
|
||||||
|
* {param} string(sourceString) inStr : inputting String
|
||||||
|
*
|
||||||
|
* {param} int(startLocation) beginIndex : the location where to start, if it is negative, just count from tail
|
||||||
|
*
|
||||||
|
* {example} mid(",1,2,3,4,", 3) # ",2,3,4,"
|
||||||
|
*
|
||||||
|
* {example} mid(",1,2,3,4,", -3) # ",4,"
|
||||||
|
*/
|
||||||
|
public static String mid(String inStr, int beginIndex) {
|
||||||
|
if (inStr == null || "".equals(inStr)) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
return mid(inStr, beginIndex, inStr.length());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get the part of String, reference MySQL: mid
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} SQLike
|
||||||
|
*
|
||||||
|
* {param} string(sourceString) inStr : inputting String
|
||||||
|
*
|
||||||
|
* {param} int(startLocation) beginIndex : the location where to start, if it is negative, just count from tail
|
||||||
|
*
|
||||||
|
* {param} int(characterCount) count : number of returning chars. if bigger than length of String, will get the part
|
||||||
|
* from beginIndex to tail-end
|
||||||
|
*
|
||||||
|
* {example} mid(",1,2,3,4,", 3, 2) # ",2"
|
||||||
|
*
|
||||||
|
* {example} mid(",1,2,3,4,", -3, 2) # ",4"
|
||||||
|
*/
|
||||||
|
|
||||||
|
public static String mid(String inStr, int beginIndex, int count) {
|
||||||
|
if (inStr == null || "".equals(inStr)) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
int len = inStr.length();
|
||||||
|
int absBeginIndex = Math.abs(beginIndex);
|
||||||
|
|
||||||
|
if (beginIndex == 0 || absBeginIndex > len || count <= 0) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
int start = 0, end = 0;
|
||||||
|
|
||||||
|
if (beginIndex > 0) {
|
||||||
|
start = beginIndex - 1;
|
||||||
|
} else {
|
||||||
|
start = len - absBeginIndex;
|
||||||
|
}
|
||||||
|
end = start + count;
|
||||||
|
|
||||||
|
if (end > len)
|
||||||
|
end = len;
|
||||||
|
return inStr.substring(start, end);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get the part of String by specify the delimiter, reference MySQL: substring_index
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} SQLike
|
||||||
|
*
|
||||||
|
* {param} string(sourceString) inStr : inputting String
|
||||||
|
*
|
||||||
|
* {param} string(delimiter) delimiter : specify the delimiter
|
||||||
|
*
|
||||||
|
* {param} int(count) count : if it is positive, count from beginning, return the front of the matching delimiter, <br>
|
||||||
|
* if it is negative, count from tail, return the after of the matching delimiter, <br>
|
||||||
|
* if it is zero, return empty
|
||||||
|
*
|
||||||
|
* {example} mid_index(",1,2,3,4,", ",", 3) # ",1,2"
|
||||||
|
*
|
||||||
|
* {example} mid_index(",1,2,3,4,", ",", -3) # "3,4,"
|
||||||
|
*
|
||||||
|
* {example} mid_index(",1,2,3,4,", ",", 100) # ",1,2,3,4,"
|
||||||
|
*/
|
||||||
|
public static String mid_index(String inStr, String delimiter, int count) {
|
||||||
|
|
||||||
|
if (inStr == null || "".equals(inStr)) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
if (count == 0) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
if (inStr.indexOf(delimiter) == -1) {
|
||||||
|
return inStr;
|
||||||
|
}
|
||||||
|
|
||||||
|
java.util.List<String> tmpList = new java.util.ArrayList<String>();
|
||||||
|
String tmp = inStr, pi = "";
|
||||||
|
int tmp_end = 0;
|
||||||
|
|
||||||
|
while (!"".equals(tmp)) {
|
||||||
|
tmp_end = tmp.indexOf(delimiter);
|
||||||
|
|
||||||
|
if (tmp_end != -1) {
|
||||||
|
pi = tmp.substring(0, tmp_end);
|
||||||
|
tmp = tmp.substring(tmp_end + 1);
|
||||||
|
} else {
|
||||||
|
pi = tmp;
|
||||||
|
tmp = "";
|
||||||
|
}
|
||||||
|
tmpList.add(pi);
|
||||||
|
}
|
||||||
|
|
||||||
|
// add last part ""
|
||||||
|
if (inStr.endsWith(delimiter))
|
||||||
|
tmpList.add("");
|
||||||
|
|
||||||
|
int actualCount = tmpList.size();
|
||||||
|
int absCount = Math.abs(count);
|
||||||
|
|
||||||
|
if (absCount > actualCount)
|
||||||
|
absCount = actualCount;
|
||||||
|
|
||||||
|
int start_index = 0, end_index = 0;
|
||||||
|
|
||||||
|
if (count > 0) {
|
||||||
|
start_index = 0;
|
||||||
|
end_index = absCount - 1;
|
||||||
|
} else {
|
||||||
|
start_index = actualCount - absCount;
|
||||||
|
end_index = actualCount - 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
String retStr = "";
|
||||||
|
|
||||||
|
for (int i = start_index; i <= end_index; i++) {
|
||||||
|
if (i != start_index)
|
||||||
|
retStr = retStr.concat(delimiter);
|
||||||
|
|
||||||
|
retStr = retStr.concat(tmpList.get(i));
|
||||||
|
}
|
||||||
|
return retStr;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* DOC ytao Comment method "main".
|
||||||
|
*
|
||||||
|
* @param args
|
||||||
|
*/
|
||||||
|
// public static void main(String[] args) {
|
||||||
|
// // TODO Auto-generated method stub
|
||||||
|
// String in = "";
|
||||||
|
//
|
||||||
|
// // mid
|
||||||
|
// in = "12345678";
|
||||||
|
// System.out.println("mid in: " + in);
|
||||||
|
// System.out.println("mid return: " + mid(in, 2, 10));
|
||||||
|
//
|
||||||
|
// // mid_index
|
||||||
|
// in = "12,3,...,.";
|
||||||
|
// System.out.println("=======\nmid_index in: " + in);
|
||||||
|
// System.out.println("mid_index return: " + mid_index(in, ",", -1));
|
||||||
|
//
|
||||||
|
// }
|
||||||
|
}
|
||||||
@@ -0,0 +1,778 @@
|
|||||||
|
// ============================================================================
|
||||||
|
//
|
||||||
|
// Copyright (c) 2006-2014, Talend Inc.
|
||||||
|
//
|
||||||
|
// This source code has been automatically generated by_Talend MDM Platform
|
||||||
|
// CodeGenerator version 7.3.1.20220805_1617-patch
|
||||||
|
// You can find more information about Talend products at www.talend.com.
|
||||||
|
// You may distribute this code under the terms of the GNU LGPL license
|
||||||
|
// http://www.gnu.org/licenses/lgpl.html).
|
||||||
|
//
|
||||||
|
// ============================================================================
|
||||||
|
package routines;
|
||||||
|
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
|
||||||
|
public class StringHandling {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determines whether the expression is sorted alphabetically or not.
|
||||||
|
*
|
||||||
|
* {talendTypes} boolean | Boolean
|
||||||
|
*
|
||||||
|
* {Category} StringHandling
|
||||||
|
*
|
||||||
|
* {param} string("abcdefg") input: String need to be known whether is sorted alphabetically or not.
|
||||||
|
*
|
||||||
|
* {example} ALPHA("abcdefg") # true
|
||||||
|
*/
|
||||||
|
public static boolean ALPHA(String input) {
|
||||||
|
if (input != null) {
|
||||||
|
char[] val = input.toCharArray();
|
||||||
|
|
||||||
|
for (int i = 0; i < val.length - 1; i++) {
|
||||||
|
if (val[i] > val[i + 1]) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determines whether the expression is an alphabetic or nonalphabetic.
|
||||||
|
*
|
||||||
|
* {talendTypes} boolean | Boolean
|
||||||
|
*
|
||||||
|
* {Category} StringHandling
|
||||||
|
*
|
||||||
|
* {param} string("abc") input: String need to be known whether is an alphabetic or not.
|
||||||
|
*
|
||||||
|
* {example} ALPHA("abc") # true
|
||||||
|
*/
|
||||||
|
public static boolean IS_ALPHA(String input) {
|
||||||
|
if (input != null) {
|
||||||
|
char[] val = input.toCharArray();
|
||||||
|
|
||||||
|
for (int i = 0; i < val.length; i++) {
|
||||||
|
if (!Character.isLetter(val[i])) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Substitutes all substrings that match the given regular expression in the given old string with the given replacement and returns a new string.
|
||||||
|
* @param oldStr: the old string.
|
||||||
|
* @param regex: the regular expression to match.
|
||||||
|
* @param replacement: the string to be substituted for every match.
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} StringHandling
|
||||||
|
*
|
||||||
|
* {param} string("hello world!") oldStr: The whole string.
|
||||||
|
*
|
||||||
|
* {param} string("world") regex: Regx.
|
||||||
|
*
|
||||||
|
* {param} string("guy") replacement: Replacement.
|
||||||
|
*
|
||||||
|
* {example} CHANGE("hello world!","world","guy") # hello world
|
||||||
|
*/
|
||||||
|
public static String CHANGE(String oldStr, String regex, String replacement) {
|
||||||
|
if (oldStr == null || regex == null || replacement == null)
|
||||||
|
return oldStr;
|
||||||
|
else
|
||||||
|
return oldStr.replaceAll(regex, replacement);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Evaluates the number of times a substring is repeated in a string.
|
||||||
|
*
|
||||||
|
* {talendTypes} int | Integer
|
||||||
|
*
|
||||||
|
* {Category} StringHandling
|
||||||
|
*
|
||||||
|
* {param} string("hello world!") string: The whole string.
|
||||||
|
*
|
||||||
|
* {param} string("world") subString: subString.
|
||||||
|
*
|
||||||
|
* {example} COUNT("hello world!","world") # 1
|
||||||
|
*/
|
||||||
|
public static int COUNT(String string, String subString) {
|
||||||
|
if (string == null || subString == null){
|
||||||
|
return 0;
|
||||||
|
} else{
|
||||||
|
int counter = 0;
|
||||||
|
int i = -1;
|
||||||
|
while ((i = string.indexOf(subString, i + 1)) != -1) {
|
||||||
|
counter++;
|
||||||
|
}
|
||||||
|
return counter;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts all uppercase letters in an expression to lowercase.
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} StringHandling
|
||||||
|
*
|
||||||
|
* {param} string("Hello") string: String
|
||||||
|
*
|
||||||
|
* {example} DOWNCASE("Hello") # hello
|
||||||
|
*/
|
||||||
|
public static String DOWNCASE(String string) {
|
||||||
|
return string == null ? null : string.toLowerCase();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts all lowercase letters in an expression to uppercase.
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} StringHandling
|
||||||
|
*
|
||||||
|
* {param} string("Hello") string: String
|
||||||
|
*
|
||||||
|
* {example} UPCASE("Hello") # HELLO
|
||||||
|
*/
|
||||||
|
public static String UPCASE(String string) {
|
||||||
|
return string == null ? null : string.toUpperCase();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Encloses an expression in double quotation marks.
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} StringHandling
|
||||||
|
*
|
||||||
|
* {param} string("Hello") string: String
|
||||||
|
*
|
||||||
|
* {example} DQUOTE("hello") # "hello"
|
||||||
|
*/
|
||||||
|
public static String DQUOTE(String string) {
|
||||||
|
return string == null ? null : ("\"" + string + "\""); //$NON-NLS-1$ //$NON-NLS-2$
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Substitutes all substrings that match the given regular expression in the given old string with the given replacement and returns a new string.
|
||||||
|
* @param oldStr: the old string.
|
||||||
|
* @param regex: the regular expression to match.
|
||||||
|
* @param replacement: the string to be substituted for every match.
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} StringHandling
|
||||||
|
*
|
||||||
|
* {param} string("hello world!") oldStr: The whole string.
|
||||||
|
*
|
||||||
|
* {param} string("world") regex: Regx.
|
||||||
|
*
|
||||||
|
* {param} string("guy") replacement: Replacement.
|
||||||
|
*
|
||||||
|
* {example} EREPLACE("hello world!","world","guy") # hello world
|
||||||
|
*/
|
||||||
|
public static String EREPLACE(String oldStr, String regex, String replacement) {
|
||||||
|
return CHANGE(oldStr, regex, replacement);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the starting column position of a specified occurrence of a particular substring within a string
|
||||||
|
* expression.
|
||||||
|
*
|
||||||
|
* {talendTypes} int | Integer
|
||||||
|
*
|
||||||
|
* {Category} StringHandling
|
||||||
|
*
|
||||||
|
* {param} string("hello world!") string: string.
|
||||||
|
*
|
||||||
|
* {param} string("hello") element: element
|
||||||
|
*
|
||||||
|
* {example} INDEX("hello world!","hello") # 0
|
||||||
|
*/
|
||||||
|
public static int INDEX(String string, String element) {
|
||||||
|
if (string == null || element == null)
|
||||||
|
return -1;
|
||||||
|
else
|
||||||
|
return string.indexOf(element);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Specifies a substring consisting of the first n characters of a string.
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} StringHandling
|
||||||
|
*
|
||||||
|
* {param} string("hello world!") string: String.
|
||||||
|
*
|
||||||
|
* {param} int(5) index : index
|
||||||
|
*
|
||||||
|
* {example} LEFT("hello world!",5) # hello
|
||||||
|
*/
|
||||||
|
public static String LEFT(String string, int index) {
|
||||||
|
return string == null ? null : string.substring(0, Math.min(string.length(), index));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Specifies a substring consisting of the last n characters of a string.
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} StringHandling
|
||||||
|
*
|
||||||
|
* {param} string("hello world!") string: String
|
||||||
|
*
|
||||||
|
* {param} int(6) index : Index
|
||||||
|
*
|
||||||
|
* {example} RIGHT("hello world!",6) # world!
|
||||||
|
*/
|
||||||
|
public static String RIGHT(String string, int index) {
|
||||||
|
return string == null ? null : string.substring(string.length() - Math.min(string.length(), index));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculates the length of a string.
|
||||||
|
*
|
||||||
|
* {talendTypes} int | Integer
|
||||||
|
*
|
||||||
|
* {Category} StringHandling
|
||||||
|
*
|
||||||
|
* {param} string("hello world!") string:
|
||||||
|
*
|
||||||
|
* {example} LEN("hello world!") # 12
|
||||||
|
*/
|
||||||
|
public static int LEN(String string) {
|
||||||
|
return string == null ? -1 : string.length();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generates a string consisting of a specified number of blank spaces.
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} StringHandling
|
||||||
|
*
|
||||||
|
* {param} int(2) i: amount of blank space need to generate.
|
||||||
|
*
|
||||||
|
* {example} SPACE(2) # " "
|
||||||
|
*/
|
||||||
|
public static String SPACE(int i) {
|
||||||
|
StringBuffer buffer = new StringBuffer();
|
||||||
|
for (int j = 0; j < i; j++) {
|
||||||
|
buffer.append(" "); //$NON-NLS-1$
|
||||||
|
}
|
||||||
|
return buffer.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Encloses an expression in single quotation marks.
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} StringHandling
|
||||||
|
*
|
||||||
|
* {param} string("hellow world!") string:
|
||||||
|
*
|
||||||
|
* {example} SQUOTE("hellow world!") # 'hello world!'
|
||||||
|
*/
|
||||||
|
public static String SQUOTE(String string) {
|
||||||
|
return string == null ? null : ("'" + string + "'"); //$NON-NLS-1$ //$NON-NLS-2$
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generates a particular character string a specified number of times.
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} StringHandling
|
||||||
|
*
|
||||||
|
* {param} string('a') string: character
|
||||||
|
*
|
||||||
|
* {param} int(6) int: times
|
||||||
|
*
|
||||||
|
* {example} SPACE("hellow world!",2) # hello world!
|
||||||
|
*/
|
||||||
|
public static String STR(char letter, int i) {
|
||||||
|
StringBuffer buffer = new StringBuffer();
|
||||||
|
for (int j = 0; j < i; j++) {
|
||||||
|
buffer.append(letter);
|
||||||
|
}
|
||||||
|
return buffer.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deletes extra blank spaces and tabs from a character string.
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} StringHandling
|
||||||
|
*
|
||||||
|
* {param} string(" hellow world! ") string: string.
|
||||||
|
*
|
||||||
|
* {example} TRIM(" hellow world! ") # hello world!
|
||||||
|
*/
|
||||||
|
public static String TRIM(String string) {
|
||||||
|
return string == null ? null : string.trim();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deletes all blank spaces and tabs after the last nonblank character in an expression.
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} StringHandling
|
||||||
|
*
|
||||||
|
* {param} string("hellow world! ") string: string.
|
||||||
|
*
|
||||||
|
* {example} BTRIM("hellow world! ") # helloworld!
|
||||||
|
*/
|
||||||
|
public static String BTRIM(String string) {
|
||||||
|
if (string == null){
|
||||||
|
return null;
|
||||||
|
} else {
|
||||||
|
char[] val = string.toCharArray();
|
||||||
|
int len = val.length;
|
||||||
|
while (len > 0 && val[len - 1] <= ' ') {
|
||||||
|
len--;
|
||||||
|
}
|
||||||
|
return string.substring(0, len);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deletes all blank spaces and tabs up to the first nonblank character in an expression.
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} StringHandling
|
||||||
|
*
|
||||||
|
* {param} string(" hellow world!") string: string.
|
||||||
|
*
|
||||||
|
* {example} FTRIM(" hellow world!") # hello world!
|
||||||
|
*/
|
||||||
|
public static String FTRIM(String string) {
|
||||||
|
if (string == null){
|
||||||
|
return null;
|
||||||
|
} else {
|
||||||
|
char[] val = string.toCharArray();
|
||||||
|
int st = 0;
|
||||||
|
int len = val.length;
|
||||||
|
while ((st < len) && (val[st] <= ' ')) {
|
||||||
|
st++;
|
||||||
|
}
|
||||||
|
return string.substring(st);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param string : Passes the strings you want to search.
|
||||||
|
* @param start : Must be an integer. The position in the string where you want to start counting.
|
||||||
|
* @param length : Must be an integer greater than 0. The number of characters you want SUBSTR to return
|
||||||
|
* @return
|
||||||
|
* {example} SUBSTR("This is a test.",1,5) #his i
|
||||||
|
*/
|
||||||
|
public static String SUBSTR(String string, int start, Integer length) {
|
||||||
|
|
||||||
|
if (isVacant(string)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
if (start > string.length()) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
if (start > 0) {
|
||||||
|
start--;
|
||||||
|
} else if (start < 0) {
|
||||||
|
start = string.length() + start;
|
||||||
|
if (start < 0) {
|
||||||
|
start = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
String result;
|
||||||
|
if (length == null || (length + start) > string.length()) {
|
||||||
|
result = string.substring(start);
|
||||||
|
} else if (length <= 0) {
|
||||||
|
return "";
|
||||||
|
} else {
|
||||||
|
result = string.substring(start, length + start);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param value : Any string value. Passes the strings you want to modify.
|
||||||
|
* @param trim_set : Any string value. Passes the characters you want to remove from the end of the string.
|
||||||
|
* @return
|
||||||
|
* {example} LTRIM("aatestaa","a") #testaa
|
||||||
|
*/
|
||||||
|
public static String LTRIM(String value, String trim_set) {
|
||||||
|
if (isVacant(value)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
int len = value.length();
|
||||||
|
int st = 0;
|
||||||
|
char[] val = value.toCharArray();
|
||||||
|
if (isVacant(trim_set)) {
|
||||||
|
|
||||||
|
while ((st < len) && (val[st] <= ' ')) {
|
||||||
|
st++;
|
||||||
|
}
|
||||||
|
return st > 0 ? value.substring(st) : value;
|
||||||
|
} else {
|
||||||
|
char[] chars = trim_set.toCharArray();
|
||||||
|
do {
|
||||||
|
for (char c : chars) {
|
||||||
|
for (; value.indexOf(c, st) == st; st++);
|
||||||
|
}
|
||||||
|
if(st==len){
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
}while(trim_set.contains(String.valueOf(value.charAt(st))));
|
||||||
|
|
||||||
|
return st > 0 ? value.substring(st) : value;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String LTRIM(String value) {
|
||||||
|
return LTRIM(value, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param value : Any string value. Passes the strings you want to modify.
|
||||||
|
* @param trim_set : Any string value. Passes the characters you want to remove from the beginning of the first string
|
||||||
|
* @return
|
||||||
|
* {example} RTRIM("aatestaa","a") #aatest
|
||||||
|
*/
|
||||||
|
public static String RTRIM(String value, String trim_set) {
|
||||||
|
if (isVacant(value)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
int len = value.length();
|
||||||
|
char[] val = value.toCharArray();
|
||||||
|
if (isVacant(trim_set)) {
|
||||||
|
|
||||||
|
while ((0 < len) && (val[len - 1] <= ' ')) {
|
||||||
|
len--;
|
||||||
|
}
|
||||||
|
return len < value.length() ? value.substring(0, len) : value;
|
||||||
|
} else {
|
||||||
|
|
||||||
|
char[] chars = trim_set.toCharArray();
|
||||||
|
|
||||||
|
do {
|
||||||
|
for (int i = chars.length-1;i>=0;i--) {
|
||||||
|
for (; value.lastIndexOf(chars[i]) == len-1; len--){
|
||||||
|
if(len==0){
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
value = value.substring(0, len-1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}while(trim_set.contains(value.substring(value.length()-1)));
|
||||||
|
|
||||||
|
return value;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String RTRIM(String value) {
|
||||||
|
return RTRIM(value, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param first_string : The strings you want to change.
|
||||||
|
* @param length : Must be a positive integer literal. Specifies the length you want each string to be.
|
||||||
|
* @param second_string : Can be any string value. The characters you want to append to the left-side of the first_string values.
|
||||||
|
* @return
|
||||||
|
* {example} LPAD("test",6,"a") #aatest
|
||||||
|
*/
|
||||||
|
public static String LPAD(String first_string, int length, String second_string) {
|
||||||
|
|
||||||
|
if (isVacant(first_string) || length < 1) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
int OriginLength = first_string.length();
|
||||||
|
if (OriginLength >= length) {
|
||||||
|
return first_string;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isVacant(second_string)) {
|
||||||
|
StringBuilder result = new StringBuilder();
|
||||||
|
for (int i = OriginLength; i < length; i++) {
|
||||||
|
result.append(" ");
|
||||||
|
}
|
||||||
|
result.append(first_string);
|
||||||
|
return result.toString();
|
||||||
|
} else {
|
||||||
|
StringBuilder result = new StringBuilder(second_string);
|
||||||
|
for (int len = result.length(); len < length - OriginLength; result.append(second_string))
|
||||||
|
len = result.length();
|
||||||
|
|
||||||
|
first_string = result.substring(0, length - OriginLength)+first_string;
|
||||||
|
return first_string;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String LPAD(String first_string, int length) {
|
||||||
|
return LPAD(first_string, length, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param first_string : The strings you want to change.
|
||||||
|
* @param length : Must be a positive integer literal. Specifies the length you want each string to be.
|
||||||
|
* @param second_string : Any string value. Passes the string you want to append to the right-side of the first_string values.
|
||||||
|
* @return
|
||||||
|
* {example} RPAD("test",6,"a") #testaa
|
||||||
|
*/
|
||||||
|
public static String RPAD(String first_string, int length, String second_string) {
|
||||||
|
|
||||||
|
if (isVacant(first_string) || length < 1) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
int OriginLength = first_string.length();
|
||||||
|
if (OriginLength >= length) {
|
||||||
|
return first_string;
|
||||||
|
}
|
||||||
|
StringBuilder result = new StringBuilder(first_string);
|
||||||
|
for (int i = OriginLength; i < length; i++) {
|
||||||
|
if (isVacant(second_string)) {
|
||||||
|
result.append(" ");
|
||||||
|
} else {
|
||||||
|
result.append(second_string);
|
||||||
|
if(result.length()>length){
|
||||||
|
return result.toString().substring(0, length);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String RPAD(String first_string, int length) {
|
||||||
|
return RPAD(first_string, length, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param string Can be a String
|
||||||
|
* @param search_value Can be a String
|
||||||
|
* @param start
|
||||||
|
* The default is 1, meaning that INSTR starts the search at the first character in the string.
|
||||||
|
* @param occurrence
|
||||||
|
* If the search value appears more than once in the string, you can specify which occurrence you want to search for.
|
||||||
|
* @param comparison_type
|
||||||
|
* default 0: INSTR performs a linguistic string comparison. 1: INSTR performs a binary string comparison.
|
||||||
|
* @return
|
||||||
|
* {example} new StringHandling<String>.INSTR("This is a test","t",1,2,0) #14
|
||||||
|
*/
|
||||||
|
|
||||||
|
public static Integer INSTR(String string, String search_value, Integer start, Integer occurrence) {
|
||||||
|
// linguistic string comparison.
|
||||||
|
|
||||||
|
int defaultStart = 1;
|
||||||
|
int defaultOccurrence = 1;
|
||||||
|
Integer result = 0;
|
||||||
|
|
||||||
|
if (isVacant(string) || isVacant(search_value)|| Math.abs(defaultStart) >= string.length()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (start != null && start != 0) {
|
||||||
|
defaultStart = start;
|
||||||
|
}
|
||||||
|
if (occurrence != null) {
|
||||||
|
if (occurrence <= 0) {
|
||||||
|
throw new IllegalArgumentException(
|
||||||
|
"The occurrence argument can only accept a positive integer greater than 0.");
|
||||||
|
}
|
||||||
|
defaultOccurrence = occurrence;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (defaultStart < 0) {
|
||||||
|
string = string.substring(0, string.length() + defaultStart + 1);
|
||||||
|
int temp = string.lastIndexOf(search_value);
|
||||||
|
while (temp != -1 && defaultOccurrence != 1) {
|
||||||
|
string = string.substring(0, temp);
|
||||||
|
defaultOccurrence--;
|
||||||
|
temp = string.lastIndexOf(search_value);
|
||||||
|
}
|
||||||
|
return ++temp;
|
||||||
|
|
||||||
|
} else {
|
||||||
|
string = string.substring(defaultStart - 1);
|
||||||
|
|
||||||
|
if (defaultOccurrence != 1) {
|
||||||
|
int temp;
|
||||||
|
do {
|
||||||
|
temp = string.indexOf(search_value) + 1;
|
||||||
|
string = string.substring(temp);
|
||||||
|
result += temp;
|
||||||
|
defaultOccurrence--;
|
||||||
|
} while (defaultOccurrence != 0);
|
||||||
|
if (temp == 0) {
|
||||||
|
result = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
result = string.indexOf(search_value) + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param string Can be a byte[]
|
||||||
|
* @param search_value Can be byte[]
|
||||||
|
* @param start
|
||||||
|
* The default is 1, meaning that INSTR starts the search at the first character in the string.
|
||||||
|
* @param occurrence
|
||||||
|
* If the search value appears more than once in the string, you can specify which occurrence you want to search for.
|
||||||
|
* @param comparison_type
|
||||||
|
* default 0: INSTR performs a linguistic string comparison. 1: INSTR performs a binary string comparison.
|
||||||
|
* @return
|
||||||
|
* {example} new StringHandling<String>.INSTR("This is a test".getBytes(),"t".getBytes(),1,2,0) #14
|
||||||
|
*/
|
||||||
|
|
||||||
|
public static Integer INSTR(byte[] string, byte[] search_value, Integer start, Integer occurrence) {
|
||||||
|
|
||||||
|
int defaultStart = 1;
|
||||||
|
int defaultOccurrence = 1;
|
||||||
|
|
||||||
|
// binary string comparison
|
||||||
|
if (string == null || search_value == null || Math.abs(defaultStart) >= string.length) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
if (start != null && start != 0) {
|
||||||
|
defaultStart = start;
|
||||||
|
}
|
||||||
|
int max = string.length - 1;
|
||||||
|
|
||||||
|
if (occurrence != null) {
|
||||||
|
if (occurrence <= 0) {
|
||||||
|
throw new IllegalArgumentException(
|
||||||
|
"The occurrence argument can only accept a positive integer greater than 0.");
|
||||||
|
}
|
||||||
|
defaultOccurrence = occurrence;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (defaultStart > 0) {
|
||||||
|
return byteINSTR(string, search_value, defaultStart, defaultOccurrence);
|
||||||
|
} else {
|
||||||
|
int total = string.length;
|
||||||
|
byte[] revers = new byte[total];
|
||||||
|
byte[] revers_search = new byte[search_value.length];
|
||||||
|
for (int i = 0; i < total; i++)
|
||||||
|
revers[max - i] = string[i];
|
||||||
|
for (int i = 0; i < search_value.length; i++)
|
||||||
|
revers_search[search_value.length - 1 - i] = search_value[i];
|
||||||
|
defaultStart = -defaultStart;
|
||||||
|
int result = byteINSTR(revers, revers_search, defaultStart, defaultOccurrence);
|
||||||
|
if (result == 0) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return total - result + 2 - search_value.length;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int byteINSTR(byte[] string, byte[] search_value, int defaultStart, int defaultOccurrence) {
|
||||||
|
int max = string.length - 1;
|
||||||
|
for (int i = defaultStart - 1; i <= max; i++) {
|
||||||
|
/* Look for first character. */
|
||||||
|
if (string[i] != search_value[0]) {
|
||||||
|
while (++i <= max && string[i] != search_value[0])
|
||||||
|
;
|
||||||
|
}
|
||||||
|
if (i > max) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if (search_value.length > 1) {
|
||||||
|
|
||||||
|
/* Found first character, now look at the rest of v2 */
|
||||||
|
if (i <= max) {
|
||||||
|
int j = i;
|
||||||
|
int end = i + search_value.length - 1;
|
||||||
|
for (int k = 1; j + 1 <= max && k < search_value.length
|
||||||
|
&& string[j + 1] == search_value[k]; j++, k++)
|
||||||
|
;
|
||||||
|
|
||||||
|
if (j == end) {
|
||||||
|
/* Found whole string. */
|
||||||
|
if (defaultOccurrence == 1) {
|
||||||
|
return i - defaultStart + 2;
|
||||||
|
} else {
|
||||||
|
defaultOccurrence--;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (defaultOccurrence == 1) {
|
||||||
|
return i - defaultStart + 2;
|
||||||
|
} else {
|
||||||
|
defaultOccurrence--;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static boolean isVacant(String value) {
|
||||||
|
return value == null || "".equals(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param numeric_value
|
||||||
|
* Numeric datatype. The numeric value you want to convert to a
|
||||||
|
* string
|
||||||
|
* @return String. NULL if a value passed to the function is NULL.
|
||||||
|
*
|
||||||
|
* Converts double values to strings of up to 16 digits and provides
|
||||||
|
* accuracy up to 15 digits. If you pass a number with more than 15
|
||||||
|
* digits, TO_CHAR rounds the number to the sixteenth digit.
|
||||||
|
* Returns decimal notation for numbers in the ranges (-1e16,-1e-16]
|
||||||
|
* and [1e-16, 1e16). TO_CHAR returns scientific notation for
|
||||||
|
* numbers outside these ranges.
|
||||||
|
*/
|
||||||
|
|
||||||
|
public static String TO_CHAR(Object numeric_value) {
|
||||||
|
if(numeric_value==null){
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
BigDecimal bigDecimal = new BigDecimal(numeric_value.toString());
|
||||||
|
if(bigDecimal.abs().compareTo(new BigDecimal("1e16"))<0&&bigDecimal.abs().compareTo(new BigDecimal("1e-16"))>0){
|
||||||
|
return bigDecimal.toPlainString();
|
||||||
|
}
|
||||||
|
|
||||||
|
return numeric_value.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
1188
MDM-LCAG-JavaFunctions/src/main/java/routines/StringUtil.java
Normal file
1188
MDM-LCAG-JavaFunctions/src/main/java/routines/StringUtil.java
Normal file
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,64 @@
|
|||||||
|
package routines;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* user specification: the function's comment should contain keys as follows: 1. write about the function's comment.but
|
||||||
|
* it must be before the "{talendTypes}" key.
|
||||||
|
*
|
||||||
|
* 2. {talendTypes} 's value must be talend Type, it is required . its value should be one of: String, char | Character,
|
||||||
|
* long | Long, int | Integer, boolean | Boolean, byte | Byte, Date, double | Double, float | Float, Object, short |
|
||||||
|
* Short
|
||||||
|
*
|
||||||
|
* 3. {Category} define a category for the Function. it is required. its value is user-defined .
|
||||||
|
*
|
||||||
|
* 4. {param} 's format is: {param} <type>[(<default value or closed list values>)] <name>[ : <comment>]
|
||||||
|
*
|
||||||
|
* <type> 's value should be one of: string, int, list, double, object, boolean, long, char, date. <name>'s value is the
|
||||||
|
* Function's parameter name. the {param} is optional. so if you the Function without the parameters. the {param} don't
|
||||||
|
* added. you can have many parameters for the Function.
|
||||||
|
*
|
||||||
|
* 5. {example} gives a example for the Function. it is optional.
|
||||||
|
*/
|
||||||
|
public class SystemEnvironmentUtil {
|
||||||
|
|
||||||
|
private static final String DELIMITER = "___";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* helloExample: not return value, only print "hello" + message.
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} SystemEnvironmentUtil
|
||||||
|
*
|
||||||
|
* {param} string(jobName) input: The string need to be printed.
|
||||||
|
*
|
||||||
|
* {example} getCoreJobName("jobToBeDone#init") # jobToBeDone !.
|
||||||
|
*/
|
||||||
|
public static String getJobPackageName(String jobName) {
|
||||||
|
int pos = jobName.lastIndexOf(DELIMITER);
|
||||||
|
if (pos > 0) {
|
||||||
|
return jobName.substring(0, pos);
|
||||||
|
} else {
|
||||||
|
return jobName;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public static void checkPrerequisits(java.util.Properties context)
|
||||||
|
{
|
||||||
|
// put given time into calender with environment time zone
|
||||||
|
java.util.Calendar envCalendar = java.util.Calendar.getInstance();
|
||||||
|
|
||||||
|
// bail out when job is not running in UTC,
|
||||||
|
// since this would result in wrong toString function of sql components
|
||||||
|
java.util.TimeZone envZone = envCalendar.getTimeZone();
|
||||||
|
if (!"UTC".equals(envZone.getID())) {
|
||||||
|
throw new RuntimeException("JVM is not in UTC time zone. Please add -Duser.timezone=UTC to JVM parameters of job.") ;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean isWindows()
|
||||||
|
{
|
||||||
|
return (System.getProperty("os.name").toLowerCase().indexOf("win") >= 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,125 @@
|
|||||||
|
// ============================================================================
|
||||||
|
//
|
||||||
|
// Copyright (c) 2006-2014, Talend Inc.
|
||||||
|
//
|
||||||
|
// This source code has been automatically generated by_Talend MDM Platform
|
||||||
|
// CodeGenerator version 7.3.1.20220805_1617-patch
|
||||||
|
// You can find more information about Talend products at www.talend.com.
|
||||||
|
// You may distribute this code under the terms of the GNU LGPL license
|
||||||
|
// http://www.gnu.org/licenses/lgpl.html).
|
||||||
|
//
|
||||||
|
// ============================================================================
|
||||||
|
package routines;
|
||||||
|
|
||||||
|
import routines.system.RandomUtils;
|
||||||
|
|
||||||
|
public class TalendDataGenerator {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} TalendDataGenerator
|
||||||
|
*
|
||||||
|
* {example} getFirstName() # Bill.
|
||||||
|
*/
|
||||||
|
public static String getFirstName() {
|
||||||
|
String[] list = { "Abraham", "Andrew", "Benjamin", "Bill", "Calvin", "Chester", "Dwight", "Franklin", "George", "Gerald", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$ //$NON-NLS-10$
|
||||||
|
"Grover", "Harry", "Herbert", "James", "Jimmy", "John", "Lyndon", "Martin", "Millard", "Richard", "Ronald", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$ //$NON-NLS-10$ //$NON-NLS-11$
|
||||||
|
"Rutherford", "Theodore", "Thomas", "Ulysses", "Warren", "William", "Woodrow", "Zachary" }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$
|
||||||
|
Integer random = 0 + ((Long) Math.round(RandomUtils.random() * (list.length - 1 - 0))).intValue();
|
||||||
|
return list[random];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} TalendDataGenerator
|
||||||
|
*
|
||||||
|
* {example} getLastName() # Johnson.
|
||||||
|
*/
|
||||||
|
public static String getLastName() {
|
||||||
|
String[] list = { "Lincoln", "Jackson", "Johnson", "Harrison", "Clinton", "Coolidge", "Arthur", "Eisenhower", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$
|
||||||
|
"Roosevelt", "Pierce", "Washington", "Ford", "Cleveland", "Truman", "Hoover", "Garfield", "Buchanan", "Polk", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$ //$NON-NLS-10$
|
||||||
|
"Madison", "Monroe", "Carter", "Adams", "Kennedy", "Quincy", "Adams", "Tyler", "Johnson", "Van Buren", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$ //$NON-NLS-10$
|
||||||
|
"Fillmore", "Nixon", "Reagan", "Hayes", "Roosevelt", "Jefferson", "Grant", "Harding", "Harrison", "Taft", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$ //$NON-NLS-10$
|
||||||
|
"McKinley", "Wilson", "Taylor" }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
|
||||||
|
Integer random = 0 + ((Long) Math.round(RandomUtils.random() * (list.length - 1 - 0))).intValue();
|
||||||
|
return list[random];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} TalendDataGenerator
|
||||||
|
*
|
||||||
|
* {example} getUsStreet() # Corona Del Mar.
|
||||||
|
*/
|
||||||
|
public static String getUsStreet() {
|
||||||
|
String[] list = { "Apalachee Parkway", "Bailard Avenue", "Bayshore Freeway", "Bowles Avenue", "Burnett Road", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
|
||||||
|
"Cabrillo Highway", "Calle Real", "Camelback Rd", "Carpinteria Avenue", "Carpinteria North", "Carpinteria South", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$
|
||||||
|
"Castillo Drive", "Cerrillos Road", "Cleveland Ave.", "Corona Del Mar", "E Fowler Avenue", "East 1st Street", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$
|
||||||
|
"East Calle Primera", "East Fry Blvd.", "East Main Street", "El Camino Real", "Erringer Road", "Fairview Avenue", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$
|
||||||
|
"Fontaine Road", "French Camp Turnpike Road", "Grandview Drive", "Greenwood Road", "Harbor Dr", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
|
||||||
|
"Harry S Truman Blvd", "Hutchinson Rd", "Jean de la Fontaine", "Jones Road", "Katella Avenue", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
|
||||||
|
"Lake Tahoe Blvd.", "Lawrenceville Suwanee", "Lindbergh Blvd", "Milpas Street", "Monroe Street", "Moreno Drive", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$
|
||||||
|
"N Harrison St", "N Kentwood", "Newbury Road", "North Atherton Street", "North Broadway Street", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
|
||||||
|
"North Erringer Road", "North Preisker Lane", "North Ventu Park Road", "Pacific Hwy S", "Padre Boulevard", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
|
||||||
|
"Redwood Highway", "Richmond Hill", "S Rustle St", "San Diego Freeway", "San Luis Obispo North", "San Marcos", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$
|
||||||
|
"San Simeon", "San Ysidro Blvd", "Santa Ana Freeway", "Santa Monica Road", "Santa Rosa North", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
|
||||||
|
"Santa Rosa South", "South Highway", "South Roosevelt Drive", "Steele Lane", "Tanger Blvd", "Timberlane Drive", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$
|
||||||
|
"Tully Road East", "Via Real", "W. Russell St.", "Westside Freeway", "Woodson Rd." }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
|
||||||
|
Integer random = 0 + ((Long) Math.round(RandomUtils.random() * (list.length - 1 - 0))).intValue();
|
||||||
|
return list[random];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} TalendDataGenerator
|
||||||
|
*
|
||||||
|
* {example} getUsCity() # Sacramento.
|
||||||
|
*/
|
||||||
|
public static String getUsCity() {
|
||||||
|
String[] list = { "Montgomery", "Juneau", "Phoenix", "Little Rock", "Sacramento", "Raleigh", "Columbia", "Denver", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$
|
||||||
|
"Hartford", "Bismarck", "Pierre", "Dover", "Tallahassee", "Atlanta", "Honolulu", "Boise", "Springfield", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$
|
||||||
|
"Indianapolis", "Des Moines", "Topeka", "Frankfort", "Baton Rouge", "Augusta", "Annapolis", "Boston", "Lansing", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$
|
||||||
|
"Saint Paul", "Jackson", "Jefferson City", "Helena", "Lincoln", "Carson City", "Concord", "Trenton", "Santa Fe", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$
|
||||||
|
"Albany", "Columbus", "Oklahoma City", "Salem", "Harrisburg", "Providence", "Nashville", "Austin", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$
|
||||||
|
"Salt Lake City", "Montpelier", "Richmond", "Charleston", "Olympia", "Madison", "Cheyenne" }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$
|
||||||
|
Integer random = 0 + ((Long) Math.round(RandomUtils.random() * (list.length - 1 - 0))).intValue();
|
||||||
|
return list[random];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} TalendDataGenerator
|
||||||
|
*
|
||||||
|
* {example} getUsState() # California.
|
||||||
|
*/
|
||||||
|
public static String getUsState() {
|
||||||
|
String[] list = { "Alabama", "Alaska", "Arizona", "Arkansas", "California", "North Carolina", "South Carolina", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$
|
||||||
|
"Colorado", "Connecticut", "North Dakota", "South Dakota", "Delaware", "Florida", "Georgia", "Hawaii", "Idaho", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$
|
||||||
|
"Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$
|
||||||
|
"Michigan", "Minnesota", "Mississippi", "Missouri", "Montana", "Nebraska", "Nevada", "New Hampshire", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$
|
||||||
|
"New Jersey", "New Mexico", "New York", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$
|
||||||
|
"Tennessee", "Texas", "Utah", "Vermont", "Virginia", "West Virginia", "Washington", "Wisconsin", "Wyoming" }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$
|
||||||
|
Integer random = 0 + ((Long) Math.round(RandomUtils.random() * (list.length - 1 - 0))).intValue();
|
||||||
|
return list[random];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} TalendDataGenerator
|
||||||
|
*
|
||||||
|
* {example} getUsStateId() # CA.
|
||||||
|
*/
|
||||||
|
public static String getUsStateId() {
|
||||||
|
String[] list = { "AL", "AK", "AZ", "AR", "CA", "NC", "SC", "CO", "CT", "ND", "SD", "DE", "FL", "GA", "HI", "ID", "IL", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$ //$NON-NLS-10$ //$NON-NLS-11$ //$NON-NLS-12$ //$NON-NLS-13$ //$NON-NLS-14$ //$NON-NLS-15$ //$NON-NLS-16$ //$NON-NLS-17$
|
||||||
|
"IN", "IA", "KS", "KY", "LA", "ME", "MD", "MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH", "NJ", "NM", "NY", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$ //$NON-NLS-10$ //$NON-NLS-11$ //$NON-NLS-12$ //$NON-NLS-13$ //$NON-NLS-14$ //$NON-NLS-15$ //$NON-NLS-16$ //$NON-NLS-17$ //$NON-NLS-18$ //$NON-NLS-19$
|
||||||
|
"OH", "OK", "OR", "PA", "RI", "TN", "TX", "UT", "VT", "VA", "WV", "WA", "WI", "WY" }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$ //$NON-NLS-10$ //$NON-NLS-11$ //$NON-NLS-12$ //$NON-NLS-13$ //$NON-NLS-14$
|
||||||
|
Integer random = 0 + ((Long) Math.round(RandomUtils.random() * (list.length - 1 - 0))).intValue();
|
||||||
|
return list[random];
|
||||||
|
}
|
||||||
|
}
|
||||||
1513
MDM-LCAG-JavaFunctions/src/main/java/routines/TalendDate.java
Normal file
1513
MDM-LCAG-JavaFunctions/src/main/java/routines/TalendDate.java
Normal file
File diff suppressed because it is too large
Load Diff
325
MDM-LCAG-JavaFunctions/src/main/java/routines/TalendString.java
Normal file
325
MDM-LCAG-JavaFunctions/src/main/java/routines/TalendString.java
Normal file
@@ -0,0 +1,325 @@
|
|||||||
|
// ============================================================================
|
||||||
|
//
|
||||||
|
// Copyright (c) 2006-2014, Talend Inc.
|
||||||
|
//
|
||||||
|
// This source code has been automatically generated by_Talend MDM Platform
|
||||||
|
// CodeGenerator version 7.3.1.20220805_1617-patch
|
||||||
|
// You can find more information about Talend products at www.talend.com.
|
||||||
|
// You may distribute this code under the terms of the GNU LGPL license
|
||||||
|
// http://www.gnu.org/licenses/lgpl.html).
|
||||||
|
//
|
||||||
|
// ============================================================================
|
||||||
|
package routines;
|
||||||
|
|
||||||
|
import java.security.SecureRandom;
|
||||||
|
import java.util.Vector;
|
||||||
|
|
||||||
|
public class TalendString {
|
||||||
|
|
||||||
|
/** Index of the first accent character **/
|
||||||
|
private static final int MIN = 192;
|
||||||
|
|
||||||
|
/** Index of the last accent character **/
|
||||||
|
private static final int MAX = 255;
|
||||||
|
|
||||||
|
/** used to save the link between with or without accents **/
|
||||||
|
private static final Vector map = initMap();
|
||||||
|
|
||||||
|
public static Vector getMap() {
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* return Replace the special character(e.g. <,>,& etc) within a string for XML file.
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} TalendString
|
||||||
|
*
|
||||||
|
* {param} string("") input: The string with the special character(s) need to be replaced.
|
||||||
|
*
|
||||||
|
* {example} replaceSpecialCharForXML("<title>Empire <>Burlesque</title>") # <title>Empire <>Burlesque</title>
|
||||||
|
*/
|
||||||
|
public static String replaceSpecialCharForXML(String input) {
|
||||||
|
input = input.replaceAll("&", "&"); //$NON-NLS-1$ //$NON-NLS-2$
|
||||||
|
input = input.replaceAll("<", "<"); //$NON-NLS-1$ //$NON-NLS-2$
|
||||||
|
input = input.replaceAll(">", ">"); //$NON-NLS-1$ //$NON-NLS-2$
|
||||||
|
input = input.replaceAll("'", "'"); //$NON-NLS-1$ //$NON-NLS-2$
|
||||||
|
input = input.replaceAll("\"", """); //$NON-NLS-1$ //$NON-NLS-2$
|
||||||
|
return input;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* check CDATA for xml
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} TalendString
|
||||||
|
*
|
||||||
|
* {param} string("") input: the CDATA format data to be checked.
|
||||||
|
*/
|
||||||
|
public static String checkCDATAForXML(String input) {
|
||||||
|
if (input.startsWith("<![CDATA[") && input.endsWith("]]>")) { //$NON-NLS-1$ //$NON-NLS-2$
|
||||||
|
return input;
|
||||||
|
} else {
|
||||||
|
return TalendString.replaceSpecialCharForXML(input);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* getAsciiRandomString : Return a randomly generated String
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} TalendString
|
||||||
|
*
|
||||||
|
* {param} int(6) length: length of the String to return
|
||||||
|
*
|
||||||
|
* {example} getAsciiRandomString(6) # Art34Z
|
||||||
|
*/
|
||||||
|
public static String getAsciiRandomString(int length) {
|
||||||
|
SecureRandom random = new SecureRandom();
|
||||||
|
int cnt = 0;
|
||||||
|
StringBuilder builder = new StringBuilder();
|
||||||
|
char ch;
|
||||||
|
int end = 'z' + 1;
|
||||||
|
int start = ' ';
|
||||||
|
while (cnt < length) {
|
||||||
|
ch = (char) (random.nextInt(end - start) + start);
|
||||||
|
if (Character.isLetterOrDigit(ch)) {
|
||||||
|
builder.append(ch);
|
||||||
|
cnt++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return builder.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* talendTrim: Returns a copy of the string, with leading and trailing specified char omitted.
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} TalendString
|
||||||
|
*
|
||||||
|
* {param} string("") origin: The original string need to be trimed.
|
||||||
|
*
|
||||||
|
* {param} char(' ') padding_char: The padding char for triming.
|
||||||
|
*
|
||||||
|
* {param} int(0) align: The alignment of the content in the original string. Positive int for right, negative int
|
||||||
|
* for left and zero for center. Positive integer to trim the left part, zero to trim both the left and the right part, negative to trim the right part.
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* {example} talendTrim("$$talend open studio$$$$", '$', 0) # talend open studio
|
||||||
|
*/
|
||||||
|
public static String talendTrim(String origin, char padding_char, int align) {
|
||||||
|
if (null == origin) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
String sPaddingChar = quoteChars(Character.toString(padding_char));
|
||||||
|
|
||||||
|
if (align > 0) {// positive integer to trim left
|
||||||
|
origin = origin.replaceAll("^" + sPaddingChar + "+", "");
|
||||||
|
} else if (align == 0) {// zero to trim both left and right
|
||||||
|
origin = origin.replaceAll("^" + sPaddingChar + "+", "");
|
||||||
|
origin = origin.replaceAll(sPaddingChar + "+$", "");
|
||||||
|
} else if (align < 0) {// negative integer to trim right
|
||||||
|
origin = origin.replaceAll(sPaddingChar + "+$", "");
|
||||||
|
}
|
||||||
|
|
||||||
|
return origin;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String quoteChars( String padding_chars){
|
||||||
|
StringBuffer sb = new StringBuffer();
|
||||||
|
for (int i = 0; i < padding_chars.length(); i++) {
|
||||||
|
char c = padding_chars.charAt(i);
|
||||||
|
if (!(Character.isLetterOrDigit(c) || c == '_')) {
|
||||||
|
sb.append('\\');
|
||||||
|
sb.append(c);
|
||||||
|
} else {
|
||||||
|
sb.append(c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* self definition:escape char ,for example -->'\\'
|
||||||
|
*/
|
||||||
|
public static String addEscapeChars(String padding_chars,char escapeChar){
|
||||||
|
StringBuffer sb = new StringBuffer();
|
||||||
|
for (int i = 0; i < padding_chars.length(); i++) {
|
||||||
|
char c = padding_chars.charAt(i);
|
||||||
|
if (!(Character.isLetterOrDigit(c) || c == '_'||Character.isWhitespace(c))) {
|
||||||
|
sb.append(escapeChar);
|
||||||
|
sb.append(c);
|
||||||
|
} else {
|
||||||
|
sb.append(c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initialisation of the map for the accents.
|
||||||
|
*/
|
||||||
|
private static Vector initMap() {
|
||||||
|
Vector result = new Vector();
|
||||||
|
String car = null;
|
||||||
|
|
||||||
|
car = new String("A"); //$NON-NLS-1$
|
||||||
|
result.add(car); /* '\u00C0' alt-0192 */
|
||||||
|
result.add(car); /* '\u00C1' alt-0193 */
|
||||||
|
result.add(car); /* '\u00C2' alt-0194 */
|
||||||
|
result.add(car); /* '\u00C3' alt-0195 */
|
||||||
|
result.add(car); /* '\u00C4' alt-0196 */
|
||||||
|
result.add(car); /* '\u00C5' alt-0197 */
|
||||||
|
car = new String("AE"); //$NON-NLS-1$
|
||||||
|
result.add(car); /* '\u00C6' alt-0198 */
|
||||||
|
car = new String("C"); //$NON-NLS-1$
|
||||||
|
result.add(car); /* '\u00C7' alt-0199 */
|
||||||
|
car = new String("E"); //$NON-NLS-1$
|
||||||
|
result.add(car); /* '\u00C8' alt-0200 */
|
||||||
|
result.add(car); /* '\u00C9' alt-0201 */
|
||||||
|
result.add(car); /* '\u00CA' alt-0202 */
|
||||||
|
result.add(car); /* '\u00CB' alt-0203 */
|
||||||
|
car = new String("I"); //$NON-NLS-1$
|
||||||
|
result.add(car); /* '\u00CC' alt-0204 */
|
||||||
|
result.add(car); /* '\u00CD' alt-0205 */
|
||||||
|
result.add(car); /* '\u00CE' alt-0206 */
|
||||||
|
result.add(car); /* '\u00CF' alt-0207 */
|
||||||
|
car = new String("D"); //$NON-NLS-1$
|
||||||
|
result.add(car); /* '\u00D0' alt-0208 */
|
||||||
|
car = new String("N"); //$NON-NLS-1$
|
||||||
|
result.add(car); /* '\u00D1' alt-0209 */
|
||||||
|
car = new String("O"); //$NON-NLS-1$
|
||||||
|
result.add(car); /* '\u00D2' alt-0210 */
|
||||||
|
result.add(car); /* '\u00D3' alt-0211 */
|
||||||
|
result.add(car); /* '\u00D4' alt-0212 */
|
||||||
|
result.add(car); /* '\u00D5' alt-0213 */
|
||||||
|
result.add(car); /* '\u00D6' alt-0214 */
|
||||||
|
car = new String("*"); //$NON-NLS-1$
|
||||||
|
result.add(car); /* '\u00D7' alt-0215 */
|
||||||
|
car = new String("0"); //$NON-NLS-1$
|
||||||
|
result.add(car); /* '\u00D8' alt-0216 */
|
||||||
|
car = new String("U"); //$NON-NLS-1$
|
||||||
|
result.add(car); /* '\u00D9' alt-0217 */
|
||||||
|
result.add(car); /* '\u00DA' alt-0218 */
|
||||||
|
result.add(car); /* '\u00DB' alt-0219 */
|
||||||
|
result.add(car); /* '\u00DC' alt-0220 */
|
||||||
|
car = new String("Y"); //$NON-NLS-1$
|
||||||
|
result.add(car); /* '\u00DD' alt-0221 */
|
||||||
|
car = new String("_"); //$NON-NLS-1$
|
||||||
|
result.add(car); /* '\u00DE' alt-0222 */
|
||||||
|
car = new String("B"); //$NON-NLS-1$
|
||||||
|
result.add(car); /* '\u00DF' alt-0223 */
|
||||||
|
car = new String("a"); //$NON-NLS-1$
|
||||||
|
result.add(car); /* '\u00E0' alt-0224 */
|
||||||
|
result.add(car); /* '\u00E1' alt-0225 */
|
||||||
|
result.add(car); /* '\u00E2' alt-0226 */
|
||||||
|
result.add(car); /* '\u00E3' alt-0227 */
|
||||||
|
result.add(car); /* '\u00E4' alt-0228 */
|
||||||
|
result.add(car); /* '\u00E5' alt-0229 */
|
||||||
|
car = new String("ae"); //$NON-NLS-1$
|
||||||
|
result.add(car); /* '\u00E6' alt-0230 */
|
||||||
|
car = new String("c"); //$NON-NLS-1$
|
||||||
|
result.add(car); /* '\u00E7' alt-0231 */
|
||||||
|
car = new String("e"); //$NON-NLS-1$
|
||||||
|
result.add(car); /* '\u00E8' alt-0232 */
|
||||||
|
result.add(car); /* '\u00E9' alt-0233 */
|
||||||
|
result.add(car); /* '\u00EA' alt-0234 */
|
||||||
|
result.add(car); /* '\u00EB' alt-0235 */
|
||||||
|
car = new String("i"); //$NON-NLS-1$
|
||||||
|
result.add(car); /* '\u00EC' alt-0236 */
|
||||||
|
result.add(car); /* '\u00ED' alt-0237 */
|
||||||
|
result.add(car); /* '\u00EE' alt-0238 */
|
||||||
|
result.add(car); /* '\u00EF' alt-0239 */
|
||||||
|
car = new String("d"); //$NON-NLS-1$
|
||||||
|
result.add(car); /* '\u00F0' alt-0240 */
|
||||||
|
car = new String("n"); //$NON-NLS-1$
|
||||||
|
result.add(car); /* '\u00F1' alt-0241 */
|
||||||
|
car = new String("o"); //$NON-NLS-1$
|
||||||
|
result.add(car); /* '\u00F2' alt-0242 */
|
||||||
|
result.add(car); /* '\u00F3' alt-0243 */
|
||||||
|
result.add(car); /* '\u00F4' alt-0244 */
|
||||||
|
result.add(car); /* '\u00F5' alt-0245 */
|
||||||
|
result.add(car); /* '\u00F6' alt-0246 */
|
||||||
|
car = new String("/"); //$NON-NLS-1$
|
||||||
|
result.add(car); /* '\u00F7' alt-0247 */
|
||||||
|
car = new String("0"); //$NON-NLS-1$
|
||||||
|
result.add(car); /* '\u00F8' alt-0248 */
|
||||||
|
car = new String("u"); //$NON-NLS-1$
|
||||||
|
result.add(car); /* '\u00F9' alt-0249 */
|
||||||
|
result.add(car); /* '\u00FA' alt-0250 */
|
||||||
|
result.add(car); /* '\u00FB' alt-0251 */
|
||||||
|
result.add(car); /* '\u00FC' alt-0252 */
|
||||||
|
car = new String("y"); //$NON-NLS-1$
|
||||||
|
result.add(car); /* '\u00FD' alt-0253 */
|
||||||
|
car = new String("_"); //$NON-NLS-1$
|
||||||
|
result.add(car); /* '\u00FE' alt-0254 */
|
||||||
|
car = new String("y"); //$NON-NLS-1$
|
||||||
|
result.add(car); /* '\u00FF' alt-0255 */
|
||||||
|
result.add(car); /* '\u00FF' alt-0255 */
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* removeAccents: remove accents from the string given.
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} TalendString
|
||||||
|
*
|
||||||
|
* {param} string("") text: Text to remove accents.
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* {example} removeAccents("Accès à la base")
|
||||||
|
*/
|
||||||
|
public static String removeAccents(String text) {
|
||||||
|
StringBuffer result = new StringBuffer();
|
||||||
|
|
||||||
|
for (int bcl = 0; bcl < text.length(); bcl++) {
|
||||||
|
char carVal = text.charAt(bcl);
|
||||||
|
if (carVal >= 192 && carVal <= 255) {
|
||||||
|
String newVal = (String) map.get(carVal - 192);
|
||||||
|
result.append(newVal);
|
||||||
|
} else {
|
||||||
|
result.append(carVal);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result.toString();
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* unionString: Union the variable number of arguments with separator String
|
||||||
|
*
|
||||||
|
* @param separator union arguments .
|
||||||
|
* @param objects variable number of arguments.
|
||||||
|
* @return A union string.
|
||||||
|
*
|
||||||
|
* {talendTypes} String
|
||||||
|
*
|
||||||
|
* {Category} TalendString
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static String unionString(String separator,Object... objects){
|
||||||
|
if(objects!=null){
|
||||||
|
String value = "";
|
||||||
|
boolean isFirst = true;
|
||||||
|
for(Object obj:objects){
|
||||||
|
if(isFirst){
|
||||||
|
value = String.valueOf(obj);
|
||||||
|
isFirst = false;
|
||||||
|
}else{
|
||||||
|
value = value +separator + String.valueOf(obj);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
}else{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,49 @@
|
|||||||
|
package routines;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class TalendStringUtil<T,Y> {
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Searches a port for a value you specify. If the function finds the value, it returns a result value, which you define.
|
||||||
|
* You can build an unlimited number of searches within a DECODE function.
|
||||||
|
*
|
||||||
|
* @param value : Passes the values you want to search
|
||||||
|
* @param defaultValue : The value you want to return if the search does not find a matching value.
|
||||||
|
* @param search : a Map contains the search-value & result-value.
|
||||||
|
* @return result-value if the search finds a matching value. Default-value if the search does not find a matching value.
|
||||||
|
*
|
||||||
|
* PS:You cannot create a DECODE function with both string and numeric return
|
||||||
|
* values. When you validate the expression above, you receive the following
|
||||||
|
* mismatching datatypes.
|
||||||
|
*/
|
||||||
|
public Y DECODE(T value, Y defaultValue, Map<T, Y> search) {
|
||||||
|
|
||||||
|
return search.containsKey(value)?search.get(value):defaultValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Searches a port for a value you specify. If the function finds the value, it returns a result value, which you define.
|
||||||
|
* You can build an unlimited number of searches within a DECODE function.
|
||||||
|
*
|
||||||
|
* @param value : Passes the values you want to search
|
||||||
|
* @param defaultValue : The value you want to return if the search does not find a matching value.
|
||||||
|
* @param search : a Map contains the search-value & result-value.
|
||||||
|
* @param searchAndResult : pairs of search-value & result-value. You can enter one or more pairs of values.
|
||||||
|
* @return result-value if the search finds a matching value. Default-value if the search does not find a matching value.
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public Y DECODE(T value, Y defaultValue, Object... searchAndResult) {
|
||||||
|
if (searchAndResult.length % 2 != 0) {
|
||||||
|
throw new IllegalArgumentException("Parameter searchAndResult should be in pair.");
|
||||||
|
}
|
||||||
|
Map<T, Y> search = new HashMap<T, Y>();
|
||||||
|
for (int i = 0; i < searchAndResult.length; i += 2) {
|
||||||
|
search.put((T)searchAndResult[i], (Y)searchAndResult[i + 1]);
|
||||||
|
}
|
||||||
|
return DECODE(value, defaultValue, search);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
598
MDM-LCAG-JavaFunctions/src/main/java/routines/ValidityCheck.java
Normal file
598
MDM-LCAG-JavaFunctions/src/main/java/routines/ValidityCheck.java
Normal file
@@ -0,0 +1,598 @@
|
|||||||
|
package routines;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.text.ParseException;
|
||||||
|
|
||||||
|
public class ValidityCheck {
|
||||||
|
private static LcagLogger log = LcagLogger.getLogger(ValidityCheck.class);
|
||||||
|
|
||||||
|
public enum CheckResult
|
||||||
|
{
|
||||||
|
TO_FROM_SWITCHED
|
||||||
|
,EXTENDS_BEFORE
|
||||||
|
,EXTENDS_AFTER
|
||||||
|
,OVERLAPS_WITH_START
|
||||||
|
,OVERLAPS_WITH_END
|
||||||
|
,CONTAINS
|
||||||
|
,HAS_GAP_BEFORE
|
||||||
|
,HAS_GAP_AFTER
|
||||||
|
,FIRSTINSERT
|
||||||
|
,EQUALS
|
||||||
|
,SHORTENED
|
||||||
|
,SETVALIDFROM
|
||||||
|
,UNKNOWN
|
||||||
|
}
|
||||||
|
|
||||||
|
public enum ResultInsertUpdate
|
||||||
|
{
|
||||||
|
INSERT
|
||||||
|
,UPDATE
|
||||||
|
,ERROR
|
||||||
|
}
|
||||||
|
|
||||||
|
public enum ResultValidityCheck
|
||||||
|
{
|
||||||
|
OK
|
||||||
|
,ERROR
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* validityCheckResult: Checks the timeline of the validities
|
||||||
|
*
|
||||||
|
* {talendTypes} Date
|
||||||
|
*
|
||||||
|
* {Category} ValidityCheck
|
||||||
|
*
|
||||||
|
* {param} Dated(Date existing_validFrom, Date existing_validTo, Date new_validFrom,Date new_validTo) input: Dates to check.
|
||||||
|
*
|
||||||
|
* {example} helloExemple(2018-03-02,2099-12-31,2013-01-01,2018-03-01) # EXTENDS_AFTER !.
|
||||||
|
*/
|
||||||
|
public static CheckResult timelineValidity(Date existing_validFrom, Date existing_validTo, Date new_validFrom,Date new_validTo){
|
||||||
|
|
||||||
|
|
||||||
|
CheckResult checkResult=CheckResult.UNKNOWN;
|
||||||
|
|
||||||
|
if (existing_validFrom == null && existing_validTo==null)
|
||||||
|
{
|
||||||
|
//First insert of a record -> no date exists
|
||||||
|
log.debug("CheckResult.FIRSTINSERT");
|
||||||
|
return CheckResult.FIRSTINSERT;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if (new_validFrom == null && new_validTo==null)
|
||||||
|
{
|
||||||
|
log.debug("existing_validFrom:" + existing_validFrom.toString());
|
||||||
|
log.debug("existing_validTo:" + existing_validTo.toString());
|
||||||
|
//No Gap (Related Check)
|
||||||
|
log.debug("CheckResult.HAS_GAP_AFTER1");
|
||||||
|
return CheckResult.HAS_GAP_AFTER;
|
||||||
|
}
|
||||||
|
|
||||||
|
//log.debug("diffDate(existing_validTo,constant.getFarPastDate()):" + String.valueOf(diffDate(existing_validTo,constant.getFarPastDate())));
|
||||||
|
//log.debug("diffDate(existing_validFrom,new_validFrom):" + String.valueOf(diffDate(existing_validTo,constant.getFarPastDate())));
|
||||||
|
log.debug("diffDate(existing_validTo,new_validTo):" + String.valueOf(diffDate(existing_validTo,new_validTo)));
|
||||||
|
|
||||||
|
|
||||||
|
if (diffDate(existing_validTo,constant.getFarFutureDate()) ==0 && diffDate(existing_validFrom,new_validFrom)==0 && diffDate(existing_validTo,new_validTo)<0)
|
||||||
|
{
|
||||||
|
// MDM *......* (2010 to 2099)
|
||||||
|
// Inc *...* (2010 to 2050)
|
||||||
|
|
||||||
|
log.debug("CheckResult.SHORTENED");
|
||||||
|
return CheckResult.SHORTENED;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (diffDate(existing_validFrom,constant.getFarPastDate()) ==0 && diffDate(existing_validTo,new_validTo)==0 && diffDate(existing_validFrom,new_validFrom)>0)
|
||||||
|
{
|
||||||
|
// MDM *......* (1970 to 2099)
|
||||||
|
// Inc *...* (2015 to 2099)
|
||||||
|
|
||||||
|
log.debug("CheckResult.SETVALIDFROM");
|
||||||
|
return CheckResult.SETVALIDFROM;
|
||||||
|
}
|
||||||
|
|
||||||
|
log.debug("existing_validFrom:" + existing_validFrom.toString());
|
||||||
|
log.debug("existing_validTo:" + existing_validTo.toString());
|
||||||
|
log.debug("new_validFrom:" + (new_validFrom.equals(null)?"NULL":new_validFrom.toString()));
|
||||||
|
log.debug("new_validTo:" + new_validTo.toString());
|
||||||
|
|
||||||
|
log.debug("diffDate(new_validFrom,new_validTo):" + String.valueOf(diffDate(new_validFrom,new_validTo)));
|
||||||
|
log.debug("diffDate(existing_validFrom,new_validFrom):" + String.valueOf(diffDate(existing_validFrom,new_validFrom)));
|
||||||
|
log.debug("diffDate(existing_validTo,new_validTo):" + String.valueOf(diffDate(existing_validTo,new_validTo)));
|
||||||
|
|
||||||
|
if (diffDate(new_validFrom,existing_validFrom)==0 && diffDate(new_validTo,existing_validTo)==0)
|
||||||
|
{
|
||||||
|
//First insert of a record -> no date exists
|
||||||
|
log.debug("CheckResult.EQUALS");
|
||||||
|
return CheckResult.EQUALS;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (diffDate(new_validFrom,new_validTo)<=0){
|
||||||
|
checkResult=CheckResult.TO_FROM_SWITCHED;
|
||||||
|
log.debug("CheckResult.TO_FROM_SWITCHED");
|
||||||
|
}else{
|
||||||
|
/*
|
||||||
|
* existing *-----*
|
||||||
|
* new *----*
|
||||||
|
*/
|
||||||
|
if (diffDate(existing_validTo,new_validFrom)==1){
|
||||||
|
checkResult=CheckResult.EXTENDS_AFTER;
|
||||||
|
log.debug("CheckResult.EXTENDS_AFTER");
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
* existing *------*
|
||||||
|
* new *----*
|
||||||
|
*/
|
||||||
|
if (diffDate(existing_validTo, new_validFrom)<0 &&
|
||||||
|
diffDate(existing_validTo, new_validTo)>0 ){
|
||||||
|
checkResult=CheckResult.OVERLAPS_WITH_END;
|
||||||
|
log.debug("CheckResult.OVERLAPS_WITH_END");
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
* existing *------*
|
||||||
|
* new *----*
|
||||||
|
*/
|
||||||
|
if (diffDate(existing_validTo,new_validFrom)>1){
|
||||||
|
checkResult=CheckResult.HAS_GAP_AFTER;
|
||||||
|
log.debug("CheckResult.HAS_GAP_AFTER");
|
||||||
|
log.debug("diffDate(existing_validTo,new_validFrom)" + String.valueOf(diffDate(existing_validTo,new_validFrom)));
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
* existing *------*
|
||||||
|
* new *----*
|
||||||
|
*/
|
||||||
|
if (diffDate(new_validTo, existing_validFrom)==1){
|
||||||
|
checkResult=CheckResult.EXTENDS_BEFORE;
|
||||||
|
log.debug("CheckResult.EXTENDS_BEFORE");
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
* existing *------*
|
||||||
|
* new *----*
|
||||||
|
*/
|
||||||
|
if (diffDate(existing_validFrom,new_validTo)<-1){
|
||||||
|
checkResult=CheckResult.HAS_GAP_BEFORE;
|
||||||
|
log.debug("CheckResult.HAS_GAP_BEFORE");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* existing *------*
|
||||||
|
* new -----*
|
||||||
|
log.debug("## contains");
|
||||||
|
log.debug("## diffDate("+existing_validFrom+","+new_validFrom+"):"+diffDate(existing_validFrom,new_validFrom));
|
||||||
|
log.debug("## diffDate("+existing_validTo+","+new_validTo+"):"+diffDate(existing_validTo,new_validTo));
|
||||||
|
*/
|
||||||
|
if (diffDate(existing_validFrom,new_validFrom) >0 &&
|
||||||
|
diffDate(existing_validTo,new_validTo)<=0 ){
|
||||||
|
checkResult=CheckResult.CONTAINS;
|
||||||
|
log.debug("CONTAINS ENTERED");
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
* existing *------*
|
||||||
|
* new *----*
|
||||||
|
|
||||||
|
log.debug("## OVERLAPS_WITH_START");
|
||||||
|
log.debug("## diffDate("+existing_validFrom+","+ new_validTo+"):"+diffDate(existing_validFrom, new_validTo));
|
||||||
|
log.debug("## diffDate("+existing_validFrom+","+ new_validFrom+"):"+diffDate(existing_validFrom, new_validFrom));
|
||||||
|
*/
|
||||||
|
if (diffDate(existing_validFrom, new_validTo)>0 &&
|
||||||
|
diffDate(existing_validFrom, new_validFrom)<0 ){
|
||||||
|
checkResult=CheckResult.OVERLAPS_WITH_START;
|
||||||
|
log.debug("OVERLAPS_WITH_START");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return checkResult;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private static long diffDate(Date d1, Date d2){
|
||||||
|
return TalendDate.diffDate(d2,d1,"dd",true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Date calc_ValidFrom(Date existing_validFrom, Date new_validFrom){
|
||||||
|
|
||||||
|
Date initValidFromDate = null;
|
||||||
|
Date dresult = null;
|
||||||
|
Date curDate = null;
|
||||||
|
|
||||||
|
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
|
||||||
|
Date date = new Date();
|
||||||
|
String strcurDate = dateFormat.format(date);
|
||||||
|
|
||||||
|
if (new_validFrom == null) new_validFrom = constant.getFarPastDate();
|
||||||
|
|
||||||
|
try {
|
||||||
|
curDate = dateFormat.parse(strcurDate);
|
||||||
|
log.debug("curDate:"+curDate.toString());
|
||||||
|
}
|
||||||
|
catch (ParseException e)
|
||||||
|
{}
|
||||||
|
|
||||||
|
log.debug("calc_ValidFrom(existing_validFrom = %s, new_validFrom = %s) called", existing_validFrom, new_validFrom);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
SimpleDateFormat textFormat = new SimpleDateFormat("yyyy-MM-dd");
|
||||||
|
String paramDateAsString = "1970-01-01";
|
||||||
|
initValidFromDate = textFormat.parse(paramDateAsString);
|
||||||
|
|
||||||
|
if ( TalendDate.diffDate(existing_validFrom,initValidFromDate,"dd") == 0)
|
||||||
|
{
|
||||||
|
log.debug("calc_ValidFrom: ValidFrom in MDM is DefaultDate");
|
||||||
|
//MDM enthält default Wert
|
||||||
|
if ( TalendDate.diffDate(new_validFrom,initValidFromDate,"dd") == 0)
|
||||||
|
{
|
||||||
|
log.debug("calc_ValidFrom: ValidFrom in msg is DefaultDate => use Default date");
|
||||||
|
//auch Message enthält default Wert
|
||||||
|
dresult = initValidFromDate;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
log.debug("calc_ValidFrom: ValidFrom in msg is NOT DefaultDate => use msg date");
|
||||||
|
dresult = new_validFrom;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
log.debug("calc_ValidFrom: ValidFrom in MDM is NOT DefaultDate");
|
||||||
|
|
||||||
|
//MDM enthält KEINE default Wert
|
||||||
|
if ( TalendDate.diffDate(new_validFrom,initValidFromDate,"dd") == 0)
|
||||||
|
{
|
||||||
|
log.debug("calc_ValidFrom: ValidFrom in msg is DefaultDate => use MDM date");
|
||||||
|
// Message enthält default Wert
|
||||||
|
dresult = existing_validFrom;
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if ( TalendDate.diffDate(new_validFrom,curDate,"dd") < 0)
|
||||||
|
{
|
||||||
|
//log.debug("calc_ValidFrom: ValidFrom in msg is in PAST => use MDM date");
|
||||||
|
dresult = existing_validFrom;
|
||||||
|
|
||||||
|
// 2021-02-21: deactivate condition to allow Update in past
|
||||||
|
log.debug("calc_ValidFrom: ValidFrom in msg is in PAST => use Msg date (changed 2021-02-21)");
|
||||||
|
dresult = new_validFrom;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
log.debug("calc_ValidFrom: ValidFrom in msg is NOT in PAST => use Msg date");
|
||||||
|
dresult = new_validFrom;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
catch (ParseException e) {
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
log.debug("calc_ValidFrom: Returning %s", dresult);
|
||||||
|
return dresult;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Date calc_ValidTo(Date existing_validTo, Date new_validTo){
|
||||||
|
|
||||||
|
Date initValidToDate = null;
|
||||||
|
Date dresult = null;
|
||||||
|
|
||||||
|
if (new_validTo == null) new_validTo = constant.getFarFutureDate();
|
||||||
|
|
||||||
|
log.debug("calc_ValidTo(existing_validTo = %s, new_validTo = %s) called", existing_validTo, new_validTo);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
SimpleDateFormat textFormat = new SimpleDateFormat("yyyy-MM-dd");
|
||||||
|
String paramDateAsString = "2099-12-31";
|
||||||
|
initValidToDate = textFormat.parse(paramDateAsString);
|
||||||
|
|
||||||
|
if ( TalendDate.diffDate(existing_validTo,initValidToDate,"dd") == 0)
|
||||||
|
{
|
||||||
|
log.debug("calc_ValidTo: ValidTo in MDM is DefaultDate");
|
||||||
|
//MDM enthält default Wert
|
||||||
|
if ( TalendDate.diffDate(new_validTo,initValidToDate,"dd") == 0)
|
||||||
|
{
|
||||||
|
log.debug("calc_ValidTo: ValidTo in Msg is also DefaultDate => use Default Date");
|
||||||
|
//auch Message enthält default Wert
|
||||||
|
dresult = initValidToDate;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
log.debug("calc_ValidTo: but ValidTo in Msg is NOT DefaultDate => use Msg Date");
|
||||||
|
dresult = new_validTo;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
log.debug("calc_ValidTo: ValidTo in MDM is NOT DefaultDate");
|
||||||
|
|
||||||
|
//MDM enthält KEINE default Wert
|
||||||
|
if ( TalendDate.diffDate(new_validTo,initValidToDate,"dd") == 0)
|
||||||
|
{
|
||||||
|
log.debug("calc_ValidTo: and ValidTo in Msg is DefaultDate => use MDM Date");
|
||||||
|
// Message enthält default Wert
|
||||||
|
dresult = existing_validTo;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
log.debug("calc_ValidTo: and ValidTo in Msg is also NOT DefaultDate => use Msg Date");
|
||||||
|
dresult = new_validTo;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
catch (ParseException e) {
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
log.debug("calc_ValidTo: Returning %s", dresult);
|
||||||
|
return dresult;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ResultInsertUpdate DetermineIfInsOrUpd(Date existing_validFrom, Date existing_validTo, Date new_validFrom,Date new_validTo, String ValidityResult, String isNew)
|
||||||
|
{
|
||||||
|
|
||||||
|
//ONLY IF MDM is not set to default and Values from Incoming Message is NOT set to defaults
|
||||||
|
// log.debug("Enter DetermineIfInsOrUpd");
|
||||||
|
// log.debug("existing_validFrom:" + existing_validFrom.toString());
|
||||||
|
// log.debug("existing_validTo:" + existing_validTo.toString());
|
||||||
|
// log.debug("new_validFrom:" + (new_validFrom.equals(null)?"NULL":new_validFrom.toString()));
|
||||||
|
// log.debug("new_validTo:" + new_validTo.toString());
|
||||||
|
log.debug("DetermineIfInsOrUpd: checking ValidityResult:" + ValidityResult);
|
||||||
|
|
||||||
|
ResultInsertUpdate rInsUpd= ResultInsertUpdate.ERROR;
|
||||||
|
|
||||||
|
if (ValidityResult.contains("EXTENDS_AFTER") || ValidityResult.contains("EXTENDS_BEFORE") || ValidityResult.contains("HAS_GAP_AFTER") || ValidityResult.contains("HAS_GAP_BEFORE") || ValidityResult.contains("TO_FROM_SWITCHED") || (ValidityResult.contains("SETVALIDFROM") && "new".equals(isNew)) )
|
||||||
|
{
|
||||||
|
//First insert of a record -> no date exists
|
||||||
|
log.debug("ResultInsertUpdate.INSERT");
|
||||||
|
return ResultInsertUpdate.INSERT;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ValidityResult.contains("EQUALS") || ValidityResult.contains("SHORTENED"))
|
||||||
|
{
|
||||||
|
//First insert of a record -> no date exists
|
||||||
|
log.debug("ResultInsertUpdate.EQUALS");
|
||||||
|
return ResultInsertUpdate.UPDATE;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ValidityResult.contains("OVERLAPS_WITH_START") || ValidityResult.contains("OVERLAPS_WITH_END") || ValidityResult.contains("CONTAINS"))
|
||||||
|
{
|
||||||
|
//First insert of a record -> no date exists
|
||||||
|
log.debug("ResultInsertUpdate.ERROR");
|
||||||
|
return ResultInsertUpdate.ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
return rInsUpd;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String DetermineIfValidityCheckOK(Date existing_validFrom, Date existing_validTo, Date new_validFrom,Date new_validTo, String ValidityResult, String OPType)
|
||||||
|
{
|
||||||
|
|
||||||
|
//ONLY IF MDM is not set to default and Values from Incoming Message is NOT set to defaults
|
||||||
|
// log.debug("Enter DetermineIfValidityCheckOK");
|
||||||
|
// log.debug("existing_validFrom:" + existing_validFrom.toString());
|
||||||
|
// log.debug("existing_validTo:" + existing_validTo.toString());
|
||||||
|
// log.debug("new_validFrom:" + (new_validFrom.equals(null)?"NULL":new_validFrom.toString()));
|
||||||
|
// log.debug("new_validTo:" + new_validTo.toString());
|
||||||
|
// log.debug("ValidityResult:" + ValidityResult);
|
||||||
|
|
||||||
|
String rValChck= "OPType";
|
||||||
|
|
||||||
|
if ("INSERT".equals(OPType))
|
||||||
|
{
|
||||||
|
if (existing_validFrom == null && existing_validTo== null)
|
||||||
|
{
|
||||||
|
if ("FIRSTINSERT".equals(ValidityResult))
|
||||||
|
{
|
||||||
|
log.debug("CheckResult.FIRSTINSERT");
|
||||||
|
return "OK";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
log.debug("!FIRSTINSERT");
|
||||||
|
return "ERROR";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (ValidityResult.contains("EXTENDS_AFTER") || ValidityResult.contains("EXTENDS_BEFORE") || ValidityResult.contains("HAS_GAP_AFTER") || ValidityResult.contains("HAS_GAP_BEFORE") || ValidityResult.contains("TO_FROM_SWITCHED"))
|
||||||
|
{ //for inserts
|
||||||
|
log.debug("EXTENDS_AFTER....");
|
||||||
|
return "OK";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{ //for inserts
|
||||||
|
log.debug("!EXTENDS_AFTER....");
|
||||||
|
return "ERROR";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if ("UPDATE".equals(OPType))
|
||||||
|
{
|
||||||
|
if
|
||||||
|
(
|
||||||
|
(TalendDate.diffDate(existing_validFrom,constant.getFarPastDate(),"dd") == 0) &&
|
||||||
|
(TalendDate.diffDate(existing_validTo,constant.getFarFutureDate(),"dd") == 0)
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//log.debug("MDM Default Validities");
|
||||||
|
|
||||||
|
if ((diffDate(existing_validFrom,constant.getFarPastDate()) ==0 && diffDate(existing_validTo,new_validTo)==0 && diffDate(existing_validFrom,new_validFrom)>0) || ValidityResult.contains("SETVALIDFROM"))
|
||||||
|
{
|
||||||
|
// MDM *......* (1970 to 2099)
|
||||||
|
// Inc *...* (2015 to 2099)
|
||||||
|
|
||||||
|
log.debug("CheckResult.SETVALIDFROM");
|
||||||
|
return "OK";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ValidityResult.contains("CONTAINS") || ValidityResult.contains("EQUALS") || ValidityResult.contains("SHORTENED"))
|
||||||
|
{
|
||||||
|
log.debug("CONTAINS, EQUALS, SHORTENED");
|
||||||
|
return "OK";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
log.debug("!UPDATE....");
|
||||||
|
return "ERROR";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (ValidityResult.contains("EQUALS") || ValidityResult.contains("SHORTENED"))
|
||||||
|
{
|
||||||
|
log.debug("SHORTENED....");
|
||||||
|
return "OK";
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//MDM has default Validities
|
||||||
|
if
|
||||||
|
(
|
||||||
|
(TalendDate.diffDate(existing_validFrom,constant.getFarPastDate(),"dd") == 0) &&
|
||||||
|
(TalendDate.diffDate(existing_validTo,constant.getFarFutureDate(),"dd") == 0)
|
||||||
|
)
|
||||||
|
{
|
||||||
|
log.debug("MDM Default Validities");
|
||||||
|
|
||||||
|
if (ValidityResult.contains("UPDATE") || ValidityResult.contains("EQUALS"))
|
||||||
|
{
|
||||||
|
return "OK";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return "ERROR";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
//MDM element has no default validities
|
||||||
|
if (ValidityResult.contains("EXTENDS_AFTER") || ValidityResult.contains("EXTENDS_BEFORE") || ValidityResult.contains("HAS_GAP_AFTER") || ValidityResult.contains("HAS_GAP_BEFORE"))
|
||||||
|
{ //for inserts
|
||||||
|
return "OK";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ValidityResult.contains("EQUALS") || ValidityResult.contains("SHORTENED") || ValidityResult.contains("TO_FROM_SWITCHED"))
|
||||||
|
{ //for updates
|
||||||
|
return "OK";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ValidityResult.contains("OVERLAPS_WITH_START") || ValidityResult.contains("OVERLAPS_WITH_END") )
|
||||||
|
{ //for updates
|
||||||
|
return "ERROR";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
return rValChck;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String DetermineIfValidityCheckOK_ALT(Date existing_validFrom, Date existing_validTo, Date new_validFrom,Date new_validTo, String ValidityResult)
|
||||||
|
{
|
||||||
|
|
||||||
|
//ONLY IF MDM is not set to default and Values from Incoming Message is NOT set to defaults
|
||||||
|
log.debug("Enter DetermineIfValidityCheckOK");
|
||||||
|
log.debug("existing_validFrom:" + existing_validFrom.toString());
|
||||||
|
log.debug("existing_validTo:" + existing_validTo.toString());
|
||||||
|
log.debug("new_validFrom:" + (new_validFrom.equals(null)?"NULL":new_validFrom.toString()));
|
||||||
|
log.debug("new_validTo:" + new_validTo.toString());
|
||||||
|
log.debug("ValidityResult:" + ValidityResult);
|
||||||
|
|
||||||
|
String rValChck= "ERROR";
|
||||||
|
|
||||||
|
if (existing_validFrom == null && existing_validTo== null)
|
||||||
|
{
|
||||||
|
//log.debug("CheckResult.FIRSTINSERT");
|
||||||
|
return "OK";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//MDM has default Validities
|
||||||
|
if
|
||||||
|
(
|
||||||
|
(TalendDate.diffDate(existing_validFrom,constant.getFarPastDate(),"dd") == 0) &&
|
||||||
|
(TalendDate.diffDate(existing_validTo,constant.getFarFutureDate(),"dd") == 0)
|
||||||
|
)
|
||||||
|
{
|
||||||
|
log.debug("MDM Default Validities");
|
||||||
|
|
||||||
|
if (ValidityResult.contains("UPDATE") || ValidityResult.contains("EQUALS"))
|
||||||
|
{
|
||||||
|
return "OK";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return "ERROR";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
//MDM element has no default validities
|
||||||
|
if (ValidityResult.contains("EXTENDS_AFTER") || ValidityResult.contains("EXTENDS_BEFORE") || ValidityResult.contains("HAS_GAP_AFTER") || ValidityResult.contains("HAS_GAP_BEFORE"))
|
||||||
|
{ //for inserts
|
||||||
|
return "OK";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ValidityResult.contains("EQUALS") || ValidityResult.contains("SHORTENED") || ValidityResult.contains("TO_FROM_SWITCHED"))
|
||||||
|
{ //for updates
|
||||||
|
return "OK";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ValidityResult.contains("OVERLAPS_WITH_START") || ValidityResult.contains("OVERLAPS_WITH_END") )
|
||||||
|
{ //for updates
|
||||||
|
return "ERROR";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
return rValChck;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
170
MDM-LCAG-JavaFunctions/src/main/java/routines/constant.java
Normal file
170
MDM-LCAG-JavaFunctions/src/main/java/routines/constant.java
Normal file
@@ -0,0 +1,170 @@
|
|||||||
|
package routines;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Calendar;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.HashSet;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* user specification: the function's comment should contain keys as follows: 1. write about the function's comment.but
|
||||||
|
* it must be before the "{talendTypes}" key.
|
||||||
|
*
|
||||||
|
* 2. {talendTypes} 's value must be talend Type, it is required . its value should be one of: String, char | Character,
|
||||||
|
* long | Long, int | Integer, boolean | Boolean, byte | Byte, Date, double | Double, float | Float, Object, short |
|
||||||
|
* Short
|
||||||
|
*
|
||||||
|
* 3. {Category} define a category for the Function. it is required. its value is user-defined .
|
||||||
|
*
|
||||||
|
* 4. {param} 's format is: {param} <type>[(<default value or closed list values>)] <name>[ : <comment>]
|
||||||
|
*
|
||||||
|
* <type> 's value should be one of: string, int, list, double, object, boolean, long, char, date. <name>'s value is the
|
||||||
|
* Function's parameter name. the {param} is optional. so if you the Function without the parameters. the {param} don't
|
||||||
|
* added. you can have many parameters for the Function.
|
||||||
|
*
|
||||||
|
* 5. {example} gives a example for the Function. it is optional.
|
||||||
|
*/
|
||||||
|
public class constant {
|
||||||
|
|
||||||
|
/* Directory structure pre project root. Localtion of config_root directory that defined all environments prd, tst1, dev1,... */
|
||||||
|
public static final String hardCodedRootDirectory ="/cimt";
|
||||||
|
|
||||||
|
/* Framework specific returncode collections*/
|
||||||
|
private static HashSet<Integer> rerunErrorCodes = null;
|
||||||
|
|
||||||
|
public static final String unusableDataErrorCodeListSQL="1,2,3,7,9";
|
||||||
|
public static final String rerunErrorCodeListSQL="1,2,3,7,9,20,22,23,25,29";
|
||||||
|
public static final String onlyUsableDataReturncodeConstraintSQL="( Return_Code not in ("+unusableDataErrorCodeListSQL+") and Return_Code <100)";
|
||||||
|
public static final String onlyCompletedRunsConstraintSQL="( Return_Code not in ("+rerunErrorCodeListSQL+") and Return_Code <100)";
|
||||||
|
|
||||||
|
/* Database Connection names*/
|
||||||
|
public static final String pg_manage_connectionName="pgManageConnection";
|
||||||
|
|
||||||
|
/* Date Patterns */
|
||||||
|
public static final String timestampPatternForSQL="yyyy-MM-dd HH:mm:ss";
|
||||||
|
public static final String timestampPatternForSQLHD="yyyy-MM-dd HH:mm:ss.SSS";
|
||||||
|
public static final String timestampPatternCompactSortable="yyyyMMddHHmmss";
|
||||||
|
public static final String timestampPatternCompactSortableHD="yyyyMMddHHmmssSSS";
|
||||||
|
|
||||||
|
|
||||||
|
/* Content group default */
|
||||||
|
public static final String contentGroup_allData="*ALL*";
|
||||||
|
|
||||||
|
/* caches for calculated values */
|
||||||
|
private static Date farFutureDate;
|
||||||
|
private static String farFutureDateAsDBConst;
|
||||||
|
private static Date farPastDate;
|
||||||
|
|
||||||
|
/* Implicit context load helper */
|
||||||
|
private static String ImplicitContextFilePath;
|
||||||
|
|
||||||
|
private static LcagLogger log = LcagLogger.getLogger(constant.class);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* getFarFutureDate:
|
||||||
|
* @param none
|
||||||
|
* @return Date constant, defined as far future date
|
||||||
|
*
|
||||||
|
* {talendTypes} Date
|
||||||
|
*
|
||||||
|
* {Category} constant (check)
|
||||||
|
*
|
||||||
|
* {example} getFarFutureDate
|
||||||
|
*/
|
||||||
|
public static Date getFarFutureDate() {
|
||||||
|
if (farFutureDate == null) {
|
||||||
|
Calendar cal = Calendar.getInstance();
|
||||||
|
cal.setTimeInMillis(0); /* Reset time part*/
|
||||||
|
cal.set(2099, 11, 31); /* 11= December */
|
||||||
|
farFutureDate = cal.getTime();
|
||||||
|
cal = null;
|
||||||
|
}
|
||||||
|
return farFutureDate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getFarFutureDateAsDBConst() {
|
||||||
|
if(farFutureDateAsDBConst==null) {
|
||||||
|
farFutureDateAsDBConst=TalendDate.formatDate(timestampPatternForSQL, getFarFutureDate());
|
||||||
|
}
|
||||||
|
return farFutureDateAsDBConst;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* getFarPastDate:
|
||||||
|
* @param none
|
||||||
|
* @return Date constant, defined as far past date
|
||||||
|
*
|
||||||
|
* {talendTypes} Date
|
||||||
|
*
|
||||||
|
* {Category} constant (check)
|
||||||
|
*
|
||||||
|
* {example} getFarPastDate
|
||||||
|
*/
|
||||||
|
public static Date getFarPastDate() {
|
||||||
|
if (farPastDate == null) {
|
||||||
|
Calendar cal = Calendar.getInstance();
|
||||||
|
cal.setTimeInMillis(0);
|
||||||
|
farPastDate = cal.getTime();
|
||||||
|
cal = null;
|
||||||
|
}
|
||||||
|
return farPastDate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getImplicitContextFilePath(String actualImplicitContextFilePath, String projectName,String contextName) {
|
||||||
|
// This routine is run at each job start if Implicit context load is set in Job->Extra
|
||||||
|
log.trace("getImplicitContextFilePath(%s, %s, %s) called", actualImplicitContextFilePath, projectName, contextName);
|
||||||
|
|
||||||
|
String[] ctxNameParts = contextName.split("_");
|
||||||
|
|
||||||
|
if ( ctxNameParts.length > 1 ) {
|
||||||
|
LcagLogger.getLogger(log).warn("getImplicitContextFilePath: Context values passed by TAC. Temp context name %s set to %s",
|
||||||
|
contextName, ctxNameParts[0]
|
||||||
|
);
|
||||||
|
|
||||||
|
// Reset context name to know values to prevent file not found exception
|
||||||
|
contextName = ctxNameParts[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
if(ImplicitContextFilePath==null) {
|
||||||
|
ImplicitContextFilePath= String.format("%s/%s/config_root/amc_db_%s.properties",
|
||||||
|
hardCodedRootDirectory, StringHandling.DOWNCASE(projectName), contextName
|
||||||
|
);
|
||||||
|
log.trace("getImplicitContextFilePath: ImplicitContextFilePath initialized");
|
||||||
|
}
|
||||||
|
log.debug("getImplicitContextFilePath: returning ImplicitContextFilePath %s", ImplicitContextFilePath);
|
||||||
|
return ImplicitContextFilePath;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* isReturnCodeTheRerunError: returns date + given months
|
||||||
|
* @param returnCode job return code
|
||||||
|
* @return if return code is an rerun error
|
||||||
|
*
|
||||||
|
* {talendTypes} Integer
|
||||||
|
*
|
||||||
|
* {Category} constant (check)
|
||||||
|
*
|
||||||
|
* {example} isReturnCodeTheRerunError(0).
|
||||||
|
*
|
||||||
|
* This function is placed here due to reference tracking
|
||||||
|
* issues in talend. It uses the "rerunErrorCodeListSQL"
|
||||||
|
* constant. Would function be placed in other module and
|
||||||
|
* used in Job, it could lead to "rerunErrorCodeListSQL" not
|
||||||
|
* beeing included in exported job.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static boolean isReturnCodeTheRerunError(Integer returnCode) {
|
||||||
|
// lazy initialization, happens only once
|
||||||
|
if (rerunErrorCodes == null) {
|
||||||
|
rerunErrorCodes = new HashSet<Integer>();
|
||||||
|
String[] unusableDataErrorCodeArray = rerunErrorCodeListSQL.split(",");
|
||||||
|
for (String string : unusableDataErrorCodeArray) {
|
||||||
|
rerunErrorCodes.add(Integer.parseInt(string));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// check if return code is a rerun error
|
||||||
|
return rerunErrorCodes.contains(returnCode);
|
||||||
|
}
|
||||||
|
}
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user