cancel
Showing results for 
Search instead for 
Did you mean: 

Scheduled actions with javascript and/or xml

lorenz
Champ in-the-making
Champ in-the-making
Hi all,

I'm trying to define some scheduled processes that execute actions defined in xml or in JavaScript.

So far I managed to persuade Alfresco to execute a query on the repository and sending me a test email for every record found. I use a custom aspect called "expiring" in my own namespace http://www.mydomain.org/model/content/1.0 like the following:


   <bean id="sendTestMail" class="org.alfresco.repo.action.scheduled.CronScheduledQueryBasedTemplateActionDefinition">
      <property name="transactionMode">
         <value>UNTIL_FIRST_FAILURE</value>
      </property>
      <property name="compensatingActionMode">
         <value>IGNORE</value>
      </property>
      <property name="searchService">
         <ref bean="SearchService"/>
      </property>
      <property name="templateService">
         <ref bean="TemplateService"/>
      </property>
      <property name="queryLanguage">
         <value>lucene</value>
      </property>
      <property name="stores">
         <list>
            <value>workspace://SpacesStore</value>
         </list>
      </property>
      <!– Find all nodes that have the aspect my:expiring and are due tomorrow –>
      <property name="queryTemplate">
         <value>
            +PATH:"/app:company_home/*//*"
            +ASPECT:"{http://www.mydomain.org/model/content/1.0}expiring"
            @my\:expirationDate:${luceneDateRange(today, "P1D")}
         </value>
      </property>
      <property name="cronExpression">
         <value>10 * * * * ?</value>
      </property>
      <property name="jobName">
         <value>mydomainExpirationChecker</value>
      </property>
      <property name="jobGroup">
         <value>mydomainJobGroup</value>
      </property>
      <property name="triggerName">
         <value>mydomainTriggerEC</value>
      </property>
      <property name="triggerGroup">
         <value>mydomainTriggerGroup</value>
      </property>
      <property name="scheduler">
         <ref bean="schedulerFactory"/>
      </property>
      <property name="actionService">
         <ref bean="ActionService"/>
      </property>
      <property name="templateActionModelFactory">
         <ref bean="templateActionModelFactory"/>
      </property>
      <property name="templateActionDefinition">
         <ref bean="sendTestMailAction"/>
      </property>
      <property name="transactionService">
         <ref bean="TransactionService"/>
      </property>
      <property name="runAsUser">
         <value>System</value>
      </property>
   </bean>

The above configuration executes an action called "sendTestMailAction" defined as follows:


<bean id="sendTestMailAction" class="org.alfresco.repo.action.scheduled.SimpleTemplateActionDefinition">
      <property name="actionName">
         <value>mail</value>
      </property>
      <property name="parameterTemplates">
         <map>
            <entry>
               <key><value>to</value></key>
               <value>lorenz@mydomain.org</value>
            </entry>
            <entry>
               <key><value>from</value></key>
               <value>lorenz@mydomain.org</value>
            </entry>
            <entry>
               <key><value>text</value></key>
               <value>this is a test</value>
            </entry>
            <entry>
               <key><value>subject</value></key>
               <value>test from scheduler</value>
            </entry>
         </map>
      </property>
      <property name="templateActionModelFactory">
         <ref bean="templateActionModelFactory"/>
      </property>
      <property name="dictionaryService">
         <ref bean="DictionaryService"/>
      </property>
      <property name="actionService">
         <ref bean="ActionService"/>
      </property>
      <property name="templateService">
         <ref bean="TemplateService"/>
      </property>
   </bean>

