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.HybsSystemException;
020    
021    import org.opengion.fukurou.util.ApplicationInfo;
022    import org.opengion.fukurou.db.DBUtil;
023    
024    import java.util.Map;
025    import java.util.HashMap;
026    import java.util.LinkedHashMap;
027    
028    /**
029     * ユーザーアクセス画面管?ーブルを維持する為のクラスです?
030     * @og.group リソース管?
031     *
032     * @version  4.1.1.0
033     * @author   Sen.Li
034     * @since    JDK5.0,
035     */
036    public final class UserAccessTable {
037            // 5.3.0.0 (2010/12/01) 画面アクセスの履歴(?)を管?る機?を追?
038    //      private static final String GEA09_QUERY  = "SELECT GUIKEY,SEQNO,NAME_JA,LAST_ACCESS,FGFAVORITE,CLASSIFY FROM GEA09"
039    //                                                                      + " WHERE SYSTEM_ID=? AND USERID=?"
040    //                                                                      + " ORDER BY SEQNO,CLASSIFY,GUIKEY";
041            private static final String GEA09_QUERY  = "SELECT GUIKEY,SEQNO,NAME_JA,LAST_ACCESS,FGFAVORITE,CLASSIFY,NEXTGUI FROM GEA09"
042                                                                            + " WHERE SYSTEM_ID=? AND USERID=?"
043                                                                            + " ORDER BY SEQNO,CLASSIFY,GUIKEY";
044            private static final String GEA09_DELETE = "DELETE FROM GEA09"
045                                                                            + " WHERE SYSTEM_ID=? AND USERID=? AND GUIKEY=?";
046            private static final String GEA09_INSERT = "INSERT INTO GEA09(SYSTEM_ID,USERID,GUIKEY,SEQNO,NAME_JA,CLASSIFY,FGJ"
047                                                                            + ",DYSET,USRSET,PGUPD) VALUES(?,?,?,?,?,?,?,?,?,?)";
048            // 5.3.0.0 (2010/12/01) 画面アクセスの履歴(?)を管?る機?を追?
049    //      private static final String GEA09_UPDATE = "UPDATE GEA09 SET LAST_ACCESS=?"
050    //                                                                      + " WHERE SYSTEM_ID=? AND USERID=? AND GUIKEY=?";
051            private static final String GEA09_UPDATE = "UPDATE GEA09 SET LAST_ACCESS=?,NEXTGUI=?"
052                                                                            + " WHERE SYSTEM_ID=? AND USERID=? AND GUIKEY=?";
053    
054            private static final String DBID = HybsSystem.sys( "RESOURCE_DBID" );
055            private static final int GEA09_GUIKEY           = 0;
056            private static final int GEA09_SEQNO            = 1;
057            private static final int GEA09_NAME_JA          = 2;
058            private static final int GEA09_LAST_ACCESS      = 3;
059            private static final int GEA09_FGFAVORITE       = 4;
060            private static final int GEA09_CLASSIFY         = 5;
061            private static final int GEA09_NEXTGUI          = 6;    // 5.3.0.0 (2010/12/01) 追?
062            private static final String  FGFAVORITE_ON      = "1";
063            private static enum FgKeys { GUIMAP_ONLY,GEA09_ONLY };
064    
065            /**
066             * コンストラクター
067             * オブジェクトを作?できな??、privateにします?
068             *
069             * @og.rev 4.3.1.1 (2008/08/23) コンストラクタは、void 宣?付けません?
070             */
071    //      private void UserAccessTable() { }
072            private UserAccessTable() { }
073    
074            /**
075             * ユーザーアクセス画面管?ーブルを整?ます?
076             * こ?メソ?では guiMap へのセ?をして??副作用の扱??
077             *
078             * @og.rev 4.1.1.0 (2008/01/30) 新規追?
079             * @og.rev 5.3.0.0 (2010/12/01) 画面アクセスの履歴(?)を管?る機?を追?
080             *
081             * @param  guiMap               画面オブジェクト???への参?
082             * @param  systemId             シス?ID
083             * @param  userId               ユーザーID
084             * @param  lang                 ??
085             *
086             * @return      お気に入り???
087             */
088            public static Map<String,FavoriteGUIData> makeAccessDB(
089                                                            final Map<String,GUIInfo> guiMap,final String systemId,final String userId,final String lang ) {
090                    String today                    = HybsSystem.getDate( "yyyyMMddHHmmss" );
091                    Map<String,FgKeys>        conGuiKey = new HashMap<String,FgKeys>();
092                    /** コネクションにアプリケーション??を追記するかど???*/
093                    boolean USE_DB_APPLICATION_INFO  = HybsSystem.sysBool( "USE_DB_APPLICATION_INFO" ) ;
094                    ApplicationInfo appInfo = null;
095                    if( USE_DB_APPLICATION_INFO ) {
096                            appInfo = new ApplicationInfo();
097                            // ユーザーID,IPアドレス,ホスト名
098                            appInfo.setClientInfo( systemId,HybsSystem.HOST_ADRS,HybsSystem.HOST_NAME );
099                            // 画面ID,操?プログラ?D
100                            appInfo.setModuleInfo( "UserAccessTable",userId,"makeAccessDB" );
101                    }
102    
103                    // guiMapのキーで、新マップを作??
104                    for( String conKey : guiMap.keySet() ) {
105                            conGuiKey.put( conKey,FgKeys.GUIMAP_ONLY );
106                    }
107    
108                    Map<String,FavoriteGUIData> favoriteGuiMap = new LinkedHashMap<String,FavoriteGUIData>();
109                    // 4.1.1.0(2008/01/22)ユーザーアクセスの日付を取得し、accessPastDays 前?日付を計算する?
110                    String accessPastDays   = HybsSystem.sys( "ACCESS_TOKEI_PAST_DAYS" );
111                    int    diffDate                 = ( accessPastDays == null ) ? 0 : -Integer.parseInt( accessPastDays );
112                    String judgeTime                = HybsSystem.getDate( today.substring( 0,8 ),diffDate );
113                    String lastAccessTime   = null;
114                    String key                              = null;
115                    String[]   args                 = new String[] { systemId,userId };
116                    String[][] vals                 = DBUtil.dbExecute( GEA09_QUERY,args,appInfo,DBID );
117                    int len                                 = vals.length;
118    
119                    for( int i=0; i<len; i++ ) {
120                            key = vals[i][GEA09_GUIKEY];
121                            GUIInfo gui = guiMap.get( key );
122                            if( gui == null ) {
123                                    conGuiKey.put( key,FgKeys.GEA09_ONLY );
124                            }
125                            else {
126                                    // 5.3.0.0 (2010/12/01) 画面アクセスの履歴(?)を管?る機?を追?
127                                    String nxtgui = vals[i][GEA09_NEXTGUI];
128                                    if( nxtgui != null ) {
129                                            String[] keys = nxtgui.split( "," );
130                                            for( String nextKey : keys ) {
131                                                    gui.setNextGuiKey( nextKey );           // DB から復活
132                                            }
133                                    }
134    
135                                    conGuiKey.remove( key );
136                                    // 4.1.1.0(2008/01/22)お気に入り???の作?
137                                    if( FGFAVORITE_ON.equals( vals[i][GEA09_FGFAVORITE] ) && gui.isRead() ) {
138                                            favoriteGuiMap.put( key,new FavoriteGUIData( vals[i][GEA09_GUIKEY],vals[i][GEA09_SEQNO]
139                                                    ,vals[i][GEA09_NAME_JA],vals[i][GEA09_CLASSIFY] ) );
140                                    }
141                                    // 4.1.1.0(2008/01/22)ACCESS_TOKEI_PAST_DAYSに定義された期間以??画面を?上する?
142                                    lastAccessTime = vals[i][GEA09_LAST_ACCESS];
143                                    if( lastAccessTime.length() >= 8 ) {
144                                            if( lastAccessTime.compareTo( judgeTime ) >=0 ) {
145                                                    gui.setLevelUp();
146                                            }
147                                    }
148                            }
149                    }
150    
151                    ResourceManager resource = ResourceFactory.newInstance( lang );
152                    String undefined = resource.getLabel( "UNDEFINED" );
153                    FgKeys fgKey = null;
154                    for( String conKey : conGuiKey.keySet() ) {
155                            fgKey = conGuiKey.get( conKey );
156                            switch ( fgKey ) {
157                                    case GUIMAP_ONLY:
158                                            args = new String[] { systemId,userId,conKey,"9999999",guiMap.get(conKey).getLabel()
159                                                                                      ,undefined,"1",today,userId,"UsrAccsTbl" };
160                            DBUtil.dbExecute( GEA09_INSERT,args,appInfo,DBID );
161                                            break;
162                                    case GEA09_ONLY:
163                                            args = new String[] { systemId,userId,conKey };
164                                            DBUtil.dbExecute( GEA09_DELETE,args,appInfo,DBID );
165                                            break;
166                                    default:
167                                            String errMsg = "guiMapとGEA09の突合せフラグ?GUIMAP_ONLY と GEA09_ONLY 以外?値がセ?されて?す?"
168                                                                      + "画面ID?? + conKey + " フラグ?? + fgKey;
169                                            throw new HybsSystemException( errMsg );
170                            }
171                    }
172                    return favoriteGuiMap;
173            }
174    
175            /**
176             * ユーザーアクセス画面管?ーブルの?アクセス時間を更新します?
177             *
178             * @og.rev 4.1.1.0 (2008/01/30) 新規追?
179             * @og.rev 5.3.0.0 (2010/12/01) 画面アクセスの履歴(?)を管?る機?を追?
180             *
181             * @param systemId                      シス?ID
182             * @param userId                        ユーザーID
183             * @param guiKey                        画面ID
184             * @param lastAccessTime        画面の?アクセス時間
185             * @param nextGuiKeys           次にアクセスして?画面IDのCSV??
186             */
187    //      public static void updateLastAccessTime( final String systemId,final String userId,final String guiKey,final String lastAccessTime ) {
188            public static void updateLastAccessTime( final String systemId,final String userId,final String guiKey,final String lastAccessTime,final String nextGuiKeys ) {
189                    /** コネクションにアプリケーション??を追記するかど???*/
190                    boolean USE_DB_APPLICATION_INFO  = HybsSystem.sysBool( "USE_DB_APPLICATION_INFO" ) ;
191                    ApplicationInfo appInfo = null;
192                    if( USE_DB_APPLICATION_INFO ) {
193                            appInfo = new ApplicationInfo();
194                            // ユーザーID,IPアドレス,ホスト名
195                            appInfo.setClientInfo( systemId,HybsSystem.HOST_ADRS,HybsSystem.HOST_NAME );
196                            // 画面ID,操?プログラ?D
197                            appInfo.setModuleInfo( "UserAccessTable",userId,"updateLastAccessTime" );
198                    }
199    
200                    // 5.3.0.0 (2010/12/01) 画面アクセスの履歴(?)を管?る機?を追?
201    //              String[] args = new String[] { lastAccessTime,systemId,userId,guiKey };
202                    String[] args = new String[] { lastAccessTime,nextGuiKeys,systemId,userId,guiKey };
203                    DBUtil.dbExecute( GEA09_UPDATE,args,appInfo,DBID );
204            }
205    }