Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Table of Contents

 


Summary

  • This tutorial shows how to make the workflow send the publication request to different groups of users depending on the content path, using the workflow that comes by default on Magnolia 5.3+.

...

Code Block
languagehtml/xml
titlemyproject-workflow.xml
[...]    
<dependency>
    <name>workflow-jbpm</name>
    <version>5.4/*</version>
</dependency>
[...] 

 


Changing Publication task

 


Our default workflow uses a publication task to send the name of the publishers groups to the running workflow, we will need to extend this task to add the new groups.

  1. Locate the publishing task.
    Get to the config app, under /modules/workflow-jbpm, There you can see the task definition class and the parameter resolver class.
  2. Before you create your own TaskDefinition class and TaskParameterResolver class, we need to decide how will we set the new parameters in the configuration, in this tutorial we will create a mapping with path, groupids and repository, this way you can create as many mappings as you need. Once we have this clear, we need to read the new parameters from the configuration into our class.


  3. Create custom TaskDefinition extending from the default one and provide its implementation.

    Code Block
    languagejava
    themeEclipse
    collapsetrue
    public interface CustomTaskDefiniton extends PublicationTaskDefinition {
    
    	Collection<CustomMapping> getCustomMappings();
    
    }
    Code Block
    languagejava
    themeEclipse
    public class CustomTaskDefinitionImpl extends ConfiguredPublicationTaskDefinition implements CustomTaskDefiniton {
    
    	private Collection<CustomMapping> customMappings = new LinkedList<CustomMapping>();
    
    	public Collection<CustomMapping> getCustomMappings() {
    
    		return customMappings;
    	}
    
    	public void setCustomMappings(Collection<CustomMapping> mappings) {
    
    		this.customMappings = mappings;
    	}
    
    	/**
         * Used to define the mappings.
         */
        public static class CustomMapping {
    
        	private String repository;
    		private String path;
            private List<String> groups;
            private boolean enabled = true;
    
            public String getRepository() {
    			return repository;
    		}
    
    		public void setRepository(String repository) {
    			this.repository = repository;
    		}
    
            public String getPath() {
                return this.path;
            }
    
            public void setPath(String path) {
                this.path = path;
            }
    
            public List<String> getGroups() {
                return groups;
            }
    
            public void setGroups(List<String> groups) {
                this.groups = groups;
    
            }
    
            public boolean isEnabled() {
                return this.enabled;
            }
    
            public void setEnabled(boolean enabled) {
                this.enabled = enabled;
            }
        }
    }
    
    
    
  4. Create custom ParameterResolver extending from the default one. The method to override is the setTaskParameters.

    Code Block
    languagejava
    themeEclipse
    collapsetrue
    public class CustomParameterResolver extends PublicationTaskParameterResolver {
    
    	@Inject
    	public CustomParameterResolver(CustomTaskDefiniton definition) {
    		super(definition);
    	}
    
    	@Override
    	public void setTaskParameters(HumanTask task, WorkItem workItem) {
    
    		super.setTaskParameters(task, workItem);
    		Map<String, Object> content = task.getContent();
    
    		if (content != null) {
    			String path = (String) content.get(Context.ATTRIBUTE_PATH);
    			String repository =	(String) content.get(Context.ATTRIBUTE_REPOSITORY);
    			task.setGroupIds(getGroups(repository, path));
    		}
    	}
    
    	protected List<String> getGroups(String repository, String path) {
    
            for (Iterator<CustomMapping> iter = ((CustomTaskDefiniton) getDefinition()).getCustomMappings().iterator(); iter.hasNext();) {
                CustomMapping mapping = iter.next();
    
                if (mapping.isEnabled() && path != null && path.startsWith(mapping.getPath())
                		&amp;&amp; repository != null && repository.startsWith(mapping.getRepository())) {
    
                    return mapping.getGroups();
                }
            }
    		return getDefinition().getGroups();
        }
    }
    
    
  5. Now add the new definition to the module properties file.

    Code Block
    languagexml
    [...]  
    <components>
        <id>main</id>
        <type-mapping>
          <type>info.magnolia.workflow.custom4eye.commands.CustomTaskDefiniton</type>
          <implementation>info.magnolia.workflow.custom4eye.commands.CustomTaskDefinitionImpl</implementation>
        </type-mapping>
    </components>
    [...]  
  6. Add the new class to the task configuration and the mappings.
  7. Compile and run! If you are lucky you will get something like this.

...

Thanks to Rich and Espen for ideas and formattings (wink).