package com.plugin.test.DynUser; import org.joget.plugin.base.ApplicationPlugin; import org.joget.plugin.base.DefaultPlugin; import org.joget.plugin.base.FormVariablePlugin; import org.joget.plugin.base.ParticipantPlugin; import org.joget.plugin.base.PluginProperty; import java.util.logging.Level; import java.util.logging.Logger; import java.util.Map; import org.joget.workflow.model.WorkflowAssignment; import bsh.Interpreter; import org.apache.commons.collections.SequencedHashMap; import java.util.*; import org.joget.directory.model.*; import org.joget.plugin.base.PluginManager; import org.joget.directory.model.service.DirectoryManager; import org.joget.workflow.util.WorkflowUtil; public class DynamicUserMapping extends DefaultPlugin implements ApplicationPlugin, FormVariablePlugin, ParticipantPlugin { public String getName() { return "DynamicUserMapping"; } public String getDescription() { return "Maps the user dynamically for an activity based on conditions..."; } public String getVersion() { return "1.0.0"; } public PluginProperty[] getPluginProperties() { PluginProperty[] properties = new PluginProperty[]{ new PluginProperty("script", "Script", PluginProperty.TYPE_TEXTAREA, null, null) }; return properties; } public Object execute(Map properties) { //Object result = null; String script = (String) properties.get("script"); System.out.println("Config Value : "+script); System.out.println("Inside Dynamic user Mapping plugin..."); WorkflowAssignment wfAssignment = (WorkflowAssignment) properties.get("workflowAssignment"); try { PluginManager pluginManager = null; if (properties != null) { pluginManager = (PluginManager) properties.get("pluginManager"); } /*DirectoryManager dm = (DirectoryManager) pluginManager.getBean("directoryManager"); System.out.println("Current User Name : "+WorkflowUtil.getCurrentUsername()); User user = dm.getUserByUsername( WorkflowUtil.getCurrentUsername() ); Collection employments = user.getEmployments(); Collection result = new ArrayList(); if (employments.iterator().hasNext()){ Employment employment = (Employment) employments.iterator().next(); Department department = employment.getDepartment(); System.out.println("Dept Id : "+department.getId()); Collection users = dm.getUserByDepartmentId(department.getId()); for (Iterator it=users.iterator(); it.hasNext(); ) { User tempUser = (User) it.next(); if(dm.isUserInGroup(tempUser.getUsername(), "HOD") )){ result.add(tempUser.getUsername()); } result.add(tempUser.getUsername()); System.out.println("User Name : "+tempUser.getUsername()); } }*/ Collection result = new ArrayList(); DirectoryManager dm = (DirectoryManager) pluginManager.getBean("directoryManager"); Collection usrList = dm.getUserList(); for (Iterator it=usrList.iterator(); it.hasNext(); ) { User tempUser = (User) it.next(); result.add(tempUser.getUsername()); System.out.println("User Name : "+tempUser.getUsername()); //wfAssignment.setAssigneeName(tempUser.getUsername()); } return result; } catch (Exception e) { Logger.getLogger(getClass().getName()).log(Level.WARNING, "Error executing plugin", e); return null; } } public Map getVariableOptions(Map properties) { Map resultMap = null; String script = (String) properties.get("script"); script = WorkflowUtil.processVariable(script, "", null); Object result = executeScript(script, properties); if (result instanceof Map) { resultMap = (Map) result; } else if (result instanceof Object[]) { resultMap = new SequencedHashMap(); for (Object row : (Object[]) result) { String val = row.toString(); resultMap.put(val, val); } } else { resultMap = new HashMap(); } return resultMap; } protected Object executeScript(String script, Map properties) { Object result = null; try { Interpreter interpreter = new Interpreter(); interpreter.setClassLoader(getClass().getClassLoader()); for (Object key : properties.keySet()) { interpreter.set(key.toString(), properties.get(key)); } Logger.getLogger(getClass().getName()).log(Level.FINE, "Executing script " + script); result = interpreter.eval(script); return result; } catch (Exception e) { Logger.getLogger(getClass().getName()).log(Level.WARNING, "Error executing script", e); return null; } } public Collection getActivityAssignments(Map properties) { String script = (String) properties.get("script"); script = WorkflowUtil.processVariable(script, "", null); return (Collection) executeScript(script, properties); } }