<?php

/**
 *
 * @author Peter Csubak (chuby)
 * @copyright Peter Csubak (chuby)
 *
 * //Annotations:
 * @Stateful
 */
class AdministratorSF {

	/**
	 * 
	 * @XmlMethod(schemaName="desktop", methodName="login")
	 */
	public function login($userName, $passCode){
		$where = new Where("UserName", "=", $userName);
		$where->_and("PassCode", "=", $passCode);
		$res = PersistenceHandler::getInstance()->selectEntities("Administrator", $where);

		if (is_array($res) && (count($res) == 1)){
			SecureSession::getInstance()->setLoggedInUser($res[0]);
		}else{
			throw new Exception("Hibás felhasználói név, vagy jelszó.");
		}

		return "OK";
	}

	/**
	 * 
	 * @param Dvd $dvd
	 * @XmlMethod(schemaName="desktop", methodName="addDvd", paramsName={"advd" = "dvd"}, paramsType={"dvd" = "Dvd"}) 
	 * @GrantTo("administrator")
	 */
	public function addDvd($dvd){
		if (!$dvd->hasTableCreated()){
			$dvd->createTheTable();
		}
		$dvd->save();
	}

	/**
	 * 
	 * @XmlMethod(schemaName="desktop", methodName="getDvdList")
	 * @XmlArray(schemaName="desktop", elementName="e", valueType="Dvd")
	 * @GrantTo("administrator")
	 */
	public function getDvdList(){
		return PersistenceHandler::getInstance()->selectEntities("Dvd", "1");
	}

	/**
	 * 
	 * @param Dvd $dvd
	 * @XmlMethod(schemaName="desktop", methodName="modifyDvd", paramsName={"advd" = "dvd"}, paramsType={"dvd" = "Dvd"}) 
	 * @GrantTo("administrator")
	 */
	public function modifyDvd($dvd){
		$ndvd = new Dvd();
		try{
			$ndvd->autoLoad($dvd->ID);
			$ndvd->updateFrom($dvd);
		}catch (PersistenceException $e){
			if ($e->getCode() == 214){
				throw new Exception("Hiba: A módosítani kívánt elem már nem létezik az adatbázisban.");
			}else{
				throw $e;
			}
		}
	}
	
	/**
	 * 
	 * @param Dvd $dvd
	 * @XmlMethod(schemaName="desktop", methodName="removeDvd", paramsName={"advd" = "dvd"}, paramsType={"dvd" = "Dvd"}) 
	 * @GrantTo("administrator")
	 */
	public function removeDvd($dvd){
		$ndvd = new Dvd();
		$ndvd->autoLoad($dvd->ID);
		$ndvd->remove();
	}
	
	/**
	 * 
	 * @XmlMethod(schemaName="desktop", methodName="getCustomerList")
	 * @XmlArray(schemaName="desktop", elementName="e", valueType="Customer")
	 * @GrantTo("administrator")
	 */
	public function getCustomerList(){
		return PersistenceHandler::getInstance()->selectEntities("Customer", "1");
	}
	
	/**
	 * 
	 * @param Customer $customer
	 * @XmlMethod(schemaName="desktop", methodName="removeCustomer",paramsName={"acustomer" = "customer"}, paramsType={"customer" = "Customer"}) 
	 * @GrantTo("administrator")
	 */
	public function removeCustomer($customer){
		$nCustomer = new Customer();
		$nCustomer->autoLoad($customer->ID);
		$nCustomer->remove();
	}
	
	/**
	 * 
	 * @XmlMethod(schemaName="desktop", methodName="getOrderList")
	 * @XmlArray(schemaName="desktop", elementName="e", valueType="Order")
	 * @GrantTo("administrator")
	 */
	public function getOrderList(){
		return PersistenceHandler::getInstance()->selectEntities("Order", "1");
	}
	
	/**
	 * 
	 * @param Order $order
	 * @XmlMethod(schemaName="desktop", methodName="removeOrder", paramsName={"aorder" = "order"}, paramsType={"order" = "Order"}) 
	 * @GrantTo("administrator")
	 */
	public function removeOrder($order){
		$nOrder = new Order();
		$nOrder->autoLoad($order->ID);
		$nOrder->remove();
	}
	
	/**
	 * 
	 * @XmlMethod(schemaName="desktop", methodName="getDeliveredOrders")
	 * @XmlArray(schemaName="desktop", elementName="e", valueType="Order")
	 * @GrantTo("administrator")
	 */
	public function getDeliveredOrders(){
		$where = new Where("Status", "=", "after");
		return PersistenceHandler::getInstance()->selectEntities("Order", $where);
	}
	/**
	 * 
	 * @param Order $order
	 * @XmlMethod(schemaName="desktop", methodName="setOrderNeed", paramsName={"aOrder" = "order"}, paramsType={"order" = "Order"}) 
	 * @GrantTo("administrator")
	 */
	public function setOrderNeed($order){
		$nOrder = new Order();
		$nOrder->autoLoad($order->ID);
		$nOrder->status = "need";
		$nOrder->save();
	}
	
	/**
	 * 
	 * @param Transporter $atransporter
	 * @return unknown_type
	 * 
	 * @XmlMethod(schemaName="desktop", methodName="addTransporter", paramsType={"atransporter" = "Transporter"})
	 * @GrantTo("administrator")
	 */
	public function addTransporter($atransporter){
		if (!$atransporter->hasTableCreated()){
			$atransporter->createTheTable();
		}
		$atransporter->save();
	}
	
	/**
	 * 
	 * @param Transporter $atransporter
	 * @return unknown_type
	 * 
	 * @XmlMethod(schemaName="desktop", methodName="delTransporter", paramsType={"atransporter" = "Transporter"})
	 * @GrantTo("administrator")
	 */
	public function delTransporter($atransporter){
		$nt = new Transporter();
		$nt->autoLoad($atransporter->ID);
		$nt->remove();
	}
	
	/**
	 * 
	 * @XmlMethod(schemaName="desktop", methodName="getTransporterList")
	 * @XmlArray(schemaName="desktop", elementName="e", valueType="Transporter")
	 * @GrantTo("administrator")
	 */
	public function getTransporterList(){
		return PersistenceHandler::getInstance()->selectEntities("Transporter", "1");
	}

}
?>