| 1 | # (C) Copyright 2004 Nuxeo SARL <http://nuxeo.com> |
|---|
| 2 | # Author: Georges Racinet <gracinet@nuxeo.com> |
|---|
| 3 | # Patched by Joachim Schmitz <js@aixtraware.de> |
|---|
| 4 | # This program is free software; you can redistribute it and/or modify |
|---|
| 5 | # it under the terms of the GNU General Public License version 2 as published |
|---|
| 6 | # by the Free Software Foundation. |
|---|
| 7 | # |
|---|
| 8 | # This program is distributed in the hope that it will be useful, |
|---|
| 9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 11 | # GNU General Public License for more details. |
|---|
| 12 | # |
|---|
| 13 | # You should have received a copy of the GNU General Public License |
|---|
| 14 | # along with this program; if not, write to the Free Software |
|---|
| 15 | # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
|---|
| 16 | # 02111-1307, USA. |
|---|
| 17 | # |
|---|
| 18 | # $Id: utils.py 35055 2006-04-10 15:51:21Z gracinet $ |
|---|
| 19 | |
|---|
| 20 | from zLOG import LOG, DEBUG, TRACE |
|---|
| 21 | from copy import deepcopy |
|---|
| 22 | from AccessControl import ClassSecurityInfo |
|---|
| 23 | from Products.CPSSchemas.StorageAdapter import AttributeStorageAdapter |
|---|
| 24 | from Products.CPSDirectory.utils import QueryMatcher |
|---|
| 25 | security = ClassSecurityInfo() |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | security.declarePrivate('_searchEntries') |
|---|
| 29 | def _searchEntries(self, return_fields=None, **kw): |
|---|
| 30 | """Search for entries in the directory. |
|---|
| 31 | """ |
|---|
| 32 | if self.ZCacheable_isCachingEnabled(): |
|---|
| 33 | keyset = {'return_fields' : return_fields} |
|---|
| 34 | keyset.update(kw) |
|---|
| 35 | LOG('ZODBDirectory._searchEntries', TRACE, |
|---|
| 36 | "Searching cache for %s" % (keyset,)) |
|---|
| 37 | from_cache = self.ZCacheable_get(keywords=keyset) |
|---|
| 38 | if from_cache is not None: |
|---|
| 39 | LOG('ZODBDirectory._searchEntries', TRACE, " -> results=%s" % |
|---|
| 40 | (from_cache[:20],)) |
|---|
| 41 | return deepcopy(from_cache) |
|---|
| 42 | else: |
|---|
| 43 | keyset = None |
|---|
| 44 | |
|---|
| 45 | matcher = QueryMatcher(kw, accepted_keys=self._getFieldIds(), |
|---|
| 46 | substring_keys=self.search_substring_fields) |
|---|
| 47 | |
|---|
| 48 | # Compute needed fields from object. |
|---|
| 49 | # All fields we need to return. |
|---|
| 50 | field_ids_d, return_fields = self._getSearchFields(return_fields) |
|---|
| 51 | # Add all fields the search is made on. |
|---|
| 52 | field_ids = matcher.getKeysSet().union(field_ids_d) |
|---|
| 53 | |
|---|
| 54 | # Do the search. |
|---|
| 55 | schema = self._getUniqueSchema() |
|---|
| 56 | adapter = AttributeStorageAdapter(schema, None, |
|---|
| 57 | field_ids=list(field_ids)) |
|---|
| 58 | res = [] |
|---|
| 59 | for id, ob in self.objectItems(): |
|---|
| 60 | if getattr(ob,'homeless',False): |
|---|
| 61 | continue |
|---|
| 62 | adapter.setContextObject(ob) |
|---|
| 63 | entry = adapter.getData() |
|---|
| 64 | adapter.finalizeDefaults(entry) |
|---|
| 65 | if not matcher.match(entry): |
|---|
| 66 | continue |
|---|
| 67 | # Compute result to return. |
|---|
| 68 | if return_fields is None: |
|---|
| 69 | res.append(id) |
|---|
| 70 | else: |
|---|
| 71 | d = {} |
|---|
| 72 | for key in return_fields: |
|---|
| 73 | d[key] = entry[key] |
|---|
| 74 | res.append((id, d)) |
|---|
| 75 | |
|---|
| 76 | if keyset is not None: |
|---|
| 77 | LOG('ZODBDirectory._searchEntries', TRACE, "Putting in cache") |
|---|
| 78 | self.ZCacheable_set(res, keywords=keyset) |
|---|
| 79 | |
|---|
| 80 | return deepcopy(res) |
|---|
| 81 | from Products.CPSDirectory.ZODBDirectory import ZODBDirectory |
|---|
| 82 | ZODBDirectory._searchEntries = _searchEntries |
|---|