KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > module > admininterface > pages > DevelopmentUtilsPage


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2006 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.module.admininterface.pages;
14
15 import info.magnolia.cms.beans.config.ContentRepository;
16 import info.magnolia.cms.beans.config.ModuleLoader;
17 import info.magnolia.cms.core.Content;
18 import info.magnolia.cms.core.HierarchyManager;
19 import info.magnolia.cms.core.ItemType;
20 import info.magnolia.cms.core.Path;
21 import info.magnolia.cms.core.Content.ContentFilter;
22 import info.magnolia.cms.core.ie.DataTransporter;
23 import info.magnolia.cms.security.AccessDeniedException;
24 import info.magnolia.cms.util.AlertUtil;
25 import info.magnolia.cms.util.ContentUtil;
26 import info.magnolia.context.MgnlContext;
27 import info.magnolia.module.admininterface.TemplatedMVCHandler;
28
29 import java.io.File JavaDoc;
30 import java.io.FileNotFoundException JavaDoc;
31 import java.io.FileOutputStream JavaDoc;
32 import java.io.IOException JavaDoc;
33 import java.util.Iterator JavaDoc;
34
35 import javax.jcr.PathNotFoundException;
36 import javax.jcr.RepositoryException;
37 import javax.jcr.Session;
38 import javax.servlet.http.HttpServletRequest JavaDoc;
39 import javax.servlet.http.HttpServletResponse JavaDoc;
40
41 import org.apache.commons.io.IOUtils;
42 import org.apache.commons.lang.StringUtils;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45
46
47 /**
48  * Utilities that can be used during development.
49  * @author Fabrizio Giustina
50  * @version $Revision: 6341 $ ($Author: philipp $)
51  */

