Coverage Report - jp.co.y_net.amm.page.InfoEntryPage
 
Classes in this File Line Coverage Branch Coverage Complexity
InfoEntryPage
0%
0/55
0%
0/12
0
InfoEntryPage$1
0%
0/5
0%
0/2
0
InfoEntryPage$2
0%
0/8
0%
0/2
0
InfoEntryPage$3
0%
0/2
N/A
0
InfoEntryPage$4
0%
0/5
0%
0/2
0
InfoEntryPage$5
0%
0/4
N/A
0
InfoEntryPage$6
0%
0/23
0%
0/6
0
InfoEntryPage$7
0%
0/6
N/A
0
 
 1  0
 package jp.co.y_net.amm.page;
 2  
 
 3  
 import jp.co.y_net.amm.AppSession;
 4  
 import jp.co.y_net.amm.AppSession.LoginUser;
 5  
 import jp.co.y_net.amm.common.AppStringUtils;
 6  
 import jp.co.y_net.amm.common.AppUtils;
 7  
 import jp.co.y_net.amm.common.InfoRenderer;
 8  
 import jp.co.y_net.amm.dao.Inf;
 9  
 import jp.co.y_net.amm.dao.Usr;
 10  
 
 11  
 import org.apache.commons.lang.StringUtils;
 12  
 import org.apache.wicket.ajax.AjaxRequestTarget;
 13  
 import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
 14  
 import org.apache.wicket.ajax.markup.html.form.AjaxButton;
 15  
 import org.apache.wicket.markup.html.WebMarkupContainer;
 16  
 import org.apache.wicket.markup.html.basic.Label;
 17  
 import org.apache.wicket.markup.html.form.Button;
 18  
 import org.apache.wicket.markup.html.form.Form;
 19  
 import org.apache.wicket.markup.html.form.TextArea;
 20  
 import org.apache.wicket.markup.html.form.TextField;
 21  
 import org.apache.wicket.model.Model;
 22  
 import org.apache.wicket.model.PropertyModel;
 23  
 import org.apache.wicket.request.mapper.parameter.PageParameters;
 24  
 
 25  
 public class InfoEntryPage extends CommonFrameA {
 26  
         private static final long serialVersionUID = 1L;
 27  
         
 28  
         /*
 29  
          * 画面入力値
 30  
          */
 31  0
         private String infotitleValue;
 32  0
         private String infobodyValue;
 33  
 
 34  
         /*
 35  
          * 画面の状態
 36  
          */
 37  0
         private boolean complete = false;
 38  
 
 39  
     /**
 40  
      * ログインチェック定義
 41  
      * @see jp.co.y_net.amm.page.AppPage#validateLogin()
 42  
      */
 43  
     @Override
 44  
     public boolean validateLogin() {
 45  0
         if(AppSession.get().isLogin()) {
 46  0
             if(AppSession.get().getLoginUser().getKind().equals(Usr.KIND_運営管理者)) {
 47  0
                 return true;
 48  
             }
 49  
         }
 50  0
         return false;
 51  
     }
 52  
         
 53  0
     public InfoEntryPage(PageParameters params) {
 54  
         
 55  
         /* 共通戻るリンクの設置 */
 56  0
         createCommonBackLinks();
 57  
         
 58  
         /* 画面固有の戻るボタンの設置 */
 59  0
         createBackLink(InfoListPage.class, "お知らせ管理");
 60  
         
 61  
         /* フィードバックパネルの設置 */
 62  0
         createFeedbackPanel();
 63  
         
 64  
         /* 入力フォーム */
 65  0
         Form<Void> form  = new Form<Void>("form");
 66  0
         add(form);
 67  
         
 68  
         /* お知らせ編集の時はパラメータが指定されるのでチェック */
 69  0
         String paramInfid = params.get("infid").toString("");
 70  
         final Inf targetInf;
 71  0
         if(StringUtils.isEmpty(paramInfid)) {
 72  0
             targetInf = null;
 73  0
         } else {
 74  0
             Integer targetInfid =  AppUtils.toInteger(paramInfid);
 75  0
             targetInf = infDao.getByInfid(targetInfid);
 76  
         }
 77  0
         if(targetInf != null) {
 78  0
             infotitleValue = targetInf.getTitle();
 79  0
             infobodyValue = targetInf.getBody();
 80  
         }
 81  
         
 82  
         
 83  
         /* 初期表示エラーチェック・共通変数設定 --------------------------------------------------------------------- */
 84  0
         form.setVisible(false); // Formはエラーチェックが完了するまで非表示とする。
 85  
         
 86  0
         final LoginUser loginUser  = AppSession.get().getLoginUser();
 87  0
         if(loginUser == null) {
 88  0
             error("ログイン情報が取得できません。");
 89  0
             return;
 90  
         }
 91  0
         final Integer loginUserId = loginUser.getUsrid();
 92  
         
 93  0
         if(hasInputError()) return; // ----------エラーがあれば中止
 94  0
         form.setVisible(true);
 95  
         
 96  
         /* Form内の設定 ----------------------------------------------------------------------------------------------*/
 97  
         /* 完了メッセージ */
 98  0
         Model<String> resultMessageModel = new Model<String>() {
 99  
             @Override
 100  
             public String getObject() {
 101  0
                 if(complete) {
 102  0
                     return "登録しました。";
 103  
                 }
 104  0
                 return "";
 105  
             }
 106  
         };
 107  0
         final Label resutlMessage = new Label("resutlMessage", resultMessageModel);
 108  0
         form.add(resutlMessage);
 109  
         
 110  
         /* コンテナの設定 ----------------------------------------------------------------------------------*/
 111  0
         final WebMarkupContainer cntPreview = new WebMarkupContainer("cntPreview");
 112  0
         cntPreview.setOutputMarkupId(true);
 113  0
         form.add(cntPreview);
 114  
         
 115  
         /* 入力コンポーネントの設定 ----------------------------------------------------------------------------------*/
 116  
         
 117  0
         final TextField<String> infotitle = new TextField<String>("infotitle", new PropertyModel<String>(this, "infotitleValue"));
 118  0
         form.add(infotitle);
 119  
         
 120  0
         final TextArea<String> infobody = new TextArea<String>("infobody", new PropertyModel<String>(this, "infobodyValue"));
 121  0
         infobody.setEscapeModelStrings(false);
 122  0
         form.add(infobody);
 123  
         
 124  
         /* 表示項目の設定 ----------------------------------------------------------------------------------*/
 125  
         
 126  0
         Model<String> modelPreview = new Model<String>() {
 127  
             @Override
 128  
             public String getObject() {
 129  0
                 Inf previewInf = new Inf();
 130  0
                 previewInf.setTitle(infotitle.getValue());
 131  0
                 previewInf.setBody(infobody.getValue());
 132  0
                 if(targetInf != null) {
 133  0
                     previewInf.setPubdate(targetInf.getPubdate());
 134  
                 }
 135  0
                 return InfoRenderer.execute(previewInf);
 136  
             }
 137  
         };
 138  0
         final Label preview = new Label("preview", modelPreview) {};
 139  0
         preview.setVisible(false); // 初期 非表示
 140  0
         preview.setEscapeModelStrings(false);
 141  0
         cntPreview.add(preview);
 142  
         
 143  0
         final Label wordcount = new Label("wordcount", new Model<String>(){
 144  
             @Override
 145  
             public String getObject() {
 146  0
                 String body = infobody.getValue();
 147  0
                 int bodyCount = body == null ? 0 :body.length();
 148  0
                 return "あと" + (Inf.DEFお知らせ本文の文字数 - bodyCount) + "文字入力できます。";
 149  
             }
 150  
             
 151  
         });
 152  0
         wordcount.setOutputMarkupId(true);
 153  0
         form.add(wordcount);
 154  
         
 155  
         /* その他の振る舞い ------------------------------------------------------------------------------------------*/
 156  
         
 157  
         /* 本文が入力されたら残り文字数をリアルタイムで再描画する */
 158  0
         infobody.add(new AjaxFormComponentUpdatingBehavior("onKeyUp") {
 159  
             @Override
 160  
             protected void onUpdate(AjaxRequestTarget target) {
 161  0
                 target.add(wordcount);
 162  0
             }
 163  
         });
 164  
         
 165  
         
 166  
         /* 処理ボタン定義 --------------------------------------------------------------------------------------------*/
 167  0
         Button btnRegist = new Button("btnRegist") {
 168  
             @Override
 169  
             public void onSubmit() {
 170  
                 
 171  
                 /* モデル(PropertyModel以外)から値取得 */
 172  
                 // なし
 173  
                 
 174  
                 /* 入力値変換 */
 175  0
                 infotitleValue = AppStringUtils.trim(infotitleValue);
 176  0
                 infobodyValue = AppStringUtils.trim(infobodyValue);
 177  
                 
 178  
                 /* 入力値検査 */
 179  0
                 validItem(infotitleValue, "タイトル", 1, 512);
 180  0
                 validItem(infobodyValue, "本文", 1, Inf.DEFお知らせ本文の文字数);
 181  
                 
 182  0
                 if(hasInputError()) return; // ここまででエラーチェック
 183  
                 
 184  
                 /* データアクセス */
 185  0
                 if(targetInf == null) {
 186  
                     /* 新規作成 */
 187  0
                     Inf item = new Inf();
 188  0
                     item.setTitle(infotitleValue);
 189  0
                     item.setBody(infobodyValue);
 190  0
                     item.setPubdate(AppUtils.createNowLong()); // 現在日時
 191  0
                     item.setStatus(Inf.STATUS_掲載中);
 192  0
                     infDao.add(item, loginUserId); // データ更新
 193  0
                 } else {
 194  
                     
 195  
                     /* データ更新 */
 196  0
                     targetInf.setTitle(infotitleValue);
 197  0
                     targetInf.setBody(infobodyValue);
 198  0
                     infDao.update(targetInf, loginUserId); // データ更新
 199  
                 }
 200  
 
 201  
                 
 202  
                 /* 完了状態へ移行 */
 203  0
                 complete = true;
 204  0
             }
 205  
             @Override
 206  
             public boolean isVisible() {
 207  0
                 if(complete == true) {
 208  0
                     return false;
 209  
                 }
 210  0
                 return true;
 211  
             }
 212  
         };
 213  0
         form.add(btnRegist);
 214  
         
 215  0
         AjaxButton btnPreview = new AjaxButton("btnPreview") {
 216  
             private static final long serialVersionUID = 1L;
 217  
             @Override
 218  
             protected void onSubmit(AjaxRequestTarget target, Form<?> form){
 219  0
                 preview.setVisible(true);
 220  0
                 target.add(cntPreview); // プレビュー再描画
 221  0
             }
 222  
             @Override
 223  
             protected void onError(AjaxRequestTarget target, Form<?> form) {
 224  
                 /* エラーハンドリングなし */
 225  0
             }
 226  
         };
 227  0
         form.add(btnPreview);
 228  
 
 229  
         
 230  0
     }
 231  
 
 232  
 }