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

Fix apm editor #425

Merged
merged 4 commits 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 @@ -22,13 +22,12 @@

import com.cognifide.apm.api.scripts.LaunchEnvironment;
import com.cognifide.apm.api.scripts.LaunchMode;
import com.cognifide.apm.core.endpoints.params.DateFormat;
import com.cognifide.apm.core.endpoints.params.FileName;
import com.cognifide.apm.core.endpoints.params.RequestParameter;
import com.cognifide.apm.core.scripts.LaunchMetadata;
import com.cognifide.apm.core.scripts.ScriptNode;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import javax.inject.Inject;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.models.annotations.Model;
Expand Down Expand Up @@ -75,8 +74,7 @@ public class ScriptUploadForm {

@Inject
@RequestParameter(ScriptNode.APM_LAUNCH_SCHEDULE)
@DateFormat("yyyy-MM-dd'T'HH:mm:ss")
private LocalDateTime launchSchedule;
private OffsetDateTime launchSchedule;

@Inject
@RequestParameter(ScriptNode.APM_LAUNCH_CRON_EXPRESSION)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.Map;
Expand Down Expand Up @@ -84,8 +84,8 @@ private Object getValue(SlingHttpServletRequest request, Class<?> type, String p
return BooleanUtils.toBoolean(parameterValue.getString());
} else if (type == InputStream.class) {
return toInputStream(parameterValue);
} else if (type == LocalDateTime.class) {
return toLocalDateTime(annotatedElement, parameterValue);
} else if (type == OffsetDateTime.class) {
return toOffsetDateTime(parameterValue);
} else if (type.isEnum()) {
return toEnum(type, parameterValue);
} else if (type == String[].class) {
Expand Down Expand Up @@ -120,11 +120,8 @@ private Map<String, String> extractParams(SlingHttpServletRequest request, Strin
));
}

