View Javadoc
1   /**
2    * This file Copyright (c) 2012-2016 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.dam.app.setup.migration;
35  
36  import info.magnolia.dam.jcr.DamConstants;
37  import info.magnolia.jcr.util.NodeUtil;
38  import info.magnolia.jcr.util.NodeVisitor;
39  import info.magnolia.module.InstallContext;
40  import info.magnolia.module.delta.AbstractRepositoryTask;
41  import info.magnolia.module.delta.TaskExecutionException;
42  
43  import java.util.ArrayList;
44  import java.util.List;
45  
46  import javax.jcr.Node;
47  import javax.jcr.Session;
48  
49  import org.slf4j.Logger;
50  import org.slf4j.LoggerFactory;
51  
52  /**
53   * This task is responsible for cleaning the content repository.<br>
54   * Iterate the list of contentPathsList, get the related Node, and apply the
55   * visitor to the Node tree.<br>
56   * It use the {@link NodeVisitor} pattern. In order to define your own
57   * NodeVisior, extend this migration task and override the addCustomVisitors()
58   * protected method.<br>
59   **/
60  public abstract class AbstractCleanContentForDamMigrationTask extends AbstractRepositoryTask {
61  
62      private static final Logger log = LoggerFactory.getLogger(AbstractCleanContentForDamMigrationTask.class);
63  
64      private List<String> contentPathsList;
65      private String contentRepository;
66      protected Session contentSession;
67      protected Session damSession;
68  
69      /**
70       * Default constructor.
71       */
72      public AbstractCleanContentForDamMigrationTask(String taskName, String taskDescription, String contentRepository, List<String> contentPathsList) {
73          super(taskName, taskDescription);
74          this.contentPathsList = contentPathsList;
75          this.contentRepository = contentRepository;
76      }
77  
78      @Override
79      public void doExecute(InstallContext ctx) throws TaskExecutionException {
80          log.info("Start to clean Content repository ");
81          try {
82              // Init
83              contentSession = ctx.getJCRSession(contentRepository);
84              damSession = ctx.getJCRSession(DamConstants.WORKSPACE);
85              for (String path : this.contentPathsList) {
86                  if (!contentSession.nodeExists(path)) {
87                      log.warn("'{}' path do not exist for the following repository: '{}' No Data migration will be performed", path, contentRepository);
88                      continue;
89                  }
90                  Node contentNode = contentSession.getNode(path);
91                  List<NodeVisitor> visitors = createVisitorList();
92                  for (NodeVisitor visitor : visitors) {
93                      NodeUtil.visit(contentNode, visitor);
94                  }
95              }
96          } catch (Exception e) {
97              log.error("Unable to clean content repository", e);
98              ctx.error("Unable to perform Migration task " + getName(), e);
99              throw new TaskExecutionException(e.getMessage());
100         }
101         log.info("Successfully execute cleanup of the content repository ");
102     }
103 
104     /**
105      * Create the NodeVisitor list used to navigate into the content tree and modify Data references.
106      */
107     private List<NodeVisitor> createVisitorList() {
108         List<NodeVisitor> visitorList = new ArrayList<NodeVisitor>();
109         List<NodeVisitor> customVisitor = addCustomVisitors();
110         if (customVisitor != null) {
111             visitorList.addAll(customVisitor);
112         }
113         return visitorList;
114     }
115 
116     /**
117      * Override in order to add your custom visitors.
118      */
119     abstract List<NodeVisitor> addCustomVisitors();
120 
121 }