Changeset 7041 for SRUAggregator


Ignore:
Timestamp:
07/18/16 22:00:27 (8 years ago)
Author:
Leif-Jöran
Message:

Adding Open document spreadsheet (ODS) export.

Location:
SRUAggregator/trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • SRUAggregator/trunk/pom.xml

    r7034 r7041  
    151151                                </exclusion>
    152152                        </exclusions>
     153                </dependency>
     154                <dependency>
     155                        <groupId>org.jopendocument</groupId>
     156                        <artifactId>jOpenDocument</artifactId>
     157                        <version>1.3</version>
    153158                </dependency>
    154159        </dependencies>
  • SRUAggregator/trunk/src/main/java/eu/clarin/sru/fcs/aggregator/rest/RestService.java

    r7034 r7041  
    5555        private static final String EXPORT_FILENAME_PREFIX = "ClarinFederatedContentSearch-";
    5656        private static final String TCF_MEDIA_TYPE = "text/tcf+xml";
     57        private static final String ODS_MEDIA_TYPE = "application/vnd.oasis.opendocument.spreadsheet";
    5758        private static final String EXCEL_MEDIA_TYPE = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
    5859        private static final String SEARCH_RESULTS_ENCODING = "UTF-8";
     
    249250                                        search.getResults(corpusId), search.getSearchLanguage(), filterLanguage);
    250251                        return download(bytes, TCF_MEDIA_TYPE, search.getQuery() + ".xml");
     252                } else if (format.equals("ods")) {
     253                        byte[] bytes = Exports.getExportODS(search.getResults(corpusId), filterLanguage);
     254                        return download(bytes, ODS_MEDIA_TYPE, search.getQuery() + ".ods");
    251255                } else if (format.equals("excel")) {
    252256                        byte[] bytes = Exports.getExportExcel(search.getResults(corpusId), filterLanguage);
     
    258262
    259263                return Response.status(Response.Status.BAD_REQUEST)
    260                                 .entity("format parameter must be one of: text, tcf, excel, csv")
     264                                .entity("format parameter must be one of: text, tcf, ods, excel, csv")
    261265                                .build();
    262266        }
  • SRUAggregator/trunk/src/main/java/eu/clarin/sru/fcs/aggregator/search/Exports.java

    r7030 r7041  
    77import eu.clarin.weblicht.wlfxb.tc.xb.TextCorpusStored;
    88import eu.clarin.weblicht.wlfxb.xb.WLData;
     9
     10import java.awt.Color;
    911import java.io.ByteArrayOutputStream;
    1012import java.io.IOException;
     
    1921import org.apache.poi.xssf.streaming.SXSSFWorkbook;
    2022
     23import org.jopendocument.dom.ODPackage;
     24import org.jopendocument.dom.ODDocument;
     25import org.jopendocument.dom.text.TextDocument;
     26import org.jopendocument.dom.spreadsheet.SpreadSheet;
     27
    2128/**
    2229 * Utility for representing SearchResult data in different formats.
     
    2835
    2936        private static final Logger LOGGER = Logger.getLogger(Exports.class.getName());
    30 
     37        private static final Color HIT_BACKGROUND = new Color(230,242,254);
     38        private static final Color CQL_BACKGROUND = new Color(255,240,225);
     39        private static final Color FCS_BACKGROUND = new Color(240,255,225);
    3140        public static String getExportCSV(List<Result> resultsProcessed, String filterLanguage, String separator) {
    3241                boolean noResult = true;
     
    140149
    141150    public static byte[] getExportExcel(List<Result> resultsProcessed, String filterLanguage) throws ExportException {
    142             SXSSFWorkbook workbook = null;
    143                 ByteArrayOutputStream excelStream = new ByteArrayOutputStream();
    144                 int rownum = 0;
    145                 boolean firstRow = true;
    146                 if (resultsProcessed != null && !resultsProcessed.isEmpty()) {
    147                     try {
    148                         workbook = new SXSSFWorkbook();
    149                         Sheet sheet = workbook.createSheet();
     151        SXSSFWorkbook workbook = null;
     152        ByteArrayOutputStream excelStream = new ByteArrayOutputStream();
     153        int rownum = 0;
     154        boolean firstRow = true;
     155        if (resultsProcessed != null && !resultsProcessed.isEmpty()) {
     156            try {
     157                workbook = new SXSSFWorkbook();
     158                Sheet sheet = workbook.createSheet();
     159               
     160                Font boldFont = workbook.createFont();
     161                boldFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
     162               
     163                // Header
     164                CellStyle headerStyle = workbook.createCellStyle();
     165                headerStyle.setFont(boldFont);
     166               
     167                Row row = sheet.createRow(rownum++);
     168               
     169                Cell cell;
     170                for (Result result : resultsProcessed) {
     171                    if (result.getAdvancedLayers().size() == 0) {
     172                        for (Kwic kwic : result.getKwics()) {
     173                            if (firstRow) {
     174                                String[] headers = new String[]{
     175                                    "PID", "REFERENCE", "LEFT CONTEXT", "KEYWORD", "RIGHT CONTEXT"};
     176                                for (int j = 0; j < headers.length; ++j) {
     177                                    cell = row.createCell(j, Cell.CELL_TYPE_STRING);
     178                                    cell.setCellValue(headers[j]);
     179                                    cell.setCellStyle(headerStyle);
     180                                }
     181                                firstRow = false;
     182                            }
     183                            // Body
     184                           
     185                            if (filterLanguage != null && !filterLanguage.equals(kwic.getLanguage())) {
     186                                continue;
     187                            }
     188                            row = sheet.createRow(rownum++);
     189                            cell = row.createCell(0, Cell.CELL_TYPE_STRING);
     190                            if (kwic.getPid() != null) {
     191                                cell.setCellValue(kwic.getPid());
     192                            }
     193                            cell = row.createCell(1, Cell.CELL_TYPE_STRING);
     194                            if (kwic.getReference() != null) {
     195                                cell.setCellValue(kwic.getReference());
     196                            }
     197                           
     198                            cell = row.createCell(2, Cell.CELL_TYPE_STRING);
     199                            cell.setCellValue(kwic.getLeft());
     200                            cell = row.createCell(3, Cell.CELL_TYPE_STRING);
     201                            cell.setCellValue(kwic.getKeyword());
     202                            cell.setCellStyle(headerStyle);
     203                            cell = row.createCell(4, Cell.CELL_TYPE_STRING);
     204                            cell.setCellValue(kwic.getRight());
     205                        }
     206                    }
     207                    for (AdvancedLayer layer : result.getAdvancedLayers()) {
     208                        if (firstRow) {
     209                            String[] headers = new String[]{
     210                                "PID", "REFERENCE", "SPANS"};
     211                            for (int j = 0; j < headers.length; ++j) {
     212                                cell = row.createCell(j, Cell.CELL_TYPE_STRING);
     213                                cell.setCellValue(headers[j]);
     214                                cell.setCellStyle(headerStyle);
     215                            }
     216                            firstRow = false;
     217                        }
    150218                       
    151                         Font boldFont = workbook.createFont();
    152                         boldFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
     219                        if (filterLanguage != null && !filterLanguage.equals(layer.getLanguage())) {
     220                            continue;
     221                        }
     222                        row = sheet.createRow(rownum++);
     223                        int j = 0;
     224                        cell = row.createCell(j, Cell.CELL_TYPE_STRING);
     225                        if (layer.getPid() != null) {
     226                            cell.setCellValue(layer.getPid());
     227                        }
     228                        j++;
     229                        cell = row.createCell(j, Cell.CELL_TYPE_STRING);
     230                        if (layer.getReference() != null) {
     231                            cell.setCellValue(layer.getReference());
     232                        }
     233                        j++;
     234                        for (AdvancedLayer.Span span : layer.getSpans()) {
     235                            cell = row.createCell(j, Cell.CELL_TYPE_STRING);
     236                            cell.setCellValue(span.getText());
     237                            if (span.isHit()) {
     238                                cell.setCellStyle(headerStyle);
     239                            }
     240                            j++;
     241                        }
     242                    }
     243                }
     244                workbook.write(excelStream);
     245            } catch (IOException ex) {
     246                LOGGER.log(Level.SEVERE, null, ex);
     247                throw new ExportException("Exception exporting Excel", ex);
     248            } finally {
     249                if (workbook != null) {
     250                    workbook.dispose();
     251                }
     252            }
     253        }
     254        if (rownum <= 1) {
     255            return null;
     256        } else {
     257            return excelStream.toByteArray();
     258        }
     259    }
     260
     261    public static byte[] getExportODS(List<Result> resultsProcessed, String filterLanguage) throws ExportException {
     262        SpreadSheet spreadSheet = null;
     263        org.jopendocument.dom.spreadsheet.Sheet sheet = null;
     264        ByteArrayOutputStream odsStream = new ByteArrayOutputStream();
     265        int rownum = 0;
     266        boolean firstRow = true;
     267        if (resultsProcessed != null && !resultsProcessed.isEmpty()) {
     268            spreadSheet = SpreadSheet.create(1, 5, resultsProcessed.size());
     269            sheet = spreadSheet.getSheet(0);
     270            int largestColumnCount = 5;
     271            for (Result result : resultsProcessed) {
     272                if (result.getAdvancedLayers().size() == 0) {
     273                    if (firstRow) {
     274                        String[] headers = new String[]{
     275                            "PID", "REFERENCE", "LEFT CONTEXT", "KEYWORD", "RIGHT CONTEXT"};
     276                        for (int j = 0; j < headers.length; ++j) {
     277                            sheet.setValueAt(headers[j], j, rownum);
     278                            sheet.getCellAt(j, rownum).setBackgroundColor(CQL_BACKGROUND);
     279                        }
     280                        firstRow = false;
     281                    }
     282                    sheet.ensureRowCount(rownum + result.getKwics().size() + 2);
     283                    for (Kwic kwic : result.getKwics()) {
     284                        // Body
     285                        if (filterLanguage != null && !filterLanguage.equals(kwic.getLanguage())) {
     286                            continue;
     287                        }
     288                        rownum++;
     289                        if (kwic.getPid() != null) {
     290                            sheet.setValueAt(kwic.getPid(), 0, rownum);
     291                        }
     292                        if (kwic.getReference() != null) {
     293                            sheet.setValueAt(kwic.getReference(), 1, rownum);
     294                        }
    153295                       
    154                         // Header
    155                         CellStyle headerStyle = workbook.createCellStyle();
    156                         headerStyle.setFont(boldFont);
    157                        
    158                         Row row = sheet.createRow(rownum++);
    159                        
    160                         Cell cell;
    161                         for (Result result : resultsProcessed) {
    162                             if (result.getAdvancedLayers().size() == 0) {
    163                                 for (Kwic kwic : result.getKwics()) {
    164                                     if (firstRow) {
    165                                         String[] headers = new String[]{
    166                                             "PID", "REFERENCE", "LEFT CONTEXT", "KEYWORD", "RIGHT CONTEXT"};
    167                                         for (int j = 0; j < headers.length; ++j) {
    168                                             cell = row.createCell(j, Cell.CELL_TYPE_STRING);
    169                                             cell.setCellValue(headers[j]);
    170                                             cell.setCellStyle(headerStyle);
    171                                         }
    172                                         firstRow = false;
    173                                     }
    174                                     // Body
    175                                    
    176                                     if (filterLanguage != null && !filterLanguage.equals(kwic.getLanguage())) {
    177                                         continue;
    178                                     }
    179                                     row = sheet.createRow(rownum++);
    180                                     cell = row.createCell(0, Cell.CELL_TYPE_STRING);
    181                                     if (kwic.getPid() != null) {
    182                                         cell.setCellValue(kwic.getPid());
    183                                     }
    184                                     cell = row.createCell(1, Cell.CELL_TYPE_STRING);
    185                                     if (kwic.getReference() != null) {
    186                                         cell.setCellValue(kwic.getReference());
    187                                     }
    188                                    
    189                                     cell = row.createCell(2, Cell.CELL_TYPE_STRING);
    190                                     cell.setCellValue(kwic.getLeft());
    191                                     cell = row.createCell(3, Cell.CELL_TYPE_STRING);
    192                                     cell.setCellValue(kwic.getKeyword());
    193                                     cell.setCellStyle(headerStyle);
    194                                     cell = row.createCell(4, Cell.CELL_TYPE_STRING);
    195                                     cell.setCellValue(kwic.getRight());
    196                                 }
    197                             }
    198                             firstRow = true;
    199                             for (AdvancedLayer layer : result.getAdvancedLayers()) {
    200                                 if (firstRow) {
    201                                     String[] headers = new String[]{
    202                                         "PID", "REFERENCE", "SPANS"};
    203                                     for (int j = 0; j < headers.length; ++j) {
    204                                         cell = row.createCell(j, Cell.CELL_TYPE_STRING);
    205                                         cell.setCellValue(headers[j]);
    206                                         cell.setCellStyle(headerStyle);
    207                                     }
    208                                     firstRow = false;
    209                                 }
    210                                
    211                                 if (filterLanguage != null && !filterLanguage.equals(layer.getLanguage())) {
    212                                     continue;
    213                                 }
    214                                 row = sheet.createRow(rownum++);
    215                                 int j = 0;
    216                                 cell = row.createCell(j, Cell.CELL_TYPE_STRING);
    217                                 if (layer.getPid() != null) {
    218                                     cell.setCellValue(layer.getPid());
    219                                 }
    220                                 j++;
    221                                 cell = row.createCell(j, Cell.CELL_TYPE_STRING);
    222                                 if (layer.getReference() != null) {
    223                                     cell.setCellValue(layer.getReference());
    224                                 }
    225                                 j++;
    226                                 for (AdvancedLayer.Span span : layer.getSpans()) {
    227                                     cell = row.createCell(j, Cell.CELL_TYPE_STRING);
    228                                     cell.setCellValue(span.getText());
    229                                     if (span.isHit()) {
    230                                         cell.setCellStyle(headerStyle);
    231                                     }
    232                                     j++;
    233                                 }
    234                             }
    235                         }
    236                         workbook.write(excelStream);
    237                     } catch (IOException ex) {
    238                         LOGGER.log(Level.SEVERE, null, ex);
    239                         throw new ExportException("Exception exporting Excel", ex);
    240                     } finally {
    241                         if (workbook != null) {
    242                             workbook.dispose();
    243                         }
    244                     }
    245                 }
    246                 if (rownum <= 1) {
    247                         return null;
    248                 } else {
    249                         return excelStream.toByteArray();
    250                 }
    251         }
     296                        sheet.setValueAt(kwic.getLeft(), 2, rownum);
     297                        sheet.setValueAt(kwic.getKeyword(), 3, rownum);
     298                        sheet.getCellAt(3, rownum).setBackgroundColor(HIT_BACKGROUND);
     299                        sheet.setValueAt(kwic.getRight(), 4, rownum);
     300                    }
     301                } else { // ADV
     302                    if (firstRow) {
     303                        String[] headers = new String[]{
     304                            "PID", "REFERENCE", "SPANS"};
     305                        for (int j = 0; j < headers.length; ++j) {
     306                            sheet.setValueAt(headers[j], j, rownum);
     307                            sheet.getCellAt(j, rownum).setBackgroundColor(FCS_BACKGROUND);
     308                        }
     309                        firstRow = false;
     310                    }
     311
     312                    sheet.ensureRowCount(rownum + result.getAdvancedLayers().size() + 2);
     313                    for (AdvancedLayer layer : result.getAdvancedLayers()) {
     314                        if (filterLanguage != null && !filterLanguage.equals(layer.getLanguage())) {
     315                            continue;
     316                        }
     317                        rownum++;
     318                        if (layer.getPid() != null) {
     319                            sheet.setValueAt(layer.getPid(), 0, rownum);
     320                        }
     321                        if (layer.getReference() != null) {
     322                            sheet.setValueAt(layer.getReference(), 1, rownum);
     323                        }
     324
     325                        if (layer.getSpans().size() + 2 > largestColumnCount) {
     326                            largestColumnCount = layer.getSpans().size() + 2;
     327                            sheet.ensureColumnCount(largestColumnCount);
     328                        }
     329                        int j = 2;
     330                        for (AdvancedLayer.Span span : layer.getSpans()) {
     331                            sheet.setValueAt(span.getText(), j, rownum);
     332                            if (span.isHit()) {
     333                                sheet.getCellAt(j, rownum).setBackgroundColor(HIT_BACKGROUND);
     334                            }
     335                            j++;
     336                        }
     337                    }
     338                }
     339            }
     340            try {
     341                spreadSheet.getPackage().save(odsStream);
     342            } catch (IOException ex) {
     343                LOGGER.log(Level.SEVERE, null, ex);
     344                throw new ExportException("Exception exporting ODS", ex);
     345            } finally {
     346                if (spreadSheet != null) {
     347                    spreadSheet = null;
     348                }
     349            }
     350        }
     351        if (rownum <= 1) {
     352            return null;
     353        } else {
     354            return odsStream.toByteArray();
     355        }
     356    }
    252357
    253358        public static byte[] getExportTCF(List<Result> resultsProcessed,
  • SRUAggregator/trunk/src/main/resources/assets/js/search.jsx

    r7040 r7041  
    949949                                        <li> <a href={this.props.getDownloadLink(corpusId, "csv")}>
    950950                                                        {" "} As CSV file</a></li>
     951                                        <li> <a href={this.props.getDownloadLink(corpusId, "ods")}>
     952                                                        {" "} As ODS file</a></li>
    951953                                        <li> <a href={this.props.getDownloadLink(corpusId, "excel")}>
    952954                                                        {" "} As Excel file</a></li>
Note: See TracChangeset for help on using the changeset viewer.