SwmmData.java [src/SwmmObjects] Revision: default  Date:
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package SwmmObjects;

import java.util.ArrayList;
import org.apache.commons.lang3.StringUtils;

/**
 *
 * @author Lucas Yaege
 */
public class SwmmData {

    public String title;
    public Options options;
    public Evaporation evaporation;
    public ArrayList<SwmmFile> files;
    public ArrayList<RainGage> rainGages;
    public ArrayList<Subcatchment> subcatchments;
    public ArrayList<Subarea> subareas;
    public ArrayList<Infiltration> infiltrations;
    public ArrayList<LIDControl> lidControls;
    public ArrayList<LIDUsage> lidUsages;
    public ArrayList<Outfall> outfalls;
    public ArrayList<Conduit> conduits;
    public ArrayList<Junction> junctions;
    public ArrayList<XSection> xSections;
    public ReportOptions reportOptions;

    private int longestObjectName = 17;

    private final int columnWidth = 17; //Determines how much space to pad each column with.

    public SwmmData()
    {
        title = "";
        options = new Options();
        files = new ArrayList<>();
        evaporation = new Evaporation();
        rainGages = new ArrayList<>();
        subcatchments = new ArrayList<>();
        subareas = new ArrayList<>();
        infiltrations = new ArrayList<>();
        outfalls = new ArrayList<>();
        reportOptions = new ReportOptions();
        lidControls = new ArrayList<>();
        lidUsages = new ArrayList<>();
        junctions = new ArrayList<>();
        conduits = new ArrayList<>();
        xSections = new ArrayList<>();
    }

    private void readTitle( String line )
    {
        title += line + "\n";
    }

    private void readOption( String line )
    {
        options.setOption( line );
    }

    private void readEvaporation( String line )
    {
        evaporation.parse( line );
    }

    private void readRainGage( String line )
    {
        RainGage r = new RainGage( line );
        rainGages.add( r );
    }

    private void readSubcatchment( String line )
    {
        Subcatchment s = new Subcatchment( line );
        subcatchments.add( s );
    }

    private void readSubarea( String line )
    {
        Subarea s = new Subarea( line );
        subareas.add( s );
    }

    private void readInfiltration( String line )
    {
        Infiltration infiltration = new Infiltration( line );
        infiltrations.add( infiltration );
    }

    private void readOutfall( String line )
    {
        Outfall outfall = new Outfall( line );
        outfalls.add( outfall );
    }

    private void readReportOption( String line )
    {
        reportOptions.setReportOption( line );
    }

    private void readFile( String line )
    {
        SwmmFile file = new SwmmFile( line );
        files.add( file );
    }

    //ToString section.
    @Override
    public String toString()
    {
        getLongestObjectName();
        StringBuilder sb = new StringBuilder();
        sb.append( titleToString() );
        sb.append( options.toString() );
        sb.append( filesToString( columnWidth ) );
        sb.append( evaporation.toString() );
        sb.append( rainGagesToString( columnWidth ) );
        sb.append( subcatchmentsToString( columnWidth ) );
        sb.append( subareasToString( columnWidth ) );
        sb.append( infiltrationsToString( columnWidth ) );
        sb.append( outfallsToString( columnWidth ) );
        sb.append( lidControlsToString( columnWidth ) );
        sb.append( lidUsagesToString( columnWidth ) );
        sb.append( junctionsToString( columnWidth ) );
        sb.append( conduitsToString( columnWidth ) );
        sb.append( xSectionsToString( columnWidth ) );
        sb.append( reportOptions.toString() );

        return sb.toString();
    }

    private String infiltrationsToString( int padDistance )
    {
        StringBuilder sb = new StringBuilder();
        //Section Title
        sb.append( "[INFILTRATION]\n" );

        //Headers
        sb.append( StringUtils.rightPad( ";;Subcatchment", longestObjectName + 2 ) );
        sb.append( StringUtils.rightPad( "MaxRate", padDistance ) );
        sb.append( StringUtils.rightPad( "MinRate", padDistance ) );
        sb.append( StringUtils.rightPad( "Decay", padDistance ) );
        sb.append( StringUtils.rightPad( "DryTime", padDistance ) );
        sb.append( StringUtils.rightPad( "MaxInfil", padDistance ) );
        sb.append( "\n" );

        //Separator Line
        sb.append( StringUtils.rightPad( ";;--------------", longestObjectName + 2 ) );
        sb.append( StringUtils.rightPad( "--------------", padDistance ) );
        sb.append( StringUtils.rightPad( "--------------", padDistance ) );
        sb.append( StringUtils.rightPad( "--------------", padDistance ) );
        sb.append( StringUtils.rightPad( "--------------", padDistance ) );
        sb.append( StringUtils.rightPad( "--------------", padDistance ) );
        sb.append( "\n" );

        for ( Infiltration i : infiltrations )
        {
            sb.append( i.toString( padDistance, longestObjectName + 2 ) );
        }

        sb.append( "\n\n" );

        return sb.toString();
    }

