# -*- coding: latin-1 -*-
""" 
   Copyright (C) 2003 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.	
"""

__version__='$Revision: 1.66 $'[11:-2]

import types
from Products.PimenTechLibCommon.object import Object
from Products.PimenTechLibCommon.zlog import *
	

from Globals import HTMLFile	  # fakes a method from a DTML file
from Globals import MessageDialog # provid
from OFS.SimpleItem import *

class CommonMap(Object):

	__super_delitem = None
	
	Log = ZLog() # pour compatibilité ascendante
	
	def update(self, map):
		for (key, value) in map.items():
			self[key] = value
		return self

	def insert_map(self, map): # backward compatibility
		return self.update(map)
	
	def __repr__(self):
		xmlstr = "<%s id='%s' title='%s'>\n" % (self.meta_type, self.getId(), self.title)
		for (key, value) in self.items():
			xmlstr = "%s<pair>\n" % xmlstr
			xmlstr = "%s<key>%s</key>\n" % (xmlstr, `key`)
			xmlstr = "%s<value>%s</value>\n" % (xmlstr, `value`)			
			xmlstr = "%s</pair>\n" % xmlstr
		return "%s</%s>" % (xmlstr, self.meta_type)
	
	def __getitem__(self, key):
		"wrap"
		#self.Log.message("%s (DictMap)%s.__getitem__(%s)" % (self.meta_type, self, key))		
		if not self.has_key(key):
			return None
		return self.data[key]
		
	def __delitem__(self, key):
		if self.has_key(key):
			self.__super_delitem(key)

	def is_string(self, value):
		if type(value) == type(''):
			return 1
		return 0

from UserDict import UserDict

try:
	from PersistentMapping import *
except:
	pass
try:
	from Persistence.mapping import *
except:
	pass


class DictMap(CommonMap, UserDict):
	"the map class (based on dict)"

	meta_type = 'DictMap'

	__super_delitem = UserDict.__delitem__
	__super_update = CommonMap.update

	def __init__(self, id, title=''):
		CommonMap.__init__(self, id , title)
		UserDict.__init__(self)

	def __delitem__(self, key):
		if self.has_key(key):
			self.__super_delitem(key)
			
	def update(self, map):
		try:
			self.data.update(map.data)
		except:
			try:
				self.data.update(map)
			except:
				self.__super_update(map)
		return self
		
class PDictMap(CommonMap, SimpleItem, PersistentMapping):
	"the map class (based on dict)"

	meta_type = 'PDictMap'

	manage_options = (
		{'label':'View', 'action':'index_html'},
		{'label':'Edit', 'action':'manage_main'}
		) + SimpleItem.manage_options 
	
	index_html = HTMLFile('dtml/dictMapIndex', globals()) # View Interface
	manage_main = HTMLFile('dtml/dictMapEdit', globals()) # Edit Interface

	__super_delitem = PersistentMapping.__delitem__
	
	def __init__(self, id, title=''):
		PersistentMapping.__init__(self)
		self.id = id
		self.title = title

	def __delitem__(self, key):
		if self.has_key(key):
			self.__super_delitem(key)

	_dtml_types = {
		types.StringType : ':string',
		types.FloatType : ':float',
		types.IntType : ':int',
		}

	def dtml_type(self, value):
		"for html forms"
		return self._dtml_types.get(type(value), None)
	def set_from_form(self, REQUEST):
		"set_from_form."
		for key,value in REQUEST.form.items():
			if self.has_key(key):
				self[key] = value
		REQUEST.RESPONSE.redirect('manage_main')
		

from OFS.Folder import Folder

class MyFolder(CommonMap, Folder):

	def __setitem__(self, key, value):
		return self._setObject(str(key), value)
	def __getitem__(self, key):
		return self._getOb(str(key),None)
	def __delitem__(self, object):
		self._delObject(str(object))
	def has_key(self,key):
		if self[key] is None or str(self[key]) == '':
			return None
		return 1
	def values(self):
		return self.objectValues()
	def keys(self):
		"funky with it ?"
		return self.objectIds()
	def items(self):
		return self.objectItems()
	
class Map(MyFolder):
	"the map class (based on dict or Folder)"

	meta_type = 'Map'

