cancel
Showing results for 
Search instead for 
Did you mean: 

behaviourFilter.disableBehaviour in JavaScript

thbaymet
Champ in-the-making
Champ in-the-making
Hi,

How can I do the behaviour disable in javascript as it is


behaviourFilter.disableBehaviour(nodeRef, ContentModel.ASPECT_AUDITABLE);


in Java.

Thanks
9 REPLIES 9

mrogers
Star Contributor
Star Contributor
Don't think you can.    You probably need to add another root scoped object to alfresco script.

romschn
Star Collaborator
Star Collaborator
You can achieve this by writing a custom javascript extension.
Have tried to provide you the steps at high-level below.
1. Create a java class which extends BaseProcessorExtension
2. Register the class in your context.xml
  
<bean id="myjscriptbean" parent="baseJavaScriptExtension"  class="com.my.jscript">
       <property name="extensionName">
           <value>myjscript</value>
       </property>
        <property name="behaviourFilter" ref="policyBehaviourFilter" />
   <!– you may inject other required services if you want –>
   </bean>

3. In your java class, create public method and inside that method, perform behaviourFilter.disableBehaviour.
4. From the javascript, invoke the method using myjscript.methodname.

As Ramesh indicates you can create a javascript root object to do this. I think that is the preferable way of doing this. I would add the caveat that unless you are careful any user may be able to utilize your script extensions so be careful what you expose or add code in your method to make certain that the user has whatever minimum role/permissions you want associated with the action.

Having said that there is a sneaky way of doing this with pure JavaScript if your script is part of a web script. JavaScript run from a web script that is loaded from the classpath has elevated permissions compared to a web script that is run with the execute script action or that is loaded from the repository. Also, if this is a regular script and you execute it from the JavaScript Console (see Share Extras on GitHub), you will also get the elevated security. With the elevated security you have access to the Package "root object" that Rhino makes available; with this you can do things like:

var foo = new Package.java.io.File("test.txt");

now you have a JavaScript object foo that can access the Java File object you just constructed. Given this insight you can get a handle on the Alfresco Spring Application Context. From that you can obtain the bean named policyBehaviourFilter. From there you can call the disableBehaviour() method.

I plan to show details of how to do this exact thing in my talk at summit this year.

mlagneaux
Champ on-the-rise
Champ on-the-rise
Hello,

I've already tried that and it didn't work. I've created a topic in the french forum about that:
https://forums.alfresco.com/fr/forum/d%C3%A9veloppement/d%C3%A9veloppement-autour-dalfresco/d%C3%A9s...

I've also tried to create a new action based on "executeScript": it doesn't work neither.

mlagneaux
Champ on-the-rise
Champ on-the-rise
Just one more thing: as far as i'm concerned, i didn't try to do that in a webscript but in a script called by executeScript action in Alfresco Explorer. Perhaps there's a difference in the way transactions are managed in those 2 cases.

kaynezhang
World-Class Innovator
World-Class Innovator
Why not create a custom action ,in your java action you can disable behaviour and do oher work.Then call your custom action in explorer.

romschn
Star Collaborator
Star Collaborator
@mlagneaux - The approach I have provided does work both the ways - either through webscript or through Run Action > Execute Script > your javascript having a method call using custom javascript extension implemented above.

I am not sure what you have tried did not work for you.

beber7
Champ in-the-making
Champ in-the-making
Hey all,

Can we go a little more low level.
I've been trying to create a javascript extension as Romschn adviced but I can't figure out how to deploy my Java Class and the Bean.

Can we have some documentation or some step on how to achieve this, I'm sure it's not something to complicated.

Thanks guys,
Bert

beber7
Champ in-the-making
Champ in-the-making
Okay, I finally figured it out.. SO i'm sharing this to help others, since it's not very clear or explained at all.

To do a custom root object, or javascript api extension you need to create an AMP file. ( And it's the only way according to what I read in alfresco documentation ).

To create an amp use the following maven command :
mvn archetype:generate -Dfilter=org.alfresco:

Pick option 1

then

You need to add your Java file at the path

/src/main/java of your amp folder previously generated


then add the service-context at

src/main/amp/config/alfresco/module/repo-amp/context/service-context.xml


You can build your amp using the command

mvn clean install

—————-

Go to target, get your amp file freshly created and upload it on your server at <your_alfresco_install> (often /opt/alfresco-share/) copy in /amps
Execute bin/apply_amps.sh

Then execute alfreso.sh restart

There you go Smiley Happy


Here are the two best link to my opinion to understand how it works :
How to create the amp : http://docs.alfresco.com/5.1/tasks/alfresco-sdk-tutorials-share-amp-archetype.html
How to modify/custom the amp : http://docs.alfresco.com/5.1/references/dev-extension-points-javascript-root-objects.html
For the runAS : https://shahpritesh.wordpress.com/2013/07/17/run-javascript-function-as-admin-privileges/comment-pag...

Good luck everyone !