Skip to content

Instantly share code, notes, and snippets.

@sdumitriu
Last active November 18, 2016 16:20
Show Gist options
  • Save sdumitriu/019057c677df82a5cc10f30ddc100c8d to your computer and use it in GitHub Desktop.
Save sdumitriu/019057c677df82a5cc10f30ddc100c8d to your computer and use it in GitHub Desktop.
PhenoTips - Set a specific study for a patient record via REST
#!/bin/bash
# Change the following three variables to what you need:
XAUTH='Admin:admin'
PATIENT='P0000001'
STUDY='Hereditary motor neuropathies'
# This is the identifier of a virtual instance in a farm
# Most installations aren't set up as farms, so the default shouldn't be changed
INSTANCE='xwiki'
# Find the object number of the existing StudyBindingClass, if it exists, in order to update its value:
# - get all the objects on the patient profile
# - reformat the resulting XML to have one tag per line
# - look for the PhenoTips.StudyBindingClass object and print only this and the following line (the one with the object number)
# - keep only the line with the object number
# - extract the object number from the XML tag
# Or, use this XPath on the XML directly:
# string(//*[*[local-name() = "className" and string(.) = "PhenoTips.StudyBindingClass"]]/*[local-name() = "number"])
# Or, if JSON is preferred, add the "Accept: application/json" header to get back a JSON with the same structure
XOBJNR=`curl -s -u ${XAUTH} http://localhost:8080/rest/wikis/${INSTANCE}/spaces/data/pages/${PATIENT}/objects | sed -r -e ': start' -e 's/(<\/[^>]+?>)([^\n])/\1\n\2/g' -e 't start' | grep -E '<className>PhenoTips.StudyBindingClass</className>' -A 1 | grep -E '<number>' | sed -r -e 's/.*>(.*)<.*/\1/'`
if [[ -n $XOBJNR ]]
then
if [[ -z $STUDY ]]
then
# Object exists, but the study to set is empty -> delete the object
curl -s -o /dev/null -u ${XAUTH} -X DELETE http://localhost:8080/rest/wikis/${INSTANCE}/spaces/data/pages/${PATIENT}/objects/PhenoTips.StudyBindingClass/${XOBJNR}
else
# Object exists and we're setting a specific study -> update the object
curl -s -o /dev/null -u ${XAUTH} -X PUT -H 'Content-Type: text/plain' -d "${INSTANCE}:Studies.${STUDY}" http://localhost:8080/rest/wikis/${INSTANCE}/spaces/data/pages/${PATIENT}/objects/PhenoTips.StudyBindingClass/${XOBJNR}/properties/studyReference
fi
elif [[ -n $STUDY ]]
then
# Object doesn't exist and we're setting a specific study -> create and initialize a new object
curl -s -o /dev/null -u ${XAUTH} -X POST -H 'Content-Type: application/x-www-form-urlencoded' -d "className=PhenoTips.StudyBindingClass&property#studyReference=${INSTANCE}:Studies.${STUDY}" http://localhost:8080/rest/wikis/${INSTANCE}/spaces/data/pages/${PATIENT}/objects
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment