001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: * $Header:$
018: */
019: package org.apache.beehive.netui.databinding.datagrid.api;
020:
021: import java.util.HashMap;
022: import javax.servlet.jsp.PageContext;
023: import javax.servlet.jsp.JspContext;
024: import javax.servlet.ServletRequest;
025:
026: import org.apache.beehive.netui.util.Bundle;
027:
028: /**
029: * <p>
030: * Factory class used to construct instances of {@link DataGridState} objects. This
031: * class is used by the data grid and other clients to obtain the state for a data grid
032: * with a given name.
033: * </p>
034: * <p>
035: * Data grid state information is scoped by a unique data grid String name. This name
036: * should be unique for a particular scope in a request. For all factory methods that
037: * take a data grid name as a parameter, the value of the <code>name</code> attribute
038: * should match the value of the
039: * {@link org.apache.beehive.netui.tags.databinding.datagrid.DataGrid#setName(String)}
040: * attribute for the data grid whose state to lookup.
041: * </p>
042: */
043: public final class DataGridStateFactory {
044:
045: private static final String KEY = DataGridStateFactory.class
046: .getName()
047: + "REQUEST_KEY";
048: private static final DataGridConfig DEFAULT_DATA_GRID_CONFIG = DataGridConfigFactory
049: .getInstance();
050:
051: /**
052: * Get an instance of a DataGridStateFactory given a {@link JspContext}.
053: *
054: * @param jspContext the current {@link JspContext}
055: * @return an instance of the factory
056: */
057: public static final DataGridStateFactory getInstance(
058: JspContext jspContext) {
059: assert jspContext instanceof PageContext;
060: return getInstance(((PageContext) jspContext).getRequest());
061: }
062:
063: /**
064: * Get an instance of a DataGridStateFactory given a {@link ServletRequest}.
065: *
066: * @param request the current {@link ServletRequest}
067: * @return an instance of the factory
068: */
069: public static final DataGridStateFactory getInstance(
070: ServletRequest request) {
071: Object obj = request.getAttribute(KEY);
072: if (obj != null) {
073: assert obj instanceof DataGridStateFactory;
074: return (DataGridStateFactory) obj;
075: } else {
076: DataGridStateFactory factory = new DataGridStateFactory(
077: request);
078: request.setAttribute(KEY, factory);
079: return factory;
080: }
081: }
082:
083: private final ServletRequest _request;
084: private final HashMap/*<String, DataGridStateCodec>*/_cache;
085:
086: private DataGridStateFactory(ServletRequest request) {
087: _request = request;
088: _cache = new HashMap/*<String, DataGridStateCodec>*/();
089: }
090:
091: /**
092: * <p>
093: * Lookup a {@link DataGridState} object given a data grid identifier.
094: * </p>
095: * <p>
096: * This method will use the default {@link DataGridConfig} object when returning a data grid specific
097: * implementation of the {@link DataGridState} object. In order to specify a {@link DataGridConfig},
098: * the {@link DataGridStateFactory#getDataGridState(String, DataGridConfig)} can be supplied
099: * with a specific data grid configuration.
100: * </p>
101: *
102: * @param name the name of a data grid.
103: * @return the {@link DataGridState} for the data grid with the given name
104: */
105: public final DataGridState getDataGridState(String name) {
106: return getDataGridState(name, DEFAULT_DATA_GRID_CONFIG);
107: }
108:
109: /**
110: * <p>
111: * Lookup a {@link DataGridState} object given a data grid identifier and a specific
112: * {@link DataGridConfig} object.
113: * </p>
114: *
115: * @param name the name of the data grid
116: * @param config the {@link DataGridConfig} object to use when creating the
117: * grid's {@link DataGridState} object.
118: * @return the data grid state object
119: */
120: public final DataGridState getDataGridState(String name,
121: DataGridConfig config) {
122: if (config == null)
123: throw new IllegalArgumentException(
124: Bundle
125: .getErrorString("DataGridStateFactory_nullDataGridConfig"));
126:
127: DataGridStateCodec codec = lookupCodec(name, config);
128: DataGridState state = codec.getDataGridState();
129: return state;
130: }
131:
132: /**
133: * <p>
134: * Lookup a {@link DataGridURLBuilder} object given a data grid identifier.
135: * </p>
136: * <p>
137: * This method will use the default {@link DataGridConfig} object when returning a data grid specific
138: * implementation of the {@link DataGridURLBuilder} object. In order to specify a {@link DataGridConfig},
139: * the {@link DataGridStateFactory#getDataGridURLBuilder(String, DataGridConfig)} can be supplied
140: * with a specific data grid configuration.
141: * </p>
142: *
143: * @param name the name of the data grid
144: * @return the {@link DataGridURLBuilder} for the data grid with the given name
145: */
146: public final DataGridURLBuilder getDataGridURLBuilder(String name) {
147: return getDataGridURLBuilder(name, DEFAULT_DATA_GRID_CONFIG);
148: }
149:
150: /**
151: * <p>
152: * Lookup a {@link DataGridURLBuilder} object given a data grid identifier and a specific
153: * {@link DataGridConfig} object.
154: * </p>
155: *
156: * @param name the name of the data grid
157: * @param config the {@link DataGridConfig} object to use when creating the
158: * grid's {@link DataGridURLBuilder} object.
159: * @return the URL builder for a data grid's state object
160: */
161: public final DataGridURLBuilder getDataGridURLBuilder(String name,
162: DataGridConfig config) {
163: if (config == null)
164: throw new IllegalArgumentException(
165: Bundle
166: .getErrorString("DataGridStateFactory_nullDataGridConfig"));
167:
168: DataGridStateCodec codec = lookupCodec(name, config);
169: DataGridURLBuilder builder = codec.getDataGridURLBuilder();
170: return builder;
171: }
172:
173: /**
174: * <p>
175: * Convenience method that allows a {@link DataGridState} object from a client to
176: * be attached to the factory. This allows subsequent calls to retrieve this same {@link DataGridState}
177: * instance.
178: * </p>
179: *
180: * @param name the name of the data grid
181: * @param state the {@link DataGridState} object to attach
182: */
183: public final void attachDataGridState(String name,
184: DataGridState state) {
185: DataGridStateCodec codec = lookupCodec(name,
186: DEFAULT_DATA_GRID_CONFIG);
187: codec.setDataGridState(state);
188: }
189:
190: private final DataGridStateCodec lookupCodec(String name,
191: DataGridConfig config) {
192: DataGridStateCodec codec = null;
193: if (_cache.containsKey(name))
194: codec = (DataGridStateCodec) _cache.get(name);
195: else {
196: assert config != null;
197: codec = config.createStateCodec(_request, name);
198: _cache.put(name, codec);
199: }
200:
201: return codec;
202: }
203: }
|