Skip to content

Commit 0653f43

Browse files
authored
Merge pull request #425 from wttech/fix-apm-editor
fixed script editor
2 parents ad22830 + f3d29c5 commit 0653f43

File tree

13 files changed

+42
-132
lines changed

13 files changed

+42
-132
lines changed

app/aem/core/src/main/java/com/cognifide/apm/core/endpoints/ScriptUploadForm.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,12 @@
2222

2323
import com.cognifide.apm.api.scripts.LaunchEnvironment;
2424
import com.cognifide.apm.api.scripts.LaunchMode;
25-
import com.cognifide.apm.core.endpoints.params.DateFormat;
2625
import com.cognifide.apm.core.endpoints.params.FileName;
2726
import com.cognifide.apm.core.endpoints.params.RequestParameter;
2827
import com.cognifide.apm.core.scripts.LaunchMetadata;
2928
import com.cognifide.apm.core.scripts.ScriptNode;
3029
import java.io.InputStream;
31-
import java.time.LocalDateTime;
30+
import java.time.OffsetDateTime;
3231
import javax.inject.Inject;
3332
import org.apache.sling.api.SlingHttpServletRequest;
3433
import org.apache.sling.models.annotations.Model;
@@ -75,8 +74,7 @@ public class ScriptUploadForm {
7574

7675
@Inject
7776
@RequestParameter(ScriptNode.APM_LAUNCH_SCHEDULE)
78-
@DateFormat("yyyy-MM-dd'T'HH:mm:ss")
79-
private LocalDateTime launchSchedule;
77+
private OffsetDateTime launchSchedule;
8078

8179
@Inject
8280
@RequestParameter(ScriptNode.APM_LAUNCH_CRON_EXPRESSION)

app/aem/core/src/main/java/com/cognifide/apm/core/endpoints/params/DateFormat.java

Lines changed: 0 additions & 33 deletions
This file was deleted.

app/aem/core/src/main/java/com/cognifide/apm/core/endpoints/params/RequestParameterInjector.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import java.lang.reflect.AnnotatedElement;
2727
import java.lang.reflect.ParameterizedType;
2828
import java.lang.reflect.Type;
29-
import java.time.LocalDateTime;
29+
import java.time.OffsetDateTime;
3030
import java.time.format.DateTimeFormatter;
3131
import java.util.Arrays;
3232
import java.util.Map;
@@ -84,8 +84,8 @@ private Object getValue(SlingHttpServletRequest request, Class<?> type, String p
8484
return BooleanUtils.toBoolean(parameterValue.getString());
8585
} else if (type == InputStream.class) {
8686
return toInputStream(parameterValue);
87-
} else if (type == LocalDateTime.class) {
88-
return toLocalDateTime(annotatedElement, parameterValue);
87+
} else if (type == OffsetDateTime.class) {
88+
return toOffsetDateTime(parameterValue);
8989
} else if (type.isEnum()) {
9090
return toEnum(type, parameterValue);
9191
} else if (type == String[].class) {
@@ -120,11 +120,8 @@ private Map<String, String> extractParams(SlingHttpServletRequest request, Strin
120120
));
121121
}
122122

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

130127
@Override

