package com.terradue.dsione;

/*
 *  Copyright 2012 Terradue srl
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

import static java.beans.Introspector.getBeanInfo;

import java.beans.BeanInfo;
import java.beans.PropertyDescriptor;
import java.util.Collection;
import java.util.Formatter;
import java.util.LinkedList;
import java.util.List;

import com.beust.jcommander.Parameter;

abstract class AbstractDescribeCommand
    extends BaseTool
{

    private static final String CLASS = "class";

    // CLI Parameters

    @Parameter( names = { "--headers" }, description = "Display column headers" )
    protected boolean headers = false;

    @Parameter( names = { "--fields" }, description = "Comma separated fields list that have to be shown" )
    private List<String> fields = getDefaultFields();

    @Parameter( description = "The image identificator(s) as returned by the upload command" )
    protected List<String> ids = new LinkedList<String>();

    protected abstract List<String> getDefaultFields();

    protected final <T> void log( Collection<T> items )
        throws Exception
    {
        if ( items.isEmpty() )
        {
            logger.info( "0 items found" );
            return;
        }

        boolean first = true;

        for ( T item : items )
        {
            log( item, headers && first );

            if ( first )
            {
                first = false;
            }
        }
    }

    protected final <T> void log( T item, boolean printHeaders )
        throws Exception
    {
        BeanInfo beanInfo = getBeanInfo( item.getClass() );

        if ( printHeaders )
        {
            Formatter header = new Formatter();

            for ( PropertyDescriptor propertyDescriptor : beanInfo.getPropertyDescriptors() )
            {
                if ( !CLASS.equals( propertyDescriptor.getName() ) )
                {
                    append( propertyDescriptor.getName(), header );
                }
            }

            logger.info( header.toString() );
        }

        Formatter line = new Formatter();
        for ( PropertyDescriptor propertyDescriptor : beanInfo.getPropertyDescriptors() )
        {
            if ( !CLASS.equals( propertyDescriptor.getName() ) )
            {
                append( propertyDescriptor.getReadMethod().invoke( item ), line );
            }
        }

        logger.info( line.toString() );
    }

    private static <E> void append( E element, Formatter formatter )
    {
        formatter.format( "| %-20s |", element );
    }

}

@@ -635,88 +635,8 @@
 */%0A%0A
-import static java.beans.Introspector.getBeanInfo;%0A%0Aimport java.beans.BeanInfo;%0A
 impo
@@ -886,58 +886,8 @@
 %0A%7B%0A%0A
-    private static final String CLASS = %22class%22;%0A%0A
     
@@ -1922,69 +1922,8 @@
   %7B%0A
-        BeanInfo beanInfo = getBeanInfo( item.getClass() );%0A%0A
     
@@ -2027,164 +2027,29 @@
 r ( 
-PropertyDescriptor propertyDescriptor : beanInfo.getPropertyDescriptors() )%0A            %7B%0A                if ( !CLASS.equals( propertyDescriptor.getName() )
+String field : fields
  )%0A 
@@ -2055,34 +2055,26 @@
             
- 
-   %7B%0A    
+%7B%0A
             
@@ -2089,65 +2089,24 @@
 nd( 
-propertyDescriptor.getName(), header );%0A                %7D
+field, header );
 %0A   
@@ -2229,16 +2229,96 @@
    for (
+ String field : fields )%0A        %7B%0A            try%0A            %7B%0A               
  Propert
@@ -2352,22 +2352,14 @@
 tor 
-: beanInfo.get
+= new 
 Prop
@@ -2376,24 +2376,42 @@
 ptor
+( field, item.getClas
 s() )
+;
 %0A      
-  %7B%0A
     
@@ -2420,28 +2420,14 @@
     
-  if ( !CLASS.equals
+append
 ( pr
@@ -2450,106 +2450,141 @@
 .get
-Name() ) )%0A            %7B%0A                append( propertyDescriptor.getReadMethod().invoke( item )
+ReadMethod().invoke( item ), line );%0A            %7D%0A            catch ( Exception e )%0A            %7B%0A                append( %22-- NA --%22
 , li

print selected fields only