package org.hisp.dhis.fieldfilter;

/*
 * Copyright (c) 2004-2018, University of Oslo
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 * Neither the name of the HISP project nor the names of its contributors may
 * be used to endorse or promote products derived from this software without
 * specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Morten Olav Hansen <mortenoh@gmail.com>
 */
public class DefaultFieldParser implements FieldParser
{
    @Override
    public FieldMap parse( String fields )
    {
        List<String> prefixList = Lists.newArrayList();
        FieldMap fieldMap = new FieldMap();

        StringBuilder builder = new StringBuilder();

        for ( String c : fields.split( "" ) )
        {
            if ( c.equals( "," ) )
            {
                putInMap( fieldMap, joinedWithPrefix( builder, prefixList ) );
                builder = new StringBuilder();
                continue;
            }

            if ( c.equals( "[" ) )
            {
                prefixList.add( builder.toString() );
                builder = new StringBuilder();
                continue;
            }

            if ( c.equals( "]" ) )
            {
                if ( !builder.toString().isEmpty() )
                {
                    putInMap( fieldMap, joinedWithPrefix( builder, prefixList ) );
                }

                prefixList.remove( prefixList.size() - 1 );
                builder = new StringBuilder();
                continue;
            }

            if ( StringUtils.isAlphanumeric( c ) || c.equals( "*" ) || c.equals( ":" ) || c.equals( ";" ) || c.equals( "~" ) || c.equals( "!" )
                || c.equals( "|" ) || c.equals( "{" ) || c.equals( "}" ) || c.equals( "(" ) || c.equals( ")" ) )
            {
                builder.append( c );
            }
        }

        if ( !builder.toString().isEmpty() )
        {
            putInMap( fieldMap, joinedWithPrefix( builder, prefixList ) );
        }

        return fieldMap;
    }

    @Override
    public List<String> modifyFilter( Collection<String> fields, Collection<String> excludeFields )
    {
        if ( fields == null )
        {
            fields = new LinkedList<String>();
        }

        return fields.stream()
            .map( s -> s.replaceAll( "]", String.format( ",%s]", excludeFields.toString().replaceAll( "\\[|\\]", "" ) ) ) )
            .collect( Collectors.toList() );
    }

    private String joinedWithPrefix( StringBuilder builder, List<String> prefixList )
    {
        String prefixes = StringUtils.join( prefixList, "." );
        prefixes = prefixes.isEmpty() ? builder.toString() : (prefixes + "." + builder.toString());
        return prefixes;
    }

    private void putInMap( FieldMap fieldMap, String path )
    {
        if ( StringUtils.isEmpty( path ) )
        {
            return;
        }

        for ( String p : path.split( "\\." ) )
        {
            fieldMap.putIfAbsent( p, new FieldMap() );
            fieldMap = fieldMap.get( p );
        }
    }
}

@@ -2137,40 +2137,818 @@
     
-for ( String c : fields.split
+String%5B%5D fieldSplit = fields.split( %22%22 );%0A%0A        for ( int i = 0; i %3C fieldSplit.length; i++ )%0A        %7B%0A            String c = fieldSplit%5Bi%5D;%0A%0A            if ( c.equals( %22:%22 ) %7C%7C c.equals( %22~%22 ) )%0A            %7B%0A                boolean insideParameters = false;%0A%0A                for ( ; i %3C fieldSplit.length; i++ )%0A                %7B%0A                    c = fieldSplit%5Bi%5D;%0A%0A                    if ( StringUtils.isAlphanumeric( c ) %7C%7C c.equals( %22:%22 ) %7C%7C c.equals( %22~%22 ) )%0A                    %7B%0A                        builder.append( c );%0A                    %7D%0A                    else if ( c.equals( %22(%22 ) )%0A                    %7B%0A                        insideParameters = true;%0A                        builder.append( c );%0A                    %7D%0A                    else if ( insideParameters && c.equals
 ( %22
+)
 %22 ) 
@@ -2949,33 +2949,295 @@
 %22)%22 ) )%0A        
-%7B
+            %7B%0A                        insideParameters = false;%0A                        builder.append( c );%0A                    %7D%0A                    else%0A                    %7B%0A                        break;%0A                    %7D%0A                %7D%0A            %7D%0A
 %0A            if 
@@ -3469,16 +3469,35 @@
 s( %22%5B%22 )
+ %7C%7C c.equals( %22(%22 )
  )%0A     
@@ -3679,16 +3679,35 @@
 s( %22%5D%22 )
+ %7C%7C c.equals( %22)%22 )
  )%0A     
@@ -4911,24 +4911,150 @@
  %22%22 ) ) ) )%0A
+            .map( s -%3E s.replaceAll( %22%5C%5C)%22, String.format( %22,%25s)%22, excludeFields.toString().replaceAll( %22%5C%5C(%7C%5C%5C)%22, %22%22 ) ) ) )%0A
             

Support () as field filter (#2273)