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.dam.app.setup.migration;
35
36 import info.magnolia.dam.api.ItemKey;
37 import info.magnolia.dam.jcr.DamConstants;
38 import info.magnolia.jcr.util.NodeTypes;
39 import info.magnolia.jcr.util.NodeUtil;
40 import info.magnolia.jcr.util.NodeVisitor;
41
42 import java.util.ArrayList;
43 import java.util.Arrays;
44 import java.util.List;
45
46 import javax.jcr.ItemNotFoundException;
47 import javax.jcr.Node;
48 import javax.jcr.Property;
49 import javax.jcr.PropertyIterator;
50 import javax.jcr.RepositoryException;
51
52 import org.apache.commons.lang.StringUtils;
53 import org.apache.jackrabbit.core.id.NodeId;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87 public class CleanContentForDamMigrationTask extends AbstractCleanContentForDamMigrationTask {
88
89 private static final Logger log = LoggerFactory.getLogger(CleanContentForDamMigrationTask.class);
90
91
92
93
94 public CleanContentForDamMigrationTask(String taskName, String taskDescription, String contentRepository, List<String> contentPathsList) {
95 super(taskName, taskDescription, contentRepository, contentPathsList);
96 }
97
98
99
100
101 @Override
102 protected List<NodeVisitor> addCustomVisitors() {
103 List<NodeVisitor> visitorList = new ArrayList<NodeVisitor>();
104 visitorList.add(createCleanUpNodeVisitor());
105 visitorList.add(createHandleDamLinkNodeVisitor());
106 return visitorList;
107 }
108
109
110
111
112
113
114
115 protected String[] createCleanUpVisitorPrefixTable() {
116 return new String[] { "image", "video", "flash", "logoImg", "printLogoImg", "teaserImg", "image_*", "video_*", "flash_*", "logoImg_*", "printLogoImg_*", "teaserImg_*" };
117 }
118
119
120
121
122
123
124
125
126
127 private NodeVisitor createCleanUpNodeVisitor() {
128 NodeVisitor cleanUpVisitor = new NodeVisitor() {
129 private String[] prefix = createCleanUpVisitorPrefixTable();
130
131 @Override
132 public void visit(Node node) throws RepositoryException {
133 if (NodeUtil.isNodeType(node, NodeTypes.Page.NAME) || NodeUtil.isNodeType(node, NodeTypes.Component.NAME)) {
134 PropertyIterator propertyIterator = node.getProperties(prefix);
135 while (propertyIterator.hasNext()) {
136 Property property = propertyIterator.nextProperty();
137 if (!StringUtils.contains(property.getName(), "_") || DamMigrationUtil.isPropertyNameI18nBased(property.getName())) {
138
139 cleanUpVisitorTakeAction(node, property);
140 }
141 }
142 }
143 }
144 };
145 return cleanUpVisitor;
146 }
147
148
149
150
151
152
153 private void cleanUpVisitorTakeAction(Node node, Property property) throws RepositoryException {
154
155 try {
156
157 NodeId.valueOf(ItemKey.from(property.getString()).getAssetId());
158 if (!damNodeExist(ItemKey.from(property.getString()).getAssetId())) {
159 log.warn("The property {} located under {} with value {} seems not to be related to an Identifier. This property will be removed.'",
160 Arrays.asList(property.getName(), property.getPath(), property.getString()));
161 property.remove();
162 }
163 } catch (IllegalArgumentException ie) {
164 log.info("The property {} located under {} with value {} seems not to be related to an Identifier. This property will be keeped.'",
165 Arrays.asList(property.getName(), property.getPath(), property.getString()));
166 }
167 }
168
169
170
171
172
173
174
175
176 private NodeVisitor createHandleDamLinkNodeVisitor() {
177 NodeVisitor cleanUpVisitor = new NodeVisitor() {
178 private String[] prefix = new String[] { "link", "link_*" };
179
180 @Override
181 public void visit(Node node) throws RepositoryException {
182 if (NodeUtil.isNodeType(node, NodeTypes.Page.NAME) || NodeUtil.isNodeType(node, NodeTypes.Component.NAME)) {
183 PropertyIterator propertyIterator = node.getProperties(prefix);
184 while (propertyIterator.hasNext()) {
185 Property property = propertyIterator.nextProperty();
186 if (!StringUtils.contains(property.getName(), "_") || DamMigrationUtil.isPropertyNameI18nBased(property.getName())) {
187
188 handleDamLinkVisitorTakeAction(node, property);
189 }
190 }
191 }
192 }
193 };
194 return cleanUpVisitor;
195 }
196
197
198
199
200
201
202 private void handleDamLinkVisitorTakeAction(Node node, Property property) throws RepositoryException {
203
204 try {
205 String damIdentifier = property.getString();
206
207
208 NodeId.valueOf(damIdentifier);
209 if (damNodeExist(damIdentifier)) {
210 log.info("The property {} located under {} with value {} is related to an DAM Identifier. This property will be Changed to a Composite ID.'",
211 Arrays.asList(property.getName(), property.getPath(), damIdentifier));
212 property.setValue(new ItemKey(DamConstants.DEFAULT_JCR_PROVIDER_ID, damIdentifier).asString());
213 }
214 } catch (IllegalArgumentException ie) {
215 log.debug("The property {} located under {} with value {} seems not to be related to an Identifier. This property will be keeped.'",
216 Arrays.asList(property.getName(), property.getPath(), property.getString()));
217 }
218 }
219
220
221
222
223
224 private boolean damNodeExist(String identifier) {
225 try {
226 damSession.getNodeByIdentifier(identifier);
227 return true;
228 } catch (ItemNotFoundException infe) {
229 log.debug("Following identifier not found in the DAM workspace " + identifier);
230 return false;
231 } catch (RepositoryException re) {
232 log.warn("DamSession.getNodeByIdentifier({}) generated a RepositoryException.", identifier, re);
233 return false;
234 }
235 }
236 }