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