52 public class DevelopmentUtilsPage extends TemplatedMVCHandler {
53
54     private boolean templates;
55
56     private boolean paragraphs;
57
58     private boolean dialogs;
59
60     private boolean website;
61
62     private boolean users;
63
64     private boolean groups;
65
66     private boolean roles;
67
68     private boolean secure;
69
70     private String JavaDoc rootdir;
71
72     private String JavaDoc parentpath;
73
74     private String JavaDoc repository;
75
76     private static ContentFilter MAGNOLIA_FILTER = new ContentFilter() {
77
78         public boolean accept(Content content) {
79
80             try {
81                 String JavaDoc nodetype = content.getNodeType().getName();
82                 // export only "magnolia" nodes
83
return nodetype.startsWith("mgnl:");
84             }
85             catch (RepositoryException e) {
86                 log.error("Unable to read nodetype for node {}", content.getHandle());
87             }
88             return false;
89         }
90     };
91
92     /**
93      * Logger.
94      */

95     protected static Logger log = LoggerFactory.getLogger(DevelopmentUtilsPage.class);
96
97     /**
98      * @param name
99      * @param request
100      * @param response
101      */

102     public DevelopmentUtilsPage(String JavaDoc name, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
103         super(name, request, response);
104     }
105
106     /**
107      * Setter for <code>dialogs</code>.
108      * @param dialogs The dialogs to set.
109      */

110     public void setDialogs(boolean dialogs) {
111         this.dialogs = dialogs;
112     }
113
114     /**
115      * Setter for <code>paragraphs</code>.
116      * @param paragraphs The paragraphs to set.
117      */

118     public void setParagraphs(boolean paragraphs) {
119         this.paragraphs = paragraphs;
120     }
121
122     /**
123      * Setter for <code>templates</code>.
124      * @param templates The templates to set.
125      */

126     public void setTemplates(boolean templates) {
127         this.templates = templates;
128     }
129
130     /**
131      * Setter for <code>rootdir</code>.
132      * @param rootdir The rootdir to set.
133      */

134     public void setRootdir(String JavaDoc rootdir) {
135         this.rootdir = rootdir;
136     }
137
138     /**
139      * Setter for <code>website</code>.
140      * @param website The website to set.
141      */

142     public void setWebsite(boolean website) {
143         this.website = website;
144     }
145
146     /**
147      * Setter for <code>parentpath</code>.
148      * @param parentpath The parentpath to set.
149      */

150     public void setParentpath(String JavaDoc parentpath) {
151         this.parentpath = parentpath;
152     }
153
154     /**
155      * Setter for <code>groups</code>.
156      * @param groups The groups to set.
157      */

158     public void setGroups(boolean groups) {
159         this.groups = groups;
160     }
161
162     /**
163      * Setter for <code>roles</code>.
164      * @param roles The roles to set.
165      */

166     public void setRoles(boolean roles) {
167         this.roles = roles;
168     }
169
170     /**
171      * Setter for <code>users</code>.
172      * @param users The users to set.
173      */

174     public void setUsers(boolean users) {
175         this.users = users;
176     }
177
178     /**
179      * Setter for <code>repository</code>.
180      * @param repository The repository to set.
181      */

182     public void setRepository(String JavaDoc repository) {
183         this.repository = repository;
184     }
185
186     public Iterator JavaDoc getRepositories() {
187         return ContentRepository.getAllRepositoryNames();
188     }
189
190     /**
191      * Setter for <code>secure</code>.
192      * @param secure The secure to set.
193      */

194     public void setSecure(boolean secure) {
195         this.secure = secure;
196     }
197
198     public String JavaDoc backup() {
199
200         Iterator JavaDoc modules = ModuleLoader.getInstance().getModuleInstances().keySet().iterator();
201         HierarchyManager hm = MgnlContext.getHierarchyManager(ContentRepository.CONFIG);
202         Session session = hm.getWorkspace().getSession();
203
204         while (modules.hasNext()) {
205             String JavaDoc moduleName = (String JavaDoc) modules.next();
206
207             if (!"templating".equals(moduleName)) {
208                 // @todo temporary, this is what I need for now...
209
continue;
210             }
211             try {
212                 Content moduleroot = hm.getContent("/modules/" + moduleName);
213                 if (templates) {
214                     exportChildren(ContentRepository.CONFIG, session, moduleroot, "templates", new ItemType[]{
215                         ItemType.CONTENT,
216                         ItemType.CONTENTNODE}, false);
217                 }
218                 if (paragraphs) {
219                     exportChildren(ContentRepository.CONFIG, session, moduleroot, "paragraphs", new ItemType[]{
220                         ItemType.CONTENT,
221                         ItemType.CONTENTNODE}, false);
222                 }
223                 if (dialogs) {
224                     exportChildren(
225                         ContentRepository.CONFIG,
226                         session,
227                         moduleroot,
228                         "dialogs",
229                         new ItemType[]{ItemType.CONTENT},
230                         true);
231                 }
232                 AlertUtil.setMessage("Backup done!");
233             }
234             catch (Exception JavaDoc e) {
235                 log.error(e.getMessage(), e);
236                 AlertUtil.setMessage("Error while processing module " + moduleName, e);
237             }
238
239         }
240
241         if (secure) {
242             backupChildren(ContentRepository.CONFIG, "/server/secureURIList");
243             backupChildren(ContentRepository.CONFIG, "/server/unsecureURIList");
244         }
245
246         if (website) {
247             extractWorkspaceRoots(ContentRepository.WEBSITE);
248         }
249
250         if (users) {
251             extractWorkspaceRoots(ContentRepository.USERS);
252         }
253
254         if (groups) {
255             extractWorkspaceRoots(ContentRepository.USER_GROUPS);
256         }
257
258         if (roles) {
259             extractWorkspaceRoots(ContentRepository.USER_ROLES);
260         }
261
262         return this.show();
263     }
264
265     /**
266      * @param repositoryName
267      */

268     private void extractWorkspaceRoots(String JavaDoc repositoryName) {
269         try {
270             HierarchyManager hm = MgnlContext.getHierarchyManager(repositoryName);
271             Content wesiteRoot = hm.getRoot();
272
273             Iterator JavaDoc children = wesiteRoot.getChildren(MAGNOLIA_FILTER).iterator();
274             while (children.hasNext()) {
275                 Content exported = (Content) children.next();
276                 exportNode(repositoryName, hm.getWorkspace().getSession(), exported);
277             }
278         }
279         catch (Exception JavaDoc e) {
280             log.error(e.getMessage(), e);
281             AlertUtil.setMessage("Error while processing " + repositoryName + " repository", e);
282         }
283     }
284
285     public String JavaDoc backupChildren() {
286         backupChildren(this.repository, this.parentpath);
287         AlertUtil.setMessage("Backup done!");
288
289         return this.show();
290     }
291
292     private void backupChildren(String JavaDoc repository, String JavaDoc parentpath) {
293         HierarchyManager hm = MgnlContext.getHierarchyManager(repository);
294         try {
295             Content parentNode = hm.getContent(parentpath);
296             Iterator JavaDoc children = parentNode.getChildren(MAGNOLIA_FILTER).iterator();
297             while (children.hasNext()) {
298                 Content exported = (Content) children.next();
299                 exportNode(repository, hm.getWorkspace().getSession(), exported);
300             }
301
302         }
303         catch (Exception JavaDoc e) {
304             log.error(e.getMessage(), e);
305             AlertUtil.setMessage("Error while processing actions", e);
306         }
307
308     }
309
310     /**
311      * @param session
312      * @param moduleroot
313      * @param exportContentContainingContentNodes
314      * @throws PathNotFoundException
315      * @throws RepositoryException
316      * @throws AccessDeniedException
317      * @throws FileNotFoundException
318      * @throws IOException
319      */

320     private void exportChildren(String JavaDoc repository, Session session, Content moduleroot, String JavaDoc path,
321         ItemType[] itemTypes, boolean exportContentContainingContentNodes) throws PathNotFoundException,
322         RepositoryException, AccessDeniedException, FileNotFoundException JavaDoc, IOException JavaDoc {
323         Content templateRoot = moduleroot.getContent(path);
324
325         Iterator JavaDoc children = ContentUtil.collectAllChildren(templateRoot, itemTypes).iterator();
326         while (children.hasNext()) {
327             Content exported = (Content) children.next();
328             if (!exported.getNodeDataCollection().isEmpty() // ignore "directories"
329
|| (exportContentContainingContentNodes && exported.hasChildren(ItemType.CONTENTNODE.getSystemName()))) {
330                 exportNode(repository, session, exported);
331             }
332         }
333     }
334
335     /**
336      * @param session
337      * @param exported
338      * @throws FileNotFoundException
339      * @throws IOException
340      */

341     private void exportNode(String JavaDoc repository, Session session, Content exported) throws FileNotFoundException JavaDoc,
342         IOException JavaDoc {
343         String JavaDoc handle = exported.getHandle();
344
345         String JavaDoc xmlName = repository + StringUtils.replace(handle, "/", ".") + ".xml";
346
347         // create necessary parent directories
348
File JavaDoc folder = new File JavaDoc(Path.getAbsoluteFileSystemPath(rootdir), repository);
349         folder.mkdirs();
350         File JavaDoc xmlFile = new File JavaDoc(folder.getAbsoluteFile(), xmlName);
351         FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(xmlFile);
352
353         try {
354             DataTransporter.executeExport(fos, false, true, session, handle, repository, DataTransporter.XML);
355         }
356         finally {
357             IOUtils.closeQuietly(fos);
358         }
359     }
360 }
361
Popular Tags