Clover icon

Magnolia Resources Module 2.4.2

  1. Project Clover database Fri Nov 6 2015 16:15:26 CET
  2. Package info.magnolia.module.resources.setup

File BackupResourceTask.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart0.png
76% of files have more coverage

Code metrics

4
27
12
1
227
82
14
0.52
2.25
12
1.17

Classes

Class Line # Actions
BackupResourceTask 57 27 0% 14 43
0.00%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /**
2    * This file Copyright (c) 2009-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.resources.setup;
35   
36    import info.magnolia.cms.core.HierarchyManager;
37    import info.magnolia.cms.core.ItemType;
38    import info.magnolia.cms.util.ContentUtil;
39    import info.magnolia.cms.util.DateUtil;
40    import info.magnolia.module.InstallContext;
41    import info.magnolia.module.delta.AbstractRepositoryTask;
42    import info.magnolia.module.delta.TaskExecutionException;
43   
44    import java.util.Calendar;
45   
46    import javax.jcr.RepositoryException;
47   
48    import org.apache.commons.lang3.StringUtils;
49   
50    /**
51    * Task to backup resources when installing updates.
52    *
53    * @author cringele
54    * @version $Id$
55    *
56    */
 
57    public class BackupResourceTask extends AbstractRepositoryTask {
58   
59    public final static String DEFAULT_BACKUP_PATH = "/backup";
60    public final static String WORKSPACE = "resources";
61    public final static String DEFAULT_TIMESTAMP_FORMAT = "'date'-yyyy-MM-dd-'time'-kk-mm-ss-SSSS";
62   
63    public final static boolean MOVE_ORIGIN_NODE = true;
64    public final static boolean COPY_ORIGIN_NODE = false;
65    public final static boolean DEFAULT_ORIGIN_NODE_ACTION = MOVE_ORIGIN_NODE;
66    public final static boolean ADD_TIMESTAMP = false;
67    public final static boolean NO_TIMESTAMP = true;
68    public final static boolean DEFAULT_TIMESTAMP_ACTION = ADD_TIMESTAMP;
69   
70    private final String resourcePath;
71    private final String backupPath;
72    private final boolean moveOrigin;
73    private final boolean noTimeStamp;
74    private final String timeStampFormat;
75   
76    /**
77    * @param name task name
78    * @param description task description
79    * @param resourcePath the path of the resource to backup
80    */
 
81  0 toggle public BackupResourceTask(String name, String description, String resourcePath) {
82  0 this(name, description, resourcePath, DEFAULT_BACKUP_PATH, DEFAULT_ORIGIN_NODE_ACTION, DEFAULT_TIMESTAMP_ACTION, DEFAULT_TIMESTAMP_FORMAT);
83    }
84   
85    /**
86    * @param name task name
87    * @param description task description
88    * @param resourcePath the path of the resource to backup
89    * @param moveOrigin true if moving the origin node, false if copying the origin node. Default value = true (DEFAULT_ORIGIN_NODE_ACTION)
90    */
 
91  0 toggle public BackupResourceTask(String name, String description, String resourcePath, boolean moveOrigin) {
92  0 this(name, description, resourcePath, DEFAULT_BACKUP_PATH, moveOrigin, DEFAULT_TIMESTAMP_ACTION, DEFAULT_TIMESTAMP_FORMAT);
93    }
94   
95    /**
96    * @param name task name
97    * @param description task description
98    * @param resourcePath the path of the resource to backup
99    * @param moveOrigin true if moving the origin node, false if copying the origin node. Default value = true (DEFAULT_ORIGIN_NODE_ACTION)
100    * @param timeStampFormat the format of the time stamp which is added to the backed up resource name. Default value = "'date'-yyyy-MM-dd-'time'-kk-mm-ss-SSSS" (DEFAULT_TIMESTAMP_FORMAT)
101    */
 
102  0 toggle public BackupResourceTask(String name, String description, String resourcePath, boolean moveOrigin, String timeStampFormat) {
103  0 this(name, description, resourcePath, DEFAULT_BACKUP_PATH, moveOrigin, DEFAULT_TIMESTAMP_ACTION, timeStampFormat);
104    }
105   
106    /**
107    * @param name task name
108    * @param description task description
109    * @param resourcePath the path of the resource to backup
110    * @param moveOrigin true if moving the origin node, false if copying the origin node. Default value = true (DEFAULT_ORIGIN_NODE_ACTION)
111    * @param noTimeStamp not adding a time stamp folder to the backup path. Default value = false (DEFAULT_TIMESTAMP_ACTION)
112    */
 
113  0 toggle public BackupResourceTask(String name, String description, String resourcePath, boolean moveOrigin, boolean noTimeStamp) {
114  0 this(name, description, resourcePath, DEFAULT_BACKUP_PATH, moveOrigin, noTimeStamp, DEFAULT_TIMESTAMP_FORMAT);
115    }
116   
117    /**
118    * @param name task name
119    * @param description task description
120    * @param resourcePath the path of the resource to backup
121    * @param backupPath destination path of the backup within resources. Default value = /backup (DEFAULT_BACKUP_PATH)
122    */
 
123  0 toggle public BackupResourceTask(String name, String description, String resourcePath, String backupPath) {
124  0 this(name, description, resourcePath, backupPath, DEFAULT_ORIGIN_NODE_ACTION, DEFAULT_TIMESTAMP_ACTION, DEFAULT_TIMESTAMP_FORMAT);
125    }
126   
127    /**
128    * @param name task name
129    * @param description task description
130    * @param resourcePath the path of the resource to backup
131    * @param backupPath destination path of the backup within resources. Default value = /backup (DEFAULT_BACKUP_PATH)
132    * @param moveOrigin true if moving the origin node, false if copying the origin node. Default value = true (DEFAULT_ORIGIN_NODE_ACTION)
133    */
 
134  0 toggle public BackupResourceTask(String name, String description, String resourcePath, String backupPath, boolean moveOrigin) {
135  0 this(name, description, resourcePath, backupPath, moveOrigin, DEFAULT_TIMESTAMP_ACTION, DEFAULT_TIMESTAMP_FORMAT);
136    }
137   
138    /**
139    * @param name task name
140    * @param description task description
141    * @param resourcePath the path of the resource to backup
142    * @param backupPath destination path of the backup within resources. Default value = /backup (DEFAULT_BACKUP_PATH)
143    * @param timeStampFormat the format of the time stamp which is added to the backed up resource name. Default value = "'date'-yyyy-MM-dd-'time'-kk-mm-ss-SSSS" (DEFAULT_TIMESTAMP_FORMAT)
144    */
 
145  0 toggle public BackupResourceTask(String name, String description, String resourcePath, String backupPath, String timeStampFormat) {
146  0 this(name, description, resourcePath, backupPath, DEFAULT_ORIGIN_NODE_ACTION, DEFAULT_TIMESTAMP_ACTION, timeStampFormat);
147    }
148   
149    /**
150    * @param name task name
151    * @param description task description
152    * @param resourcePath the path of the resource to backup
153    * @param backupPath destination path of the backup within resources. Default value = /backup (DEFAULT_BACKUP_PATH)
154    * @param moveOrigin true if moving the origin node, false if copying the origin node. Default value = true (DEFAULT_ORIGIN_NODE_ACTION)
155    * @param timeStampFormat the format of the time stamp which is added to the backed up resource name. Default value = "'date'-yyyy-MM-dd-'time'-kk-mm-ss-SSSS" (DEFAULT_TIMESTAMP_FORMAT)
156    */
 
157  0 toggle public BackupResourceTask(String name, String description, String resourcePath, String backupPath, boolean moveOrigin, String timeStampFormat) {
158  0 this(name, description, resourcePath, backupPath, moveOrigin, DEFAULT_TIMESTAMP_ACTION, timeStampFormat);
159    }
160   
161    /**
162    * @param name task name
163    * @param description task description
164    * @param resourcePath the path of the resource to backup
165    * @param backupPath destination path of the backup within resources. Default value = /backup (DEFAULT_BACKUP_PATH)
166    * @param moveOrigin true if moving the origin node, false if copying the origin node. Default value = true (DEFAULT_ORIGIN_NODE_ACTION)
167    * @param noTimeStamp not adding a time stamp folder to the backup path. Default value = false (DEFAULT_TIMESTAMP_ACTION)
168    */
 
169  0 toggle public BackupResourceTask(String name, String description, String resourcePath, String backupPath, boolean moveOrigin, boolean noTimeStamp) {
170  0 this(name, description, resourcePath, backupPath, moveOrigin, noTimeStamp, DEFAULT_TIMESTAMP_FORMAT);
171    }
172   
173    /**
174    * This constructor is private cause no need to define 'noTimeStamp=true' and a timeStampFormat at the same time.
175    *
176    * @param name task name
177    * @param description task description
178    * @param resourcePath the path of the resource to backup
179    * @param backupPath destination path of the backup within resources. Default value = /backup (DEFAULT_BACKUP_PATH)
180    * @param moveOrigin true if moving the origin node, false if copying the origin node. Default value = true (DEFAULT_ORIGIN_NODE_ACTION)
181    * @param noTimeStamp not adding a time stamp folder to the backup path. Default value = false (DEFAULT_TIMESTAMP_ACTION)
182    * @param timeStampFormat the format of the time stamp which is added to the backed up resource name. Default value = "'date'-yyyy-MM-dd-'time'-kk-mm-ss-SSSS" (DEFAULT_TIMESTAMP_FORMAT)
183    */
 
184  0 toggle private BackupResourceTask(String name, String description, String resourcePath, String backupPath, boolean moveOrigin, boolean noTimeStamp, String timeStampFormat) {
185  0 super(name, description);
186  0 this.resourcePath = StringUtils.removeEnd(resourcePath, "/");
187  0 this.backupPath = StringUtils.removeEnd(backupPath, "/");
188  0 this.moveOrigin = moveOrigin;
189  0 this.noTimeStamp = noTimeStamp;
190  0 this.timeStampFormat = timeStampFormat;
191    }
192   
 
193  0 toggle @Override
194    protected void doExecute(InstallContext installContext) throws TaskExecutionException, RepositoryException {
195  0 final HierarchyManager hm = installContext.getHierarchyManager(WORKSPACE);
196   
197  0 String fullBackupPath = backupPath + resourcePath;
198   
199    //add time stamp to resources's name in path
200  0 if(!noTimeStamp){
201  0 fullBackupPath = addTimestampFolder(backupPath) + resourcePath;
202    }
203   
204    //create full path for the final backup resource node
205  0 ContentUtil.createPath(hm, fullBackupPath, ItemType.FOLDER, false);
206   
207    //delete last node of the just created path for replacement with the real node
208  0 hm.delete(fullBackupPath);
209   
210    //either move or copy the node to backup
211  0 if(moveOrigin){
212  0 ContentUtil.moveInSession(hm.getContent(resourcePath), fullBackupPath);
213    } else{
214  0 ContentUtil.copyInSession(hm.getContent(resourcePath), fullBackupPath);
215    }
216    }
217   
218    /**
219    * @param backupPath string to add time stamp to. Time stamp format is dependent on defines private variable timeStampFormat.
220    * @return @param backupPath with added time stamp
221    */
 
222  0 toggle private String addTimestampFolder(String backupPath) {
223  0 String date = DateUtil.format(Calendar.getInstance().getTime(), timeStampFormat);
224  0 backupPath += "/"+ date;
225  0 return backupPath;
226    }
227    }