1 13 package info.magnolia.cms.util; 14 15 import info.magnolia.cms.core.Content; 16 import info.magnolia.cms.core.HierarchyManager; 17 import info.magnolia.cms.core.ItemType; 18 import info.magnolia.cms.core.NodeData; 19 import info.magnolia.cms.core.Content.ContentFilter; 20 import info.magnolia.cms.security.AccessDeniedException; 21 import info.magnolia.context.MgnlContext; 22 23 import java.lang.reflect.InvocationTargetException ; 24 import java.util.ArrayList ; 25 import java.util.Collection ; 26 import java.util.HashMap ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 import java.util.Map ; 30 31 import javax.jcr.PathNotFoundException; 32 import javax.jcr.RepositoryException; 33 34 import org.apache.commons.beanutils.BeanUtils; 35 import org.apache.commons.lang.StringUtils; 36 import org.slf4j.Logger; 37 import org.slf4j.LoggerFactory; 38 39 43 public class ContentUtil { 44 45 private static Logger log = LoggerFactory.getLogger(ContentUtil.class); 46 47 50 public static ContentFilter ALL_NODES_CONTENT_FILTER = new ContentFilter() { 51 52 public boolean accept(Content content) { 53 return true; 54 } 55 }; 56 57 62 public interface Visitor { 63 void visit(Content node) throws Exception ; 64 } 65 66 69 public static ContentFilter EXCLUDE_META_DATA_CONTENT_FILTER = new ContentFilter() { 70 public boolean accept(Content content) { 71 return !content.getName().startsWith("jcr:") && !content.isNodeType(ItemType.NT_METADATA); 72 } 73 }; 74 75 81 public static Content getContent(String repository, String path) { 82 try { 83 return MgnlContext.getHierarchyManager(repository).getContent(path); 84 } 85 catch (RepositoryException e) { 86 return null; 87 } 88 } 89 90 96 public static Content getContent(Content node, String name) { 97 try { 98 return node.getContent(name); 99 } 100 catch (RepositoryException e) { 101 return null; 102 } 103 } 104 105 114 public static Content getOrCreateContent(Content node, String name, ItemType contentType) 115 throws AccessDeniedException, RepositoryException { 116 Content res = null; 117 try { 118 res = node.getContent(name); 119 } 120 catch (PathNotFoundException e) { 121 res = node.createContent(name, contentType); 122 } 123 return res; 124 } 125 126 133 public static Content getCaseInsensitive(Content node, String name) { 134 if (name == null || node == null) { 135 return null; 136 } 137 name = name.toLowerCase(); 138 for (Iterator iter = node.getChildren(ALL_NODES_CONTENT_FILTER).iterator(); iter.hasNext();) { 139 Content child = (Content) iter.next(); 140 if (child.getName().toLowerCase().equals(name)) { 141 return child; 142 } 143 } 144 return null; 145 } 146 147 150 public static List collectAllChildren(Content node) { 151 List nodes = new ArrayList (); 152 return collectAllChildren(nodes, node, new ItemType[]{ItemType.CONTENT, ItemType.CONTENTNODE}); 153 } 154 155 161 public static List collectAllChildren(Content node, ContentFilter filter) { 162 List nodes = new ArrayList (); 163 return collectAllChildren(nodes, node, filter); 164 } 165 166 173 private static List collectAllChildren(List nodes, Content node, ContentFilter filter) { 174 Collection children = node.getChildren(filter); 176 for (Iterator iter = children.iterator(); iter.hasNext();) { 177 Content child = (Content) iter.next(); 178 nodes.add(child); 179 } 180 181 Collection allChildren = node.getChildren(EXCLUDE_META_DATA_CONTENT_FILTER); 183 184 for (Iterator iter = allChildren.iterator(); iter.hasNext();) { 186 Content child = (Content) iter.next(); 187 collectAllChildren(nodes, child, filter); 188 } 189 190 return nodes; 191 } 192 193 199 public static List collectAllChildren(Content node, ItemType type) { 200 List nodes = new ArrayList (); 201 return collectAllChildren(nodes, node, new ItemType[]{type}); 202 } 203 204 209 public static Collection getAllChildren(Content node){ 210 return node.getChildren(EXCLUDE_META_DATA_CONTENT_FILTER); 211 } 212 213 219 public static List collectAllChildren(Content node, ItemType[] types) { 220 List nodes = new ArrayList (); 221 return collectAllChildren(nodes, node, types); 222 } 223 224 231 private static List collectAllChildren(List nodes, Content node, ItemType[] types) { 232 for (int i = 0; i < types.length; i++) { 233 ItemType type = types[i]; 234 235 Collection children = node.getChildren(type); 236 for (Iterator iter = children.iterator(); iter.hasNext();) { 237 Content child = (Content) iter.next(); 238 nodes.add(child); 239 collectAllChildren(nodes, child, types); 240 } 241 } 242 return nodes; 243 } 244 245 public static void orderNodes(Content node, String [] nodes) throws RepositoryException{ 246 for (int i = nodes.length - 1; i > 0; i--) { 247 node.orderBefore(nodes[i-1], nodes[i]); 248 } 249 node.save(); 250 } 251 252 public static void visit(Content node, Visitor visitor) throws Exception { 253 visit(node, visitor, EXCLUDE_META_DATA_CONTENT_FILTER); 254 } 255 256 public static void visit(Content node, Visitor visitor, ContentFilter filter) throws Exception { 257 visitor.visit(node); 258 for (Iterator iter = node.getChildren(filter).iterator(); iter.hasNext();) { 259 visit((Content) iter.next(), visitor); 260 } 261 } 262 263 public static Content createPath(HierarchyManager hm, String path) throws AccessDeniedException, 264 PathNotFoundException, RepositoryException { 265 return ContentUtil.createPath(hm, path, ItemType.CONTENT); 266 } 267 268 public static Content createPath(HierarchyManager hm, String path, ItemType type) throws AccessDeniedException, 269 PathNotFoundException, RepositoryException { 270 Content node = hm.getRoot(); 271 return createPath(node, path, type); 272 } 273 274 283 public static Content createPath(Content node, String path, ItemType type) throws RepositoryException, 284 PathNotFoundException, AccessDeniedException { 285 path = StringUtils.removeStart(path, "/"); 287 288 if (StringUtils.isEmpty(path)) { 289 return node; 290 } 291 292 String [] names = path.split("/"); 294 for (int i = 0; i < names.length; i++) { 295 String name = names[i]; 296 if (node.hasContent(name)) { 297 node = node.getContent(name); 298 } 299 else { 300 node = node.createContent(name, type); 301 } 302 } 303 return node; 304 } 305 306 311 public static Map toMap(Content node) { 312 Map map = new HashMap (); 313 for (Iterator iter = node.getNodeDataCollection().iterator(); iter.hasNext();) { 314 NodeData nd = (NodeData) iter.next(); 315 Object val = NodeDataUtil.getValue(nd); 316 if (val != null) { 317 map.put(nd.getName(), val); 318 } 319 } 320 return map; 321 } 322 323 329 public static Object setProperties(Object bean, Content node) { 330 try { 331 BeanUtils.populate(bean, toMap(node)); 332 } 333 catch (IllegalAccessException e) { 334 log.error("can't set properties", e); 335 } 336 catch (InvocationTargetException e) { 337 log.error("can't set properties", e); 338 } 339 return bean; 340 } 341 342 349 public static Object setProperties(Object bean, String repository, String path) { 350 Content node = getContent(repository, path); 351 if (node != null) { 352 setProperties(bean, node); 353 } 354 return bean; 355 } 356 357 public static void setNodeDatas(Content node, Object obj) throws RepositoryException { 358 try { 359 setNodeDatas(node, BeanUtils.describe(obj)); 360 } 361 catch (InvocationTargetException e) { 362 log.error("can't persist", e); 363 } 364 catch (NoSuchMethodException e) { 365 log.error("can't persist", e); 366 } 367 catch (IllegalAccessException e) { 368 log.error("can't persist", e); 369 } 370 } 371 372 public static void setNodeDatas(Content node, Map map) throws RepositoryException { 373 for (Iterator iter = map.keySet().iterator(); iter.hasNext();) { 374 String name = (String ) iter.next(); 375 NodeDataUtil.getOrCreate(node, name).setValue(map.get(name).toString()); 376 } 377 } 378 379 public static void setNodeDatas(Content node, Object bean, String [] excludes) throws RepositoryException { 380 try { 381 Map properties = BeanUtils.describe(bean); 382 for (int i = 0; i < excludes.length; i++) { 383 String exclude = excludes[i]; 384 properties.remove(exclude); 385 } 386 setNodeDatas(node, properties); 387 } 388 catch (InvocationTargetException e) { 389 log.error("can't persist", e); 390 } 391 catch (NoSuchMethodException e) { 392 log.error("can't persist", e); 393 } 394 catch (IllegalAccessException e) { 395 log.error("can't persist", e); 396 } 397 } 398 399 public static String uuid2path(String repository, String uuid){ 400 if(StringUtils.isNotEmpty(uuid)){ 401 HierarchyManager hm = MgnlContext.getHierarchyManager(repository); 402 try { 403 Content node = hm.getContentByUUID(uuid); 404 return node.getHandle(); 405 } 406 catch (Exception e) { 407 } 409 410 } 411 return uuid; 412 } 413 414 } | Popular Tags |