\r
import org.apache.logging.log4j.LogManager;\r
import org.apache.logging.log4j.Logger;\r
+import org.zkoss.bind.BindUtils;\r
import org.zkoss.bind.annotation.AfterCompose;\r
+import org.zkoss.bind.annotation.Command;\r
import org.zkoss.bind.annotation.ContextParam;\r
import org.zkoss.bind.annotation.ContextType;\r
import org.zkoss.zk.ui.Component;\r
+import org.zkoss.zk.ui.Execution;\r
import org.zkoss.zk.ui.Executions;\r
+import org.zkoss.zk.ui.event.Event;\r
import org.zkoss.zk.ui.select.Selectors;\r
+import org.zkoss.zk.ui.util.Clients;\r
+\r
+import com.ibm.nosql.json.api.BasicDBObject;\r
\r
import user.commons.ListUtils;\r
+import user.jobengine.zk.util.SessionUtil;\r
\r
public class IndexModel extends BaseModel {\r
private static final Logger logger = LogManager.getLogger();\r
+\r
+ static public void redirect(String path) {\r
+ String query = IndexModel.toQueryString(Executions.getCurrent().getParameterMap());\r
+ if (query != null)\r
+ path += query;\r
+ logger.info("Redirecting to {}", path);\r
+ Executions.sendRedirect(path);\r
+ }\r
+\r
+ static public BasicDBObject toParamObject(Map<String, String[]> parameters) {\r
+ BasicDBObject result = null;\r
+ if (parameters != null && parameters.size() > 0) {\r
+ result = new BasicDBObject();\r
+ for (String key : parameters.keySet()) {\r
+ result.put(key, parameters.get(key)[0]);\r
+ //logger.info("{}:{}", key, parameters.get(key)[0]);\r
+ }\r
+ }\r
+ return result;\r
+ }\r
+\r
+ static public String toQueryString(Map<String, String[]> parameters) {\r
+ String result = null;\r
+ if (parameters != null && parameters.size() > 0) {\r
+ StringBuilder sb = new StringBuilder("?");\r
+ for (String key : parameters.keySet()) {\r
+ if (sb.length() > 1)\r
+ sb.append("&");\r
+ sb.append(key);\r
+ sb.append("=");\r
+ sb.append(parameters.get(key)[0]);\r
+ //logger.info("{}:{}", key, parameters.get(key)[0]);\r
+ }\r
+ result = sb.toString();\r
+ }\r
+ return result;\r
+ }\r
+\r
private String page;\r
\r
+ private Map<String, Object> pathMap = ListUtils.asMap("/", "searchitems", "jobs", "joblist", "missingmaterials", "missingmaterials", "newshistory",\r
+ "newshistory", "statistics", "statistics");\r
+\r
@AfterCompose\r
public void afterCompose(@ContextParam(ContextType.VIEW) Component view) {\r
Selectors.wireComponents(view, this, false);\r
Selectors.wireEventListeners(view, this);\r
\r
- Map<String, Object> pathMap = ListUtils.asMap("jobs", "joblist", "missingmaterials", "missingmaterials");\r
- String parameter = Executions.getCurrent().getParameter("action");\r
- String action = (String) pathMap.get(parameter);\r
- if ("null".equals(parameter) || action == null) {\r
- setPage("/pages/searchitems.zul");\r
- } else {\r
- setPage(String.format("/pages/%s.zul", action));\r
- }\r
+ Execution execution = Executions.getCurrent();\r
+ String action = execution.getParameter("action");\r
+ Map<String, String[]> parameters = execution.getParameterMap();\r
+ route(action, toQueryString(parameters), true);\r
+\r
+ SessionUtil.setAttribute(IndexModel.class.getCanonicalName(), this);\r
}\r
\r
public String getPage() {\r
return page;\r
}\r
\r
+ @Command\r
+ public void popHistory(@ContextParam(ContextType.TRIGGER_EVENT) Event evt) {\r
+ BasicDBObject h = SessionUtil.popHistory();\r
+ if (h == null)\r
+ return;\r
+ route(h.getString("action"), h.getString("query"), h.getBoolean("useQueryString"));\r
+ // JSONObject data = (JSONObject) evt.getData();\r
+ // if (data != null) {\r
+ // JSONObject location = (JSONObject) data.get("location");\r
+ // if (location != null) {\r
+ // String pathName = (String) location.get("pathname");\r
+ // if (pathName != null) {\r
+ // Path path = Paths.get(pathName);\r
+ // Path action = path.getFileName();\r
+ // if (action != null) {\r
+ // route(action.toString(), null);\r
+ // return;\r
+ // }\r
+ // }\r
+ //\r
+ // }\r
+ // }\r
+ // route(null, null);\r
+ }\r
+\r
+ public void pushClientHistory(String action, String query, boolean useQueryString) {\r
+ if (!action.equals("/"))\r
+ action = "/actions/" + action;\r
+ if (useQueryString && query != null)\r
+ action += query;\r
+ Clients.evalJavaScript(String.format("window.history.pushState('%s', null, '%s');", query, action));\r
+ logger.info("Pushed to client: {} {}", action, query);\r
+ }\r
+\r
+ public void route(String action, String query, boolean useQueryString) {\r
+ String actionString = action == null ? "/" : action;\r
+ String page = (String) pathMap.get(actionString);\r
+ if (useQueryString && query != null)\r
+ page = String.format("/pages/%s.zul?%s", page, query);\r
+ else\r
+ page = String.format("/pages/%s.zul", page);\r
+ setPage(page);\r
+ pushClientHistory(actionString, query, useQueryString);\r
+ SessionUtil.pushHistory(actionString, query, useQueryString);\r
+ }\r
+\r
public void setPage(String page) {\r
+ if (page.equals(this.page))\r
+ return;\r
this.page = page;\r
+ BindUtils.postNotifyChange(null, null, this, "page");\r
+ logger.info("Page set to {}", getPage());\r
}\r
\r
}
\ No newline at end of file