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.module.admininterface.setup.for4_3;
35
36 import info.magnolia.cms.core.Content;
37 import info.magnolia.cms.core.ItemType;
38 import info.magnolia.cms.util.ContentUtil;
39 import info.magnolia.cms.util.QueryUtil;
40 import info.magnolia.module.InstallContext;
41 import info.magnolia.module.delta.AbstractRepositoryTask;
42 import info.magnolia.module.delta.TaskExecutionException;
43 import info.magnolia.repository.RepositoryConstants;
44
45 import java.util.ArrayList;
46 import java.util.Collection;
47 import java.util.Iterator;
48 import java.util.List;
49
50 import javax.jcr.RepositoryException;
51 import javax.jcr.query.Query;
52
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
55
56
57
58
59
60
61
62
63
64
65 public class ReplaceWrongDialogNodeTypeTask extends AbstractRepositoryTask {
66
67 private final static Logger log = LoggerFactory.getLogger(ReplaceWrongDialogNodeTypeTask.class);
68
69 public ReplaceWrongDialogNodeTypeTask() {
70 super("Replace incorrect dialog node types", "Checks for each module in the config repository if dialogs are of the incorrect type mgnl:content and replaces them with the correct one mgnl:contentNode");
71 }
72
73 @Override
74 protected void doExecute(InstallContext installContext) throws RepositoryException, TaskExecutionException {
75 final Collection<Content> dialogs = QueryUtil.query(RepositoryConstants.CONFIG, "modules/*/dialogs", Query.XPATH);
76 List<Content> dialogNodes = new ArrayList<Content>();
77
78 for (Iterator<Content> iterator = dialogs.iterator(); iterator.hasNext();) {
79 collectDialogNodes(iterator.next(), dialogNodes);
80 }
81 log.debug("Found {} dialog(s)", dialogNodes.size());
82
83 for (Content srcNode : dialogNodes) {
84 final String handle = srcNode.getHandle();
85 try {
86 log.debug("Checking if {} needs to be replaced due to incorrect dialog type...", handle);
87 if(!srcNode.isNodeType(ItemType.CONTENTNODE.getSystemName())){
88 ContentUtil.changeNodeType(srcNode, ItemType.CONTENTNODE, false);
89 }
90 }
91 catch (RepositoryException e) {
92 installContext.error("Can't replace " + handle, e);
93 }
94 }
95 }
96
97
98 private void collectDialogNodes(Content current, List<Content> dialogNodes) throws RepositoryException {
99 if(isDialogNode(current)){
100 dialogNodes.add(current);
101 return;
102 }
103 for (Content child : ContentUtil.getAllChildren(current)) {
104 collectDialogNodes(child, dialogNodes);
105 }
106 }
107
108 private boolean isDialogNode(Content node) throws RepositoryException{
109 if(isDialogControlNode(node)){
110 return false;
111 }
112
113
114 if(ContentUtil.getAllChildren(node).isEmpty()){
115 return true;
116 }
117
118
119 if(!node.getNodeDataCollection().isEmpty()){
120 return true;
121 }
122
123
124 for (Content child : node.getChildren(ItemType.CONTENTNODE)) {
125 if (isDialogControlNode(child)) {
126 return true;
127 }
128 }
129 return false;
130 }
131
132 private boolean isDialogControlNode(Content node) throws RepositoryException{
133 return node.hasNodeData("controlType") || node.hasNodeData("reference");
134 }
135
136 }