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