1 package org.jahia.taglibs.container; 2 3 import java.io.IOException ; 4 5 import javax.servlet.ServletRequest ; 6 import javax.servlet.jsp.JspException ; 7 import javax.servlet.jsp.tagext.BodyTagSupport ; 8 9 import org.jahia.data.JahiaData; 10 import org.jahia.data.beans.ContainerListBean; 11 import org.jahia.data.containers.JahiaContainer; 12 import org.jahia.data.containers.JahiaContainerList; 13 import org.jahia.exceptions.JahiaException; 14 import org.jahia.taglibs.util.Utils; 15 import org.jahia.data.beans.ContainerBean; 16 17 26 public class ContainerListTag extends BodyTagSupport { 27 28 private static org.apache.log4j.Logger logger = 29 org.apache.log4j.Logger.getLogger(ContainerListTag.class); 30 31 private String listName = ""; 32 private JahiaContainerList containerList = null; 33 34 private int intWindowSize = -1; private int intWindowOffset = -1; 38 39 private String windowSize; 40 private String windowOffset; 41 42 private JahiaData jData = null; 43 44 private boolean hasParent = false; 45 private String parentListName = ""; 46 private String parentContainerName = null; 47 48 public void setName (String name) { 49 this.listName = name; 50 } 51 52 public String getName () { 53 return this.listName; 54 } 55 56 public int getSize () { 57 if (this.getContainerList() != null) { 58 return this.getContainerList().size(); 59 } 60 return 0; 61 } 62 63 public void setWindowSize (String windowSize) { 64 try { 65 this.windowSize = windowSize; 66 this.intWindowSize = Integer.parseInt(windowSize); 67 } catch (NumberFormatException nfe) { 68 this.windowSize = ""; 69 this.intWindowSize = -1; 70 } 71 } 72 73 public String getWindowSize () { 74 return this.windowSize; 75 } 76 77 public void setWindowOffset (String windowOffset) { 78 try { 79 this.windowOffset = windowOffset; 80 this.intWindowOffset = Integer.parseInt(windowOffset); 81 } catch (NumberFormatException nfe) { 82 this.intWindowOffset = -1; 83 } 84 } 85 86 public String getWindowOffset () { 87 return this.windowOffset; 88 } 89 90 public JahiaContainerList getContainerList () { 91 return this.containerList; 92 } 93 94 public ContainerListBean getContainerListBean() { 95 if (this.containerList == null) { 96 return null; 97 } 98 return new ContainerListBean(this.containerList, jData.params()); 99 } 100 101 public String getParentContainerName() { 102 return parentContainerName; 103 } 104 105 public void setParentContainerName(String parentContainerName) { 106 this.parentContainerName = parentContainerName; 107 } 108 109 public int doStartTag () { 110 111 ServletRequest request = pageContext.getRequest(); 112 jData = (JahiaData) request.getAttribute("org.jahia.data.JahiaData"); 113 114 JahiaContainer parentContainer = null; 115 if (parentContainerName != null) { 116 ContainerBean parentContainerBean = (ContainerBean) pageContext.findAttribute(parentContainerName); 119 if (parentContainerBean != null) { 120 parentContainer = parentContainerBean.getJahiaContainer(); 121 try { 122 this.parentListName = parentContainer.getDefinition(). 123 getName(); 124 retrieveContainerList(parentContainer); 125 } catch (JahiaException je) { 126 logger.error("Error while retrieving container list for parent container " + parentContainer.getID(), je); 127 } 128 } 129 } 130 131 if (this.containerList == null) { 132 ContainerListTag parentContainerListTag = (ContainerListTag) 136 findAncestorWithClass(this, 137 ContainerListTag.class); 138 if (parentContainerListTag != null) { 139 this.parentListName = parentContainerListTag.getName(); 140 hasParent = true; 143 144 ContainerTag parentContainerTag = (ContainerTag) 146 findAncestorWithClass(this, 147 ContainerTag.class); 148 try { 149 if (parentContainerTag != null) { 151 parentContainer = parentContainerTag. 152 getContainer(); 153 if (parentContainer != null) { 154 retrieveContainerList(parentContainer); 155 } else { 156 logger.error("Parent container not found..."); 157 } 158 } else { 159 logger.error("Parent container tag not found..."); 160 } 161 } catch (JahiaException je) { 162 logger.error("Error:", je); 163 } 164 } else { 165 try { 168 this.containerList = getContainerList(jData, getName()); 169 } catch (JahiaException je) { 170 logger.error("Error:", je); 171 } 172 } 173 } 174 if (getId() != null) { 175 if (getContainerListBean() != null) { 176 pageContext.setAttribute(getId(), getContainerListBean()); 177 } else { 178 pageContext.removeAttribute(getId()); 179 } 180 } 181 182 return EVAL_BODY_BUFFERED; 183 } 184 185 private void retrieveContainerList (JahiaContainer parentContainer) 186 throws JahiaException { 187 this.containerList = parentContainer. 188 getContainerList(this. 189 listName); 190 if (this.containerList == null) { 191 194 this.listName = Utils.buildUniqueName( 195 parentListName, this.listName); 196 logger.warn( 197 "Using legacy method to find sub-container list " + 198 this.listName); 199 this.containerList = parentContainer. 200 getContainerList(this. 201 listName); 202 } 203 } 204 205 public int doAfterBody () { 207 208 try { 209 bodyContent.writeOut(bodyContent.getEnclosingWriter()); 210 } catch (IOException ioe) { 211 logger.error("Error:", ioe); 212 } 213 return SKIP_BODY; 214 } 215 216 protected JahiaContainerList getContainerList (JahiaData jData, 218 String listName) 219 throws JahiaException { 220 221 return jData.containers().getContainerList(listName); 222 } 223 224 public int doEndTag () 225 throws JspException { 226 listName = ""; 229 containerList = null; 230 231 intWindowSize = -1; intWindowOffset = -1; 233 234 windowSize = null; 235 windowOffset = null; 236 237 jData = null; 238 239 hasParent = false; 240 parentListName = ""; 241 parentContainerName = null; 242 return EVAL_PAGE; 243 } 244 245 } 246 | Popular Tags |