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    * Unless otherwise indicated, all code in ModeShape is licensed
10   * 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.request;
25  
26  import org.modeshape.common.util.CheckArg;
27  import org.modeshape.graph.Location;
28  import org.modeshape.graph.property.Path;
29  
30  /**
31   * Request that a new workspace be created with the supplied name. The request also specifies the desired
32   * {@link #conflictBehavior() behavior} for the recipient if a workspace already exists with the name.
33   */
34  public final class CreateWorkspaceRequest extends ChangeRequest {
35  
36      private static final long serialVersionUID = 1L;
37  
38      /**
39       * The options for the behavior when a request specifies a workspace name that already is used by an existing workspace.
40       */
41      public static enum CreateConflictBehavior {
42          /** Do not create the workspace, and record as an {@link Request#setError(Throwable) error} on the request. */
43          DO_NOT_CREATE,
44  
45          /** Create the workspace by adjusting the name so that it is unique. */
46          CREATE_WITH_ADJUSTED_NAME
47      }
48  
49      /**
50       * The default {@link CreateConflictBehavior} that will be used if it is unspecified.
51       */
52      public static final CreateConflictBehavior DEFAULT_CREATE_CONFLICT_BEHAVIOR = CreateConflictBehavior.DO_NOT_CREATE;
53  
54      private final String desiredNameOfNewWorkspace;
55      private final CreateConflictBehavior createConflictBehavior;
56      private String actualWorkspaceName;
57      private Location actualLocationOfRoot;
58  
59      /**
60       * Create a request to create a new workspace, and specify the behavior should a workspace already exists with a name that
61       * matches the desired name for the new workspace.
62       * 
63       * @param desiredNameOfNewWorkspace the desired name of the new workspace
64       * @param createConflictBehavior the behavior if a workspace already exists with the same name
65       */
66      public CreateWorkspaceRequest( String desiredNameOfNewWorkspace,
67                                     CreateConflictBehavior createConflictBehavior ) {
68          CheckArg.isNotNull(desiredNameOfNewWorkspace, "desiredNameOfNewWorkspace");
69          this.desiredNameOfNewWorkspace = desiredNameOfNewWorkspace;
70          this.createConflictBehavior = createConflictBehavior != null ? createConflictBehavior : DEFAULT_CREATE_CONFLICT_BEHAVIOR;
71      }
72  
73      /**
74       * Get the desired name for the new workspace.
75       * 
76       * @return the desired name for the new workspace; never null
77       */
78      public String desiredNameOfNewWorkspace() {
79          return desiredNameOfNewWorkspace;
80      }
81  
82      /**
83       * Get the desired behavior if a workspace already exists with the {@link #desiredNameOfNewWorkspace() desired workspace name}
84       * .
85       * 
86       * @return the desired behavior; never null
87       */
88      public CreateConflictBehavior conflictBehavior() {
89          return createConflictBehavior;
90      }
91  
92      /**
93       * Get the actual name of the workspace that was created. This will be the same as the {@link #desiredNameOfNewWorkspace()
94       * desired name} unless there was a conflict and the {@link #conflictBehavior() desired behavior} was to
95       * {@link CreateConflictBehavior#CREATE_WITH_ADJUSTED_NAME alter the name}.
96       * 
97       * @return the actual name of the workspace that was created, or null if a workspace was not created (yet)
98       */
99      public String getActualWorkspaceName() {
100         return actualWorkspaceName;
101     }
102 
103     /**
104      * Set the actual name of the workspace that was created. This should be the same as the {@link #desiredNameOfNewWorkspace()
105      * desired name} unless there was a conflict and the {@link #conflictBehavior() desired behavior} was to
106      * {@link CreateConflictBehavior#CREATE_WITH_ADJUSTED_NAME alter the name}.
107      * 
108      * @param actualWorkspaceName the actual name of the workspace that was created, or null if a workspace was not created
109      * @throws IllegalStateException if the request is frozen
110      */
111     public void setActualWorkspaceName( String actualWorkspaceName ) {
112         checkNotFrozen();
113         this.actualWorkspaceName = actualWorkspaceName;
114     }
115 
116     /**
117      * Get the actual location of the root node in the new workspace, or null if the workspace was not (yet) created.
118      * 
119      * @return the actual location of the root node in the new workspace, or null if the workspace was not (yet) created
120      */
121     public Location getActualLocationOfRoot() {
122         return actualLocationOfRoot;
123     }
124 
125     /**
126      * Set the actual location of the root node in the new workspace.
127      * 
128      * @param actualLocationOfRoot the actual location of the workspace's root node.
129      * @throws IllegalStateException if the request is frozen
130      */
131     public void setActualRootLocation( Location actualLocationOfRoot ) {
132         checkNotFrozen();
133         this.actualLocationOfRoot = actualLocationOfRoot;
134     }
135 
136     /**
137      * {@inheritDoc}
138      * 
139      * @see org.modeshape.graph.request.Request#isReadOnly()
140      */
141     @Override
142     public boolean isReadOnly() {
143         return false;
144     }
145 
146     /**
147      * {@inheritDoc}
148      * 
149      * @see org.modeshape.graph.request.Request#cancel()
150      */
151     @Override
152     public void cancel() {
153         super.cancel();
154         this.actualWorkspaceName = null;
155         this.actualLocationOfRoot = null;
156     }
157 
158     /**
159      * {@inheritDoc}
160      * 
161      * @see java.lang.Object#hashCode()
162      */
163     @Override
164     public int hashCode() {
165         return desiredNameOfNewWorkspace.hashCode();
166     }
167 
168     /**
169      * {@inheritDoc}
170      * 
171      * @see java.lang.Object#equals(java.lang.Object)
172      */
173     @Override
174     public boolean equals( Object obj ) {
175         if (obj == this) return true;
176         if (this.getClass().isInstance(obj)) {
177             CreateWorkspaceRequest that = (CreateWorkspaceRequest)obj;
178             if (!this.desiredNameOfNewWorkspace.equals(that.desiredNameOfNewWorkspace())) return false;
179             return true;
180         }
181         return false;
182     }
183 
184     /**
185      * {@inheritDoc}
186      * 
187      * @see java.lang.Object#toString()
188      */
189     @Override
190     public String toString() {
191         return "create new workspace \"" + desiredNameOfNewWorkspace() + "\"";
192     }
193 
194     /**
195      * {@inheritDoc}
196      * 
197      * @see org.modeshape.graph.request.ChangeRequest#changedLocation()
198      */
199     @Override
200     public Location changedLocation() {
201         return actualLocationOfRoot;
202     }
203 
204     /**
205      * {@inheritDoc}
206      * 
207      * @see org.modeshape.graph.request.ChangeRequest#changedWorkspace()
208      */
209     @Override
210     public String changedWorkspace() {
211         return actualWorkspaceName;
212     }
213 
214     /**
215      * {@inheritDoc}
216      * 
217      * @see org.modeshape.graph.request.ChangeRequest#changes(java.lang.String, org.modeshape.graph.property.Path)
218      */
219     @Override
220     public boolean changes( String workspace,
221                             Path path ) {
222         return actualWorkspaceName != null && actualWorkspaceName.equals(workspace);
223     }
224 
225     /**
226      * {@inheritDoc}
227      * <p>
228      * This method does not clone the results.
229      * </p>
230      * 
231      * @see org.modeshape.graph.request.ChangeRequest#clone()
232      */
233     @Override
234     public CreateWorkspaceRequest clone() {
235         CreateWorkspaceRequest request = new CreateWorkspaceRequest(
236                                                                     actualWorkspaceName != null ? actualWorkspaceName : desiredNameOfNewWorkspace,
237                                                                     createConflictBehavior);
238         request.setActualWorkspaceName(actualWorkspaceName);
239         request.setActualRootLocation(actualLocationOfRoot);
240         return request;
241     }
242 
243     @Override
244     public RequestType getType() {
245         return RequestType.CREATE_WORKSPACE;
246     }
247 }