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