Index: trunk/prototype/modules/mail/interceptors/Helpers.php
===================================================================
--- trunk/prototype/modules/mail/interceptors/Helpers.php	(revision 5909)
+++ trunk/prototype/modules/mail/interceptors/Helpers.php	(revision 5959)
@@ -69,4 +69,13 @@
 	}
 	
+	//label:before.create
+	public function verifyNameLabel(&$uri , &$params , &$criteria , $original){
+		$nameLabel = $params['name'];
+		$Labels = Controller::find(array('concept' => 'label'), false, array('filter' => array('i=', 'name', $nameLabel) ));
+		if($Labels){
+			Throw new Exception('JÃ¡ existe um marcador com esse nome.');
+		}
+	}
+		
 	public static function in_arrayr($needle, $haystack) {
 		//if(!is_array($haystack)) return false;
@@ -78,5 +87,4 @@
 		return false;
 	} 
-		
 	
 /**
@@ -84,66 +92,87 @@
  */ 
 
- 	//followupflag:before.find
-	public function addDefaultElementsFilter (&$uri , &$params , &$criteria , $original ){
-		//if (!self::in_arrayr('id', $criteria['filter'])) {
-			$defaultsFilter = array( 'IN' , 'id' , array('1','2','3','4','5','6') );
-			
-			if (isset($criteria['filter']) && $criteria['filter']!=NULL && count($criteria['filter']) != 0)
-				$criteria['filter'] = array( 'OR', $criteria['filter'], $defaultsFilter );
-			else
-				$criteria['filter'] =  $defaultsFilter;
-		//}
-	}
-	
-	//followupflagged:after.find
-	public function deepnessFollowupflaggeds( &$uri , &$result , &$criteria , $original ){                
-
-		if(isset($original['criteria']['deepness']))
-		{
-			foreach ($result as $i => $v)
-			{
-				if(isset($v['followupflagId']))
-				{
-					$followupflags = Controller::find(array('concept' => 'followupflag'), false, array('filter' => array('=', 'id', $v['followupflagId']) ));
-					
-					/**
-					 * Devido hÃ¡ inconsistencias na chamada do interceptor addDefaultElementsFilter (followupflag:before.find)
-					 * os followupflag defaults sÃ£o inseridos no retorno, por isso Ã© necessÃ¡rio procurar o objeto correto
-					 * atravÃ©s do id
-					 */
-					foreach ($followupflags as $followupflag) {
-						if ($v['followupflagId'] == $followupflag['id']) {
-							$result[$i]['followupflag'] = $followupflag;
-							break;
-						}
-					}
-				}
-				
-				if(isset($v['messageNumber']) && isset($v['folderName']))
-				{
-					$details = $original['criteria']['deepness'] == '1' ? 'headers' : 'all';
-					
-					$imapService = new imap_functions();
-					$message = $imapService->getMessages(array('messages' => array($v['folderName'] => array($v['messageNumber'])), 'details' => $details));
-					$result[$i]['message'] = $message[$v['folderName']][0];				
-				}     
-			}
-		}
-
+	//Remove as dependencias de uma followupflag 
+	public function clearAllFlaggedOfFollowupflag( &$uri , &$result , &$criteria , $original ){
+		//remove apenas se vier o id da Followupflag
+		if(isset($uri['id']))
+			Controller::deleteALL(array('concept' => 'followupflagged'), null , array('filter' => array('=' , 'followupflagId' , $uri['id'])));
+
+	}
+
+
+	//followupflagged:PostgreSQL.before.delete
+	public function doImapDelete( &$uri , &$params , &$criteria , $original ) {
+	
+		$imap_uri = $uri;
+		$imap_uri['service'] = 'Imap';
+		list($messageId, $uri['id']) = explode('#', $uri['id']);
+
+		$result = Controller::delete($imap_uri, $params, $imap_criteria);
+
+		if (!$result)
+			return false;
+	}
+	
+
+	
+		
+
+	
+	
+	
+	
+	
+	
+	
+	
+	
+	
+	
+	
+	
+	
+	
+	
+	
+	
+	
+	
+	
+	
+	//followupflagged:PostgreSQL.before.create
+	public function doImapCreate( &$uri , &$params , &$criteria , $original ) {	
+		
+		$imap_uri = $uri;
+		$imap_uri['service'] = 'Imap';
+
+		$params = Controller::create($imap_uri, $params);
+		
+		if (!$params)
+			return false;	
+	}
+	
+	//followupflagged:PostgreSQL.after.read
+	public function doImapRead( &$uri , &$result , &$criteria , $original ) {
+
+		if ($reusult) {
+			$imap_result = Controller::read(
+				array('concept' => 'followupflagged', 'service' => 'Imap'), 
+				false, 
+				array('filter' => array('=', 'messageId', $result['messageId']))
+			);
+		}
+
+		if (!$imap_result) {
+			/**
+			 * TODO - deletar do banco se nÃ£o existe no imap 
+			 */
+			 return false;
+		}
+		
+		$result = $result + $imap_result;
+		
 		return $result;
-	}	
-	
-	//followupflagged:PostgreSQL.before.find
-	public function clearFilterAtFind( &$uri , &$params , &$criteria , $original ) {
-	
-		if ($criteria['filter'] && 
-				(self::in_arrayr('messageNumber', $criteria['filter']) || self::in_arrayr('folderName', $criteria['filter'])) ) {
-			unset($criteria['filter']);
-		}
-		
-		/**
-		 * TODO - fazer parse do $criteria['filter'] para fazer strip no id composto
-		 */
-	}
+	}
+	
 	
 	/**
@@ -153,25 +182,9 @@
 	//followupflagged:PostgreSQL.after.find
 	public function doImapFind( &$uri , &$result , &$criteria , $original ){
-		/**
-		 * TODO - ver implicaÃ§Ãµes de busca com id no filtro 
-		 */
-
-		if ($result) {
-			$list = array();
-			foreach ($result as $followupflagged)
-				$list[] = $followupflagged['id'];
-				
-			$filter = array( 'IN' , 'id' , $list );
-			
-			if (isset($original['criteria']['filter']) && $original['criteria']['filter']!=NULL && count($original['criteria']['filter']) != 0)
-				$criteria['filter'] = array( 'AND', $original['criteria']['filter'], $filter );
-			else
-				$criteria['filter'] =  $filter;
-		}
-		
+
 		$imap_uri = $uri;
 		$imap_uri['service'] = 'Imap';
