import java.util.List;
import java.util.Locale;
import java.util.Map;
-
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import mir.config.MirPropertiesConfiguration;
-import mir.config.MirPropertiesConfiguration.PropertiesConfigExc;
import mir.entity.adapter.EntityAdapterEngine;
import mir.entity.adapter.EntityAdapterModel;
import mir.log.LoggerWrapper;
import mir.module.AbstractModule;
+import mir.module.ModuleExc;
import mir.storage.StorageObject;
import mir.util.HTTPRequestParser;
import mir.util.URLBuilder;
import mircoders.global.MirGlobal;
import mircoders.servlet.ServletHelper;
+import mircoders.localizer.MirLocalizerExc;
+import multex.Failure;
/**
*
public abstract class ServletModule {
public String defaultAction;
protected LoggerWrapper logger;
- protected MirPropertiesConfiguration configuration;
- protected Locale fallbackLocale;
+ protected static MirPropertiesConfiguration configuration = MirPropertiesConfiguration.instance();
+ private static Locale fallbackLocale = new Locale(configuration.getString("Mir.Admin.FallbackLanguage", "en"), "");
protected AbstractModule mainModule;
protected String definition;
public ServletModule(){
definition = null;
- model = null;
-
try {
- configuration = MirPropertiesConfiguration.instance();
+ model = MirGlobal.localizer().dataModel().adapterModel();
}
- catch (PropertiesConfigExc e) {
- throw new RuntimeException("Can't get configuration: " + e.getMessage());
+ catch (MirLocalizerExc e) {
+ logger.error("Can't create model: " + e.toString());
+ throw new ServletModuleFailure("Can't retrieve model", e);
}
+
+
listGenerator = configuration.getString("ServletModule."+getOperationModuleName()+".ListTemplate");
editGenerator = configuration.getString("ServletModule."+getOperationModuleName()+".EditTemplate");
deleteConfirmationGenerator = configuration.getString("ServletModule."+getOperationModuleName()+".DeleteConfirmationTemplate");
configuration.getInt("ServletModule."+getOperationModuleName()+".ListSize",
configuration.getInt("ServletModule.Default.ListSize", 20));
- fallbackLocale = new Locale(configuration.getString("Mir.Admin.FallbackLanguage", "en"), "");
}
}
/**
- * Get the module name
- *
- * @return
+ * Return the module name
*/
protected String getOperationModuleName() {
return getClass().getName().substring((new String("mircoders.servlet.ServletModule")).length());
}
+ public static Locale[] getLocales(HttpServletRequest aRequest) {
+ return new Locale[] { getLocale(aRequest), fallbackLocale };
+ }
+
/**
- * get the locale either from the session or the accept-language header ot the request
+ * Return the locale either from the session or the accept-language header ot the request
* this supersedes getLanguage for the new i18n
*/
- public Locale getLocale(HttpServletRequest aRequest) {
+ public static Locale getLocale(HttpServletRequest aRequest) {
Locale loc = null;
HttpSession session = aRequest.getSession(false);
if (session != null) {
public String getDefaultListOrdering() {
if (mainModule!=null && mainModule.getStorageObject()!=null){
- if (mainModule.getStorageObject().getFields().contains("webdb_create"))
+ if (mainModule.getStorageObject().getFieldNames().contains("webdb_create"))
return "webdb_create desc";
}
}
/**
- *
- * @param aResponse
- * @param aQuery
- * @throws ServletModuleExc
- * @throws ServletModuleFailure
- */
- public void redirect(HttpServletResponse aResponse, String aQuery) throws ServletModuleExc, ServletModuleFailure {
- try {
- aResponse.sendRedirect(aResponse.encodeRedirectURL(MirPropertiesConfiguration.instance().getString("RootUri") + "/servlet/Mir?"+aQuery));
- }
- catch (Throwable t) {
- throw new ServletModuleFailure("ServletModule.redirect: " +t.getMessage(), t);
- }
- }
-
- /**
- * Generic list method
- *
- * @param aRequest
- * @param aResponse
- * @throws ServletModuleExc
- * @throws ServletModuleUserExc
- * @throws ServletModuleFailure
+ * Generic list servlet method
*/
public void list(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
int count;
try {
- Map responseData = ServletHelper.makeGenerationData(aRequest, aResponse, new Locale[] { getLocale(aRequest), getFallbackLocale(aRequest)});
+ Map responseData = ServletHelper.makeGenerationData(aRequest, aResponse, getLocales(aRequest));
List list =
EntityAdapterEngine.retrieveAdapterList(model, definition, aWhereClause, anOrderByClause, nrEntitiesPerListPage, anOffset);
public void editObject(HttpServletRequest aRequest, HttpServletResponse aResponse, Object anObject, boolean anIsNew, String anId) throws ServletModuleExc {
HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
URLBuilder urlBuilder = new URLBuilder();
- EntityAdapterModel model;
try {
- Map responseData = ServletHelper.makeGenerationData(aRequest, aResponse, new Locale[] { getLocale(aRequest), getFallbackLocale(aRequest)});
+ Map responseData = ServletHelper.makeGenerationData(aRequest, aResponse, getLocales(aRequest));
responseData.put("module", getOperationModuleName());
responseData.put("entity", anObject);
}
}
-
/**
- * Generic add method
- *
- * @param aRequest
- * @param aResponse
- * @throws ServletModuleExc
- * @throws ServletModuleUserExc
- * @throws ServletModuleFailure
+ * Generic add servlet method
*/
public void add(HttpServletRequest aRequest, HttpServletResponse aResponse)
throws ServletModuleExc, ServletModuleUserExc, ServletModuleFailure {
Map object = new HashMap();
- Iterator i = mainModule.getStorageObject().getFields().iterator();
+ Iterator i = mainModule.getStorageObject().getFieldNames().iterator();
while (i.hasNext())
object.put(i.next(), "");
}
/**
- * Method called when the user edits an object.
- *
- * @param aRequest
- * @param aResponse
- * @throws ServletModuleExc
- * @throws ServletModuleUserExc
- * @throws ServletModuleFailure
+ * Generic edit servlet method
*/
public void edit(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc, ServletModuleUserExc, ServletModuleFailure {
edit(aRequest, aResponse, aRequest.getParameter("id"));
}
/**
- * Generic edit method
- *
- * @param aRequest
- * @param aResponse
- * @param anIdentifier
- * @throws ServletModuleExc
- * @throws ServletModuleUserExc
- * @throws ServletModuleFailure
*/
public void edit(HttpServletRequest aRequest, HttpServletResponse aResponse, String anIdentifier)
throws ServletModuleExc, ServletModuleUserExc, ServletModuleFailure {
}
/**
- * Generic update method
- *
- * @param aRequest
- * @param aResponse
- * @throws ServletModuleExc
- * @throws ServletModuleUserExc
- * @throws ServletModuleFailure
+ * Generic update servlet method
*/
public void update(HttpServletRequest aRequest, HttpServletResponse aResponse)
throws ServletModuleExc, ServletModuleUserExc, ServletModuleFailure {
String returnUrl = requestParser.getParameter("returnurl");
if (returnUrl!=null) {
- redirect(aResponse, returnUrl);
+ ServletHelper.redirect(aResponse, returnUrl);
}
else {
edit(aRequest, aResponse, id);
}
/**
- * Generic insert method
- *
- * @param aRequest
- * @param aResponse
- * @throws ServletModuleExc
- * @throws ServletModuleUserExc
- * @throws ServletModuleFailure
+ * Generic insert servlet method
*/
public void insert(HttpServletRequest aRequest, HttpServletResponse aResponse)
throws ServletModuleExc, ServletModuleUserExc, ServletModuleFailure {
String returnUrl = requestParser.getParameter("returnurl");
if (returnUrl!=null) {
- redirect(aResponse, returnUrl);
+ ServletHelper.redirect(aResponse, returnUrl);
}
else {
edit(aRequest, aResponse, id);
}
}
+ /**
+ *
+ */
public String processInstertedObject(Map anObject, HttpServletRequest aRequest, HttpServletResponse aResponse) {
try {
return mainModule.add(anObject);
}
- catch (Throwable t) {
+ catch (ModuleExc t) {
throw new ServletModuleFailure(t);
}
};
/**
- *
- * @param aRequest
- * @param aResponse
+ * Generic delete confirmation servlet method
*/
public void confirmdelete(HttpServletRequest aRequest, HttpServletResponse aResponse) {
try {
String idParam = aRequest.getParameter("id");
String confirmParam = aRequest.getParameter("confirm");
- String cancelParam = aRequest.getParameter("cancel");
if (confirmParam != null && !confirmParam.equals("")) {
mainModule.deleteById(idParam);
logAdminUsage(aRequest, idParam, "object deleted");
- redirect(aResponse, aRequest.getParameter("okurl"));
+ ServletHelper.redirect(aResponse, aRequest.getParameter("okurl"));
}
else
- redirect(aResponse, aRequest.getParameter("cancelurl"));
+ ServletHelper.redirect(aResponse, aRequest.getParameter("cancelurl"));
}
catch (Throwable t) {
throw new ServletModuleFailure(t);
}
/**
- *
- * @param aRequest
- * @param aResponse
- * @throws ServletModuleExc
- * @throws ServletModuleUserExc
- * @throws ServletModuleFailure
+ * Generic delete servlet method
*/
public void delete(HttpServletRequest aRequest, HttpServletResponse aResponse)
throws ServletModuleExc, ServletModuleUserExc, ServletModuleFailure {
if (idParam == null)
throw new ServletModuleExc("Invalid call to delete: no id supplied");
- Map responseData = ServletHelper.makeGenerationData(aRequest, aResponse, new Locale[] { getLocale(aRequest), getFallbackLocale(aRequest)});
+ Map responseData = ServletHelper.makeGenerationData(aRequest, aResponse, getLocales(aRequest));
responseData.put("module", getOperationModuleName());
responseData.put("id", idParam);
}
/**
- * Wenn die abgeleitete Klasse diese Methode ueberschreibt und einen String mit einem
- * Methodennamen zurueckliefert, dann wird diese Methode bei fehlender Angabe des
- * doParameters ausgefuehrt.
- *
- * @return Name der Default-Action
*/
public String defaultAction() {
return defaultAction;
public Map getIntersectingValues(HttpServletRequest aRequest, StorageObject theStorage)
throws ServletModuleExc, ServletModuleFailure {
- try {
- HTTPRequestParser parser;
- List theFieldList;
+ HTTPRequestParser parser;
+ List theFieldList;
- parser = new HTTPRequestParser(aRequest);
+ parser = new HTTPRequestParser(aRequest);
- theFieldList = theStorage.getFields();
+ theFieldList = theStorage.getFieldNames();
- Map withValues = new HashMap();
- String aField, aValue;
+ Map withValues = new HashMap();
+ String aField, aValue;
- for (int i = 0; i < theFieldList.size(); i++) {
- aField = (String) theFieldList.get(i);
-
- aValue = parser.getParameter(aField);
- if (aValue != null)
- withValues.put(aField, aValue);
- }
- return withValues;
- }
- catch (Throwable e) {
- e.printStackTrace(logger.asPrintWriter(LoggerWrapper.DEBUG_MESSAGE));
+ for (int i = 0; i < theFieldList.size(); i++) {
+ aField = (String) theFieldList.get(i);
- throw new ServletModuleFailure( "ServletModule.getIntersectingValues: " + e.getMessage(), e);
+ aValue = parser.getParameter(aField);
+ if (aValue != null)
+ withValues.put(aField, aValue);
}
+ return withValues;
}
}
\ No newline at end of file