package com.adaptrex.core.ext.jsf;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.faces.component.FacesComponent;
import javax.faces.context.FacesContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.adaptrex.core.config.AdaptrexConfig;
import com.adaptrex.core.config.ContextConfig;
import com.adaptrex.core.service.CdiService;
@FacesComponent("ext.data.Config")
public class ConfigComponent extends ExtComponentBase {
/*
* The ability to inject into faces components should be available
* in JSF 2.2 scheduled for late 2012.
*/
private static Logger log = LoggerFactory.getLogger(ConfigComponent.class);
@Override
public String getFamily() {
return "ext.data.config";
}
@SuppressWarnings("unchecked")
@Override
public void encodeBegin(FacesContext context) throws IOException {
try {
Map<String,Object> a = getAttributes();
ContextConfig contextConfig = CdiService.getContextConfig();
AdaptrexConfig adaptrexConfig = CdiService.getAdaptrexConfig();
Map<String,Object> extConfig = new HashMap<String,Object>();
if (adaptrexConfig.get("ext") != null) {
extConfig.putAll((Map<String,Object>) adaptrexConfig.get("ext"));
}
if (contextConfig.get("ext") != null) {
extConfig.putAll((Map<String,Object>) contextConfig.get("ext"));
}
/*
* If we're configuring Ext, do it here
*/
String extVersion = (String) a.get("ext-version");
if (extVersion == null) extVersion = (String) extConfig.get("version");
if (extVersion != null) {
/*
* Get the context path
*/
String contextPath = context.getExternalContext().getRequestContextPath();
if (contextPath.equals("/")) contextPath = "";
/*
* Get the weblib path inside this context
*/
String weblibPath = (String) a.get("weblibpath");
if (weblibPath == null) weblibPath = "/lib";
/*
* Get the ext build. Check in this order:
* tag --> app config --> server config --> default (
*/
String extBuildName = "ext";
if (a.get("ext-build") != null) {
extBuildName += "-" + (String) a.get("ext-build");
} else if (extConfig.containsKey("build")) {
extBuildName += "-" + extConfig.get("build");
}
/*
* Ext folder
*/
String extFolder = contextPath + weblibPath + "/extjs-" + extVersion;
/*
* Write Ext Javascript Bootstrap File
*/
write("<script type='text/javascript' src='" + extFolder + "/" + extBuildName + ".js'></script>");
/*
* Theme
*/
String theme = "all";
if (a.containsKey("ext-theme")) {
theme = (String) a.get("ext-theme");
} else if (extConfig.containsKey("theme")) {
theme = (String) extConfig.get("theme");
}
if (theme.contains("neptune")) {
write("<script type='text/javascript' src='" + extFolder + "/ext-neptune.js'></script>");
}
write("<link rel='stylesheet' type='text/css' href='" + extFolder + "/resources/css/ext-" + theme + ".css' />");
/*
* Set up adaptrex loader if we're in debug mode
*/
String adaptrexFolder = contextPath + weblibPath + "/adaptrexjs";
write("<script type='text/javascript' src='" + adaptrexFolder + "/adaptrex.js'></script>");
write("<script type='text/javascript'>Adaptrex.Context={path:'" + contextPath + "'}</script>");
if (extBuildName.contains("debug")) {
write("<script type='text/javascript'>" +
"Ext.Loader.setPath('Adaptrex', '" + adaptrexFolder + "/src')</script>");
}
}
/*
* Set up namespace
*/
String ns = (String) a.get("ns");
if (ns != null) {
setAttribute("ns", ns);
write("<script type='text/javascript'>Ext.ns(\"" + ns + "\")</script>");
}
/*
* Application bootstrap path
*/
String bootstrapPath = a.containsKey("ext-bootstrap") ? (String) a.get("ext-bootstrap") : "app.js";
write("<script type='text/javascript' src='" + bootstrapPath + "'></script>");
String jpaPU = (String) a.get("jpa-pu");
if (jpaPU != null) {
setAttribute("jpa-pu", jpaPU);
}
if (a.get("touch") != null) {
setAttribute("touch", Boolean.parseBoolean((String) a.get("touch")));
} else {
setAttribute("touch", false);
}
} catch (Exception e) {
log.warn("Error", e);
}
}
}