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.importexport;
35
36 import info.magnolia.cms.core.Content;
37 import info.magnolia.cms.core.HierarchyManager;
38 import info.magnolia.cms.util.SiblingsHelper;
39 import info.magnolia.cms.util.StringLengthComparator;
40 import info.magnolia.context.MgnlContext;
41
42 import java.io.File;
43 import java.io.FileOutputStream;
44 import java.io.IOException;
45 import java.io.InputStream;
46 import java.util.ArrayList;
47 import java.util.Arrays;
48 import java.util.Collections;
49 import java.util.Iterator;
50 import java.util.List;
51
52 import javax.jcr.RepositoryException;
53
54 import org.apache.commons.io.IOUtils;
55 import org.apache.commons.lang.StringUtils;
56 import org.slf4j.Logger;
57 import org.slf4j.LoggerFactory;
58
59
60
61
62
63
64 public class BootstrapUtil {
65 private static final Logger log = LoggerFactory.getLogger(BootstrapUtil.class);
66
67 public static void bootstrap(String[] resourceNames, int importUUIDBehavior) throws IOException, RepositoryException {
68
69 List<String> list = new ArrayList<String>(Arrays.asList(resourceNames));
70
71 Collections.sort(list, new StringLengthComparator());
72
73 for (Iterator<String> iter = list.iterator(); iter.hasNext();) {
74 String resourceName = iter.next();
75
76 String name = getFilenameFromResource(resourceName, ".xml");
77 String repository = getWorkspaceNameFromResource(resourceName);
78 String pathName = getPathnameFromResource(resourceName);
79 String fullPath = getFullpathFromResource(resourceName);
80 String nodeName = StringUtils.substringAfterLast(fullPath, "/");
81
82 log.debug("Will bootstrap {}", resourceName);
83
84 final InputStream stream = BootstrapUtil.class.getResourceAsStream(resourceName);
85 if (stream == null) {
86 throw new IOException("Can't find resource to bootstrap at " + resourceName);
87 }
88
89
90 String nameOfNodeAfterTheImportedNode = null;
91
92 final HierarchyManager hm = MgnlContext.getHierarchyManager(repository);
93
94
95 try {
96
97
98 if (hm != null && hm.isExist(fullPath)) {
99
100 Content node = hm.getContent(fullPath);
101 SiblingsHelper siblings = SiblingsHelper.of(node);
102 if(!siblings.isLast()){
103 nameOfNodeAfterTheImportedNode = siblings.next().getName();
104 }
105
106 hm.delete(fullPath);
107 log.warn("Deleted already existing node for bootstrapping: {}", fullPath);
108 }
109 } catch (RepositoryException e) {
110 throw new RepositoryException("Can't check existence of node for bootstrap file: [" + name + "]", e);
111 }
112
113 DataTransporter.importXmlStream(stream, repository, pathName, name, false, importUUIDBehavior, false, true);
114
115 if(nameOfNodeAfterTheImportedNode != null){
116 Content newNode = hm.getContent(fullPath);
117 newNode.getParent().orderBefore(nodeName, nameOfNodeAfterTheImportedNode);
118 }
119
120 }
121 }
122
123 public static void export(Content content, File directory) throws IOException, RepositoryException{
124 String fileName = content.getWorkspace().getName() + DataTransporter.createExportPath(content.getHandle()) + ".xml";
125 File file = new File(directory, fileName);
126 FileOutputStream out = new FileOutputStream(file);
127 try{
128 DataTransporter.executeExport(out,false, true, content.getWorkspace().getSession(), content.getHandle(), content.getWorkspace().getName(), DataTransporter.XML);
129 }
130 finally{
131 IOUtils.closeQuietly(out);
132 }
133 }
134
135
136
137
138 public static String getWorkspaceNameFromResource(String resourcePath) {
139 String resourceName = StringUtils.replace(resourcePath, "\\", "/");
140
141 String name = getFilenameFromResource(resourceName, ".xml");
142 String fullPath = DataTransporter.revertExportPath(name);
143 return StringUtils.substringBefore(fullPath, "/");
144 }
145
146
147
148
149 public static String getFullpathFromResource(String resourcePath) {
150 String resourceName = StringUtils.replace(resourcePath, "\\", "/");
151
152 String name = getFilenameFromResource(resourceName, ".xml");
153 String fullPath = DataTransporter.revertExportPath(name);
154 String repository = StringUtils.substringBefore(fullPath, "/");
155
156 return StringUtils.removeStart(fullPath, repository);
157 }
158
159
160
161
162 public static String getPathnameFromResource(String resourcePath) {
163 String resourceName = StringUtils.replace(resourcePath, "\\", "/");
164
165 String name = getFilenameFromResource(resourceName, ".xml");
166 String fullPath = DataTransporter.revertExportPath(name);
167 String pathName = StringUtils.substringAfter(StringUtils.substringBeforeLast(fullPath, "/"), "/");
168
169 if(!pathName.startsWith("/")) {
170 pathName = "/" + pathName;
171 }
172 return pathName;
173 }
174
175
176
177
178
179 public static String getFilenameFromResource(String resourcePath, String extension) {
180 String ext = StringUtils.defaultIfEmpty(extension, ".xml");
181 return StringUtils.removeEnd(StringUtils.substringAfterLast(resourcePath, "/"), ext.startsWith(".") ? ext : "." + ext);
182 }
183
184 }