View Javadoc

1   /**
2    * This file Copyright (c) 2003-2013 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.jcr.util.PropertyUtil;
37  import info.magnolia.jcr.util.SessionUtil;
38  import info.magnolia.module.InstallContext;
39  
40  import java.io.InputStream;
41  import java.math.BigDecimal;
42  import java.util.Calendar;
43  
44  import javax.jcr.Binary;
45  import javax.jcr.RepositoryException;
46  import javax.jcr.Node;
47  import javax.jcr.Session;
48  import javax.jcr.Value;
49  
50  /**
51   * Sets a new value for a property. Consider using CheckAndModifyPropertyValueTask if you want your update tasks
52   * to only modify expected values.
53   * @see CheckAndModifyPropertyValueTask
54   *
55   * @author fgiust
56   * @version $Revision: $ ($Author: $)
57   */
58  public class SetPropertyTask extends PropertyValuesTask {
59  
60      private final String workspaceName;
61  
62      private final String nodePath;
63  
64      private final String propertyName;
65  
66      private final Object newValue;
67  
68      public SetPropertyTask(String workspaceName, String nodePath, String propertyName, String newValue) {
69          this("Set " + nodePath + "/" + propertyName, workspaceName, nodePath, propertyName, newValue);
70      }
71  
72      public SetPropertyTask(String taskName, String workspaceName, String nodePath, String propertyName, String newValue) {
73          this(taskName, workspaceName, nodePath, propertyName, (Object) newValue);
74      }
75  
76      public SetPropertyTask(String taskName, String workspaceName, String nodePath, String propertyName, Value newValue) {
77          this(taskName, workspaceName, nodePath, propertyName, (Object) newValue);
78      }
79  
80      public SetPropertyTask(String taskName, String workspaceName, String nodePath, String propertyName, Node newValue) {
81          this(taskName, workspaceName, nodePath, propertyName, (Object) newValue);
82      }
83  
84      public SetPropertyTask(String taskName, String workspaceName, String nodePath, String propertyName, Binary newValue) {
85          this(taskName, workspaceName, nodePath, propertyName, (Object) newValue);
86      }
87  
88      public SetPropertyTask(String taskName, String workspaceName, String nodePath, String propertyName, Calendar newValue) {
89          this(taskName, workspaceName, nodePath, propertyName, (Object) newValue);
90      }
91  
92      public SetPropertyTask(String taskName, String workspaceName, String nodePath, String propertyName, BigDecimal newValue) {
93          this(taskName, workspaceName, nodePath, propertyName, (Object) newValue);
94      }
95  
96      public SetPropertyTask(String taskName, String workspaceName, String nodePath, String propertyName, Long newValue) {
97          this(taskName, workspaceName, nodePath, propertyName, (Object) newValue);
98      }
99  
100     public SetPropertyTask(String taskName, String workspaceName, String nodePath, String propertyName, Double newValue) {
101         this(taskName, workspaceName, nodePath, propertyName, (Object) newValue);
102     }
103 
104     public SetPropertyTask(String taskName, String workspaceName, String nodePath, String propertyName, Boolean newValue) {
105         this(taskName, workspaceName, nodePath, propertyName, (Object) newValue);
106     }
107 
108     public SetPropertyTask(String taskName, String workspaceName, String nodePath, String propertyName, InputStream newValue) {
109         this(taskName, workspaceName, nodePath, propertyName, (Object) newValue);
110     }
111 
112     private SetPropertyTask(String taskName, String workspaceName, String nodePath, String propertyName, Object newValue) {
113         super(taskName, "Sets " + nodePath + "/" + propertyName + " to " + newValue + ".");
114         this.workspaceName = workspaceName;
115         this.nodePath = nodePath;
116         this.propertyName = propertyName;
117         this.newValue = newValue;
118     }
119 
120     /**
121      * {@inheritDoc}
122      */
123     @Override
124     public void execute(InstallContext installContext) {
125         try {
126             final Session session = installContext.getJCRSession(workspaceName);
127             final Node node = SessionUtil.getNode(session, nodePath);
128 
129             PropertyUtil.setProperty(node, propertyName, this.newValue);
130         } catch (RepositoryException e) {
131             final String msg = format("Unable to set property \"{0}\" to value \"{1}\" on node \"{2}\".", propertyName, newValue, nodePath);
132             installContext.error(msg, e);
133         }
134     }
135 }