Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed rename/move/copy/paste script issue #426

Merged
merged 1 commit into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,7 @@ public Progress process(Script script, ExecutionMode mode, Map<String, String> c
}

updateScriptProperties(script, mode, progress.isSuccess());
if (progress.isSuccess()) {
versionService.updateVersionIfNeeded(resolver, script);
}
versionService.updateVersionIfNeeded(resolver, script);
saveHistory(script, mode, progress);
eventManager.trigger(new ScriptExecutedEvent(script, mode, progress.isSuccess()));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
import com.cognifide.apm.core.Apm;
import com.cognifide.apm.core.Property;
import com.cognifide.apm.core.launchers.ApmInstallService;
import com.cognifide.apm.core.ui.models.ScriptsRowModel;
import com.cognifide.apm.core.utils.sling.SlingHelper;
import com.day.cq.commons.jcr.JcrConstants;
import java.text.SimpleDateFormat;
import java.util.Dictionary;
import java.util.HashMap;
Expand All @@ -38,6 +40,8 @@
import java.util.Map;
import java.util.Objects;
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.resource.ModifiableValueMap;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.resource.observation.ResourceChange;
import org.apache.sling.api.resource.observation.ResourceChangeListener;
import org.osgi.framework.Bundle;
Expand Down Expand Up @@ -96,38 +100,48 @@ public void onChange(List<ResourceChange> changes) {
Bundle currentBundle = FrameworkUtil.getBundle(ScriptsResourceChangeListener.class);
BundleContext bundleContext = currentBundle.getBundleContext();

SlingHelper.operateTraced(resolverProvider, resolver ->
changes.stream()
.filter(change -> StringUtils.endsWith(change.getPath(), Apm.FILE_EXT))
.forEach(change -> {
if (change.getType() == ResourceChange.ChangeType.ADDED) {
Script script = scriptFinder.find(change.getPath(), resolver);
SlingHelper.operateTraced(resolverProvider, resolver -> {
// rename copy/paste folders
changes.stream()
.filter(change -> change.getType() == ResourceChange.ChangeType.ADDED)
.map(change -> resolver.getResource(change.getPath()))
.filter(ScriptsRowModel::isFolder)
.forEach(resource -> {
ValueMap valueMap = resource.adaptTo(ModifiableValueMap.class);
valueMap.put(JcrConstants.JCR_TITLE, resource.getName());
});
//register schedule or cron expression scripts
changes.stream()
.filter(change -> StringUtils.endsWith(change.getPath(), Apm.FILE_EXT))
.forEach(change -> {
if (change.getType() == ResourceChange.ChangeType.ADDED) {
Script script = scriptFinder.find(change.getPath(), resolver);
if (onScheduleOrCronExpression(runModesProvider).test(script)) {
registerScript(script, bundleContext);
}
} else if (change.getType() == ResourceChange.ChangeType.REMOVED) {
RegisterScript registeredScript = registeredScripts.get(change.getPath());
if (registeredScript != null) {
registeredScript.registration.unregister();
registeredScripts.remove(change.getPath());
}
} else if (change.getType() == ResourceChange.ChangeType.CHANGED) {
Script script = scriptFinder.find(change.getPath(), resolver);
RegisterScript registeredScript = registeredScripts.get(change.getPath());
if (registeredScript == null) {
if (onScheduleOrCronExpression(runModesProvider).test(script)) {
registerScript(script, bundleContext);
}
} else if (change.getType() == ResourceChange.ChangeType.REMOVED) {
RegisterScript registeredScript = registeredScripts.get(change.getPath());
if (registeredScript != null) {
registeredScript.registration.unregister();
registeredScripts.remove(change.getPath());
}
} else if (change.getType() == ResourceChange.ChangeType.CHANGED) {
Script script = scriptFinder.find(change.getPath(), resolver);
RegisterScript registeredScript = registeredScripts.get(change.getPath());
if (registeredScript == null) {
if (onScheduleOrCronExpression(runModesProvider).test(script)) {
registerScript(script, bundleContext);
}
} else if (!Objects.equals(script, registeredScript.script)) {
registeredScript.registration.unregister();
registeredScripts.remove(change.getPath());
if (onScheduleOrCronExpression(runModesProvider).test(script)) {
registerScript(script, bundleContext);
}
} else if (!Objects.equals(script, registeredScript.script)) {
registeredScript.registration.unregister();
registeredScripts.remove(change.getPath());
if (onScheduleOrCronExpression(runModesProvider).test(script)) {
registerScript(script, bundleContext);
}
}
})
);
}
});
});
}

private void registerScript(Script script, BundleContext bundleContext) {
Expand Down
Loading