View Javadoc

1   /**
2    * This file Copyright (c) 2003-2011 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.cms.core.NodeData;
38  import info.magnolia.module.InstallContext;
39  
40  import java.text.MessageFormat;
41  
42  import javax.jcr.Node;
43  import javax.jcr.Property;
44  import javax.jcr.RepositoryException;
45  
46  import org.apache.commons.lang.StringUtils;
47  
48  
49  /**
50   * A tasks that offers helper methods to check on certain properties.
51   *
52   * @author gjoseph
53   * @version $Revision: $ ($Author: $)
54   */
55  public abstract class PropertyValuesTask extends AbstractTask {
56  
57      public PropertyValuesTask(String name, String description) {
58          super(name, description);
59      }
60  
61      /**
62       * Checks that the given String property has the expected value. Changes it if so, logs otherwise.
63       */
64      protected void checkAndModifyPropertyValue(InstallContext ctx, Content node, String propertyName, String expectedCurrentValue, String newValue) throws RepositoryException {
65          final NodeData prop = node.getNodeData(propertyName);
66          final String currentvalue = prop.getString();
67          if (prop.isExist() && currentvalue.equals(expectedCurrentValue)) {
68              prop.setValue(newValue);
69          } else {
70              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}.",
71                              propertyName, node.getHandle(), expectedCurrentValue,
72                              Integer.valueOf(prop.isExist() ? 1 : 0), currentvalue);
73              ctx.warn(msg);
74          }
75      }
76  
77      /**
78       * Checks if property contains concrete string. If contains then change this part of string, logs otherwise.
79       */
80      protected void checkAndModifyPartOfPropertyValue(InstallContext ctx, Node node, String propertyName, String expectedValue, String newValue) throws RepositoryException {
81          if(node.hasProperty(propertyName)){
82              final Property prop = node.getProperty(propertyName);
83              final String currentvalue = prop.getString();
84              if(currentvalue.contains(expectedValue)) { 
85                  prop.setValue(StringUtils.replace(currentvalue, expectedValue, newValue));
86              } else {
87                  final String msg = format("Property \"{0}\" was expected to exist at {1} with part string \"{2}\" but does not contain this string.",
88                              propertyName, node.getPath(), expectedValue);
89                  ctx.warn(msg);
90              }
91          } else {
92              final String msg = format("Property \"{0}\" was expected to exist at {1} with part string \"{2}\" but does not exist.",
93                      propertyName, node.getPath(), expectedValue);
94              ctx.warn(msg);
95          }
96      }
97  
98      protected void checkOrCreateProperty(InstallContext ctx, Content node, String propertyName, String expectedValue) throws RepositoryException {
99          if (node.hasNodeData(propertyName)) {
100             final NodeData prop = node.getNodeData(propertyName);
101             final String currentvalue = prop.getString();
102             if (!currentvalue.equals(expectedValue)) {
103                 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}.",
104                                 propertyName, node.getHandle(), expectedValue, Integer.valueOf(prop.isExist() ? 1 : 0),
105                                 currentvalue);
106                 ctx.warn(msg);
107             }
108         } else {
109             node.createNodeData(propertyName, expectedValue);
110         }
111     }
112 
113     /**
114      * Checks that the given property does not exist and creates it with the given value, logs otherwise.
115      */
116     protected void newProperty(InstallContext ctx, Content node, String propertyName, String value) throws RepositoryException {
117         newProperty(ctx, node, propertyName, value, true);
118     }
119 
120     protected void newProperty(InstallContext ctx, Content node, String propertyName, String value, boolean log) throws RepositoryException {
121         final NodeData prop = node.getNodeData(propertyName);
122         if (!prop.isExist()) {
123             node.createNodeData(propertyName, value);
124         } else if (log) {
125             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}\".",
126                     propertyName, node.getHandle(), prop.getString(), value);
127             ctx.warn(msg);
128         }
129     }
130 
131     // TODO move this to the InstallContext interface ?
132     protected String format(String pattern, Object arg0, Object arg1, Object arg2, Object arg3, Object arg4) {
133         return MessageFormat.format(pattern, new Object[]{arg0, arg1, arg2, arg3, arg4});
134     }
135 
136     protected String format(String pattern, Object arg0, Object arg1, Object arg2, Object arg3) {
137         return format(pattern, arg0, arg1, arg2, arg3, null);
138     }
139 
140     protected String format(String pattern, Object arg0, Object arg1, Object arg2) {
141         return format(pattern, arg0, arg1, arg2, null);
142     }
143 }