View Javadoc
1   /**
2    * This file Copyright (c) 2003-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.Content;
37  import info.magnolia.jcr.util.PropertyUtil;
38  import info.magnolia.module.InstallContext;
39  
40  import java.text.MessageFormat;
41  import java.util.Arrays;
42  import java.util.Collection;
43  
44  import javax.jcr.Node;
45  import javax.jcr.Property;
46  import javax.jcr.RepositoryException;
47  
48  import org.apache.commons.lang3.StringUtils;
49  
50  /**
51   * A tasks that offers helper methods to check on certain properties.
52   */
53  public abstract class PropertyValuesTask extends AbstractTask {
54  
55      public PropertyValuesTask(String name, String description) {
56          super(name, description);
57      }
58  
59      /**
60       * Checks that the given String property has the expected value. Changes it if so, logs otherwise.
61       *
62       * @deprecated since 5.2.2, use {@link #checkAndModifyPropertyValue(InstallContext, Node, String, Collection, String) instead}.
63       */
64      protected void checkAndModifyPropertyValue(InstallContext ctx, Content node, String propertyName, String expectedCurrentValue, String newValue) throws RepositoryException {
65          checkAndModifyPropertyValue(ctx, node.getJCRNode(), propertyName, Arrays.asList(expectedCurrentValue), newValue);
66      }
67  
68      /**
69       * Checks that the given String property has the expected value. Changes it if so, logs otherwise.
70       */
71      protected void checkAndModifyPropertyValue(InstallContext ctx, Node node, String propertyName, String expectedCurrentValue, String newValue) throws RepositoryException {
72          checkAndModifyPropertyValue(ctx, node, propertyName, Arrays.asList(expectedCurrentValue), newValue);
73      }
74  
75      /**
76       * Checks that the given String property has one of the expected values. Changes it if so, logs otherwise.
77       */
78      protected void checkAndModifyPropertyValue(InstallContext ctx, Node node, String propertyName, Collection<String> expectedCurrentValues, String newValue) throws RepositoryException {
79          if (node.hasProperty(propertyName)) {
80              final Property prop = node.getProperty(propertyName);
81              final String currentvalue = prop.getString();
82              if (expectedCurrentValues.contains(currentvalue)) {
83                  prop.setValue(newValue);
84              } else {
85                  String msg;
86                  if (expectedCurrentValues.size() == 1) {
87                      msg = format("Property \"{0}\" was expected to exist at {1} with value \"{2}\" but has the value \"{3}\" instead.",
88                              propertyName, node.getPath(), expectedCurrentValues.iterator().next(), currentvalue);
89                  } else {
90                      msg = format("Property \"{0}\" was expected to exist at {1} with one of values {2} but has the value \"{3}\" instead.",
91                              propertyName, node.getPath(), expectedCurrentValues.toString(), currentvalue);
92                  }
93                  ctx.warn(msg);
94              }
95          } else {
96              String msg;
97              if (expectedCurrentValues.size() == 1) {
98                  msg = format("Property \"{0}\" was expected to exist at {1} with value \"{2}\" but does not exist.",
99                          propertyName, node.getPath(), expectedCurrentValues.iterator().next());
100             } else {
101                 msg = format("Property \"{0}\" was expected to exist at {1} with one of values {2} but does not exist.",
102                         propertyName, node.getPath(), expectedCurrentValues.toString());
103             }
104             ctx.warn(msg);
105         }
106     }
107 
108     /**
109      * Checks if property contains concrete string. If contains then change this part of string, logs otherwise.
110      */
111     protected void checkAndModifyPartOfPropertyValue(InstallContext ctx, Node node, String propertyName, String expectedValue, String newValue) throws RepositoryException {
112         if (node.hasProperty(propertyName)) {
113             final Property prop = node.getProperty(propertyName);
114             final String currentvalue = prop.getString();
115             if (currentvalue.contains(expectedValue)) {
116                 prop.setValue(StringUtils.replace(currentvalue, expectedValue, newValue));
117             } else {
118                 final String msg = format("Property \"{0}\" was expected to exist at {1} with part string \"{2}\" but does not contain this string.",
119                         propertyName, node.getPath(), expectedValue);
120                 ctx.warn(msg);
121             }
122         } else {
123             final String msg = format("Property \"{0}\" was expected to exist at {1} with part string \"{2}\" but does not exist.",
124                     propertyName, node.getPath(), expectedValue);
125             ctx.warn(msg);
126         }
127     }
128 
129     protected void checkOrCreateProperty(InstallContext ctx, Node node, String propertyName, String expectedValue) throws RepositoryException {
130 
131         if (node.hasProperty(propertyName)) {
132             final Property prop = node.getProperty(propertyName);
133             final String currentvalue = prop.getString();
134 
135             if (!currentvalue.equals(expectedValue)) {
136                 final String msg = format("Property \"{0}\" was expected to exist at {1} with value \"{2}\" but {3,choice,0#does not exist|1#has the value \"{4}\" instead}.",
137                         propertyName, node.getPath(), expectedValue, 1, currentvalue);
138                 ctx.warn(msg);
139             }
140         } else {
141             node.setProperty(propertyName, expectedValue);
142         }
143     }
144 
145     /**
146      * @deprecated since 5.2.2, use {@link #checkOrCreateProperty(InstallContext, Node, String, String)} instead.
147      */
148     protected void checkOrCreateProperty(InstallContext ctx, Content node, String propertyName, String expectedValue) throws RepositoryException {
149         this.checkOrCreateProperty(ctx, node.getJCRNode(), propertyName, expectedValue);
150     }
151 
152     /**
153      * Checks that the given property does not exist and creates it with the given value, logs otherwise.
154      */
155     @Deprecated
156     protected void newProperty(InstallContext ctx, Content node, String propertyName, String value) throws RepositoryException {
157         newProperty(ctx, node.getJCRNode(), propertyName, value);
158     }
159 
160     /**
161      * Checks that the given property does not exist and creates it with the given value, logs otherwise.
162      */
163     protected void newProperty(InstallContext ctx, Node node, String propertyName, Object value) throws RepositoryException {
164         if (!node.hasProperty(propertyName)) {
165             PropertyUtil.setProperty(node, propertyName, value);
166         } else {
167             final String msg = format("Property \"{0}\" was expected not to exist at {1}, but exists with value \"{2}\" and was going to be created with value \"{3}\".",
168                     propertyName, node.getPath(), node.getProperty(propertyName).getValue().getString(), value);
169             ctx.warn(msg);
170         }
171     }
172 
173     // TODO move this to the InstallContext interface ?
174     protected String format(String pattern, Object arg0, Object arg1, Object arg2, Object arg3, Object arg4) {
175         return MessageFormat.format(pattern, arg0, arg1, arg2, arg3, arg4);
176     }
177 
178     protected String format(String pattern, Object arg0, Object arg1, Object arg2, Object arg3) {
179         return format(pattern, arg0, arg1, arg2, arg3, null);
180     }
181 
182     protected String format(String pattern, Object arg0, Object arg1, Object arg2) {
183         return format(pattern, arg0, arg1, arg2, null);
184     }
185 }