1 /***
2 * Copyright (c) 2003 held jointly by the individual authors.
3 *
4 * This library is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License as published
6 * by the Free Software Foundation; either version 2.1 of the License, or
7 * (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; with out even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this library; if not, write to the Free Software Foundation,
16 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17 *
18 * > http://www.gnu.org/copyleft/lesser.html
19 * > http://www.opensource.org/licenses/lgpl-license.php
20 */
21 package net.mlw.vlh.web.tag;
22
23 import javax.servlet.http.HttpServletRequest;
24 import javax.servlet.jsp.JspException;
25
26 import net.mlw.vlh.ValueList;
27 import net.mlw.vlh.ValueListHandler;
28 import net.mlw.vlh.ValueListInfo;
29 import net.mlw.vlh.web.ValueListRequestUtil;
30 import net.mlw.vlh.web.util.JspUtils;
31
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34 import org.springframework.web.context.WebApplicationContext;
35 import org.springframework.web.context.support.WebApplicationContextUtils;
36
37
38
39 /*** This tag is used if the pull pattern is used.
40 *
41 * @todo Document this tag.
42 *
43 * @author Matthew L. Wilson, A.Zachar
44 * @version $Revision: 1.18 $ $Date: 2005/11/23 14:51:53 $
45 */
46 public class ValueListRetriever extends ConfigurableTag
47 {
48 private static final long serialVersionUID = 8975676126886164801L;
49
50 /***
51 * Logger for this class
52 */
53 private static final Log LOGGER = LogFactory.getLog(ValueListRetriever.class);
54
55 private String valueListName = null;
56
57 private String focusProperty = null;
58
59 private String focusValue = null;
60
61 /***
62 * @see javax.servlet.jsp.tagext.Tag#doStartTag()
63 */
64 public int doStartTag() throws JspException
65 {
66 getCellAttributes().getMap().clear();
67 return super.doStartTag();
68 }
69
70 /***
71 * @see javax.servlet.jsp.tagext.Tag#doEndTag()
72 */
73 public int doEndTag() throws JspException
74 {
75 ValueListSpaceTag rootTag = (ValueListSpaceTag) JspUtils.getParent(this, ValueListSpaceTag.class);
76
77 ValueListInfo info = ValueListRequestUtil.buildValueListInfo((HttpServletRequest) pageContext.getRequest(), rootTag.getTableInfo()
78 .getId());
79 info.getFilters().putAll(getCellAttributes().getMap());
80
81 WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(pageContext.getServletContext());
82
83 if (context == null)
84 {
85 final String message = "Cannot locate the spring WebApplicationContext. Is it configured in your web.xml?";
86 LOGGER.error(message);
87 throw new JspException(message);
88 }
89
90 ValueListHandler vlh = (ValueListHandler) context.getBean(ValueListHandler.DEFAULT_SERVICE_NAME, ValueListHandler.class);
91 if (vlh == null)
92 {
93 final String message = "Cannot locate the ValueListHanlder in the WebApplicationContext, under the name: \""
94 + ValueListHandler.DEFAULT_SERVICE_NAME + "\"";
95 LOGGER.error(message);
96 throw new JspException(message);
97 }
98
99 info.setFocusValue(focusValue);
100 info.setFocusProperty(focusProperty);
101
102 if (LOGGER.isDebugEnabled())
103 {
104 LOGGER.debug("Focus settings was setted up. Focus property '" + focusProperty + "', focus value '" + focusValue + "'");
105 }
106
107 ValueList list = vlh.getValueList(valueListName, info);
108 pageContext.getRequest().setAttribute(rootTag.getTableInfo().getName(), list);
109 rootTag.setValueList(list);
110
111 if (LOGGER.isDebugEnabled())
112 {
113 LOGGER.debug("ValueList was retrieved from adapter inside of the jsp.");
114 }
115
116 release();
117
118 return EVAL_PAGE;
119 }
120
121 /***
122 * @param valueListName
123 * The valueListName to set.
124 */
125 public void setName(String valueListName)
126 {
127 this.valueListName = valueListName;
128 }
129
130 /***
131 * @return Returns the valueListName.
132 */
133 public String getName()
134 {
135 return valueListName;
136 }
137
138 /***
139 * @param focusProperty
140 * The focusProperty set usually name of column used to be id in
141 * table.
142 */
143 public void setFocusProperty(String focusProperty)
144 {
145 this.focusProperty = focusProperty;
146 }
147
148 /***
149 * @param focusValue
150 * The focusValue set value on which will be occured in the
151 * generated table.
152 */
153 public void setFocusValue(String focusValue)
154 {
155 this.focusValue = focusValue;
156 }
157
158 private void reset()
159 {
160 this.focusProperty = null;
161 this.focusValue = null;
162 this.valueListName = null;
163 }
164
165 /***
166 * Called on a Tag handler to release state.
167 * The page compiler guarantees that JSP page implementation
168 * objects will invoke this method on all tag handlers,
169 * but there may be multiple invocations on doStartTag and doEndTag in between.
170 *
171 * @see javax.servlet.jsp.tagext.Tag#release()
172 */
173 public void release()
174 {
175 super.release();
176 reset();
177 }
178 }