    private String subcatchmentsToString( int padDistance )
    {
        StringBuilder sb = new StringBuilder();
        //Section Title
        sb.append( "[SUBCATCHMENTS]\n" );

        //Headers
        sb.append( StringUtils.rightPad( ";;Name", longestObjectName + 2 ) );
        sb.append( StringUtils.rightPad( "Rain Gage", longestObjectName ) );
        sb.append( StringUtils.rightPad( "Outlet", longestObjectName + 2 ) );
        sb.append( StringUtils.rightPad( "Area", padDistance ) );
        sb.append( StringUtils.rightPad( "%Imperv", padDistance ) );
        sb.append( StringUtils.rightPad( "Width", padDistance ) );
        sb.append( StringUtils.rightPad( "%Slope", padDistance ) );
        sb.append( StringUtils.rightPad( "CurbLen", padDistance ) );
        sb.append( StringUtils.rightPad( "SnowPack", padDistance ) );
        sb.append( "\n" );

        //Separator Line
        sb.append( StringUtils.rightPad( ";;--------------", longestObjectName + 2 ) );
        sb.append( StringUtils.rightPad( "--------------", longestObjectName ) );
        sb.append( StringUtils.rightPad( "--------------", longestObjectName + 2 ) );
        sb.append( StringUtils.rightPad( "--------------", padDistance ) );
        sb.append( StringUtils.rightPad( "--------------", padDistance ) );
        sb.append( StringUtils.rightPad( "--------------", padDistance ) );
        sb.append( StringUtils.rightPad( "--------------", padDistance ) );
        sb.append( StringUtils.rightPad( "--------------", padDistance ) );
        sb.append( StringUtils.rightPad( "--------------", padDistance ) );
        sb.append( "\n" );

        for ( Subcatchment s : subcatchments )
        {
            sb.append( s.toString( padDistance, longestObjectName + 2 ) );
        }

        sb.append( "\n\n" );

        return sb.toString();
    }

    private String subareasToString( int padDistance )
    {
        StringBuilder sb = new StringBuilder();
        //Section Title
        sb.append( "[SUBAREAS]\n" );

        //Headers
        sb.append( StringUtils.rightPad( ";;Subcatchment", longestObjectName + 2 ) );
        sb.append( StringUtils.rightPad( "N-Imperv", padDistance ) );
        sb.append( StringUtils.rightPad( "N-Perv", padDistance ) );
        sb.append( StringUtils.rightPad( "S-Imperv", padDistance ) );
        sb.append( StringUtils.rightPad( "S-Perv", padDistance ) );
        sb.append( StringUtils.rightPad( "PctZero", padDistance ) );
        sb.append( StringUtils.rightPad( "RouteTo", padDistance ) );
        sb.append( StringUtils.rightPad( "PctRouted", padDistance ) );
        sb.append( "\n" );

        //Separator Line
        sb.append( StringUtils.rightPad( ";;--------------", longestObjectName + 2 ) );
        sb.append( StringUtils.rightPad( "--------------", padDistance ) );
        sb.append( StringUtils.rightPad( "--------------", padDistance ) );
        sb.append( StringUtils.rightPad( "--------------", padDistance ) );
        sb.append( StringUtils.rightPad( "--------------", padDistance ) );
        sb.append( StringUtils.rightPad( "--------------", padDistance ) );
        sb.append( StringUtils.rightPad( "--------------", padDistance ) );
        sb.append( StringUtils.rightPad( "--------------", padDistance ) );
        sb.append( "\n" );

        for ( Subarea s : subareas )
        {
            sb.append( s.toString( padDistance, longestObjectName + 2 ) );
        }

        sb.append( "\n\n" );
        return sb.toString();
    }

