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.setup;
35
36 import java.util.Collection;
37 import java.util.Iterator;
38
39 import info.magnolia.cms.core.Content;
40 import info.magnolia.cms.core.HierarchyManager;
41 import info.magnolia.cms.core.ItemType;
42 import info.magnolia.cms.core.Content.ContentFilter;
43 import info.magnolia.cms.util.ContentUtil;
44 import info.magnolia.module.InstallContext;
45 import info.magnolia.module.delta.AbstractRepositoryTask;
46 import info.magnolia.module.delta.TaskExecutionException;
47
48 import javax.jcr.RepositoryException;
49
50 import org.apache.commons.lang.StringUtils;
51
52
53
54
55
56
57 public class ChangeNodeTypesInUserWorkspace extends AbstractRepositoryTask {
58
59
60
61
62 private static ContentFilter NT_FOLDER_FILTER = new ContentFilter() {
63 public boolean accept(Content content) {
64 return content.isNodeType("nt:folder");
65 }
66 };
67
68 public ChangeNodeTypesInUserWorkspace() {
69 super("Nodetypes change", "Changes node type of folders in users workspace.");
70 }
71
72 protected void doExecute(InstallContext ctx) throws RepositoryException, TaskExecutionException {
73 final HierarchyManager hm = ctx.getHierarchyManager("users");
74 try {
75 ContentUtil.visit(hm.getRoot(), new TypeChanger(), NT_FOLDER_FILTER);
76 } catch (Exception e) {
77 throw new TaskExecutionException("Cant' update folder node types in users workspace: " + e.getMessage(), e);
78 }
79 }
80
81 private final static class TypeChanger implements ContentUtil.PostVisitor {
82
83 public void postVisit(Content node) throws Exception {
84 Content parent = null;
85 if (node.getLevel() == 0) {
86
87 return;
88 }
89 parent = node.getParent();
90 String name = node.getName();
91 Content tmp = parent.createContent("tmp---tmp---" + name, ItemType.FOLDER);
92 Collection children = node.getChildren(ContentUtil.EXCLUDE_META_DATA_CONTENT_FILTER);
93 for (Iterator iterator = children.iterator(); iterator.hasNext();) {
94 Content object = (Content) iterator.next();
95
96 ContentUtil.moveInSession(object, tmp.getHandle() + "/" + object.getName());
97 }
98 node.delete();
99
100 ContentUtil.moveInSession(tmp, StringUtils.removeEnd(parent.getHandle(), "/") + "/" + name);
101 }
102
103 public void visit(Content node) throws Exception {
104
105 }
106 }
107 }