1 16 package com.blandware.atleap.webapp.action.core.siteMap; 17 18 import com.blandware.atleap.common.Constants; 19 import com.blandware.atleap.common.util.QueryInfo; 20 import com.blandware.atleap.common.util.StringUtil; 21 import com.blandware.atleap.model.core.ActionPage; 22 import com.blandware.atleap.model.core.BaseObject; 23 import com.blandware.atleap.model.core.ContentDocument; 24 import com.blandware.atleap.model.core.ContentPage; 25 import com.blandware.atleap.model.core.Page; 26 import com.blandware.atleap.service.core.ContentResourceManager; 27 import com.blandware.atleap.service.core.PageManager; 28 import com.blandware.atleap.webapp.action.core.BaseAction; 29 import com.blandware.atleap.webapp.util.core.GlobalProperties; 30 import com.blandware.atleap.webapp.util.core.WebappConstants; 31 import com.blandware.atleap.webapp.util.core.WebappUtil; 32 import org.apache.commons.validator.GenericValidator; 33 import org.apache.struts.Globals; 34 import org.apache.struts.action.ActionForm; 35 import org.apache.struts.action.ActionForward; 36 import org.apache.struts.action.ActionMapping; 37 38 import javax.servlet.http.HttpServletRequest ; 39 import javax.servlet.http.HttpServletResponse ; 40 import java.io.Serializable ; 41 import java.lang.reflect.InvocationTargetException ; 42 import java.util.ArrayList ; 43 import java.util.HashMap ; 44 import java.util.Iterator ; 45 import java.util.List ; 46 import java.util.Locale ; 47 import java.util.Map ; 48 49 62 public final class ViewSiteMapAction extends BaseAction { 63 72 public ActionForward execute(ActionMapping mapping, ActionForm form, 73 HttpServletRequest request, HttpServletResponse response) throws Exception { 74 75 MapSection rootSection = null; 76 78 if ( rootSection == null ) { 79 rootSection = new MapSection("/"); 81 82 PageManager pageManager = (PageManager) getBean(Constants.PAGE_MANAGER_BEAN); 83 ContentResourceManager resourceManager = (ContentResourceManager) getBean(Constants.CONTENT_RESOURCE_MANAGER_BEAN); 84 85 Locale locale = (Locale ) request.getSession().getAttribute(Globals.LOCALE_KEY); 87 String localeIdentifier = locale.getLanguage(); 88 Map queryParameters = new HashMap (); 89 queryParameters.put("localeIdentifier", localeIdentifier); 90 QueryInfo queryInfo = new QueryInfo(); 91 queryInfo.setQueryParameters(queryParameters); 92 queryInfo.setOrderByClause("r.uri asc"); 93 94 List documents = resourceManager.listContentDocuments(queryInfo).asList(); 96 97 queryInfo.setWhereClause("page.active = 'T'"); 99 queryInfo.setOrderByClause("page.uri asc"); 100 101 List contentPages = pageManager.listContentPages(queryInfo).asList(); 103 104 List actionPages = pageManager.listActionPages(queryInfo).asList(); 106 107 109 for ( Iterator i = contentPages.iterator(); i.hasNext(); ) { 111 ContentPage contentPage = (ContentPage) i.next(); 112 String uri = contentPage.getUri(); 113 rootSection.addElement(contentPage, uri.substring(WebappConstants.CONTENT_PAGES_URI_PREFIX.length()), localeIdentifier, request); 114 } 115 116 for ( Iterator i = actionPages.iterator(); i.hasNext(); ) { 118 ActionPage actionPage = (ActionPage) i.next(); 119 String uri = actionPage.getUri(); 120 rootSection.addElement(actionPage, uri, localeIdentifier, request); 121 } 122 123 for ( Iterator i = documents.iterator(); i.hasNext(); ) { 125 ContentDocument contentDocument = (ContentDocument) i.next(); 126 String uri = contentDocument.getUri(); 127 rootSection.addElement(contentDocument, uri.substring(Constants.RESOURCES_URI_PREFIX.length()), localeIdentifier, request); 128 } 129 130 } 132 133 String patternString = GlobalProperties.getInstance(request.getSession().getServletContext()).getString(Constants.MAP_SECTION_INDEX_PATTERNS_PROPERTY); 135 List patternList = new ArrayList (); 136 if ( !GenericValidator.isBlankOrNull(patternString) ) { 137 String [] patterns = patternString.split(","); 138 for ( int i = 0; i < patterns.length; i++ ) { 139 String pattern = patterns[i]; 140 if ( !GenericValidator.isBlankOrNull(pattern) ) { 141 patternList.add(pattern.trim()); 142 } 143 } 144 } 145 146 rootSection = processSection(rootSection, patternList, request); 149 150 request.setAttribute(WebappConstants.SITE_MAP_KEY, rootSection); 152 153 return mapping.findForward("viewSiteMap"); 154 } 155 156 165 public MapSection processSection(MapSection section, List patterns, HttpServletRequest request) throws IllegalAccessException , NoSuchMethodException , InvocationTargetException { 166 MapSection newSection = new MapSection(section.getPath()); 167 newSection.setIndexElement(null); 168 List elements = section.getElements(); 169 for ( Iterator i = elements.iterator(); i.hasNext(); ) { 170 MapSection.SectionElement element = (MapSection.SectionElement) i.next(); 171 boolean indexElement = false; 172 if ( WebappUtil.isUserInRole(element.getRoles(), false, "name", request) ) { 173 for ( Iterator j = patterns.iterator(); j.hasNext() && newSection.getIndexElement() == null; ) { 174 String pattern = (String ) j.next(); 175 String uri = element.getUri(); 176 int k = uri.indexOf(section.getPath()); 181 if ( matchesPattern(uri.substring(k + section.getPath().length()), pattern) ) { 182 newSection.setIndexElement(element); 183 indexElement = true; 184 } 185 } 186 if ( !indexElement ) { 187 newSection.getElements().add(element); 189 } 190 } 191 } 192 193 List sections = section.getSections(); 195 196 for ( Iterator i = sections.iterator(); i.hasNext(); ) { 197 MapSection subSection = processSection((MapSection) i.next(), patterns, request); 198 if ( subSection.getElements().isEmpty() && subSection.getIndexElement() == null ) { 199 for ( Iterator j = subSection.getSections().iterator(); j.hasNext(); ) { 201 MapSection s = (MapSection) j.next(); 202 newSection.addSection(s); 203 } 204 } else { 205 newSection.addSection(subSection); 206 } 207 } 208 209 if ( newSection.getElements().size() == 1 && newSection.getIndexElement() == null ) { 211 newSection.setIndexElement((MapSection.SectionElement) newSection.getElements().get(0)); 212 newSection.setElements(new ArrayList ()); 213 } 214 215 return newSection; 216 } 217 218 225 protected boolean matchesPattern(String uri, String pattern) { 226 int dot = uri.lastIndexOf('.'); 229 if ( dot != -1 ) { 230 uri = uri.substring(0, dot); 231 } 232 int k = pattern.indexOf('*'); 233 if ( k == -1 ) { 234 return uri.equals(pattern); 236 } else { 237 if ( k == 0 ) { 238 return uri.endsWith(pattern.substring(1)); 240 } else if ( k == pattern.length() - 1 ) { 241 return uri.startsWith(pattern.substring(0, k)); 243 } else { 244 String start = pattern.substring(0, k); 246 String end = pattern.substring(k + 1, pattern.length()); 247 return uri.startsWith(start) && uri.endsWith(end); 248 } 249 } 250 } 251 252 255 public static class MapSection implements Serializable { 256 257 260 protected String path; 261 262 265 protected SectionElement indexElement; 266 267 270 protected List sections = new ArrayList (); 271 272 275 protected List elements = new ArrayList (); 276 277 282 public MapSection(String path) { 283 if ( path == null ) { 284 throw new IllegalArgumentException ("Path is null"); 285 } 286 this.path = path; 287 } 288 289 294 public String getPath() { 295 return path; 296 } 297 298 303 public void setPath(String path) { 304 this.path = path; 305 } 306 307 312 public SectionElement getIndexElement() { 313 return indexElement; 314 } 315 316 321 public void setIndexElement(SectionElement indexElement) { 322 this.indexElement = indexElement; 323 } 324 325 330 public List getSections() { 331 return sections; 332 } 333 334 339 public void setSections(List sections) { 340 this.sections = sections; 341 } 342 343 348 public List getElements() { 349 return elements; 350 } 351 352 357 public void setElements(List elements) { 358 this.elements = elements; 359 } 360 361 366 public void addSection(MapSection section) { 367 sections.add(section); 368 } 369 370 378 public void addElement(BaseObject obj, String path, String localeIdentifier, HttpServletRequest request) { 379 380 int k = path.indexOf("/", this.path.length()); 382 if ( k != -1 ) { 383 String sectionPath = path.substring(0, k + 1); 386 MapSection section = findSection(sectionPath); 388 if ( section == null ) { 389 section = new MapSection(sectionPath); 391 addSection(section); 392 } 393 section.addElement(obj, path, localeIdentifier, request); 395 } else { 396 SectionElement se = new SectionElement(obj, localeIdentifier, request); 398 elements.add(se); 399 } 400 } 401 402 408 protected MapSection findSection(String path) { 409 for ( Iterator i = sections.iterator(); i.hasNext(); ) { 410 MapSection section = (MapSection) i.next(); 411 if ( section.getPath().equals(path) ) { 412 return section; 413 } 414 } 415 return null; 416 } 417 418 public boolean equals(Object o) { 419 if ( this == o ) { 420 return true; 421 } 422 if ( !(o instanceof MapSection) ) { 423 return false; 424 } 425 426 final MapSection mapSection = (MapSection) o; 427 428 if ( path != null ? !path.equals(mapSection.path) : mapSection.path != null ) { 429 return false; 430 } 431 432 return true; 433 } 434 435 public int hashCode() { 436 return (path != null ? path.hashCode() : 0); 437 } 438 439 442 public static class SectionElement implements Serializable { 443 444 public static final String SECTION_ELEMENT_PAGE = "page"; 445 public static final String SECTION_ELEMENT_DOCUMENT = "document"; 446 447 450 protected String title; 451 452 455 protected String uri; 456 457 460 protected List roles; 461 462 465 protected String type; 466 467 474 public SectionElement(BaseObject obj, String localeIdentifier, HttpServletRequest request) { 475 if ( !(obj instanceof ContentPage) && !(obj instanceof ActionPage) && !(obj instanceof ContentDocument) ) { 476 throw new IllegalArgumentException ("Cannot wrap instance of " + obj.getClass().getName()); 477 } 478 479 if ( obj instanceof Page ) { 480 Page page = (Page) obj; 481 title = StringUtil.shortenString(WebappUtil.getFieldValue(page.getTitle(), localeIdentifier, request, true), 40); 482 uri = WebappUtil.getActionMappingURL(page.getUri(), null, request, WebappConstants.URL_TYPE_CONTEXT_RELATIVE); 483 if ( GenericValidator.isBlankOrNull(title) ) { 484 title = uri; 485 } 486 type = SECTION_ELEMENT_PAGE; 487 if ( page instanceof ContentPage ) { 488 ContentPage cp = (ContentPage) page; 489 roles = cp.getRoles(); 490 } else if ( page instanceof ActionPage ) { 491 ActionPage ap = (ActionPage) page; 492 roles = WebappUtil.getAPRoleNamesAsList(ap.getUri(), request); 493 } 494 } else { 495 ContentDocument document = (ContentDocument) obj; 496 if ( !GenericValidator.isBlankOrNull(document.getDescription()) ) { 497 title = StringUtil.shortenString(document.getDescription(), 40); 498 } else { 499 title = document.getUri(); 500 } 501 uri = document.getUri() + "?view=true"; 502 roles = document.getRoles(); 503 type = SECTION_ELEMENT_DOCUMENT; 504 } 505 506 if ( roles == null ) { 507 roles = new ArrayList (); 508 } 509 } 510 511 516 public String getTitle() { 517 return title; 518 } 519 520 525 public void setTitle(String title) { 526 this.title = title; 527 } 528 529 534 public String getUri() { 535 return uri; 536 } 537 538 543 public void setUri(String uri) { 544 this.uri = uri; 545 } 546 547 552 public List getRoles() { 553 return roles; 554 } 555 556 561 public void setRoles(List roles) { 562 this.roles = roles; 563 } 564 565 570 public String getType() { 571 return type; 572 } 573 574 579 public void setType(String type) { 580 this.type = type; 581 } 582 583 public boolean equals(Object o) { 584 if ( this == o ) { 585 return true; 586 } 587 if ( !(o instanceof SectionElement) ) { 588 return false; 589 } 590 591 final SectionElement sectionElement = (SectionElement) o; 592 593 if ( !type.equals(sectionElement.type) ) { 594 return false; 595 } 596 if ( !uri.equals(sectionElement.uri) ) { 597 return false; 598 } 599 600 return true; 601 } 602 603 public int hashCode() { 604 int result; 605 result = uri.hashCode(); 606 result = 29 * result + type.hashCode(); 607 return result; 608 } 609 610 } 611 } 612 613 } | Popular Tags |