All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.isisaddons.wicket.excel.cpt.ui.ExcelFileModel Maven / Gradle / Ivy

Go to download

A component for Apache Isis' Wicket viewer, allowing tables to be downloaded as Excel spreadsheets.

There is a newer version: 1.17.0
Show newest version
package org.isisaddons.wicket.excel.cpt.ui;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.wicket.model.LoadableDetachableModel;
import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import org.joda.time.LocalDateTime;

import org.apache.isis.applib.annotation.Where;
import org.apache.isis.applib.filter.Filter;
import org.apache.isis.applib.filter.Filters;
import org.apache.isis.core.metamodel.adapter.ObjectAdapter;
import org.apache.isis.core.metamodel.spec.ObjectSpecification;
import org.apache.isis.core.metamodel.spec.feature.ObjectAssociation;
import org.apache.isis.core.metamodel.spec.feature.ObjectAssociationFilters;
import org.apache.isis.viewer.wicket.model.models.EntityCollectionModel;

class ExcelFileModel extends LoadableDetachableModel {

        private static final long serialVersionUID = 1L;
        
        private final EntityCollectionModel model;

        public ExcelFileModel(EntityCollectionModel model) {
            this.model = model;
        }

        static class RowFactory {
            private final Sheet sheet;
            private int rowNum;

            RowFactory(Sheet sheet) {
                this.sheet = sheet;
            }

            public Row newRow() {
                return sheet.createRow((short) rowNum++);
            }
        }
        
        @Override
        protected File load() {
            
            try {
                return createFile();
                
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

        private File createFile() throws IOException, FileNotFoundException {
            final Workbook wb = new XSSFWorkbook();
            String sheetName = model.getName();
            final File tempFile = File.createTempFile(ExcelFileModel.class.getCanonicalName(), sheetName + ".xlsx");
            final FileOutputStream fos = new FileOutputStream(tempFile);
            final Sheet sheet = wb.createSheet(sheetName);
            
            final ObjectSpecification typeOfSpec = model.getTypeOfSpecification();
            @SuppressWarnings("unchecked")
            final Filter filter = Filters.and(
                    ObjectAssociationFilters.PROPERTIES, 
                    ObjectAssociationFilters.staticallyVisible(model.isParented()? Where.PARENTED_TABLES: Where.STANDALONE_TABLES));
            final List propertyList = typeOfSpec.getAssociations(filter);

            final ExcelFileModel.RowFactory rowFactory = new RowFactory(sheet);
            Row row = rowFactory.newRow();


            // header row
            int i=0;
            for (ObjectAssociation property : propertyList) {
                final Cell cell = row.createCell((short) i++);
                cell.setCellValue(property.getName());
            }
            
            final CellStyle dateCellStyle = createDateFormatCellStyle(wb);
            
            // detail rows
            final List adapters = model.getObject();
            for (final ObjectAdapter objectAdapter : adapters) {
                row = rowFactory.newRow();
                i=0;
                for (final ObjectAssociation property : propertyList) {
                    final Cell cell = row.createCell((short) i++);
                    setCellValue(objectAdapter, property, cell, dateCellStyle);
                }
            }
            
            // freeze panes
            sheet.createFreezePane(0, 1);

            wb.write(fos);
            fos.close();
            return tempFile;
        }

        protected void autoSize(final Sheet sh, int numProps) {
            for(int prop=0; prop




© 2015 - 2024 Weber Informatics LLC | Privacy Policy