OnContentRead behavior
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-14-2013 06:17 AM
I want to execute a script when users READ files on Alfresco. I read Jeff Potts guide "Implementing custom behaviors" and tried to create a behavior using OnContentRead policy.
This is what I did until now:
Modified custom-model-context.xml like this:
<?xml version='1.0' encoding='UTF-8'?><!DOCTYPE beans PUBLIC '-//SPRING//DTD BEAN//EN' 'http://www.springframework.org/dtd/spring-beans.dtd'> <beans> <!– Registration of new models –> <bean id="extension.dictionaryBootstrap" parent="dictionaryModelBootstrap" depends-on="dictionaryBootstrap"> <property name="models"> <list> <value>alfresco/extension/customModel.xml</value> </list> </property> </bean> <bean id="onContentReadpolicy" class="org.alfresco.repo.policy.registration.ClassPolicyRegistration" parent="policyRegistration"> <property name="policyName"> <value>{http://www.alfresco.org}onContentRead</value> </property> <property name="className"> <value>{http://www.alfresco.org/model/content/1.0}content</value> </property> <property name="behaviour"> <bean class="org.alfresco.repo.jscript.ScriptBehaviour" parent="scriptBehaviour"> <property name="location"> <bean class="org.alfresco.repo.jscript.ClasspathScriptLocation"> <constructor-arg> <value>alfresco/extension/scripts/myscript.js</value> </constructor-arg> </bean> </property> </bean> </property></bean></beans>
And added a simple myscript.js in alfresco/extension/scripts.
When I restart Alfresco it doesn't work, it seems to crash at all…
I'm pretty sure I'm doing something wrong, I looked at alfresco.log but can't understand what's the problem…
Hope in some help!
P.S. I'm using Alfresco Community version 4.2.e on Windows 7 machine
- Labels:
-
Archive
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-14-2013 09:59 AM
Your error may be caused by other code ,for example your customModel or your myscript.js.
You'd better paste your error log here.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-14-2013 01:35 PM
myscript.js
logger.log("Script works!");
And file
customModel.xml has the following code:
<?xml version="1.0" encoding="UTF-8"?><!– Custom Model –><!– Note: This model is pre-configured to load at startup of the Repository. So, all custom –><!– types and aspects added here will automatically be registered –><model name="custom:customModel" xmlns="http://www.alfresco.org/model/dictionary/1.0"> <!– Optional meta-data about the model –> <description>Custom Model</description> <author></author> <version>1.0</version> <imports> <!– Import Alfresco Dictionary Definitions –> <import uri="http://www.alfresco.org/model/dictionary/1.0" prefix="d"/> <!– Import Alfresco Content Domain Model Definitions –> <import uri="http://www.alfresco.org/model/content/1.0" prefix="cm"/> </imports> <!– Introduction of new namespaces defined by this model –> <!– NOTE: The following namespace custom.model should be changed to reflect your own namespace –> <namespaces> <namespace uri="custom.model" prefix="custom"/> </namespaces> </model>
I don't know what exactly it does, but anyway it is located in the same folder of custom-model-context.xml
alfresco.log or catalina.out aren't useful because like I said Alfresco at the restart crashes, nothing works and I need to restart the machine too. But when I comment oncontentread policy (in custom-model-context.xml) alfresco begins working again… and when I look at the logs I can't find errors about this…
Did I miss something??
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-16-2013 12:53 AM
2.nothing is wrong myscript.js,
Although there is something wrong in your customModel.xml,but It will not crash alfresco.
Did you place all files in correct location? custom-model-context.xml and customModel.xml should be placed under shared\classes\alfresco\extension, myscript.js should be placed under hared\classes\alfresco\extension\scripts.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-16-2013 05:19 AM
Anyways thank you very much for helping me I really appreciate it.
I will ask another quick help. Is there a chance to execute the script on the workspace?
It will be nice to put the script in dictionary/scripts.
I tried to change value in the custom-model-context like following:
…. <property name="location"> <bean class="org.alfresco.repo.jscript.ClasspathScriptLocation"> <constructor-arg> <value>\$\{selectSingleNode('workspace://SpacesStore', 'lucene', 'PATH:"/app:company_home/app:dictionary/app:scripts/cm:myscript.js"' )\}</value> </constructor-arg> </bean> </property>….
But it gives me error:
11:10:04,136 ERROR [org.springframework.web.context.ContextLoader] Context initialization failedorg.alfresco.service.cmr.repository.InvalidNodeRefException: Node does not exist: \$\{selectSingleNode('workspace://SpacesStore', 'lucene', 'PATH:"/app:company_home/app:dictionary/app:scripts/cm:myscript.js"' )\}(null)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-16-2013 08:10 PM
you are using classpath script location ,alfresco will search myscript.js in classpath instead of repostitory.
I'm afraid you can't implement it just using spring configuration ,although alfresco provide an org.alfresco.repo.web.scripts.RepositoryScriptLocation ,you can't use it in spring configuration
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-17-2013 04:19 AM
On the repository myscript.js does this:
var companyhome = search.findNode("path", ['workspace','SpacesStore','/']);var newfolderz = companyhome.childByNamePath("Siti/Test-repository/documentLibrary");var nodeToCopy = getNodeToCopy();var yourNewNode = nodeToCopy.copy(newfolder);function getNodeToCopy(){ return utils.getNodeFromString("workspace://SpacesStore/c1fcf42b-b61e-41c7-bafc-88aaccfa5d1c")}
Obviously in the classpath all these API do not work… I will need a workaround or some other APIs to operate on the repository
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-17-2013 09:56 PM
The reason why you can't call these api in classpath is because:
1.when alfresco startup ,it'll try to registe your custom class policy.during registing ,it'll parse myscript.js file ,but at that time a lot of repository javascript api is not avaliable (for example companyhome,userhome and so on).
So to avoid this problem ,your custom script behaviour should not contain any these api.
You can use java backed behaviour as instead or you'can implement your requirement using action an rule .
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-18-2013 06:51 AM
I know I can trigger an action with rules on folder, but they are only when user update, copy or delete content, no viewing……….. …
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-18-2013 10:09 AM
I guess by " users READ files" you mean use download or view document.so you should not use behaviour ,here is the reason:
If you use OnContentRead behaviour ,your behaviour will be invoked when the content is accessed(for example full text indexing ,file tranformation),not just when it is downloaded or viewed by user .
So I suggest you customize an proxy operation(servlet or webscript) that response to user'download(view) reqest and do your requirement processing.