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     */
016    package org.opengion.plugin.io;
017    
018    import java.io.PrintWriter;
019    import java.util.List;
020    import java.util.Map ;
021    import java.util.LinkedHashMap ;
022    
023    import org.odftoolkit.odfdom.OdfFileDom;
024    import org.odftoolkit.odfdom.doc.office.OdfOfficeAutomaticStyles;
025    import org.odftoolkit.odfdom.doc.office.OdfOfficeMasterStyles;
026    import org.odftoolkit.odfdom.doc.style.OdfStyle;
027    import org.odftoolkit.odfdom.doc.style.OdfStyleFooter;
028    import org.odftoolkit.odfdom.doc.style.OdfStyleFooterLeft;
029    import org.odftoolkit.odfdom.doc.style.OdfStyleHeader;
030    import org.odftoolkit.odfdom.doc.style.OdfStyleHeaderLeft;
031    import org.odftoolkit.odfdom.doc.style.OdfStyleMasterPage;
032    import org.odftoolkit.odfdom.doc.style.OdfStylePageLayout;
033    import org.odftoolkit.odfdom.doc.style.OdfStyleParagraphProperties;
034    import org.odftoolkit.odfdom.doc.style.OdfStyleTableCellProperties;
035    import org.odftoolkit.odfdom.doc.table.OdfTableCell;
036    import org.odftoolkit.odfdom.doc.table.OdfTableRow;
037    import org.odftoolkit.odfdom.dom.element.style.StylePageLayoutPropertiesElement;
038    import org.odftoolkit.odfdom.dom.style.OdfStyleFamily;
039    import org.w3c.dom.Node;
040    
041    import org.opengion.fukurou.model.NativeType;
042    import org.opengion.fukurou.util.HybsEntry;
043    import org.opengion.hayabusa.common.HybsSystemException;
044    import org.opengion.hayabusa.db.DBColumn;
045    import org.opengion.hayabusa.db.DBTableModel;
046    
047    /**
048     * Calcファイルの書き?しクラスです?
049     *
050     * こ?クラスでは??常の出力クラスと異なり???タ部?は、データではなく?
051     * {@カラ?_行番号}が?力されます?
052     *
053     * こ?出力結果は??常、Calc帳票シス?の雛形を作?するための、???として
054     * 利用することを想定して?す?
055     *
056     * writeTableParam タグで key="Size" を指定できます?これは、作?するレコード???タ件数です?
057     * 初期値は?5件です?
058     *
059     * @og.group ファイル出?
060     *
061     * @version  5.0
062     * @author       Hiroki Nakamura
063     * @since    JDK6.0,
064     */
065    public 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    }