package com.galaxygoby;

import java.lang.* ;
import java.io.* ;
import java.util.* ;
import com.galaxygoby.* ;

public class TestShmStTbl {
	TestShmStTbl() {
	}
	public int checkParameter ( String args[] ) {

		if ( args.length == 1 ) {
			return ( 0 ) ;
		} else {
			System.out.println ( "Usage : TestShmStTbl [パラメータファイル名]") ;
			return ( -1 ) ;
		}
	}
	public int execShmStaticTable ( String[] args ) throws IOException {

		BufferedReader br = null ;
		PrintWriter p0_pw = null ;
		String		p1_Table , p2_Id ;
		String line ;
		String[] param ;
		try {
			br = new BufferedReader( new InputStreamReader( new FileInputStream( args[0] ))) ;
		} catch ( IOException e ) {
			System.out.println ( "Usage : TestShmStTbl [パラメータファイル名]") ;
			System.out.println ( "        パラメータファイルがありません") ;
			return (-1) ;
		}
		Map<String,String>   in  = new HashMap<String,String>() ;
		Map<String,String[]> out = new HashMap<String,String[]>() ;
		for ( ;(line = br.readLine()) != null ; ) {
			param = line.split(",") ;
			if ( param.length < 3 ) {
				System.out.println ( "× " + line ) ;
				continue ;
			}
			try {
				p0_pw = new PrintWriter ( new OutputStreamWriter ( new FileOutputStream ( param[0] ))) ;
			} catch ( IOException e ) {
				System.out.println ( "        パラメータファイルがありません") ;
				continue ;
			}
			p1_Table = param[1] ;
			p2_Id    = param[2] ;
			in.clear() ;
			int i ;
			for ( i=3 ; i<param.length ; i++) {
				String[] p3_keyValue = param[i].split ( "=" ) ;
				if ( p3_keyValue.length != 2 ) {
					System.out.println ( "× " + line ) ;
					break ;
				}
				in.put ( p3_keyValue[0],p3_keyValue[1] ) ;
			}
			if ( i < param.length ) {
				continue ;
			}
			out = ShmStaticTable.getTableData ( p1_Table , p2_Id , in ) ;

			if ( out == null ) {
				System.out.println ( "空 " + line ) ;
				continue ;
			} else {
				System.out.println ( "〇 " + line ) ;
			}

			List<String> keyArray = new ArrayList<String>() ;
			String tb = "" ;
			for (String key : out.keySet()) {
				keyArray.add ( key ) ;
				p0_pw.print ( tb + key ) ;
				tb = "\t" ;
			}
			p0_pw.print ( "\n" ) ;
			String[][] outHyou = new String [ keyArray.size() ][ out.get(keyArray.get(0)).length ] ;
			// System.out.println (  "keyArray.size()=" + keyArray.size()
			//                     + " out.get(keyArray.get(0)).length=" + out.get(keyArray.get(0)).length ) ;
			for ( i = 0 ; i < keyArray.size() ; i ++ ) {
				String[] outTemp = out.get ( keyArray.get(i) ) ;
				for ( int j = 0 ; j < out.get ( keyArray.get(i) ).length ; j++ ) {
					outHyou [i] [j] = outTemp[j] ;
				}
			}
			for ( int j = 0 ; j < outHyou[0].length ; j++ ) {
				tb = "" ;
				for ( i = 0 ; i < keyArray.size() ; i++ ) {
					p0_pw.print ( tb + outHyou[i][j] ) ;
					tb = "\t" ;
				}
				p0_pw.print ( "\n" ) ;
			}
			p0_pw.close() ;
		}
		return ( 0 ) ;
	}
	public static void main ( String args[] ) throws Exception {

		TestShmStTbl testSST = new TestShmStTbl() ;
		int rc0 = testSST.checkParameter ( args ) ;
		if ( rc0 != 0 ) System.exit ( rc0 ) ;
		int rc1 = testSST.execShmStaticTable ( args ) ;

	}
}
