-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
376 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
...src/main/java/de/valtech/aecu/core/groovy/console/bindings/actions/resource/AddMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package de.valtech.aecu.core.groovy.console.bindings.actions.resource; | ||
|
||
import de.valtech.aecu.core.groovy.console.bindings.actions.Action; | ||
import de.valtech.aecu.core.groovy.console.bindings.actions.util.MixinUtil; | ||
import javax.annotation.Nonnull; | ||
import javax.jcr.Node; | ||
import javax.jcr.RepositoryException; | ||
import javax.jcr.nodetype.NoSuchNodeTypeException; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.apache.sling.api.resource.PersistenceException; | ||
import org.apache.sling.api.resource.Resource; | ||
|
||
/** | ||
* Adds a mixin to a resource/node if the mixin exists. | ||
* | ||
* @author Bart Thierens | ||
* @author Eric Manzi | ||
*/ | ||
public class AddMixin implements Action { | ||
|
||
private final String mixinName; | ||
|
||
public AddMixin(String mixinName) { | ||
this.mixinName = mixinName; | ||
} | ||
|
||
@Override | ||
public String doAction(@Nonnull Resource resource) throws PersistenceException { | ||
if (StringUtils.isBlank(mixinName)) { | ||
return "WARNING: mixin name is empty"; | ||
} | ||
|
||
Node node = resource.adaptTo(Node.class); | ||
if (node == null) { | ||
return "WARNING: could not get node for " + resource.getPath(); | ||
} | ||
|
||
try { | ||
MixinUtil.ensureMixin(node, mixinName); | ||
return String.format("Adding mixin %s to %s", mixinName, resource.getPath()); | ||
} catch (NoSuchNodeTypeException nsnte) { | ||
return "WARNING: non-existing mixin: " + mixinName; | ||
} | ||
catch (RepositoryException re) { | ||
throw new PersistenceException("Problem when adding mixin to node", re); | ||
} | ||
} | ||
|
||
} |
50 changes: 50 additions & 0 deletions
50
.../main/java/de/valtech/aecu/core/groovy/console/bindings/actions/resource/RemoveMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package de.valtech.aecu.core.groovy.console.bindings.actions.resource; | ||
|
||
import de.valtech.aecu.core.groovy.console.bindings.actions.Action; | ||
import de.valtech.aecu.core.groovy.console.bindings.actions.util.MixinUtil; | ||
import javax.annotation.Nonnull; | ||
import javax.jcr.Node; | ||
import javax.jcr.RepositoryException; | ||
import javax.jcr.nodetype.NoSuchNodeTypeException; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.apache.sling.api.resource.PersistenceException; | ||
import org.apache.sling.api.resource.Resource; | ||
|
||
/** | ||
* Removes a mixin from a resource/node if the mixin is present. | ||
* | ||
* @author Bart Thierens | ||
* @author Eric Manzi | ||
*/ | ||
public class RemoveMixin implements Action { | ||
|
||
private final String mixinName; | ||
|
||
public RemoveMixin(String mixinName) { | ||
this.mixinName = mixinName; | ||
} | ||
|
||
@Override | ||
public String doAction(@Nonnull Resource resource) throws PersistenceException { | ||
if (StringUtils.isBlank(mixinName)) { | ||
return "WARNING: mixin name is empty"; | ||
} | ||
Node node = resource.adaptTo(Node.class); | ||
if (node == null) { | ||
return "WARNING: could not get node for " + resource.getPath(); | ||
} | ||
|
||
try { | ||
if (MixinUtil.hasMixin(node, mixinName)) { | ||
node.removeMixin(mixinName); | ||
return String.format("Removing mixin %s from %s", mixinName, resource.getPath()); | ||
} | ||
return String.format("No mixin %s present on %s", mixinName, resource.getPath()); | ||
} catch (NoSuchNodeTypeException nsnte) { | ||
return "WARNING: non-existing mixin: " + mixinName; | ||
} catch (RepositoryException re) { | ||
throw new PersistenceException("Problem when removing mixin from node", re); | ||
} | ||
} | ||
|
||
} |
43 changes: 43 additions & 0 deletions
43
core/src/main/java/de/valtech/aecu/core/groovy/console/bindings/actions/util/MixinUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package de.valtech.aecu.core.groovy.console.bindings.actions.util; | ||
|
||
import java.util.Arrays; | ||
import javax.annotation.Nonnull; | ||
import javax.jcr.Node; | ||
import javax.jcr.RepositoryException; | ||
|
||
/** | ||
* Utility class for mixin related actions | ||
* | ||
* @author Bart Thierens | ||
*/ | ||
public class MixinUtil { | ||
|
||
private MixinUtil() { | ||
throw new UnsupportedOperationException("Cannot instantiate MixinUtil"); | ||
} | ||
|
||
/** | ||
* Checks if a mixin is present on a node | ||
* @param node the non-null node to check | ||
* @param mixin the non-null mixin to check (can be a non-existing mixin type) | ||
* @return true if the mixin is present on the node, otherwise false | ||
* @throws RepositoryException if an exception occurs while performing repository operations | ||
*/ | ||
public static boolean hasMixin(@Nonnull Node node, @Nonnull String mixin) throws RepositoryException { | ||
return Arrays.stream(node.getMixinNodeTypes()) | ||
.anyMatch(nodeType -> nodeType.isNodeType(mixin)); | ||
} | ||
|
||
/** | ||
* Ensures a mixin is set on a node if it wasn't already | ||
* @param node the non-null node to add the mixin to | ||
* @param mixin the non-null mixin to add | ||
* @throws RepositoryException if an exception occurs while performing repository operations | ||
*/ | ||
public static void ensureMixin(@Nonnull Node node, @Nonnull String mixin) throws RepositoryException { | ||
if (!hasMixin(node, mixin) && node.canAddMixin(mixin)) { | ||
node.addMixin(mixin); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.