Skip to content

Instantly share code, notes, and snippets.

@stillalex
Created March 6, 2012 09:43
Show Gist options
  • Select an option

  • Save stillalex/1985351 to your computer and use it in GitHub Desktop.

Select an option

Save stillalex/1985351 to your computer and use it in GitHub Desktop.
Jackrabbit Version tweaking
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.jackrabbit.core.version;
import javax.jcr.Node;
import javax.jcr.Session;
import javax.jcr.version.Version;
import javax.jcr.version.VersionHistory;
import javax.jcr.version.VersionIterator;
import javax.jcr.version.VersionManager;
import org.apache.jackrabbit.JcrConstants;
import org.apache.jackrabbit.test.AbstractJCRTest;
public class VTest extends AbstractJCRTest {
protected Session session;
private VersionManager vm;
private Node n;
protected void setUp() throws Exception {
super.setUp();
session = getHelper().getReadWriteSession();
vm = session.getWorkspace().getVersionManager();
n = session.getRootNode().addNode(
"versiontest-" + System.currentTimeMillis());
n.addMixin(JcrConstants.MIX_VERSIONABLE);
session.save();
}
protected void tearDown() throws Exception {
if (session != null) {
session.removeItem(n.getPath());
session.save();
session.logout();
session = null;
}
super.tearDown();
}
public void testV() throws Exception {
n.setProperty("vp", 1);
session.save();
vm.checkin(n.getPath());
vm.checkout(n.getPath());
n.setProperty("vp", 2);
session.save();
vm.checkin(n.getPath());
printHist(n.getPath());
tweakHist(n, true);
System.out.println("after change ------");
printHist(n.getPath());
}
private void printHist(String path) throws Exception {
System.out.println("History for " + path);
VersionHistory vh = vm.getVersionHistory(path);
VersionIterator it = vh.getAllVersions();
while (it.hasNext()) {
Version version = it.nextVersion();
Node fn = version.getFrozenNode();
StringBuilder sb = new StringBuilder();
sb.append(version.getName());
sb.append(": ");
if (fn.hasProperty("vp")) {
sb.append("vp ");
sb.append(fn.getProperty("vp").getLong());
} else {
sb.append(" no vp");
}
sb.append(" ");
if (fn.hasProperty("v2")) {
sb.append("v2 ");
sb.append(fn.getProperty("v2").getLong());
} else {
sb.append(" no v2");
}
sb.append(" ");
System.out.println(sb.toString());
}
}
private void tweakHist(Node n, boolean deleteOldVersion) throws Exception {
VersionHistory vh = vm.getVersionHistory(n.getPath());
VersionIterator it = vh.getAllVersions();
while (it.hasNext()) {
Version version = it.nextVersion();
if (version.getName()
.equalsIgnoreCase(JcrConstants.JCR_ROOTVERSION)) {
// skipping root version
continue;
}
vm.restore(version, true);
session.refresh(false);
vm.checkout(n.getPath());
n.setProperty("vp", n.getProperty("vp").getLong() * 10);
n.setProperty("v2", "1");
session.save();
vm.checkin(n.getPath());
if (deleteOldVersion) {
vh.removeVersion(version.getName());
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment