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.hayabusa.resource;
017    
018    import org.opengion.hayabusa.common.HybsSystem;
019    import org.opengion.hayabusa.common.SystemManager;
020    import org.opengion.fukurou.db.DBUtil;
021    import org.opengion.fukurou.util.Cleanable;
022    import org.opengion.fukurou.util.ApplicationInfo;
023    
024    import java.util.Map;
025    import java.util.WeakHashMap;
026    
027    /**
028     * 事業æ‰?CDJGS) , å¹´æœ?YYYYMM) ã«å¯¾å¿œã—ãŸä¼‘日カレンãƒ?ƒ‡ãƒ¼ã‚¿ã‚’作æ?ã™ã‚‹ãƒ•ァクトリクラスã§ã™ã?
029     *
030     * カレンãƒ?ƒ‡ãƒ¼ã‚¿ã¯ã€?¼‘æ—¥(DY1)?žï¼“1日(DY31)ã¾ã§ã®æ—¥ä»˜ã‘æ¬?«å¯¾ã—ã¦ã€?:平日 1:休日 ã¨ã?†
031     * ãƒ??ã‚¿ã‚’æŒã£ã¦ã?¾ã™ã?実際ã«ã¯ã€å?部ã§ã¯ true:平日 false:休日 ã¨ã?†æŒã¡æ–¹ã‚’ã—ã¾ã™ã?
032     * (カレンãƒ?¼¤?¢ã¯ã€æ–‡å­—å?ã§ã™ãŒã€å?éƒ¨ã§æŒã¤å ´åˆã?ã€æ•°å­—ã§ç®¡ç?—ã¦ã?¾ã™ã?)
033     *
034     * 通常ã®ã‚«ãƒ¬ãƒ³ãƒ?§ã¯ã€æœˆæ¯Žã«ã€??’月ã¯ã€å¹´ã«ã‚ˆã£ã¦)æœ?¤§æ—¥ä»˜ã‘ãŒå¤‰ã‚りã¾ã™ã?
035     * ã“れã¯ã€ã‚«ãƒ¬ãƒ³ãƒ?ƒ‡ãƒ¼ã‚¿ã‚¯ãƒ©ã‚¹ã¨ã—ã¦ã¯ã€?1 を設定ã—ã¦ãŠãã¾ã™ã?
036     *
037     * カレンãƒ?ƒ‡ãƒ¼ã‚¿ã«ã¯ã€?¼“ã¤ã®ãƒ¬ãƒ™ãƒ«ã®ã‚ªãƒ–ジェクト作æ?方法ãŒé©ç”¨ã•れã¾ã™ã?
038     * ä»–ã?リソースã®?“ã¤ã®ãƒ¬ãƒ™ãƒ«ã¨ã¯ç•°ãªã‚Šã?エンジンリソースã‹ã‚‰ã®èª­ã¿å–りã¯
039     * 存在ã—ãªãã?第?”ã?方法ãŒåŠ?‚ã£ãŸäº‹ã«ã‚ˆã‚Šã€?¼“ã¤ã®æ–¹æ³•ã«ãªã£ã¦ã?¾ã™ã?
040     *
041     * ã¾ãšã?読込フラグ(FGLOAD)='1'ã®ã‚«ãƒ¬ãƒ³ãƒ?ƒ‡ãƒ¼ã‚¿ã¯ã€ã“ã®CalendarFactoryオブジェクトãŒ
042     * 構築ã•ã‚ŒãŸæ™‚ã«ã€ã™ã¹ã¦ã‚­ãƒ£ãƒ?‚·ãƒ¥ã¨ã—ã¦å†?ƒ¨ãƒ¡ãƒ¢ãƒªã«èª­ã¿å–りã¾ã™ã?
043     * 読込フラグãŒã?'1' 以外ã?ãƒ??ã‚¿ã¯ã€å?期起動時ã«ã¯ã€ãƒ¡ãƒ¢ãƒªã«ã‚­ãƒ£ãƒ?‚·ãƒ¥ã•れã?
044     * 実際ã«ä½¿ç”¨ã•れるã¾ã§ã€ã‚ªãƒ–ジェクトãŒä½œæ?ã•れã¾ã›ã‚“ã€?
045     * カレンãƒ??å ´åˆã?éŽåŽ»ã®ä½¿ç”¨ã•れるå¯èƒ½æ€§ãŒä½Žã„ãƒ??ã‚¿ã¾ã§ã€ã‚­ãƒ£ãƒ?‚·ãƒ¥ã—ãªã?§˜ã«ã€?
046     * ã“ã?フラグを使用ã—ã¦ã€ãƒ¡ãƒ¢ãƒªã®ç¯?´?‚’図るã“ã¨ãŒå¯èƒ½ã«ãªã‚Šã¾ã™ã?
047     *
048     * 読込フラグ(FGLOAD)自動設定機è?を使用ã™ã‚Œã°ã€èª­ã¿å–ã£ãŸãƒ©ãƒ™ãƒ«ãƒ??ã‚¿ã«å¯¾ã—ã¦ã€?
049     * 読込フラグ(FGLOAD)ã« '1' ã‚’è?å‹•çš„ã«ã‚»ãƒ?ƒˆã—ã¾ã™ã?ã“ã?機è?ã«ã‚ˆã‚Šã€ä¸?º¦ã§ã‚?
050     * 読ã¿å–ã£ãŸã“ã¨ãŒã‚るデータ㫠'1' を付ã‘ã€æ¬¡å›žèµ·å‹•時ã«ã¯ã€ãƒ¡ãƒ¢ãƒªã‚­ãƒ£ãƒ?‚·ãƒ¥ã•ã›ã‚?
051     * 事ã¨ã€ä¸?º¦ã‚‚読ã¿å–ã£ã¦ã?ªã?ƒ‡ãƒ¼ã‚¿ã‚’判別ã—ã¦ã€ãƒ‡ãƒ¼ã‚¿å‰Šé™¤ç­‰ã?メンãƒ?ƒŠãƒ³ã‚¹ã«
052     * 使用ã™ã‚‹ã“ã¨ãŒå¯èƒ½ã§ã™ã?
053     * カレンãƒ?«é™å®šã™ã‚Œã?ã€å½“é¢ä½¿ç”¨ã•れãªã??ã®ã‚«ãƒ¬ãƒ³ãƒ??‘å¹´åˆ?ªã©)を登録ã™ã‚‹æ™‚ã«ã€?
054     * 読込フラグ(FGLOAD)='0' ã«ã—ã¦ãŠã‘ã°ã€å®Ÿéš›ã«èª­ã¿å–られるã¾ã§ã€ãƒ¡ãƒ¢ãƒªã‚­ãƒ£ãƒ?‚·ãƒ¥
055     * ã•れãªã?Ÿã‚ã?ã•らã«ãƒ¡ãƒ¢ãƒªåйçŽ?Œå‘上ã—ã¾ã™ã?
056     *
057     * ?’ã¤ã‚ã?方法ã?ã€ã‚­ãƒ£ãƒ?‚·ãƒ¥ã«å­˜åœ¨ã—ãªã??åˆã?ã€?¼¤?¢ã‹ã‚‰èª­ã¿å–りã¾ã™ã?
058     *
059     * ?“ã¤ã‚ã?ã€ã‚«ãƒ¬ãƒ³ãƒ?‰¹æœ‰ã?方法ã§ã€?¼¤?¢ã«ãƒ??ã‚¿ãŒå­˜åœ¨ã—ãªã??åˆã?設定ã§ã™ã?
060     * ã“れã¯ã€ã‚«ãƒ¬ãƒ³ãƒ?ƒ†ãƒ¼ãƒ–ル未設定時ã§ã‚‚ã?カレンãƒ?¨ã—ã¦ä½¿ç”¨ã§ãるよã†ã«
061     * 自動的ã«ã‚«ãƒ¬ãƒ³ãƒ?ƒ‡ãƒ¼ã‚¿ã‚’作æ?ã—ã¾ã™ã?日曜日を休日ã¨ã—ã¦è‡ªå‹•設定ã—ã¾ã™ã?
062     *
063     * @og.rev 3.6.0.0 (2004/09/17) æ–°è¦ä½œæ?
064     * @og.group リソース管ç?
065     *
066     * @version  4.0
067     * @author   Kazuhiko Hasegawa
068     * @since    JDK5.0,
069     */
070    public final class CalendarFactory {
071            private static final Map<String,CalendarData> pool = new WeakHashMap<String,CalendarData>();                                // 4.0.0 (2005/01/31)
072            private static final Map<String,CalendarQuery> queryClassPool = new WeakHashMap<String,CalendarQuery>();            // 4.0.0 (2005/01/31)
073            // 4.0.0.0 PGカレンãƒ??ã®åˆæœŸè¨­å®šã‚’シスãƒ?ƒ ãƒªã‚½ãƒ¼ã‚¹ã«å¤‰æ›´
074    //      private static final CalendarData PG_CALENDAR_DATA = new CalendarPGData();
075            private static CalendarData pgCalData = null;
076            private static final String PG_CALENDAR_DATA_CLASS = HybsSystem.sys( "DEFAULT_CALENDAR_CLASS");
077            private static final Object lock = new Object();
078    
079            // カレンãƒ?¼¤?¢ã®æŽ¥ç¶šå?ã‚’ã?å–å¾—ã—ã¾ã™ã?
080            private static final String DBID  = HybsSystem.sys( "RESOURCE_CALENDAR_DBID" );
081    
082            // カレンãƒ?¼¤?¢ã‚’使用ã™ã‚‹ã‹ã©ã?‹ã‚’指定ã—ã¾ã™ã?(互æ›ãƒ¢ãƒ¼ãƒ?
083            private static boolean useDB = HybsSystem.sysBool( "USE_CALENDAR_DATABASE" );
084    
085            // 3.8.7.0 (2006/12/15) アクセスログå–å¾—ã?為,ApplicationInfoオブジェクトを設å®?
086            private static final ApplicationInfo appInfo;
087    
088            // 4.0.0 (2005/01/31) Cleanable インターフェースã«ã‚ˆã‚‹åˆæœŸåŒ–å?ç?
089            static {
090                    Cleanable clr = new Cleanable() {
091                            public void clear() {
092                                    CalendarFactory.clear();
093                            }
094                    };
095    
096                    SystemManager.addCleanable( clr );
097    
098                    /** コãƒã‚¯ã‚·ãƒ§ãƒ³ã«ã‚¢ãƒ—リケーションæƒ??を追記ã™ã‚‹ã‹ã©ã?‹æŒ?®?*/
099                    boolean USE_DB_APPLICATION_INFO  = HybsSystem.sysBool( "USE_DB_APPLICATION_INFO" ) ;
100    
101                    // 3.8.7.0 (2006/12/15) アクセスログå–å¾—ã?為,ApplicationInfoオブジェクトを設å®?
102                    if( USE_DB_APPLICATION_INFO ) {
103                            String SYSTEM_ID = HybsSystem.sys( "SYSTEM_ID" );
104                            appInfo = new ApplicationInfo();
105                            // ユーザーID,IPアドレス,ホストå
106                            appInfo.setClientInfo( SYSTEM_ID,HybsSystem.HOST_ADRS,HybsSystem.HOST_NAME );
107                            // ç”»é¢ID,æ“ä½?プログラãƒ?D
108                            appInfo.setModuleInfo( "CalendarFactory",null,null );
109                    }
110                    else {
111                            appInfo = null;
112                    }
113            }
114    
115            /**
116             * コンストラクタ
117             * ファクトリクラスã®ç‚ºã€private 化ã—ã¦ã?¾ã™ã?
118             *
119             */
120            private CalendarFactory() { }
121    
122            /**
123             * CalendarData オブジェクトをå–å¾—ã—ã¾ã™ã?
124             * 作æ?ã—ãŸCalendarDataオブジェクトã??Œå?部ã«ãƒ—ã?ルã—ã¦ãŠã?ŒåŒã˜ãƒªã‚½ãƒ¼ã‚¹è¦æ±‚ãŒ
125             * ã‚ã£ãŸã¨ãã??Œã?ール㮠CalendarDataã‚’è¿”ã—ã¾ã™ã?
126             * ?¤?¢ã«ãƒ??ã‚¿ãŒå­˜åœ¨ã—ãªã??åˆã?ã€è?å‹•ã§ã‚«ãƒ¬ãƒ³ãƒ?‚’作æ?ã—ã¾ã™ã?
127             *
128             * @og.rev 3.8.7.0 (2006/12/15) アクセスログå–å¾—ã?為,ApplicationInfoオブジェクトを設å®?
129             * @og.rev 4.0.0.0 (2007/08/29) カレンãƒ??ãƒ??ブルãŒå­˜åœ¨ã—ãªã??åˆã?ãƒ?ƒ•ォルトã?カレンãƒ??ãƒ??タをシスãƒ?ƒ ãƒªã‚½ãƒ¼ã‚¹ã§è¨­å®šã™ã‚?
130             *
131             * @param       cls     CalendarQueryオブジェクトをå称を指定ã—ã¾ã™ã?
132             * @param       arg1    ãƒ??タベã?ス検索時ã?第?‘引数
133             * @param       arg2    ãƒ??タベã?ス検索時ã?第?’引数
134             * @param       arg3    ãƒ??タベã?ス検索時ã?第?“引数
135             * @param       arg4    ãƒ??タベã?ス検索時ã?第?”引数
136             *
137             * @return      CalendarDataオブジェク�
138             */
139            public static CalendarData getCalendarData( final String cls,final String arg1,final String arg2,final String arg3,final String arg4 ) {
140    
141                    // 4.0.0.0 PGカレンãƒ??ã®åˆæœŸè¨­å®šã‚’シスãƒ?ƒ ãƒªã‚½ãƒ¼ã‚¹ã«å¤‰æ›´
142                    synchronized( lock ) {
143                            if(pgCalData == null) {
144                                    pgCalData = (CalendarData)HybsSystem.newInstance(PG_CALENDAR_DATA_CLASS);
145                            }
146                    }
147    
148    //              if( ! useDB || cls == null ) { return PG_CALENDAR_DATA; }
149                    if( ! useDB || cls == null ) { return pgCalData; }
150    
151                    CalendarData calData ;
152    
153                    String key = cls + ":" + arg1 + ":" + arg2 + ":" + arg3 + ":" + arg4 ;
154                    synchronized( lock ) {
155                            calData = pool.get( key ) ;
156                    }
157    
158                    if( calData == null ) {
159                            CalendarQuery query ;
160                            synchronized( lock ) {
161                                    query = queryClassPool.get( cls );
162                                    if( query == null ) {
163                                            String queryClass = HybsSystem.sys( "CalendarQuery_" + cls );
164                                            query = (CalendarQuery)HybsSystem.newInstance( queryClass );
165                                            queryClassPool.put( cls,query );
166                                    }
167                            }
168    
169                            String[] args = query.checkArgment( arg1,arg2,arg3,arg4 );
170                            String[][] vals = DBUtil.dbExecute( query.getQuery(),args,appInfo,DBID );       // 3.8.7.0 (2006/12/15)
171                            boolean  isFlat = query.isFlatTable();
172    
173                            if( vals != null && vals.length > 0 ) {
174                                    calData = new CalendarDBData( vals,isFlat );
175                                    // 完å?åŒæœŸã§ã¯ãªã??DB処ç?¸­ã«åˆ¥ã«ã‚?B処ç?‚’行ã„ã€?
176                                    // 先㫠put ã•れãŸã¨ã—ã¦ã‚‚ã?åŒä¸?‚­ãƒ¼ã«åŒã˜å±žæ?ã®ã‚ªãƒ–ジェクトを
177                                    // 登録ã™ã‚‹ã?‘ãªã®ã§ã€å®Ÿè³ªçš?ªå•題ã?発生ã—ãªã??
178                                    // ã“れãŒã?List ãªã©ã®é ?ºãŒé–¢ä¿‚ã™ã‚‹ã¨ã€ã“ã®ã‚³ãƒ¼ãƒ‰ã?使ãˆãªã??
179                                    synchronized( lock ) {
180                                            pool.put( key,calData );
181                                    }
182                            }
183                            else {
184                                    // カレンãƒ?ƒ†ãƒ¼ãƒ–ルã«ãƒ??ã‚¿ãŒå­˜åœ¨ã—ãªã??åˆã?
185                                    // 4.0.0.0 PGカレンãƒ??ã®åˆæœŸè¨­å®šã‚’シスãƒ?ƒ ãƒªã‚½ãƒ¼ã‚¹ã«å¤‰æ›´
186    //                              calData = PG_CALENDAR_DATA;
187                                    calData = pgCalData;
188                            }
189                    }
190                    return calData ;
191            }
192    
193            /**
194             * キャãƒ?‚·ãƒ¥(プã?ル)ã‹ã‚‰ã€ã™ã¹ã¦ã®ã‚ªãƒ–ジェクトをクリアã—ã¾ã™ã?
195             *
196             */
197            public static void clear() {
198                    synchronized( lock ) {
199                            pool.clear();
200                            queryClassPool.clear();
201                            useDB = HybsSystem.sysBool( "USE_CALENDAR_DATABASE" );
202                    }
203            }
204    }