1 16 package com.blandware.atleap.webapp.taglib.core.content; 17 18 import com.blandware.atleap.common.Constants; 19 import com.blandware.atleap.model.core.ContentField; 20 import com.blandware.atleap.model.core.Layout; 21 import com.blandware.atleap.model.core.Page; 22 import com.blandware.atleap.service.core.ContentFieldManager; 23 import com.blandware.atleap.service.core.LayoutManager; 24 import com.blandware.atleap.service.core.PageManager; 25 import com.blandware.atleap.webapp.struts.ContentTilesRequestProcessor; 26 import com.blandware.atleap.webapp.struts.HeritableComponentDefinition; 27 import com.blandware.atleap.webapp.util.core.CacheUtil; 28 import com.blandware.atleap.webapp.util.core.WebappUtil; 29 import org.apache.commons.logging.Log; 30 import org.apache.commons.logging.LogFactory; 31 import org.apache.struts.Globals; 32 import org.apache.struts.tiles.ComponentDefinition; 33 import org.apache.struts.tiles.TilesUtil; 34 import org.springframework.context.ApplicationContext; 35 import org.springframework.web.context.support.WebApplicationContextUtils; 36 37 import javax.servlet.ServletContext ; 38 import javax.servlet.http.HttpServletRequest ; 39 import javax.servlet.jsp.JspTagException ; 40 import javax.servlet.jsp.PageContext ; 41 import javax.servlet.jsp.tagext.JspFragment ; 42 import javax.servlet.jsp.tagext.SimpleTagSupport ; 43 import java.io.IOException ; 44 import java.util.ArrayList ; 45 import java.util.Arrays ; 46 import java.util.Collection ; 47 import java.util.Collections ; 48 import java.util.Iterator ; 49 import java.util.LinkedList ; 50 import java.util.List ; 51 import java.util.Locale ; 52 53 109 public class ContentIteratorTag extends SimpleTagSupport { 110 111 protected transient final Log log = LogFactory.getLog(ContentIteratorTag.class); 112 113 protected ApplicationContext applicationCtx = null; 114 115 118 protected String index; 119 120 123 protected String identifier = null; 124 128 protected String definition = null; 129 133 protected String uri = null; 134 138 protected String locale = null; 139 143 protected Boolean ignore = Boolean.FALSE; 144 145 protected Iterator fieldIndicesIterator = null; 146 147 148 157 public String getIdentifier() { 158 return identifier; 159 } 160 161 166 public void setIdentifier(String identifier) { 167 this.identifier = identifier; 168 } 169 170 179 public String getUri() { 180 return uri; 181 } 182 183 188 public void setUri(String uri) { 189 this.uri = uri; 190 } 191 192 201 public String getDefinition() { 202 return definition; 203 } 204 205 210 public void setDefinition(String definition) { 211 this.definition = definition; 212 } 213 214 223 public String getIndex() { 224 return index; 225 } 226 227 232 public void setIndex(String index) { 233 this.index = index; 234 } 235 236 245 public String getLocale() { 246 return locale; 247 } 248 249 254 public void setLocale(String locale) { 255 this.locale = locale; 256 } 257 258 267 public Boolean getIgnore() { 268 return ignore; 269 } 270 271 276 public void setIgnore(Boolean ignore) { 277 this.ignore = ignore; 278 } 279 280 287 public void doTag() throws JspTagException , IOException { 288 289 PageContext pageContext = (PageContext ) getJspContext(); 290 HttpServletRequest request = (HttpServletRequest ) pageContext.getRequest(); 291 ServletContext servletContext = request.getSession().getServletContext(); 292 293 try { 294 295 if ( log.isDebugEnabled() ) { 296 log.debug("Start processing content fields."); 297 } 298 299 if ( applicationCtx == null ) { 300 applicationCtx = WebApplicationContextUtils.getRequiredWebApplicationContext(pageContext.getServletContext()); 301 } 302 303 if ( locale == null ) { 305 Locale l = (Locale ) pageContext.getAttribute(Globals.LOCALE_KEY, PageContext.SESSION_SCOPE); 306 if ( l != null ) { 307 locale = l.getLanguage(); 308 } 309 } 310 if ( locale == null ) { 311 locale = Locale.getDefault().getLanguage(); 312 } 313 314 if ( definition != null ) { 316 ComponentDefinition tmpDef = null; 317 try { 318 tmpDef = TilesUtil.getDefinition(definition, request, servletContext); 319 } catch ( Exception ex ) { 320 } 322 if ( tmpDef == null ) { 323 if ( ignore.booleanValue() ) { 324 return; 325 } 326 String errorMessage = "Specified definition '" + definition + "' has not been found"; 327 JspTagException e = new JspTagException (errorMessage); 328 throw e; 329 } 330 } else { 331 definition = (String ) pageContext.getAttribute(ContentTilesRequestProcessor.DEFINITION_NAME, PageContext.REQUEST_SCOPE); 332 } 333 if ( definition == null ) { 334 if ( ignore.booleanValue() ) { 335 return; 336 } 337 String errorMessage = "DEFINITION_NAME attribute has not been found in request. This can be used only on pages processed by action using tile definition."; 338 JspTagException e = new JspTagException (errorMessage); 339 throw e; 340 } 341 342 if ( uri != null ) { 344 PageManager pageManager = (PageManager) applicationCtx.getBean(Constants.PAGE_MANAGER_BEAN); 345 Page page = pageManager.findPageByUri(uri); 346 if ( page == null ) { 347 if ( ignore.booleanValue() ) { 348 return; 349 } 350 String errorMessage = "Specified page uri '" + uri + "' has not been found"; 351 JspTagException e = new JspTagException (errorMessage); 352 throw e; 353 } 354 } else { 355 uri = (String ) pageContext.getAttribute(ContentTilesRequestProcessor.PROCESSED_URI, PageContext.REQUEST_SCOPE); 356 } 357 if ( uri == null ) { 358 if ( ignore.booleanValue() ) { 359 return; 360 } 361 String errorMessage = "PROCESSED_URI attribute has not been found in request. This can be used only on pages processed by action using tile definition."; 362 JspTagException e = new JspTagException (errorMessage); 363 throw e; 364 } 365 366 CacheUtil cacheUtil = CacheUtil.getInstance(request); 367 368 String [] fieldIndices = null; 369 fieldIndices = cacheUtil.getFieldIndices(identifier, locale, definition, uri); 370 371 if ( fieldIndices == null ) { 372 if ( log.isDebugEnabled() ) { 374 log.debug("Trying to search in database"); 375 } 376 377 ContentFieldManager contentFieldManager = (ContentFieldManager) applicationCtx.getBean(Constants.CONTENT_FIELD_MANAGER_BEAN); 378 LayoutManager layoutManager = (LayoutManager) applicationCtx.getBean(Constants.LAYOUT_MANAGER_BEAN); 379 Collection allFields = null; 380 381 List layouts = new ArrayList (); 382 String tmpDefinition = definition; 383 do { 384 Layout layout = layoutManager.findLayoutByDefinition(tmpDefinition); 385 if ( layout != null ) { 386 layouts.add(layout); 387 } 388 tmpDefinition = ((HeritableComponentDefinition) TilesUtil.getDefinition(tmpDefinition, request, servletContext)).getExtends(); 389 } while ( tmpDefinition != null ); 390 391 Collections.reverse(layouts); 392 for ( int i = 0; i < layouts.size(); i++ ) { 393 Layout layout = (Layout) layouts.get(i); 394 List fields = contentFieldManager.findIndexedContentFieldsByLayoutDefinition(identifier, layout.getDefinition(), locale); 395 if ( log.isDebugEnabled() && fields != null ) { 396 log.debug("Using " + fields.size() + " fields from layout with definition: " + layout.getDefinition()); 397 } 398 allFields = WebappUtil.joinFields(allFields, fields); 399 } 400 401 List pageFields = contentFieldManager.findIndexedContentFieldsByPageUri(identifier, uri, locale); 402 if ( log.isDebugEnabled() && pageFields != null ) { 403 log.debug("Using " + pageFields.size() + " fields from page with uri: " + uri); 404 } 405 allFields = WebappUtil.joinFields(allFields, pageFields); 406 407 if ( allFields != null ) { 408 List sortedFields = new LinkedList (allFields); 409 Collections.sort(sortedFields, new WebappUtil.IndexedContentFieldComparator()); 410 411 if ( log.isDebugEnabled() ) { 412 log.debug("Found " + allFields.size() + " fields for definition name: " + definition + ", locale: " + locale + ", uri: " + uri + ", identifier: " + identifier); 413 } 414 415 List fieldIndicesList = new ArrayList (); 416 417 for ( int i = 0; i < sortedFields.size(); i++ ) { 418 ContentField contentField = (ContentField) sortedFields.get(i); 419 String indexValue = WebappUtil.getContentFieldIndex(contentField.getIdentifier()); 420 if ( indexValue != null ) { 421 fieldIndicesList.add(indexValue); 422 } 423 } 424 425 fieldIndices = (String []) fieldIndicesList.toArray(new String []{}); 426 427 cacheUtil.putFieldIndicesInCache(fieldIndices, identifier, locale, definition, uri); 428 } 429 } 430 431 if ( fieldIndices != null ) { 432 fieldIndicesIterator = Arrays.asList(fieldIndices).iterator(); 433 } 434 435 if ( fieldIndicesIterator != null ) { 436 while ( fieldIndicesIterator.hasNext() ) { 437 String indexValue = (String ) fieldIndicesIterator.next(); 438 pageContext.setAttribute(index, indexValue); 439 if ( log.isDebugEnabled() ) { 440 log.debug("Putting as attribute with key=" + index + " and value=" + indexValue); 441 } 442 JspFragment body = getJspBody(); 443 if ( body != null ) { 444 body.invoke(null); 445 } 446 } 447 } else { 448 if ( ignore.booleanValue() ) { 449 JspFragment body = getJspBody(); 450 if ( body != null ) { 451 body.invoke(null); 452 } 453 } 454 } 455 456 } catch ( Exception ex ) { 457 if ( ex instanceof IOException ) { 458 throw (IOException ) ex; 459 } 460 JspTagException e = new JspTagException (ex); 461 throw e; 462 } 463 464 if ( index != null ) { 465 pageContext.removeAttribute(index); 466 } 467 468 } 469 470 } 471 | Popular Tags |