Skip to content

Commit e2af52b

Browse files
authored
Merge pull request #430 from wttech/fix-delete-items
fixed script, history delete
2 parents a306b14 + 77faf4b commit e2af52b

File tree

9 files changed

+228
-7
lines changed

9 files changed

+228
-7
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* ========================LICENSE_START=================================
3+
* AEM Permission Management
4+
* %%
5+
* Copyright (C) 2013 Wunderman Thompson Technology
6+
* %%
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* =========================LICENSE_END==================================
19+
*/
20+
21+
package com.cognifide.apm.core.endpoints;
22+
23+
import com.cognifide.apm.core.endpoints.params.RequestParameter;
24+
import javax.inject.Inject;
25+
import org.apache.sling.api.SlingHttpServletRequest;
26+
import org.apache.sling.models.annotations.Model;
27+
28+
@Model(adaptables = SlingHttpServletRequest.class)
29+
public class ScriptDeleteForm {
30+
31+
@Inject
32+
@RequestParameter("paths")
33+
private String[] paths;
34+
35+
public String[] getPaths() {
36+
return paths;
37+
}
38+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* ========================LICENSE_START=================================
3+
* AEM Permission Management
4+
* %%
5+
* Copyright (C) 2013 Wunderman Thompson Technology
6+
* %%
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* =========================LICENSE_END==================================
19+
*/
20+
package com.cognifide.apm.core.endpoints;
21+
22+
import com.cognifide.apm.core.Property;
23+
import com.cognifide.apm.core.endpoints.response.ResponseEntity;
24+
import com.cognifide.apm.core.endpoints.utils.RequestProcessor;
25+
import java.io.IOException;
26+
import javax.jcr.Session;
27+
import javax.servlet.Servlet;
28+
import org.apache.commons.lang3.StringUtils;
29+
import org.apache.sling.api.SlingHttpServletRequest;
30+
import org.apache.sling.api.SlingHttpServletResponse;
31+
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
32+
import org.apache.sling.models.factory.ModelFactory;
33+
import org.osgi.service.component.annotations.Component;
34+
import org.osgi.service.component.annotations.Reference;
35+
import org.slf4j.Logger;
36+
import org.slf4j.LoggerFactory;
37+
38+
@Component(
39+
service = Servlet.class,
40+
property = {
41+
Property.PATH + "/bin/apm/scripts/delete",
42+
Property.METHOD + "POST",
43+
Property.DESCRIPTION + "APM Script Delete Servlet",
44+
Property.VENDOR
45+
}
46+
)
47+
public class ScriptDeleteServlet extends SlingAllMethodsServlet {
48+
49+
private static final Logger LOGGER = LoggerFactory.getLogger(ScriptDeleteServlet.class);
50+
51+
@Reference
52+
private ModelFactory modelFactory;
53+
54+
@Override
55+
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException {
56+
RequestProcessor.process(modelFactory, ScriptDeleteForm.class, request, response, (form, resourceResolver) -> {
57+
try {
58+
Session session = resourceResolver.adaptTo(Session.class);
59+
for (String path : form.getPaths()) {
60+
if (session.nodeExists(path)) {
61+
session.removeItem(path);
62+
LOGGER.info("Item {} successfully deleted", path);
63+
}
64+
}
65+
session.save();
66+
return ResponseEntity.ok("Item(s) successfully deleted");
67+
} catch (Exception e) {
68+
LOGGER.error("Error while deleting item(s)", e);
69+
return ResponseEntity.badRequest(StringUtils.defaultString(e.getMessage(), "Errors while deleting item(s)"));
70+
}
71+
});
72+
}
73+
}

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,13 @@ private Object getValue(SlingHttpServletRequest request, Class<?> type, String p
8989
} else if (type.isEnum()) {
9090
return toEnum(type, parameterValue);
9191
} else if (type == String[].class) {
92-
return parameterValue.getString().split(",");
92+
if (parameterValue.getString().contains(",")) {
93+
return parameterValue.getString().split(",");
94+
} else {
95+
return Arrays.stream(request.getRequestParameters(parameterName))
96+
.map(org.apache.sling.api.request.RequestParameter::getString)
97+
.toArray(String[]::new);
98+
}
9399
}
94100
return parameterValue.getString();
95101
}

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,11 @@
2020

2121
package com.cognifide.apm.core.history;
2222

