Skip to content

Commit

Permalink
[select2] Add support for JSON IDs
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-g committed Nov 25, 2014
1 parent 6417916 commit fa77e2e
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 1 deletion.
@@ -0,0 +1,56 @@
package org.wicketstuff.select2;

import java.util.List;
import org.junit.Assert;
import org.junit.Test;

public class Select2MultiChoiceTest extends Assert {
@Test
public void testSplitWithSingleValue() throws Exception
{
List<String> strings = Select2MultiChoice.splitInput("A");
assertEquals(1, strings.size());
assertEquals("A", strings.get(0));
}

@Test
public void testSplitWithRegularCSV() throws Exception
{
List<String> strings = Select2MultiChoice.splitInput("A,B,C");
assertEquals(3, strings.size());
}

@Test
public void testSplitWithSingleJsonId() throws Exception
{
String jsonId = "{\"someKey\":\"someValue\"}";
List<String> strings = Select2MultiChoice.splitInput(jsonId);
assertEquals(1, strings.size());
assertEquals(jsonId, strings.get(0));
}

@Test
public void testSplitWithSingleJsonIdThatHasNestedObjects() throws Exception
{
String jsonId = "{\"email\":{\"emailAddress\":\"test@test.com\"},\"isContact\":true}";
List<String> strings = Select2MultiChoice.splitInput(jsonId);
assertEquals(1, strings.size());
assertEquals(jsonId, strings.get(0));
}

@Test
public void testSplitWithMultipleJsonIds() throws Exception
{
String jsonId = "{\"someKey\":\"someValue\"},{\"someKey\":\"otherValue\"}";
List<String> strings = Select2MultiChoice.splitInput(jsonId);
assertEquals(2, strings.size());
}

@Test
public void testSplitWithMultipleJsonIdsAndNestedJsonObjects() throws Exception
{
List<String> strings = Select2MultiChoice.splitInput(
"{\"email\":{\"emailAddress\":\"nouser@test.com\"},\"isContact\":true},{\"email\":{\"emailAddress\":\"otheruser@test.com\"},\"isContact\":contact}" );
assertEquals(2, strings.size());
}
}
Expand Up @@ -61,7 +61,8 @@ public void convertInput() {
choices = new ArrayList<>();
} else {
if (isAjax()) {
choices = getProvider().toChoices(Arrays.asList(input.split(",")));
List<String> ids = splitInput( input );
choices = getProvider().toChoices( ids );
} else {
choices = new ArrayList<>();
String[] data = input.split(",");
Expand Down Expand Up @@ -141,4 +142,35 @@ protected void renderInitializationScript(IHeaderResponse response) {
}
}

static List<String> splitInput( String input ) {
if (input.startsWith("{") && input.endsWith("}")) {
// Assume we're using JSON IDs
List<String> result = new ArrayList<>();

int openBracket = 0;
Integer lastStartIdx = null;
for (int i = 0; i < input.length(); i++) {
char c = input.charAt(i);
if (c == '{') {
openBracket++;
if (lastStartIdx == null) {
lastStartIdx = i;
}
}
if (c == '}') {
openBracket--;
if (openBracket == 0) {
String substring = input.substring(lastStartIdx, i + 1);
result.add(substring);
lastStartIdx = null;
}
}
}

return result;
}

return Arrays.asList(input.split( "," ));
}

}

0 comments on commit fa77e2e

Please sign in to comment.