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.cms.util.QueryUtil;
37 import info.magnolia.dam.api.ItemKey;
38 import info.magnolia.dam.jcr.DamConstants;
39 import info.magnolia.jcr.util.NodeUtil;
40 import info.magnolia.module.InstallContext;
41 import info.magnolia.module.delta.AbstractRepositoryTask;
42 import info.magnolia.module.delta.TaskExecutionException;
43
44 import java.util.List;
45
46 import javax.jcr.Node;
47 import javax.jcr.NodeIterator;
48 import javax.jcr.Property;
49 import javax.jcr.RepositoryException;
50 import javax.jcr.Session;
51 import javax.jcr.query.Query;
52
53 import org.apache.commons.lang.StringUtils;
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
88
89
90
91
92
93
94
95
96
97
98
99 public class ChangeDataDmsReferenceToDamMigrationTask extends AbstractRepositoryTask {
100
101 private static final Logger log = LoggerFactory.getLogger(ChangeDataDmsReferenceToDamMigrationTask.class);
102
103 private final List<String> contentPathsList;
104 private final String contentRepository;
105 private final String identifierPropertyName;
106 private final String pathPropertyName;
107 private Session contentSession;
108 private Session damSession;
109
110
111
112
113 public ChangeDataDmsReferenceToDamMigrationTask(String taskName, String taskDescription, String contentRepository, List<String> contentPathsList, String identifierPropertyName, String pathPropertyName) {
114 super(taskName, taskDescription);
115 this.contentPathsList = contentPathsList;
116 this.contentRepository = contentRepository;
117 this.identifierPropertyName = StringUtils.isNotBlank(identifierPropertyName) ? identifierPropertyName : "imgUUID";
118 this.pathPropertyName = StringUtils.isNotBlank(pathPropertyName) ? pathPropertyName : "img";
119 ;
120 }
121
122 @Override
123 public void doExecute(InstallContext ctx) throws TaskExecutionException {
124 log.info("Start to update DATA reference to DAM for the following repository '{}'", this.contentRepository);
125 try {
126
127 contentSession = ctx.getJCRSession(contentRepository);
128 damSession = ctx.getJCRSession(DamConstants.WORKSPACE);
129 for (String path : this.contentPathsList) {
130 if (!contentSession.nodeExists(path)) {
131 log.warn("'{}' path do not exist for the following repository: '{}' No Data migration will be performed ", path, contentRepository);
132 continue;
133 }
134
135 handlePath(path);
136 }
137 } catch (Exception e) {
138 log.error("Unable to execute update of DATA reference to DAM", e);
139 ctx.error("Unable to perform Migration task " + getName(), e);
140 throw new TaskExecutionException(e.getMessage());
141 }
142 log.info("Successfully execute update of DATA reference to DAM for the following repository '{}'", this.contentRepository);
143 }
144
145
146
147
148 private void handlePath(String path) throws RepositoryException {
149 final String queryString = "SELECT * FROM [nt:base] AS t WHERE (ISSAMENODE(t, '" + path + "') OR ISDESCENDANTNODE(t, '" + path + "')) AND t." + this.identifierPropertyName + " is not null";
150 NodeIterator iterator = QueryUtil.search(this.contentRepository, queryString, Query.JCR_SQL2);
151 while (iterator.hasNext()) {
152 Node node = iterator.nextNode();
153 handleDamReferenceForNode(node);
154 }
155 }
156
157
158
159
160 private void handleDamReferenceForNode(Node node) throws RepositoryException {
161 Property identifierProperty = node.getProperty(this.identifierPropertyName);
162 Property pathProperty = null;
163 if (node.hasProperty(this.pathPropertyName)) {
164 pathProperty = node.getProperty(this.pathPropertyName);
165 }
166
167
168 Node assetNode = getAssetNode(identifierProperty.getString());
169 if (assetNode != null) {
170 String compositeId = new ItemKey(DamConstants.DEFAULT_JCR_PROVIDER_ID, identifierProperty.getString()).asString();
171 if (pathProperty != null && StringUtils.equals("dms", pathProperty.getString())) {
172 handleDmsReference(identifierProperty, pathProperty, assetNode, compositeId);
173 } else if (pathProperty != null && StringUtils.equals("upload", pathProperty.getString())) {
174 log.info("Uploaded content not handled by this task");
175 } else {
176 handleDataReference(identifierProperty, pathProperty, assetNode, compositeId);
177 }
178 } else {
179
180 log.info("No Asset found for the following identifier '{}'. The property named '{}' will be ignore", identifierProperty.getString(), this.identifierPropertyName);
181 }
182 }
183
184
185
186
187 protected void handleDataReference(Property identifierProperty, Property pathProperty, Node assetNode, String compositeId) throws RepositoryException {
188 identifierProperty.setValue(compositeId);
189 if (pathProperty != null) {
190 pathProperty.setValue(assetNode.getPath());
191 log.info("The value of the property '{}' was changed to point to the following composite asset path '{}' .", pathProperty.getName(), NodeUtil.getPathIfPossible(assetNode));
192 }
193 log.info("The value of the property '{}' was changed to the following composite id '{}' referencing '{}' asset.", identifierProperty.getName(), compositeId, NodeUtil.getPathIfPossible(assetNode));
194 }
195
196
197
198
199 protected void handleDmsReference(Property identifierProperty, Property pathProperty, Node assetNode, String compositeId) throws RepositoryException {
200
201 pathProperty.setValue(compositeId);
202 identifierProperty.remove();
203 log.info("The value of the property '{}' was changed from 'dms' to the following composite id '{}' referencing '{}' asset.", pathProperty.getName(), compositeId, NodeUtil.getPathIfPossible(assetNode));
204 }
205
206
207
208
209 private Node getAssetNode(String identifier) {
210 try {
211 return damSession.getNodeByIdentifier(identifier);
212 } catch (RepositoryException re) {
213 return null;
214 }
215 }
216 }