001 package hirondelle.web4jtools.codegenerator.action; 002 003 import java.util.*; 004 import hirondelle.web4j.model.AppException; 005 import hirondelle.web4j.action.ActionImpl; 006 import hirondelle.web4j.request.RequestParser; 007 import hirondelle.web4j.action.ResponsePage; 008 import hirondelle.web4jtools.util.TemplatedPage; 009 import hirondelle.web4jtools.codegenerator.codes.UiStyle; 010 import hirondelle.web4jtools.codegenerator.feature.Feature; 011 import hirondelle.web4jtools.codegenerator.feature.FeatureAction; 012 import hirondelle.web4jtools.codegenerator.field.Field; 013 import hirondelle.web4jtools.codegenerator.field.FieldAction; 014 import hirondelle.web4jtools.util.Functions; 015 import javax.servlet.ServletConfig; 016 import hirondelle.web4jtools.util.Ensure; 017 018 /** 019 * Generate the source code of your feature's Action class, as plain text. 020 * 021 * <P>Uses the {@link hirondelle.web4jtools.codegenerator.feature.Feature} and 022 * List of {@link hirondelle.web4jtools.codegenerator.field.Field} objects placed in session scope. 023 */ 024 public final class ActionAction extends ActionImpl { 025 026 /** 027 * Read in necessary config from <tt>web.xml</tt>. 028 * 029 * Called only upon startup. 030 */ 031 public static void readConfig(ServletConfig aConfig){ 032 fTEMPLATED_PAGE_CLASS = aConfig.getInitParameter(TEMPLATED_PAGE_CLASS); 033 Ensure.isPresentInWebXml(TEMPLATED_PAGE_CLASS, fTEMPLATED_PAGE_CLASS); 034 } 035 036 public ActionAction(RequestParser aRequestParser){ 037 super(LIST_AND_EDIT, aRequestParser); 038 } 039 040 /** Generate the Action class. */ 041 @Override public ResponsePage execute() throws AppException { 042 UiStyle uiStyle = getUiStyle(); 043 if ( UiStyle.ListAndEdit == uiStyle ) { 044 //use default forward 045 } 046 else if ( UiStyle.ShowAndApply == uiStyle ) { 047 setResponsePage(SHOW_AND_APPLY); 048 } 049 else { 050 throw new AssertionError("Unexpected value for UiStyle : " + uiStyle); 051 } 052 053 addPKIdentifierToRequest(); 054 addImportForTemplatedPage(); 055 056 return getResponsePage(); 057 } 058 059 // PRIVATE // 060 private static final ResponsePage LIST_AND_EDIT = TemplatedPage.getPlain("Action", "listAndEdit.jsp", ActionAction.class); 061 private static final ResponsePage SHOW_AND_APPLY = TemplatedPage.getPlain("Action", "showAndApply.jsp", ActionAction.class); 062 063 private static final String PK_KEY = "primaryKey"; 064 065 private static final String TEMPLATED_PAGE_CLASS = "TemplatedPageClass"; 066 private static String fTEMPLATED_PAGE_CLASS = ""; 067 068 private UiStyle getUiStyle() { 069 Feature feature = (Feature)getFromSession(FeatureAction.FEATURE_KEY); 070 return feature.getUiStyle(); 071 } 072 073 private void addPKIdentifierToRequest(){ 074 boolean isPkPresent = false; 075 List<Field> fields = (List<Field>) getFromSession(FieldAction.FIELDS_KEY); 076 for(Field field : fields){ 077 if( field.getIsPrimaryKey() ) { 078 isPkPresent = true; 079 addToRequest(PK_KEY, Functions.asConstant(field.getName().toString())); 080 break; 081 } 082 } 083 if ( ! isPkPresent ) { 084 addError("No field is marked as being Primary Key."); 085 addToRequest(PK_KEY, "ID"); 086 } 087 } 088 089 private void addImportForTemplatedPage(){ 090 addToRequest(TEMPLATED_PAGE_CLASS, fTEMPLATED_PAGE_CLASS); 091 } 092 }