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