/*

 * Delivered to the U.S. Government in Accordance with Contract FA8650-10-C-7080.

 *

 * EXPORT CONTROLLED

 * ECCN#   EAR99

 * Warning: This document contains technical data which is controlled under the

 * Bureau of Industry & Security's Export Administration Regulations and may not be

 * exported or re-exported without authorization by the Bureau of Industry & Security.

 * Diversion contrary to US law is prohibited.

 *

 * (c) 2011 United Technologies Corp.

 * Developed with the sponsorship of the Defense Advanced Research Projects Agency (DARPA).

 *

 * Permission is hereby granted, free of charge, to any person obtaining a copy of this data,

 * including any software or models in source or binary form, as well as any drawings, 

 * specifications, and documentation (collectively "the Data"), to deal in the Data without 

 * restriction, including without limitation the rights to use, copy, modify, merge, publish,

 * distribute, sublicense, and/or sell copies of the Data, and to permit persons to whom the Data

 * is furnished to do so, subject to the following conditions:

 * 

 * The above copyright notice and this permission notice shall be included in all copies or

 * substantial portions of the Data.

 *

 * THE DATA IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING

 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND

 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS, SPONSORS, DEVELOPERS, CONTRIBUTORS, OR

 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION

 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE DATA OR THE

 * USE OR OTHER DEALINGS IN THE DATA.

*/



package main;



import java.io.BufferedReader;

import java.io.ByteArrayInputStream;

import java.io.FileReader;

import java.io.IOException;



import org.jdom.Document;

import org.jdom.input.SAXBuilder;



import proxy.clients.RepositoryProxy;



/**

 * Utility saves a mission and platform into the repository

 */

public class RepositorySave {

	private static final String RepositoryHost = "ae438610";



	/**

	 * @param args 0) platform xml file

	 *             1) mission xml file

	 */

	public static void main(String[] args) {

		if (args.length != 2) {

			System.out.println("Usage: RepositorySave platform.xml mission.xml");

			System.exit(1);

		}

		

		String platxml    = args[0];

		String missionxml = args[1];

		

		// Services needed

		RepositoryProxy repository = new RepositoryProxy(RepositoryHost);

		

		// Store the repository

		try {

			String     platform = read(platxml);

		 	SAXBuilder builder  = new SAXBuilder();

	    Document   doc      = builder.build(new ByteArrayInputStream(platform.getBytes()));

			repository.storePlatform(doc);

			

		} catch (Exception ex) {

			System.out.println(ex.getMessage());

			ex.printStackTrace();

			System.exit(2);

		}

		

		// Store the mission

		try {

			String     mission = read(missionxml);

		 	SAXBuilder builder  = new SAXBuilder();

	    Document   doc      = builder.build(new ByteArrayInputStream(mission.getBytes()));

			repository.storeMission(doc);

			

		} catch (Exception ex) {

			System.out.println(ex.getMessage());

			ex.printStackTrace();

			System.exit(2);

		}

	}





  /**

   * Reads text from a file 

   */

  public static String read(String filename) throws IOException {

  	StringBuilder sb = new StringBuilder();

  	BufferedReader bufferedReader = null;

  	try {

  		bufferedReader = new BufferedReader(new FileReader(filename));



  		String line = null;



  		while ((line = bufferedReader.readLine()) != null) {

  			sb.append(line);

  			sb.append('\n');

  		}



  	} finally {

  		try {

  			if (bufferedReader != null)

  				bufferedReader.close();

  		} catch (IOException ex) {

  			ex.printStackTrace();

  		}

  	}

  	return sb.toString();

  }   

}

