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.html;
017    
018    import org.opengion.hayabusa.common.HybsSystem;
019    import org.opengion.hayabusa.db.DBTableModel;
020    import org.opengion.fukurou.security.URLHashMap;
021    import org.opengion.fukurou.util.StringUtil;
022    import org.opengion.fukurou.util.Attributes;
023    import org.opengion.fukurou.util.XHTMLTag;
024    import org.opengion.fukurou.model.Formatter;
025    
026    import java.util.Map;
027    import java.util.HashMap;
028    import java.util.List;
029    import java.util.ArrayList;
030    import java.util.Arrays ;
031    
032    /**
033     * ViewLink インターフェース の実è£?‚ªブジェクトですã?
034     * これã‚?共通ã?スーパã?クラスとして å�?¨®表示フォーãƒ?例:HTML表示ç­?に使ã�?�¾すã?
035     *
036     * こã?クラスは、setter/getterメソãƒ?ƒ‰のãƒ?ƒ•ォルト実è£?‚’提供してã�?�¾すã?
037     * å�?¨®表示フォーãƒ?�«対応したサブクラス上で, create() をオーバã?ライドして下さã�??
038     *
039     * @og.rev 2.1.0.3 (2002/11/08) エンコードã?開å§?終äº?‚¢ドレスを求めるå?ç�??修正
040     * @og.rev 4.0.0.0 (2005/08/31) 同ä¸?‚«ラãƒ??è¤?•°登録を許可しますã?
041     * @og.group 画面表示
042     *
043     * @version  4.0
044     * @author   Kazuhiko Hasegawa
045     * @since    JDK5.0,
046     */
047    public class ViewLink_LINK implements ViewMarker {
048            private static final String REQ_KEY = HybsSystem.URL_HASH_REQ_KEY ;
049    
050            private static final int ACCS_LVL = HybsSystem.sysInt( "URL_ACCESS_SECURITY_LEVEL" );
051    
052            private List<Attributes>          markData        = null;         // 4.0.0 (2005/08/31)
053    //      private Map<Integer,int[]>                formMap_c       = new HashMap<Integer,int[]>();   // 3.5.6.1 (2004/06/25)
054    //      private Map<Integer,String[]>     formMap_f       = new HashMap<Integer,String[]>();        // 3.5.6.1 (2004/06/25)
055            private Map<Integer,Formatter>    formMap         = new HashMap<Integer,Formatter>();
056            private DBTableModel            table       = null;
057            private int[]                           markCmlNo       = null;
058            private int[]                           isMark          = null;
059            // 2.1.0.3 (2002/11/08) エンコードã?開å§?終äº?‚¢ドレスを求めるå?ç�??修正
060            private int[]               encodeIn    = null;         // 初期値:ç¯?›²å¤?
061            private int[]               encodeOut   = null;         // 初期値:ç¯?›²å¤?
062            private static final int        MARK_NULL   = -1;               // リンク未設å®?
063            private static final int        MARK_TRUE   = 1;                // リンク作æ?
064            private static final int        MARK_FALSE  = 0;                // リンク作æ?せず
065            // 3.5.2.0 (2003/10/20)
066            private String[]                        markKey         = null;
067            private String[]                        markLists       = null;
068            private int[]                           markListNo      = null;
069    
070            private boolean[]                       useURLCheck     = null; // 4.3.7.1 (2009/06/08)
071            private String[]                        urlCheckUser= null;     // 4.3.7.1 (2009/06/08)
072            private long[]                          urlCheckTime= null;     // 4.3.7.1 (2009/06/08)
073    
074            private Map<Integer,List<Integer>>  clmMap  = new HashMap<Integer,List<Integer>>();     // 4.0.0 (2005/08/31)
075    
076            /**
077             * å†?®¹をクリア(初期åŒ?しますã?
078             *
079             * @og.rev 3.1.1.0 (2003/03/28) 同期メソãƒ?ƒ‰(synchronized付き)を非同期に変更するã€?
080             * @og.rev 3.5.2.0 (2003/10/20) markLists,markListNo,markKey属æ?を追åŠ?
081             * @og.rev 3.5.6.1 (2004/06/25) formMap属æ?を追åŠ?
082             * @og.rev 4.3.7.1 (2009/06/08) URLチェãƒ?‚¯属æ?追åŠ?
083             */
084            public void clear() {
085                    markData        = null;         // 4.0.0 (2005/08/31)
086    //              formMap_c       = new HashMap<Integer,int[]>();           // 3.5.6.1 (2004/06/25)
087    //              formMap_f       = new HashMap<Integer,String[]>();        // 3.5.6.1 (2004/06/25)
088                    formMap         = new HashMap<Integer,Formatter>();
089                    table       = null;
090                    isMark      = null;
091                    encodeIn    = null;
092                    encodeOut   = null;
093                    markKey         = null;
094                    markLists       = null;
095                    markListNo      = null;
096                    clmMap          = new HashMap<Integer,List<Integer>>();     // 4.0.0 (2005/08/31)
097                    useURLCheck = null; // 4.3.7.1 (2009/06/08)
098                    urlCheckUser= null; // 4.3.7.1 (2009/06/08)
099                    urlCheckTime= null; // 4.3.7.1 (2009/06/08)
100            }
101    
102            /**
103             * カラãƒ?�«対するリンクアトリビュートをセãƒ?ƒˆしますã?
104             *
105             * @og.rev 3.1.0.0 (2003/03/20) Hashtable を使用してã�?‚‹ç®?‰€でã€?�ž同期でも構わなã�?®?‰€をã?HashMap に置換えã€?
106             * @og.rev 3.1.1.0 (2003/03/28) 同期メソãƒ?ƒ‰(synchronized付き)を非同期に変更するã€?
107             * @og.rev 4.0.0.0 (2005/08/31) 同ä¸?‚«ラãƒ??è¤?•°登録を許可しますã?
108             *
109             * @param       attri   リンクアトリビューãƒ?
110             */
111            public void addAttribute( final Attributes attri ) {
112                    if( markData == null ) { markData = new ArrayList<Attributes>(); }
113                    markData.add( attri );
114            }
115    
116            /**
117             * å†?ƒ¨に DBTableModel をセãƒ?ƒˆしますã?
118             *
119             * @og.rev 2.1.0.3 (2002/11/08) エンコードã?開å§?終äº?‚¢ドレスを求めるå?ç�??修正
120             * @og.rev 3.1.1.0 (2003/03/28) 同期メソãƒ?ƒ‰(synchronized付き)を非同期に変更するã€?
121             * @og.rev 3.5.2.0 (2003/10/20) markLists,markListNo,markKey属æ?を追åŠ?
122             * @og.rev 3.5.5.0 (2004/03/12) xlink 属æ?によるリンクæƒ??作æ?方法ã?åˆ?²�を追åŠ?
123             * @og.rev 3.5.6.1 (2004/06/25) DBTableModel の再設定に対応ã?
124             * @og.rev 3.5.6.2 (2004/07/05) linkFormat をパラメータで取得するよã�?�«変更ã€?
125             * @og.rev 3.8.1.1 (2005/11/21) linkFormat ã�?"[","]" をエンコードしてしまったå?合にå…?�«戻しますã?
126             * @og.rev 4.0.0.0 (2005/08/31) 同ä¸?‚«ラãƒ??è¤?•°登録を許可しますã?
127             * @og.rev 4.3.7.1 (2009/06/08) URLチェãƒ?‚¯機è?追åŠ?
128             *
129             * @param  tbl DBTableModelオブジェクãƒ?
130             */
131            public void setDBTableModel( final DBTableModel tbl ) {
132                    table   = tbl;
133    //              String linkFormat       = null;
134                    int     count           = markData.size();                      // 4.0.0 (2005/08/31)
135    
136                    isMark          = new int[ count ];
137                    markKey         = new String[ count ];
138                    markCmlNo       = new int[ count ];
139                    markLists       = new String[ count ];
140                    markListNo      = new int[ count ];
141                    encodeIn    = new int[ count ];
142                    encodeOut   = new int[ count ];
143                    useURLCheck = new boolean[ count ];                             // 4.3.7.1 (2009/06/08)
144                    urlCheckUser= new String[ count ];                              // 4.3.7.1 (2009/06/08)
145                    urlCheckTime= new long[ count ];                                // 4.3.7.1 (2009/06/08)
146    
147                    Arrays.fill( isMark,MARK_FALSE );       // リンクの表示可否
148                    Arrays.fill( markCmlNo,-1 );            // リンクの可否を判断するカラãƒ?•ª号
149                    Arrays.fill( encodeIn ,10000 );         // 初期値:ç¯?›²å¤?
150                    Arrays.fill( encodeOut,-1 );            // 初期値:ç¯?›²å¤?
151                    Arrays.fill( useURLCheck, false );      // 4.3.7.1 (2009/06/08)
152                    Arrays.fill( urlCheckTime, 0L );        // 4.3.7.1 (2009/06/08)
153    
154                    // 4.0.0 (2005/08/31) 同ä¸?‚«ラãƒ??è¤?•°登録を許可しますã?
155                    for( int intKey=0; intKey<count; intKey++ ) {
156                            Attributes attri = markData.get( intKey );
157    
158                            String column = attri.get( "column" );
159                            int clm = table.getColumnNo( column );
160                            List<Integer> list = clmMap.get( clm );
161                            if( list == null ) { list = new ArrayList<Integer>(); }
162                            list.add( intKey );
163                            clmMap.put( clm,list );
164    
165                            String linkFormat = attri.get( "linkFormat" );
166                            linkFormat = StringUtil.replace( linkFormat,"%5B","[" );        // 3.8.1.1 (2005/11/21)
167                            linkFormat = StringUtil.replace( linkFormat,"%5D","]" );        // 3.8.1.1 (2005/11/21)
168    
169    //                      makeFormat( intKey,linkFormat );
170                            Formatter formatter = new Formatter( table );
171                            formatter.setFormat( linkFormat );
172                            formMap.put( intKey, formatter );
173    
174                            // URLエンコード用のç¯?›²設定ã?こã?ç¯?›²å†??ãƒ??タをURLエンコードするã?
175                            String[] format = formatter.getFormat();
176                            boolean findHref = false;
177                            for( int j=0; j<format.length; j++ ) {
178                                    if( format[j] != null && format[j].indexOf( "href" ) >= 0 ) { findHref = true; }
179                                    if( findHref && format[j].indexOf( '?' ) >= 0   ) { encodeIn[intKey]  = j; }
180                                    if( findHref && format[j].indexOf( "\" " ) >= 0 ) { encodeOut[intKey] = j; findHref = false; }
181                            }
182    
183                            // 4.3.7.1 (2009/06/08)
184                            useURLCheck[intKey] = StringUtil.nval( attri.get( "useURLCheck" ), false );
185                            urlCheckUser[intKey] = StringUtil.nval( attri.get( "urlCheckUser" ), null );
186                            urlCheckTime[intKey] = StringUtil.nval( attri.get( "urlCheckTime" ), 0L );
187    
188                            makeOnLinkFormat( intKey,attri );
189                    }
190            }
191    
192            /**
193             * æŒ?®šã?行å?に対するマã?カーæ–?­—å?を返しますã?
194             * こã?値は,すでにマã?カーæ–?­—å?処ç�?�•れてã�?‚‹為, RendererValue で
195             * 変換するå¿?¦�ã?ありませんã€?
196             * 引数の value はそã?カラãƒ??値として利用されますã?こã?値は、修飾済みの
197             * 値を与えることが可能ですã?
198             *
199             * @og.rev 2.1.0.3 (2002/11/08) エンコードã?開å§?終äº?‚¢ドレスを求めるå?ç�??修正
200             * @og.rev 3.0.0.0 (2002/12/25) URLEncoder.encode ã‚?StringUtil#urlEncode に置換え
201             * @og.rev 3.0.0.1 (2003/02/14) リンクの引数部åˆ?�«、RendererValue が適用されるç®?‰€を修正
202             * @og.rev 3.0.0.1 (2003/02/14) リンクの引数部åˆ?�«、RendererValue が適用されるç®?‰€を修正
203             * @og.rev 3.5.6.1 (2004/06/25) formMap属æ?を使用しますã?
204             * @og.rev 3.7.0.3 (2005/03/01) "{I}" æ–?­—å?に、行番号(row)を割り当てますã?
205             * @og.rev 3.8.5.0 (2006/03/20) "{I}" â‡?"%7BI%7D" として、行番号(row)を割り当てますã?
206             * @og.rev 4.3.7.1 (2009/06/08) URLチェãƒ?‚¯機è?追åŠ?
207             * @og.rev 4.3.7.4 (2009/07/01) 循環参ç?を解æ¶?
208             * @og.rev 5.2.3.0 (2010/12/01) URLのハッシュåŒ?暗号化を行いますã?
209             *
210             * @param   row æŒ?®šã?è¡?
211             * @param   clm æŒ?®šã?åˆ?
212             * @param   value カラãƒ??値
213             *
214             * @return  row行,columåˆ?のマã?カーæ–?­—å?
215             */
216            public String getMarkerString( final int row,final int clm,final String value ) {
217                    int intKey = isOnLink(row,clm) ;
218                    if( intKey < 0 ) { return value; }
219    
220                    Formatter formatter = formMap.get( intKey );
221                    int[]    clmNo  = formatter.getClmNos();
222                    String[] format = formatter.getFormat();
223    
224    //              int[] clmNo = formMap_c.get( intKey );
225    //              String[] format = formMap_f.get( intKey );
226    
227                    StringBuilder strLink = new StringBuilder( HybsSystem.BUFFER_LARGE );
228                    int j=0;
229                    String val ;
230                    for( ; j<clmNo.length; j++ ) {
231                            strLink.append( format[j] );
232                            if( encodeIn[intKey] <= j && j < encodeOut[intKey] ) {
233    //                              val = table.getValue(row,clmNo[j]);
234                                    val = formatter.getValue(row,clmNo[j]);
235                                    strLink.append( StringUtil.urlEncode( val ) );
236                            }
237                            else if( clm == clmNo[j] ) {
238                                    strLink.append( value );
239                            }
240                            else {
241    //                              val = table.getValue(row,clmNo[j]);
242                                    val = formatter.getValue(row,clmNo[j]);
243                                    strLink.append( val );
244                            }
245                    }
246                    strLink.append( format[j] );
247    
248                    // 3.8.5.0 (2006/03/27) "{I}" と そã?エンコード文å­?"%7BI%7D" に、行番号(row)を割り当てますã?
249                    String rtn = strLink.toString();
250                    String sRow = String.valueOf( row );
251                    rtn = StringUtil.replace( rtn,"{I}",sRow );
252                    rtn = StringUtil.replace( rtn,"%7BI%7D",sRow );
253    
254                    // 4.3.7.1 (2009/06/08)
255                    if( useURLCheck[intKey] ) {
256                            // 4.3.7.4 (2009/07/01)
257                            rtn = XHTMLTag.embedURLCheckKey( rtn, HybsSystem.URL_CHECK_KEY, urlCheckUser[intKey], urlCheckTime[intKey] );
258                    }
259    
260                    // 5.2.3.0 (2010/12/01) URLのハッシュåŒ?暗号åŒ?
261                    if( ACCS_LVL == 2 ) {
262                            // ACCS_LVL == 2 の場合ã?、外部のみ処ç�?�™るã?で、extOnly=true をセãƒ?ƒˆするã€?
263                            rtn = URLHashMap.makeUrlChange( rtn,REQ_KEY,true );
264                    }
265                    else if( ACCS_LVL == 3 ) {
266                            rtn = URLHashMap.makeUrlChange( rtn,REQ_KEY,false );
267                    }
268    
269                    return rtn ;
270            }
271    
272            /**
273             * リンクフォーマットを作æ?しますã?
274             *
275             * @og.rev 2.1.0.3 (2002/11/08) エンコードã?開å§?終äº?‚¢ドレスを求めるå?ç�??修正
276             * @og.rev 3.5.6.1 (2004/06/25) formMap属æ?を使用しますã?
277             *
278             * @param       intKey  カラãƒ?‚­ーの番号
279             * @param       fmt     フォーマット文字å?
280             */
281    //      private void makeFormat( final int intKey,final String fmt ) {
282    //              boolean findHref = false;
283    //
284    //              StringTokenizer token = new StringTokenizer( fmt,"[]" );
285    //              int count = token.countTokens() / 2 ;
286    //              int[] clmNo = new int[ count ];
287    //              String[] format      = new String[ count+1 ];
288    //              for( int j=0; j<count; j++ ) {
289    //                      format[j] = token.nextToken();
290    //                      clmNo[j]  = table.getColumnNo( token.nextToken() );
291    //
292    //                      // URLエンコード用のç¯?›²設定ã?こã?ç¯?›²å†??ãƒ??タをURLエンコードするã?
293    //                      if( format[j] != null && format[j].indexOf( "href" ) >= 0 ) { findHref = true; }
294    //                      if( findHref && format[j].indexOf( '?' ) >= 0   ) { encodeIn[intKey]  = j; }
295    //                      if( findHref && format[j].indexOf( "\" " ) >= 0 ) { encodeOut[intKey] = j; findHref = false; }
296    //              }
297    //              format[count] = token.nextToken();
298    //              formMap_c.put( intKey, clmNo );
299    //              formMap_f.put( intKey, format );
300    //
301    //              if( format[count] != null && format[count].indexOf( "href" ) >= 0 ) { findHref = true; }
302    //              if( findHref && format[count].indexOf( '?' ) >= 0 )   { encodeIn[intKey]  = count; }
303    //              if( findHref && format[count].indexOf( "\" " ) >= 0 ) { encodeOut[intKey] = count; }
304    //      }
305    
306            /**
307             * リンクを張ã‚?張らなã�??æŒ?®šカラãƒ?•ª号を求めますã?
308             * またã?int[列番号] isMark をå?期化しますã?
309             *
310             * @og.rev 3.5.2.0 (2003/10/20) markLists,markListNo,markKey属æ?を追åŠ?
311             *
312             * @param       intKey  カラãƒ?‚­ーの番号
313             * @param       attri   アトリビューãƒ?
314             */
315            private void makeOnLinkFormat( final int intKey,final Attributes attri ) {
316                    String onMark   = attri.get( "onLink" );
317                    String markList = attri.get( "markList" );
318    
319                    // 3.5.6.0 (2004/06/18) nullポインタの参ç?外しバグの対å¿?
320                    // こã?ロジãƒ?‚¯で値が設定済みであれば、以下ã?処ç�??不要であるã€?
321                    isMark[intKey] = MARK_NULL;
322                    if( onMark == null || onMark.length() == 0 ||
323                            markList == null || markList.length() == 0 ) {
324                                    isMark[intKey] = MARK_FALSE;
325                                    return ;        // 3.5.6.0 (2004/06/18)
326                    }
327                    else if( onMark.charAt( 0 ) != '[' && markList.charAt( 0 ) != '[' ) {
328                            isMark[intKey] = ( markList.indexOf( onMark ) >= 0 ) ? MARK_TRUE : MARK_FALSE;
329                            return ;        // 3.5.6.0 (2004/06/18)
330                    }
331    
332                    if( onMark.charAt( 0 ) == '[' ) {
333                            markCmlNo[intKey] = table.getColumnNo( onMark.substring( 1,onMark.length()-1 ));
334                    }
335                    else {
336                            markCmlNo[intKey]  = -1;
337                            markKey[intKey]    = onMark ;
338                    }
339    
340                    if( markList.charAt( 0 ) == '[' ) {
341                            markListNo[intKey] = table.getColumnNo( markList.substring( 1,markList.length()-1 ));
342                    }
343                    else {
344                            markListNo[intKey] = -1;
345                            markLists[intKey] = markList;
346                    }
347            }
348    
349            /**
350             * リンクを張るかどã�?�‹を判断しますã?
351             * int[列番号] isMark にはã€?未設å®?FALSE TRUE の状態を持っておりã€?
352             * 列でリンクを張る状態が固定ã?場å�?例えば,onLink属æ?がデフォルãƒ?"true" の場å�?
353             * カラãƒ?�«関係なくã?同じ値を返すときに、使用しますã?
354             *
355             * @og.rev 3.5.2.0 (2003/10/20) markLists,markListNo,markKey属æ?を追åŠ?
356             * @og.rev 3.5.4.0 (2003/11/25) onMark ,markList ã�?null(またã?ゼロストリング)の場合ã?、false とするã€?
357             * @og.rev 4.0.0.0 (2005/08/31) 同ä¸?‚«ラãƒ??è¤?•°登録を許可しますã?
358             *
359             * @param       row     列番号
360             * @param       clm     カラãƒ?‚­ーの名称
361             *
362             * @return      処ç�?�™るリスト番号ã€?1 の場合ã?、該当なã�?
363             */
364            private int isOnLink( final int row,final int clm ) {
365    
366                    List<Integer> list = clmMap.get( clm );
367                    if( list == null ) { return -1; }
368    
369                    for( int i=0; i<list.size(); i++ ) {
370                            int intKey = list.get( i );
371                            if( isMark[intKey] != MARK_NULL ) {
372                                    if( isMark[intKey] == MARK_TRUE ) { return intKey; }
373                                    else { continue; }
374                            }
375    
376                            String onMark ;
377                            if( markCmlNo[intKey] < 0 ) { onMark = markKey[intKey] ; }
378                            else { onMark = table.getValue( row,markCmlNo[intKey] ); }
379    
380                            // 3.5.4.0 (2003/11/25) 追åŠ?
381                            if( onMark == null || onMark.length() == 0 ) { continue; }
382    
383                            String markList ;
384                            if( markListNo[intKey] < 0 ) { markList = markLists[intKey] ; }
385                            else { markList = table.getValue( row,markListNo[intKey] ); }
386    
387                            // 3.5.4.0 (2003/11/25) 修正
388                            if( markList == null || markList.length() == 0 ) { continue; }
389    
390                            if( markList.indexOf( onMark ) >= 0 ) { return intKey; }
391                    }
392                    return -1;
393            }
394    }