1 package fr.improve.struts.taglib.layout.sort; 2 3 import java.io.Serializable ; 4 import java.util.ArrayList ; 5 import java.util.Collections ; 6 import java.util.Enumeration ; 7 import java.util.HashMap ; 8 import java.util.Iterator ; 9 import java.util.List ; 10 import java.util.Locale ; 11 import java.util.Map ; 12 import java.util.MissingResourceException ; 13 import java.util.Vector ; 14 15 import javax.servlet.http.HttpServletRequest ; 16 import javax.servlet.jsp.tagext.Tag ; 17 18 import org.apache.commons.logging.Log; 19 import org.apache.commons.logging.LogFactory; 20 import org.apache.struts.Globals; 21 import org.apache.struts.util.RequestUtils; 22 23 import fr.improve.struts.taglib.layout.datagrid.DatagridTag; 24 import fr.improve.struts.taglib.layout.field.AbstractLayoutFieldTag; 25 import fr.improve.struts.taglib.layout.field.DateFieldTag; 26 import fr.improve.struts.taglib.layout.pager.PagerTag; 27 import fr.improve.struts.taglib.layout.skin.Skin; 28 import fr.improve.struts.taglib.layout.tab.TabTag; 29 import fr.improve.struts.taglib.layout.util.LayoutUtils; 30 import fr.improve.struts.taglib.layout.util.ParentFinder; 31 39 public class SortUtil implements Serializable { 40 43 private static final Log LOG = LogFactory.getLog(SortUtil.class); 44 45 48 public static final String SORT_UTIL_CLASS = "sortutil.class"; 49 50 53 public static final String SORTUTIL_KEY = 54 "fr.improve.struts.taglib.layout.sort.SortUtil"; 55 56 59 public static final String SORTUTIL_COLLECTION = "layoutCollection"; 60 61 64 public static final String SORTUTIL_PROPERTY = "layoutCollectionProperty"; 65 66 69 public static final String SORT_UTIL_ACTION = "sortutil.action"; 70 71 74 protected Vector collections = new Vector (); 75 76 79 protected Map sortState = new HashMap (); 80 81 84 protected Map attributes = new HashMap (); 85 86 89 protected Map parameters = new HashMap (); 90 91 94 protected String url; 95 96 99 protected int index = 0; 100 101 104 protected SortRules sortRules; 105 106 protected int hashCode; 107 108 114 public static short getSortState(HttpServletRequest in_request, String in_property) { 115 SortUtil lc_util = (SortUtil) in_request.getAttribute(SORTUTIL_KEY); 116 if (lc_util==null || lc_util.sortState.isEmpty()) { 117 return 0; 118 } else { 119 Object [] lc_sort = (Object []) lc_util.sortState.values().iterator().next(); 120 if (in_property.equals(lc_sort[0])) { 121 return ((Integer )lc_sort[1]).shortValue(); 122 } else { 123 return 0; 124 } 125 } 126 } 127 128 public void initSortUtil(HttpServletRequest in_request, String in_url) { 129 130 Enumeration lc_enumeration = in_request.getParameterNames(); 133 boolean hasUrlParameter = in_url.indexOf('?')!=-1; 134 parameters.clear(); 135 while (lc_enumeration.hasMoreElements()) { 136 String lc_key = (String ) lc_enumeration.nextElement(); 137 if (!hasUrlParameter || in_url.indexOf(lc_key + "=") ==-1) { 138 parameters.put(lc_key, in_request.getParameter(lc_key)); 139 } 140 } 141 142 lc_enumeration = in_request.getAttributeNames(); 144 attributes.clear(); 145 while (lc_enumeration.hasMoreElements()) { 146 Object lc_key = lc_enumeration.nextElement(); 147 Object lc_value = in_request.getAttribute(lc_key.toString()); 148 if (lc_value instanceof Tag ) { 149 LOG.debug("Skipping JSP tag " + lc_key + " : " + lc_value); 151 continue; 152 } 153 if (ParentFinder.TAGS_KEY.equals(lc_key)) { 154 LOG.debug("Skipping JSP tags stack"); 156 continue; 157 } 158 if (DateFieldTag.CALENDAR_KEY.equals(lc_key)) { 159 LOG.debug("Skipping calendar code"); 161 continue; 162 } 163 if (DatagridTag.LOADED.equals(lc_key)) { 164 LOG.debug("Skipping datagrid code"); 166 continue; 167 } 168 169 if (AbstractLayoutFieldTag.FIRST_ERROR_FIELD_FOCUS.equals(lc_key)) { 170 LOG.debug("Skipping first field in error focus"); 171 continue; 172 } 173 174 if (((String )lc_key).startsWith("javax.servlet")) { 175 LOG.debug("Skipping servlet information " + lc_key); 177 continue; 178 } 179 180 if (((String )lc_key).startsWith("atg.servlet")) { 181 LOG.debug("Skipping dynamo information " + lc_key); 183 continue; 184 } 185 186 if (((String )lc_key).startsWith("_")) { 187 if (lc_value!=null && lc_value.getClass().getName().indexOf("atg.")!=-1) { 188 LOG.debug("Skipping unsafe atg data " + lc_key); 190 continue; 191 } 192 } 193 194 if (((String )lc_key).equals(Globals.ERROR_KEY) && !LayoutUtils.getSkin(in_request.getSession()).getSortKeepErrorMessage()) { 195 LOG.debug("Skipping Struts error messages " + lc_key); 197 continue; 198 } 199 200 if (((String )lc_key).startsWith(TabTag.ATG_FIX_KEY)) { 201 LOG.debug("Skipping dynamo tabs cookie bug fix"); 203 continue; 204 } 205 206 LOG.debug("Adding " + lc_key + " : " + lc_value); 207 attributes.put(lc_key, lc_value); 208 } 209 210 url = in_url; 212 213 } 219 220 public static void addCollection( 221 Object in_list, 222 HttpServletRequest in_request) { 223 224 SortUtil lc_sortUtil = 226 (SortUtil) in_request.getSession().getAttribute(SORTUTIL_KEY); 227 if (lc_sortUtil==null || in_request.getAttribute(SORTUTIL_KEY + "aaa")==null) { 228 lc_sortUtil = createSortUtil(in_request); 229 in_request.getSession().setAttribute(SORTUTIL_KEY, lc_sortUtil); 230 in_request.setAttribute(SORTUTIL_KEY + "aaa", "aaa"); 231 } 232 lc_sortUtil.collections.add(in_list); 233 } 234 237 protected void buildRequest(HttpServletRequest in_request) { 238 Iterator lc_iterator = attributes.keySet().iterator(); 239 while (lc_iterator.hasNext()) { 240 Object lc_key = lc_iterator.next(); 241 in_request.setAttribute(lc_key.toString(), attributes.get(lc_key)); 242 } 243 } 244 245 248 protected void setCurrentPage(HttpServletRequest in_request) { 249 String lc_name = in_request.getParameter(PagerTag.PAGER_SESSION_KEY); 250 String lc_value = in_request.getParameter(PagerTag.PAGE_NUMBER_KEY); 251 if (lc_name!=null && lc_value!=null && in_request.getSession().getAttribute(lc_name)!=null) { 252 in_request.getSession().setAttribute(lc_name, lc_value); 253 } 254 } 255 256 257 260 protected String buildURL(HttpServletRequest in_request) { 261 Iterator lc_iterator = parameters.keySet().iterator(); 262 StringBuffer lc_url = new StringBuffer (url); 263 boolean hasParameter = url.indexOf('?')!=-1; 264 while (lc_iterator.hasNext()) { 265 Object lc_key = lc_iterator.next(); 266 if (!PagerTag.PAGE_NUMBER_KEY.equals(lc_key)) { 267 if (hasParameter) { 268 lc_url.append("&"); 269 } else { 270 lc_url.append("?"); 271 hasParameter = true; 272 } 273 lc_url.append(LayoutUtils.encodeURL(lc_key.toString())); 274 lc_url.append("="); 275 276 Object lc_param = parameters.get(lc_key); 277 if (lc_param!=null) { 278 lc_url.append(LayoutUtils.encodeURL(lc_param.toString())); 279 } 280 } 281 } 282 return lc_url.toString(); 283 } 284 protected List getCollectionToSort(HttpServletRequest in_request) throws SortException { 285 String lc_indexAsString = in_request.getParameter(SORTUTIL_COLLECTION); 286 int lc_index; 287 try { 288 lc_index = Integer.parseInt(lc_indexAsString); 289 } catch (NumberFormatException e) { 290 throw new SortException("Impossible to find the collection to sort"); 291 } 292 List lc_list = null; 293 if (lc_index>=0) { 294 try { 295 lc_list = (List ) collections.get(lc_index); 296 } catch (ClassCastException e) { 297 throw new SortException("Impossible to sort a bean of type " + collections.get(lc_index).getClass()); 298 } 299 } 300 return lc_list; 301 } 302 protected String getPropertyToSort(HttpServletRequest in_request) throws SortException { 303 return in_request.getParameter(SORTUTIL_PROPERTY); 304 } 305 public List getSortedCollection() { 306 List lc_list = (List ) collections.get(index); 307 index++; 308 return lc_list; 309 } 310 313 public static String getURLForCollection( 314 String in_property, 315 HttpServletRequest in_request) { 316 317 SortUtil lc_sortUtil = (SortUtil) in_request.getSession().getAttribute(SORTUTIL_KEY); 319 if (lc_sortUtil==null || in_request.getAttribute(SORTUTIL_KEY + "aaa")==null) { 320 lc_sortUtil = createSortUtil(in_request); 321 in_request.getSession().setAttribute(SORTUTIL_KEY, lc_sortUtil); 322 in_request.setAttribute(SORTUTIL_KEY + "aaa", "aaa"); 323 } 324 325 String lc_action = null; 327 try { 328 lc_action = LayoutUtils.getSkin(in_request.getSession()).getProperty(SORT_UTIL_ACTION); 329 } catch (MissingResourceException e) { 330 lc_action = "sort.do"; 331 } 332 333 StringBuffer lc_buffer = new StringBuffer (in_request.getContextPath()); 335 lc_buffer.append("/"); 336 lc_buffer.append(lc_action); 337 lc_buffer.append("?"); 338 lc_buffer.append(SORTUTIL_COLLECTION); 339 lc_buffer.append('='); 340 lc_buffer.append(lc_sortUtil.collections.size()-1); 341 lc_buffer.append("&"); 342 lc_buffer.append(SORTUTIL_PROPERTY); 343 lc_buffer.append('='); 344 lc_buffer.append(in_property); 345 lc_buffer.append("&"); 346 lc_buffer.append("layoutCollectionState"); 347 lc_buffer.append('='); 348 lc_buffer.append(getRequestState(in_request)); 349 return lc_buffer.toString(); 350 } 351 public String sort(HttpServletRequest in_request) throws SortException { 352 validateRequest(in_request); 354 355 List lc_collection = getCollectionToSort(in_request); 357 358 String lc_property = getPropertyToSort(in_request); 360 361 sortCollection(lc_collection, lc_property); 363 364 buildRequest(in_request); 366 367 setCurrentPage(in_request); 369 370 in_request.getSession().removeAttribute(SORTUTIL_KEY); 372 in_request.setAttribute(SORTUTIL_KEY, this); 373 374 376 return buildURL(in_request); 377 } 378 protected void sortCollection(List in_collection, String in_property) { 379 if (in_property!=null && in_property.length()!=0) { 381 382 List copy = new ArrayList (in_collection); 384 385 Collections.sort(in_collection, new BeanComparator(in_property, sortRules)); 387 int order = 1; 389 390 if (copy.equals(in_collection)) { 391 Collections.reverse(in_collection); 393 order = -1; 394 } 395 396 int index = collections.indexOf(in_collection); 398 sortState.put(new Integer (index), new Object []{in_property, new Integer (order)}); 399 } 400 } 401 402 403 406 private static final String getRequestState(HttpServletRequest in_request) { 407 Integer lc_value = (Integer ) in_request.getSession().getAttribute(SORTUTIL_KEY + "bbb"); 408 if (lc_value==null) { 409 lc_value = new Integer (0); 410 in_request.getSession().setAttribute(SORTUTIL_KEY + "bbb", lc_value); 411 } 412 return lc_value.toString(); 413 } 414 415 419 protected final void validateRequest(HttpServletRequest in_request) throws SortException { 420 Integer lc_integer = (Integer ) in_request.getSession().getAttribute(SORTUTIL_KEY + "bbb"); 422 if (lc_integer == null) { 423 throw new SortException("session state unknown"); 424 } 425 426 String lc_value = in_request.getParameter("layoutCollectionState"); 428 if (lc_value == null) { 429 throw new SortException("bad request"); 430 } 431 432 Skin lc_skin = LayoutUtils.getSkin(in_request.getSession()); 435 Integer lc_integerValue = new Integer (lc_value); 436 437 if (lc_skin.isSortTokenRequired() && lc_integer.compareTo(lc_integerValue)>0) { 438 throw new SortException("bad session state"); 439 } 440 441 in_request.getSession().setAttribute(SORTUTIL_KEY + "bbb", new Integer (lc_integer.intValue()+1)); 443 } 444 445 449 public static SortUtil createSortUtil(HttpServletRequest in_request) { 450 451 SortUtil lc_sortUtil; 452 try { 453 String lc_sortUtilClass = LayoutUtils.getSkin(in_request.getSession()).getProperty(SORT_UTIL_CLASS); 455 try { 456 lc_sortUtil = (SortUtil) RequestUtils.applicationInstance(lc_sortUtilClass); 458 } catch (Exception e) { 459 e.printStackTrace(); 460 lc_sortUtil = new SortUtil(); 461 } 462 } catch (MissingResourceException mre) { 463 lc_sortUtil = new SortUtil(); 464 } 465 466 lc_sortUtil.sortRules = getRules(in_request); 468 469 lc_sortUtil.initSortUtil(in_request, in_request.getServletPath()); 471 return lc_sortUtil; 472 } 473 474 477 private static SortRules getRules(HttpServletRequest in_request) { 478 Locale locale = LayoutUtils.getLocale(in_request); 479 return LayoutUtils.getSkin(in_request.getSession()).getSortRules(locale); 480 } 481 482 } 483 | Popular Tags |