View Javadoc

1   /*
2    * ModeShape (http://www.modeshape.org)
3    * See the COPYRIGHT.txt file distributed with this work for information
4    * regarding copyright ownership.  Some portions may be licensed
5    * to Red Hat, Inc. under one or more contributor license agreements.
6   * See the AUTHORS.txt file in the distribution for a full listing of 
7   * individual contributors.
8    *
9    * ModeShape is free software. Unless otherwise indicated, all code in ModeShape
10   * is licensed to you under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation; either version 2.1 of
12   * the License, or (at your option) any later version.
13   *
14   * ModeShape is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17   * Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this software; if not, write to the Free
21   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23   */
24  package org.modeshape.graph.connector.path;
25  
26  import java.util.HashMap;
27  import java.util.Map;
28  import java.util.UUID;
29  import org.modeshape.graph.ExecutionContext;
30  import org.modeshape.graph.NodeConflictBehavior;
31  import org.modeshape.graph.connector.LockFailedException;
32  import org.modeshape.graph.property.Name;
33  import org.modeshape.graph.property.Path;
34  import org.modeshape.graph.property.PathFactory;
35  import org.modeshape.graph.property.Property;
36  import org.modeshape.graph.property.Path.Segment;
37  import org.modeshape.graph.query.QueryResults;
38  import org.modeshape.graph.request.AccessQueryRequest;
39  import org.modeshape.graph.request.LockBranchRequest.LockScope;
40  
41  /**
42   * Implementation of some methods from {@link WritablePathWorkspace} to assist in the development of path-based connectors.
43   * Subclasses of this class should be made thread-safe.
44   */
45  public abstract class AbstractWritablePathWorkspace implements WritablePathWorkspace {
46  
47      private final String name;
48      protected final UUID rootNodeUuid;
49  
50      public AbstractWritablePathWorkspace( String name,
51                                            UUID rootNodeUuid ) {
52          super();
53          this.name = name;
54          this.rootNodeUuid = rootNodeUuid;
55      }
56  
57      /**
58       * This should copy the subgraph rooted at the original node and place the new copy under the supplied new parent. Note that
59       * internal references between nodes within the original subgraph must be reflected as internal nodes within the new subgraph.
60       * 
61       * @param context the context; may not be null
62       * @param original the node to be copied; may not be null
63       * @param originalWorkspace the workspace containing the original node; may not be null
64       * @param newParent the parent where the copy is to be placed; may not be null
65       * @param desiredName the desired name for the node; if null, the name will be obtained from the original node
66       * @param recursive true if the copy should be recursive
67       * @return the new node, which is the top of the new subgraph
68       */
69      public PathNode copyNode( ExecutionContext context,
70                                PathNode original,
71                                PathWorkspace originalWorkspace,
72                                PathNode newParent,
73                                Name desiredName,
74                                boolean recursive ) {
75          PathFactory pathFactory = context.getValueFactories().getPathFactory();
76          PathNode copy = createNode(context, newParent, desiredName, original.getProperties(), NodeConflictBehavior.REPLACE);
77  
78          if (recursive) {
79              Path originalPath = original.getPath();
80  
81              for (Segment childSegment : original.getChildSegments()) {
82                  Path childPath = pathFactory.create(originalPath, childSegment);
83                  PathNode childNode = originalWorkspace.getNode(childPath);
84                  copyNode(context, childNode, originalWorkspace, copy, childSegment.getName(), true);
85              }
86          }
87          return copy;
88      }
89  
90      public PathNode createNode( ExecutionContext context,
91                                  String pathToNewNode,
92                                  Map<Name, Property> properties,
93                                  NodeConflictBehavior conflictBehavior ) {
94          PathFactory pathFactory = context.getValueFactories().getPathFactory();
95          Path newPath = pathFactory.create(pathToNewNode);
96  
97          return createNode(context, getNode(newPath.getParent()), newPath.getLastSegment().getName(), properties, conflictBehavior);
98      }
99  
100     public PathNode moveNode( ExecutionContext context,
101                               PathNode node,
102                               Name desiredNewName,
103                               WritablePathWorkspace originalWorkspace,
104                               PathNode newParent,
105                               PathNode beforeNode ) {
106         if (desiredNewName == null) {
107             assert !node.getPath().isRoot();
108             desiredNewName = node.getPath().getLastSegment().getName();
109         }
110 
111         PathNode newCopy = copyNode(context, node, originalWorkspace, newParent, desiredNewName, true);
112         originalWorkspace.removeNode(context, node.getPath());
113         return newCopy;
114     }
115 
116     public PathNode removeProperties( ExecutionContext context,
117                                       Path nodePath,
118                                       Iterable<Name> propertyNames ) {
119         Map<Name, Property> properties = new HashMap<Name, Property>();
120         for (Name propertyName : propertyNames) {
121             properties.put(propertyName, null);
122         }
123 
124         return setProperties(context, nodePath, properties);
125     }
126 
127     public QueryResults query( ExecutionContext context,
128                                AccessQueryRequest accessQuery ) {
129         return null;
130     }
131 
132     public QueryResults search( ExecutionContext context,
133                                 String fullTextSearchExpression ) {
134         return null;
135     }
136 
137     public String getName() {
138         return this.name;
139     }
140 
141     public void lockNode( PathNode node,
142                           LockScope lockScope,
143                           long lockTimeoutInMillis ) throws LockFailedException {
144     }
145 
146     public void unlockNode( PathNode node ) {
147     }
148 
149 }