<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<body link="#355491" alink="#4262a1" vlink="#355491" style="background: #e2e2e2; margin: 0; padding: 20px;">
<div>
        <table cellpadding="0" bgcolor="#FFFFFF" border="0" cellspacing="0" style="border: 1px solid #dadada; margin-bottom: 30px; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                <tbody>
                        <tr>
                                <td>
                                        <table border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF" style="border: solid 2px #ccc; background: #dadada; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                                                <tbody>
                                                        <tr>
                                                                <td bgcolor="#000000" valign="middle" height="58px" style="border-bottom: 1px solid #ccc; padding: 20px; -moz-border-radius-topleft: 3px; -moz-border-radius-topright: 3px; -webkit-border-top-right-radius: 5px; -webkit-border-top-left-radius: 5px;">
                                                                        <h1 style="color: #333333; font: bold 22px Arial, Helvetica, sans-serif; margin: 0; display: block !important;">
                                                                        <!-- To have a header image/logo replace the name below with your img tag -->
                                                                        <!-- Email clients will render the images when the message is read so any image -->
                                                                        <!-- must be made available on a public server, so that all recipients can load the image. -->
                                                                        <a href="http://community.jboss.org/index.jspa" style="text-decoration: none; color: #E1E1E1">JBoss Community</a></h1>
                                                                </td>
                                                        </tr>
                                                        <tr>
                                                                <td bgcolor="#FFFFFF" style="font: normal 12px Arial, Helvetica, sans-serif; color:#333333; padding: 20px; -moz-border-radius-bottomleft: 4px; -moz-border-radius-bottomright: 4px; -webkit-border-bottom-right-radius: 5px; -webkit-border-bottom-left-radius: 5px;"><h3 style="margin: 10px 0 5px; font-size: 17px; font-weight: normal;">
