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.core;
35
36 import info.magnolia.cms.security.AccessDeniedException;
37 import info.magnolia.cms.util.NodeDataUtil;
38 import info.magnolia.context.MgnlContext;
39 import org.apache.commons.lang.StringUtils;
40
41 import javax.jcr.ItemNotFoundException;
42 import javax.jcr.PathNotFoundException;
43 import javax.jcr.PropertyType;
44 import javax.jcr.RepositoryException;
45 import javax.jcr.ValueFormatException;
46
47 import java.util.Calendar;
48 import java.util.Collection;
49
50
51
52
53
54
55
56 public abstract class AbstractNodeData implements NodeData{
57 private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(AbstractNodeData.class);
58
59 protected String name;
60 protected Content parent;
61 private int multiValue = MULTIVALUE_UNDEFINED;
62
63 protected AbstractNodeData(Content parent, String name) {
64 this.parent = parent;
65 this.name = name;
66 }
67
68 public HierarchyManager getHierarchyManager() {
69 return parent.getHierarchyManager();
70 }
71
72 public String getName() {
73 return this.name;
74 }
75
76 public String getHandle() {
77 return Path.getAbsolutePath(parent.getHandle(), name);
78 }
79
80 public boolean isGranted(long permissions) {
81 return getHierarchyManager().getAccessManager().isGranted(getHandle(), permissions);
82 }
83
84 public String getString(String lineBreak) {
85 return getString().replaceAll("\n", lineBreak);
86 }
87
88 public Content getParent() throws AccessDeniedException, ItemNotFoundException, javax.jcr.AccessDeniedException, RepositoryException {
89 return this.parent;
90 }
91
92 public void setParent(Content parent) {
93 this.parent = parent;
94 }
95
96 public Content getReferencedContent(String repositoryId) throws PathNotFoundException, RepositoryException {
97 if(this.getHierarchyManager().getName().equals(repositoryId)){
98 return getReferencedContent();
99 }
100 return getReferencedContent(MgnlContext.getHierarchyManager(repositoryId));
101 }
102
103 public Content getReferencedContent() throws PathNotFoundException, RepositoryException {
104 return getReferencedContent(this.getHierarchyManager());
105 }
106
107 protected Content getReferencedContent(HierarchyManager hm) throws PathNotFoundException, RepositoryException {
108 if(!isExist()){
109 return null;
110 }
111
112 Content node = getParent();
113 Content refNode = null;
114
115 int type = getType();
116
117 if (type == PropertyType.REFERENCE) {
118 refNode = getContentFromJCRReference();
119 } else if (type != PropertyType.PATH && type != PropertyType.STRING) {
120 throw new ItemNotFoundException("can't find referenced node for value " + PropertyType.nameFromValue(type) + "[" + getString() + "]. This type is not supported.");
121 }
122
123 final String pathOrUUID = this.getString();
124 if (StringUtils.isNotBlank(pathOrUUID)) {
125
126 if (!StringUtils.contains(pathOrUUID, "/")) {
127 try {
128 refNode = hm.getContentByUUID(pathOrUUID);
129 } catch (ItemNotFoundException e) {
130
131 }
132 }
133
134 if (refNode == null) {
135
136 if (!pathOrUUID.startsWith("/") && node.hasContent(pathOrUUID)) {
137 refNode = node.getContent(pathOrUUID);
138 } else if (pathOrUUID.startsWith("/") && hm.isExist(pathOrUUID)){
139 refNode = hm.getContent(pathOrUUID);
140 }
141 }
142 }
143
144 if(refNode==null){
145 throw new ItemNotFoundException("can't find referenced node for value [" + getString() + "]");
146 }
147
148 return refNode;
149 }
150
151
152
153
154 protected abstract Content getContentFromJCRReference() throws RepositoryException;
155
156 public int isMultiValue() {
157 if(multiValue == MULTIVALUE_UNDEFINED) {
158 try {
159 if (isExist()) {
160 getJCRProperty().getValue();
161 multiValue = MULTIVALUE_FALSE;
162 }
163
164 } catch (ValueFormatException e) {
165 multiValue = MULTIVALUE_TRUE;
166
167 } catch (Exception e) {
168 log.debug(e.getMessage(), e);
169 }
170 }
171 return this.multiValue;
172 }
173
174 public String getAttribute(String name) {
175 throw new UnsupportedOperationException("Attributes are only supported for BINARY type");
176 }
177
178 public Collection<String> getAttributeNames() throws RepositoryException {
179 throw new UnsupportedOperationException("Attributes are only supported for BINARY type");
180 }
181
182 public void setAttribute(String name, String value) throws RepositoryException, AccessDeniedException, UnsupportedOperationException {
183 throw new UnsupportedOperationException("Attributes are only supported for BINARY type");
184 }
185
186 public void setAttribute(String name, Calendar value) throws RepositoryException, AccessDeniedException, UnsupportedOperationException {
187 throw new UnsupportedOperationException("Attributes are only supported for BINARY type");
188 }
189
190 public String toString() {
191 final StringBuilder buffer = new StringBuilder();
192 buffer.append(getHierarchyManager().getName()).append(":");
193 buffer.append(getHandle());
194 buffer.append("[");
195 buffer.append(NodeDataUtil.getTypeName(this));
196 buffer.append("]");
197
198 return buffer.toString();
199 }
200 }