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

[ELI_46] Delete report button. #55

Merged
merged 9 commits into from
Sep 4, 2024
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 @@ -10,4 +10,6 @@ public interface GridResourcesCache {
CopyOnWriteArrayList<GridResource> getGridResourcesList();

void setGridResourcesList(List<GridResource> gridResources);

void clearCache();
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ public synchronized void setGridResourcesList(List<GridResource> gridResources)
gridResourcesCache.asMap().put(BROKEN_LINKS_MAP_KEY, new CopyOnWriteArrayList<>(gridResources));
}

@Override
public void clearCache() {
gridResourcesCache.invalidateAll();
}

@SuppressWarnings("UnstableApiUsage")
@Override
public CopyOnWriteArrayList<GridResource> getGridResourcesList() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import com.exadel.etoolbox.linkinspector.core.services.data.models.DataFilter;
import com.exadel.etoolbox.linkinspector.core.services.data.models.GridResource;
import com.exadel.etoolbox.linkinspector.core.services.exceptions.DataFeedException;
import org.apache.sling.api.resource.Resource;

import java.util.List;
Expand Down Expand Up @@ -59,4 +60,9 @@ public interface DataFeedService {
* @param valuesMap - {@code Map<String, String>} property location as key and new url as value
*/
void modifyDataFeed(Map<String, String> valuesMap);

/**
* Method for deleting data in data feed
*/
void deleteDataFeed() throws DataFeedException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
package com.exadel.etoolbox.linkinspector.core.services.data.impl;

import com.adobe.granite.ui.components.ds.ValueMapResource;
import com.exadel.etoolbox.linkinspector.api.Link;
import com.exadel.etoolbox.linkinspector.core.models.ui.GridViewItem;
import com.exadel.etoolbox.linkinspector.core.services.cache.GridResourcesCache;
import com.exadel.etoolbox.linkinspector.core.services.data.DataFeedService;
import com.exadel.etoolbox.linkinspector.core.services.data.GridResourcesGenerator;
import com.exadel.etoolbox.linkinspector.core.services.data.models.DataFilter;
import com.exadel.etoolbox.linkinspector.core.services.exceptions.DataFeedException;
import com.exadel.etoolbox.linkinspector.core.services.helpers.LinkHelper;
import com.exadel.etoolbox.linkinspector.core.services.util.CsvUtil;
import com.exadel.etoolbox.linkinspector.core.services.helpers.RepositoryHelper;
Expand Down Expand Up @@ -183,6 +183,20 @@ public void modifyDataFeed(Map<String, String> valuesMap) {
}
}

@Override
public void deleteDataFeed() throws DataFeedException {
try (ResourceResolver serviceResourceResolver = repositoryHelper.getServiceResourceResolver()) {
removePreviousDataFeed(serviceResourceResolver);
removeCsvReport(serviceResourceResolver);
removePendingNode(serviceResourceResolver);
gridResourcesCache.clearCache();
serviceResourceResolver.commit();
} catch (PersistenceException e) {
LOG.error("Failed to delete data feed", e);
throw new DataFeedException("Exception Deleting Data Feed");
}
}

