Index: trunk/prototype/api/controller.php
===================================================================
--- trunk/prototype/api/controller.php	(revision 5415)
+++ trunk/prototype/api/controller.php	(revision 5437)
@@ -63,106 +63,4 @@
 	    return self::$cache->put( $id, $data, $expires, $compressed );
 	}
-
-	public static function put( $URI, $data, $txIds = false )
-	{
-	    try
-	    {
-		$URI = self::serviceName( $URI );
-
-		if( $commit = !$txIds )
-			    $txIds = array();
-
-		if( !isset( self::$tx[ $URI['service'] ] ) )
-		{
-			    self::call( 'begin', $URI );
-			    self::$tx[ $txIds[] = $URI['service'] ] = array();
-	    }
-
-		$method = $data ? isset( $data['id'] ) ?
-			  'update' : 'create' : 'delete';
-
-		$links = self::links( $URI['concept'] );
-
-		$Tx = array( 'order' => self::$txID++ );
-
-		$postpone = array();
-
-		if( $data )
-		{
-		    $URI['id'] = isset( $data['id'] ) ? $data['id'] : false;
-
-		    foreach( $links as $linkName => $linkTarget )
-		    {
-				if( isset( $data[$linkName] ) && is_array( $data[$linkName] ) )
-				{
-					if( self::isConcept( $linkName ) )
-						$data[$linkName] = self::put( array( 'concept' => $linkTarget ), $data[$linkName], &$txIds );
-					else
-						$postpone[$linkTarget] = $data[$linkName];
-				}
-	}
-		}
-		else
-		  $URI['id'] = isset( $data['id'] ) ? $data['id'] : $URI['id'];
-
-		$result = Controller::call( $method, $URI, $data, false, false, true );
-
-		if( !is_bool( $result ) && !is_string( $result ) && isset( $result['id'] ) )
-		      $URI['id'] = $result['id'];
-
-		self::$tx[ $URI['service'] ][] = array_merge( $Tx, array( 'id' => $URI['id'], 'concept' => $URI['concept'], 'method' => $method, 'service' => $URI['service'], 'rollback' => !!!$result ) );
-
-		foreach( $postpone as $linkTarget => $dt )
-		      foreach( $dt as $ii => $value )
-	{
-			  if( !is_array( $value ) )
-			    $value = array( 'id' => $value );
-
-			  $value[ $URI['concept'] ] = $URI['id'];
-
-			  self::put( array( 'concept' => $linkTarget ), $value, &$txIds );
-	}
-
-		if( $commit )
-	{
-		      $result = array();
-
-		      for( $i = count( $txIds ) - 1; $i >= 0; $i-- )
-		      {
-			      $currentTx = self::$tx[ $txIds[$i] ];
-			      unset( self::$tx[ $txIds[$i] ] );
-
-			      if( !self::commit( array( 'service' => $txIds[$i] ), $currentTx ) )
-			      {
-				  self::rollback( array( 'service' => $txIds[$i] ), $currentTx );
-
-				  foreach( $currentTx as $i => $st )
-				      $currentTx[$i][ 'rollback' ] = true;
-	}
-
-			      $result = array_merge( $result, $currentTx );
-		      }
-			  
-			  self::$txID = 0;
-
-		      return( $result );
-		}
-
-	    }
-	    catch( Exception $e )
-	{
-		if( !self::fallback( $e ) )
-		    self::closeAll();
-
-		return( false );
-	    }
-	
-	    return( $URI['id'] );
-	}
-	
-	public static function get()
-	    {
-	    
-	    }
 
 	public static function find( $URI, $params = false, $criteria = false )
@@ -462,5 +360,5 @@
 	    $result = array( $eventType => array() );
         
