/*
 * 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.Attribute;
import org.jdom.DataConversionException;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.XMLOutputter;

import meta.rmi.Log;
import meta.rmi.ServiceHandler;
import meta.rmi.ServiceRequest;
import meta.rmi.ServiceResponse;
/**
 *  Store (persist) architectures into the repository for
 *  a given platform and abstraction level.
 */
public class StoreArchitectures extends ServiceHandler {
	 
	public void handle(ServiceRequest request, ServiceResponse response) {
		List<?>  list = request.getCompositeArg("engine", "architectures");
		XMLOutputter outp = new XMLOutputter();
		for ( Object object : list ) {
			Element e  = (Element) object;
			Element eA = e.getChild("architecture");
			Element eAclone = (Element) eA.clone();
			Document doc = new Document(eAclone);
	    	String content = outp.outputString(doc); 
	    	boolean saved = store(eA,content);
	    	if ( ! saved ) {
	    		response.setStatus(ServiceResponse.SERVER_ERROR);
	    		response.setStatusMsg("Problem saving architecture");
	    		Log.err("problem saving architecture");
	    	}
		}		 
	}
	/**
	 * Store the architecture into the repository
	 */
	private boolean store(Element e, String content) {
		boolean saved = false;
		Attribute idA = e.getAttribute("id");
		Attribute levelA = e.getAttribute("level");
		String platform = e.getAttributeValue("platform");
		if ( idA == null) {
			Log.err("invalid store architecture request : id attribute not found");
			return false;
		}
		if ( levelA == null){
			Log.err("invalid store architecture request : level attribute not found");
			return false;
		}
		if ( platform == null ){
			Log.err("invalid store architecture request : platform attribute not found");
			return false;
		}
		int id ;
		int level;
		try {
			id = idA.getIntValue();
			level = levelA.getIntValue();
		} catch (DataConversionException e1) {
			Log.err("invalid store architecture request : id or level not integer "); 
			return false; 
		} 
		Repository repository = new RepositoryImpl();
		saved = repository.saveArchitecture(content, id, platform, level);
		if ( saved ) {
			 String msg = String.format("Repository: stored architecture %d for platform %s level %d", id,platform,level);
			 Log.info(msg);
		}
		return saved;
	}
}
