Skip to content

Instantly share code, notes, and snippets.

@oanhthai
Last active November 9, 2015 03:42
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save oanhthai/613a11449f1c2cd26c69 to your computer and use it in GitHub Desktop.
Index: ../../git/magnolia-5.4-repo/main/magnolia-core/src/main/java/info/magnolia/commands/impl/VersionCommand.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- ../../git/magnolia-5.4-repo/main/magnolia-core/src/main/java/info/magnolia/commands/impl/VersionCommand.java (revision 47e28b95d4660e1d8e7916466f07ead711a24869)
+++ ../../git/magnolia-5.4-repo/main/magnolia-core/src/main/java/info/magnolia/commands/impl/VersionCommand.java (revision )
@@ -44,6 +44,7 @@
import java.util.List;
import java.util.Map;
+import javax.inject.Inject;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.RepositoryException;
@@ -60,6 +61,13 @@
private String comment;
+ private final VersionManager versionManager;
+
+ @Inject
+ public VersionCommand(final VersionManager versionManager) {
+ this.versionManager = versionManager;
+ }
+
/**
* @see info.magnolia.commands.MgnlCommand#execute(info.magnolia.context.Context)
*/
@@ -69,11 +77,17 @@
if (isRecursive()) {
// set versionMap and version name for this node
List versionMap = new ArrayList();
- versionRecursively(node, ctx, versionMap);
+
+ String userName = null;
+ if (ctx.containsKey(Context.ATTRIBUTE_REQUESTOR) && ctx.get(Context.ATTRIBUTE_REQUESTOR) != null) {
+ userName = ctx.get(Context.ATTRIBUTE_REQUESTOR).toString();
+ }
+
+ versionRecursively(node, ctx, versionMap, userName);
ctx.setAttribute(Context.ATTRIBUTE_VERSION_MAP, versionMap, Context.LOCAL_SCOPE);
} else {
addComment(node);
- Version version = VersionManager.getInstance().addVersion(node, getRule());
+ Version version = versionManager.addVersion(node, getRule());
if (version != null) {
ctx.setAttribute(Context.ATTRIBUTE_VERSION, version.getName(), Context.LOCAL_SCOPE);
}
@@ -107,10 +121,11 @@
*
* @param versionMap keeps tack of the versioned nodes and gets passed to the context.
*/
- private void versionRecursively(Node node, Context ctx, List versionMap) throws RepositoryException {
+ private void versionRecursively(Node node, Context ctx, List versionMap, String userName) throws RepositoryException {
addComment(node);
- Version version = VersionManager.getInstance().addVersion(node, getRule());
+ Version version = versionManager.addVersion(node, getRule(), userName);
+
Map entry = new HashMap();
entry.put("uuid", node.getIdentifier());
if (version != null) {
@@ -127,7 +142,7 @@
while (children.hasNext()) {
Node child = children.nextNode();
if (!getRule().isAllowed(child)) {
- versionRecursively(child, ctx, versionMap);
+ versionRecursively(child, ctx, versionMap, userName);
}
}
}
Index: ../../git/magnolia-5.4-repo/main/magnolia-core/src/main/java/info/magnolia/cms/core/version/BaseVersionManager.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- ../../git/magnolia-5.4-repo/main/magnolia-core/src/main/java/info/magnolia/cms/core/version/BaseVersionManager.java (revision 47e28b95d4660e1d8e7916466f07ead711a24869)
+++ ../../git/magnolia-5.4-repo/main/magnolia-core/src/main/java/info/magnolia/cms/core/version/BaseVersionManager.java (revision )
@@ -78,6 +78,7 @@
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.IOUtils;
+import org.apache.commons.lang3.StringUtils;
import org.apache.jackrabbit.JcrConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -198,13 +199,28 @@
public synchronized Version addVersion(final Node node, final Rule rule) throws UnsupportedRepositoryOperationException,
RepositoryException {
final String userName = getSafelyUserNameFromMgnlContext();
+ return addVersion(node, rule, userName);
+ }
+
+ /**
+ * Add version of the specified node and all child nodes while ignoring the same node type.
+ *
+ * @param node to be versioned
+ * @return newly created version node
+ * @throws UnsupportedOperationException if repository implementation does not support Versions API
+ * @throws javax.jcr.RepositoryException if any repository error occurs
+ */
+ public synchronized Version addVersion(final Node node, final Rule rule, final String userName) throws UnsupportedRepositoryOperationException,
+ RepositoryException {
final String workspaceName = node.getSession().getWorkspace().getName();
+ final String versionedUser = StringUtils.isNotEmpty(userName) ? userName : getSafelyUserNameFromMgnlContext();
+
Version version = MgnlContext.doInSystemContext(new JCRSessionOp<Version>(workspaceName) {
@Override
public Version exec(Session session) throws RepositoryException {
try {
- return createVersion(session.getNodeByIdentifier(node.getIdentifier()), rule, userName);
+ return createVersion(session.getNodeByIdentifier(node.getIdentifier()), rule, versionedUser);
} catch (RepositoryException re) {
// since add version is synchronized on a singleton object, its safe to revert all changes made in
// the session attached to workspace - mgnlVersion
Index: ../../git/magnolia-5.4-repo/main/magnolia-core/src/test/java/info/magnolia/commands/impl/VersionCommandTest.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- ../../git/magnolia-5.4-repo/main/magnolia-core/src/test/java/info/magnolia/commands/impl/VersionCommandTest.java (revision 47e28b95d4660e1d8e7916466f07ead711a24869)
+++ ../../git/magnolia-5.4-repo/main/magnolia-core/src/test/java/info/magnolia/commands/impl/VersionCommandTest.java (revision )
@@ -81,7 +81,7 @@
component = node.addNode("component-test", NodeTypes.Component.NAME);
node.getSession().save();
- cmd = new VersionCommand();
+ cmd = new VersionCommand(VersionManager.getInstance());
cmd.setComment("comment");
cmd.setRepository("website");
cmd.setUuid(node.getIdentifier());
@@ -146,7 +146,7 @@
@Test
public void testCommandWithRuleSet() throws Exception {
// GIVEN
- cmd = new VersionCommand();
+ cmd = new VersionCommand(VersionManager.getInstance());
cmd.setComment("comment");
cmd.setRepository("website");
cmd.setUuid(node.getIdentifier());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment