cancel
Showing results for 
Search instead for 
Did you mean: 

OnContentRead behavior

cristianfrog
Champ in-the-making
Champ in-the-making
Three days I'm stuck on this:

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
11 REPLIES 11

kaynezhang
World-Class Innovator
World-Class Innovator
It seems nothing is wrong with your custom spring configuration file custom-model-context.xml .
Your error may be caused by other code ,for example your customModel or your myscript.js.
You'd better paste your error log here.

Really my script does nothing special:

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??

kaynezhang
World-Class Innovator
World-Class Innovator
1.You have define an custom model ,but there isn't any type or aspect defination in it.and also namespace format is wrong.
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.

cristianfrog
Champ in-the-making
Champ in-the-making
Yes, the files are in the right path. Anyway strangely now everything works and script is executed… Can't say what was going wrong… i'm pretty surprised about this… maybe something else on the machine was blocking Alfresco.

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 failed
org.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)

kaynezhang
World-Class Innovator
World-Class Innovator
<strong><bean class="org.alfresco.repo.jscript.ClasspathScriptLocation"></strong>

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

myscript.js needs to copy 1 file in a new location, I can't do anything with my script except log some information with logger.log? so sick….

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 Smiley Sad

kaynezhang
World-Class Innovator
World-Class Innovator
Script api saved in classpath and saved in repository share the same runtime,so in the classpath all these API  will work.
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 .

What do you mean with "implement your requirement using action an rule" ?
I know I can trigger an action with rules on folder, but they are only when user update, copy or delete content, no viewing……….. …

kaynezhang
World-Class Innovator
World-Class Innovator
I'm sorry I ignored your viewing requirement .

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.