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
65 public class BootstrapUtil {
66 private static final Logger log = LoggerFactory.getLogger(BootstrapUtil.class);
67
68 public static void bootstrap(String[] resourceNames, int importUUIDBehavior) throws IOException, RepositoryException {
69
70 List list = new ArrayList(Arrays.asList(resourceNames));
71
72 Collections.sort(list, new StringLengthComparator());
73
74 for (Iterator iter = list.iterator(); iter.hasNext();) {
75 String resourceName = (String) iter.next();
76
77
78 resourceName = StringUtils.replace(resourceName, "\\", "/");
79
80 String name = StringUtils.removeEnd(StringUtils.substringAfterLast(resourceName, "/"), ".xml");
81
82 String repository = StringUtils.substringBefore(name, ".");
83 String pathName = StringUtils.substringAfter(StringUtils.substringBeforeLast(name, "."), ".");
84 String nodeName = StringUtils.substringAfterLast(name, ".");
85 String fullPath;
86 if (StringUtils.isEmpty(pathName)) {
87 pathName = "/";
88 fullPath = "/" + nodeName;
89 }
90 else {
91 pathName = "/" + StringUtils.replace(pathName, ".", "/");
92 fullPath = pathName + "/" + nodeName;
93 }
94
95 log.debug("Will bootstrap {}", resourceName);
96 final InputStream stream = BootstrapUtil.class.getResourceAsStream(resourceName);
97 if (stream == null) {
98 throw new IOException("Can't find resource to bootstrap at " + resourceName);
99 }
100
101
102 String nameOfNodeAfterTheImportedNode = null;
103
104 final HierarchyManager hm = MgnlContext.getHierarchyManager(repository);
105
106
107 try {
108
109
110 if (hm != null && hm.isExist(fullPath)) {
111
112 Content node = hm.getContent(fullPath);
113 SiblingsHelper siblings = SiblingsHelper.of(node);
114 if(!siblings.isLast()){
115 nameOfNodeAfterTheImportedNode = siblings.next().getName();
116 }
117
118 hm.delete(fullPath);
119 log.warn("Deleted already existing node for bootstrapping: {}", fullPath);
120 }
121 } catch (RepositoryException e) {
122 throw new RepositoryException("Can't check existence of node for bootstrap file: [" + name + "]", e);
123 }
124
125 DataTransporter.importXmlStream(stream, repository, pathName, name, false, importUUIDBehavior, false, true);
126
127 if(nameOfNodeAfterTheImportedNode != null){
128 Content newNode = hm.getContent(fullPath);
129 newNode.getParent().orderBefore(nodeName, nameOfNodeAfterTheImportedNode);
130 }
131
132 }
133 }
134
135 public static void export(Content content, File directory) throws IOException, RepositoryException{
136 String fileName = content.getHierarchyManager().getName() + content.getHandle().replace("/", ".") + ".xml";
137 File file = new File(directory, fileName);
138 FileOutputStream out = new FileOutputStream(file);
139 try{
140 DataTransporter.executeExport(out,false, true, content.getWorkspace().getSession(), content.getHandle(), content.getHierarchyManager().getName(), DataTransporter.XML);
141 }
142 finally{
143 IOUtils.closeQuietly(out);
144 }
145 }
146
147 }