Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import org.apache.jackrabbit.oak.spi.commit.CommitInfo
import org.apache.jackrabbit.oak.spi.commit.EmptyHook
import org.apache.jackrabbit.oak.spi.state.NodeStore
import org.apache.jackrabbit.oak.commons.PathUtils
import com.google.common.collect.Lists
import java.util.List
def setProperty(def session, def nodepath, def propertyName, def propertyValue, def isMulti) {
NodeStore ns = session.store
def rnb = ns.root.builder()
def nb = rnb;
String path;
if (PathUtils.isAbsolute(nodepath)) {
path = nodepath;
} else {
path = PathUtils.concat(session.getWorkingPath(), nodepath);
}
List<String> elements = Lists.newArrayList();
PathUtils.elements(path).each{String element ->
if (PathUtils.denotesParent(element)) {
if (!elements.isEmpty()) {
elements.remove(elements.size() - 1);
}
} else if (!PathUtils.denotesCurrent(element)) {
elements.add(element);
}
}
elements.each {
if(it.size() > 0) {
nb = nb.getChildNode(it)
}
}
println "Setting property ${propertyName}: ${propertyValue} on node ${nodepath}"
if(isMulti) {
nb.setProperty(propertyName, Lists.newArrayList(propertyValue), org.apache.jackrabbit.oak.api.Type.STRINGS)
} else {
nb.setProperty(propertyName, propertyValue)
}
ns.merge(rnb, EmptyHook.INSTANCE, CommitInfo.EMPTY)
println "Done"
}
  1. Download https://files.acrobat.com/a/preview/e885654b-8424-4698-b5e9-7751b647276b (or build oak-run 1.1.x version)
  2. Stop all AEM instances
  3. Upload the oak-run 1.1 version to the AEM server if using TarMK or MongoDB server (if using MongoMK)
  4. Run this command to start the oak console
  • on TarMK:
   java -jar target/oak-run.jar console --quiet /path/to/segmentstore
  • For MongoMK run this:
   java -jar oak-run.jar console mongodb://localhost/aem-author
  1. Run these commands to set a property on a node
:load https://gist.githubusercontent.com/andrewmkhoury/b2599fa59079828bea83/raw/setProperty.groovy
setProperty(session, "/path", "propName", "propValue", false)

To build latest oak version:

   $ svn checkout http://svn.apache.org/viewvc/jackrabbit/oak/trunk/
   $ cd oak-run
   $ mvn clean install

Note: use at least maven 3.1.0

@ireasor

This comment has been minimized.

Copy link

ireasor commented Mar 29, 2016

Super useful. Saved our butts with this today! Thanks, Andrew.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.