/*
 * Copyright 2003 Code Futures Software, Ltd. All rights reserved.
 *
 * This is proprietary code. Use is subject to license terms.
 *
 * Author: Andy Grove
 * Date: 03-Dec-2003
 * Time: 11:14:02
 *  
 * Visit http://www.codefutures.com for more information.
 */
package com.codefutures.firestorm.codegen.dto;

import com.codefutures.firestorm.api.codegen.Provider;
import com.codefutures.firestorm.api.codegen.ProviderOptions;
import com.codefutures.firestorm.api.codegen.CodeGenException;
import com.codefutures.firestorm.api.studio.Studio;
import com.codefutures.firestorm.api.studio.Wizard;
import com.codefutures.firestorm.config.dom.FireStormConfig;
import com.codefutures.firestorm.project.dom.Project;
import com.codefutures.firestorm.project.dom.TableDao;
import com.codefutures.progress.ProgressListener;
import com.codefutures.jingo.source.dom.JClass;
import com.codefutures.jingo.source.factory.SourceWriterFactory;
import com.codefutures.jingo.source.io.SourceWriter;

import java.io.IOException;
import java.io.File;
import java.util.Map;

public class DtoProvider implements Provider
{
    private FireStormConfig config;

    public void setConfig(FireStormConfig config)
    {
        this.config = config;
    }

    public String getMenuItemName()
    {
        return "DTO";
    }

    public char getMenuItemMnemonic()
    {
        return 'T';
    }

    public String getProviderId()
    {
        return "dto";
    }

    public String getDescription()
    {
        return "Generates DTO classes";
    }

    public ProviderOptions getDefaultOptions(
            Project project
            )
    {
        DtoProviderOptions options = new DtoProviderOptions();
        options.setPackageName( "com.codefutures.example.dto" );
        options.setOutputDir( new File( "dto-output" ) );
        return options;
    }

    public void readOptions(Map map, ProviderOptions options) {
    }

    public void writeOptions(Map map, ProviderOptions options) {
    }

    public boolean validateSetup(
            Studio studio,
            Project project,
            ProviderOptions options
            )
    {
        // nothing to validate so always return true
        return true;
    }

    public Wizard getWizard(
            Studio studio,
            Project project,
            ProviderOptions options
            )
    {
        // this example doesn't require a wizard so return null
        return null;
    }

    public void generateCode(
            Project project,
            ProviderOptions options,
            ProgressListener listener)
            throws CodeGenException, IOException
    {
        // display a progress message
        listener.message( "Generating DTO Source Code" );

        // prepare a Jingo SourceWriter
        SourceWriter w = SourceWriterFactory.getSourceWriter();
        File srcDir = new File( options.getOutputDir(), "src" );

        // get a list of tables
        TableDao table[] = project.getDatabase().getTableList();

        // iterate through the tables
        for (int i = 0; i < table.length; i++)
        {
            // update the progress bar
            listener.progress( i, table.length );

            // generate the DTO
            JClass c = GenTableDto.generate( table[i], (DtoProviderOptions)options );

            w.writeSource( srcDir, c );
        }

        // it is important to signal to the studio that the work is completed
        listener.complete();
    }

    public void completedAction(
            Studio studio,
            Project project,
            ProviderOptions options,
            long timeMilliseconds
            )
    {
        //TODO: display a popup to tell the user that everything worked ok
    }

}
