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.delta;
35
36 import info.magnolia.cms.core.HierarchyManager;
37 import info.magnolia.context.MgnlContext;
38 import info.magnolia.importexport.BootstrapUtil;
39 import info.magnolia.importexport.DataTransporter;
40 import info.magnolia.module.InstallContext;
41
42 import java.io.IOException;
43 import java.io.InputStream;
44
45 import javax.jcr.ImportUUIDBehavior;
46 import javax.jcr.RepositoryException;
47
48 import org.apache.commons.lang.StringUtils;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51
52
53
54
55
56
57 public class PartialBootstrapTask extends AbstractTask {
58
59 private static Logger log = LoggerFactory.getLogger(PartialBootstrapTask.class);
60
61 private final String resource;
62 private final String itemName;
63 private final String itemPath;
64 private final int importUUIDBehavior;
65
66 private String targetResource;
67 static private final int defaultImportUUIDBehavior = ImportUUIDBehavior.IMPORT_UUID_COLLISION_THROW;
68
69
70
71
72
73
74 public PartialBootstrapTask(String name, String description, String resource, String itemPath) {
75 this(name, description, resource, itemPath, defaultImportUUIDBehavior);
76 }
77
78
79
80
81
82
83
84 public PartialBootstrapTask(String name, String description, String resource, String itemPath, String targetResource) {
85 this(name, description, resource, itemPath, targetResource, ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW);
86 }
87
88 public PartialBootstrapTask(String name, String description, String resource, String itemPath, String targetResource, int importUUIDBehavior) {
89 this(name, description, resource, itemPath, importUUIDBehavior);
90 this.targetResource = targetResource;
91 }
92
93 public PartialBootstrapTask(String name, String description, String resource, String itemPath, int importUUIDBehavior) {
94 super(name, description);
95
96 this.importUUIDBehavior = importUUIDBehavior;
97 this.resource = resource;
98 this.itemPath = StringUtils.chomp(itemPath, "/").replace(".", "..");
99 this.itemName = StringUtils.substringAfterLast(itemPath , "/");
100 }
101
102 @Override
103 public void execute(InstallContext ctx) throws TaskExecutionException {
104
105 try {
106 String outputResourceName = getOutputResourceName(resource, itemPath);
107
108 bootstrap(outputResourceName, itemName, importUUIDBehavior, getNodeStream(resource, itemPath));
109
110 } catch (IOException e) {
111 throw new TaskExecutionException("Cant find resource file");
112 } catch (RepositoryException e) {
113 throw new TaskExecutionException("Cant bootstrap resource file");
114 }
115
116 }
117
118 protected InputStream getNodeStream(String fileName, String nodePath) {
119
120 return BootstrapFileUtil.getElementAsStream(fileName, nodePath);
121
122 }
123
124 protected void bootstrap(String resourceName, String itemName, int importUUIDBehavior, InputStream stream) throws IOException, RepositoryException {
125
126
127 String name = BootstrapUtil.getFilenameFromResource(resourceName, ".xml");
128 String repository = BootstrapUtil.getWorkspaceNameFromResource(resourceName);
129 String pathName = BootstrapUtil.getPathnameFromResource(resourceName);
130 String fullPath = BootstrapUtil.getFullpathFromResource(resourceName);
131
132 log.debug("Will bootstrap {}", resourceName);
133 if (stream == null) {
134 throw new IOException("Can't find resource to bootstrap at " + resourceName);
135 }
136
137
138 try {
139 final HierarchyManager hm = MgnlContext.getHierarchyManager(repository);
140
141
142 if (hm != null && hm.isExist(fullPath)) {
143 hm.delete(fullPath);
144 log.warn("Deleted already existing node for bootstrapping: {}", fullPath);
145 }
146 } catch (RepositoryException e) {
147 throw new RepositoryException("Can't check existence of node for bootstrap file: [" + name + "]", e);
148 }
149
150 DataTransporter.importXmlStream(stream, repository, pathName, name, false, importUUIDBehavior, false, true);
151 }
152
153 protected String getOutputResourceName(final String resource, final String itemPath) {
154
155 String inputResourceName = BootstrapUtil.getFilenameFromResource(resource, ".xml");
156
157 String tmpitemPath = itemPath.replace("/", ".");
158
159 tmpitemPath = StringUtils.removeStart(tmpitemPath, ".");
160 tmpitemPath = StringUtils.substringAfter(tmpitemPath, ".");
161 String outputResourceName = inputResourceName + "." + tmpitemPath;
162 if(StringUtils.isNotEmpty(targetResource)) {
163 outputResourceName = targetResource;
164 }
165 return outputResourceName;
166 }
167
168 protected String getResource() {
169 return resource;
170 }
171 protected String getItemName() {
172 return itemName;
173 }
174
175 protected String getItemPath() {
176 return itemPath;
177 }
178
179 }