# -*- coding: utf-8 -*-

""" 
   Copyright (C) 2006 PimenTech SARL (http://www.pimentech.net)

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with this library; see the file COPYING.LIB.  If not,
   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.  
"""

from Products.PimenTechLibCommon.object import Object

class PGEntityMap(Object):
	"""
	Subclassed in pgroot
	"""

	def __init__(self, id, title=''):
		Object.__init__(self, id, title)
		self.reset_entities()
		
	def reset_entities(self, table=None):
		" delete entities "
		self.notice("Reset Entity map for table %s" % table)
		if table == None:
			self.dict_sort = {}
			self.dict_uid = {}
			self.dict_ztitle = {}
			self.dict_zid = {}
		else:
			self.dict_sort[table] = []
			self.dict_uid[table] = {}
			self.dict_ztitle[table] = {}
			self.dict_zid[table] = {}

	def __table_load(self, table):
		"Charge une table entite dans les dico dict_uid et dic_zid_uid"
		self.notice("Load Entity map for table %s" % table)
		self.reset_entities(table)
		query = "select uid, zid::text, ztitle::text from %s where uid!=0 and ref_statut=0 ORDER BY ztitle" % table
		if table == "statut":
			query = "select uid, zid::text, ztitle::text from %s ORDER BY ztitle" % table
		for (uid, zid, ztitle) in self.fetch(query):
			self.dict_sort[table].append(uid)
			self.dict_uid[table][uid] = zid
			self.dict_ztitle[table][uid] = ztitle
			self.dict_zid[table][zid] = uid
			
		if table != 'statut':
			self.dict_zid[table]['indefini'] = 0
			self.dict_ztitle[table][0] = 'Indéfini'
			self.dict_uid[table][0] = 'indefini'
		self._p_changed = 1

	def uid_entity(self, table, zid):
		"Renvoie l'uid.."
		if not self.dict_zid.has_key(table):
			self.__table_load(table)
		return self.dict_zid[table][zid]

	def ztitle_entity(self, table, uid):
		"Renvoie le ztitle"
		if uid is None:
			return ''
		uid = int(uid)
		if not self.dict_uid.has_key(table) or not self.dict_ztitle[table].has_key(uid):
			self.__table_load(table)
		ret = self.dict_ztitle[table].get(uid, 'Erreur')
		if ret == 'Erreur':
			self.error("[ztitle_entity] %s unfound in table %s" % (uid, table))
		return ret

	def zid_entity(self, table, uid):
		"Renvoie le zid"
		if uid is None:
			return ''
		uid = int(uid)
		if not self.dict_uid.has_key(table) or not self.dict_ztitle[table].has_key(uid):
			self.__table_load(table)
		ret = self.dict_uid[table].get(uid, 'Erreur')
		if ret == 'Erreur':
			self.error("[zid_entity] %s not found in table %s" % (uid, table))
		return ret

	def get_ztitle_entities(self, table):
		" renvoie le hash "
		if not self.dict_ztitle.has_key(table):
			self.__table_load(table)
		return self.dict_ztitle[table].items()

	def ztitle_entities(self, table):
		" renvoie le hash "
		if not self.dict_ztitle.has_key(table):
			self.__table_load(table)
		return self.dict_ztitle[table].items()[1:]

	def ztitle_entities_2(self, table, sort='ztitle'):
		" renvoie le hash trie "
		if not self.dict_ztitle.has_key(table):
			self.__table_load(table)

		keys = self.dict_sort[table]
		if sort == 'uid':
			keys.sort()

		ret = []
		for k in keys:
			ret.append((k, self.dict_ztitle[table][k]))
		return ret



