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