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 }