1 13 package info.magnolia.cms.taglibs; 14 15 import info.magnolia.cms.core.Content; 16 import info.magnolia.cms.core.NodeData; 17 import info.magnolia.cms.util.Resource; 18 19 import javax.jcr.RepositoryException; 20 import javax.servlet.http.HttpServletRequest ; 21 import javax.servlet.jsp.JspException ; 22 import javax.servlet.jsp.jstl.core.ConditionalTagSupport; 23 24 import org.apache.commons.lang.StringUtils; 25 import org.slf4j.Logger; 26 import org.slf4j.LoggerFactory; 27 28 29 34 public class IfEmpty extends ConditionalTagSupport { 35 36 39 private static final long serialVersionUID = 222L; 40 41 44 private static Logger log = LoggerFactory.getLogger(IfEmpty.class); 45 46 private String nodeDataName = StringUtils.EMPTY; 47 48 private String contentNodeName = StringUtils.EMPTY; 49 50 private String contentNodeCollectionName = StringUtils.EMPTY; 51 52 private transient Content contentNodeCollection; 53 54 private transient Content contentNode; 55 56 private transient NodeData nodeData; 57 58 private boolean actpage; 59 60 63 public void setAtomName(String name) { 64 this.setNodeDataName(name); 65 } 66 67 70 public void setNodeDataName(String name) { 71 this.nodeDataName = name; 72 } 73 74 77 public void setContainerName(String name) { 78 this.setContentNodeName(name); 79 } 80 81 84 public void setContentNodeName(String contentNodeName) { 85 this.contentNodeName = contentNodeName; 86 } 87 88 92 public void setContainerListName(String name) { 93 this.setContentNodeCollectionName(name); 94 } 95 96 99 public void setContentNodeCollectionName(String name) { 100 this.contentNodeCollectionName = name; 101 } 102 103 107 public void setActpage(boolean set) { 108 this.actpage = set; 109 } 110 111 114 protected boolean condition() { 115 HttpServletRequest req = (HttpServletRequest ) pageContext.getRequest(); 116 if (StringUtils.isNotEmpty(this.contentNodeCollectionName)) { 118 try { 119 this.contentNodeCollection = Resource.getCurrentActivePage(req).getContent( 120 this.contentNodeCollectionName); 121 } 122 catch (RepositoryException e) { 123 if (log.isDebugEnabled()) { 124 log.debug("Exception caught: " + e.getMessage(), e); } 126 } 127 if (this.contentNodeCollection == null) { 128 return true; 129 } 130 if (!this.contentNodeCollection.hasChildren()) { 131 return true; 132 } 133 return false; 134 } 135 if (StringUtils.isNotEmpty(this.contentNodeName) && StringUtils.isEmpty(this.nodeDataName)) { 137 try { 138 this.contentNode = Resource.getCurrentActivePage(req).getContent(this.contentNodeName); 139 } 140 catch (RepositoryException re) { 141 log.error(re.getMessage()); 142 } 143 if (this.contentNode == null) { 144 return true; 146 } 147 } 148 else if (StringUtils.isNotEmpty(this.contentNodeName) && StringUtils.isNotEmpty(this.nodeDataName)) { 151 try { 152 this.contentNode = Resource.getCurrentActivePage(req).getContent(this.contentNodeName); 153 } 154 catch (RepositoryException re) { 155 if (log.isDebugEnabled()) { 156 log.debug("Repository exception while reading " + this.contentNodeName + ": " + re.getMessage()); } 158 } 159 if (this.contentNode == null) { 160 return true; 161 } 162 if (this.contentNode != null) { 163 164 this.nodeData = this.contentNode.getNodeData(this.nodeDataName); 165 166 if ((this.nodeData == null) 167 || !this.nodeData.isExist() 168 || StringUtils.isEmpty(this.nodeData.getString())) { 169 return true; 170 } 171 } 172 } 173 else if (StringUtils.isEmpty(this.contentNodeName) && StringUtils.isNotEmpty(this.nodeDataName)) { 176 if (this.actpage) { 177 this.contentNode = Resource.getCurrentActivePage((HttpServletRequest ) pageContext.getRequest()); 178 } 179 else { 180 this.contentNode = Resource.getLocalContentNode((HttpServletRequest ) pageContext.getRequest()); 181 if (this.contentNode == null) { 182 this.contentNode = Resource.getGlobalContentNode((HttpServletRequest ) pageContext.getRequest()); 183 } 184 } 185 if (this.contentNode == null) { 186 return true; 187 } 188 if (this.contentNode != null) { 189 190 this.nodeData = this.contentNode.getNodeData(this.nodeDataName); 191 192 if ((this.nodeData == null) 193 || !this.nodeData.isExist() 194 || StringUtils.isEmpty(this.nodeData.getString())) { 195 return true; 196 } 197 } 198 } 199 else { 201 this.contentNode = Resource.getLocalContentNode((HttpServletRequest ) pageContext.getRequest()); 202 if (this.contentNode == null) { 203 this.contentNode = Resource.getGlobalContentNode((HttpServletRequest ) pageContext.getRequest()); 204 } 205 if (this.contentNode == null) { 206 return true; 207 } 208 } 209 return false; 210 } 211 212 215 public int doEndTag() throws JspException { 216 this.contentNodeCollection = null; 217 this.contentNode = null; 218 this.nodeData = null; 219 return super.doEndTag(); 220 } 221 222 225 public void release() { 226 this.nodeDataName = StringUtils.EMPTY; 227 this.contentNodeName = StringUtils.EMPTY; 228 this.contentNodeCollectionName = StringUtils.EMPTY; 229 this.actpage = false; 230 } 231 232 } 233 | Popular Tags |