    private String rainGagesToString( int padDistance )
    {
        StringBuilder sb = new StringBuilder();
        //Section Title
        sb.append( "[RAINGAGES]\n" );

        //Header
        sb.append( StringUtils.rightPad( ";;Name", padDistance ) );
        sb.append( StringUtils.rightPad( "Format", padDistance ) );
        sb.append( StringUtils.rightPad( "Interval", padDistance ) );
        sb.append( StringUtils.rightPad( "SCF", padDistance ) );
        sb.append( "Source\n" );

        //Separator Line
        sb.append( StringUtils.rightPad( ";;--------------", padDistance ) );
        sb.append( StringUtils.rightPad( "--------------", padDistance ) );
        sb.append( StringUtils.rightPad( "--------------", padDistance ) );
        sb.append( StringUtils.rightPad( "--------------", padDistance ) );
        sb.append( StringUtils.rightPad( "--------------", padDistance ) );
        sb.append( "\n" );

        for ( RainGage rg : rainGages )
        {
            sb.append( rg.toString( padDistance ) );
        }

        sb.append( "\n\n" );
        return sb.toString();
    }

    private String titleToString()
    {
        StringBuilder sb = new StringBuilder();
        sb.append( "[TITLE]\n" );
        sb.append( ";;Project Title/Notes\n" );
        sb.append( title.toString() );
        sb.append( "\n" );
        return sb.toString();
    }

    private String outfallsToString( int padDistance )
    {
        StringBuilder sb = new StringBuilder();
        //Section Title
        sb.append( "[OUTFALLS]\n" );

        //Header
        sb.append( StringUtils.rightPad( ";;Name", longestObjectName + 2 ) );
        sb.append( StringUtils.rightPad( "Elevation", padDistance ) );
        sb.append( StringUtils.rightPad( "Type", padDistance ) );
        sb.append( StringUtils.rightPad( "Stage Data", padDistance ) );
        sb.append( StringUtils.rightPad( "Gated", padDistance ) );
        sb.append( StringUtils.rightPad( "Route To", longestObjectName + 2 ) );
        sb.append( "\n" );
        //Separator Line
        sb.append( StringUtils.rightPad( ";;--------------", longestObjectName + 2 ) );
        sb.append( StringUtils.rightPad( "--------------", padDistance ) );
        sb.append( StringUtils.rightPad( "--------------", padDistance ) );
        sb.append( StringUtils.rightPad( "--------------", padDistance ) );
        sb.append( StringUtils.rightPad( "--------------", padDistance ) );
        sb.append( StringUtils.rightPad( "--------------", longestObjectName + 2 ) );
        sb.append( "\n" );

        for ( Outfall o : outfalls )
        {
            sb.append( o.toString( padDistance, longestObjectName + 2 ) + "\n" );

        }

        sb.append( "\n\n" );
        return sb.toString();
    }

    private String filesToString( int padDistance )
    {
        StringBuilder sb = new StringBuilder();

        sb.append( "[FILES]\n" );
        sb.append( ";;Interfacing Files\n" );
        for ( SwmmFile f : files )
        {
            sb.append( f.toString( padDistance ) );
        }

        sb.append( "\n" );
        return sb.toString();
    }

    private String conduitsToString( int padDistance )
    {
        StringBuilder sb = new StringBuilder();
        //Section Title
        sb.append( "[CONDUITS]\n" );

        //Header
        sb.append( StringUtils.rightPad( ";;Name", longestObjectName + 2 ) );
        sb.append( StringUtils.rightPad( "From Node", longestObjectName + 2 ) );
        sb.append( StringUtils.rightPad( "To Node", padDistance ) );
        sb.append( StringUtils.rightPad( "Length", padDistance ) );
        sb.append( StringUtils.rightPad( "Roughness", padDistance ) );
        sb.append( StringUtils.rightPad( "InOffset", padDistance ) );
        sb.append( StringUtils.rightPad( "OutOffset", padDistance ) );
        sb.append( StringUtils.rightPad( "InitFlow", padDistance ) );
        sb.append( StringUtils.rightPad( "MaxFlow", padDistance ) );
        sb.append( "\n" );
        //Separator Line
        sb.append( StringUtils.rightPad( ";;--------------", longestObjectName + 2 ) );
        sb.append( StringUtils.rightPad( "--------------", longestObjectName + 2 ) );
        sb.append( StringUtils.rightPad( "--------------", padDistance ) );
        sb.append( StringUtils.rightPad( "--------------", padDistance ) );
        sb.append( StringUtils.rightPad( "--------------", padDistance ) );
        sb.append( StringUtils.rightPad( "--------------", padDistance ) );
        sb.append( StringUtils.rightPad( "--------------", padDistance ) );
        sb.append( StringUtils.rightPad( "--------------", padDistance ) );
        sb.append( StringUtils.rightPad( "--------------", padDistance ) );

        sb.append( "\n" );

        for ( Conduit c : conduits )
        {
            sb.append( c.toString( padDistance, longestObjectName + 2 ) );
        }

        sb.append( "\n\n" );
        return sb.toString();
    }

