Skip to content

Commit 7710efd

Browse files
committed
Merge branch 'master' into fix-move-issue
2 parents 1dee38a + a95e13c commit 7710efd

File tree

11 files changed

+250
-19
lines changed

11 files changed

+250
-19
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/ScriptUploadServlet.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
import org.apache.sling.models.factory.ModelFactory;
4040
import org.osgi.service.component.annotations.Component;
4141
import org.osgi.service.component.annotations.Reference;
42+
import org.slf4j.Logger;
43+
import org.slf4j.LoggerFactory;
4244

4345
@Component(
4446
service = Servlet.class,
@@ -51,6 +53,8 @@
5153
)
5254
public class ScriptUploadServlet extends SlingAllMethodsServlet {
5355

56+
private static final Logger LOGGER = LoggerFactory.getLogger(ScriptUploadServlet.class);
57+
5458
@Reference
5559
private ScriptStorage scriptStorage;
5660

@@ -66,12 +70,15 @@ protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse
6670
try {
6771
Script script = scriptStorage.save(form, resourceResolver);
6872
scriptManager.process(script, ExecutionMode.VALIDATION, resourceResolver);
69-
return ResponseEntity.ok("File successfully saved")
73+
LOGGER.info("Script {} successfully saved", script.getPath());
74+
return ResponseEntity.ok("Script successfully saved")
7075
.addEntry("uploadedScript", new ScriptDto(script));
7176
} catch (ScriptStorageException e) {
77+
LOGGER.error("Errors while saving script", e);
7278
return ResponseEntity.badRequest(StringUtils.defaultString(e.getMessage(), "Errors while saving script"))
7379
.addEntry("errors", e.getErrors());
7480
} catch (PersistenceException | RepositoryException e) {
81+
LOGGER.error("Errors while saving script", e);
7582
throw new RuntimeException(e);
7683
}
7784
});

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/clientlibs/views/scripts/js/fileupload.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717
* limitations under the License.
1818
* =========================LICENSE_END==================================
1919
*/
20-
(function(window, $, Coral) {
20+
(function (window, $, Coral) {
2121
'use strict';
2222

2323
let dragCounter = 0,
24-
fileUploader;
24+
fileUploader ;
2525

2626
function init() {
2727
fileUploader = new Coral.FileUpload();
@@ -42,9 +42,9 @@
4242
];
4343
fileUploader.upload(filename);
4444
})
45-
.on('coral-fileupload:load', function(event) {
45+
.on('coral-fileupload:load', function (event) {
4646
let savePath = window.location.pathname.split('.html')[1];
47-
fileUploader.uploadQueue.forEach(function(item, index) {
47+
fileUploader.uploadQueue.forEach(function (item, index) {
4848
let filename = event.detail.item.file.name;
4949
if (item.file.name === filename) {
5050
item._parameters = [
@@ -57,23 +57,26 @@
5757
_reload();
5858
}
5959
});
60+
fileUploader._getTargetChangeInput().addEventListener('change', function (event) {
61+
fileUploader._onInputChange(event);
62+
});
6063

6164
const coralShell = $('coral-shell-content').get(0);
62-
coralShell.addEventListener('drop', function(event) {
65+
coralShell.addEventListener('drop', function (event) {
6366
dragCounter = 0;
6467
event.preventDefault();
6568
_dropZoneDrop();
6669
fileUploader._onInputChange(event);
6770
}, false);
68-
coralShell.addEventListener('dragenter', function(event) {
71+
coralShell.addEventListener('dragenter', function (event) {
6972
event.preventDefault();
7073
dragCounter++;
7174
_dropZoneDragEnter();
7275
}, false);
73-
coralShell.addEventListener('dragover', function(event) {
76+
coralShell.addEventListener('dragover', function (event) {
7477
event.preventDefault();
7578
}, false);
76-
coralShell.addEventListener('dragleave', function() {
79+
coralShell.addEventListener('dragleave', function () {
7780
dragCounter--;
7881
if (dragCounter === 0) {
7982
_dropZoneDragLeave();
@@ -87,21 +90,21 @@
8790

8891
function _dropZoneDragEnter() {
8992
let message = Granite.I18n.get('Drag and drop to upload'),
90-
dragAndDropMessage = $('<div class=\"drag-drop-message\" style="text-align: center;"><h1 > <span>{</span>' + message + '<span>}</span></h1></div>');
93+
dragAndDropMessage = $('<div class=\"drag-drop-message\" style="text-align: center;"><h1 > <span>{</span>' + message + '<span>}</span></h1></div>');
9194
$('.foundation-collection').overlayMask('show', dragAndDropMessage);
9295
}
9396

9497
function _dropZoneDragLeave() {
9598
$('.foundation-collection').overlayMask('hide');
9699
}
97100

98-
function _dropZoneDrop () {
101+
function _dropZoneDrop() {
99102
$('.foundation-collection').overlayMask('hide');
100103
}
101104

102105
$(window).adaptTo('foundation-registry').register('foundation.collection.action.action', {
103106
name: 'scripts.upload',
104-
handler: function() {
107+
handler: function () {
105108
fileUploader._showFileDialog();
106109
}
107110
});

0 commit comments

Comments
 (0)