source: TI05-delivery/ows_framework/trunk/ows_server/ows_server/controllers/discovery.py @ 2760

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI05-delivery/ows_framework/trunk/ows_server/ows_server/controllers/discovery.py@2760
Revision 2760, 11.4 KB checked in by lawrence, 12 years ago (diff)

Adding help to results page. Adding initial support for semantic search.

Line 
1from ows_server.lib.base import *
2from paste.request import parse_querystring
3from ows_server.lib.Date import *
4from ows_server.templates import DiscoveryTemplate
5from ows_server.models import ndgSearch as NS
6from ows_server.models.DIF import DIF
7from ows_server.templates.renderDiscoverySet import renderDiscoverySet
8from ows_server.models.DiscoveryState import DiscoveryState,constraints
9from ows_server.models.ndgObject import ndgObject
10from ows_server.models.Utilities import myConfig
11from ows_server.lib.mailer import mailHandler
12
13
14class DiscoveryController(BaseController):
15    ''' Provides the pylons controller for NDG discovery '''
16   
17    def __setup(self):
18        ''' Common setup for controller methods '''
19        self.cf=request.environ['ndgConfig']
20        self.inputs=dict(parse_querystring(request.environ))
21        self.message=''
22   
23    def index(self):
24       
25        self.__setup()
26        # parse the query string and hand off to a discovery engine
27        if self.inputs=={} or 'ClearForm' in self.inputs: return self.__advancedPrompt()
28       
29        #the following need to be defined
30        continuations={'start':1,'howmany':10}
31        for i in continuations:
32            if i not in self.inputs: self.inputs[i]=continuations[i]
33           
34           
35        # the simplest query we might get is a text search, in which case
36        # the inputs should be start, howmany and searchString (although
37        # maybe not in that order. The next simplest is one with
38        # a specified textTarget, after that we need all the inputs.
39       
40        if len(self.inputs)==3 and 'searchString' in self.inputs:
41            # it's a simple text search
42            self.inputs['textTarget']='All'
43           
44        # the next simplest is one that includes texttarget as well ...
45        expected=['searchString','textTarget','start','howmany']
46        self.__checkform(expected)
47   
48        if self.message!='':
49            c.xml=self.message
50            return render_response('content')
51       
52        if len(self.inputs)==4:
53           
54            # now we add the defaults ...
55            # this is kind of historical ...
56            bbox=None
57            dateRange=None
58            scope=None
59       
60        else:
61       
62            #check advanced search options
63            expected=['startDateDay','startDateMon','startDateYear',
64                            'endDateDay','endDateMon','endDateYear',
65                            'bboxN','bboxE','bboxS','bboxW','searchString','textTarget'] 
66            self.__checkform(expected)
67            if self.message!='': 
68                c.xml=self.message
69                return render_response('content')
70           
71            # ------------- Handle scope from radio button on form -------
72            if 'source' in self.inputs:
73                # the WSDL expects a list, we're just providing one ... via a radio ...
74                scope=[self.inputs['source']]
75                if scope==['All']: scope=None
76            else:
77                scope=None
78           
79            # ------------- Handle Location Bounding --------
80            # default form has a global bounding box
81            bbox=[self.inputs['bboxN'],self.inputs['bboxS'],self.inputs['bboxW'],self.inputs['bboxE']]
82            self.__checkbox(bbox)
83            if self.message!='': 
84                c.xml=self.message
85                return render_response('content')
86   
87            # ------------- Handle Date Ranges ---------------
88            try:
89                dateRange=[(self.inputs['startDateDay'],self.inputs['startDateMon'],self.inputs['startDateYear']),
90                            (self.inputs['endDateDay'],self.inputs['endDateMon'],self.inputs['endDateYear'])]
91                #default form has blanks, in which case we don't want to check for date range
92                if dateRange<>[("","",""),("","","")]:
93                    self.__checkdates(dateRange)
94                else: dateRange=None           
95            except:
96                self.message='Invalid date provided'
97            if self.message!='': 
98                c.xml=self.message
99                return render_response('content')
100               
101               
102        if 'constrained' in self.inputs: 
103            con=self.__buildconstraints(dateRange,bbox,scope,self.inputs['searchString'])
104            return self.__advancedPrompt(searchConstraints=con)
105        else:
106        # ------------- ok, now go do the search -----------
107           response=self.doText(self.inputs['searchString'],self.inputs['textTarget'],
108                self.inputs['start'],self.inputs['howmany'],scope=scope,dateRange=dateRange,bbox=bbox)
109           return response
110
111    def doText(self,searchString,textTarget,start,howmany,scope=None,dateRange=None,bbox=None):
112       
113        ''' Carry out a text search for <searchString>
114        in the <textTarget> where the accepted text target values are controlled
115        by the DiscoveryTemplate GUI, and are: All, Authors, Parameters '''
116       
117        start,howmany=int(start),int(howmany)  # url arguments need conversion ...
118       
119        ws=NS.ndgSearch(logger=logger)
120        documents=ws.search(searchString,start=start,howmany=howmany,target=textTarget,
121                            scope=scope,dateRange=dateRange,bbox=bbox)
122        if ws.error !=None:
123            m=''
124            for i in ws.error:m+='<p>%s</p>'%i
125            c.xml=m
126            return render_response('content')
127       
128        #build constraints info for report
129        searchConstraints=self.__buildconstraints(dateRange,bbox,scope,searchString)
130       
131        hits=ws.hits
132        id=ws.serverSessionID
133       
134        if hits<howmany:howmany=hits
135        state=DiscoveryState(id,searchString,request.environ,hits,searchConstraints,start,howmany)
136       
137        if hits==0: 
138            c.xml='<p>No records found [%s]</p>>'%state.constraints
139            return render_response('content')
140       
141        try:
142            results=ws.getLabelledDocs(format='DIF')
143            difs=[]
144            errors=[]
145            for result in results: 
146                obj=ndgObject(result[0])
147                obj.setConfig(self.cf)
148                try:
149                    difs.append(DIF(result[1],ndgObj=obj))
150                except ValueError,e:
151                    errors.append((result[0],str(e)))
152            if results==[]:
153                c.xml='<p> No results for "%s"!</p>'%searchString
154                return render_response('content')
155            elif difs==[]:
156                c.xml='<p>No usable results for "%s"!</p>'%searchString
157                return render_response('content')
158            else:
159                if errors<>[]:
160                    c.xml='<p>Search results for "%s"'%searchString
161                    dp=[]
162                    for e in errors:
163                        n=ndgObject(e[0])
164                        if n.repository not in dp: dp.append(n.repository)
165                    if len(dp)<>1: 
166                        dp='[Various Data Providers]'
167                    else:
168                        dp='[%s]'%dp[0] 
169                    c.xml+=' (unfortunately %s hits matched unformattable documents from %s, an internal error has been logged):</p>'%(len(errors),dp)
170                    status,message=mailHandler(['b.n.lawrence@rl.ac.uk'],'DIF errors',str(errors),
171                                    server=self.cf.get('DEFAULT',mailsever))
172                    if not status:
173                        c.xml+='<p> Actually, not even an internal error has been logged. <br/>'
174                        c.xml+='Internal sending of mail failed with error [%s]</p>'%message
175                    return render_response('content')
176                if 0:
177                    c.xml=renderDiscoverySet(difs,state,self.cf)
178                    f=file('debug-dif.xml','w')
179                    f.write(c.xml)
180                    f.close()
181                    return render_response('content')
182                else:
183                    c.state=state
184                    c.difs=difs
185                    c.querystring='searchString=%s&textTarget=%s'%(request.params['searchString'],request.params['textTarget'])
186                    return render_response('results')
187               
188        except ValueError,e:
189            if debug: 
190                raise ValueError,str(e)
191            else:
192                c.xml='<p> Error retrieving documents for %s hits is [%s]</p>'%(hits,e)
193                return render_response('content')
194                               
195        return
196    def __advancedPrompt(self,searchConstraints=None):
197        ''' This provides the advanced search input page '''
198        try:
199            discoveryURL=self.cf.get('SEARCH','discoveryURL')
200            advancedURL=self.cf.get('SEARCH','advancedURL')
201        except:
202            return 'Error, invalid configuration for search interface'
203        #defaults
204        c.bbox='+90.0','-180.0','+180.0','-90.0'
205        c.startDateDay,c.startDateMon,c.startDateYear='','',''
206        c.endDateDay,c.endDateMon,c.endDateYear='','',''
207        c.textTarget='All'
208        c.searchString=''
209        c.source=['All']
210        #constraints
211        if searchConstraints is not None:
212            if searchConstraints['dateRange'] is not None:
213                c.startDateDay,c.startDateMon,c.startDateYear=searchConstraints['dateRange'][0]
214                c.endDateDay,c.endDateMon,c.endDateYear=searchConstraints['dateRange'][1]
215            if searchConstraints['bbox'] is not None:
216                bboxN,bboxW,bboxE,bboxS=searchConstraints['bbox']
217            if searchConstraints['textTarget'] is not None:
218                c.textTarget=searchConstraints['textTarget']
219            if searchConstraints['searchString'] is not None:
220                c.searchString=searchConstraints['searchString']
221            if searchConstraints['scope'] is not None:
222                c.source=searchConstraintss['scope']
223               
224        return render_response('advanced')
225       
226    def __checkbox(self,bbox):
227        if float(bbox[0])>90.0 or float(bbox[1])<-90. or float(bbox[2])<-180. or float(bbox[3])>180.:
228            self.message='Invalid bounding box dimensions entered (limits 90,-90,-180,180)'
229           
230    def __checkform(self,expected):
231        ''' Simply checks the inputs to make sure the elements in expected are present '''
232        message="An incomplete NDG search form was received: "
233        for i in expected:
234            if i not in self.inputs: 
235                self.message=message+i
236        if self.message!='':self.message+='[%s]'%self.inputs
237               
238    def __checkdates(self,dateRange):
239        ''' Check input dates for sanity '''
240        print 'dr',dateRange
241        if not ValidDate(dateRange[0])*ValidDate(dateRange[1]):
242            self.message='Input dates are not valid [%s]'%dateRange
243        elif JulDay(dateRange[0])>=JulDay(dateRange[1]):
244            self.message='Second date must be after first date'
245       
246    def __buildconstraints(self,dateRange,bbox,scope,searchString):
247        ''' Just build a constraint string '''
248        return constraints(dateRange=dateRange,bbox=bbox,scope=scope,searchString=searchString)
249       
250    #def oneLineSearch(self):
251    #    try:
252    #        discoveryURL=self.cf.get('SEARCH','discoveryURL')
253    #        oneLiner=DiscoveryTemplate.oneLiner
254    #    except:
255    #        return 'Error, invalid configuration for search interface'
256    #    return DiscoveryTemplate.searchTextOneLine%locals() 
257   
258    def semantic(self):
259        c.constraints=request.environ['QUERY_STRING']
260        return render_response('semantic',fragment=True)
Note: See TracBrowser for help on using the repository browser.