# Заполнение роли в процессе руководителем инициатора (пример скрипта для дизайнера) **Category:** [Дизайнер процессов](https://community.tezis-doc.ru/c/workflow-guides/44) **Created:** 2024-07-02 07:48 UTC **Views:** 161 **Replies:** 0 **URL:** https://community.tezis-doc.ru/t/zapolnenie-roli-v-proczesse-rukovoditelem-inicziatora-primer-skripta-dlya-dizajnera/262 --- ## Post #1 by @d.slepnev В дизайне модуль выполнения скрипта добавляем до назначения на роль руководителя, например, как на скриншоте ниже: Такое расположение модулей позволяет назначить вручную руководителя в процессе, либо оставить на автоматическое назначение. В текст скрипта пишем следующий скрипт 1, в скрипте идет поиск в том же подразделении что и сам инициатор процесса. Для работы процесса нужно проверить, что роли инициатора прописаны одинаково и в процессе и в скрипте. Либо Инициатор, либо Initiatior. Если после скрипта идет модуль не **Назначение**, а **Согласование**, то скрипт должен быть немного другой - скрипт 2. Если после скрипта идет модуль **Параллельное назначение**, то скрипт должен выглядеть вот так - скрипт 3. **Скрипт 1** ``` // initChief import com.haulmont.cuba.core.EntityManager import com.haulmont.cuba.core.Persistence import com.haulmont.cuba.core.Transaction import com.haulmont.cuba.core.global.AppBeans import com.haulmont.cuba.core.global.Metadata import com.haulmont.cuba.security.entity.User import com.haulmont.thesis.core.app.ThesisConstants import com.haulmont.thesis.core.entity.Department import com.haulmont.thesis.core.entity.Employee import com.haulmont.workflow.core.entity.CardRole import com.haulmont.workflow.core.entity.ProcRole import com.haulmont.workflow.core.global.TimeUnit import org.apache.commons.logging.Log import org.apache.commons.logging.LogFactory Log log = LogFactory.getLog("123"); List roles = card.proc.getRoles(); ProcRole procRoleInitiator = null; List employees = null; List chiefs; Persistence persistence = AppBeans.get(Persistence.NAME) Transaction tx = persistence.getTransaction() try { EntityManager em = persistence.getEntityManager() employees = em.createQuery("select e from tm\$User u join u.employees e, wf\$CardRole cr where cr.card.id=:cardId and cr.user.id=u.id and cr.procRole.code=:code", Employee.class) .setParameter("cardId", card.getId()) .setParameter("code", "Initiator") .getResultList() if (!employees.isEmpty()) { log.info("Employees finded") Department dep = employees.get(0).getDepartment(); if (dep == null) return chiefs = em.createQuery("select u from tm\$User u join u.employees e join u.userRoles ur where e.department.id=:departmentId and " + "((u.group.id=:groupDepId and ur.role.name=:depChief) or (u.group.id=:groupDivId and ur.role.name=:divChief))", User.class) .setParameter("departmentId", dep.getId()) .setParameter("groupDepId", ThesisConstants.SEC_GROUP_DEPARTMENT_CHIEF_ID) .setParameter("groupDivId", ThesisConstants.SEC_GROUP_SUBDIVISION_CHIEF_ID) .setParameter("depChief", ThesisConstants.SEC_ROLE_DEPARTMENT_CHIEF) .setParameter("divChief", ThesisConstants.SEC_ROLE_SUBDIVISION_CHIEF).getResultList(); log.info("chiefs count: "+chiefs.size()) } tx.commit(); } finally { tx.end() } ProcRole procRole = null; for (ProcRole pr : card.proc.getRoles()) { if ("Руководитель".equals(pr.getCode())) { procRole = pr; break; } } if (procRole == null || chiefs == null || chiefs.isEmpty()) { return; } log.info("chiefs finded") Metadata metadata = AppBeans.get(Metadata.NAME); CardRole cardRole = metadata.create(CardRole.class); cardRole.setProcRole(procRole); cardRole.setCode(procRole.getCode()); cardRole.setNotifyByEmail(true); cardRole.setNotifyByCardInfo(true); cardRole.setCard(card); cardRole.setUser(chiefs.get(0)); // cardRole.setSortOrder(1) cardRole.setDuration(1); cardRole.setTimeUnit(TimeUnit.DAY) Transaction tx2 = persistence.getTransaction() try { EntityManager em = persistence.getEntityManager() em.persist(cardRole); card.getRoles().add(cardRole) tx2.commit(); } finally { tx2.end() } ``` **Скрипт 2** ``` // initChief import com.haulmont.cuba.core.EntityManager import com.haulmont.cuba.core.Persistence import com.haulmont.cuba.core.Transaction import com.haulmont.cuba.core.global.AppBeans import com.haulmont.cuba.core.global.Metadata import com.haulmont.cuba.security.entity.User import com.haulmont.thesis.core.app.ThesisConstants import com.haulmont.thesis.core.entity.Department import com.haulmont.thesis.core.entity.Employee import com.haulmont.workflow.core.entity.CardRole import com.haulmont.workflow.core.entity.ProcRole import com.haulmont.workflow.core.global.TimeUnit import org.apache.commons.logging.Log import org.apache.commons.logging.LogFactory Log log = LogFactory.getLog("123"); List roles = card.proc.getRoles(); ProcRole procRoleInitiator = null; List employees = null; List chiefs; Persistence persistence = AppBeans.get(Persistence.NAME) Transaction tx = persistence.getTransaction() try { EntityManager em = persistence.getEntityManager() employees = em.createQuery("select e from tm\$User u join u.employees e, wf\$CardRole cr where cr.card.id=:cardId and cr.user.id=u.id and cr.procRole.code=:code", Employee.class) .setParameter("cardId", card.getId()) .setParameter("code", "»нициатор") .getResultList() if (!employees.isEmpty()) { log.info("Employees finded") Department dep = employees.get(0).getDepartment(); if (dep == null) return chiefs = em.createQuery("select u from tm\$User u join u.employees e join u.userRoles ur where e.department.id=:departmentId and " + "((u.group.id=:groupDepId and ur.role.name=:depChief) or (u.group.id=:groupDivId and ur.role.name=:divChief))", User.class) .setParameter("departmentId", dep.getId()) .setParameter("groupDepId", ThesisConstants.SEC_GROUP_DEPARTMENT_CHIEF_ID) .setParameter("groupDivId", ThesisConstants.SEC_GROUP_SUBDIVISION_CHIEF_ID) .setParameter("depChief", ThesisConstants.SEC_ROLE_DEPARTMENT_CHIEF) .setParameter("divChief", ThesisConstants.SEC_ROLE_SUBDIVISION_CHIEF).getResultList(); log.info("chiefs count: "+chiefs.size()) } tx.commit(); } finally { tx.end() } ProcRole procRole = null; for (ProcRole pr : card.proc.getRoles()) { if ("–уководитель".equals(pr.getCode())) { procRole = pr; break; } } if (procRole == null || chiefs == null || chiefs.isEmpty()) { return; } log.info("chiefs finded") Metadata metadata = AppBeans.get(Metadata.NAME); CardRole cardRole = metadata.create(CardRole.class); // cardRole.setProcRole(procRole); cardRole.setCode(procRole.getCode()); cardRole.setNotifyByEmail(true); cardRole.setNotifyByCardInfo(true); // cardRole.setCard(card); // cardRole.setUser(chiefs.get(0)); // cardRole.setSortOrder(1) cardRole.setDuration(1); cardRole.setTimeUnit(TimeUnit.DAY) Transaction tx2 = persistence.createTransaction() try { EntityManager em = persistence.getEntityManager() cardRole.setProcRole(em.reload(procRole)); cardRole.setCard(em.reload(card)); cardRole.setUser(em.reload(chiefs.get(0))); em.persist(cardRole); // card.getRoles().add(cardRole) tx2.commit(); } finally { tx2.end() } ``` **Скрипт 3** ``` // initChief import com.haulmont.cuba.core.EntityManager import com.haulmont.cuba.core.Persistence import com.haulmont.cuba.core.Transaction import com.haulmont.cuba.core.global.AppBeans import com.haulmont.cuba.core.global.Metadata import com.haulmont.cuba.security.entity.User import com.haulmont.thesis.core.app.ThesisConstants import com.haulmont.thesis.core.entity.Department import com.haulmont.thesis.core.entity.Employee import com.haulmont.workflow.core.entity.CardRole import com.haulmont.workflow.core.entity.ProcRole import com.haulmont.workflow.core.global.TimeUnit import org.apache.commons.logging.Log import org.apache.commons.logging.LogFactory Log log = LogFactory.getLog("123"); List roles = card.proc.getRoles(); ProcRole procRoleInitiator = null; List employees = null; List chiefs; Persistence persistence = AppBeans.get(Persistence.NAME) Transaction tx = persistence.getTransaction() try { EntityManager em = persistence.getEntityManager() employees = em.createQuery("select e from tm\$User u join u.employees e, wf\$CardRole cr where cr.card.id=:cardId and cr.user.id=u.id and cr.procRole.code=:code", Employee.class) .setParameter("cardId", card.getId()) .setParameter("code", "Initiator") .getResultList() if (!employees.isEmpty()) { log.info("Employees finded") Department dep = employees.get(0).getDepartment(); if (dep == null) return chiefs = em.createQuery("select u from tm\$User u join u.employees e join u.userRoles ur where e.department.id=:departmentId and " + "((u.group.id=:groupDepId and ur.role.name=:depChief) or (u.group.id=:groupDivId and ur.role.name=:divChief))", User.class) .setParameter("departmentId", dep.getId()) .setParameter("groupDepId", ThesisConstants.SEC_GROUP_DEPARTMENT_CHIEF_ID) .setParameter("groupDivId", ThesisConstants.SEC_GROUP_SUBDIVISION_CHIEF_ID) .setParameter("depChief", ThesisConstants.SEC_ROLE_DEPARTMENT_CHIEF) .setParameter("divChief", ThesisConstants.SEC_ROLE_SUBDIVISION_CHIEF).getResultList(); log.info("chiefs count: "+chiefs.size()) } tx.commit(); } finally { tx.end() } ProcRole procRole = null; for (ProcRole pr : card.proc.getRoles()) { if ("ChiefOfInitiator".equals(pr.getCode())) { procRole = pr; break; } } if (procRole == null || chiefs == null || chiefs.isEmpty()) { return; } log.info("chiefs finded") Transaction tx2 = persistence.createTransaction() try { EntityManager em = persistence.getEntityManager() Metadata metadata = AppBeans.get(Metadata.NAME); CardRole cardRole = metadata.create(CardRole.class); cardRole.setProcRole(em.reload(procRole,"_minimal")); cardRole.setCode(procRole.getCode()); cardRole.setNotifyByEmail(true); cardRole.setNotifyByCardInfo(true); cardRole.setCard(em.reload(card,"_minimal")); cardRole.setUser(em.reload(chiefs.get(0),"_minimal")); // cardRole.setSortOrder(1) cardRole.setDuration(1); cardRole.setTimeUnit(TimeUnit.DAY) em.persist(cardRole); card.getRoles().add(cardRole) tx2.commit(); } finally { tx2.end() } ``` --- **Canonical:** https://community.tezis-doc.ru/t/zapolnenie-roli-v-proczesse-rukovoditelem-inicziatora-primer-skripta-dlya-dizajnera/262 **Original content:** https://community.tezis-doc.ru/t/zapolnenie-roli-v-proczesse-rukovoditelem-inicziatora-primer-skripta-dlya-dizajnera/262