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    }