private List<GridResource> dataFeedToGridResources(ResourceResolver resourceResolver) {
List<GridResource> gridResources = new ArrayList<>();
JSONArray jsonArray = JsonUtil.getJsonArrayFromFile(JSON_FEED_PATH, resourceResolver);
Expand Down Expand Up @@ -218,6 +232,10 @@ private void removePreviousDataFeed(ResourceResolver resourceResolver) {
LinkInspectorResourceUtil.removeResource(JSON_FEED_PATH, resourceResolver);
}

private void removeCsvReport(ResourceResolver resourceResolver) {
LinkInspectorResourceUtil.removeResource(CSV_REPORT_PATH, resourceResolver);
}

private void saveGridResourcesToJcr(ResourceResolver resourceResolver, JSONArray jsonArray) {
LinkInspectorResourceUtil.saveFileToJCR(
JSON_FEED_PATH,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.exadel.etoolbox.linkinspector.core.services.exceptions;

public class DataFeedException extends Exception {
public DataFeedException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.exadel.etoolbox.linkinspector.core.servlets;

import com.exadel.etoolbox.linkinspector.core.services.data.DataFeedService;
import com.exadel.etoolbox.linkinspector.core.services.exceptions.DataFeedException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.HttpConstants;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.Servlet;

@Component(service = {Servlet.class},
property = {
"sling.servlet.methods=" + HttpConstants.METHOD_DELETE,
"sling.servlet.paths=" + "/bin/etoolbox/link-inspector/delete-report"
}
)
public class DeleteReportServlet extends SlingAllMethodsServlet {

private static final Logger LOG = LoggerFactory.getLogger(DeleteReportServlet.class);

@Reference
private DataFeedService dataFeedService;

@Override
protected void doDelete(SlingHttpServletRequest request, SlingHttpServletResponse response) {
try {
dataFeedService.deleteDataFeed();
response.setStatus(HttpStatus.SC_OK);
} catch (DataFeedException e) {
LOG.error(e.getMessage());
response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,9 @@ public List<GridResource> dataFeedToGridResources() {
public void modifyDataFeed(Map<String, String> valuesMap) {
// No operation
}

@Override
public void deleteDataFeed() {
// No operation
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ tr.elc-card {
display: none;
}

.foundation-collection-item-link-type{
.collection-item_link-type{
width:30%;
}

Expand All @@ -131,6 +131,10 @@ tr.elc-card {
width:30%
}

.collection-item_status_code {
text-align: center;
}

#elc-filter-options.elc-filter-active, #elc-filter-options.elc-filter-active:hover {
background-color: #d3d3d3;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ console-ui.conf.js
console-ui.replace.js
console-ui.filter.js
console-ui.pagination.js
console-ui.run.js
console-ui.run.js
console-ui.delete.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* EToolbox Link Inspector clientlib.
* "Delete Report" action definition.
*/
(function (window, document, $, ELC, Granite, Coral) {
'use strict';
const DIALOG_TITLE_LABEL = Granite.I18n.get('Delete Report');
const CANCEL_LABEL = Granite.I18n.get('Cancel');
const SUBMIT_FILTER_LABEL = Granite.I18n.get('Delete');
const ERROR_MSG = Granite.I18n.get('Something went wrong');

function onFilterAction(name, el, config, collection, selections) {
const dialog = document.querySelector('#delete-dialog');
dialog.show();
}

function initDeleteDialog(){
const dialog = new Coral.Dialog().set({
id : 'delete-dialog',
closable: Coral.Dialog.closable.ON,
backdrop: Coral.Dialog.backdrop.STATIC,
interaction: 'off',
header :{
innerHTML : DIALOG_TITLE_LABEL
}
});

$('<p>').html('Are you sure you want to delete the report?').appendTo(dialog.content);

const $cancelBtn = $('<button is="coral-button" variant="default" coral-close>').text(CANCEL_LABEL);
const $updateBtn =
$('<button data-dialog-action is="coral-button" variant="primary" coral-close>').text(SUBMIT_FILTER_LABEL);

$cancelBtn.appendTo(dialog.footer);
$updateBtn.appendTo(dialog.footer);

function onDeleteDialogSubmit(){
dialog.trigger('coral-overlay:close');
$.ajax({
url: '/bin/etoolbox/link-inspector/delete-report',
type: 'DELETE',
success: function(){
window.location.reload();
},
error: function(){
var alertPopup = new Coral.Alert().set({
variant: "error",
header: {
innerHTML: 'ERROR'
},
content: {
textContent: ERROR_MSG
}
});
alertPopup.classList.add('elc-coral-alert');
document.body.append(alertPopup);
}
aliakseiTraihel marked this conversation as resolved.
Show resolved Hide resolved
});
}

dialog.on('click', '[data-dialog-action]', onDeleteDialogSubmit);
dialog.on('coral-overlay:close', function (event) {
dialog.remove();
initDeleteDialog();
});

document.body.appendChild(dialog);
}

$(window).adaptTo('foundation-registry').register('foundation.collection.action.action', {
name: 'cq-admin.etoolbox.linkinspector.action.delete-report',
handler: onFilterAction
});

$(document).ready(function () {
initDeleteDialog();
});

})(window, document, Granite.$, Granite.ELC, Granite, Coral);
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
var BACKUP_CHECKBOX_LABEL = Granite.I18n.get('Backup before replacement');
var CSV_OUT_CHECKBOX_LABEL = Granite.I18n.get('Download CSV with updated items');
var DRY_RUN_TOOLTIP = Granite.I18n.get("If checked, no changes will be applied in the repository");
var REPLACEMENT_DESCRIPTION = Granite.I18n.get('* Replacement will be applied within the detected broken links scope');
var REPLACEMENT_DESCRIPTION = Granite.I18n.get('* Replacement will be applied within the selected broken links scope');
var REPLACEMENT_ACL_DESCRIPTION = Granite.I18n.get('** User should have sufficient read/write permissions in order to complete replacement successfully and create the backup package');
var VALIDATION_MSG = Granite.I18n.get('Replacement can\'t be the same as pattern');
var LINK_TO_UPDATE_LABEL = Granite.I18n.get('The following links will be updated:');
Expand Down Expand Up @@ -63,7 +63,7 @@
advancedMode: data.advancedMode,
selected: data.selected
}].filter(function (item) {
return item.pattern && item.replacement && item.pattern !== item.replacement;
return !item.advancedMode || item.pattern && item.replacement && item.pattern !== item.replacement;
});
ELC.bulkLinksUpdate(replacementList, buildReplaceRequest);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<img itemprop="thumbnail" width="192" src="${model.thumbnail}" alt="">
</td>

<td class="foundation-collection-item-keys" is="coral-table-cell">
<td class="foundation-collection-item-keys collection-item_status_code" is="coral-table-cell">
<div data-sly-set.statuscode="${model.linkStatusCode == '200' ? 'ok' : 'error'}"
class="status" title="HTTP Status ${model.linkStatusCode}">
<coral-icon icon="circle" class="elc-link-status--${statuscode}"></coral-icon>
Expand All @@ -19,7 +19,7 @@
</div>
</td>

<td class="foundation-collection-item-title foundation-collection-item-link-type" is="coral-table-cell">
<td class="foundation-collection-item-title collection-item_link-type" is="coral-table-cell">
<div class="elc-complex-col">
<h4 class="elc-primary-info" title="${model.link}">${model.link}</h4>
<span class="elc-secondary-info">${model.linkType}</span>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,16 @@
text="Filter"
title="Filter"
variant="minimal"/>
<deleteReport
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/collection/action"
granite:id="elc-delete-report"
action="cq-admin.etoolbox.linkinspector.action.delete-report"
icon="delete"
target=".etoolbox-link-inspector"
text="Delete"
title="Delete"
variant="minimal"/>
</primary>
<secondary jcr:primaryType="nt:unstructured">
<options
Expand Down