## UPDATE VARIABLES

NUXEO_BASEURL=http://localhost:8080/nuxeo
NUXEO_CREDS="-u Administrator:Administrator"
NUXEO_WORKSPACE_PATH=/default-domain/workspaces/SUPNXP-34260
NUXEO_SECTION_PATH=/default-domain/sections/Section_SUPNXP-34260
ES_BASEURL=http://localhost:9200
ES_INDEX=nuxeo
PICTURE_FILE_PATH=~/Pictures/NuxeoHoodies-Group3.png

#### YOU DONT NEED TO CHANGE ANYTHING BELOW THIS LINE

## Check connection to servers
NUXEO_STATUS=`curl -Isw "%{http_code}\n" -o /dev/null ${NUXEO_BASEURL}/login.jsp`
[ "${NUXEO_STATUS}" != "200" ] && { echo "! Connection to ${NUXEO_BASEURL} failed (${NUXEO_STATUS})."; exit 1; }

## Check if workspace and section exist
curl -sf ${NUXEO_CREDS} -o /dev/null -r0-0 ${NUXEO_BASEURL}/api/v1/path${NUXEO_WORKSPACE_PATH}
[ $? -eq 22 ] && { echo "! Workspace ${NUXEO_WORKSPACE_PATH} does not exist, please create"; exit 2; }
curl -sf ${NUXEO_CREDS} -o /dev/null -r0-0 ${NUXEO_BASEURL}/api/v1/path${NUXEO_SECTION_PATH}
[ $? -eq 22 ] && { echo "! Section ${NUXEO_SECTION_PATH} does not exist, please create"; exit 3; }

## Create a Picture document with an image
PIC_JSON=$(curl -XPOST -s ${NUXEO_CREDS} \
-H'content-type: application/json' -H'properties:*' \
${NUXEO_BASEURL}/api/v1/path${NUXEO_WORKSPACE_PATH} \
-d'{
 "entity-type": "document",
 "type": "Picture",
 "name": "Pic1",
 "properties": { "dc:title": "Pic1" }
}')
PIC_UUID=$(echo ${PIC_JSON} |jq -r '.uid')
curl -s ${NUXEO_CREDS} -XPOST \
${NUXEO_BASEURL}/api/v1/automation/Blob.AttachOnDocument \
-F request="{\"params\":{\"document\":\"${PIC_UUID}\"},\"context\":{}}" \
-F input=@${PICTURE_FILE_PATH} \
> /dev/null

## Publish the Picture document (hopefully before the picture views are computed)
PROXY_JSON=$(curl -s ${NUXEO_CREDS} -XPOST \
-H'properties:*' -H 'Content-Type:application/json' \
${NUXEO_BASEURL}/api/v1/id/${PIC_UUID}/@op/Document.PublishToSection \
-d '{ "params": { "target": "'${NUXEO_SECTION_PATH}'" } }')

## Check the proxy and the version in DB just after publication
echo "* Proxy in DB just after publication:"
echo ${PROXY_JSON} |jq
echo "* Observe that properties 'picture:views' and 'picture:info' of proxy still have default values."
PROXY_UUID=$(echo ${PROXY_JSON} |jq -r '.uid')
VERSION_UUID=$(echo ${PROXY_JSON} |jq -r '.proxyTargetId')
echo "* Version in DB just after publication:"
curl -s ${NUXEO_CREDS} -H 'Content-Type:application/json' ${NUXEO_BASEURL}'/api/v1/id/'${VERSION_UUID}'?enrichers.document=acls,permissions&properties=*'|jq
echo "* Observe that properties 'picture:views' and 'picture:info' of version have default values just after publication."

## Check if the proxy and the version it points to, have valid picture views in DB after the picture views are generated
echo "* Please wait for picture views to be generated ..."
read -p "? Get JSON export of proxy ${PROXY_UUID}? (Y/n) " -n 1 -r REPLY
REPLY=${REPLY:-Y}
REPLY=${REPLY^}
[ $REPLY == "Y" ] && curl -s ${NUXEO_CREDS} -H 'Content-Type:application/json' ${NUXEO_BASEURL}'/api/v1/id/'${PROXY_UUID}'?enrichers.document=acls,permissions&properties=*'|jq
echo "* Observe that properties 'picture:views' and 'picture:info' of proxy are updated."

read -p "? Get JSON export of version ${VERSION_UUID}? (Y/n) " -n 1 -r REPLY
REPLY=${REPLY:-Y}
REPLY=${REPLY^}
[ $REPLY == "Y" ] && curl -s ${NUXEO_CREDS} -H 'Content-Type:application/json' ${NUXEO_BASEURL}'/api/v1/id/'${VERSION_UUID}'?enrichers.document=acls,permissions&properties=*'|jq
echo "* Observe that properties 'picture:views' and 'picture:info' of version are updated."

## Check if the proxy and the version it points to, have valid picture views in elasticsearch
ES_PROXY_JSON=$(curl -s ${ES_BASEURL}/${ES_INDEX}/doc/${PROXY_UUID})
echo "* Proxy in elasticsearch:"
echo ${ES_PROXY_JSON} |jq
echo "* Observe that properties 'picture:views' and 'picture:info' of proxy are NOT updated in elasticsearch and still have the default values!!!!!!!!!!!!!!"
read -p "? Continue?" -n 1 -r REPLY

ES_VERSION_JSON=$(curl -s ${ES_BASEURL}/${ES_INDEX}/doc/${VERSION_UUID})
echo "* Version in elasticsearch:"
echo ${ES_VERSION_JSON} |jq
echo "* Observe that properties 'picture:views' and 'picture:info' of version are updated in elasticsearch!!!!!!!!!!!!!!"

## Delete proxy and Picture document
read -p "? Delete Picture document and its proxy? (Y/n) " -n 1 -r REPLY
REPLY=${REPLY:-Y}
REPLY=${REPLY^}
[ $REPLY == "Y" ] && { curl -s ${NUXEO_CREDS} -X DELETE -H 'Content-Type:application/json' ${NUXEO_BASEURL}/api/v1/id/${PROXY_UUID}; curl -s ${NUXEO_CREDS} -X DELETE -H 'Content-Type:application/json' ${NUXEO_BASEURL}/api/v1/id/${PIC_UUID}; }