    private String junctionsToString( int padDistance )
    {
        StringBuilder sb = new StringBuilder();
        //Section Title
        sb.append( "[JUNCTIONS]\n" );

        //Header
        sb.append( StringUtils.rightPad( ";;Name", longestObjectName + 2 ) );
        sb.append( StringUtils.rightPad( "Elevation", padDistance ) );
        sb.append( StringUtils.rightPad( "MaxDepth", padDistance ) );
        sb.append( StringUtils.rightPad( "InitDepth", padDistance ) );
        sb.append( StringUtils.rightPad( "SurDepth", padDistance ) );
        sb.append( StringUtils.rightPad( "Aponded", padDistance ) );
        sb.append( "\n" );
        //Separator Line
        sb.append( StringUtils.rightPad( ";;--------------", longestObjectName + 2 ) );
        sb.append( StringUtils.rightPad( "--------------", padDistance ) );
        sb.append( StringUtils.rightPad( "--------------", padDistance ) );
        sb.append( StringUtils.rightPad( "--------------", padDistance ) );
        sb.append( StringUtils.rightPad( "--------------", padDistance ) );
        sb.append( StringUtils.rightPad( "--------------", padDistance ) );

        sb.append( "\n" );

        for ( Junction c : junctions )
        {
            sb.append( c.toString( padDistance, longestObjectName + 2 ) );
        }

        sb.append( "\n\n" );
        return sb.toString();
    }

    private String lidControlsToString( int padDistance )
    {
        StringBuilder sb = new StringBuilder();
        //Section Title
        sb.append( "[LID_CONTROLS]\n" );

        //Header
        sb.append( StringUtils.rightPad( ";;Name", longestObjectName + 2 ) );
        sb.append( StringUtils.rightPad( "Type/Layer Parameters", padDistance ) );

        sb.append( "\n" );
        //Separator Line
        sb.append( StringUtils.rightPad( ";;--------------", longestObjectName + 2 ) );
        sb.append( StringUtils.rightPad( "--------------", padDistance ) );

        sb.append( "\n" );

        for ( LIDControl lc : lidControls )
        {
            sb.append( lc.toString( padDistance, longestObjectName + 2 ) );
        }

        sb.append( "\n\n" );
        return sb.toString();
    }

    private String lidUsagesToString( int padDistance )
    {
        StringBuilder sb = new StringBuilder();
        //Section Title
        sb.append( "[LID_USAGE]\n" );

        //Header
        sb.append( StringUtils.rightPad( ";;Subcatchment", longestObjectName + 2 ) );
        sb.append( StringUtils.rightPad( "LID Process", longestObjectName + 2 ) );
        sb.append( StringUtils.rightPad( "Number", padDistance ) );
        sb.append( StringUtils.rightPad( "Area", padDistance ) );
        sb.append( StringUtils.rightPad( "Width", padDistance ) );
        sb.append( StringUtils.rightPad( "InitSat", padDistance ) );
        sb.append( StringUtils.rightPad( "FromImp", padDistance ) );
        sb.append( StringUtils.rightPad( "ToPerv", padDistance ) );
        sb.append( StringUtils.rightPad( "RptFile", padDistance ) );
        sb.append( StringUtils.rightPad( "DrainTo", longestObjectName + 2 ) );
        sb.append( StringUtils.rightPad( "FromPerv", padDistance ) );

        sb.append( "\n" );
        //Separator Line
        sb.append( StringUtils.rightPad( ";;--------------", longestObjectName + 2 ) );
        sb.append( StringUtils.rightPad( "--------------", longestObjectName + 2 ) );
        sb.append( StringUtils.rightPad( "--------------", padDistance ) );
        sb.append( StringUtils.rightPad( "--------------", padDistance ) );
        sb.append( StringUtils.rightPad( "--------------", padDistance ) );
        sb.append( StringUtils.rightPad( "--------------", padDistance ) );
        sb.append( StringUtils.rightPad( "--------------", padDistance ) );
        sb.append( StringUtils.rightPad( "--------------", padDistance ) );
        sb.append( StringUtils.rightPad( "--------------", padDistance ) );
        sb.append( StringUtils.rightPad( "--------------", longestObjectName + 2 ) );
        sb.append( StringUtils.rightPad( "--------------", padDistance ) );

        sb.append( "\n" );

        for ( LIDUsage lu : lidUsages )
        {
            sb.append( lu.toString( padDistance, longestObjectName + 2 ) );
        }

        sb.append( "\n\n" );
        return sb.toString();
    }

    private String xSectionsToString( int padDistance )
    {
        StringBuilder sb = new StringBuilder();
        //Section Title
        sb.append( "[XSECTIONS]\n" );

        //Header
        sb.append( StringUtils.rightPad( ";;Link", longestObjectName + 2 ) );
        sb.append( StringUtils.rightPad( "Shape", padDistance ) );
        sb.append( StringUtils.rightPad( "Geom1", padDistance ) );
        sb.append( StringUtils.rightPad( "Geom2", padDistance ) );
        sb.append( StringUtils.rightPad( "Geom3", padDistance ) );
        sb.append( StringUtils.rightPad( "Geom4", padDistance ) );
        sb.append( StringUtils.rightPad( "Barrels", padDistance ) );
        sb.append( StringUtils.rightPad( "Culvert", padDistance ) );
        sb.append( "\n" );

        sb.append( StringUtils.rightPad( ";;--------------", longestObjectName + 2 ) );
        sb.append( StringUtils.rightPad( "--------------", padDistance ) );
        sb.append( StringUtils.rightPad( "--------------", padDistance ) );
        sb.append( StringUtils.rightPad( "--------------", padDistance ) );
        sb.append( StringUtils.rightPad( "--------------", padDistance ) );
        sb.append( StringUtils.rightPad( "--------------", padDistance ) );
        sb.append( StringUtils.rightPad( "--------------", padDistance ) );
        sb.append( StringUtils.rightPad( "--------------", padDistance ) );

        sb.append( "\n" );

        for ( XSection x : xSections )
        {
            sb.append( x.toString( padDistance, longestObjectName + 2 ) );
        }

        sb.append( "\n\n" );
        return sb.toString();

    }

    private void getLongestObjectName()
    {
        //Subcatchments
        for ( Subcatchment s : subcatchments )
        {
            if ( s.name.length() > longestObjectName )
            {
                longestObjectName = s.name.length();
            }
        }

        //RainGages
        for ( RainGage r : rainGages )
        {
            if ( r.name.length() > longestObjectName )
            {
                longestObjectName = r.name.length();
            }
        }

        //Junctions
        for ( Junction j : junctions )
        {
            if ( j.name.length() > longestObjectName )
            {
                longestObjectName = j.name.length();
            }
        }

        //Conduits
        for ( Conduit c : conduits )
        {
            if ( c.name.length() > longestObjectName )
            {
                longestObjectName = c.name.length();
            }
        }

        //Outfalls
        for ( Outfall o : outfalls )
        {
            if ( o.name.length() > longestObjectName )
            {
                longestObjectName = o.name.length();
            }
        }

        //LID Controls
        //Outfalls
        for ( LIDControl lc : lidControls )
        {
            if ( lc.name.length() > longestObjectName )
            {
                longestObjectName = lc.name.length();
            }
        }
    }

    public Junction getJunctionByID( String name )
    {
        for ( Junction j : junctions )
        {
            if ( j.name.equalsIgnoreCase( name ) )
            {
                return j;
            }
        }

        return null;
    }

    public Subcatchment getSubcatchmentByID( String name )
    {
        for ( Subcatchment s : subcatchments )
        {
            if ( s.name.equalsIgnoreCase( name ) )
            {
                return s;
            }
        }

        return null;
    }
    
    public LIDUsage getLIDUsageByID( String name )
    {
        for ( LIDUsage lu : lidUsages )
        {
            if ( lu.subcatchment.equalsIgnoreCase( name ) )
            {
                return lu;
            }
        }

        return null;
    }

    public Outfall getOutfallByID( String name )
    {
        for ( Outfall o : outfalls )
        {
            if ( o.name.equalsIgnoreCase( name ) )
            {
                return o;
            }
        }

        return null;
    }

}