В скрипте заполнить:
String PROC_CODE = “proc_20190128_154253315” //код процесса
String ROLE_CODE = “Исполнители” //код роли из процесса
Скрипт разместить с модулем Начало.
Скрипт
import com.haulmont.cuba.core.global.AppBeans
import com.haulmont.cuba.core.global.DataManager
import com.haulmont.cuba.core.global.LoadContext
import com.haulmont.cuba.core.global.Metadata
import com.haulmont.cuba.security.entity.User
import com.haulmont.thesis.core.entity.Employee
import com.haulmont.thesis.core.entity.SimpleDoc
import com.haulmont.workflow.core.entity.CardRole
import com.haulmont.workflow.core.entity.ProcRole
import groovy.transform.Field
String PROC_CODE = "proc_20190128_154253315"
String ROLE_CODE = "Исполнители"
@Field DataManager dataManager = AppBeans.get(DataManager.NAME)
SimpleDoc doc = card as SimpleDoc
Employee docReceiver = doc.docReceiver
if (docReceiver == null)
return true
docReceiver = dataManager.reload(docReceiver, "employee.with.user")
if (docReceiver && docReceiver.user) {
User user = docReceiver.user
CardRole executor = findCardRole(user, PROC_CODE, ROLE_CODE)
if (executor == null) {
ProcRole procRole = getProcRole(PROC_CODE, ROLE_CODE)
println(procRole)
if (procRole != null)
createCardRole(user, procRole)
}
}
return true
CardRole findCardRole(User user, String procCode, String roleCode) {
LoadContext loadContext = new LoadContext(CardRole.class)
loadContext.setQueryString("select cr from wf\$CardRole cr " +
"where cr.card.id=:cardId and cr.user.id=:userId and cr.code=:roleCode " +
"and cr.procRole.proc.code=:procCode")
.setParameter("cardId", card)
.setParameter("userId", user)
.setParameter("roleCode", roleCode)
.setParameter("procCode", procCode)
return dataManager.load(loadContext)
}
ProcRole getProcRole(String procCode, String roleCode) {
LoadContext loadContext = new LoadContext(ProcRole.class)
loadContext.setQueryString("select pr from wf\$ProcRole pr " +
"where pr.code=:roleCode and pr.proc.code=:procCode")
.setParameter("roleCode", roleCode)
.setParameter("procCode", procCode)
return dataManager.load(loadContext)
}
void createCardRole(User user, ProcRole procRole) {
Metadata metadata = AppBeans.get(Metadata.NAME)
CardRole cardRole = metadata.create(CardRole.class)
cardRole.setCard(card)
cardRole.setUser(user)
cardRole.setProcRole(procRole)
cardRole.setCode(procRole.getCode())
cardRole.setSortOrder(1)
dataManager.commit(cardRole)
}