.. so far everything runs smoothly (well at least I get tons of emails Smiley Wink

I now wonder how I could change the above to send some meaningful messages, i.e. using an email template containing a reference to the content linked to the things the query found.

Another thing I was not able to figure out is how I manage to execute some JavaScript code instead of the above email action.

I'm quite new to Alfresco, so please be forgiving in case I just asked something stupid 😕 I read through the Wiki and various posts here on the forum, but couldn't find an answer to my questions.

What I'm trying to achieve in the end is to implement some sort of scheduling processes that can move content from space to another or start advanced workflow processes depending on an objects expiration date.

Any help ist greatly appreciated.

Lorenz
6 REPLIES 6

kevinr
Star Contributor
Star Contributor
I now wonder how I could change the above to send some meaningful messages, i.e. using an email template containing a reference to the content linked to the things the query found.

Another thing I was not able to figure out is how I manage to execute some JavaScript code instead of the above email action.

Both those things will require you to call the Template and Script services directly in code, there is not configuration for this. In your action you can call the services and perform the steps you need, it shouldn't be hard to add custom configuration for your action so it knows what template/javascript to execute. You can find examples of calling the script/template services in the TemplateContentServlet and ExecuteScriptCommand classes.

Hope this helps,

Kevin

lorenz
Champ in-the-making
Champ in-the-making
thank you kevin 🙂

I now just execute a JavaScript file in my data dictionary. From there I try to do all the tings I need in js..

Just in case somebody is interested. The following SimpleTemplateActionDefinition does the trick on the "action-side":


<bean id="javaAction" class="org.alfresco.repo.action.scheduled.SimpleTemplateActionDefinition">
   <property name="actionName">
      <value>script</value>
   </property>
   <property name="parameterTemplates">
      <map>
         <entry>
            <key><value>script-ref</value></key>  
            <value>${selectSingleNode('workspace://SpacesStore', 'lucene', 'PATH:"/app:company_home/app:dictionary/app:scripts/cm:mydomain-scheduled-action.js"' )}</value>
         </entry>               
      </map>
   </property>
   <property name="templateActionModelFactory">
      <ref bean="templateActionModelFactory"/>
   </property>
   <property name="dictionaryService">
      <ref bean="DictionaryService"/>
   </property>
   <property name="actionService">
      <ref bean="ActionService"/>
   </property>
   <property name="templateService">
      <ref bean="TemplateService"/>
   </property>
</bean>

I created a file called mydomain-scheduled-action.js and loaded it into the data dictionary in Alfresco. The CronScheduledQueryBasedTemplateActionDefinition definition changed a bit too:



   <property name="queryTemplate">
      <value>PATH:"/app:company_home"</value>
   </property>
   <property name="cronExpression">
      <value>30 * * * * ?</value>
   </property>
   <property name="jobName">
      <value>mydomainExpirationChecker</value>
   </property>
   <property name="jobGroup">
      <value>mydomainJobGroup</value>
   </property>
   <property name="triggerName">
      <value>mydomainTriggerEC</value>
   </property>
   <property name="triggerGroup">
      <value>mydomainTriggerGroup</value>
   </property>
   <property name="scheduler">
      <ref bean="schedulerFactory"/>
   </property>
   <property name="actionService">
      <ref bean="ActionService"/>
   </property>
   <property name="templateActionModelFactory">
      <ref bean="templateActionModelFactory"/>
   </property>
   <property name="templateActionDefinition">
      <ref bean="javaAction"/>
   </property>
   <property name="transactionService">
      <ref bean="TransactionService"/>
   </property>
   <property name="runAsUser">
      <value>System</value>
   </property>
</bean>

another question for Kevin:
Did I get it right that if I query for "PATH:"/app:company_home" in the queryTemplate value I'll always get only one "hit" and therefore my js-file is only executed once per run?

Cheers
Lorenz

kevinr
Star Contributor
Star Contributor
Yes fully qualified PATHs are unique so you should only get one result.

Thanks,

Kevin

vishalkumarnall
Champ in-the-making
Champ in-the-making
Hi Lorenz,

Can you give me steps on how you have configured for Scheduled Actions.Please help, i am looking for similar sheduled Action where files older than 7 days in a space should be moved to another space.

Thanks.

seraphon
Champ in-the-making
Champ in-the-making
Just in case somebody is interested. The following SimpleTemplateActionDefinition does the trick on the "action-side":
Hi

I am really interested in this.
Can you please precise what file you modify or create and where do you do it, meanin in the extension folder or in the files in the webapps?
It would greatly help because I m not sure if I understood the mechanism right.
It looks to me that you added to the file scheduled-action-services-context.xml in the shared/…/extension folder
your code, which consists in the def of 2 actions Thought I have no idea how to add aspect that works, having failed to make the example given in the wiki work..

Regards seraphon

naveenkumar
Champ in-the-making
Champ in-the-making
hi all,
   For my application i need to move the content from one space to another after the expirationdate. Outside the web project i am able to do. But inside i have no idea about the path.here is my schedular job


<bean id="moveAction" class="org.alfresco.repo.action.scheduled.SimpleTemplateActionDefinition">
        <property name="actionName">
            <value>move</value>
        </property>
        <property name="parameterTemplates">
            <map>
               <entry>
                    <key>
                        <value>destination-folder</value>
                    </key>
                    <value>${selectSingleNode('workspace://SpacesStore', 'lucene', 'PATH:"//cm:Folder_x0020_Archive"')</value>
                </entry>
                <entry>
                    <key>
                        <value>assoc-type</value>
                    </key>
                    <value>${node.primaryParentAssoc.typeQName}</value>
                </entry>
                <entry>
                    <key>
                        <value>assoc-name</value>
                    </key>
                    <value>${node.primaryParentAssoc.QName}</value>
                </entry>
               <!– <entry>
                    <key>
                        <value>deep-copy</value>
                    </key>
                    <value>false</value>
                </entry> –>
            </map>
        </property>
        <property name="templateActionModelFactory">
            <ref bean="templateActionModelFactory"/>
        </property>
        <property name="dictionaryService">
            <ref bean="DictionaryService"/>
        </property>
        <property name="actionService">
            <ref bean="ActionService"/>
        </property>
        <property name="templateService">
            <ref bean="TemplateService"/>
        </property>
    </bean>

<bean id="moveTutorialEveryTenMinutes" class="org.alfresco.repo.action.scheduled.CronScheduledQueryBasedTemplateActionDefinition">
        <property name="transactionMode">
            <value>UNTIL_FIRST_FAILURE</value>
        </property>
        <property name="compensatingActionMode">
            <value>IGNORE</value>
        </property>
        <property name="searchService">
            <ref bean="SearchService"/>
        </property>
        <property name="templateService">
            <ref bean="TemplateService"/>
        </property>
        <property name="queryLanguage">
            <value>lucene</value>
        </property>
        <property name="stores">
            <list>
                <value>workspace://SpacesStore</value>
            </list>
        </property>
        <property name="queryTemplate">
           <value> @wca\:expirationDate:${luceneDateRange(today,today)}</value>
        </property>
        <property name="cronExpression">
            <value>0 0/1 * * * ?</value>
        </property>
        <property name="jobName">
            <value>jobC</value>
        </property>
        <property name="jobGroup">
            <value>jobGroup</value>
        </property>
        <property name="triggerName">
            <value>triggerC</value>
        </property>
        <property name="triggerGroup">
            <value>triggerGroup</value>
        </property>
        <property name="scheduler">
            <ref bean="schedulerFactory"/>
        </property>
        <property name="actionService">
            <ref bean="ActionService"/>
        </property>
        <property name="templateActionModelFactory">
            <ref bean="templateActionModelFactory"/>
        </property>
        <property name="templateActionDefinition">
            <ref bean="moveAction"/>
        </property>
        <property name="transactionService">
            <ref bean="TransactionService"/>
        </property>
        <property name="runAsUser">
            <value>System</value>
        </property>
    </bean>

Created two folders(Folder Archive and software) inside the web project, After expiry i want to move the content from software folder to Archive folder.here is my destination folder location:: I think the path is wrong??
<value>${selectSingleNode('workspace://SpacesStore', 'lucene', 'PATH:"//cm:Folder_x0020_Archive"')</value>

Here is my query template based on the property.
<property name="queryTemplate">
           <value> @wca\:expirationDate:${luceneDateRange(today,today)}</value>
        </property>

Please any one help me..

Thanks
Naveen