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 */
016package org.opengion.plugin.column;
017
018import org.opengion.hayabusa.db.AbstractRenderer;
019import org.opengion.hayabusa.db.CellRenderer;
020import org.opengion.hayabusa.db.DBColumn;
021
022/**
023 * YM レンデラーは、カラムのデータを日付(年/月)表示する場合に使用するクラスです。
024 *
025 * このクラスは、不変オブジェクトとして、共有されます。
026 *
027 * @og.group データ表示
028 *
029 * @version  4.0
030 * @author   Kazuhiko Hasegawa
031 * @since    JDK5.0,
032 */
033public class Renderer_YM extends AbstractRenderer {
034        /** このプログラムのVERSION文字列を設定します。   {@value} */
035        private static final String VERSION = "6.4.2.0 (2016/01/29)" ;
036
037        private static final CellRenderer DB_CELL = new Renderer_YM() ;
038
039        /**
040         * デフォルトコンストラクター
041         *
042         * @og.rev 6.4.2.0 (2016/01/29) PMD refactoring. Each class should declare at least one constructor.
043         */
044        public Renderer_YM() { super(); }               // これも、自動的に呼ばれるが、空のメソッドを作成すると警告されるので、明示的にしておきます。
045
046        /**
047         * 各オブジェクトから自分のインスタンスを返します。
048         * 自分自身をキャッシュするのか、新たに作成するのかは、各サブクラスの実装に
049         * まかされます。
050         *
051         * @og.rev 3.1.1.1 (2003/04/03) 各オブジェクトから自分のインスタンスを返すファクトリメソッドを追加。
052         * @og.rev 3.1.2.1 (2003/04/10) synchronized を、削除します。
053         *
054         * @param       clm     DBColumnオブジェクト
055         *
056         * @return      CellRendererオブジェクト
057         * @og.rtnNotNull
058         */
059        public CellRenderer newInstance( final DBColumn clm ) {
060                return DB_CELL;
061        }
062
063        /**
064         * データの表示用文字列を返します。
065         * ここでは、null か ゼロ文字列の場合は、ゼロ文字列を返します。
066         * 6ケタ以下のデータは、エラー表示を行います。
067         * 6ケタ以上の場合は、YYYYMMxx として、YYYY/MM を返します。
068         *
069         * @og.rev 3.8.1.1 (2005/11/21) フォーマットエラー時には、class="error" を付けて表示させる。
070         * @og.rev 5.5.7.2 (2012/10/09) 処理ロジックの見直し
071         * @og.rev 6.0.4.0 (2014/11/28) ロジックの共通化
072         *
073         * @param   value 入力値
074         *
075         * @return  データの表示用文字列
076         * @og.rtnNotNull
077         */
078        @Override
079        public String getValue( final String value ) {
080                return getValue( value , true );
081
082        }
083
084        /**
085         * データ出力用の文字列を作成します。
086         * ファイル等に出力する形式を想定しますので、HTMLタグを含まない
087         * データを返します。
088         * 基本は、#getValue( String ) をそのまま返します。
089         *
090         * @og.rev 6.0.4.0 (2014/11/28) データ出力用のレンデラー
091         *
092         * @param   value 入力値
093         *
094         * @return  データ出力用の文字列
095         * @og.rtnNotNull
096         * @see         #getValue( String )
097         */
098        @Override
099        public String getWriteValue( final String value ) {
100                return getValue( value , false );
101        }
102
103        /**
104         * データ表示用/出力用の文字列を作成します。
105         * 第二引数の isView == true で、データ表示用文字列を、false で
106         * データ出力用の文字列を作成します。
107         * 処理の共通化を行うためのメソッドです。
108         *
109         * @og.rev 6.0.4.0 (2014/11/28) ロジックの共通化
110         *
111         * @param   value  入力値
112         * @param   isView データ表示用かどうか(true:表示用/false:出力用)
113         *
114         * @return  データ表示用/出力用の文字列
115         * @og.rtnNotNull
116         * @see         #getValue( String )
117         */
118        private String getValue( final String value , final boolean isView ) {
119
120                if( value == null || value.isEmpty() ) { return ""; }
121
122                String rtn = value;
123
124                if( rtn.length() >= 6 ) {
125                        char[] ch1 = new char[7];
126                        ch1[0] = rtn.charAt(0);
127                        ch1[1] = rtn.charAt(1);
128                        ch1[2] = rtn.charAt(2);
129                        ch1[3] = rtn.charAt(3);
130                        ch1[4] = '/' ;
131                        ch1[5] = rtn.charAt(4);
132                        ch1[6] = rtn.charAt(5);
133
134                        rtn = new String( ch1 );
135                }
136                else {
137                        if( isView ) {
138                                rtn = "<span class=\"error\">" + value + "</span>";
139                        }
140                }
141                return rtn;
142        }
143}