app/aem/core/src/main/java/com/cognifide/apm/core/history/HistoryEntryWriter.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,13 @@ public void writeTo(Resource historyLogResource) throws IOException {
7373
valueMap.put(HistoryEntryImpl.SCRIPT_PATH, filePath);
7474
valueMap.put(HistoryEntryImpl.AUTHOR, author);
7575
valueMap.put(HistoryEntryImpl.MODE, mode);
76-
try (InputStream progressLogInput = IOUtils.toInputStream(progressLog, StandardCharsets.UTF_8)) {
77-
valueMap.put(HistoryEntryImpl.PROGRESS_LOG, progressLogInput);
76+
int logWarnStringSizeThreshold = Integer.getInteger("oak.repository.node.property.logWarnStringSizeThreshold", 102400);
77+
if (progressLog.length() > logWarnStringSizeThreshold) {
78+
try (InputStream progressLogInput = IOUtils.toInputStream(progressLog, StandardCharsets.UTF_8)) {
79+
valueMap.put(HistoryEntryImpl.PROGRESS_LOG, progressLogInput);
80+
}
81+
} else {
82+
valueMap.put(HistoryEntryImpl.PROGRESS_LOG, progressLog);
7883
}
7984
valueMap.put(HistoryEntryImpl.IS_RUN_SUCCESSFUL, isRunSuccessful);
8085
valueMap.put(HistoryEntryImpl.EXECUTION_TIME, executionTime);

app/aem/core/src/main/java/com/cognifide/apm/core/scripts/LaunchMetadata.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
import com.cognifide.apm.api.scripts.LaunchEnvironment;
2424
import com.cognifide.apm.api.scripts.LaunchMode;
25-
import java.time.LocalDateTime;
25+
import java.time.OffsetDateTime;
2626

2727
public class LaunchMetadata {
2828

@@ -36,12 +36,12 @@ public class LaunchMetadata {
3636

3737
private final String launchHook;
3838

39-
private final LocalDateTime launchSchedule;
39+
private final OffsetDateTime launchSchedule;
4040

4141
private final String launchCronExpression;
4242

4343
public LaunchMetadata(boolean launchEnabled, LaunchMode launchMode, LaunchEnvironment launchEnvironment,
44-
String[] launchRunModes, String launchHook, LocalDateTime launchSchedule, String launchCronExpression) {
44+
String[] launchRunModes, String launchHook, OffsetDateTime launchSchedule, String launchCronExpression) {
4545
this.launchEnabled = launchEnabled;
4646
this.launchMode = launchMode;
4747
this.launchEnvironment = launchEnvironment;
@@ -71,7 +71,7 @@ public String getLaunchHook() {
7171
return launchHook;
7272
}
7373

74-
public LocalDateTime getLaunchSchedule() {
74+
public OffsetDateTime getLaunchSchedule() {
7575
return launchSchedule;
7676
}
7777

app/aem/core/src/main/java/com/cognifide/apm/core/scripts/ScriptStorageImpl.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,12 @@
2929
import com.day.cq.commons.jcr.JcrConstants;
3030
import java.nio.charset.Charset;
3131
import java.nio.charset.StandardCharsets;
32-
import java.time.LocalDateTime;
32+
import java.time.OffsetDateTime;
3333
import java.util.ArrayList;
3434
import java.util.Calendar;
3535
import java.util.Collection;
3636
import java.util.Collections;
37+
import java.util.Date;
3738
import java.util.List;
3839
import java.util.regex.Pattern;
3940
import java.util.stream.Collectors;
@@ -128,12 +129,11 @@ private Script saveScript(FileDescriptor descriptor, LaunchMetadata launchMetada
128129
private void setOrRemoveProperty(Node node, String name, Object value) throws RepositoryException {
129130
if (value == null) {
130131
removeProperty(node, name);
131-
} else if (value instanceof LocalDateTime) {
132-
LocalDateTime localDateTime = (LocalDateTime) value;
132+
} else if (value instanceof OffsetDateTime) {
133+
OffsetDateTime offsetDateTime = (OffsetDateTime) value;
134+
Date date = Date.from(offsetDateTime.toInstant());
133135
Calendar calendar = Calendar.getInstance();
134-
calendar.clear();
135-
calendar.set(localDateTime.getYear(), localDateTime.getMonthValue() - 1, localDateTime.getDayOfMonth(),
136-
localDateTime.getHour(), localDateTime.getMinute(), localDateTime.getSecond());
136+
calendar.setTime(date);
137137
node.setProperty(name, calendar);
138138
} else if (value instanceof String[]) {
139139
node.setProperty(name, (String[]) value);

app/aem/core/src/main/java/com/cognifide/apm/core/services/ScriptsResourceChangeListener.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ public class ScriptsResourceChangeListener implements ResourceChangeListener {
7878
@Activate
7979
public void activate(BundleContext bundleContext) {
8080
registeredScripts = new HashMap<>();
81-
8281
SlingHelper.operateTraced(resolverProvider, resolver ->
8382
scriptFinder.findAll(onScheduleOrCronExpression(runModesProvider), resolver)
8483
.forEach(script -> registerScript(script, bundleContext))
@@ -115,10 +114,16 @@ public void onChange(List<ResourceChange> changes) {
115114
} else if (change.getType() == ResourceChange.ChangeType.CHANGED) {
116115
Script script = scriptFinder.find(change.getPath(), resolver);
117116
RegisterScript registeredScript = registeredScripts.get(change.getPath());
118-
if (onScheduleOrCronExpression(runModesProvider).test(script) && !Objects.equals(script, registeredScript.script)) {
117+
if (registeredScript == null) {
118+
if (onScheduleOrCronExpression(runModesProvider).test(script)) {
119+
registerScript(script, bundleContext);
120+
}
121+
} else if (!Objects.equals(script, registeredScript.script)) {
119122
registeredScript.registration.unregister();
120123
registeredScripts.remove(change.getPath());
121-
registerScript(script, bundleContext);
124+
if (onScheduleOrCronExpression(runModesProvider).test(script)) {
125+
registerScript(script, bundleContext);
126+
}
122127
}
123128
}
124129
})
@@ -130,7 +135,8 @@ private void registerScript(Script script, BundleContext bundleContext) {
130135
Dictionary<String, Object> dictionary = new Hashtable<>();
131136
if (script.getLaunchMode() == LaunchMode.ON_SCHEDULE) {
132137
SimpleDateFormat cronExpressionFormat = new SimpleDateFormat("s m H d M ? y");
133-
dictionary.put("scheduler.expression", cronExpressionFormat.format(script.getLaunchSchedule()));
138+
String cronExpression = cronExpressionFormat.format(script.getLaunchSchedule());
139+
dictionary.put("scheduler.expression", cronExpression);
134140
} else if (script.getLaunchMode() == LaunchMode.ON_CRON_EXPRESSION) {
135141
dictionary.put("scheduler.expression", script.getLaunchCronExpression());
136142
}

app/aem/ui.apps.base/src/main/content/jcr_root/apps/apm/clientlibs/views/editor/js/apm-editor.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
$(document).on('cui-contentloaded', function () {
3636

3737
const fieldNames = [
38-
'apm:launchEnabled',
3938
'apm:launchMode',
4039
'apm:launchEnvironment',
4140
'apm:launchRunModes',
@@ -101,9 +100,9 @@
101100
const formData = new FormData();
102101
$.each(fieldNames, (index, fieldName) => {
103102
const originalValue = fieldName === 'apm:launchRunModes'
104-
? originalFormData.getAll(fieldName)
105-
: originalFormData.get(fieldName);
106-
if (originalValue) {
103+
? originalFormData.getAll(fieldName).filter((item) => item.trim().length)
104+
: (originalFormData.get(fieldName) || '').trim();
105+
if (originalValue.length) {
107106
formData.set(fieldName, originalValue);
108107
}
109108
});
@@ -182,7 +181,7 @@
182181
let message = response.message;
183182
if (response.errors) {
184183
message += '<ul>';
185-
response.errors.forEach((error) => message += '<li>' + error);
184+
response.errors.forEach((error) => message += '<li>' + error + '</li>');
186185
message += '</ul>';
187186
}
188187
self.uiHelper.notify('error', message, 'error');

app/aem/ui.apps.base/src/main/content/jcr_root/apps/apm/clientlibs/views/scripts/.content.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
xmlns:jcr="http://www.jcp.org/jcr/1.0"
44
jcr:primaryType="cq:ClientLibraryFolder"
55
sling:resourceType="widgets/clientlib"
6-
categories="[apm.scripts]"/>
6+
categories="[apm-scripts]"/>

app/aem/ui.apps.base/src/main/content/jcr_root/apps/apm/components/scriptLaunch/.content.xml

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)