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.wrapper.StringPropertyValueFilteringNodeWrapper;
39 import info.magnolia.module.InstallContext;
40 import info.magnolia.module.delta.TaskExecutionException;
41
42 import java.util.List;
43
44 import javax.jcr.ItemNotFoundException;
45 import javax.jcr.Node;
46 import javax.jcr.NodeIterator;
47 import javax.jcr.Property;
48 import javax.jcr.PropertyIterator;
49 import javax.jcr.RepositoryException;
50 import javax.jcr.Session;
51 import javax.jcr.query.QueryResult;
52
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
55
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
88
89
90
91 public class ChangeWebsiteDmsReferenceToDamMigrationTask extends AbstractPropertyValueSearchDamMigrationTask {
92
93 private static final Logger log = LoggerFactory.getLogger(ChangeWebsiteDmsReferenceToDamMigrationTask.class);
94
95 private List<String> contentPathsList;
96 private String contentRepository;
97 private Session contentSession;
98 private Session damSession;
99 private String propertySuffix = "DmsUUID";
100
101
102
103
104 public ChangeWebsiteDmsReferenceToDamMigrationTask(String taskName, String taskDescription, String contentRepository, List<String> contentPathsList) {
105 super(taskName, taskDescription, "dms");
106 this.contentPathsList = contentPathsList;
107 this.contentRepository = contentRepository;
108 }
109
110 @Override
111 public void doExecute(InstallContext ctx) throws TaskExecutionException {
112 log.info("Start to update DATA reference to DAM for the following repository ");
113 try {
114
115 contentSession = ctx.getJCRSession(contentRepository);
116 damSession = ctx.getJCRSession(DamConstants.WORKSPACE);
117 for (String path : this.contentPathsList) {
118 if (!contentSession.nodeExists(path)) {
119 log.warn("'{}' path do not exist for the following repository: '{}' No Data migration will be performed ", path, contentRepository);
120 continue;
121 }
122
123 handlePath(path);
124 }
125 } catch (Exception e) {
126 log.error("Unable to execute update of DATA reference to DAM", e);
127 ctx.error("Unable to perform Migration task " + getName(), e);
128 throw new TaskExecutionException(e.getMessage());
129 }
130 log.info("Successfully execute update of DATA reference to DAM for the following repository ");
131 }
132
133
134
135
136 private void handlePath(String path) throws RepositoryException {
137
138 Node rootNode = contentSession.getNode(path);
139 if (rootNode.hasNodes()) {
140 String query = createQuery(path);
141 QueryResult result = executeQuery(query, contentSession);
142 if (result != null) {
143 NodeIterator nodeIterator = result.getNodes();
144 while (nodeIterator.hasNext()) {
145 Node node = nodeIterator.nextNode();
146 handleDamReferenceForNode(node);
147 }
148 } else {
149 log.info("No Node found for the following JCR-JQOM query '{}' against the following session '{}'", query, contentSession.getWorkspace().getName());
150 }
151 }
152
153
154 handleDamReferenceForNode(rootNode);
155 }
156
157
158
159
160 private void handleDamReferenceForNode(Node node) throws RepositoryException {
161 Node wrappedNode = new StringPropertyValueFilteringNodeWrapper(node, getPropertyValue());
162 PropertyIterator propertyIterator = wrappedNode.getProperties();
163 while (propertyIterator.hasNext()) {
164 Property property = propertyIterator.nextProperty();
165 String propertyName = property.getName();
166 String propertyUUIDName = DamMigrationUtil.buildI18nSuffixPropertyName(propertyName, propertySuffix);
167
168 handleDamReferenceForProperty(node, property, propertyUUIDName);
169 }
170 }
171
172
173
174
175
176
177
178
179
180
181
182
183 private void handleDamReferenceForProperty(Node node, Property property, String propertyUUIDName) throws RepositoryException {
184 if (node.hasProperty(propertyUUIDName)) {
185
186 Property uuid = node.getProperty(propertyUUIDName);
187 String propertyName = property.getName();
188 property.setValue(new ItemKey(DamConstants.DEFAULT_JCR_PROVIDER_ID, uuid.getString()).asString());
189 if (!damNodeExist(uuid.getString())) {
190 log.warn("The property '{}' will be removed from the following content node '{}'", propertyName, node.getPath());
191 property.remove();
192 }
193 log.debug("Property was removed, '{}' changed to contain the following UUID link '{}'", propertyName, uuid.getString());
194 uuid.remove();
195
196 } else {
197 log.warn("Following content should have an UUID link named:'{}' but don't have one. This property will be removed from the following content node '{}'", propertyUUIDName, node.getPath());
198 property.remove();
199 }
200 }
201
202
203
204
205
206 private boolean damNodeExist(String identifier) {
207 try {
208 damSession.getNodeByIdentifier(identifier);
209 return true;
210 } catch (ItemNotFoundException infe) {
211 log.warn("Following identifier not found in the DAM workspace " + identifier);
212 return false;
213 } catch (RepositoryException re) {
214 log.warn("DamSession.getNodeByIdentifier(" + identifier + ") generated a RepositoryException.", re);
215 return false;
216 }
217 }
218 }