package com.nexirius.ulc.example1;
import com.nexirius.framework.FWLog;
import com.nexirius.framework.datamodel.DataModel;
import com.nexirius.framework.datamodel.DataModelEnumeration;
import com.nexirius.framework.datamodel.DataModelVector;
import com.nexirius.framework.htmlview.*;
import com.nexirius.framework.htmlview.application.HTMLApplication;
import com.nexirius.framework.htmlview.function.HTMLFunction;
import com.nexirius.framework.htmlview.function.HTMLTransition;
import com.nexirius.ulc.example1.datamodel.ItemListModel;
import com.nexirius.ulc.example1.datamodel.ItemModel;
import com.nexirius.ulc.example1.persistence.PersistenceManager;
import com.nexirius.ulc.example1.function.MyColorFunction;
import com.nexirius.util.XFile;
public class ExampleHTMLApplication extends HTMLApplication {
public static final String TEMPLATE_Item = "item";
public static final String TEMPLATE_ItemList = "itemlist";
public static final String TEMPLATE_ItemRow = "itemrow";
public static final String TEMPLATE_DrillDown = "drilldown";
public static final String TEMPLATE_NewItem = "newitem";
public static final String EVENT_reload = "reload";
public ExampleHTMLApplication() {
}
public void preInit() {
}
public String getApplicationName() {
return "ExampleUlcApplication";
}
public HTMLTransition[] getHTMLTransistions(DataModel homeModel) {
ItemListModel list = (ItemListModel) homeModel;
HTMLState overview = new HTMLState("Overview", list, true, null);
HTMLState drillDown = new HTMLState("DrillDown", null, true, TEMPLATE_DrillDown);
HTMLState newItem = new HTMLState("NewItem", list.getSelected(), true, TEMPLATE_NewItem);
return new HTMLTransition[]{
new HTMLTransition(overview, HTMLFunction.EVENT_POPUP, drillDown),
new HTMLTransition(overview, EVENT_reload, new ReloadCommand(), overview),
new HTMLTransition(overview, "deleteItemCommand", new DeleteCommand(), overview),
new HTMLTransition(overview, "sort", new SortItemsCommand(), overview),
new HTMLTransition(drillDown, HTMLFunction.EVENT_OK, new UpdateCommand(), overview),
new HTMLTransition(drillDown, HTMLFunction.EVENT_CANCEL, overview),
new HTMLTransition(overview, "newItemCommand", newItem),
new HTMLTransition(newItem, HTMLFunction.EVENT_OK, new NewCommand(), overview),
new HTMLTransition(newItem, HTMLFunction.EVENT_CANCEL, overview),
new HTMLTransition("toggleDebug", new ToggleDebugCommand()),
};
}
public HTMLStreamMapEntry[] getStreamMapperEntries() {
return new HTMLStreamMapEntry[]{
new HTMLStreamMapEntry(TEMPLATE_Item, new XFile("html/item.html")),
new HTMLStreamMapEntry(TEMPLATE_ItemRow, new XFile("html/itemrow.html")),
new HTMLStreamMapEntry(TEMPLATE_ItemList, new XFile("html/itemlist.html")),
new HTMLStreamMapEntry(TEMPLATE_DrillDown, new XFile("html/drilldown.html")),
new HTMLStreamMapEntry(TEMPLATE_NewItem, new XFile("html/newitem.html")),
new HTMLStreamMapEntry("header", new XFile("html/header.html")),
new HTMLStreamMapEntry("footer", new XFile("html/footer.html")),
};
}
public HTMLTemplateMapEntry[] getTemplateMapEntries() {
return new HTMLTemplateMapEntry[]{
new HTMLTemplateMapEntry(ItemListModel.class, TEMPLATE_ItemList, true),
new HTMLTemplateMapEntry(ItemModel.class, TEMPLATE_Item, true),
};
}
public HTMLFunction[] getHTMLFunctions() {
return new HTMLFunction[]{new MyColorFunction()};
}
public void postInit(HTMLSessionVariable sessionVariable) {
sessionVariable.getResolver().getVariableStore().setVariable("DEBUG", (System.getProperty("debug") != null) ? "true" : "false");
}
public void handleException(HTMLSessionVariable sessionVariable, Exception e) {
}
public DataModel createHomeModel() {
ItemListModel mainModel = new ItemListModel();
try {
PersistenceManager.init();
mainModel.reloadCommand();
} catch (Exception e) {
e.printStackTrace(); //TODO
}
return mainModel;
}
class NewCommand implements HTMLCommand {
public boolean requiresMapping() {
return true;
}
public boolean execute(HTMLSessionVariable sessionVariable) throws Exception {
DataModel model = sessionVariable.getActModel();
if (!model.isValid()) {
return false;
}
if (model instanceof ItemModel) {
ItemModel item = (ItemModel) model;
ItemListModel homeModel = (ItemListModel) sessionVariable.getApplicationModel();
homeModel.getArray().sortInsert(item.duplicate(null, null));
PersistenceManager.save(homeModel.getArray());
}
return true;
}
}
class UpdateCommand implements HTMLCommand {
public boolean requiresMapping() {
return true;
}
public boolean execute(HTMLSessionVariable sessionVariable) throws Exception {
DataModel model = sessionVariable.getActModel();
if (!model.isValid()) {
return false;
}
DataModel orig = sessionVariable.getActState().finishDuplicatePopup();
PersistenceManager.saveItem((ItemModel) orig);
return true;
}
}
class ReloadCommand implements HTMLCommand {
public boolean requiresMapping() {
return true;
}
public boolean execute(HTMLSessionVariable sessionVariable) throws Exception {
DataModel model = sessionVariable.getActModel();
if (!model.isValid()) {
return false;
}
if (model instanceof ItemListModel) {
((ItemListModel) sessionVariable.getActModel()).reloadCommand();
}
return true;
}
}
class DeleteCommand implements HTMLCommand {
public boolean requiresMapping() {
return true;
}
public boolean execute(HTMLSessionVariable sessionVariable) throws Exception {
DataModelVector selectedChildren = sessionVariable.getSelectedChildren();
DataModelEnumeration en = selectedChildren.getEnumeration();
while (en.hasMore()) {
DataModel model = en.next();
PersistenceManager.remove(model);
model.getParentDataModelContainer().removeItem(model);
}
return true;
}
}
class ToggleDebugCommand implements HTMLCommand {
public boolean requiresMapping() {
return false;
}
public boolean execute(HTMLSessionVariable sessionVariable) throws Exception {
VariableStore variableStore = sessionVariable.getResolver().getVariableStore();
String debug = variableStore.getValueOf("DEBUG");
if (debug == null || debug.equals("false")) {
variableStore.setVariable("DEBUG", "true");
FWLog.setDebugging(true);
} else {
variableStore.setVariable("DEBUG", "false");
FWLog.setDebugging(false);
}
return false;
}
}
class SortItemsCommand implements HTMLCommand {
public boolean requiresMapping() {
return false;
}
public boolean execute(HTMLSessionVariable sessionVariable) throws Exception {
String attr = sessionVariable.getRequestParameter("sortAttribute");
ItemListModel itemListModel = (ItemListModel) sessionVariable.getApplicationModel();
itemListModel.getArray().sortByAttribute(attr);
return true;
}
}
}