Skip to content

Commit

Permalink
Merge pull request #31 from openlibraryenvironment/feature-search-con…
Browse files Browse the repository at this point in the history
…solidate

Harmonize SearchController template & json returns
  • Loading branch information
philboeselager authored Apr 10, 2019
2 parents 6359ce1 + c37eddc commit 80d5690
Showing 1 changed file with 55 additions and 51 deletions.
106 changes: 55 additions & 51 deletions server/gokbg3/grails-app/controllers/org/gokb/SearchController.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -166,67 +166,71 @@ class SearchController {
}
}

def apiresponse = null
if ( ( response.format == 'json' ) || ( response.format == 'xml' ) ) {
apiresponse = [:]
apiresponse.count = result.reccount
apiresponse.max = result.max
apiresponse.offset = result.offset
apiresponse.records = []
result.recset.each { r ->
def response_record = [:]
response_record.oid = "${r.class.name}:${r.id}"
result.qbetemplate.qbeConfig.qbeResults.each { rh ->
response_record[rh.heading] = groovy.util.Eval.x(r, 'x.' + rh.property)
}
apiresponse.records.add(response_record);
}
} else {
result.new_recset = []
log.debug("Create new recset..")
result.recset.each { r ->
def response_record = [:]
response_record.oid = "${r.class.name}:${r.id}"
response_record.obj = r
response_record.cols = []
result.qbetemplate.qbeConfig.qbeResults.each { rh ->
def ppath = rh.property.split(/\./)
def cobj = r
def final_oid = "${cobj.class.name}:${cobj.id}"

if (!params.hide || !params.hide.contains(rh.qpEquiv)) {

ppath.eachWithIndex { prop, idx ->
def sp = prop.minus('?')

if( cobj?.class?.name == 'org.gokb.cred.RefdataValue' ) {
cobj = cobj.value
}
else {
if ( cobj && KBComponent.has(cobj, sp)) {
cobj = cobj[sp]

if (ppath.size() > 1 && idx == ppath.size()-2) {
if (cobj && sp != 'class') {
final_oid = "${cobj.class.name}:${cobj.id}"
}
else {
final_oid = null
}
}
def apiresponse = ['count': result.reccount, 'max': result.max, 'offset': result.offset, records: []]

result.new_recset = []
log.debug("Create new recset..")
result.recset.each { r ->
def response_record = [:]
response_record.oid = "${r.class.name}:${r.id}"
response_record.obj = r
response_record.cols = []
def api_record = ['oid': response_record.oid]

result.qbetemplate.qbeConfig.qbeResults.each { rh ->
def ppath = rh.property.split(/\./)
def cobj = r
def final_oid = "${cobj.class.name}:${cobj.id}"

if (!params.hide || !params.hide.contains(rh.qpEquiv)) {

ppath.eachWithIndex { prop, idx ->
def sp = prop.minus('?')

if( cobj?.class?.name == 'org.gokb.cred.RefdataValue' ) {
cobj = cobj.value
}
else {
if ( cobj && KBComponent.has(cobj, sp)) {
def oobj = cobj

cobj = cobj[sp]

if ( sp == 'name' && !cobj && oobj.respondsTo('getDisplayName')) {
cobj = oobj.displayName
}
else {
cobj = null

if (ppath.size() > 1 && idx == ppath.size()-2) {
if (cobj && sp != 'class') {
final_oid = "${cobj.class.name}:${cobj.id}"
}
else {
final_oid = null
}
}
}
else {
cobj = null
}
}
}
if( response.format == 'json' || response.format == 'xml' ) {
api_record['oid'] = response_record.oid
api_record["${rh.heading}"] = cobj ?: null
}
else {
response_record.cols.add([link: (rh.link ? (final_oid ?: response_record.oid ) : null), value: (cobj ?: '-Empty-')])
}
}
}
if( response.format == 'json' || response.format == 'xml' ) {
apiresponse.records.add(api_record)
}
else {
result.new_recset.add(response_record)
}
log.debug("Finished new recset!")
}
log.debug("Finished new recset!")

result.withoutJump = cleaned_params
result.remove('jumpToPage');
Expand Down

0 comments on commit 80d5690

Please sign in to comment.