-        if( is_array( $commitList ) )
+	    if( is_array( $commitList ) )
 	        foreach( $commitList as $i => $tx )
 	        {
@@ -536,5 +434,5 @@
 
 	//TODO: Compatibilizar as configs relativas aos modulos, adicionando os mesmos nas options passadas
-	public static function call( $method, $URI, $properties = false, $options = false, $service = false, $noTX = false )
+	public static function call( $method, $URI, $properties = false, $options = false, $service = false )
 	{
 	    try
@@ -550,4 +448,6 @@
 		    return( empty($params['properties']) ? false : $params['properties'] );
 
+		
+
 		if( $params && !$params['service'] )
 		{
@@ -559,10 +459,12 @@
 		if( isset($params['URI']['service']) )
 		{
-		    if( $commit = (!isset(self::$tx[ $params['URI']['service'] ]) && ( $method === 'create' ||
-										       $method === 'update' ||
-										       $method === 'delete' )) )
+		    if( $method === 'create' || $method === 'update' || $method === 'delete' )
 		    {
-			self::call( 'begin', $params['URI'] );
-			self::$tx[ $params['URI']['service'] ] = true;
+			if( $commit = !isset(self::$tx[ $params['URI']['service'] ])  )
+			{
+			    self::call( 'begin', $params['URI'] );
+			}
+
+			$TX = array();
 		    }
 
@@ -573,5 +475,5 @@
 		if( $params['service'] )
 		    switch( $method )
-	    { 
+		    { 
 			case 'find': $return = $params['service']->find( $params['URI'], $params['properties'], $params['criteria'] ); break;
 
@@ -596,8 +498,10 @@
 			case 'parse': $return = $params['service']->parse( $params['properties'], $params['criteria'] ); break;
 
+			case 'analize': $return = $params['service']->analize( $params['properties'], $params['criteria'] ); break;
+
 			case 'format': $return = $params['service']->format( $params['properties'], $params['criteria'] ); break;
-                        
-                        default:  $return = $params['service']->$method( $params['properties'], $params['criteria'] ); break;
-	    }
+
+			default : $return = $params['service']->$method( $params['properties'], $params['criteria'] );
+		    }
 
 		if( isset($return) && $return !== false )
@@ -605,9 +509,23 @@
 
 		if( isset($params['URI']['service']) )
-		{
 		    if( !self::fire( 'after', $method, $params, $original, true ) )
 			return( empty($params['properties']) ? false : $params['properties'] );
 
-		    if( $commit )
+		if( isset($URI['concept']) )
+		    self::fire( 'after', $method, $params, $original );
+
+		if( empty($params['properties']) )
+		    $params['properties'] = false;
+
+		if( isset( $TX ) )
+		{
+		    $TX['rollback'] = !!!$params['properties'];
+
+		    if( $params['properties'] && isset($params['properties']['id']) )
+			$TX['id'] = $params['properties']['id'];
+
+		    self::$tx[ $params['URI']['service'] ][] = array_merge( $TX, $original['URI'], array( 'service' => $params['URI']['service'], 'method' => $method ) );
+
+		    if( isset($commit) && $commit )
 		    {
 			if( !self::call( 'commit', $params['URI'], false, self::$tx[ $params['URI']['service'] ] ) )
@@ -617,7 +535,4 @@
 		    }
 		}
-
-		if( isset($URI['concept']) && !self::fire( 'after', $method, $params, $original ) )
-		    return( empty($params['properties']) ? false : $params['properties'] );
 	    }
 	    catch( Exception $e )
@@ -629,5 +544,5 @@
 	    }
 
-	    return( empty($params['properties']) ? false : $params['properties'] );
+	    return( $params['properties'] ); 
 	}
 
@@ -637,4 +552,107 @@
 	    return( true );
 	}
+
+	public static function put( $URI, $data, $txIds = false )
+	{
+	    try
+	    {
+		$URI = self::serviceName( $URI );
+
+		if( $commit = !$txIds )
+		    $txIds = array();
+
+		if( !isset( self::$tx[ $URI['service'] ] ) )
+		{
+		    self::call( 'begin', $URI );
+		    self::$tx[ $txIds[] = $URI['service'] ] = array();
+		}
+
+		$method = $data ? isset( $data['id'] ) ?
+			  'update' : 'create' : 'delete';
+
+		$links = self::links( $URI['concept'] );
+
+		$order = self::$txID++;
+
+		$postpone = array();
+
+		if( $data )
+		{
+		    $URI['id'] = isset( $data['id'] ) ? $data['id'] : false;
+
+		    foreach( $links as $linkName => $linkTarget )
+		    {
+			if( isset( $data[$linkName] ) && is_array( $data[$linkName] ) )
+			{
+			    if( self::isConcept( $linkName ) )
+				    $data[$linkName] = self::put( array( 'concept' => $linkTarget ), $data[$linkName], &$txIds );
+			    else
+				    $postpone[$linkTarget] = $data[$linkName];
+			}
+		    }
+		}
+		else
+		  $URI['id'] = isset( $data['id'] ) ? $data['id'] : $URI['id'];
+
+		$result = Controller::call( $method, $URI, $data, false, false, true );
+
+		if( !is_bool( $result ) && !is_string( $result ) && isset( $result['id'] ) )
+		      $URI['id'] = $result['id'];
+
+		self::$tx[ $URI['service'] ][ count(self::$tx[ $URI['service'] ]) - 1 ]['order'] = $order;
+		self::$tx[ $URI['service'] ][ count(self::$tx[ $URI['service'] ]) - 1 ]['id'] = $URI['id'];
+
+		foreach( $postpone as $linkTarget => $dt )
+		      foreach( $dt as $ii => $value )
+		      {
+			  if( !is_array( $value ) )
+			    $value = array( 'id' => $value );
+
+			  $value[ $URI['concept'] ] = $URI['id'];
+
+			  self::put( array( 'concept' => $linkTarget ), $value, &$txIds );
+		      }
+
+		if( $commit )
+		{
+		      $result = array();
+
+		      for( $i = count( $txIds ) - 1; $i >= 0; $i-- )
+		      {
+			      $currentTx = self::$tx[ $txIds[$i] ];
+			      unset( self::$tx[ $txIds[$i] ] );
+
+			      if( !self::commit( array( 'service' => $txIds[$i] ), $currentTx ) )
+			      {
+				  self::rollback( array( 'service' => $txIds[$i] ), $currentTx );
+
+				  foreach( $currentTx as $i => $st )
+				      $currentTx[$i][ 'rollback' ] = true;
+			      }
+
+			      $result = array_merge( $result, $currentTx );
+		      }
+
+		      self::$txID = 0;
+
+		      return( $result );
+		}
+
+	    }
+	    catch( Exception $e )
+	    {
+		if( !self::fallback( $e ) )
+		    self::closeAll();
+
+		return( false );
+	    }
+	
+	    return( $URI['id'] );
+	}
+	
+	public static function get()
+	{
+	
+	}
 }
 
