1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.cms.util;
35
36 import info.magnolia.cms.core.Content;
37 import info.magnolia.cms.core.HierarchyManager;
38 import info.magnolia.cms.core.NodeData;
39 import info.magnolia.context.MgnlContext;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 import javax.jcr.RepositoryException;
44 import java.io.Serializable;
45
46
47
48
49
50 public class LazyContentWrapper extends ContentWrapper implements Serializable {
51 private static final Logger log = LoggerFactory.getLogger(LazyContentWrapper.class);
52
53 private String repository;
54
55 private String uuid;
56
57 private transient Content node;
58
59 public LazyContentWrapper(String repository, String uuid) {
60 this.setRepository(repository);
61 this.setUuid(uuid);
62 }
63
64 public LazyContentWrapper(Content node) {
65 try {
66 this.setRepository(node.getWorkspace().getName());
67 } catch (RepositoryException e) {
68 log.error("can't read repository name from wrapping node", e);
69 }
70 this.setUuid(node.getUUID());
71 this.node = node;
72 }
73
74 @Override
75 public synchronized Content getWrappedContent() {
76 try {
77 if (node == null || !node.getJCRNode().getSession().isLive()) {
78 node = getHierarchyManager().getContentByUUID(getUuid());
79 }
80 } catch (RepositoryException e) {
81 log.error("can't reinitialize node {}", getUuid(), e);
82 }
83 return node;
84 }
85
86 @Override
87 public HierarchyManager getHierarchyManager() {
88 return MgnlContext.getSystemContext().getHierarchyManager(getRepository());
89 }
90
91 @Override
92 public NodeData wrap(NodeData nodeData) {
93 return new LazyNodeDataWrapper(nodeData);
94 }
95
96 protected void setUuid(String uuid) {
97 this.uuid = uuid;
98 }
99
100 protected String getUuid() {
101 return uuid;
102 }
103
104 protected void setRepository(String repository) {
105 this.repository = repository;
106 }
107
108 protected String getRepository() {
109 return repository;
110 }
111
112 }