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 AbstractInstallResourceTask.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

14
41
6
1
147
91
14
0.34
6.83
6
2.33

Classes

Class Line # Actions
AbstractInstallResourceTask 56 41 0% 14 4
0.9344262593.4%
 

Contributing tests

This file is covered by 16 tests. .

Source view

1    /**
2    * This file Copyright (c) 2013-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.Path;
37    import info.magnolia.cms.util.ClasspathResourcesUtil;
38    import info.magnolia.jcr.util.NodeTypes;
39    import info.magnolia.jcr.util.NodeTypes.Renderable;
40    import info.magnolia.jcr.util.NodeUtil;
41    import info.magnolia.module.InstallContext;
42    import info.magnolia.module.delta.AbstractTask;
43    import info.magnolia.module.delta.TaskExecutionException;
44    import info.magnolia.module.resources.ResourceTypes;
45    import info.magnolia.module.resources.ResourcesModule;
46   
47    import javax.jcr.Node;
48    import javax.jcr.RepositoryException;
49    import javax.jcr.Session;
50   
51    import org.apache.commons.lang3.StringUtils;
52   
53    /**
54    * Task to load single resource to the repository.
55    */
 
56    public class AbstractInstallResourceTask extends AbstractTask {
57   
58    protected final String resource;
59    protected String template;
60    protected String version;
61    protected String source;
62    protected String rights;
63    protected Node node;
64    protected String name;
65    protected String extension;
66   
67    protected boolean preserveResourceParameters = false;
68    protected boolean processed = true;
69    private boolean stripExtension = false;
70   
 
71  0 toggle public AbstractInstallResourceTask(String resource, String version, String source, String rights, boolean preserveResourceParameters, boolean processed) {
72  0 this(resource, null, processed, version, source, rights, preserveResourceParameters);
73    }
74   
 
75  3 toggle public AbstractInstallResourceTask(String resource, String template, boolean processed, String version, String source, String rights, boolean preserveResourceParameters) {
76  3 this(resource, template, processed, version, source, rights, preserveResourceParameters, true);
77    }
78   
 
79  19 toggle public AbstractInstallResourceTask(String resource, String template, boolean processed, String version, String source, String rights, boolean preserveResourceParameters, boolean stripExtension) {
80  19 super("Install resource", "Copies the " + resource + " file to the resources workspace.");
81  19 this.resource = resource;
82  19 this.template = template;
83  19 this.processed = processed;
84  19 this.version = StringUtils.defaultString(version);
85  19 this.source = StringUtils.defaultString(source);
86  19 this.rights = StringUtils.defaultString(rights);
87  19 this.preserveResourceParameters = preserveResourceParameters;
88  19 this.stripExtension = stripExtension;
89    }
90   
 
91  19 toggle @Override
92    public void execute(InstallContext installContext) throws TaskExecutionException {
93   
94  19 if (ClasspathResourcesUtil.getResource(resource) == null) {
95  2 throw new TaskExecutionException("Can't find resource [" + resource + "] to install.");
96    }
97   
98  17 try {
99  17 name = StringUtils.substringAfterLast(resource, "/");
100  17 if (stripExtension) {
101  12 name = StringUtils.substringBeforeLast(name, ".");
102  12 extension = StringUtils.substringAfterLast(resource, ".");
103    }
104  17 String path = StringUtils.substringBeforeLast(resource, "/");
105   
106  17 Session session = installContext.getJCRSession(ResourcesModule.DEFAULT_WORKSPACE);
107  17 Node parent = NodeUtil.createPath(session.getRootNode(), path, NodeTypes.Folder.NAME);
108  17 node = NodeUtil.createPath(parent, Path.getValidatedLabel(name), NodeTypes.Content.NAME);
109   
110  17 if (this.preserveResourceParameters) {
111  1 this.version = this.getProperty("version");
112  1 this.source = this.getProperty("source");
113  1 this.rights = this.getProperty("rights");
114  1 this.template = Renderable.getTemplate(node);
115    }
116   
117  17 String template = StringUtils.isNotEmpty(this.template) ? this.template : ResourceTypes.determineType(this.resource, this.processed);
118  17 Renderable.set(node, template);
119    // set meta information
120  17 if (StringUtils.isNotBlank(extension)) {
121  12 this.setProperty("extension", extension);
122    }
123  17 this.setProperty("version", version);
124  17 this.setProperty("source", source);
125  17 this.setProperty("rights", rights);
126   
127    } catch (Exception e) {
128  0 throw new TaskExecutionException("Can't install resource [" + resource + "]", e);
129    }
130    }
131   
 
132  77 toggle protected void setProperty(String name, String value) throws RepositoryException {
133  77 if (node.hasProperty(name)) {
134  3 node.getProperty(name).setValue(value);
135    } else {
136  74 node.setProperty(name, value);
137    }
138    }
139   
 
140  4 toggle protected String getProperty(String name) throws RepositoryException {
141  4 if (node.hasProperty(name)) {
142  1 return node.getProperty(name).getString();
143    }
144  3 return null;
145    }
146   
147    }