/*
 * 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 meta.repository.service;

import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.XMLOutputter;

import meta.rmi.Argument;
import meta.rmi.Log;
import meta.rmi.ServiceHandler;
import meta.rmi.ServiceRequest;
import meta.rmi.ServiceResponse;
/**
 *  Store a platform into the repository.
 *  If the platform already exists, this will replace it. 
 */
public class StorePlatform extends ServiceHandler {

	/* (non-Javadoc)
	 * @see meta.rmi.ServiceHandler#handle(meta.rmi.ServiceRequest, meta.rmi.ServiceResponse)
	 */
	@Override
	public void handle(ServiceRequest request, ServiceResponse response) {
		Argument platformArg = request.getArgument("platform", "string");
		Argument levelArg    = request.getArgument("level",    "integer");
		if (platformArg == null ||  levelArg == null) {
			String err = "Missing or invalid platform/level arguments when storing a platform";
			response.setStatus(ServiceResponse.SERVER_ERROR);
			response.setStatusMsg(err);
			Log.err(err);		

		} else {
			String       platform    = platformArg.getValue();
			int          level       = levelArg.getIntValue();

			// Save all composite args that match the platform
			List<?>      list        = request.getCompositeArgByName(platform);
			Element      plat        = new Element("platform");
			plat.setAttribute("name", platform);
			plat.setAttribute("level", String.format("%d", level));
			Document     doc         = new Document(plat);
			for (Object object : list) {
				Element ele = (Element) object;
				plat.addContent((Element) ele.clone());
			}
	
			XMLOutputter xmlout  = new XMLOutputter();
			Repository   repos   = new RepositoryImpl();
			String       content = xmlout.outputString(doc); 
			if (! repos.savePlatform(content, platform, level)) {
				String err = String.format("Error saving platform \"%s\" level %d",
				  	                       platform, level);
				response.setStatus(ServiceResponse.SERVER_ERROR);
				response.setStatusMsg(err);
				Log.err(err);		
			}
		}
	}
	 
}
