Coverage Report - jp.co.y_net.amm.common.ChoiceDateElement
 
Classes in this File Line Coverage Branch Coverage Complexity
ChoiceDateElement
0%
0/68
0%
0/32
0
ChoiceDateElement$DropDownChoiceDate
0%
0/2
N/A
0
ChoiceDateElement$MyRenderer
0%
0/2
N/A
0
 
 1  
 package jp.co.y_net.amm.common;
 2  
 
 3  
 import java.io.Serializable;
 4  
 import java.util.ArrayList;
 5  
 import java.util.Calendar;
 6  
 import java.util.Collections;
 7  
 import java.util.List;
 8  
 
 9  
 import org.apache.commons.lang.StringUtils;
 10  
 import org.apache.wicket.markup.html.form.ChoiceRenderer;
 11  
 import org.apache.wicket.markup.html.form.DropDownChoice;
 12  
 import org.apache.wicket.model.Model;
 13  
 
 14  
 /**
 15  
  * 日付選択用ドロップダウンリストの要素、ユーティリティ
 16  
  * @author k.inaba
 17  
  */
 18  
 public class ChoiceDateElement implements Serializable {
 19  
         private static final long serialVersionUID = 1L;
 20  
 
 21  
         private String id;
 22  
         private String name;
 23  
 
 24  0
         public ChoiceDateElement(String id, String name) {
 25  0
                 this.id = id;
 26  0
                 this.name = name;
 27  0
         }
 28  
         public String getId() {
 29  0
                 return id;
 30  
         }
 31  
         public void setId(String id) {
 32  0
                 this.id = id;
 33  0
         }
 34  
         public String getName() {
 35  0
                 return name;
 36  
         }
 37  
         public void setName(String name) {
 38  0
                 this.name = name;
 39  0
         }
 40  
    
 41  
     
 42  
     /**
 43  
      * 日付選択用ドロップダウンリスト
 44  
      */
 45  
     public static class DropDownChoiceDate extends DropDownChoice<ChoiceDateElement> {
 46  
         public DropDownChoiceDate(String id, Model<ChoiceDateElement> model, List<ChoiceDateElement> choice) {
 47  0
             super(id, model, choice, new MyRenderer());
 48  0
         }
 49  
     }
 50  
     /**
 51  
      * 日付選択用ドロップダウンリストに使用するレンダラ
 52  
      */
 53  
     private static class MyRenderer extends ChoiceRenderer<ChoiceDateElement>{
 54  
         public MyRenderer() {
 55  0
             super("name","id");
 56  0
         }
 57  
     }
 58  
 
 59  
     public static String 日付取得(Model<ChoiceDateElement> y, Model<ChoiceDateElement> m, Model<ChoiceDateElement> d) {
 60  0
         return y.getObject().getId() + m.getObject().getId() + d.getObject().getId();
 61  
     }
 62  
     
 63  
     
 64  
     /**
 65  
      * 年の選択肢を作成する。
 66  
      * 原則として、現在の年を含む未来6年分の選択肢を作成するが、
 67  
      * 引数の fromdateValue、todateValue が含まれない場合は追加する。
 68  
      * @param fromdateValue 
 69  
      * @param todateValue 
 70  
      * @return
 71  
      */
 72  
     public static List<ChoiceDateElement> createChoiceY(String fromdateValue, String todateValue) {
 73  
 
 74  0
         List<ChoiceDateElement> choice = new ArrayList<ChoiceDateElement>();
 75  0
         choice.add(new ChoiceDateElement("0000", "未指定"));
 76  
 
 77  0
         Integer iNowYYYY = Calendar.getInstance().get(Calendar.YEAR);
 78  0
         List<Integer> list = new ArrayList<Integer>();
 79  0
         for (int i = 0; i < 6; i++) { // 現在から6年先までの選択肢を作成
 80  0
             Integer yyyy = iNowYYYY + i;
 81  0
             list.add(yyyy);
 82  
         }
 83  
 
 84  
         /* すでに登録されている値 */
 85  0
         Integer iFromYYYY = Integer.parseInt(getYYYY(fromdateValue));
 86  0
         Integer iToYYYY = Integer.parseInt(getYYYY(todateValue));
 87  0
         if (iFromYYYY != 0 && list.contains(iFromYYYY) == false) {
 88  0
             list.add(iFromYYYY);
 89  
         }
 90  0
         if (iToYYYY != 0 && list.contains(iToYYYY) == false) {
 91  0
             list.add(iToYYYY);
 92  
         }
 93  0
         Collections.sort(list);
 94  
 
 95  0
         for (Integer yyyy : list) {
 96  0
             choice.add(new ChoiceDateElement(String.valueOf(yyyy), yyyy + "年"));
 97  
         }
 98  
 
 99  0
         return choice;
 100  
     }
 101  
     /**
 102  
      * 月の選択肢を作成する
 103  
      * @return
 104  
      */
 105  
     public static List<ChoiceDateElement> createChoiceM() {
 106  0
         List<ChoiceDateElement> choice = new ArrayList<ChoiceDateElement>();
 107  0
         choice.add(new ChoiceDateElement("00", "--"));
 108  0
         for (int i = 1; i <= 12; i++) {
 109  0
             String str = StringUtils.leftPad(String.valueOf(i), 2, '0');
 110  0
             choice.add(new ChoiceDateElement(str, i + "月"));
 111  
         }
 112  0
         return choice;
 113  
     }
 114  
     /**
 115  
      * 日の選択肢を作成する
 116  
      * @return
 117  
      */
 118  
     public static List<ChoiceDateElement> createChoiceD() {
 119  0
         List<ChoiceDateElement> choice = new ArrayList<ChoiceDateElement>();
 120  0
         choice.add(new ChoiceDateElement("00", "--"));
 121  0
         for (int i = 1; i <= 31; i++) {
 122  0
             String str = StringUtils.leftPad(String.valueOf(i), 2, '0');
 123  0
             choice.add(new ChoiceDateElement(str, i + "日"));
 124  
         }
 125  0
         return choice;
 126  
     }
 127  
     
 128  
     /**
 129  
      * 年のモデルを作成する
 130  
      * 引数 choiceY のうち、targetDate と一致するものを初期値とする。
 131  
      * targetDateは yyyyMMdd で記述されているものとし、このメソッドでは、yyyy部分を参照する。
 132  
      * @param choiceY 
 133  
      * @param targetDate 
 134  
      * @return
 135  
      */
 136  
     public static Model<ChoiceDateElement> createModelY(List<ChoiceDateElement> choiceY, String targetDate) {
 137  0
         Model<ChoiceDateElement> model = new Model<ChoiceDateElement>();
 138  0
         select(choiceY, getYYYY(targetDate), model);
 139  0
         return model;
 140  
     }
 141  
     /**
 142  
      * 月のモデルを作成する
 143  
      * 引数 choiceY のうち、targetDate と一致するものを初期値とする。
 144  
      * targetDateは yyyyMMdd で記述されているものとし、このメソッドでは、MM部分を参照する。
 145  
      * @param choiceY 
 146  
      * @param targetDate 
 147  
      * @return
 148  
      */
 149  
     public static Model<ChoiceDateElement> createModelM(List<ChoiceDateElement> choice, String targetDate) {
 150  0
         Model<ChoiceDateElement> model = new Model<ChoiceDateElement>();
 151  0
         select(choice, getMM(targetDate), model);
 152  0
         return model;
 153  
     }
 154  
     /**
 155  
      * 月のモデルを作成する
 156  
      * 引数 choiceY のうち、targetDate と一致するものを初期値とする。
 157  
      * targetDateは yyyyMMdd で記述されているものとし、このメソッドでは、dd部分を参照する。
 158  
      * @param choiceY 
 159  
      * @param targetDate 
 160  
      * @return
 161  
      */
 162  
     public static Model<ChoiceDateElement> createModelD(List<ChoiceDateElement> choice, String targetDate) {
 163  0
         Model<ChoiceDateElement> model = new Model<ChoiceDateElement>();
 164  0
         select(choice, getDD(targetDate), model);
 165  0
         return model;
 166  
     }
 167  
     
 168  
     
 169  
     private static void select(List<ChoiceDateElement> choice, String target, Model<ChoiceDateElement> model) {
 170  0
         for (ChoiceDateElement c : choice) {
 171  0
             if (c.getId().equals(target)==true) {
 172  0
                 model.setObject(c);
 173  
             }
 174  
         }
 175  0
     }
 176  
     
 177  
     
 178  
     
 179  
     
 180  
     private static String getYYYY(String yyyyMMdd) {
 181  
         String str;
 182  0
         if(yyyyMMdd == null || yyyyMMdd.length() != 8) {
 183  0
             str = "0000";
 184  0
         } else {
 185  0
             str = yyyyMMdd.substring(0, 4);
 186  
         }
 187  0
         return str;
 188  
     }
 189  
     private static String getMM(String yyyyMMdd) {
 190  
         String str;
 191  0
         if(yyyyMMdd == null || yyyyMMdd.length() != 8) {
 192  0
             str = "00";
 193  0
         } else {
 194  0
             str = yyyyMMdd.substring(4, 6);
 195  
         }
 196  0
         return str;
 197  
     }
 198  
     private static String getDD(String yyyyMMdd) {
 199  
         String str;
 200  0
         if(yyyyMMdd == null || yyyyMMdd.length() != 8) {
 201  0
             str = "00";
 202  0
         } else {
 203  0
             str = yyyyMMdd.substring(6, 8);
 204  
         }
 205  0
         return str;
 206  
     }
 207  
 }
 208