Skip to content

Commit

Permalink
Ad import export structures buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
brianbrix committed Oct 4, 2024
1 parent 26fd0d7 commit 8897dda
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,20 @@
<td colspan="2">
<span wicket:id="addbutton" />
</td>
<td colspan="2">

<td colspan="4">
<form wicket:id="form">
<input type="file" wicket:id="fileUpload" accept=".xlsx,.xls,application/vnd.openxmlformats-officedocument" />
<input wicket:id="importStructures" type="submit" value="Import Structures"/>
<button wicket:id="importStructures" type="submit" value="Import Structures">Import Structures</button>
</form>
</td>
<td colspan="2">
<td colspan="4">
<a wicket:id="downloadLink" style="display:none;">Download</a>
<button wicket:id="exportStructures" type="button">Export Structures</button>
<span wicket:id="exportStructures"/>
</td>
</tr>

<tr>
<td colspan="2">
<td colspan="12">
<span wicket:id="listWithPaginator">
<table class="inside" wicket:id="list">
<tr>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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<Void>("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");
Expand Down Expand Up @@ -368,31 +376,69 @@ 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<Void> 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<Void> downloadLink = new ResourceLink<>("downloadLink", resourceReference);
downloadLink.setOutputMarkupId(true);
add(downloadLink);

AmpAjaxLinkField exportStructures = new AmpAjaxLinkField("exportStructures", "Export Structures", "Export Structures") {
@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);
Expand All @@ -417,6 +463,51 @@ private static String getStringValueFromCell(Cell cell) {
}
}

private void writeExcelFile(OutputStream out,PagingListEditor<AmpStructure> 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<AmpStructure> listItem = (ListItem<AmpStructure>) 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<AmpStructure> structureModel) {
StructureData structureData = new StructureData();
AmpStructure structure = structureModel.getObject();
Expand Down

0 comments on commit 8897dda

Please sign in to comment.