Files
d.velop/_Vorlagen/Groovy-Skripte/updateDocuments.groovy
2025-04-04 11:35:25 +02:00

95 lines
3.5 KiB
Groovy

import com.dvelop.d3.server.Document
import com.dvelop.d3.server.DocumentType
import com.dvelop.d3.server.User
import com.dvelop.d3.server.ValueSet
import com.dvelop.d3.server.Validation
import com.dvelop.d3.server.core.D3
import com.dvelop.d3.server.core.D3Interface
import com.dvelop.d3.server.exceptions.SQLException
import com.dvelop.d3.server.exceptions.D3Exception
import java.lang.annotation.*
import java.sql.SQLWarning;
import java.sql.Timestamp
/*
Script for updating documents
*/
// #################################################################################
// Configuration
// #################################################################################
def documentTypes = "('DDRA2', 'XXXXX')" // Document types, syntax like sql "where in "
def oldValue = "AVEVA" // Existing value in the defined categories
def newValue = "AVEVA" // Value to update
int dbPos = 33 // DB Position, TODO: Extend for mutli value fields, currently only for DB-Position 1 - 49
def updateCount = "1000" // Limit the number of documents to be updated - for testing
def LogFilePath = "D:\\d3logs\\updateDocuments.log"
def ErrorLogFilePath = "D:\\d3logs\\updateDocumentsError.log"
def logOnly = "0" // 1 = Only write document ids to logfile
def apiUser = "d3_hook" // User for api calls
boolean noHooks = true // Enable / disable update hooks
// #################################################################################
// End Configuration
// #################################################################################
d3.log.info("Start to update documents " + documentTypes)
d3.log.info("Old Value: " + oldValue)
d3.log.info("New Value: " + newValue)
def LogFile = new File(LogFilePath)
def ErrorLogFile = new File(ErrorLogFilePath)
LogFile.append("\n\rStart Update documents.")
int countUpdated = 0
int countUpdateError = 0
// Define SQL Query
def sqlQuery = "select top ${updateCount} doku_id from firmen_spezifisch where kue_dokuart in" + documentTypes + " and dok_dat_feld_$dbPos = '${oldValue}'"
def resultset = d3.sql.executeAndGet(sqlQuery)
if (resultset.size() > 0)
{
d3.log.info (resultset.size() + " documents found to update.")
LogFile.append("\n\r" + resultset.size() + " documents found to update.")
// Iterate through results
resultset.each
{
if (logOnly == "1")
{
LogFile.append("\n" + it.get("doku_id"))
countUpdated++
}
else
{
try
{
Document currentDoc = d3.archive.getDocument(it.get("doku_id"), apiUser)
currentDoc.field[dbPos] = newValue
currentDoc.updateAttributes(apiUser, noHooks)
LogFile.append("\n" + it.get("doku_id"))
countUpdated++
}
catch (Exception e)
{
d3.log.error ("Update documents: Error " + e + " in script!")
ErrorLogFile.append("\n" + e)
countUpdateError++
}
}
if (countUpdated % 100 == 0)
{
d3.log.info(countUpdated + " documents updated")
}
}
d3.log.info(countUpdated + " documents updated")
LogFile.append("\n\r" + countUpdated + " documents updated")
d3.log.info(countUpdateError + " documents with error")
LogFile.append("\n\r" + countUpdateError + " documents with error")
}