Updating variables in confluence template with Java/Groovy

we have a classification of our system inside one of our Jira addons.
Now we want to sync this with Confluence template. Basically, we have a confluence space template, where one of the template variables (inttype in the script below) should have a list of values, that we take from Jira. So the idea is to periodically run a script, or confluence API call, that will update the contents of the template.
Searching through Confluence Javadoc, or API, I found no method to update page template
here is the code I used to try to do this

import com.atlassian.confluence.pages.templates.PageTemplateManager
import com.atlassian.sal.api.component.ComponentLocator
import groovy.xml.MarkupBuilder
import groovyx.net.http.HTTPBuilder
import groovyx.net.http.HttpResponseException
import org.json.JSONObject
import groovy.json.JsonSlurper
import org.apache.commons.codec.binary.Base64
import org.apache.log4j.Level
import org.apache.log4j.Logger
import com.atlassian.confluence.pages.templates.PageTemplate
import com.atlassian.confluence.api.service.content.template.ContentTemplateService
import groovy.json.JsonBuilder
import groovy.json.JsonOutput
import groovy.transform.BaseScript
import groovyx.net.http.ContentType
import groovyx.net.http.HTTPBuilder
import groovyx.net.http.Method
import com.onresolve.scriptrunner.db.DatabaseUtil


PageTemplateManager pageTemplateManager = ComponentLocator.getComponent(PageTemplateManager.class\)
def originalTemplate = pageTemplateManager.getPageTemplate(543653894L\)
def newTemplate = new PageTemplate(originalTemplate\)
newTemplate.setOriginalVersionPageTemplate(originalTemplate\)
//options to set
def currentOptions = \['Option 1', 'Option 2']

def oldValues = \[]
def variables = \[]
def changed = false
def resultBody = ""
def currentBody = ""
def newContent = ""

variables = pageTemplateManager.getTemplateVariables(newTemplate\)
variables.each\{
    if \(it.name == "inttype"\)\{
        oldValues = it.getOptions\(\)
        if \(\(currentOptions \- oldValues\).size\(\) > 0\)\{
            it.setOptions(currentOptions\)
            changed = true
        }
    }   
}
log.error(originalTemplate.getContent\(\)\)
if \(changed\)\{
    log.error(currentOptions\)
    log.error(oldValues\)
    newTemplate.setContent(pageTemplateManager.mergeVariables(newTemplate, variables, "IPD"\)\)
    pageTemplateManager.savePageTemplate(newTemplate, originalTemplate\)
    pageTemplateManager.refreshPageTemplate(originalTemplate\)
}

Could somebody point me out to a class or a documentation, that I should use to make my changes to confluence space template permanent?

Regards,
Sergey