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