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.i18n.I18nContentSupportFactory;
37 import info.magnolia.cms.security.AccessDeniedException;
38 import info.magnolia.cms.util.NodeDataUtil;
39 import info.magnolia.cms.util.NodeTypeFilter;
40 import info.magnolia.context.MgnlContext;
41 import info.magnolia.logging.AuditLoggingUtil;
42
43 import java.io.InputStream;
44 import java.util.ArrayList;
45 import java.util.Calendar;
46 import java.util.Collection;
47 import java.util.Comparator;
48
49 import javax.jcr.PathNotFoundException;
50 import javax.jcr.PropertyType;
51 import javax.jcr.RepositoryException;
52 import javax.jcr.Value;
53 import javax.jcr.Workspace;
54
55 import org.apache.commons.lang.StringUtils;
56
57
58
59
60
61
62
63
64
65
66 public abstract class AbstractContent extends ContentHandler implements Content {
67 private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(AbstractContent.class);
68
69 public Content createContent(String name) throws PathNotFoundException, RepositoryException, AccessDeniedException {
70 return createContent(name, ItemType.CONTENT);
71 }
72
73 public Content createContent(String name, ItemType contentType) throws PathNotFoundException, RepositoryException, AccessDeniedException {
74 return createContent(name, contentType.getSystemName());
75 }
76
77 public NodeData createNodeData(String name) throws PathNotFoundException, RepositoryException, AccessDeniedException {
78 return setNodeData(name, "");
79 }
80
81 public NodeData createNodeData(String name, Value value) throws PathNotFoundException, RepositoryException, AccessDeniedException {
82 return setNodeData(name, value);
83 }
84
85
86
87
88 @Deprecated
89 public NodeData createNodeData(String name, Value[] value) throws PathNotFoundException, RepositoryException, AccessDeniedException {
90 return setNodeData(name, value);
91 }
92
93
94
95
96 @Deprecated
97 public NodeData createNodeData(String name, int type) throws PathNotFoundException, RepositoryException, AccessDeniedException {
98
99 switch (type) {
100 case PropertyType.STRING:
101 return setNodeData(name, StringUtils.EMPTY);
102 case PropertyType.BOOLEAN:
103 return setNodeData(name, Boolean.FALSE);
104 case PropertyType.DATE:
105 return setNodeData(name, Calendar.getInstance());
106 case PropertyType.LONG:
107 return setNodeData(name, new Long(0));
108 case PropertyType.DOUBLE:
109 return setNodeData(name, new Double(0.0));
110 default:
111 return newNodeDataInstance(name, type, true);
112 }
113 }
114
115
116
117
118 @Deprecated
119 public NodeData createNodeData(String name, Object valueObj) throws RepositoryException {
120 return setNodeData(name, valueObj);
121 }
122
123
124
125
126
127 public NodeData getNodeData(String name) {
128 try {
129
130 return newNodeDataInstance(name, PropertyType.UNDEFINED, false);
131 }
132 catch(RepositoryException e){
133 throw new IllegalStateException("Can't instantiate node data " + name + " on node " + toString(), e);
134 }
135 }
136
137
138
139
140
141
142 abstract public NodeData newNodeDataInstance(String name, int type, boolean createIfNotExisting) throws AccessDeniedException, RepositoryException;
143
144
145
146
147 public boolean hasNodeData(String name) throws RepositoryException {
148 return getNodeData(name).isExist();
149 }
150
151 public NodeData setNodeData(String name, Value value) throws PathNotFoundException, RepositoryException, AccessDeniedException {
152 NodeData nodeData = newNodeDataInstance(name, value.getType(), true);
153 nodeData.setValue(value);
154 return nodeData;
155 }
156
157 public NodeData setNodeData(String name, Value[] value) throws PathNotFoundException, RepositoryException, AccessDeniedException {
158 if(value.length == 0){
159 throw new IllegalArgumentException("Value array can't be empty");
160 }
161 NodeData nodeData = newNodeDataInstance(name, value[0].getType(), true);
162 nodeData.setValue(value);
163 return nodeData;
164 }
165
166 public NodeData setNodeData(String name, boolean value) throws PathNotFoundException, RepositoryException, AccessDeniedException {
167 NodeData nodeData = newNodeDataInstance(name, PropertyType.BOOLEAN, true);
168 nodeData.setValue(value);
169 return nodeData;
170 }
171
172 public NodeData setNodeData(String name, long value) throws PathNotFoundException, RepositoryException, AccessDeniedException {
173 NodeData nodeData = newNodeDataInstance(name, PropertyType.LONG, true);
174 nodeData.setValue(value);
175 return nodeData;
176 }
177
178 public NodeData setNodeData(String name, double value) throws PathNotFoundException, RepositoryException, AccessDeniedException {
179 NodeData nodeData = newNodeDataInstance(name, PropertyType.DOUBLE, true);
180 nodeData.setValue(value);
181 return nodeData;
182 }
183
184 public NodeData setNodeData(String name, String value) throws PathNotFoundException, RepositoryException, AccessDeniedException {
185 NodeData nodeData = newNodeDataInstance(name, PropertyType.STRING, true);
186 nodeData.setValue(value);
187 return nodeData;
188 }
189
190 public NodeData setNodeData(String name, InputStream value) throws PathNotFoundException, RepositoryException, AccessDeniedException {
191 NodeData nodeData = newNodeDataInstance(name, PropertyType.BINARY, true);
192 nodeData.setValue(value);
193 return nodeData;
194 }
195
196 public NodeData setNodeData(String name, Calendar value) throws PathNotFoundException, RepositoryException, AccessDeniedException {
197 NodeData nodeData = newNodeDataInstance(name, PropertyType.DATE, true);
198 nodeData.setValue(value);
199 return nodeData;
200 }
201
202 public NodeData setNodeData(String name, Content value) throws PathNotFoundException, RepositoryException, AccessDeniedException {
203 NodeData nodeData = newNodeDataInstance(name, PropertyType.STRING, true);
204 nodeData.setValue(value.getUUID());
205 return nodeData;
206 }
207
208
209
210
211 public NodeData setNodeData(String name, Object value) throws PathNotFoundException, RepositoryException, AccessDeniedException {
212 NodeData nodeData = newNodeDataInstance(name, NodeDataUtil.getJCRPropertyType(value), true);
213 NodeDataUtil.setValue(nodeData, value);
214 return nodeData;
215 }
216
217 public void deleteNodeData(String name) throws PathNotFoundException, RepositoryException {
218 getNodeData(name).delete();
219 }
220
221
222
223
224
225 public Collection<Content> getChildren() {
226 String type = null;
227
228 try {
229 type = this.getNodeTypeName();
230 }
231 catch (RepositoryException re) {
232 throw new RuntimeException("Can't read type of node [" + toString() + "]", re);
233
234 }
235
236 if ("rep:root".equalsIgnoreCase(type)) {
237 type = ItemType.CONTENT.getSystemName();
238 }
239
240 return this.getChildren(type);
241 }
242
243
244
245
246
247 public Collection<Content> getChildren(ContentFilter filter) {
248 return getChildren(filter, null);
249 }
250
251
252
253
254
255 public Collection<Content> getChildren(ItemType itemType) {
256 return getChildren(new NodeTypeFilter(itemType), null);
257 }
258
259
260
261
262
263 public Collection<Content> getChildren(String contentType) {
264 return getChildren(new NodeTypeFilter(contentType), null);
265 }
266
267
268
269
270
271 public Collection<Content> getChildren(final String contentType, final String namePattern) {
272 return getChildren(new NodeTypeFilter(contentType), namePattern, null);
273 }
274
275
276
277
278
279 public Collection<Content> getChildren(ContentFilter filter, Comparator<Content> orderCriteria) {
280 return getChildren(filter, null, orderCriteria);
281 }
282
283
284
285
286 abstract public Collection<Content> getChildren(ContentFilter filter, String namePattern, Comparator<Content> orderCriteria);
287
288
289
290
291 @Deprecated
292 public Content getChildByName(String namePattern) {
293 Collection<Content> children = getChildren("nt:base", namePattern);;
294 if (!children.isEmpty()) {
295 return children.iterator().next();
296 }
297 return null;
298 }
299
300 public Collection<NodeData> getNodeDataCollection() {
301 return getNodeDataCollection(null);
302 }
303
304 protected Collection<NodeData> getBinaryNodeDatas(String namePattern) throws RepositoryException {
305 Collection<NodeData> nodeDatas = new ArrayList<NodeData>();
306 Collection<Content> binaryNodes = getChildren(ItemType.NT_RESOURCE, namePattern);
307 for (Content binaryNode : binaryNodes) {
308 nodeDatas.add(newNodeDataInstance(binaryNode.getName(), PropertyType.BINARY, false));
309 }
310 return nodeDatas;
311 }
312
313
314 public boolean hasChildren() {
315 return (this.getChildren().size() > 0);
316 }
317
318 public boolean hasChildren(String contentType) {
319 return (this.getChildren(contentType).size() > 0);
320 }
321
322 public void delete(String path) throws RepositoryException {
323 if(isNodeData(path)){
324 deleteNodeData(path);
325 }
326 else{
327 getContent(path).delete();
328 }
329 }
330
331 public boolean isNodeData(String path) throws AccessDeniedException, RepositoryException {
332 return hasNodeData(path);
333 }
334
335 public String getTemplate() {
336 return this.getMetaData().getTemplate();
337 }
338
339 public String getTitle() {
340 return I18nContentSupportFactory.getI18nSupport().getNodeData(this, "title").getString();
341 }
342
343 public void updateMetaData() throws RepositoryException, AccessDeniedException {
344 MetaData md = this.getMetaData();
345 md.setModificationDate();
346 md.setAuthorId(MgnlContext.getUser().getName());
347 AuditLoggingUtil.log(AuditLoggingUtil.ACTION_MODIFY, getHierarchyManager().getName(), this.getItemType(), getHandle());
348 }
349
350 public boolean isGranted(long permissions) {
351 return getHierarchyManager().getAccessManager().isGranted(getHandle(), permissions);
352 }
353
354 public Workspace getWorkspace() throws RepositoryException {
355 return getHierarchyManager().getWorkspace();
356 }
357
358 @Override
359 public String toString() {
360 StringBuffer buffer = new StringBuffer();
361 buffer.append(getHierarchyManager() == null ? "null" : getHierarchyManager().getName());
362 buffer.append(":" + getHandle());
363 String type = "";
364 try {
365 type = getItemType().getSystemName();
366 }
367 catch (RepositoryException e) {
368
369 }
370 buffer.append("[");
371 buffer.append(type);
372 buffer.append("]");
373
374 return buffer.toString();
375 }
376
377
378 }