1 15 16 package net.mlw.vlh.web.mvc; 17 18 import java.util.Map ; 19 20 import javax.servlet.http.HttpServletRequest ; 21 import javax.servlet.http.HttpSession ; 22 23 import net.mlw.vlh.ValueList; 24 import net.mlw.vlh.ValueListHandler; 25 import net.mlw.vlh.ValueListInfo; 26 import net.mlw.vlh.web.ValueListRequestUtil; 27 import net.mlw.vlh.web.tag.TableInfo; 28 29 import org.apache.commons.logging.Log; 30 import org.apache.commons.logging.LogFactory; 31 32 45 public class ValueListHandlerHelper 46 { 47 50 private static final Log LOGGER = LogFactory.getLog(ValueListHandlerHelper.class); 51 52 private ValueListHandler _valueListHandler = null; 53 54 57 public void setValueListHandler(ValueListHandler valueListHandler) 58 { 59 _valueListHandler = valueListHandler; 60 } 61 62 67 public ValueListHandler getValueListHandler() 68 { 69 return _valueListHandler; 70 } 71 72 79 public ValueList getValueList(String name, ValueListInfo info) 80 { 81 return getValueListHandler().getValueList(name, info); 82 } 83 84 92 public ValueListInfo getValueListInfo(HttpServletRequest request) 93 { 94 LOGGER.warn("Gettting a valueListInfo for a table with the default id!"); 95 return getValueListInfo(request, TableInfo.DEFAULT_ID); 96 } 97 98 114 115 public ValueListInfo getValueListInfo(HttpServletRequest request, String tableId) 116 { 117 if (request == null) 118 { 119 final String message = "getValueListInfo - request is null!"; 120 LOGGER.error(message); 121 throw new NullPointerException (message); 122 } 123 if (tableId == null) 124 { 125 LOGGER.error("TableId is null!"); 126 tableId = TableInfo.DEFAULT_ID; 127 } 128 129 if (LOGGER.isDebugEnabled()) 130 { 131 LOGGER.debug("Start to getting ValuelistInfo for the tableId '" + tableId + "'."); 132 } 133 134 Map requestParamsMap = ValueListRequestUtil.getRequestParameterMap(request, tableId); 135 136 ValueListInfo info = null; 137 138 if (requestParamsMap.isEmpty()) 139 { 140 141 if (LOGGER.isDebugEnabled()) 142 { 143 LOGGER.debug("Try to get backup of an info from the session for the tableId '" + tableId + "'."); 144 } 145 146 HttpSession session = request.getSession(); 147 if (session != null) 148 { 149 info = (ValueListInfo) session.getAttribute(tableId); 150 } 151 else 152 { 153 LOGGER.warn("ValueListInfo for tableId '" + tableId + "' was not found in the sesion due to session is null!"); 154 } 155 156 if (LOGGER.isDebugEnabled()) 157 { 158 if (info == null) 159 { 160 LOGGER.debug("Backup of the ValueListInfo for the tableId '" + tableId + "'was NOT found in the session."); 161 } 162 else 163 { 164 LOGGER.debug("Backup of the ValueListInfo for the tableId '" + tableId + "' was FOUND in the session"); 165 } 166 } 167 168 } 169 else 170 { 171 info = ValueListRequestUtil.buildValueListInfo(request, tableId); 172 if (LOGGER.isDebugEnabled()) 173 { 174 LOGGER.debug("ValueListInfo for the tableId '" + tableId + "' was build from the request's params."); 175 } 176 } 177 178 if (info == null) 179 { 180 if (LOGGER.isInfoEnabled()) 181 { 182 LOGGER.info("Creating a default ValueListInfo for the tableId '" + tableId + "'"); 183 } 184 info = new ValueListInfo(); 185 } 186 return info; 187 188 } 189 190 208 public void backupAndSet(HttpServletRequest request, ValueList valueList, String valueListName, String tableId) 209 { 210 211 backupInfoFor(request, valueList == null ? null : valueList.getValueListInfo(), tableId); 212 setValueListTo(request, valueList, valueListName); 213 214 } 215 216 224 public void setValueListTo(HttpServletRequest request, ValueList valueList, String valueListName) 225 { 226 if (valueListName != null && valueListName.length() > 0) 227 { 228 request.setAttribute(valueListName, valueList); 229 if (LOGGER.isDebugEnabled()) 230 { 231 LOGGER.debug("ValueList '" + valueListName + "' is stored in the request."); 232 } 233 } 234 else 235 { 236 LOGGER.error("Skiped storing of the ValueList to the request, valueListName is null or empty!"); 237 } 238 } 239 240 250 public void backupInfoFor(HttpServletRequest request, ValueListInfo info) 251 { 252 LOGGER.warn("Backing Up with the default table id "); 253 backupInfoFor(request, info, TableInfo.DEFAULT_ID); 254 255 } 256 257 267 public void backupInfoFor(HttpServletRequest request, ValueListInfo info, String tableId) 268 { 269 270 if (tableId != null && tableId.length() > 0) 271 { 272 if (info == null) 273 { 274 request.getSession().removeAttribute(tableId); 275 276 if (LOGGER.isWarnEnabled()) 277 { 278 LOGGER.warn("ValueListInfo to back up is null! ValueListInfo for the tableId '" + tableId 279 + "' was removed from the session."); 280 } 281 } 282 else 283 { 284 request.getSession().setAttribute(tableId, info); 285 if (LOGGER.isDebugEnabled()) 286 { 287 LOGGER.debug("ValueListInfo for tableId '" + tableId + "' was saved in session."); 288 } 289 } 290 } 291 else 292 { 293 throw new NullPointerException ( 294 "The session unique attribute tableId is null or empty, skipped backUp of valueListInfo into the session!"); 295 } 296 } 297 298 307 public String getActionTempParam(HttpServletRequest request, String name) 308 { 309 return request.getParameter(ValueListRequestUtil.getActionTempParamName(name)); 310 } 311 312 321 public String getActionParam(HttpServletRequest request, String name) 322 { 323 return request.getParameter(name); 324 } 325 326 337 public int getLastPageAfterDelete(ValueListInfo info) 338 { 339 int lastPage = info.getPagingPage(); 340 341 int lastCount = info.getTotalNumberOfEntries() % info.getPagingNumberPer(); 342 343 if (lastCount == 1 && lastPage > 1) 344 { 345 lastPage--; 346 } 347 return lastPage; 348 } 349 } | Popular Tags |