|
| 1 | +package org.ecocean.api; |
| 2 | + |
| 3 | +import org.apache.commons.lang3.StringUtils; |
| 4 | +import org.ecocean.*; |
| 5 | +import org.ecocean.export.EncounterAnnotationExportFile; |
| 6 | +import org.ecocean.export.EncounterImageExportFile; |
| 7 | +import org.ecocean.servlet.ServletUtilities; |
| 8 | +import org.ecocean.shepherd.core.Shepherd; |
| 9 | +import org.joda.time.Instant; |
| 10 | + |
| 11 | +import java.io.ByteArrayOutputStream; |
| 12 | +import java.io.IOException; |
| 13 | +import java.lang.reflect.InvocationTargetException; |
| 14 | +import java.util.*; |
| 15 | +import java.util.zip.ZipEntry; |
| 16 | +import java.util.zip.ZipOutputStream; |
| 17 | +import javax.jdo.PersistenceManager; |
| 18 | +import javax.jdo.PersistenceManagerFactory; |
| 19 | +import javax.jdo.Query; |
| 20 | +import javax.servlet.http.HttpServletRequest; |
| 21 | +import javax.servlet.http.HttpServletResponse; |
| 22 | +import javax.servlet.ServletException; |
| 23 | + |
| 24 | +public class EncounterExport extends ApiBase { |
| 25 | + protected void doPost(HttpServletRequest request, HttpServletResponse response) |
| 26 | + throws ServletException, IOException { |
| 27 | + String context = ServletUtilities.getContext(request); |
| 28 | + Shepherd myShepherd = new Shepherd(context); |
| 29 | + |
| 30 | + response.setContentType("application/zip"); |
| 31 | + response.setHeader("Content-Disposition", |
| 32 | + "attachment;filename=encounter_export_" + Instant.now().getMillis() + ".zip"); |
| 33 | + |
| 34 | + myShepherd.beginDBTransaction(); |
| 35 | + try (ZipOutputStream outputStream = new ZipOutputStream(response.getOutputStream())) { |
| 36 | + if (Objects.equals(request.getParameter("includeMetadata"), "true")) { |
| 37 | + writeMetadataFile(request, myShepherd, outputStream); |
| 38 | + } |
| 39 | + EncounterQueryResult queryResult = EncounterQueryProcessor.processQuery(myShepherd, |
| 40 | + request, "year descending, month descending, day descending"); |
| 41 | + List<Encounter> encounters = queryResult.getResult(); |
| 42 | + |
| 43 | + // Build map of encounter ID -> individual using join table relationship |
| 44 | + Map<String, |
| 45 | + MarkedIndividual> encounterToIndividual = buildEncounterIndividualMap(myShepherd, |
| 46 | + encounters); |
| 47 | + EnumSet<EncounterImageExportFile.ExportOptions> flags = EnumSet.noneOf( |
| 48 | + EncounterImageExportFile.ExportOptions.class); |
| 49 | + if (Objects.equals(request.getParameter("unidentifiedEncounters"), "true")) { |
| 50 | + flags = EnumSet.of( |
| 51 | + EncounterImageExportFile.ExportOptions.IncludeUnidentifiedEncounters); |
| 52 | + } |
| 53 | + int numAnnotationsPerId = -1; |
| 54 | + if (StringUtils.isNumeric(request.getParameter("numAnnotationsPerId"))) { |
| 55 | + numAnnotationsPerId = Integer.parseInt(request.getParameter("numAnnotationsPerId")); |
| 56 | + } |
| 57 | + EncounterImageExportFile imagesExport = new EncounterImageExportFile(encounters, |
| 58 | + encounterToIndividual, numAnnotationsPerId, flags); |
| 59 | + |
| 60 | + imagesExport.writeTo(outputStream); |
| 61 | + } catch (Exception e) { |
| 62 | + // todo: make this more specific |
| 63 | + e.printStackTrace(); |
| 64 | + throw new RuntimeException("Unable to export data", e); |
| 65 | + } finally { |
| 66 | + myShepherd.rollbackDBTransaction(); |
| 67 | + myShepherd.closeDBTransaction(); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Builds a map of encounter catalog number to MarkedIndividual. |
| 73 | + * This handles both the old direct foreign key and new join table relationships |
| 74 | + * in a single efficient query. |
| 75 | + */ |
| 76 | + private static Map<String, MarkedIndividual> buildEncounterIndividualMap(Shepherd myShepherd, |
| 77 | + List<Encounter> encounters) |
| 78 | + throws Exception { |
| 79 | + Map<String, MarkedIndividual> map = new HashMap<>(); |
| 80 | + |
| 81 | + if (encounters.isEmpty()) { |
| 82 | + return map; |
| 83 | + } |
| 84 | + // First, handle direct relationships (e.getIndividual() != null) |
| 85 | + for (Encounter e : encounters) { |
| 86 | + if (e.getIndividual() != null) { |
| 87 | + map.put(e.getCatalogNumber(), e.getIndividual()); |
| 88 | + } |
| 89 | + } |
| 90 | + // Build list of encounter IDs that still need individuals |
| 91 | + List<String> encountersNeedingIndividuals = new ArrayList<>(); |
| 92 | + for (Encounter e : encounters) { |
| 93 | + if (!map.containsKey(e.getCatalogNumber())) { |
| 94 | + encountersNeedingIndividuals.add(e.getCatalogNumber()); |
| 95 | + } |
| 96 | + } |
| 97 | + if (encountersNeedingIndividuals.isEmpty()) { |
| 98 | + return map; |
| 99 | + } |
| 100 | + // Query MarkedIndividuals that have these encounters (via join table) |
| 101 | + // Using fetch groups to eagerly load encounters and names |
| 102 | + PersistenceManager pm = myShepherd.getPM(); |
| 103 | + PersistenceManagerFactory pmf = pm.getPersistenceManagerFactory(); |
| 104 | + |
| 105 | + javax.jdo.FetchGroup indvGrp = pmf.getFetchGroup(MarkedIndividual.class, |
| 106 | + "individualWithEncounters"); |
| 107 | + indvGrp.addMember("individualID").addMember("names").addMember("encounters"); |
| 108 | + pm.getFetchPlan().addGroup("individualWithEncounters"); |
| 109 | + |
| 110 | + try (Query query = pm.newQuery(MarkedIndividual.class)) { |
| 111 | + query.setFilter( |
| 112 | + "encounters.contains(enc) && :catalogNumbers.contains(enc.catalogNumber)"); |
| 113 | + query.declareVariables("org.ecocean.Encounter enc"); |
| 114 | + |
| 115 | + @SuppressWarnings("unchecked") List<MarkedIndividual> individuals = (List<MarkedIndividual>)query.execute(encountersNeedingIndividuals); |
| 116 | + // Map encounters to individuals |
| 117 | + for (MarkedIndividual individual : individuals) { |
| 118 | + for (Encounter enc : individual.getEncounters()) { |
| 119 | + if (encountersNeedingIndividuals.contains(enc.getCatalogNumber())) { |
| 120 | + map.put(enc.getCatalogNumber(), individual); |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + return map; |
| 126 | + } |
| 127 | + |
| 128 | + private static void writeMetadataFile(HttpServletRequest request, Shepherd myShepherd, |
| 129 | + ZipOutputStream outputStream) |
| 130 | + throws IOException, NoSuchMethodException, ClassNotFoundException, InvocationTargetException, |
| 131 | + IllegalAccessException { |
| 132 | + EncounterAnnotationExportFile exportFile = new EncounterAnnotationExportFile(request, |
| 133 | + myShepherd); |
| 134 | + |
| 135 | + // Write Excel file to ByteArrayOutputStream first to avoid nested ZIP issues |
| 136 | + try (ByteArrayOutputStream excelBytes = new ByteArrayOutputStream()) { |
| 137 | + exportFile.writeToStream(excelBytes); |
| 138 | + |
| 139 | + // Now write the complete Excel file as a single ZIP entry |
| 140 | + ZipEntry metadataFile = new ZipEntry("metadata.xlsx"); |
| 141 | + outputStream.putNextEntry(metadataFile); |
| 142 | + outputStream.write(excelBytes.toByteArray()); |
| 143 | + outputStream.closeEntry(); |
| 144 | + } |
| 145 | + } |
| 146 | +} |
0 commit comments