View Javadoc
1   /**
2    * This file Copyright (c) 2007-2015 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.lang3.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   */
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       * Bootstraps fragment of file.
78       *
79       * @param resource - resource file i.e. /mgnl-bootstrap/standard-templating-kit/dialogs/pages/config.modules.standard-templating-kit.dialogs.pages.article.stkArticleProperties.xml
80       * @param itemPath - path in the file of the node you want to bootstrap i.e. /stkArticleProperties/tabCategorization
81       */
82      public PartialBootstrapTask(String name, String description, String resource, String itemPath) {
83          this(name, description, resource, itemPath, defaultImportUUIDBehavior);
84      }
85  
86      /**
87       * Bootstraps newly created file.
88       *
89       * @param resource - resource file i.e. /mgnl-bootstrap/standard-templating-kit/dialogs/pages/config.modules.standard-templating-kit.dialogs.pages.article.stkArticleProperties.xml
90       * @param itemPath - path in the file of the node you want to bootstrap i.e. /stkArticleProperties/tabCategorization
91       * @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
92       */
93      public PartialBootstrapTask(String name, String description, String resource, String itemPath, String targetResource) {
94          this(name, description, resource, itemPath, targetResource, ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW);
95      }
96  
97      public PartialBootstrapTask(String name, String description, String resource, String itemPath, String targetResource, int importUUIDBehavior) {
98          this(name, description, resource, itemPath, importUUIDBehavior);
99          this.targetResource = targetResource;
100     }
101 
102     public PartialBootstrapTask(String name, String description, String resource, String itemPath, int importUUIDBehavior) {
103         super(name, description);
104 
105         this.importUUIDBehavior = importUUIDBehavior;
106         this.resource = resource;
107         this.itemPath = StringUtils.chomp(itemPath, "/").replace(".", "..");
108         this.itemName = StringUtils.substringAfterLast(itemPath, "/");
109     }
110 
111     @Override
112     public void execute(InstallContext ctx) throws TaskExecutionException {
113 
114         try {
115             String outputResourceName = getOutputResourceName(resource, itemPath);
116             //bootstrap
117             bootstrap(outputResourceName, itemName, importUUIDBehavior, getNodeStream(resource, itemPath));
118 
119         } catch (IOException e) {
120             throw new TaskExecutionException("Cant find resource file");
121         } catch (RepositoryException e) {
122             throw new TaskExecutionException("Cant bootstrap resource file");
123         }
124 
125     }
126 
127     protected InputStream getNodeStream(String fileName, String nodePath) {
128 
129         return BootstrapFileUtil.getElementAsStream(fileName, nodePath);
130 
131     }
132 
133     protected void bootstrap(String resourceName, String itemName, int importUUIDBehavior, InputStream stream) throws IOException, RepositoryException {
134         //TODO: Code partially copied from BootstrapUtil class, need to refactor the core class to accept Inputstreams
135 
136         String name = BootstrapUtil.getFilenameFromResource(resourceName, ".xml");
137         String repository = BootstrapUtil.getWorkspaceNameFromResource(resourceName);
138         String pathName = BootstrapUtil.getPathnameFromResource(resourceName);
139         String fullPath = BootstrapUtil.getFullpathFromResource(resourceName);
140 
141         log.debug("Will bootstrap {}", resourceName);
142         if (stream == null) {
143             throw new IOException("Can't find resource to bootstrap at " + resourceName);
144         }
145 
146         // if the path already exists --> delete it
147         try {
148             final HierarchyManager hm = MgnlContext.getHierarchyManager(repository);
149 
150             // hm can be null if module is not properly registered and the repository has not been created
151             if (hm != null && hm.isExist(fullPath)) {
152                 hm.delete(fullPath);
153                 log.warn("Deleted already existing node for bootstrapping: {}", fullPath);
154             }
155         } catch (RepositoryException e) {
156             throw new RepositoryException("Can't check existence of node for bootstrap file: [" + name + "]", e);
157         }
158 
159         DataTransporter.importXmlStream(stream, repository, pathName, name, false, importUUIDBehavior, false, true);
160     }
161 
162     protected String getOutputResourceName(final String resource, final String itemPath) {
163         // get name as config.modules.xxx
164         String inputResourceName = BootstrapUtil.getFilenameFromResource(resource, ".xml");
165         //replacing all "/" with "."; getting string after first node
166         String tmpitemPath = itemPath.replace("/", ".");
167 
168         tmpitemPath = StringUtils.removeStart(tmpitemPath, ".");
169         tmpitemPath = StringUtils.substringAfter(tmpitemPath, ".");
170         String outputResourceName = inputResourceName + "." + tmpitemPath;
171         if (StringUtils.isNotEmpty(targetResource)) {
172             outputResourceName = targetResource;
173         }
174         return outputResourceName;
175     }
176 
177     protected String getResource() {
178         return resource;
179     }
180 
181     protected String getItemName() {
182         return itemName;
183     }
184 
185     protected String getItemPath() {
186         return itemPath;
187     }
188 
189 }