View Javadoc

1   /**
2    * This file Copyright (c) 2007-2011 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
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.lang.StringUtils;
49  import org.slf4j.Logger;
50  import org.slf4j.LoggerFactory;
51  
52  /**
53   * This task is used to bootstrap a part of a file.
54   * @author tmiyar
55   *
56   */
57  public class PartialBootstrapTask extends AbstractTask {
58  
59      private static Logger log = LoggerFactory.getLogger(PartialBootstrapTask.class);
60  
61      private final String resource;
62      private final String itemName;
63      private final String itemPath;
64      private final int importUUIDBehavior;
65  
66      private String targetResource;
67      static private final int defaultImportUUIDBehavior = ImportUUIDBehavior.IMPORT_UUID_COLLISION_THROW;
68  
69      /**
70       * Bootstraps fragment of file.
71       * @param resource - resource file i.e. /mgnl-bootstrap/standard-templating-kit/dialogs/pages/config.modules.standard-templating-kit.dialogs.pages.article.stkArticleProperties.xml
72       * @param itemPath - path in the file of the node you want to bootstrap i.e. /stkArticleProperties/tabCategorization
73       */
74      public PartialBootstrapTask(String name, String description, String resource, String itemPath) {
75          this(name, description, resource, itemPath, defaultImportUUIDBehavior);
76      }
77  
78      /**
79       * Bootstraps newly created file.
80       * @param resource - resource file i.e. /mgnl-bootstrap/standard-templating-kit/dialogs/pages/config.modules.standard-templating-kit.dialogs.pages.article.stkArticleProperties.xml
81       * @param itemPath - path in the file of the node you want to bootstrap i.e. /stkArticleProperties/tabCategorization
82       * @param targetResource - A target bootstrap file name in case you want to bootstrap the file in a different node /mgnl-bootstrap/standard-templating-kit/dialogs/pages/config.modules.standard-templating-kit.dialogs.pages.target.xml
83       */
84      public PartialBootstrapTask(String name, String description, String resource, String itemPath, String targetResource) {
85          this(name, description, resource, itemPath, targetResource, ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW);
86      }
87  
88      public PartialBootstrapTask(String name, String description, String resource, String itemPath, String targetResource, int importUUIDBehavior) {
89          this(name, description, resource, itemPath, importUUIDBehavior);
90          this.targetResource = targetResource;
91      }
92  
93      public PartialBootstrapTask(String name, String description, String resource, String itemPath, int importUUIDBehavior) {
94          super(name, description);
95  
96          this.importUUIDBehavior = importUUIDBehavior;
97          this.resource = resource;
98          this.itemPath = StringUtils.chomp(itemPath, "/").replace(".", "..");
99          this.itemName = StringUtils.substringAfterLast(itemPath , "/");
100     }
101 
102     @Override
103     public void execute(InstallContext ctx) throws TaskExecutionException {
104 
105         try {
106             String outputResourceName = getOutputResourceName(resource, itemPath);
107             //bootstrap
108             bootstrap(outputResourceName, itemName, importUUIDBehavior, getNodeStream(resource, itemPath));
109 
110         } catch (IOException e) {
111             throw new TaskExecutionException("Cant find resource file");
112         } catch (RepositoryException e) {
113             throw new TaskExecutionException("Cant bootstrap resource file");
114         }
115 
116     }
117 
118     protected InputStream getNodeStream(String fileName, String nodePath) {
119 
120         return BootstrapFileUtil.getElementAsStream(fileName, nodePath);
121 
122     }
123 
124     protected void bootstrap(String resourceName, String itemName, int importUUIDBehavior, InputStream stream) throws IOException, RepositoryException {
125         //TODO: Code partially copied from BootstrapUtil class, need to refactor the core class to accept Inputstreams
126 
127         String name = BootstrapUtil.getFilenameFromResource(resourceName, ".xml");
128         String repository = BootstrapUtil.getWorkspaceNameFromResource(resourceName);
129         String pathName = BootstrapUtil.getPathnameFromResource(resourceName);
130         String fullPath = BootstrapUtil.getFullpathFromResource(resourceName);
131 
132         log.debug("Will bootstrap {}", resourceName);
133         if (stream == null) {
134             throw new IOException("Can't find resource to bootstrap at " + resourceName);
135         }
136 
137         // if the path already exists --> delete it
138         try {
139             final HierarchyManager hm = MgnlContext.getHierarchyManager(repository);
140 
141             // hm can be null if module is not properly registered and the repository has not been created
142             if (hm != null && hm.isExist(fullPath)) {
143                 hm.delete(fullPath);
144                 log.warn("Deleted already existing node for bootstrapping: {}", fullPath);
145             }
146         } catch (RepositoryException e) {
147             throw new RepositoryException("Can't check existence of node for bootstrap file: [" + name + "]", e);
148         }
149 
150         DataTransporter.importXmlStream(stream, repository, pathName, name, false, importUUIDBehavior, false, true);
151     }
152 
153     protected String getOutputResourceName(final String resource, final String itemPath) {
154         // get name as config.modules.xxx
155         String inputResourceName = BootstrapUtil.getFilenameFromResource(resource, ".xml");
156         //replacing all "/" with "."; getting string after first node
157         String tmpitemPath = itemPath.replace("/", ".");
158 
159         tmpitemPath = StringUtils.removeStart(tmpitemPath, ".");
160         tmpitemPath = StringUtils.substringAfter(tmpitemPath, ".");
161         String outputResourceName = inputResourceName + "." + tmpitemPath;
162         if(StringUtils.isNotEmpty(targetResource)) {
163             outputResourceName = targetResource;
164         }
165         return outputResourceName;
166     }
167 
168     protected String getResource() {
169         return resource;
170     }
171     protected String getItemName() {
172         return itemName;
173     }
174 
175     protected String getItemPath() {
176         return itemPath;
177     }
178 
179 }