001/*
002 * Copyright (c) 2009 The openGion Project.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
013 * either express or implied. See the License for the specific language
014 * governing permissions and limitations under the License.
015 */
016package org.opengion.plugin.io;
017
018import java.io.PrintWriter;
019import java.util.List;
020import java.util.Map ;
021import java.util.LinkedHashMap ;
022
023import org.odftoolkit.odfdom.OdfFileDom;
024import org.odftoolkit.odfdom.doc.office.OdfOfficeAutomaticStyles;
025import org.odftoolkit.odfdom.doc.office.OdfOfficeMasterStyles;
026import org.odftoolkit.odfdom.doc.style.OdfStyle;
027import org.odftoolkit.odfdom.doc.style.OdfStyleFooter;
028import org.odftoolkit.odfdom.doc.style.OdfStyleFooterLeft;
029import org.odftoolkit.odfdom.doc.style.OdfStyleHeader;
030import org.odftoolkit.odfdom.doc.style.OdfStyleHeaderLeft;
031import org.odftoolkit.odfdom.doc.style.OdfStyleMasterPage;
032import org.odftoolkit.odfdom.doc.style.OdfStylePageLayout;
033import org.odftoolkit.odfdom.doc.style.OdfStyleParagraphProperties;
034import org.odftoolkit.odfdom.doc.style.OdfStyleTableCellProperties;
035import org.odftoolkit.odfdom.doc.table.OdfTableCell;
036import org.odftoolkit.odfdom.doc.table.OdfTableRow;
037import org.odftoolkit.odfdom.dom.element.style.StylePageLayoutPropertiesElement;
038import org.odftoolkit.odfdom.dom.style.OdfStyleFamily;
039import org.w3c.dom.Node;
040
041import org.opengion.fukurou.model.NativeType;
042import org.opengion.fukurou.util.HybsEntry;
043import org.opengion.hayabusa.common.HybsSystemException;
044import org.opengion.hayabusa.db.DBColumn;
045import org.opengion.hayabusa.db.DBTableModel;
046
047/**
048 * Calcファイルの書き出しクラスです。
049 *
050 * このクラスでは、通常の出力クラスと異なり、データ部分には、データではなく、
051 * {@カラム名_行番号}が出力されます。
052 *
053 * この出力結果は、通常、Calc帳票システムの雛形を作成するための、元情報として
054 * 利用することを想定しています。
055 *
056 * writeTableParam タグで key="Size" を指定できます。これは、作成するレコードのデータ件数です。
057 * 初期値は、25件です。
058 *
059 * @og.group ファイル出力
060 *
061 * @version  5.0
062 * @author       Hiroki Nakamura
063 * @since    JDK6.0,
064 */
065public class TableWriter_CalcDef extends TableWriter_Calc {
066        //* このプログラムのVERSION文字列を設定します。   {@value} */
067        private static final String VERSION = "5.6.6.1 (2013/07/12)" ;
068
069        // 5.6.6.1 (2013/07/12) keys の整合性チェックを行います。
070        private static final Map<String,String> keysMap = new LinkedHashMap<String,String>();
071
072        static {
073                keysMap.put( "SIZE"             , "レコードのデータ件数(初期値:25)"          );
074        }
075
076        private static final int INIT_DATA_SIZE = 25;
077
078        private int dataSize    = INIT_DATA_SIZE;
079
080        /**
081         * PrintWriter に DBTableModelのテーブル情報を書き込みます。
082         * このクラスでは,データを ダブルコーテーション(")で囲みます。
083         * PrintWriter に DBTableModelのテーブル情報を書き込みます。
084         *
085         * @og.rev 5.1.8.0 (2010/07/01) コメント出力(CalcDefAno)追加による対応
086         *
087         * @param       table  DBTableModelオブジェクト
088         * @param       writer PrintWriterオブジェクト
089         */
090        @Override
091        protected void writeData( final DBTableModel table,final PrintWriter writer ) {
092                for( int r=0; r<dataSize; r++ ) {
093                        OdfTableRow row = new OdfTableRow( contentDom );
094
095                        if( useNumber ) {
096//                              String val = "ROWNO_" + String.valueOf( r );
097                                String val = "ROWNO_" + r ;
098                                row.appendCell( createTextCell( contentDom, val, null, true, true ) );
099                        }
100
101                        boolean[] cellType = new boolean[numberOfColumns];
102                        for( int i=0; i<numberOfColumns; i++ ) {
103                                int clm = clmNo[i];
104                                NativeType nativeType = dbColumn[clm].getNativeType();
105                                switch( nativeType ) {
106                                        case INT    :
107                                        case LONG   :
108                                        case DOUBLE :
109                                                cellType[i] = true ;
110                                                break;
111                                        case STRING :
112                                        case CALENDAR :
113                                        default :
114                                                cellType[i] = false ;
115                                                break;
116                                }
117                        }
118
119                        for( int i=0; i<numberOfColumns; i++ ) {
120                                int clm = clmNo[i];
121//                              String val = String.valueOf( table.getColumnName( clm ) ) + "_" + String.valueOf( r );
122                                String val = table.getColumnName( clm ) + "_" + r ;
123                                row.appendCell( createTextCell( contentDom, val, table.getDBColumn( clm ), cellType[i], false ) );
124                                row.setStyleName( "ro1" );
125                        }
126
127                        sheet.appendRow( row );
128                }
129        }
130
131        /**
132         * テキストコンテンツ用のセルを生成する
133         *
134         * @og.rev 5.1.8.0 (2010/07/01) コメント出力(CalcDefAno)追加による対応
135         *
136         * @param       contentDom      OdfFileDomオブジェクト
137         * @param       content         コンテンツ
138         * @param       col                     DBColumnオブジェクト
139         * @param       isCellTypeNumber        [true:数字型/false:文字型]
140         * @param       isNumberList            [true:数字リスト=999/false:通常]
141         *
142         * @return      テキストコンテンツ用のセル
143         */
144//      protected OdfTableCell createTextCell( final OdfFileDom contentDom, final String content, final Boolean isCellTypeNumber, final Boolean isNumberList ) {
145        protected OdfTableCell createTextCell( final OdfFileDom contentDom, final String content, final DBColumn col, final Boolean isCellTypeNumber, final Boolean isNumberList ) {
146                OdfTableCell cell = super.createTextCell( contentDom, "{@" + content + "}", false, isNumberList );
147                if( isNumberList ) {
148                        OdfStyle style = contentAutoStyles.newStyle( OdfStyleFamily.TableCell );
149                        style.setProperty( OdfStyleTableCellProperties.TextAlignSource, "fix" );
150                        style.setProperty( OdfStyleTableCellProperties.RepeatContent, "false" );
151                        style.setProperty( OdfStyleParagraphProperties.TextAlign, "end" );
152                        style.setProperty( OdfStyleParagraphProperties.MarginRight, "0cm" );
153                        String cellStyleName = style.getStyleNameAttribute();
154                        cell.setStyleName( cellStyleName );
155                }
156
157                return cell;
158        }
159
160        /**
161         * デフォルトで用意されているStylesを調整します。
162         *
163         * ヘッダー表示しない
164         * フッターを数字のみにして、右端に出す
165         * ページレイアウトを横にする
166         * ページの設定を、拡大縮小モードを「印刷範囲をページ数に合わせる」に設定(1ページ)
167         */
168        @Override
169        protected void resetAutoStylesAndMasterStyles() {
170                try {
171                        // AutomaticStyles調整
172                        OdfOfficeAutomaticStyles oas = wb.getStylesDom().getAutomaticStyles();
173                        OdfStylePageLayout spl = oas.getPageLayout( "pm1" );
174                        spl.setProperty( StylePageLayoutPropertiesElement.PageHeight, "21.00cm" );
175                        spl.setProperty( StylePageLayoutPropertiesElement.PageWidth, "29.70cm" );
176                        spl.setProperty( StylePageLayoutPropertiesElement.PrintOrientation, "landscape" );
177                        spl.setProperty( StylePageLayoutPropertiesElement.ScaleToPages, "1" );
178
179                        // MasterStyles調整
180                        OdfOfficeMasterStyles oms = wb.getOfficeMasterStyles();
181                        OdfStyleMasterPage smp = oms.getMasterPage( "Default" );
182
183                        // MasterPageのデフォルトで用意されているノードを削除
184                        Node fcd = smp.getFirstChild();
185                        while( fcd != null ) {
186                                smp.removeChild( fcd );
187                                fcd = smp.getFirstChild();
188                        }
189
190                        // MasterPageのノードを定義と追加
191                        OdfStyleHeader sh = new OdfStyleHeader( wb.getStylesDom() );
192                        OdfStyleHeaderLeft shl = new OdfStyleHeaderLeft( wb.getStylesDom() );
193                        sh.setStyleDisplayAttribute( false );
194                        shl.setStyleDisplayAttribute( false );
195                        smp.appendChild( sh );
196                        smp.appendChild( shl );
197                        OdfStyleFooter sf = new OdfStyleFooter( wb.getStylesDom() );
198                        OdfStyleFooterLeft sfl = new OdfStyleFooterLeft( wb.getStylesDom() );
199                        sf.newStyleRegionRightElement().newTextPElement().newTextPageNumberElement();
200                        sfl.setStyleDisplayAttribute( false );
201                        smp.appendChild( sf );
202                        smp.appendChild( sfl );
203                }
204                catch( Exception ex ) {
205                        String errMsg = "AutomaticStylesとMasterStyles調整できません";
206                        throw new HybsSystemException( errMsg, ex );
207                }
208        }
209
210        /**
211         * パラメーターリストをセットします。
212         * 内部は、HybsEntry クラスを持っています。
213         * 引数が、null の場合は、何もしません。
214         *
215         * @og.rev 5.6.6.1 (2013/07/12) keys の整合性チェックを行います。
216         *
217         * @param       listParam       HybsEntryリスト
218         */
219        @Override
220        public void setParam( final List<HybsEntry> listParam ) {
221                if( listParam != null && !listParam.isEmpty() ) {
222                        for( HybsEntry entry : listParam ) {
223
224                                String key = entry.getKey() ;           // 5.6.6.1 (2013/07/12) keys の整合性チェック
225                                checkParam( key , keysMap );
226
227                                String val = entry.getValue() ;         // 5.6.6.1 (2013/07/12) val を先に取得
228                                if( val != null && val.length() > 0 ) {
229                                        if( "Size".equalsIgnoreCase( key ) ) {
230                                                dataSize = Integer.parseInt( val );
231                                        }
232                                }
233
234//                              if( "Size".equalsIgnoreCase( entry.getKey() ) ) {
235//                                      dataSize = Integer.parseInt( entry.getValue() );
236//                              }
237                        }
238                }
239        }
240}