Skip to content

Commit

Permalink
Cutting filter in parts implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
jlolling committed Feb 1, 2023
1 parent 3d23324 commit 96f51fd
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 16 deletions.
6 changes: 1 addition & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>de.jlo.talendcomp</groupId>
<artifactId>jlo-talendcomp-saprfc</artifactId>
<version>1.4</version>
<version>1.5</version>
<properties>
<encoding>UTF-8</encoding>
</properties>
Expand All @@ -21,10 +21,6 @@
<groupId>com.sap.jco</groupId>
<artifactId>sapjco</artifactId>
<version>3.0.21</version>
<scope>system</scope>
<!-- eclipse complains here a missing absolute path, but works nevertheless -->
<!-- set the path in your maven settings -->
<systemPath>${sapjco3Path}</systemPath>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
Expand Down
49 changes: 49 additions & 0 deletions src/main/java/de/jlo/talendcomp/sap/TextSplitter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package de.jlo.talendcomp.sap;

import java.util.ArrayList;
import java.util.List;

public class TextSplitter {

public static List<String> split(String text, char delimiter) {
List<String> result = new ArrayList<>();
if (text != null) {
text = text.trim();
boolean inLiteral = false;
int i = 0;
StringBuilder sb = new StringBuilder();
for (; i < text.length(); i++) {
char c = text.charAt(i);
if (c == '\'') {
if (inLiteral == false) {
inLiteral = true;
} else {
inLiteral = false;
}
}
if (inLiteral) {
sb.append(c);
} else {
if (c == delimiter) {
// found delimiter to cut
// but ignore 2 following delimiter
String part = sb.toString();
if (part.trim().isEmpty() == false) {
result.add(part);
}
sb.setLength(0); // reset string buffer
} else {
sb.append(c);
}
}
}
String lastPart = sb.toString();
if (lastPart.trim().isEmpty() == false) {
result.add(lastPart);
}
}
return result;
}


}
23 changes: 12 additions & 11 deletions src/main/java/de/jlo/talendcomp/sap/impl/TableInputImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import org.apache.commons.text.StringEscapeUtils;

Expand All @@ -18,6 +17,7 @@
import com.sap.conn.jco.rt.DefaultTable;

import de.jlo.talendcomp.sap.TableInput;
import de.jlo.talendcomp.sap.TextSplitter;

/**
* Copyright 2023 Jan Lolling jan.lolling@gmail.com
Expand Down Expand Up @@ -49,7 +49,7 @@ public class TableInputImpl implements TableInput {
private Integer rowCount = null;
private Integer rowSkip = null;
private String functionDescription = null;
private String filter72Separator = "\\";
private char filterPartSeparator = ';';

/**
* Create an instance if TableInput
Expand Down Expand Up @@ -113,18 +113,19 @@ public void execute() throws Exception {
}
JCoParameterList tableParameterList = function.getTableParameterList();
// add where condition
if (filter != null) {
if (filter.length() > 72) {
throw new Exception("Filter expression: <" + filter + "> is longer than 72 chars. The function does not allows filter expressions longer than 72 chars.");
} else {
JCoTable tableInputOptions = tableParameterList.getTable("OPTIONS");
tableInputOptions.appendRows(1);
tableInputOptions.firstRow();
tableInputOptions.setValue("TEXT", filter);
List<String> filterPartList = TextSplitter.split(filter, filterPartSeparator);
if (filterPartList.size() > 0) {
JCoTable tableInputOptions = tableParameterList.getTable("OPTIONS");
tableInputOptions.appendRows(filterPartList.size());
tableInputOptions.firstRow();
for (String part : filterPartList) {
if (part.length() > 72) {
throw new Exception("The filter expression: <" + filter + "> contains a part which is larger than 72 chars. Affected part: <"+ part + ">. Please split the filter with: <" + filterPartSeparator + "> into parts smaller than 72 chars");
}
tableInputOptions.setValue("TEXT", part);
tableInputOptions.nextRow();
}
}

// add fields
if (listFields.isEmpty()) {
throw new Exception("List of expected fields cannot not be empty");
Expand Down
63 changes: 63 additions & 0 deletions src/test/java/de/jlo/talendcomp/sap/impl/TestTextSplitter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package de.jlo.talendcomp.sap.impl;

import static org.junit.Assert.assertTrue;

import java.util.List;

import org.junit.Test;

import de.jlo.talendcomp.sap.TextSplitter;

public class TestTextSplitter {

@Test
public void test1() {
String input = "';';T1;T2; ;T3";
List<String> result = TextSplitter.split(input, ';');
for (String s : result) {
System.out.println(">" + s);
}
assertTrue("Count parts incorrect: " + result.size(), result.size() == 4);
}

@Test
public void test2() {
String input = "';'";
List<String> result = TextSplitter.split(input, ';');
for (String s : result) {
System.out.println(">" + s);
}
assertTrue("Count parts incorrect: " + result.size(), result.size() == 1);
}

@Test
public void test3() {
String input = "abcd efg";
List<String> result = TextSplitter.split(input, ';');
for (String s : result) {
System.out.println(">" + s);
}
assertTrue("Count parts incorrect: " + result.size(), result.size() == 1);
}

@Test
public void test4() {
String input = "";
List<String> result = TextSplitter.split(input, ';');
for (String s : result) {
System.out.println(">" + s);
}
assertTrue("Count parts incorrect: " + result.size(), result.size() == 0);
}

@Test
public void test5() {
String input = " ; ";
List<String> result = TextSplitter.split(input, ';');
for (String s : result) {
System.out.println(">" + s);
}
assertTrue("Count parts incorrect: " + result.size(), result.size() == 0);
}

}

0 comments on commit 96f51fd

Please sign in to comment.