| 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 | |
|
| 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); |
| 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 | |
|
| 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 | |
|
| 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 | |
} |