Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
335 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
.../src/main/java/com/evolveum/midpoint/gui/api/component/button/CsvDownloadButtonPanel.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<!-- | ||
~ Copyright (c) 2010-2016 Evolveum | ||
~ | ||
~ 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. | ||
--> | ||
|
||
<!DOCTYPE html> | ||
<html xmlns:wicket="http://wicket.apache.org"> | ||
<wicket:panel> | ||
<a wicket:id="exportData" class="btn btn-primary btn-sm"/> | ||
</wicket:panel> | ||
</html> |
87 changes: 87 additions & 0 deletions
87
.../src/main/java/com/evolveum/midpoint/gui/api/component/button/CsvDownloadButtonPanel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package com.evolveum.midpoint.gui.api.component.button; | ||
|
||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.util.List; | ||
|
||
import org.apache.wicket.ajax.AjaxRequestTarget; | ||
import org.apache.wicket.extensions.markup.html.repeater.data.table.DataTable; | ||
import org.apache.wicket.extensions.markup.html.repeater.data.table.export.CSVDataExporter; | ||
import org.apache.wicket.extensions.markup.html.repeater.data.table.export.ExportToolbar; | ||
import org.apache.wicket.extensions.markup.html.repeater.data.table.export.IExportableColumn; | ||
import org.apache.wicket.markup.repeater.data.IDataProvider; | ||
import org.apache.wicket.model.Model; | ||
import org.apache.wicket.util.resource.IResourceStream; | ||
|
||
import com.evolveum.midpoint.gui.api.component.BasePanel; | ||
import com.evolveum.midpoint.web.component.AbstractAjaxDownloadBehavior; | ||
import com.evolveum.midpoint.web.component.AjaxIconButton; | ||
import com.evolveum.midpoint.web.page.admin.reports.dto.AuditEventRecordProvider; | ||
|
||
public abstract class CsvDownloadButtonPanel extends BasePanel { | ||
|
||
private static final String ID_EXPORT_DATA = "exportData"; | ||
|
||
public CsvDownloadButtonPanel(String id) { | ||
this(id, false); | ||
} | ||
|
||
//TODO: temporal HACK.. there is no time to do it properly, just reusing the current implementation | ||
public CsvDownloadButtonPanel(String id, boolean isAudit) { | ||
super(id); | ||
initLayout(isAudit); | ||
} | ||
|
||
private static final long serialVersionUID = 1L; | ||
|
||
private void initLayout(boolean isAudit) { | ||
CSVDataExporter csvDataExporter = new CSVDataExporter() { | ||
private static final long serialVersionUID = 1L; | ||
|
||
@Override | ||
public <T> void exportData(IDataProvider<T> dataProvider, List<IExportableColumn<T, ?>> columns, | ||
OutputStream outputStream) throws IOException { | ||
if (isAudit) { | ||
((AuditEventRecordProvider) dataProvider).setExportSize(true); | ||
super.exportData(dataProvider, columns, outputStream); | ||
((AuditEventRecordProvider) dataProvider).setExportSize(false); | ||
|
||
} else { | ||
super.exportData(dataProvider, columns, outputStream); | ||
} | ||
|
||
} | ||
}; | ||
final AbstractAjaxDownloadBehavior ajaxDownloadBehavior = new AbstractAjaxDownloadBehavior() { | ||
private static final long serialVersionUID = 1L; | ||
|
||
@Override | ||
public IResourceStream getResourceStream() { | ||
return new ExportToolbar.DataExportResourceStreamWriter(csvDataExporter, getDataTable()); | ||
} | ||
|
||
public String getFileName() { | ||
return CsvDownloadButtonPanel.this.getFilename(); | ||
} | ||
}; | ||
|
||
add(ajaxDownloadBehavior); | ||
|
||
AjaxIconButton exportDataLink = new AjaxIconButton(ID_EXPORT_DATA, new Model<>("fa fa-download"), | ||
createStringResource("CsvDownloadButtonPanel.export")) { | ||
|
||
private static final long serialVersionUID = 1L; | ||
|
||
@Override | ||
public void onClick(AjaxRequestTarget target) { | ||
ajaxDownloadBehavior.initiate(target); | ||
} | ||
}; | ||
add(exportDataLink); | ||
} | ||
|
||
protected abstract DataTable<?,?> getDataTable(); | ||
|
||
protected abstract String getFilename(); | ||
|
||
} |
93 changes: 93 additions & 0 deletions
93
...n-gui/src/main/java/com/evolveum/midpoint/web/component/AbstractAjaxDownloadBehavior.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
* Copyright (c) 2010-2017 Evolveum | ||
* | ||
* 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. | ||
*/ | ||
|
||
package com.evolveum.midpoint.web.component; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
import org.apache.wicket.ajax.AjaxRequestTarget; | ||
import org.apache.wicket.behavior.AbstractAjaxBehavior; | ||
import org.apache.wicket.request.IRequestCycle; | ||
import org.apache.wicket.request.handler.resource.ResourceStreamRequestHandler; | ||
import org.apache.wicket.request.resource.ContentDisposition; | ||
import org.apache.wicket.util.resource.AbstractResourceStream; | ||
import org.apache.wicket.util.resource.IResourceStream; | ||
import org.apache.wicket.util.resource.ResourceStreamNotFoundException; | ||
import org.apache.wicket.util.time.Duration; | ||
|
||
public abstract class AbstractAjaxDownloadBehavior extends AbstractAjaxBehavior { | ||
|
||
private static final long serialVersionUID = 1L; | ||
private boolean addAntiCache; | ||
private String contentType = "text"; | ||
private String fileName = null; | ||
|
||
public AbstractAjaxDownloadBehavior() { | ||
this(true); | ||
} | ||
|
||
public AbstractAjaxDownloadBehavior(boolean addAntiCache) { | ||
super(); | ||
this.addAntiCache = addAntiCache; | ||
} | ||
|
||
/** | ||
* Call this method to initiate the download. | ||
*/ | ||
public void initiate(AjaxRequestTarget target) { | ||
String url = getCallbackUrl().toString(); | ||
|
||
if (addAntiCache) { | ||
url = url + (url.contains("?") ? "&" : "?"); | ||
url = url + "antiCache=" + System.currentTimeMillis(); | ||
} | ||
|
||
// the timeout is needed to let Wicket release the channel | ||
target.appendJavaScript("setTimeout(\"window.location.href='" + url + "'\", 100);"); | ||
} | ||
|
||
public void onRequest() { | ||
|
||
IResourceStream resourceStream = getResourceStream(); | ||
ResourceStreamRequestHandler reqHandler = new ResourceStreamRequestHandler(resourceStream) { | ||
@Override | ||
public void respond(IRequestCycle requestCycle) { | ||
super.respond(requestCycle); | ||
} | ||
}.setContentDisposition(ContentDisposition.ATTACHMENT) | ||
.setCacheDuration(Duration.ONE_SECOND); | ||
if (StringUtils.isNotEmpty(getFileName())){ | ||
reqHandler.setFileName(getFileName()); | ||
} | ||
getComponent().getRequestCycle().scheduleRequestHandlerAfterCurrent(reqHandler); | ||
} | ||
|
||
public void setContentType(String contentType) { | ||
this.contentType = contentType; | ||
} | ||
|
||
public String getFileName() { | ||
return fileName; | ||
} | ||
|
||
public void setFileName(String fileName) { | ||
this.fileName = fileName; | ||
} | ||
|
||
public abstract IResourceStream getResourceStream(); | ||
} |
Oops, something went wrong.