23+
import com.cognifide.apm.api.services.ExecutionMode;
24+
import java.util.Comparator;
2325
import javax.inject.Inject;
2426
import javax.inject.Named;
27+
import org.apache.commons.lang3.StringUtils;
2528
import org.apache.sling.api.resource.Resource;
2629
import org.apache.sling.models.annotations.DefaultInjectionStrategy;
2730
import org.apache.sling.models.annotations.Model;
@@ -72,20 +75,28 @@ public static ScriptHistoryImpl empty(String scriptPath) {
7275

7376
@Override
7477
public HistoryEntry getLastLocalRun() {
75-
lastLocalRun = getHistoryEntry(lastLocalRun, lastLocalRunPath);
78+
lastLocalRun = getHistoryEntry(lastLocalRun, lastLocalRunPath, ExecutionMode.RUN);
7679
return lastLocalRun;
7780
}
7881

7982
@Override
8083
public HistoryEntry getLastLocalDryRun() {
81-
lastLocalDryRun = getHistoryEntry(lastLocalDryRun, lastLocalDryRunPath);
84+
lastLocalDryRun = getHistoryEntry(lastLocalDryRun, lastLocalDryRunPath, ExecutionMode.DRY_RUN);
8285
return lastLocalDryRun;
8386
}
8487

85-
private HistoryEntry getHistoryEntry(HistoryEntry entry, String historyEntryPath) {
88+
private HistoryEntry getHistoryEntry(HistoryEntry entry, String historyEntryPath, ExecutionMode mode) {
8689
HistoryEntry historyEntry = entry;
8790
if (historyEntry == null && resource != null && historyEntryPath != null) {
8891
historyEntry = history.findHistoryEntry(resource.getResourceResolver(), historyEntryPath);
92+
if (historyEntry == null) {
93+
historyEntry = history.findAllHistoryEntries(resource.getResourceResolver())
94+
.stream()
95+
.filter(it -> StringUtils.equals(it.getScriptPath(), scriptPath)
96+
&& StringUtils.equals(it.getMode(), mode.toString()))
97+
.max(Comparator.comparing(HistoryEntry::getExecutionTime))
98+
.orElse(new HistoryEntryImpl());
99+
}
89100
}
90101
return historyEntry;
91102
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#base=js
22
apm-scripts.js
33
fileupload.js
4-
view-mode.js
4+
view-mode.js
5+
deleteitem.js
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*-
2+
* ========================LICENSE_START=================================
3+
* AEM Permission Management
4+
* %%
5+
* Copyright (C) 2013 - 2016 Wunderman Thompson Technology
6+
* %%
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* =========================LICENSE_END==================================
19+
*/
20+
(function (window, document, $) {
21+
function deletePages(collection, paths) {
22+
const ui = $(window).adaptTo('foundation-ui');
23+
ui.wait();
24+
$.ajax({
25+
url: '/bin/apm/scripts/delete',
26+
type: 'POST',
27+
data: {
28+
paths: paths
29+
},
30+
dataType: 'json',
31+
success: function () {
32+
ui.clearWait();
33+
const api = collection.adaptTo('foundation-collection');
34+
if (api && 'reload' in api) {
35+
api.reload();
36+
return
37+
}
38+
const contentApi = $('.foundation-content').adaptTo('foundation-content');
39+
if (contentApi) {
40+
contentApi.refresh();
41+
}
42+
},
43+
error: function () {
44+
ui.clearWait();
45+
ui.alert('Error', 'Errors while deleting item(s)', 'error');
46+
}
47+
});
48+
}
49+
50+
function createEl(name) {
51+
return $(document.createElement(name));
52+
}
53+
54+
$(window).adaptTo('foundation-registry').register('foundation.collection.action.action', {
55+
name: 'com.cognifide.apm.delete',
56+
handler: function (name, el, config, collection, selections) {
57+
const message = createEl('div');
58+
const intro = createEl('p').appendTo(message);
59+
if (selections.length === 1) {
60+
intro.text('You are going to delete the following item:');
61+
} else {
62+
intro.text('You are going to delete the following ' + selections.length + ' items:');
63+
}
64+
let list = [];
65+
const maxCount = Math.min(selections.length, 12);
66+
for (let i = 0; i < maxCount; i++) {
67+
const title = $(selections[i]).find('.foundation-collection-item-title').text();
68+
const time = $(selections[i]).find('.foundation-collection-item-time').text();
69+
list.push(createEl('b').text(time ? title + ' (' + time.trim() + ')' : title).prop('outerHTML'))
70+
}
71+
if (selections.length > maxCount) {
72+
list.push('\x26#8230;');
73+
}
74+
createEl('p').html(list.join('\x3cbr\x3e')).appendTo(message);
75+
const ui = $(window).adaptTo('foundation-ui');
76+
ui.prompt('Delete', message.html(), 'notice', [{
77+
text: 'Cancel'
78+
}, {
79+
text: 'Delete',
80+
warning: true,
81+
handler: function () {
82+
const paths = selections.map(function (value) {
83+
return $(value).data('foundationCollectionItemId');
84+
});
85+
deletePages($(collection), paths);
86+
}
87+
}]);
88+
}
89+
});
90+
})(window, document, Granite.$);

app/aem/ui.apps.base/src/main/content/jcr_root/apps/apm/components/historyRow/historyRow.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,12 @@
2727
</td>
2828
<td is="coral-table-cell" value="${item.scriptName}">
2929
<a href="${item.scriptContentPath}" download="${item.scriptName}"
30+
class="foundation-collection-item-title"
3031
style="text-decoration:none; color:#4b4b4b">${item.scriptName}</a>
3132
</td>
3233
<td is="coral-table-cell" value="${item.executionTime.timeInMillis || 0}">
3334
<time data-sly-test="${item.executionTime}" datetime="${item.executionTime.timeInMillis || 0}"
35+
class="foundation-collection-item-time"
3436
data-sly-use.execLast="${'com.adobe.cq.xf.ui.DateFormatter' @ date=item.executionTimeCalendar, simpleFormat='yyyy-MM-dd HH:mm:ss'}">
3537
${execLast.date}
3638
</time>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@
115115
granite:rel="cq-siteadmin-admin-actions-delete-activator"
116116
jcr:primaryType="nt:unstructured"
117117
sling:resourceType="granite/ui/components/coral/foundation/collection/action"
118-
action="cq.wcm.delete"
118+
action="com.cognifide.apm.delete"
119119
icon="delete"
120120
relScope="collection"
121121
target=".cq-experience-fragments-admin-childpages"

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@
262262
granite:rel="cq-siteadmin-admin-actions-delete-activator"
263263
jcr:primaryType="nt:unstructured"
264264
sling:resourceType="granite/ui/components/coral/foundation/collection/action"
265-
action="cq.wcm.delete"
265+
action="com.cognifide.apm.delete"
266266
icon="delete"
267267
relScope="collection"
268268
target=".cq-experience-fragments-admin-childpages"

0 commit comments

Comments
 (0)