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

from Products.PimenTechLibCommon.object import Object
from Products.PimenTechLibCommon import sqlcommon
from Products.PimenTechLibCommon.common import autostrftime

import DateTime

class SQLFactory(Object, SimpleItem):
	" The MaMa Object for SQL !"
	
	meta_type = 'SQLFactory'
	tablename = None
	access = None
	autostrftime = autostrftime
	ref_object = 0
	
	def _query_change(self, dict_values, REQUEST):
		query = "Set DateStyle To European;\n"
		dict_values['datemodif'] = DateTime.DateTime().strftime('%d/%m/%Y %H:%M:%S')
		dict_values['codemodif'] = self._get_code_log()
		dict_values['authmodif'] = self.get_ref_author(REQUEST)
		query += sqlcommon.sql_update(self.tablename, dict_values) + "\n"
		return query

	def _query_insert(self, dict_values, REQUEST):
		query = "Set DateStyle To European;\n"
		dict_values['datecrea'] = DateTime.DateTime().strftime('%d/%m/%Y %H:%M:%S')
		dict_values['codecrea'] = self._get_code_log()
		dict_values['authcrea'] = self.get_ref_author(REQUEST)
		query += sqlcommon.sql_insert(self.tablename, dict_values) + "\n"
		return query

	def _query_delete(self, uid, REQUEST):
		dict_values = {'uid' : uid}
		dict_values['ref_statut'] = 1
		return self._query_change(dict_values, REQUEST)

	def _copy(self, copy_dict, history_ref, REQUEST):
		" duplication in postgresql "
		query = self._query_change({'uid':copy_dict['uid'], 'ref_object':history_ref}, REQUEST)
		for key in ('uid', 'datecrea', 'datemodif', 'authcrea', 'authmodif', 'codecrea', 'codemodif'):
			try:
				copy_dict.pop(key)
			except KeyError:
				continue
		copy_dict['frozen'] = 1
		copy_dict['uid'] = history_ref
		self.ref_object = history_ref
		query += self._query_insert(copy_dict, REQUEST)
		return query
			
	def _check_security(self, REQUEST):
		aq_chain = list(REQUEST.PARENTS)
		for obj in aq_chain:
			if obj.meta_type in self.access:
				break
		if obj.meta_type not in self.access:
			raise "%s(%s) is not allowed for %s" % \
				  (REQUEST.PARENTS[0].meta_type,
				   REQUEST.PARENTS[0].getId(),
				   REQUEST.AUTHENTICATED_USER.getUserName())
		if not REQUEST.AUTHENTICATED_USER.has_permission('View', obj):
			raise "user %s is not allowed to access cibles for affectation %s" % \
				  (REQUEST.AUTHENTICATED_USER.getUserName(), obj.getId())
		return obj


