source: MILK/trunk/milk_server/milk_server/controllers/atom_editor/listatom.py @ 4596

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/MILK/trunk/milk_server/milk_server/controllers/atom_editor/listatom.py@4596
Revision 4596, 8.0 KB checked in by cbyrom, 11 years ago (diff)

Add new web page to display an example of the granulite file contents
and link this to the granulite input page.

Line 
1'''
2 Class representing pylons controller for the listing and searching of atom
3 data
4 
5 @author: C Byrom, Tessella Sep 2008
6'''
7import logging, xmlrpclib
8from paste.request import parse_querystring
9from ndgUtils import ndgObject
10from ndgUtils.models.Atom import Atom
11from ndgUtils import DocumentRetrieve
12from ndgUtils.eXistInterface import ndg_eXist
13import ndgUtils.lib.existdbclient as edc
14from ndgUtils.models.MolesEntity import MolesEntity as ME
15from ndgUtils.vocabtermdata import VocabTermData as VTD, VocabTermItem as VTI
16from milk_server.lib.base import *
17from milk_server.lib.ndgInterface import interface
18import milk_server.lib.htmlUtilities as utils
19from milk_server.lib.atomutilities import savePageAndRender
20from editatom import EditatomController as ec
21
22
23class ListatomController(BaseController):
24    '''
25    Provides the pylons controller for listing/searching NDG Atom documents.
26    '''
27    def __setup(self,uri=None):
28        ''' Common setup stuff for all the actions on this controller '''
29        logging.info("Setting up EditatomController")
30        self.cf=request.environ['ndgConfig']
31       
32        if uri:
33            try:
34                self.ndgObject = ndgObject(uri, config=self.cf)
35            except ValueError,e:
36                return e
37
38        self.inputs=dict(parse_querystring(request.environ))
39
40        logging.info("EditatomController set up")
41        return 0
42
43
44    def atomHome(self):
45        '''
46        Render a simple home page for the atom editor
47        '''
48        logging.info("Rendering atom home page")
49        c.title = "Atom Editor Home Page"
50        return savePageAndRender(self.pathInfo, "atom_editor/atom_home")
51
52
53    def showExampleGranulite(self):
54        '''
55        Show an example granulite file
56        '''
57        logging.info("Rendering example granulite file page")
58        c.title = "Example granulite file"
59        return savePageAndRender(self.pathInfo, "atom_editor/granulite_example")
60   
61   
62    def list(self, searchData=None, associatedAtomID=None, \
63             associatedAtomType=None, associationType=None):
64        '''
65        Provide a simple search interface to the atoms data
66        - display a filter + display any results + if an atomID is
67        provided, allow atoms to be linked to this
68        @keyword associatedAtomID: the ID of an atom to which other atoms
69        should be linked - via deployments
70        @keyword associatedAtomType: type of atom that the data is being
71        associated with
72        @keyword associationType: type of association to be done - NB, use
73        the constants in htmlUtilities for these
74        '''
75        try:
76            inputs = request.params
77            c.searchData = None
78            if searchData and int(searchData) > 0:
79                logging.info("Preparing atom search")
80                c.searchData = searchData
81                self.__setup()
82                atomTypeID = inputs.get('atomTypeID')
83                providerID = inputs.get('providerID')
84                # NB, avoid page being reloaded in incorrect state - e.g. after
85                # browser reload
86                if not (atomTypeID and providerID):
87                    return self.list()
88                   
89                atomTypeID = atomTypeID.split('--')[1]
90                providerID = providerID.split('--')[1]
91               
92                # if we're dealing with activity deployments, substitute the typeID
93                # for the subType Deployments term
94                if atomTypeID == VTD.ACTIVITY_DEPLOYMENT_TERM:
95                    atomTypeID = VTD.DEPLOYMENT_TERM
96                   
97                c.results = self.searchAtoms(providerID, atomTypeID, inputs.get('title'))
98               
99            logging.info("Rendering atom listings page")
100            c.errors = {}
101            c.title='Browse atoms'
102            c.assocAtomType = associatedAtomType
103            c.associateLink = ""
104            if associatedAtomID:
105                c.associateLink = h.url_for(controller='atom_editor/editatom', action='saveAtom', \
106                    uri = associatedAtomID, saveLevel = ec.ADD_ASSOCIATIONS)
107            c.searchLink = h.url_for(controller = 'atom_editor/listatom', action='list', \
108                                     searchData='1', associatedAtomID = associatedAtomID)
109           
110            c.searchTerm = inputs.get('title')
111           
112            # set up the drop down content
113            # NB, if we are here whilst doing an association, restrict the filter
114            # appropriately
115            listVals = []
116            defaultVal = None
117            # set up a default 'all' value
118            allVal = VTI(ndg_eXist.DEFAULT_ALL_VAL, ndg_eXist.DEFAULT_ALL_VAL)
119            if associationType:
120                val = int(associationType)
121                if val == utils.GRANULE_ASSOCIATION:
122                    c.title += ' - to create associations with granule data'
123                    listVals = [g.vtd.TERM_DATA[g.vtd.GRANULE_TERM]]
124                elif val == utils.DEPLOYMENT_ASSOCIATION:
125                    c.title += ' - to create associations with deployment data'
126                    listVals = [g.vtd.TERM_DATA[g.vtd.DEPLOYMENT_TERM]]
127                elif val == utils.ENTITY_ASSOCIATION:
128                    c.title += ' - to create associations with activity/data production tool/observation station data'
129                    listVals = g.vtd.getValidTypes(g.vtd.DEPLOYABLE_ATOM_CATEGORY)
130                    defaultVal = allVal
131            else:
132                listVals = g.vtd.getValidTypes(g.vtd.ATOM_CATEGORY)
133                defaultVal = allVal
134           
135            c.atomTypes = utils.getVocabTermDataDropdown(listVals, \
136                                                   defaultVal = defaultVal, \
137                                                   selected = inputs.get('atomTypeID'))
138            c.providerIDs = utils.getVocabTermDataDropdown(g.vtd.getValidTypes(g.vtd.PROVIDER_CATEGORY), 
139                                                     defaultVal = allVal, \
140                                                     selected = inputs.get('providerID'))
141
142            return savePageAndRender(self.pathInfo, 'atom_editor/atom_list')
143
144        except Exception, e:
145            c.xml='Unexpected error loading page [%s]' %str(e)
146            c.doc=''
147            logging.error(c.xml)
148       
149        response.status_code = 400
150        return render("genshi", 'atom_editor/error')
151   
152   
153    def searchAtoms(self, providerID, atomTypeID, term):
154        '''
155        Search for atoms with specified filter info
156        @param providerID: provider ID of atom or 'all' for all
157        @param atomTypeID: type of atom or 'all' for all
158        @param term: text to search for in atom
159        @return array of dicts with info for each doc found in search
160        '''
161        logging.info("Search for atoms with the following filter: \
162            \nProviderID: '%s'\nAtom Type: '%s'\nText in title: '%s'" \
163            %(providerID, atomTypeID, term))
164       
165        dr = DocumentRetrieve(self.cf.get('NDG_EXIST','local'),
166                              pwfile=self.cf.get('NDG_EXIST','passwordFile'))
167       
168        # NB, for some reason, the xmlrpc call to eXist sometimes results in a spurious
169        # 'java.lang.IndexOutOfBoundsException' - retry a couple of times, if so
170        attempt = 0
171        results = None
172        while results is None:
173            try:
174                results = dr.search(term,
175                            start=1,
176                            howmany=10000, # NB, we want to get back everything!
177                            target=ndg_eXist.ATOM_TARGET,
178                            providerID = providerID, 
179                            atomTypeID = atomTypeID)
180            except xmlrpclib.Fault, e:
181                logging.info("Error caught whilst calling eXist: %s" %e.faultString)
182                attempt += 1
183                if attempt < 3:
184                    logging.info("- retrying eXist call")
185                else:
186                    raise e
187
188        logging.info("Search completed")
189        return results
190
191           
192       
Note: See TracBrowser for help on using the repository browser.