-		$imap_result = Controller::find($imap_uri, $result, $criteria);
-		
+		$imap_result = Controller::find($imap_uri, false, $original['criteria']);
+
 		/**
 		 * Mescla os dados vindos do PostgreSQL com os dados vindos do Imap
@@ -179,13 +192,9 @@
 		$merge_result = array ();
 		foreach ($imap_result as $ir) {
-			list($messageId, $flagId) = explode('#', $ir['id']);
-			
+		
 			foreach($result as $r) {
-				if ($r['id'] == $flagId) {
-					$r['id'] = $ir['id'];
-					$r['messageNumber'] = $ir['messageNumber'];
-					$r['folderName'] = $ir['folderName'];
-					
-					$merge_result[] = $r;
+				if ($r['messageId'] == $ir['messageId']) {			
+					$merge_result[] = $r + $ir;
+					break;
 				}
 			}
@@ -195,119 +204,22 @@
 	}
 
-	//followupflagged:Imap.before.create
-	public function letFollowupflaggedKeepUnique (&$uri , &$params , &$criteria , $original ){
-
-		if (isset($params['folderName']) && isset($params['messageNumber'])) {
-			$filter = array ('AND', array('=', 'folderName', $params['folderName']), array('=', 'messageNumber', $params['messageNumber']));
-			$toClearURI = array('concept' => 'followupflagged', 'service' => 'Imap');
-
-			//ObtÃ©m do banco um possÃ­vel sinalizador existente
-			$imap_result = Controller::find($toClearURI, false, array('filter' => $filter));
-			
-			//Se existe um sinalizador nesta mensagem, deleta-o
-			foreach ($imap_result as $r) {
-				/**
-				 * TODO - deletar sem id
-				 */
+	//followupflagged:PostgreSQL.before.find
+	public function reFilterFind (&$uri , &$params , &$criteria , $original ){
+		/**
+		 * Se p filtor incluir atributos da mensagem que o banco de dados nÃ£o conhece,  
+		 * deve-se obter os messageId dos itens do resultado e passÃ¡los num novo filtro
+		 * que o banco conheÃ§a
+		 */
+		if (self::in_arrayr('messageNumber', $criteria) || self::in_arrayr('folderName', $criteria)) {
+			$result = Controller::find(array('concept' => 'message'), array('messageId'), array('filter' => $criteria['filter']));
+			$idList = array();
+			foreach ($result as $message)
+				$idList[] = $message['messageId'];
 				
-				//deleta no Imap
-				$toClearURI['id'] = $r['id'];
-				Controller::delete($toClearURI, false, false);
-				
-				//deleta no PostgreSQL
-				list($messageId, $toClearURI['id']) = explode('#', $r['id']);
-				$toClearURI['service'] = 'PostgreSQL';
-				Controller::delete($toClearURI, false, false);
-			}
-		}
-
-	}
-
-	//Remove as dependencias de uma followupflag 
-	public function clearAllFlaggedOfFollowupflag( &$uri , &$result , &$criteria , $original ){
-		//remove apenas se vier o id da Followupflag
-		if(isset($uri['id']))
-			Controller::deleteALL(array('concept' => 'followupflagged'), null , array('filter' => array('=' , 'followupflagId' , $uri['id'])));
-
-	}
-	
-
-	//followupflagged:PostgreSQL.after.create
-	public function doImapCreate( &$uri , &$result , &$criteria , $original ) {	
-		/**
-		 * Se nÃ£o consegue salvar no banco, ele retorna objeto vÃ¡lido, porÃ©m sem id
-		 */
-		if (!isset($result['id'])) return false;
-		
-		$imap_uri = $original['URI'];;
-		$imap_uri['service'] = 'Imap';
-
-		$imap_params = array_merge($original['properties'], $result);
-		
-		$result = array();
-		if ($imap_params['folderName'] && $imap_params['messageNumber'] && $imap_params['id']) {
-			$imap_params['id'] = $imap_params['folderName'] . '/' . $imap_params['messageNumber'] . '#' . $imap_params['id']; 
-			$result = Controller::create($imap_uri, $imap_params);
-		}
-
-		if (!$result) {
-			//TODO - rollback
-		}
-	}
-	
-	//followupflagged:PostgreSQL.after.read
-	public function doImapRead( &$uri , &$result , &$criteria , $original ) {
-		
-		$imap_uri = $original['URI'];
-		$imap_uri['service'] = 'Imap';
-
-		$imap_result = Controller::read($imap_uri, $params, $imap_criteria);
-		
-		if (!$imap_result)
-			return false;		
-		
-		$result = array_merge($result, $imap_result);
-
-	}
-	
-	//followupflagged:PostgreSQL.before.<...>
-	public function stripURIAttrs( &$uri , &$params , &$criteria , $original ) {
-		list($messageId, $uri['id']) = explode('#', $uri['id']);
-		$params['id'] = $uri['id'];
-	}
-
-	//followupflagged:PostgreSQL.before.delete
-	public function doImapDelete( &$uri , &$params , &$criteria , $original ) {
-	
-		$imap_uri = $uri;
-		$imap_uri['service'] = 'Imap';
-		list($messageId, $uri['id']) = explode('#', $uri['id']);
-
-		$result = Controller::delete($imap_uri, $params, $imap_criteria);
-
-		if (!$result)
-			return false;
-	}
-	
-	//followupflagged:PostgreSQL.before.update
-	public function doImapUpdate( &$uri , &$params , &$criteria , $original ) {
-		/**
-		 * Para os conceitos "labeled" e "followupflagged", sÃ³ faz sentido o update de 
-		 * atributos gravados no banco de dados e nunca no IMAP.
-		 */
-		//$imap_uri = $uri;
-		//$imap_uri['service'] = 'Imap';
-		//$result = Controller::update($imap_uri, $params, $criteria);
-	}
-	
-		
-	//label:before.create
-	public function verifyNameLabel(&$uri , &$params , &$criteria , $original){
-		$nameLabel = $params['name'];
-		$Labels = Controller::find(array('concept' => 'label'), false, array('filter' => array('i=', 'name', $nameLabel) ));
-		if($Labels){
-			Throw new Exception('JÃ¡ existe um marcador com esse nome.');
-		}
-	}
+			$filter = array( 'IN' , 'messageId' , $idList );
+
+			$criteria['filter'] =  $filter;
+		}	
+	}	
 
 }
