001 package hirondelle.web4jtools.codegenerator.feature; 002 003 import hirondelle.web4j.action.ActionTemplateShowAndApply; 004 import hirondelle.web4j.action.ResponsePage; 005 import hirondelle.web4j.model.AppException; 006 import hirondelle.web4j.model.ModelCtorException; 007 import hirondelle.web4j.model.ModelFromRequest; 008 import hirondelle.web4j.request.RequestParameter; 009 import hirondelle.web4j.request.RequestParser; 010 011 /** 012 * Edit your feature's basic info. 013 */ 014 public final class FeatureAction extends ActionTemplateShowAndApply { 015 016 /** Constructor. */ 017 public FeatureAction(RequestParser aRequestParser){ 018 super(FORWARD, REDIRECT, aRequestParser); 019 } 020 021 public static final RequestParameter FEATURE_NAME = RequestParameter.withLengthCheck("Name"); 022 public static final RequestParameter UI_STYLE = RequestParameter.withLengthCheck("UiStyle"); 023 public static final RequestParameter PACKAGE_NAME = RequestParameter.withLengthCheck("PackageName"); 024 public static final RequestParameter SHOW_OPERATION = RequestParameter.withLengthCheck("ShowOperation"); 025 public static final RequestParameter APPLY_OPERATION = RequestParameter.withLengthCheck("ApplyOperation"); 026 027 /** If no feature name has been input, then shows a blank form. */ 028 @Override protected void show() throws AppException { 029 addToRequest(ITEM_FOR_EDIT, fetchFeature()); 030 } 031 032 /** Check user input can build a {@link Feature} object. */ 033 @Override protected void validateUserInput() throws AppException { 034 try { 035 ModelFromRequest builder = new ModelFromRequest(getRequestParser()); 036 fFeature = builder.build(Feature.class, FEATURE_NAME, UI_STYLE, PACKAGE_NAME, SHOW_OPERATION, APPLY_OPERATION); 037 } 038 catch (ModelCtorException ex){ 039 addError(ex); 040 } 041 } 042 043 /** Save the {@link Feature} Model Object input by the user. */ 044 @Override protected void apply() throws AppException { 045 saveFeature(); 046 addMessage("'_1_' feature updated.", fFeature.getName()); 047 } 048 049 /** Key for storing Feature object in session scope. */ 050 public static final String FEATURE_KEY = "feature"; 051 052 // PRIVATE // 053 private Feature fFeature; 054 private static final ResponsePage FORWARD = new ResponsePage("Feature", "view.jsp", FeatureAction.class); 055 private static final ResponsePage REDIRECT = new ResponsePage("FeatureAction.do?Operation=Show"); 056 057 /** Returns null if no {@link Feature} is in the session. */ 058 private Feature fetchFeature(){ 059 return (Feature)getFromSession(FEATURE_KEY); 060 } 061 062 /** Save {@link Feature} in session scope. */ 063 private void saveFeature(){ 064 addToSession(FEATURE_KEY, fFeature); 065 } 066 }