001    /*
002     * JBoss DNA (http://www.jboss.org/dna)
003     * See the COPYRIGHT.txt file distributed with this work for information
004     * regarding copyright ownership.  Some portions may be licensed
005     * to Red Hat, Inc. under one or more contributor license agreements.
006     * See the AUTHORS.txt file in the distribution for a full listing of 
007     * individual contributors.
008     *
009     * Unless otherwise indicated, all code in JBoss DNA is licensed
010     * to you under the terms of the GNU Lesser General Public License as
011     * published by the Free Software Foundation; either version 2.1 of
012     * the License, or (at your option) any later version.
013     * 
014     * JBoss DNA is distributed in the hope that it will be useful,
015     * but WITHOUT ANY WARRANTY; without even the implied warranty of
016     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017     * Lesser General Public License for more details.
018     *
019     * You should have received a copy of the GNU Lesser General Public
020     * License along with this software; if not, write to the Free
021     * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
022     * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
023     */
024    package org.jboss.dna.graph.request;
025    
026    import org.jboss.dna.common.util.CheckArg;
027    import org.jboss.dna.graph.Location;
028    import org.jboss.dna.graph.property.Path;
029    
030    /**
031     * Request that a new workspace be created with the supplied name. The request also specifies the desired
032     * {@link #conflictBehavior() behavior} for the recipient if a workspace already exists with the name.
033     */
034    public final class CreateWorkspaceRequest extends ChangeRequest {
035    
036        private static final long serialVersionUID = 1L;
037    
038        /**
039         * The options for the behavior when a request specifies a workspace name that already is used by an existing workspace.
040         */
041        public static enum CreateConflictBehavior {
042            /** Do not create the workspace, and record as an {@link Request#setError(Throwable) error} on the request. */
043            DO_NOT_CREATE,
044    
045            /** Create the workspace by adjusting the name so that it is unique. */
046            CREATE_WITH_ADJUSTED_NAME
047        }
048    
049        /**
050         * The default {@link CreateConflictBehavior} that will be used if it is unspecified.
051         */
052        public static final CreateConflictBehavior DEFAULT_CREATE_CONFLICT_BEHAVIOR = CreateConflictBehavior.DO_NOT_CREATE;
053    
054        private final String desiredNameOfNewWorkspace;
055        private final CreateConflictBehavior createConflictBehavior;
056        private String actualWorkspaceName;
057        private Location actualLocationOfRoot;
058    
059        /**
060         * Create a request to create a new workspace, and specify the behavior should a workspace already exists with a name that
061         * matches the desired name for the new workspace.
062         * 
063         * @param desiredNameOfNewWorkspace the desired name of the new workspace
064         * @param createConflictBehavior the behavior if a workspace already exists with the same name
065         */
066        public CreateWorkspaceRequest( String desiredNameOfNewWorkspace,
067                                       CreateConflictBehavior createConflictBehavior ) {
068            CheckArg.isNotNull(desiredNameOfNewWorkspace, "desiredNameOfNewWorkspace");
069            this.desiredNameOfNewWorkspace = desiredNameOfNewWorkspace;
070            this.createConflictBehavior = createConflictBehavior != null ? createConflictBehavior : DEFAULT_CREATE_CONFLICT_BEHAVIOR;
071        }
072    
073        /**
074         * Get the desired name for the new workspace.
075         * 
076         * @return the desired name for the new workspace; never null
077         */
078        public String desiredNameOfNewWorkspace() {
079            return desiredNameOfNewWorkspace;
080        }
081    
082        /**
083         * Get the desired behavior if a workspace already exists with the {@link #desiredNameOfNewWorkspace() desired workspace name}
084         * .
085         * 
086         * @return the desired behavior; never null
087         */
088        public CreateConflictBehavior conflictBehavior() {
089            return createConflictBehavior;
090        }
091    
092        /**
093         * Get the actual name of the workspace that was created. This will be the same as the {@link #desiredNameOfNewWorkspace()
094         * desired name} unless there was a conflict and the {@link #conflictBehavior() desired behavior} was to
095         * {@link CreateConflictBehavior#CREATE_WITH_ADJUSTED_NAME alter the name}.
096         * 
097         * @return the actual name of the workspace that was created, or null if a workspace was not created (yet)
098         */
099        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.jboss.dna.graph.request.Request#isReadOnly()
140         */
141        @Override
142        public boolean isReadOnly() {
143            return false;
144        }
145    
146        /**
147         * {@inheritDoc}
148         * 
149         * @see org.jboss.dna.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.jboss.dna.graph.request.ChangeRequest#changedLocation()
198         */
199        @Override
200        public Location changedLocation() {
201            return actualLocationOfRoot;
202        }
203    
204        /**
205         * {@inheritDoc}
206         * 
207         * @see org.jboss.dna.graph.request.ChangeRequest#changedWorkspace()
208         */
209        @Override
210        public String changedWorkspace() {
211            return actualWorkspaceName;
212        }
213    
214        /**
215         * {@inheritDoc}
216         * 
217         * @see org.jboss.dna.graph.request.ChangeRequest#changes(java.lang.String, org.jboss.dna.graph.property.Path)
218         */
219        @Override
220        public boolean changes( String workspace,
221                                Path path ) {
222            return actualWorkspaceName != null && actualWorkspaceName.equals(workspace);
223        }
224    }