001/*
002 * (C) Copyright 2013 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Martin Pernollet
016 */
017
018package org.nuxeo.ecm.platform.groups.audit.service.acl.excel;
019
020import org.apache.poi.ss.usermodel.Cell;
021
022/**
023 * Override the default {@link ExcelBuilder} to write over several excel sheet once a cell column exceeds the maximum
024 * number of column for excel.
025 *
026 * @author Martin Pernollet <mpernollet@nuxeo.com>
027 */
028public class ExcelBuilderMultiSheet extends ExcelBuilder {
029    protected boolean multiSheetMode = false;
030
031    public ExcelBuilderMultiSheet() {
032        super();
033    }
034
035    public ExcelBuilderMultiSheet(Type type, String firstSheet) {
036        super(type, firstSheet);
037    }
038
039    public ExcelBuilderMultiSheet(Type type) {
040        super(type);
041    }
042
043    public boolean isMultiSheetColumns() {
044        return multiSheetMode;
045    }
046
047    /**
048     * If true, the builder is able to switch on next sheets to fill cells with column id > 255.
049     */
050    public void setMultiSheetColumns(boolean multiSheetMode) {
051        this.multiSheetMode = multiSheetMode;
052    }
053
054    @Override
055    protected Cell getOrCreateCell(int i, int j) {
056        if (multiSheetMode)
057            j = autoSelectSheet(j);
058        return super.getOrCreateCell(i, j);
059    }
060
061    /**
062     * Only invalidate cells having a too large row id. {@inheritDoc}
063     */
064    @Override
065    protected boolean validateCellIndex(int row, int column, String content) {
066        if (multiSheetMode) {
067            if (row >= MAX_ROW) {
068                String message = "max number of row (" + MAX_ROW + ") exceeded @ " + row + " by '" + content + "'";
069                if (CRASH_ON_CELL_OVERFLOW)
070                    throw new IllegalArgumentException(message);
071                else {
072                    log.warn(message);
073                    return false;
074                }
075            }
076            return true;
077        } else {
078            return super.validateCellIndex(row, column, content);
079        }
080    }
081
082    /* */
083
084    /**
085     * Return a sheet ID for the expected cell:
086     * <ul>
087     * <li>sheet 0 for cells [0;255]
088     * <li>sheet 1 for cells [256;511]
089     * <li>...
090     * </ul>
091     */
092    public int getVirtualCellSheet(int realColumn) {
093        return realColumn / MAX_COLUMN;
094    }
095
096    /**
097     * Return a column ID for the expected cell:
098     * <ul>
099     * <li>returns 255 for realColumn=255
100     * <li>returns 0 for realColumn=256
101     * <li>...
102     * </ul>
103     */
104    public int getVirtualCellColumn(int realColumn) {
105        return realColumn % MAX_COLUMN;
106    }
107
108    /**
109     * Compute the sheet/column required to access the given column. If expected sheet is not current, change it. If it
110     * does not exist, create it.
111     *
112     * @return an updated column index.
113     */
114    protected int autoSelectSheet(int column) {
115        int s = getVirtualCellSheet(column);
116
117        // change current sheet if required
118        if (s != getCurrentSheetId()) {
119            if (!sheetInitialized(s)) {
120                setCurrentSheetId(newSheet(s, "(" + s + ")"));
121            } else {
122                setCurrentSheetId(s);
123            }
124        }
125        column = getVirtualCellColumn(column); // update J!!
126        return column;
127    }
128
129}