private LocalDateTime toLocalDateTime(AnnotatedElement annotatedElement, org.apache.sling.api.request.RequestParameter parameterValue) {
String dateFormat = Optional.ofNullable(annotatedElement.getAnnotation(DateFormat.class))
.map(DateFormat::value)
.orElse(DateTimeFormatter.ISO_LOCAL_DATE_TIME.toString());
return LocalDateTime.parse(parameterValue.getString(), DateTimeFormatter.ofPattern(dateFormat));
private OffsetDateTime toOffsetDateTime(org.apache.sling.api.request.RequestParameter parameterValue) {
return OffsetDateTime.parse(parameterValue.getString(), DateTimeFormatter.ISO_OFFSET_DATE_TIME);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,13 @@ public void writeTo(Resource historyLogResource) throws IOException {
valueMap.put(HistoryEntryImpl.SCRIPT_PATH, filePath);
valueMap.put(HistoryEntryImpl.AUTHOR, author);
valueMap.put(HistoryEntryImpl.MODE, mode);
try (InputStream progressLogInput = IOUtils.toInputStream(progressLog, StandardCharsets.UTF_8)) {
valueMap.put(HistoryEntryImpl.PROGRESS_LOG, progressLogInput);
int logWarnStringSizeThreshold = Integer.getInteger("oak.repository.node.property.logWarnStringSizeThreshold", 102400);
if (progressLog.length() > logWarnStringSizeThreshold) {
try (InputStream progressLogInput = IOUtils.toInputStream(progressLog, StandardCharsets.UTF_8)) {
valueMap.put(HistoryEntryImpl.PROGRESS_LOG, progressLogInput);
}
} else {
valueMap.put(HistoryEntryImpl.PROGRESS_LOG, progressLog);
}
valueMap.put(HistoryEntryImpl.IS_RUN_SUCCESSFUL, isRunSuccessful);
valueMap.put(HistoryEntryImpl.EXECUTION_TIME, executionTime);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

import com.cognifide.apm.api.scripts.LaunchEnvironment;
import com.cognifide.apm.api.scripts.LaunchMode;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;

public class LaunchMetadata {

Expand All @@ -36,12 +36,12 @@ public class LaunchMetadata {

private final String launchHook;

private final LocalDateTime launchSchedule;
private final OffsetDateTime launchSchedule;

private final String launchCronExpression;

public LaunchMetadata(boolean launchEnabled, LaunchMode launchMode, LaunchEnvironment launchEnvironment,
String[] launchRunModes, String launchHook, LocalDateTime launchSchedule, String launchCronExpression) {
String[] launchRunModes, String launchHook, OffsetDateTime launchSchedule, String launchCronExpression) {
this.launchEnabled = launchEnabled;
this.launchMode = launchMode;
this.launchEnvironment = launchEnvironment;
Expand Down Expand Up @@ -71,7 +71,7 @@ public String getLaunchHook() {
return launchHook;
}

public LocalDateTime getLaunchSchedule() {
public OffsetDateTime getLaunchSchedule() {
return launchSchedule;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@
import com.day.cq.commons.jcr.JcrConstants;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -128,12 +129,11 @@ private Script saveScript(FileDescriptor descriptor, LaunchMetadata launchMetada
private void setOrRemoveProperty(Node node, String name, Object value) throws RepositoryException {
if (value == null) {
removeProperty(node, name);
} else if (value instanceof LocalDateTime) {
LocalDateTime localDateTime = (LocalDateTime) value;
} else if (value instanceof OffsetDateTime) {
OffsetDateTime offsetDateTime = (OffsetDateTime) value;
Date date = Date.from(offsetDateTime.toInstant());
Calendar calendar = Calendar.getInstance();
calendar.clear();
calendar.set(localDateTime.getYear(), localDateTime.getMonthValue() - 1, localDateTime.getDayOfMonth(),
localDateTime.getHour(), localDateTime.getMinute(), localDateTime.getSecond());
calendar.setTime(date);
node.setProperty(name, calendar);
} else if (value instanceof String[]) {
node.setProperty(name, (String[]) value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ public class ScriptsResourceChangeListener implements ResourceChangeListener {
@Activate
public void activate(BundleContext bundleContext) {
registeredScripts = new HashMap<>();

SlingHelper.operateTraced(resolverProvider, resolver ->
scriptFinder.findAll(onScheduleOrCronExpression(runModesProvider), resolver)
.forEach(script -> registerScript(script, bundleContext))
Expand Down Expand Up @@ -115,10 +114,16 @@ public void onChange(List<ResourceChange> changes) {
} else if (change.getType() == ResourceChange.ChangeType.CHANGED) {
Script script = scriptFinder.find(change.getPath(), resolver);
RegisterScript registeredScript = registeredScripts.get(change.getPath());
if (onScheduleOrCronExpression(runModesProvider).test(script) && !Objects.equals(script, registeredScript.script)) {
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());
registerScript(script, bundleContext);
if (onScheduleOrCronExpression(runModesProvider).test(script)) {
registerScript(script, bundleContext);
}
}
}
})
Expand All @@ -130,7 +135,8 @@ private void registerScript(Script script, BundleContext bundleContext) {
Dictionary<String, Object> dictionary = new Hashtable<>();
if (script.getLaunchMode() == LaunchMode.ON_SCHEDULE) {
SimpleDateFormat cronExpressionFormat = new SimpleDateFormat("s m H d M ? y");
dictionary.put("scheduler.expression", cronExpressionFormat.format(script.getLaunchSchedule()));
String cronExpression = cronExpressionFormat.format(script.getLaunchSchedule());
dictionary.put("scheduler.expression", cronExpression);
} else if (script.getLaunchMode() == LaunchMode.ON_CRON_EXPRESSION) {
dictionary.put("scheduler.expression", script.getLaunchCronExpression());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
$(document).on('cui-contentloaded', function () {

const fieldNames = [
'apm:launchEnabled',
'apm:launchMode',
'apm:launchEnvironment',
'apm:launchRunModes',
Expand Down Expand Up @@ -101,9 +100,9 @@
const formData = new FormData();
$.each(fieldNames, (index, fieldName) => {
const originalValue = fieldName === 'apm:launchRunModes'
? originalFormData.getAll(fieldName)
: originalFormData.get(fieldName);
if (originalValue) {
? originalFormData.getAll(fieldName).filter((item) => item.trim().length)
: (originalFormData.get(fieldName) || '').trim();
if (originalValue.length) {
formData.set(fieldName, originalValue);
}
});
Expand Down Expand Up @@ -182,7 +181,7 @@
let message = response.message;
if (response.errors) {
message += '<ul>';
response.errors.forEach((error) => message += '<li>' + error);
response.errors.forEach((error) => message += '<li>' + error + '</li>');
message += '</ul>';
}
self.uiHelper.notify('error', message, 'error');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
xmlns:jcr="http://www.jcp.org/jcr/1.0"
jcr:primaryType="cq:ClientLibraryFolder"
sling:resourceType="widgets/clientlib"
categories="[apm.scripts]"/>
categories="[apm-scripts]"/>

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@
fieldLabel="Schedule"
name="apm:launchSchedule"
type="datetime"
valueFormat="yyyy-MM-dd[T]HH:mm:ss"/>
displayedFormat="YYYY-MM-DD HH:mm"
valueFormat="YYYY-MM-DD[T]HH:mm:ssZ"/>
<cronExpression
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/form/textfield"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<clientlibs
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/includeclientlibs"
categories="[apm.scripts,cq.common.wcm,cq.sites.collectionpage,cq.siteadmin.admin.page.row,cq.experiencefragments.components.experiencefragment,cq.siteadmin.common]"/>
categories="[apm-scripts,cq.common.wcm,cq.sites.collectionpage,cq.siteadmin.admin.page.row,cq.experiencefragments.components.experiencefragment,cq.siteadmin.common]"/>
</head>
<views jcr:primaryType="nt:unstructured">
<content
Expand Down
Loading