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.jcr.DamConstants;
37 import info.magnolia.jcr.util.NodeUtil;
38 import info.magnolia.jcr.util.NodeVisitor;
39 import info.magnolia.module.InstallContext;
40 import info.magnolia.module.delta.AbstractRepositoryTask;
41 import info.magnolia.module.delta.TaskExecutionException;
42
43 import java.util.ArrayList;
44 import java.util.List;
45
46 import javax.jcr.Node;
47 import javax.jcr.Session;
48
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51
52
53
54
55
56
57
58
59
60 public abstract class AbstractCleanContentForDamMigrationTask extends AbstractRepositoryTask {
61
62 private static final Logger log = LoggerFactory.getLogger(AbstractCleanContentForDamMigrationTask.class);
63
64 private List<String> contentPathsList;
65 private String contentRepository;
66 protected Session contentSession;
67 protected Session damSession;
68
69
70
71
72 public AbstractCleanContentForDamMigrationTask(String taskName, String taskDescription, String contentRepository, List<String> contentPathsList) {
73 super(taskName, taskDescription);
74 this.contentPathsList = contentPathsList;
75 this.contentRepository = contentRepository;
76 }
77
78 @Override
79 public void doExecute(InstallContext ctx) throws TaskExecutionException {
80 log.info("Start to clean Content repository ");
81 try {
82
83 contentSession = ctx.getJCRSession(contentRepository);
84 damSession = ctx.getJCRSession(DamConstants.WORKSPACE);
85 for (String path : this.contentPathsList) {
86 if (!contentSession.nodeExists(path)) {
87 log.warn("'{}' path do not exist for the following repository: '{}' No Data migration will be performed", path, contentRepository);
88 continue;
89 }
90 Node contentNode = contentSession.getNode(path);
91 List<NodeVisitor> visitors = createVisitorList();
92 for (NodeVisitor visitor : visitors) {
93 NodeUtil.visit(contentNode, visitor);
94 }
95 }
96 } catch (Exception e) {
97 log.error("Unable to clean content repository", e);
98 ctx.error("Unable to perform Migration task " + getName(), e);
99 throw new TaskExecutionException(e.getMessage());
100 }
101 log.info("Successfully execute cleanup of the content repository ");
102 }
103
104
105
106
107 private List<NodeVisitor> createVisitorList() {
108 List<NodeVisitor> visitorList = new ArrayList<NodeVisitor>();
109 List<NodeVisitor> customVisitor = addCustomVisitors();
110 if (customVisitor != null) {
111 visitorList.addAll(customVisitor);
112 }
113 return visitorList;
114 }
115
116
117
118
119 abstract List<NodeVisitor> addCustomVisitors();
120
121 }