|
Key
This line was removed.
This word was removed. This word was added.
This line was added.
|
Comment:
Changes (2)
View Page Historyh1. Part 9 : Data Services Querying
----
{cagridroundpanel}
{pre:class=cagridheaderfont}Table of Contents{pre}
{toc:outline=false|exclude=Part 9 : Data Services Querying|style=bullets}
{cagridroundpanel}
h3. Step 1: Construct the Query Data Services template
* In *mytemplates/thewebapp* make the new template querydataservice.html
* Add the following:
h6. Greetings/Error
* Say hello to the user, show any error message that's given, and the page header
{code:html}
{% if the_username %}
Hello <b>{{ the_username }}</b>, you are logged in. Do you want to <a href="/logout/">Logout?</a><br>
{% else %}
Hello, do you want to <a href="/login/">Login</a> to save queries and query secure data services?<br>
{% endif %}
{% if error_message %}<h3><strong>Error {{ error_message }}</strong></h3>{% endif %}
<h1>Query a Data Service</h1>
{code}
h6. Query Details
* Show the query details, if given
{code:html}
{%if theurl%}URL:<dd><b>{{theurl}}</b></dd><br>{%endif%}
{%if oname%}Object:<dd><b>{{oname}}</b></dd><br>{%endif%}
{%if fname%}Input File:<dd><b>{{fname}}</b></dd><br>{%endif%}
{%if type%}Type of Query:<dd><b>{{type}}</b></dd><br>{%endif%}
{% if dname %}Attribute selected:<dd><b>{{dname}}</b></dd><br>{%endif%}
{% if sname %}
Attributes selected:
{% for asname in sname %}
<dd><b>{{asname}}</b></dd><br>
{%endfor%}
<br>
{%endif%}
{%if roleName %}
Role Name: <dd><b>{{roleName}}</b></dd><br>
Class Name: <dd><b>{{className}}</b></dd><br>
Attribute Selected: <dd><b>{{assoc_attr}}</b></dd><br>
{%endif%}
{%if predicate %}
Predicate Selected: <dd><b>{{predicate}}</b></dd><br>
Value Given: <dd><b>{{attr_value}}</b></dd><br>
{%endif%}
{code}
h6. Queries Used/Constructed
* If the query builder was not used, show the inputted query and the opposite CQL query
If the query builder was used, show the CQL1 and CQL2 queries constructed
{code:html}
{% if isfile %}
Inputed {%if cql2%}CQL2{%else%}CQL1{%endif%} Query:<dd><textarea rows="10" cols=75>{%if cql2%}{{secondsw}}{%else%}{{firstsw}}{%endif%}</textarea></dd><br>
Corresponding {%if cql2%}CQL1{%else%}CQL2{%endif%} Query:<dd><textarea rows="10" cols=75>{%if cql2%}{{firstsw}}{%else%}{{secondsw}}{%endif%}</textarea></dd><br>
{%else%}
{% if firstsw %}
CQL1 Query Constructed:<dd><textarea rows="10" cols="75">{{firstsw}}</textarea></dd><br>
CQL2 Query Constructed:<dd><textarea rows="10" cols="75">{{secondsw}}</textarea></dd><br>
{%endif%}
{%endif%}
{code}
h6. Get the Data Service URL
* Ask for the Data Service URL, the user can either give a local data service url, or select one of the available Grid Data Services
{code:html}
<h3>Give the Data Service URL</h3>
<table border="1">
<th>Local Data Service URL</th>
<th>Grid Data Service URL</th>
<tr>
<td>
<form action="/geturls/">
<input type="text" name="localurl" />
<input type="submit" value="Submit" />
</form>
</td>
<td>
<form action="/geturls/">
<select name="ds">
{% for ads in ds %}
<option value={{ads}}>{{forloop.counter}} : {{ads}}</option>
{% endfor %}
</select>
<input type="submit" value="Submit" />
</form>
</td>
</tr>
</table>
{code}
h6. Show Objects and Upload File/Query
* Show all the objects available, and give a place to upload CQL query files/text
{code:html}
{% if empty %}<p><strong>{{empty}}</strong></p>{% endif %}
{% if objects %}
<h3>Query the data service with objects or CQL query</h3>
<table border="1">
<th>Choose an object</th>
<th>Submit a CQL1/CQL2 query</th>
<tr>
<td>
<form action="/getobject/">
<select name="object">
{% for aobject in objects %}
<option value={{forloop.counter}}>{{forloop.counter}}: {{aobject.getPackageName}}.{{aobject.getClassName}}</option>
{% endfor %}
<input type="submit" value="Submit" />
</select>
</form>
</td>
<td>
<form action="/getquery/">
{% csrf_token %}
<textarea name="querytext" rows="10" cols=75>{%if text%}{{text}}{%endif%}</textarea>
<input type="submit" value="Submit" />
</form>
<form method="post" enctype="multipart/form-data" action="/getfile/">
{% csrf_token %}
Upload a CQL1/CQL2 query file instead: <input type=file name="filename">
<input type="submit" value="View File" />
</form>
</td>
</tr>
</table>
{% endif%}
{code}
h6. Show Query Modifiers
* If an object was selected, show the available query modifiers
{code:html}
{% if oname %}
<h3>Select the query modifier</h3>
<form action="/getmodifier/">
<input type="radio" name="modifier" value="object">object<br>
<input type="radio" name="modifier" value="count">count<br>
<input type="radio" name="modifier" value="dattr">distinct attributes<br>
<input type="radio" name="modifier" value="sattr">selected attributes<br>
<input type="radio" name="modifier" value="assoc">associations<br>
<input type="submit" value="Submit" /><br>
</form>
{% endif %}
{code}
h6. Show Attributes
* If a distinct or selected attribute was selected, show the available attributes
{code:html}
<form action="/getdattr/">
{% if dumlattr %}
<h3>Select the attribute</h3>
{% for auml in dumlattr %}
<input type="radio" name="dattribute" value={{auml.getName}}>{{auml.getName}}<br>
{% endfor %}
<input type="submit" value="Submit" /><br>
{% endif %}
</form>
<form action="/getsattr/">
{% if sumlattr %}
<h3>Select the attributes</h3>
{% for auml in sumlattr %}
<input type="checkbox" name="sattribute" value={{auml.getName}}>{{auml.getName}}<br>
{% endfor %}
<input type="submit" value="Submit" /><br>
{% endif %}
</form>
{code}
h6. Show Associations
* If an association query was selected, show the available associations and their attributes
{code:html}
<form action="/getassoc/">
{% if d %}
<h3>Select the association</h3>
{% for key,value in d.items%}
<b>{{key}}</b><br>
{% for key2,value in value.items%}
<dd><b>{{key2}}</b><br></dd>
{% for attr in value%}
<dd><input type="radio" name="assoc" value={{key}}/{{key2}}/{{attr}}>{{attr}}<br></dd>
{%endfor%}
{%endfor%}
{% endfor %}
<input type="submit" value="Submit" /><br>
{% endif %}
</form>
{code}
* If an association attribute was selected, ask for a predicate and a value
{code:html}
{% if assoc_attr%}
<h3>Select the predicate and values</h3>
<b>{{assoc_attr}}</b> is <form action="/getassocattr/"> <select name="predicate">
<option value="EQUAL_TO">EQUAL_TO</option>
<option value="GREATER_THAN">GREATER_THAN</option>
<option value="GREATER_THAN_EQUAL_TO">GREATER_THAN_EQUAL_TO</option>
<option value="IS_NOT_NULL">IS_NOT_NULL</option>
<option value="IS_NULL">IS_NULL</option>
<option value="LESS_THAN">LESS_THAN</option>
<option value="LESS_THAN_EQUAL_TO">LESS_THAN_EQUAL_TO</option>
<option value="LIKE">LIKE</option>
<option value="NOT_EQUAL_TO">NOT_EQUAL_TO</option>
</select>
<input type="text" name="attr_value" />
<input type="submit" value="Submit" />
</form>
{%endif%}
{code}
h6. Show Results
{code:html}
{% if stringw %}
<h3>Results</h3>
<textarea rows="50" cols="150">{{stringw}}</textarea><br>
{%endif%}
<a href="/index/">Back to the Index</a><br>
{code}
* Save the file
h3. Step 2: Add the Query Data Services methods
Open *GridClient/views.py* and add the following
h6. Imports
* Add imports, don't worry if eclipse complains about the imports
{code}
from org.apache.axis.message.addressing import EndpointReferenceType
from org.apache.axis.types import URI
from java.rmi import RemoteException
from java.io import StringWriter, StringReader
from org.cagrid.cql.utilities import CQL2SerializationUtil,CQL2toCQL1Converter,CQL1toCQL2Converter
from gov.nih.nci.cagrid.data import DataServiceConstants
from gov.nih.nci.cagrid.cqlquery import CQLQuery as CQL1Query
from gov.nih.nci.cagrid.data.utilities import DataServiceFeatureDiscoveryUtil
from gov.nih.nci.cagrid.data.faults import QueryProcessingExceptionType
from django.views.decorators.csrf import csrf_protect
from gov.nih.nci.cagrid.data.client import DataServiceClient
from gov.nih.nci.cagrid.cqlquery import Object, QueryModifier, CQLQuery as CQL1Query, Association,Attribute
from gov.nih.nci.cagrid.cqlquery.Predicate import EQUAL_TO as et,GREATER_THAN as gt,GREATER_THAN_EQUAL_TO as gtet,IS_NOT_NULL as inn,IS_NULL as iN,LESS_THAN as lt,LESS_THAN_EQUAL_TO as ltet,LIKE as l,NOT_EQUAL_TO as net
from gov.nih.nci.cagrid.cqlresultset import CQLQueryResults
from org.cagrid.cql2 import CQLQuery,CQLTargetObject, CQLQueryModifier, NamedAttribute, Aggregation, DistinctAttribute,CQLAssociatedObject,CQLAttribute, AttributeValue
from jarray import array
from java.lang import Boolean,Integer,Long,Double
from java.lang import Boolean,Integer,Long,Double
from java.util import Date
from org.cagrid.cql2.UnaryPredicate import IS_NOT_NULL, IS_NULL
from java.text import DateFormat
from java.text import DateFormat
from gov.nih.nci.cagrid.cqlquery.Predicate import EQUAL_TO as et,GREATER_THAN as gt,GREATER_THAN_EQUAL_TO as gtet,IS_NOT_NULL as inn,IS_NULL as iN,LESS_THAN as lt,LESS_THAN_EQUAL_TO as ltet,LIKE as l,NOT_EQUAL_TO as net
{code}
h6. Show Error
* Error method to show any exception to the screen
{code}
def error(e, request):
return render_to_response('thewebapp/querydataservice.html',
{'the_username':logged_in(request),
'error_message':e,
'ds':String(request.session['dslist']).split("::"),},
context_instance=RequestContext(request))
{code}
h6. Show Available Data Services
* When the dataservices page is first accessed, get all the grid data services and show them on the page.
{code}
def dataservices(request):
alldata= discClient.getAllDataServices()
alldatalist=""
for ad in alldata: alldatalist+=("%s::"%(ad.getAddress()))
request.session['dslist']=alldatalist
return render_to_response('thewebapp/querydataservice.html',
{'the_username':logged_in(request),
'ds': String(alldatalist).split("::"),},
context_instance=RequestContext(request))
{code}
h6. Return Data Service Objects
* When the user submits a url, make a service endpoint from it, and return the uml (objects)
{code}
def geturls(request):
if "localurl" in request.GET:
#get localurl and show available objects
try: serviceEndpoint= EndpointReferenceType(URI(request.GET['localurl']))
except MalformedURIException, e: return error(e, request)
request.session['url']=request.GET['localurl']
elif "ds" in request.GET:
#get dataservice and show available objects
request.session['url']=request.GET['ds']
serviceEndpoint=EndpointReferenceType(URI(request.GET['ds']))
try:
if (String(serviceEndpoint.getAddress().getScheme()).compareTo("http") != 0):
name=logged_in(request)
if(name==None):
return render_to_response('thewebapp/querydataservice.html',
{'ds': String(request.session['dslist']).split("::"),
'error_message': "Login if you want to query secure services",},
context_instance=RequestContext(request))
domainModel = MetadataUtils.getDomainModel(serviceEndpoint)
uml = domainModel.getExposedUMLClassCollection().getUMLClass()
if(len(uml)>0):
return render_to_response('thewebapp/querydataservice.html',
{'the_username':logged_in(request),
'ds': String(request.session['dslist']).split("::"),
'theurl':request.session['url'],
'objects': uml,},
context_instance=RequestContext(request))
return render_to_response('thewebapp/querydataservice.html',
{'ds': String(request.session['dslist']).split("::"),
'theurl':request.session['url'],
'empty': "There are no objects in this data service",},
context_instance=RequestContext(request))
except (MalformedURIException,
RemoteException,
RemoteResourcePropertyRetrievalException,
Exception), e:
del request.session['url']
return error(e,request)
{code}
* given a url session object, return the uml (objects)
{code}
def getuml(request):
serviceEndpoint=EndpointReferenceType(URI(request.session['url']))
domainModel = MetadataUtils.getDomainModel(serviceEndpoint)
return domainModel.getExposedUMLClassCollection().getUMLClass()
{code}
h6. Process and Execute File/CQLQuery input
* If CQLQuery file was given, get the file contents and show it on the web page
{code}
@csrf_protect
def getfile(request):
#get filename and return results
filename=request.FILES['filename']
request.session['thefilename']= filename.name
sw=StringWriter()
for chunk in filename.chunks():sw.write(String(chunk))
return render_to_response('thewebapp/querydataservice.html',
{'the_username':logged_in(request),
'ds': String(request.session['dslist']).split("::"),
'theurl':request.session["url"],
'objects': getuml(request),
'fname':filename.name,
'text':sw.toString(),},
context_instance=RequestContext(request))
{code}
* Retrieve the CQLquery echoed/submitted on the web page, store it and process it
{code}
@csrf_protect
def getquery(request):
thefilename=''
if "thefilename" in request.session:
thefilename=request.session["thefilename"]
del request.session["thefilename"]
akind="Input File"
else:
akind="Hand-typed query"
test=String(request.GET["querytext"])
if(test.contains("http://CQL.caBIG/2/org.cagrid.cql2")):
iscql2=True
query = CQL2SerializationUtil.deserializeCql2Query(StringReader(test))
if "user_id" in request.session:
u=user.objects.get(pk=int(request.session["user_id"]))
try:u.queries_set.create(url=request.session["url"],query=test,cql2=True,filename=thefilename,thekind=akind)
except:pass
elif(test.contains("http://CQL.caBIG/1/gov.nih.nci.cagrid.CQLQuery")):
iscql2=False
query = Utils.deserializeObject(StringReader(test), CQL1Query)
if "user_id" in request.session:
u=user.objects.get(pk=int(request.session["user_id"]))
try:u.queries_set.create(url=request.session["url"],query=test,cql2=False,filename=thefilename,thekind=akind)
except:pass
d={'the_username':logged_in(request),
'ds': String(request.session['dslist']).split("::"),
'theurl':request.session["url"],
'objects': getuml(request),
'fname':thefilename,
'type':akind,
'cql2':iscql2,
'isfile':True}
return getresults(request,query,d)
{code}
h6. Object Selection
* When the user selects an object, store the object index and it's name, and show the available query modifiers.
{code}
def getobject(request):
#get object and ask about query modifiers
objectNum = int(request.GET['object'])-1
uml=getuml(request)
theuml=uml[objectNum]
request.session["obnum"]=objectNum
objectName=theuml.getPackageName()+"."+theuml.getClassName()
request.session["obname"]=objectName
return render_to_response('thewebapp/querydataservice.html',
{'the_username':logged_in(request),
'ds': String(request.session['dslist']).split("::"),
'theurl':request.session["url"],
'objects': uml,
'oname':objectName,},
context_instance=RequestContext(request))
{code}
h6. Process Query Modifier
* Retrieve the query modifier and go to the appropriate methods for that query. If it was a distinct/selected attribute query, show the attributes on the page
{code}
def getmodifier(request):
serviceEndpoint=EndpointReferenceType(URI(request.session['url']))
domainModel = MetadataUtils.getDomainModel(serviceEndpoint)
uml= domainModel.getExposedUMLClassCollection().getUMLClass()
theuml=uml[int(request.session["obnum"])]
umlattr=theuml.getUmlAttributeCollection().getUMLAttribute()
mod=request.GET['modifier']
if(mod=="object" or mod=="count"):return objectcount(request,mod)
elif(mod=="dattr"):
return render_to_response('thewebapp/querydataservice.html',
{'the_username':logged_in(request),
'ds': String(request.session['dslist']).split("::"),
'theurl':request.session["url"],
'objects': uml,
'type':"Distinct Attribute",
'oname':request.session["obname"],
'dumlattr':umlattr,},
context_instance=RequestContext(request))
elif(mod=="sattr"):
return render_to_response('thewebapp/querydataservice.html',
{'the_username':logged_in(request),
'ds': String(request.session['dslist']).split("::"),
'theurl':request.session["url"],
'objects': uml,
'type':"Selected Attribute",
'oname':request.session["obname"],
'sumlattr':umlattr,},
context_instance=RequestContext(request))
else:return assocquery(request,domainModel,uml,theuml)
{code}
h6. Process Object and Count Queries
* If the user selected a/n object/count query, construct the query, store the query, and process the query
{code}
def objectcount(request,modi):
query=constructquery(request,modi,None,None,None,None,None,None,None)
if(type(query) is CQL1Query):
if "user_id" in request.session:
u=user.objects.get(pk=int(request.session["user_id"]))
try:u.queries_set.create(url=request.session["url"],query=getCQL1query(query),cql2=False,object=request.session["obname"],thekind=modi)
except:pass
else:
if "user_id" in request.session:
u=user.objects.get(pk=int(request.session["user_id"]))
try:u.queries_set.create(url=request.session["url"],query=getCQL2query(query),cql2=True,object=request.session["obname"],thekind=modi)
except:pass
d={'the_username':logged_in(request),
'ds': String(request.session['dslist']).split("::"),
'theurl':request.session["url"],
'objects': getuml(request),
'oname':request.session["obname"],
'type':modi,}
return getresults(request,query,d)
{code}
h6. Process Distinct Attribute Queries
* If the user selected a distinct attribute to query with, construct the distinct attribute query, store the query and process the query
{code}
def getdattr(request):
uml= getuml(request)
theuml=uml[int(request.session["obnum"])]
umlattr=theuml.getUmlAttributeCollection().getUMLAttribute()
dattr=String(request.GET["dattribute"])
query=constructquery(request,"dattr",dattr,None,None,None,None,None,None)
if(type(query) is CQL1Query):
if "user_id" in request.session:
u=user.objects.get(pk=int(request.session["user_id"]))
try:u.queries_set.create(url=request.session["url"],query=getCQL1query(query),cql2=False,object=request.session["obname"],dattr=dattr,thekind="distinct attribute")
except:pass
else:
if "user_id" in request.session:
u=user.objects.get(pk=int(request.session["user_id"]))
try:u.queries_set.create(url=request.session["url"],query=getCQL2query(query),cql2=True,object=request.session["obname"],dattr=dattr,thekind="distinct attribute")
except:pass
d={'the_username':logged_in(request),
'ds': String(request.session['dslist']).split("::"),
'theurl':request.session["url"],
'objects': uml,
'oname':request.session["obname"],
'type':"distinct attribute",
'dumlattr':umlattr,
'dname':dattr,}
return getresults(request,query,d)
{code}
h6. Process Selected Attribute Queries
* When the user selects the attribute/s for a selected attribute query, construct the query, store the query and process the query
{code}
def getsattr(request):
uml= getuml(request)
theuml=uml[int(request.session["obnum"])]
umlattr=theuml.getUmlAttributeCollection().getUMLAttribute()
sattr=request.GET.getlist("sattribute")
sattr2=''
for attr in sattr: sattr2 += ("%s, " % attr)
query=constructquery(request,"sattr",None,sattr,None,None,None,None,None)
if(type(query) is CQL1Query):
if "user_id" in request.session:
u=user.objects.get(pk=int(request.session["user_id"]))
try:u.queries_set.create(url=request.session["url"],query=getCQL1query(query),cql2=False,object=request.session["obname"],sattr=sattr2,thekind="selected attribute")
except:pass
else:
if "user_id" in request.session:
u=user.objects.get(pk=int(request.session["user_id"]))
try:u.queries_set.create(url=request.session["url"],query=getCQL2query(query),cql2=True,object=request.session["obname"],sattr=sattr2,thekind="selected attribute")
except:pass
d={'the_username':logged_in(request),
'ds': String(request.session['dslist']).split("::"),
'theurl':request.session["url"],
'objects': uml,
'oname':request.session["obname"],
'type':"Selected Attribute",
'sumlattr':umlattr,
'sname':sattr,}
return getresults(request,query,d)
{code}
h6. Process Association Queries
* If the user selected an association query, return the associations and their attributes to the screen
{code}
def assocquery(request,domainModel,uml,theuml):
assoc=domainModel.getExposedUMLAssociationCollection().getUMLAssociation()
tiny=[]
general=domainModel.getUmlGeneralizationCollection().getUMLGeneralization()
someuml=String(theuml.getId())
d=dict()
d2=dict()
for a_general in general:
if(String(theuml.getId()).equals(String(a_general.getSubClassReference().getRefid()))):
someuml=String(a_general.getSuperClassReference().getRefid())
for an_assoc in assoc:
if someuml.equals(String(an_assoc.getSourceUMLAssociationEdge().getUMLAssociationEdge().getUMLClassReference().getRefid())):
refid=an_assoc.getTargetUMLAssociationEdge().getUMLAssociationEdge().getUMLClassReference().getRefid()
roleName=an_assoc.getTargetUMLAssociationEdge().getUMLAssociationEdge().getRoleName()
for auml in uml:
if(String(auml.getId()).equals(String(refid))):
thing=auml.getUmlAttributeCollection().getUMLAttribute()
for attr in thing:tiny.append(attr.getName())
d2[String(auml.getPackageName()+"."+auml.getClassName())]=tiny
d[roleName]=d2
d2=dict()
tiny=[]
elif an_assoc.isBidirectional():
if someuml.equals(String(an_assoc.getTargetUMLAssociationEdge().getUMLAssociationEdge().getUMLClassReference().getRefid())):
refid=an_assoc.getSourceUMLAssociationEdge().getUMLAssociationEdge().getUMLClassReference().getRefid()
roleName=an_assoc.getSourceUMLAssociationEdge().getUMLAssociationEdge().getRoleName()
for auml in uml:
if(String(auml.getId()).equals(String(refid))):
thing=auml.getUmlAttributeCollection().getUMLAttribute()
for attr in thing:tiny.append(attr.getName())
d2[String(auml.getPackageName()+"."+auml.getClassName())]=tiny
d[roleName]=d2
d2=dict()
tiny=[]
return render_to_response('thewebapp/querydataservice.html',
{'the_username':logged_in(request),
'ds': String(request.session['dslist']).split("::"),
'theurl':request.session["url"],
'objects': uml,
'oname':request.session["obname"],
'type':"association",
'd':d,},
context_instance=RequestContext(request))
{code}
* When the user selects an association, retrieve the rolename, classname and attribute. Then prompt the user for a predicate and attribute value
{code}
def getassoc(request):
allinfo=String(request.GET["assoc"])
sarray=allinfo.split("/")
request.session['roleName']=sarray[0]
request.session['className']=sarray[1]
request.session['assoc_attr']=sarray[2]
return render_to_response('thewebapp/querydataservice.html',
{'the_username':logged_in(request),
'ds': String(request.session['dslist']).split("::"),
'theurl':request.session['url'],
'objects': getuml(request),
'oname':request.session["obname"],
'type':"association",
'roleName':request.session['roleName'],
'className':request.session['className'],
'assoc_attr':request.session['assoc_attr']},
context_instance=RequestContext(request))
{code}
* When the user gave a predicate and value, construct the association query, save the query, and process the query
{code}
def getassocattr(request):
query=constructquery(request,"assoc",None,None,String(request.GET['predicate']),String(request.GET['attr_value']),String(request.session['roleName']),String(request.session['className']),String(request.session['assoc_attr']))
if(type(query) is CQL1Query):
if "user_id" in request.session:
u=user.objects.get(pk=int(request.session["user_id"]))
try:u.queries_set.create(url=request.session["url"],query=getCQL1query(query),cql2=False,object=request.session["obname"],thekind="association",rolename=request.session['roleName'],classname=request.session['className'],aattr=request.session['assoc_attr'],predicate=String(request.GET['predicate']),aattrvalue=request.GET['attr_value'])
except Exception, e:print e
else:
if "user_id" in request.session:
u=user.objects.get(pk=int(request.session["user_id"]))
try:u.queries_set.create(url=request.session["url"],query=getCQL2query(query),cql2=True,object=request.session["obname"],thekind="association",rolename=request.session['roleName'],classname=request.session['className'],aattr=request.session['assoc_attr'],predicate=String(request.GET['predicate']),aattrvalue=request.GET['attr_value'])
except Exception, e:print e
d={'the_username':logged_in(request),
'ds': String(request.session['dslist']).split("::"),
'theurl':request.session["url"],
'objects': getuml(request),
'oname':request.session["obname"],
'type':"association",
'roleName':request.session['roleName'],
'className':request.session['className'],
'predicate':request.GET['predicate'],
'assoc_attr':request.session['assoc_attr'],
'attr_value':request.GET['attr_value']}
return getresults(request,query,d)
{code}
h6. Construct Queries
* This method constructs the CQL query, according to the query details given and the CQL type the Data Service utilizes. Don't worry if Eclipse complains about the getName function.
{code}
def constructquery(request,modifier,dattr,sattr,prevalue,value,roleName,className,assoc_attr):
epr=EndpointReferenceType(URI(request.session['url']))
domainModel = MetadataUtils.getDomainModel(epr)
uml= domainModel.getExposedUMLClassCollection().getUMLClass()
if (not(DataServiceFeatureDiscoveryUtil.serviceSupportsCql2(epr))):
query1 = CQL1Query()
target = Object()
target.setName(request.session['obname'])
mod=QueryModifier()
if(modifier=="count"):
mod.setCountOnly(1)
query1.setQueryModifier(mod)
elif(modifier=="dattr"):
mod.setDistinctAttribute(dattr)
query1.setQueryModifier(mod)
elif(modifier=="sattr"):
anothersattr=array(sattr, String)
mod.setAttributeNames(anothersattr)
query1.setQueryModifier(mod)
elif(modifier=="assoc"):
attribute=Attribute(assoc_attr,pred(prevalue,False),value)
assoc=Association(roleName)
assoc.setName(className)
assoc.setAttribute(attribute)
target.setAssociation(assoc)
query1.setTarget(target)
return query1
else:
query2 = CQLQuery()
target = CQLTargetObject()
target.setClassName(request.session['obname'])
mod=CQLQueryModifier()
if(modifier=="count"):
mod.setCountOnly(1)
query2.setCQLQueryModifier(mod)
elif(modifier=="dattr"):
attrib = DistinctAttribute()
attrib.setAttributeName(dattr)
mod.setDistinctAttribute(attrib)
query2.setCQLQueryModifier(mod)
elif(modifier=="sattr"):
selattr=[]
for attr in sattr:
selattr2=NamedAttribute()
selattr2.setAttributeName(attr)
selattr.append(selattr2)
sa=array(selattr, NamedAttribute)
mod.setNamedAttribute(sa)
query2.setCQLQueryModifier(mod)
elif(modifier=="assoc"):
for auml in uml:
temp="%s.%s"%(auml.getPackageName(),auml.getClassName())
if(String(className).equals(String(temp))):
attrs=auml.getUmlAttributeCollection().getUMLAttribute()
for an_attr in attrs:
if(String(an_attr.getName()).equals(String(assoc_attr))):
datatype=an_attr.getDataTypeName()
break
av=AttributeValue()
if(String(datatype).equals(String.getName())):
av.setStringValue(value)
elif(String(datatype).equals(Boolean.getName())):
elif(String(datatype).equals(Boolean.getName())):
try:av.setBooleanValue(Boolean.valueOf(value))
except:pass
elif(String(datatype).equals(Date.getName())):
except:pass
elif(String(datatype).equals(Date.getName())):
try:av.setDateValue(DateFormat.getDateInstance().parse(value))
except:pass
except:pass
try:av.setIntegerValue(Integer.valueOf(value))
except:pass
elif(String(datatype).equals(Long.getName())):
try:av.setLongValue(Long.valueOf(value))
except:pass
elif(String(datatype).equals(Double.getName())):
try:av.setDoubleValue(Double.valueOf(value))
except:pass
else:pass
theattr=CQLAttribute()
theattr.setAttributeValue(av)
predi=pred(prevalue,True)
if((type(predi)==IS_NULL) or (type(predi)==IS_NOT_NULL)):
theattr.setUnaryPredicate(predi)
else:theattr.setBinaryPredicate(predi)
theattr.setName(assoc_attr)
assoc=CQLAssociatedObject()
assoc.setClassName(className)
assoc.setEndName(roleName)
assoc.setCQLAttribute(theattr)
target.setCQLAssociatedObject(assoc)
query2.setCQLTargetObject(target)
return query2
{code}
* This method takes a predicate string value, and depending on which CQL query it is, it returns a predicate object
{code}
def pred(value,cql2):
if(String(value).equals("EQUAL_TO")):
if(cql2):return EQUAL_TO
return et
elif(String(value).equals("GREATER_THAN")):
if(cql2):return GREATER_THAN
return gt
elif(String(value).equals("GREATER_THAN_EQUAL_TO")):
if(cql2):return GREATER_THAN_EQUAL_TO
return gtet
elif(String(value).equals("IS_NOT_NULL")):
if(cql2):return IS_NOT_NULL
return inn
elif(String(value).equals("IS_NULL")):
if(cql2):return IS_NULL
return iN
elif(String(value).equals("LESS_THAN")):
if(cql2):return LESS_THAN
return lt
elif(String(value).equals("LESS_THAN_EQUAL_TO")):
if(cql2):return LESS_THAN_EQUAL_TO
return ltet
elif(String(value).equals("LIKE")):
if(cql2):return LIKE
return l
elif(String(value).equals("NOT_EQUAL_TO")):
if(cql2):return NOT_EQUAL_TO
return net
{code}
h6. Execute Queries
* This method serializes the query into CQL1 and CQL2 query texts and process the query. It stores the query texts and results in the dictionary and displays them on the screen
{code}
def getresults(request,query,mydictionary):
try:
serviceEndpoint=EndpointReferenceType(URI(request.session['url']))
domainModel = MetadataUtils.getDomainModel(serviceEndpoint)
if(type(query) is CQL1Query):
mydictionary['stringw']=processquery(request,None,query)
mydictionary['firstsw']=getCQL1query(query)
secondquery=CQL1toCQL2Converter(domainModel).convertToCql2Query(query)
mydictionary['secondsw']=getCQL2query(secondquery)
else:
mydictionary['stringw']=processquery(request,query,None)
mydictionary['secondsw']=getCQL2query(query)
firstquery=CQL2toCQL1Converter.convertToCql1Query(query)
mydictionary['firstsw']=getCQL1query(firstquery)
return render_to_response('thewebapp/querydataservice.html',
mydictionary,
context_instance=RequestContext(request))
except (MalformedURIException,
RemoteException,
Exception), e:
return error(e,request)
except QueryProcessingExceptionType, e:
return error("QueryProcessingExceptionType",request)
{code}
* This method executes a CQL query and returns the results
{code}
def processquery(request,query2,query1):
epr=EndpointReferenceType(URI(request.session['url']))
dm = MetadataUtils.getDomainModel(epr)
if (String(epr.getAddress().getScheme()).compareTo("http") == 0):
#non secure
client = DataServiceClient(epr)
else:
#secure
baos=String(request.session['user_cred'])
bais=ByteArrayInputStream(baos.getBytes())
cred=GlobusCredential(bais)
client = DataServiceClient(epr, cred)
sw=StringWriter()
if (not(DataServiceFeatureDiscoveryUtil.serviceSupportsCql2(epr))):
if(query1!=None):
cql1results = client.query(query1)
else:
query_1=CQL2toCQL1Converter.convertToCql1Query(query2)
cql1results= client.query(query_1)
Utils.serializeObject(cql1results,DataServiceConstants.CQL_RESULT_COLLECTION_QNAME,sw)
else:
if(query2!=None):
cqlresults= client.executeQuery(query2)
else:
converter=CQL1toCQL2Converter(dm)
query_2=converter.convertToCql2Query(query1)
cqlresults = client.executeQuery(query_2)
CQL2SerializationUtil.serializeCql2QueryResults(cqlresults,sw)
return sw
{code}
* Serialize a CQL1/CQL2 query
{code}
def getCQL1query(query):
firstsw=StringWriter()
Utils.serializeObject(query,DataServiceConstants.CQL_QUERY_QNAME,firstsw)
return firstsw
def getCQL2query(query):
secondsw=StringWriter()
secondsw=CQL2SerializationUtil.serializeCql2Query(query)
return secondsw
{code}
* Save the file
h3. Step 3: Add the Query Data Services urls
# Open *GridClient/urls.py*
# Add the query data services urls and the query data services methods in urlpatterns
{code}
urlpatterns = patterns('',
(r'^index/$', 'thewebapp.views.index'),
(r'^login/$', 'thewebapp.views.login'),
(r'^logout/$', 'thewebapp.views.logout'),
(r'^badlogin/$', 'thewebapp.views.badlogin'),
(r'^listservices/$','thewebapp.views.listservices'),
(r'^discoverservices/$','thewebapp.views.discoverservices'),
(r'^dataservices/$','thewebapp.views.dataservices'),
(r'^geturls/$','thewebapp.views.geturls'),
(r'^getfile/$','thewebapp.views.getfile'),
(r'^getquery/$','thewebapp.views.getquery'),
(r'^getobject/$','thewebapp.views.getobject'),
(r'^getmodifier/$','thewebapp.views.getmodifier'),
(r'^getdattr/$','thewebapp.views.getdattr'),
(r'^getsattr/$','thewebapp.views.getsattr'),
(r'^getassoc/$','thewebapp.views.getassoc'),
(r'^getassocattr/$','thewebapp.views.getassocattr'),
)
{code}
# Save the file
h3. Step 4: Test the Query Data Services page
h4. Select a Data Service
* Go to http://localhost:8000/dataservices/
* Select the Boot Camp service
{gallery:include=dsurl.png}
* The page will then show the objects in the service, and fields for file/query input
{gallery:include=dsobjectorfile.png}
h4. Using a CQLQuery File
* You can give a CQL1/CQL2 Query File as a input, browse for the file, and then select View File
{info}the web application will convert the query to the correct CQL the data service uses{info}
{gallery:include=dsfileinput.png}
* Submit the query, the page will show which file you used, and the equivalent CQL2/CQL1 query
{gallery:include=Screen shot 2011-11-14 at 11.25.30 AM.png}
* The CQL1/CQL2 Results will appear at the bottom of the page
{gallery:include=dscql1results.png}
h4. Give a CQL Query
* You can also just type in a CQL1/CQL2 query, in the textbox in *Submit a CQL1/CQL2 query*
{info}the web application will convert the query to the correct CQL the data service uses{info}
{gallery:include=Screen shot 2011-11-14 at 11.27.20 AM.png}
* After pressing Submit, the page will show it was a Hand-typed Query and the equivalent CQL2/CQL1 query
{gallery:include=Screen shot 2011-11-14 at 11.27.29 AM.png}
* The CQL1/CQL2 Results will appear at the bottom of the page
{gallery:include=Screen shot 2011-11-14 at 11.27.35 AM.png}
h4. Using the query builder
h5. Select an object
* Select an object
{gallery:include=Screen shot 2011-11-14 at 11.56.04 AM.png}
h5. Select a Query Modifier
* Select a Query Modifier
{gallery:include=Screen shot 2011-11-14 at 11.41.20 AM.png}
h6. Select an Object query
* If you select an object query
** The queries will be constructed
{gallery:include=Screen shot 2011-11-14 at 11.41.31 AM.png}
** Results returned
{gallery:include=Screen shot 2011-11-14 at 11.41.44 AM.png}
h6. Select a Count query
* If you select an count query
** The queries will be constructed
{gallery:include=Screen shot 2011-11-14 at 11.41.53 AM.png}
** Results returned
{gallery:include=Screen shot 2011-11-14 at 11.41.59 AM.png}
h6. Select a Distinct Attribute query
* If you select a distinct attribute query, it'll ask for which attribute
{gallery:include=Screen shot 2011-11-14 at 11.42.10 AM.png}
** The queries will be constructed
{gallery:include=Screen shot 2011-11-14 at 11.42.19 AM.png}
** Results returned
{gallery:include=Screen shot 2011-11-14 at 11.42.26 AM.png}
h6. Select a Selected Attribute query
* If you select a selected attribute query, it'll ask for which attribute/s
{gallery:include=Screen shot 2011-11-14 at 11.42.40 AM.png}
** The queries will be constructed
{gallery:include=Screen shot 2011-11-14 at 11.42.54 AM.png}
** Results returned
{gallery:include=Screen shot 2011-11-14 at 11.43.06 AM.png}
h6. Select an Association query
* If you select a selected associated query, it'll ask for which association attribute
{gallery:include=Screen shot 2011-11-14 at 11.43.25 AM.png}
* It'll then ask for which predicate and a value
{gallery:include=Screen shot 2011-11-14 at 11.43.35 AM.png}
** The queries will be constructed
{gallery:include=Screen shot 2011-11-14 at 11.47.53 AM.png}
** Results returned
{gallery:include=Screen shot 2011-11-14 at 11.43.58 AM.png}
{scrollbar}





