=== modified file 'dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/TextUtils.java' --- dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/TextUtils.java 2014-04-03 16:03:26 +0000 +++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/TextUtils.java 2014-04-10 12:43:23 +0000 @@ -35,6 +35,8 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; +import org.apache.commons.lang.StringUtils; + /** * @author Lars Helge Overland */ @@ -182,6 +184,34 @@ } /** + * Removes the last occurence of the word "or" from the given string, + * including potential trailing spaces, case-insentitive. + * + * @param string the string. + * @return the chopped string. + */ + public static String removeLastOr( String string ) + { + string = StringUtils.stripEnd( string, " " ); + + return StringUtils.removeEndIgnoreCase( string, "or" ); + } + + /** + * Removes the last occurence of the word "and" from the given string, + * including potential trailing spaces, case-insentitive. + * + * @param string the string. + * @return the chopped string. + */ + public static String removeLastAnd( String string ) + { + string = StringUtils.stripEnd( string, " " ); + + return StringUtils.removeEndIgnoreCase( string, "and" ); + } + + /** * Trims the given string from the end. * * @param value the value to trim. === modified file 'dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/TextUtilsTest.java' --- dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/TextUtilsTest.java 2014-03-22 15:30:31 +0000 +++ dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/TextUtilsTest.java 2014-04-10 12:43:23 +0000 @@ -86,4 +86,24 @@ assertEquals( new ArrayList( Arrays.asList( "John", "Doe", "Main", "Road", "25" ) ), TextUtils.getTokens( "John Doe Main Road 25" ) ); assertEquals( new ArrayList( Arrays.asList( "Ted,Johnson", "Upper-Road", "45" ) ), TextUtils.getTokens( "Ted,Johnson Upper-Road 45" ) ); } + + @Test + public void testRemoveLastOr() + { + assertEquals( null, TextUtils.removeLastOr( null ) ); + assertEquals( "", TextUtils.removeLastOr( "" ) ); + assertEquals( "or name='tom' or name='john' ", TextUtils.removeLastOr( "or name='tom' or name='john' or" ) ); + assertEquals( "or name='tom' or name='john' ", TextUtils.removeLastOr( "or name='tom' or name='john' or " ) ); + assertEquals( "or name='tom' or name='john' ", TextUtils.removeLastOr( "or name='tom' or name='john' or " ) ); + } + + @Test + public void testRemoveLastAnd() + { + assertEquals( null, TextUtils.removeLastAnd( null ) ); + assertEquals( "", TextUtils.removeLastAnd( "" ) ); + assertEquals( "and name='tom' and name='john' ", TextUtils.removeLastAnd( "and name='tom' and name='john' and" ) ); + assertEquals( "and name='tom' and name='john' ", TextUtils.removeLastAnd( "and name='tom' and name='john' and " ) ); + assertEquals( "and name='tom' and name='john' ", TextUtils.removeLastAnd( "and name='tom' and name='john' and " ) ); + } }