View Javadoc

1   /**
2    * This file Copyright (c) 2009-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.jcr.nodebuilder;
35  
36  import info.magnolia.jcr.util.NodeUtil;
37  import info.magnolia.jcr.util.PropertyUtil;
38  
39  import javax.jcr.ItemExistsException;
40  import javax.jcr.ItemNotFoundException;
41  import javax.jcr.Node;
42  import javax.jcr.RepositoryException;
43  import javax.jcr.Value;
44  
45  /**
46   * Factory methods for most common NodeOperation implementations.
47   *
48   * @version $Id$
49   */
50  public abstract class Ops {
51      public static NodeOperation addNode(final String name) {
52          return new AbstractNodeOperation() {
53              @Override
54              protected Node doExec(Node context, ErrorHandler errorHandler) throws RepositoryException {
55                  // TODO dlipp: Caution - addNode does not create/update Metadata (in contrast to Content#createContent! To be checked!
56                  return context.addNode(name);
57              }
58          };
59      }
60  
61      public static NodeOperation addNode(final String name, final String type) {
62          return new AbstractNodeOperation() {
63              @Override
64              protected Node doExec(Node context, ErrorHandler errorHandler) throws RepositoryException {
65                  return context.addNode(name, type);
66              }
67          };
68      }
69  
70      public static NodeOperation getNode(final String name) {
71          return new AbstractNodeOperation() {
72              @Override
73              protected Node doExec(Node context, ErrorHandler errorHandler) throws RepositoryException {
74                  return context.getNode(name);
75              }
76          };
77      }
78  
79      /**
80       * Adds property. Throws an ItemExistsException if the property already exists.
81       */
82      public static NodeOperation addProperty(final String name, final String newValue) {
83          return new AbstractNodeOperation() {
84              @Override
85              protected Node doExec(Node context, ErrorHandler errorHandler) throws RepositoryException {
86                  if (context.hasProperty(name)) {
87                      throw new ItemExistsException("Property " + name + " already exists at " + context.getPath());
88                  }
89                  final Value value = PropertyUtil.createValue(newValue, context.getSession().getValueFactory());
90                  context.setProperty(name, value);
91                  return context;
92              }
93          };
94      }
95  
96      /**
97       * Sets the value of an existing property, ignoring its current value.
98       * @throws ItemNotFoundException if the property does not exist.
99       */
100     public static NodeOperation setProperty(final String name, final Object newValue) {
101         return new AbstractNodeOperation() {
102             @Override
103             protected Node doExec(Node context, ErrorHandler errorHandler) throws RepositoryException {
104                 if (!context.hasProperty(name)) {
105                     throw new ItemNotFoundException(name);
106                 }
107                 final Value value = PropertyUtil.createValue(newValue, context.getSession().getValueFactory());
108                 context.setProperty(name, value);
109                 return context;
110             }
111         };
112     }
113 
114     /**
115      * Renames a node.
116      */
117     public static NodeOperation renameNode(final String currentName, final String newName) {
118         return new AbstractNodeOperation() {
119             @Override
120             protected Node doExec(Node context, ErrorHandler errorHandler) throws RepositoryException {
121                 NodeUtil.renameNode(context.getNode(currentName), newName);
122                 return context;
123             }
124         };
125     }
126 
127     /**
128      * Renames a property by creating a new one and copying the value.
129      */
130     public static NodeOperation renameProperty(final String name, final String newName) {
131         return new AbstractNodeOperation() {
132             @Override
133             protected Node doExec(Node context, ErrorHandler errorHandler) throws RepositoryException {
134                 if (!context.hasProperty(name)) {
135                     throw new ItemNotFoundException(name);
136                 }
137                 if (context.hasProperty(newName)) {
138                     //throw new ItemExistsException("Property " + newName + " already exists at " + context.getHandle());
139                     throw new ItemExistsException(newName);
140                 }
141                 final Value value = context.getProperty(name).getValue();
142                 context.setProperty(newName, value);
143                 context.getProperty(name).remove();
144                 return context;
145             }
146         };
147     }
148 
149     /**
150      * Moves a node, using session-scoped operation.
151      */
152     public static NodeOperation moveNode(final String nodeName, final String dest) {
153         return new AbstractNodeOperation() {
154             @Override
155             protected Node doExec(Node context, ErrorHandler errorHandler) throws RepositoryException {
156                 NodeUtil.moveNode(context.getNode(nodeName), context.getNode(dest));
157                 return context;
158             }
159         };
160     }
161 
162     /**
163      * No operation; can be useful in ternary expression, for instance.
164      */
165     public static NodeOperation noop() {
166         return new NodeOperation() {
167             @Override
168             public NodeOperation then(NodeOperation... childrenOps) {
169                 return null;
170             }
171 
172             @Override
173             public void exec(Node context, ErrorHandler errorHandler) {
174             }
175         };
176     }
177 }