From 8897dda4ace89598d08c05a1d4f78443c5e0536f Mon Sep 17 00:00:00 2001 From: brianbrix Date: Fri, 4 Oct 2024 22:29:16 +0300 Subject: [PATCH] Ad import export structures buttons --- .../AmpStructuresFormSectionFeature.html | 11 +- .../AmpStructuresFormSectionFeature.java | 109 ++++++++++++++++-- 2 files changed, 105 insertions(+), 15 deletions(-) diff --git a/amp/WEB-INF/src/org/dgfoundation/amp/onepager/components/features/sections/AmpStructuresFormSectionFeature.html b/amp/WEB-INF/src/org/dgfoundation/amp/onepager/components/features/sections/AmpStructuresFormSectionFeature.html index 8d49116fb13..0eca8515640 100644 --- a/amp/WEB-INF/src/org/dgfoundation/amp/onepager/components/features/sections/AmpStructuresFormSectionFeature.html +++ b/amp/WEB-INF/src/org/dgfoundation/amp/onepager/components/features/sections/AmpStructuresFormSectionFeature.html @@ -3,21 +3,20 @@ - - +
- +
- + Download - + - + diff --git a/amp/WEB-INF/src/org/dgfoundation/amp/onepager/components/features/sections/AmpStructuresFormSectionFeature.java b/amp/WEB-INF/src/org/dgfoundation/amp/onepager/components/features/sections/AmpStructuresFormSectionFeature.java index ad5144fd678..42491e25ef0 100644 --- a/amp/WEB-INF/src/org/dgfoundation/amp/onepager/components/features/sections/AmpStructuresFormSectionFeature.java +++ b/amp/WEB-INF/src/org/dgfoundation/amp/onepager/components/features/sections/AmpStructuresFormSectionFeature.java @@ -18,19 +18,26 @@ import org.apache.wicket.ajax.AjaxRequestTarget; import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior; import org.apache.wicket.behavior.AttributeAppender; +import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow; import org.apache.wicket.markup.html.TransparentWebMarkupContainer; import org.apache.wicket.markup.html.form.Button; import org.apache.wicket.markup.html.form.Form; import org.apache.wicket.markup.html.form.TextField; import org.apache.wicket.markup.html.form.upload.FileUpload; import org.apache.wicket.markup.html.form.upload.FileUploadField; +import org.apache.wicket.markup.html.link.ResourceLink; import org.apache.wicket.model.AbstractReadOnlyModel; import org.apache.wicket.model.IModel; import org.apache.wicket.model.Model; import org.apache.wicket.model.PropertyModel; +import org.apache.wicket.request.cycle.RequestCycle; +import org.apache.wicket.request.resource.AbstractResource; +import org.apache.wicket.request.resource.IResource; +import org.apache.wicket.request.resource.ResourceReference; import org.apache.wicket.util.lang.Bytes; import org.dgfoundation.amp.onepager.OnePagerUtil; import org.dgfoundation.amp.onepager.components.ListEditorRemoveButton; +import org.dgfoundation.amp.onepager.components.ListItem; import org.dgfoundation.amp.onepager.components.PagingListEditor; import org.dgfoundation.amp.onepager.components.PagingListNavigator; import org.dgfoundation.amp.onepager.components.features.CustomResourceLinkResourceLink; @@ -51,7 +58,9 @@ import org.digijava.module.categorymanager.util.CategoryConstants; import org.digijava.module.categorymanager.util.CategoryManagerUtil; +import javax.servlet.http.HttpServletResponse; import java.io.*; +import java.time.LocalDateTime; import java.util.*; public class AmpStructuresFormSectionFeature extends @@ -324,14 +333,13 @@ public void onClick(AjaxRequestTarget target) { final FileUploadField fileUploadField = new FileUploadField("fileUpload"); + fileUploadField.setOutputMarkupId(true); - // create the form final Form form = new Form("form") { private static final long serialVersionUID = 1L; @Override protected void onSubmit() { - // display uploaded info FileUpload upload = fileUploadField.getFileUpload(); if (upload == null) { logger.info("No file uploaded"); @@ -368,17 +376,56 @@ protected void onSubmit() { } }; - form.setMultiPart(true); // Enable file upload + form.setMultiPart(true); form.setMaxSize(Bytes.megabytes(10)); form.add(fileUploadField); Button importStructures = new Button("importStructures"); +// importStructures.add(new AttributeModifier("disabled", "true")); importStructures.setOutputMarkupId(true); form.add(importStructures); + fileUploadField.add(new AjaxFormComponentUpdatingBehavior("change") { + private static final long serialVersionUID = 1L; + + @Override + protected void onUpdate(AjaxRequestTarget target) { + importStructures.setEnabled(true); + importStructures.add(new AttributeModifier("disabled", (String) null)); // Enable the button + target.add(importStructures); + + } + }); + add(form); - ExportExcelResourceReference resourceReference = new ExportExcelResourceReference(); - CustomResourceLinkResourceLink downloadLink = new CustomResourceLinkResourceLink<>("downloadLink",resourceReference ); + + + ResourceReference resourceReference = new ResourceReference("exportData-"+ System.currentTimeMillis()) { + @Override + public IResource getResource() { + return new AbstractResource() { + @Override + protected ResourceResponse newResourceResponse(Attributes attributes) { + ResourceResponse response = new ResourceResponse(); + response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); + response.setFileName("exported-structures-activity-"+am.getObject().getAmpId()+".xlsx"); + response.disableCaching(); + response.setWriteCallback(new WriteCallback() { + @Override + public void writeData(Attributes attributes) { + try (OutputStream out = attributes.getResponse().getOutputStream()) { + writeExcelFile(out, list); + } catch (IOException e) { + logger.error("Error writing data to file", e); + } + } + }); + return response; + } + }; + } + }; + ResourceLink downloadLink = new ResourceLink<>("downloadLink", resourceReference); downloadLink.setOutputMarkupId(true); add(downloadLink); @@ -386,13 +433,12 @@ protected void onSubmit() { @Override public void onClick(AjaxRequestTarget target) { logger.info("Preparing to download data"); - resourceReference.setList(list); - downloadLink.setReference(resourceReference); - // Trigger the non-AJAX file download String downloadLinkMarkupId = downloadLink.getMarkupId(); - target.appendJavaScript("setTimeout(function() { document.getElementById('" + downloadLinkMarkupId + "').click(); }, 100);"); + target.add(list.getParent()); + target.appendJavaScript("document.getElementById('" + downloadLinkMarkupId + "').click();"); } }; + add(exportStructures); exportStructures.getButton().add(new AttributeModifier("class", new Model("exportStructures button_blue_btm"))); add(exportStructures); @@ -417,6 +463,51 @@ private static String getStringValueFromCell(Cell cell) { } } + private void writeExcelFile(OutputStream out,PagingListEditor list) throws IOException { + try(XSSFWorkbook workbook = new XSSFWorkbook()) { + // Create Excel sheet + XSSFSheet sheet = workbook.createSheet("Structures"); + + // Create header row + XSSFRow headerRow = sheet.createRow(0); + headerRow.createCell(0).setCellValue("Title"); + headerRow.createCell(1).setCellValue("Description"); + headerRow.createCell(2).setCellValue("Latitude"); + headerRow.createCell(3).setCellValue("Longitude"); + + int rowIndex = 1; + for (Component child : list) { + if (child instanceof ListItem) { + ListItem listItem = (ListItem) child; + AmpStructure structure = listItem.getModelObject(); + + // Create a new row for each structure + XSSFRow row = sheet.createRow(rowIndex); + if (structure != null) { + createCellIfNotNull(row, 0, structure.getTitle()); + createCellIfNotNull(row, 1, structure.getDescription()); + createCellIfNotNull(row, 2, structure.getLatitude()); + createCellIfNotNull(row, 3, structure.getLongitude()); + } + rowIndex++; + } + } + + // Write workbook content to the output stream + workbook.write(out); + + // Respond with the written content + } catch (IOException e) { + logger.error("Error exporting data to Excel", e); + } + } + + private void createCellIfNotNull(XSSFRow row, int columnIndex, Object value) { + if (value != null) { + row.createCell(columnIndex).setCellValue(value.toString()); + } + } + public StructureData getDataFromStructureModel(IModel structureModel) { StructureData structureData = new StructureData(); AmpStructure structure = structureModel.getObject();