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.lang3.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 public abstract class AbstractNodeData implements NodeData{
53 private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(AbstractNodeData.class);
54
55 protected String name;
56 private final Content parent;
57 private int multiValue = MULTIVALUE_UNDEFINED;
58
59 protected AbstractNodeData(Content parent, String name) {
60 this.parent = parent;
61 this.name = name;
62 }
63
64 @Override
65 public HierarchyManager getHierarchyManager() {
66 return parent.getHierarchyManager();
67 }
68
69 @Override
70 public String getName() {
71 return this.name;
72 }
73
74 @Override
75 public String getHandle() {
76 return Path.getAbsolutePath(parent.getHandle(), name);
77 }
78
79 @Override
80 public boolean isGranted(long permissions) {
81 return getHierarchyManager().isGranted(getHandle(), permissions);
82 }
83
84 @Override
85 public String getString(String lineBreak) {
86 return getString().replaceAll("\n", lineBreak);
87 }
88
89 @Override
90 public Content getParent() {
91 return this.parent;
92 }
93
94 @Override
95 public Content getReferencedContent(String repositoryId) throws PathNotFoundException, RepositoryException {
96 if(getParent().getWorkspace().getName().equals(repositoryId)){
97 return getReferencedContent();
98 }
99 return getReferencedContent(MgnlContext.getHierarchyManager(repositoryId));
100 }
101
102 @Override
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 @Override
157 public int isMultiValue() {
158 if(multiValue == MULTIVALUE_UNDEFINED) {
159 try {
160 if (isExist()) {
161 getJCRProperty().getValue();
162 multiValue = MULTIVALUE_FALSE;
163 }
164
165 } catch (ValueFormatException e) {
166 multiValue = MULTIVALUE_TRUE;
167
168 } catch (Exception e) {
169 log.debug(e.getMessage(), e);
170 }
171 }
172 return this.multiValue;
173 }
174
175 @Override
176 public String getAttribute(String name) {
177 throw new UnsupportedOperationException("Attributes are only supported for BINARY type");
178 }
179
180 @Override
181 public Collection<String> getAttributeNames() throws RepositoryException {
182 throw new UnsupportedOperationException("Attributes are only supported for BINARY type");
183 }
184
185 @Override
186 public void setAttribute(String name, String value) throws RepositoryException, AccessDeniedException, UnsupportedOperationException {
187 throw new UnsupportedOperationException("Attributes are only supported for BINARY type");
188 }
189
190 @Override
191 public void setAttribute(String name, Calendar value) throws RepositoryException, AccessDeniedException, UnsupportedOperationException {
192 throw new UnsupportedOperationException("Attributes are only supported for BINARY type");
193 }
194
195 @Override
196 public String toString() {
197 String workspaceName = "";
198 try {
199 workspaceName = getParent().getWorkspace().getName();
200 } catch (Exception e) {
201
202 }
203
204 final StringBuilder builder = new StringBuilder();
205 builder.append(workspaceName).append(":");
206 builder.append(getHandle());
207 builder.append("[");
208 builder.append(NodeDataUtil.getTypeName(this));
209 builder.append("]");
210
211 return builder.toString();
212 }
213 }