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