Logo Search packages:      
Sourcecode: zope-archetypes version File versions

SQLStorageConfig.py

##############################################################################
#
# Copyright (c) 2001 Zope Corporation and Contributors. All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE
#
##############################################################################
""" SQL Storage Configuration for Archetypes.

$Id: SQLStorageConfig.py,v 1.4 2003/11/03 18:51:26 dreamcatcher Exp $
"""

from OFS.SimpleItem import SimpleItem

from Globals import InitializeClass, PersistentMapping, DTMLFile
from AccessControl import ClassSecurityInfo
from Acquisition import aq_base, aq_inner, aq_parent

from Products.CMFCore.CMFCorePermissions import ManagePortal
from Products.CMFCore.utils import getToolByName

from config import TOOL_NAME
from interfaces.storage import ISQLStorage
from interfaces.field import IObjectField

00031 class SQLStorageConfig (SimpleItem):

    """ Map Archetypes to SQL Database Connections.
    """

    meta_type = 'SQL Storage Config'

    _conn_by_type = None  # PersistentMapping
    _default_conn = None

    security = ClassSecurityInfo()

    manage_options = (({ 'label' : 'Connections'
                         , 'action' : 'manage_selectConnections'
                         }
                       ),
                      )

    #
    #   ZMI methods
    #


    _manage_selectConnections = DTMLFile('www/selectConnections', globals())

    security.declareProtected( ManagePortal, 'manage_selectConnections')
00057     def manage_selectConnections(self, REQUEST, manage_tabs_message=None):

        """ Show a management screen for changing type to workflow connections.
        """
        cbt = self._conn_by_type
        ti = self.getConfigurableTypes()
        types_info = []
        for t in ti:
            id = t['name']
            title = None
            if cbt is not None and cbt.has_key(id):
                conn = cbt[id]
            else:
                conn = '(Default)'
            types_info.append({'id': id,
                               'title': title,
                               'conn': conn})
        return self._manage_selectConnections(
            REQUEST,
            default_conn=self._default_conn,
            types_info=types_info,
            management_view='Connections',
            manage_tabs_message=manage_tabs_message)

    security.declareProtected( ManagePortal, 'manage_changeConnections')
00082     def manage_changeConnections(self, default_conn, props=None, REQUEST=None):
        """ Changes which connectionss apply to objects of which type.
        """
        if props is None:
            props = REQUEST
        cbt = self._conn_by_type
        if cbt is None:
            self._conn_by_type = cbt = PersistentMapping()
        ti = self.getConfigurableTypes()
        types_info = []
        for t in ti:
            id = t['name']
            field_name = 'conn_%s' % id
            conn = props.get(field_name, '(Default)').strip()
            self.setConnForPortalTypes((id, ), conn)

        # Set up the default conn.
        self.setDefaultConn(default_conn)
        if REQUEST is not None:
            return self.manage_selectConnections(REQUEST,
                            manage_tabs_message='Changed.')

    #
    #   Administration methods
    #
    security.declareProtected( ManagePortal, 'setDefaultConn')
00108     def setDefaultConn(self, default_conn):
        """ Set the default conn for this tool
        """
        default_conn = default_conn.strip()
        if default_conn:
            if not self.getConnectionById(default_conn):
                raise ValueError, (
                    '"%s" is not a valid SQL Connector.' % default_conn)
        self._default_conn = default_conn

    security.declarePrivate('getDefaultConnFor')
00119     def getDefaultConnFor(self, ob):
        """ Return the default conn, if applicable, for ob.
        """
        types_tool = getToolByName( self, 'portal_types', None )
        if ( types_tool is not None
            and types_tool.getTypeInfo( ob ) is not None ):
            return self._default_conn

        return None

    security.declareProtected( ManagePortal, 'getConfigurableTypes')
00130     def getConfigurableTypes(self):
        """ Get a list of types that can be configured for SQL Storage.
        """
        c_types = []
        ti = self.getInstalledTypes()
        for t in ti:
            for field in t['type'].fields():
                if IObjectField.isImplementedBy(field) and \
                   ISQLStorage.isImplementedBy(field.getStorage()):
                    c_types.append(t)
                    break
        return c_types


    security.declareProtected( ManagePortal, 'getInstalledTypes')
    def getInstalledTypes(self):
        pt = getToolByName(self, 'portal_types', None)
        at = getToolByName(self, TOOL_NAME, None)
        if pt is None:
            return ()
        if at is None:
            return ()
        pt = pt.listTypeInfo()
        pt = [t.getId() for t in pt]

        ti = at.listRegisteredTypes()

        installed_types = [t for t in ti if t['name'] in pt]
        return installed_types


    security.declareProtected( ManagePortal, 'setConnForPortalTypes')
00162     def setConnForPortalTypes(self, type_names, conn):
        """ Set a conn for a specific portal type.
        """
        cbt = self._conn_by_type

        if cbt is None:
            self._conn_by_type = cbt = PersistentMapping()

        for id in type_names:
            if conn == '(Default)':
                # Remove from cbt.
                if cbt.has_key(id):
                    del cbt[id]
            else:
                conn = conn.strip()
                if conn:
                    if not self.getConnectionById(conn):
                        raise ValueError, (
                            '"%s" is not a valid SQL Connector.' % conn)
                cbt[id] = conn

    security.declarePrivate('getConnectionById')
00184     def getConnectionById(self, conn_id):
        """ Retrieve a given Connection.
        """
        conn = getattr(self, conn_id, None)
        return conn

    security.declarePrivate('getConnFor')
00191     def getConnFor(self, ob):
        """ Returns the conn that applies to the given object.
            If we get a string as the ob parameter, use it as
            the portal_type.
        """
        cbt = self._conn_by_type
        if type(ob) == type(''):
            pt = ob
        elif hasattr(aq_base(ob), '_getPortalTypeName'):
            pt = ob._getPortalTypeName()
        else:
            pt = None

        if pt is None:
            return None

        conn = None
        if cbt is not None:
            conn = cbt.get(pt, None)
            # Note that if conn is not in cbt or has a value of
            # None, we use a default conn.
        if conn is None:
            conn = self.getDefaultConnFor(ob)
            if conn is None:
                return ''
        return conn

InitializeClass(SQLStorageConfig)

Generated by  Doxygen 1.6.0   Back to index