Re: jBPM 4 - Deploy only if ProcessDefinition changed or new
</h3>
<span style="margin-bottom: 10px;">
created by <a href="http://community.jboss.org/people/rapa">rapa</a> in <i>jBPM</i> - <a href="http://community.jboss.org/message/570435#570435">View the full discussion</a>
</span>
<hr style="margin: 20px 0; border: none; background-color: #dadada; height: 1px;">
<div class="jive-rendered-content"><p>Thanks Michael, that worked.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>I have to admit that I would have been happier to use a cleaner solution provided by the api, but well...</p><p>Anyway, if anyone is interested: here is the solution to deploy a process if it is new or it has changed.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>Method executed at startup in the Jbpm class:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p><div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">/**</div><div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">     * Deploys all changed or new process definitions.</div><div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">     */</div><div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">    private void deployProcessDefinitionsIfNewOrChanged() {</div><div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;"> </div><div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">        // Maps process definitions XML to resource names for the latest deployment of each process.</div><div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">        this.processDefinitionsXMLbyResourceName = this.processEngine.execute(new GetDeploymentXmlMapCmd());</div><div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;"> </div><div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">        String currentDeploymentId = null;</div><div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">        for (String processDefinition : this.processDefinitions) {</div><div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">            </div><div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">            currentDeploymentId = deployIfNewOrChanged(processDefinition);</div><div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">            if (currentDeploymentId != null) {</div><div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">                info("Deployed process definition {0} with id {1}", processDefinition, currentDeploymentId);</div><div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">            } else {</div><div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">                info("Process definition {0} did not change from last deployment.", processDefinition);</div><div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">            }</div><div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">            currentDeploymentId = null;</div><div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">        }</div><div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">    }</div><div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">    </div><div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">    /**</div><div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">     * Deploys a process definition if it is new or it has been modified since the current deployment.</div><div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">     * @param resourceName</div><div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">     * @return the deployment id of the deployed process definition or null if the no deployment was needed.</div><div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">     */</div><div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">    private String deployIfNewOrChanged(String resourceName) {</div><div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;"> </div><div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">        RepositoryService repositoryService = this.processEngine.getRepositoryService();</div><div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;"> </div><div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">        // Get the content of the process definition XML file from the resource path</div><div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">        DeploymentImpl newDeployment = (DeploymentImpl) repositoryService.createDeployment()</div><div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">            .addResourceFromClasspath(resourceName);</div><div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">        String newXml = new String(newDeployment.getBytes(resourceName));</div><div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;"> </div><div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">        // Get the currently deployed process definition (gets the latest version)</div><div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">        String currentXml = this.processDefinitionsXMLbyResourceName.get(resourceName);</div><div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">        </div><div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">        if(currentXml != null ? !newXml.equals(currentXml) : true) {</div><div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">            return newDeployment.deploy();</div><div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">        }</div><div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;"> </div><div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">        return null;</div><div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">    }</div><pre class="jive-pre"><code class="jive-code jive-java">   <font color="darkgreen">/**
     * Deploys all changed or new process definitions.
     */</font>
    <font color="navy"><b>private</b></font> <font color="navy"><b>void</b></font> deployProcessDefinitionsIfNewOrChanged() <font color="navy">{</font>
 
        <font color="darkgreen">// Maps process definitions XML to resource names for the latest deployment of each process.</font>
        this.processDefinitionsXMLbyResourceName = this.processEngine.execute(<font color="navy"><b>new</b></font> GetDeploymentXmlMapCmd());
 
        String currentDeploymentId = <font color="navy"><b>null</b></font>;
        <font color="navy"><b>for</b></font> (String processDefinition : this.processDefinitions) <font color="navy">{</font>
 
            currentDeploymentId = deployIfNewOrChanged(processDefinition);
            <font color="navy"><b>if</b></font> (currentDeploymentId != <font color="navy"><b>null</b></font>) <font color="navy">{</font>
                info(<font color="red">"Deployed process definition {0} with id {1}"</font>, processDefinition, currentDeploymentId);
            <font color="navy">}</font> <font color="navy"><b>else</b></font> <font color="navy">{</font>
                info(<font color="red">"Process definition {0} did not change from last deployment."</font>, processDefinition);
            <font color="navy">}</font>
            currentDeploymentId = <font color="navy"><b>null</b></font>;
        <font color="navy">}</font>
    <font color="navy">}</font>
 
    <font color="darkgreen">/**
     * Deploys a process definition if it is new or it has been modified since the current deployment.
     * @param resourceName
     * @return the deployment id of the deployed process definition or null if the no deployment was needed.
     */</font>
    <font color="navy"><b>private</b></font> String deployIfNewOrChanged(String resourceName) <font color="navy">{</font>
 
        RepositoryService repositoryService = this.processEngine.getRepositoryService();
 
        <font color="darkgreen">// Get the content of the process definition XML file from the resource path</font>
        DeploymentImpl newDeployment = (DeploymentImpl) repositoryService.createDeployment()
            .addResourceFromClasspath(resourceName);
        String newXml = <font color="navy"><b>new</b></font> String(newDeployment.getBytes(resourceName));
 
        <font color="darkgreen">// Get the currently deployed process definition (gets the latest version)</font>
        String currentXml = this.processDefinitionsXMLbyResourceName.get(resourceName);
 
        <font color="navy"><b>if</b></font>(currentXml != <font color="navy"><b>null</b></font> ? !newXml.equals(currentXml) : <font color="navy"><b>true</b></font>) <font color="navy">{</font>
            <font color="navy"><b>return</b></font> newDeployment.deploy();
        <font color="navy">}</font>
 
        <font color="navy"><b>return</b></font> <font color="navy"><b>null</b></font>;
    <font color="navy">}</font>
</code></pre></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p><span style="font-family: monospace; white-space: pre;">GetDeploymentXmlMapCmd class</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p><span style="font-family: monospace;"><pre class="jive-pre"><code class="jive-code jive-java"><font color="navy"><b>public</b></font> <font color="navy"><b>class</b></font> GetDeploymentXmlMapCmd <font color="navy"><b>implements</b></font> Command<Map<String, String>> <font color="navy">{</font>
 
    <font color="navy"><b>private</b></font> <font color="navy"><b>static</b></font> <font color="navy"><b>final</b></font> <font color="navy"><b>long</b></font> serialVersionUID = 1L;
 
    @Override
    @SuppressWarnings(<font color="red">"unchecked"</font>)
    <font color="navy"><b>public</b></font> Map<String, String> execute(Environment environment) <font color="navy"><b>throws</b></font> Exception <font color="navy">{</font>
        DbSessionImpl dbSessionImpl = environment.get(DbSessionImpl.class);
        Session session = dbSessionImpl.getSession();
 
        <font color="darkgreen">// Gets the the process definition key property for the latest deployment of each process</font>
        List<DeploymentProperty> dps = session
            .createQuery(<font color="red">"select d from DeploymentProperty d where d.key=:key1 and d.deployment.dbid in "</font> +
                      <font color="red">"(select max(di.deployment.dbid) from DeploymentProperty di where di.key=:key2 "</font> +
                      <font color="red">"group by di.stringValue)"</font>)
            .setParameter(<font color="red">"key1"</font>, <font color="red">"pdkey"</font>)
            .setParameter(<font color="red">"key2"</font>, <font color="red">"pdkey"</font>)
            .list();
       
        <font color="darkgreen">// Creates the map</font>
        Map<String, String> processDefinitionsXMLbyResourceName = <font color="navy"><b>new</b></font> HashMap<String, String>();
        DeploymentImpl deployment;
        String resourceName, xml;
        <font color="navy"><b>for</b></font>(DeploymentProperty dp : dps) <font color="navy">{</font>
            deployment = dp.getDeployment();
            <font color="navy"><b>if</b></font>(deployment.getResourceNames().size() > 0) <font color="navy">{</font>
                <font color="darkgreen">// all deployments always contain 1 resource name</font>
                resourceName = deployment.getResourceNames().iterator().next();
                xml = <font color="navy"><b>new</b></font> String(deployment.getBytes(resourceName));
                processDefinitionsXMLbyResourceName.put(resourceName, xml);
            <font color="navy">}</font>
        <font color="navy">}</font>
        <font color="navy"><b>return</b></font> processDefinitionsXMLbyResourceName;
    <font color="navy">}</font>
   
<font color="navy">}</font>
</code></pre></span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>Cheers!</p><p>Andrea</p></div>
<div style="background-color: #f4f4f4; padding: 10px; margin-top: 20px;">
<p style="margin: 0;">Reply to this message by <a href="http://community.jboss.org/message/570435#570435">going to Community</a></p>
        <p style="margin: 0;">Start a new discussion in jBPM at <a href="http://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2034">Community</a></p>
</div></td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>