/*
* Copyright 2012 Adaptrex, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.adaptrex.core.view.jsf;
import java.io.IOException;
import java.util.Map;
import javax.faces.component.FacesComponent;
import javax.faces.component.UIComponentBase;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import com.adaptrex.core.view.StoreComponent;
@FacesComponent("ext.data.Store")
public class JSFStoreComponent extends UIComponentBase {
@Override
public String getFamily() {
return "ext.data.store";
}
@Override
public void encodeBegin(FacesContext context) throws IOException {
ExternalContext external = context.getExternalContext();
HttpServletRequest request = (HttpServletRequest) external.getRequest();
Map<String,Object> a = getAttributes();
String factory = (String) a.get("factory");
String table = (String) a.get("table");
String modelName = (String) a.get("name");
String clearOnPageLoad = (String) a.get("clearonpageload");
String clearRemovedOnLoad = (String) a.get("clearremovedonload");
String autoSync = (String) a.get("autosync");
String autoLoad = (String) a.get("autoload");
String remoteGroup = (String) a.get("remotegroup");
String remoteSort = (String) a.get("remotesort");
String remoteFilter = (String) a.get("remotefilter");
String sortOnFilter = (String) a.get("sortonfilter");
String buffered = (String) a.get("buffered");
String inline = (String) a.get("inline");
String start = (String) a.get("start");
String where = (String) a.get("where");
String param = (String) a.get("param");
String filter = (String) a.get("filter");
String group = (String) a.get("group");
String sort = (String) a.get("sort");
String pageSize = (String) a.get("pagesize");
String rest = (String) a.get("rest");
String include = (String) a.get("include");
String exclude = (String) a.get("exclude");
String associations = (String) a.get("associations");
/*
* Pick up namespace from various sources
*/
String namespace = (String) a.get("ns");
if (namespace == null) namespace = (String) a.get("namespace");
/*
* Create the store component
*/
StoreComponent storeComponent = new StoreComponent(request, table, factory);
storeComponent.setNamespace(namespace);
if (modelName != null) storeComponent.setModelName(modelName);
if (include != null) storeComponent.setInclude(include);
if (exclude != null) storeComponent.setExclude(exclude);
if (associations != null) storeComponent.setAssociations(associations);
if (pageSize != null) storeComponent.setPageSize(Integer.valueOf(pageSize));
if (start != null) storeComponent.setStart(Integer.valueOf(start));
if (where != null) storeComponent.setWhere(where);
if (rest != null && !rest.equals("false")) {
storeComponent.setRest(rest);
}
if (clearOnPageLoad != null && clearOnPageLoad.equals("true")) {
storeComponent.setClearOnPageLoad(true);
}
if (clearRemovedOnLoad != null && clearRemovedOnLoad.equals("true")) {
storeComponent.setClearRemovedOnLoad(true);
}
if (autoSync != null && autoSync.equals("true")) {
storeComponent.setAutoSync(true);
}
if (autoLoad != null && autoLoad.equals("true")) {
storeComponent.setAutoLoad(true);
}
if (remoteGroup != null && remoteGroup.equals("true")) {
storeComponent.setRemoteGroup(true);
}
if (remoteSort != null && remoteSort.equals("true")) {
storeComponent.setRemoteSort(true);
}
if (remoteFilter != null && remoteFilter.equals("true")) {
storeComponent.setRemoteFilter(true);
}
if (sortOnFilter != null && sortOnFilter.equals("true")) {
storeComponent.setSortOnFilter(true);
}
if (inline != null && inline.equals("true")) {
storeComponent.setInline(true);
}
if (buffered != null && !buffered.contains("false")) {
storeComponent.setBuffered(true);
if (!buffered.equals("true")) {
String[] parts = buffered.split(":");
storeComponent.setTrailingBufferZone(Integer.valueOf(parts[0]));
if (parts.length > 1) {
storeComponent.setLeadingBufferZone(Integer.valueOf(parts[1]));
}
if (parts.length > 1) {
storeComponent.setPurgePageCount(Integer.valueOf(parts[2]));
}
}
}
if (param != null) {
for (String p : param.split(",")) {
String[] parts = p.split("=");
storeComponent.addParam(parts[0], parts[1]);
}
}
if (filter != null) {
for (String p : filter.split(",")) {
String[] parts = p.split("=");
storeComponent.addFilter(parts[0], parts[1]);
}
}
if (group != null) {
for (String groupItem : group.split(",")) {
String[] parts = groupItem.split(":");
String property = parts[0];
String direction = parts.length > 1 ? parts[1] : null;
String root = parts.length > 2 ? parts[2] : null;
storeComponent.addGrouper(property, direction, root);
}
}
if (sort != null) {
for (String sortItem : sort.split(",")) {
String[] parts = sortItem.split(":");
String property = parts[0];
String direction = parts.length > 1 ? parts[1] : "ASC";
String root = parts.length > 2 ? parts[2] : null;
storeComponent.addSorter(property, direction, root);
}
}
context.getResponseWriter().write(storeComponent.toString());
}
}