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.request.CreateWorkspaceRequest.CreateConflictBehavior;
029    
030    /**
031     * Request that an existing workspace be cloned into a target workspace with the supplied name. If the target workspace exists,
032     * the {@link #targetConflictBehavior() target conflict behavior} defines the behavior to be followed. If the workspace being
033     * cloned does not exist, the {@link #cloneConflictBehavior() clone conflict behavior} defines the behavior to be followed.
034     */
035    public final class CloneWorkspaceRequest extends Request {
036    
037        private static final long serialVersionUID = 1L;
038    
039        /**
040         * The options for the behavior when a request specifies the name of the workspace to clone, but the cloned workspace does not
041         * exist.
042         */
043        public enum CloneConflictBehavior {
044            /** Do not perform the clone, and record as an {@link Request#setError(Throwable) error} on the request. */
045            DO_NOT_CLONE,
046    
047            /** The clone operation is skipped quietly, resulting in an empty new workspace. */
048            SKIP_CLONE,
049        }
050    
051        /**
052         * The default {@link CloneConflictBehavior} that will be used if it is unspecified.
053         */
054        public static final CloneConflictBehavior DEFAULT_CLONE_CONFLICT_BEHAVIOR = CloneConflictBehavior.DO_NOT_CLONE;
055    
056        /**
057         * The default {@link CreateConflictBehavior} that will be used if it is unspecified.
058         */
059        public static final CreateConflictBehavior DEFAULT_CREATE_CONFLICT_BEHAVIOR = CreateConflictBehavior.DO_NOT_CREATE;
060    
061        private final String nameOfWorkspaceToBeCloned;
062        private final String desiredNameOfTargetWorkspace;
063        private final CreateConflictBehavior createConflictBehavior;
064        private final CloneConflictBehavior cloneConflictBehavior;
065        private String actualWorkspaceName;
066        private Location actualLocationOfRoot;
067    
068        /**
069         * Create a request to clone an existing workspace to create a new workspace, and specify the behavior should a workspace
070         * already exists with a name that matches the desired name for the new workspace.
071         * 
072         * @param nameOfWorkspaceToBeCloned the name of the existing workspace that is to be cloned
073         * @param desiredNameOfTargetWorkspace the desired name of the target workspace
074         * @param createConflictBehavior the behavior if a workspace already exists with the same name
075         * @param cloneConflictBehavior the behavior if the workspace to be cloned does not exist
076         * @throws IllegalArgumentException if the either workspace name is null
077         */
078        public CloneWorkspaceRequest( String nameOfWorkspaceToBeCloned,
079                                      String desiredNameOfTargetWorkspace,
080                                      CreateConflictBehavior createConflictBehavior,
081                                      CloneConflictBehavior cloneConflictBehavior ) {
082            CheckArg.isNotNull(nameOfWorkspaceToBeCloned, "nameOfWorkspaceToBeCloned");
083            CheckArg.isNotNull(desiredNameOfTargetWorkspace, "desiredNameOfTargetWorkspace");
084            this.nameOfWorkspaceToBeCloned = nameOfWorkspaceToBeCloned;
085            this.desiredNameOfTargetWorkspace = desiredNameOfTargetWorkspace;
086            this.createConflictBehavior = createConflictBehavior != null ? createConflictBehavior : DEFAULT_CREATE_CONFLICT_BEHAVIOR;
087            this.cloneConflictBehavior = cloneConflictBehavior != null ? cloneConflictBehavior : DEFAULT_CLONE_CONFLICT_BEHAVIOR;
088        }
089    
090        /**
091         * Get the name of the existing workspace that is to be cloned into the new workspace.
092         * 
093         * @return the name of the existing workspace that is to be cloned; never null
094         */
095        public String nameOfWorkspaceToBeCloned() {
096            return nameOfWorkspaceToBeCloned;
097        }
098    
099        /**
100         * Get the desired name for the target workspace.
101         * 
102         * @return the desired name for the new workspace; never null
103         */
104        public String desiredNameOfTargetWorkspace() {
105            return desiredNameOfTargetWorkspace;
106        }
107    
108        /**
109         * Get the desired behavior if a workspace already exists with the {@link #desiredNameOfTargetWorkspace() desired workspace
110         * name} .
111         * 
112         * @return the desired behavior; never null
113         */
114        public CreateConflictBehavior targetConflictBehavior() {
115            return createConflictBehavior;
116        }
117    
118        /**
119         * Get the desired behavior if the {@link #nameOfWorkspaceToBeCloned() cloned workspace} does not exist.
120         * 
121         * @return the desired behavior; never null
122         */
123        public CloneConflictBehavior cloneConflictBehavior() {
124            return cloneConflictBehavior;
125        }
126    
127        /**
128         * Get the actual name of the workspace that was created. This will be the same as the {@link #desiredNameOfTargetWorkspace()
129         * desired target name} unless there was a conflict and the {@link #targetConflictBehavior() desired behavior} was to
130         * {@link CreateConflictBehavior#CREATE_WITH_ADJUSTED_NAME alter the name}.
131         * 
132         * @return the actual name of the workspace that was created, or null if a workspace was not created (yet)
133         */
134        public String getActualWorkspaceName() {
135            return actualWorkspaceName;
136        }
137    
138        /**
139         * Set the actual name of the workspace that was created. This should be the same as the
140         * {@link #desiredNameOfTargetWorkspace() desired target name} unless there was a conflict and the
141         * {@link #targetConflictBehavior() desired behavior} was to {@link CreateConflictBehavior#CREATE_WITH_ADJUSTED_NAME alter the
142         * name}.
143         * 
144         * @param actualWorkspaceName the actual name of the workspace that was created, or null if a workspace was not created
145         */
146        public void setActualWorkspaceName( String actualWorkspaceName ) {
147            this.actualWorkspaceName = actualWorkspaceName;
148        }
149    
150        /**
151         * Get the actual location of the root node in the new workspace, or null if the workspace was not (yet) created.
152         * 
153         * @return the actual location of the root node in the new workspace, or null if the workspace was not (yet) created
154         */
155        public Location getActualLocationOfRoot() {
156            return actualLocationOfRoot;
157        }
158    
159        /**
160         * Set the actual location of the root node in the new workspace.
161         * 
162         * @param actualLocationOfRoot the actual location of the workspace's root node.
163         */
164        public void setActualRootLocation( Location actualLocationOfRoot ) {
165            this.actualLocationOfRoot = actualLocationOfRoot;
166        }
167    
168        /**
169         * {@inheritDoc}
170         * 
171         * @see org.jboss.dna.graph.request.Request#isReadOnly()
172         */
173        @Override
174        public boolean isReadOnly() {
175            return false;
176        }
177    
178        /**
179         * {@inheritDoc}
180         * 
181         * @see java.lang.Object#hashCode()
182         */
183        @Override
184        public int hashCode() {
185            return desiredNameOfTargetWorkspace.hashCode();
186        }
187    
188        /**
189         * {@inheritDoc}
190         * 
191         * @see java.lang.Object#equals(java.lang.Object)
192         */
193        @Override
194        public boolean equals( Object obj ) {
195            if (obj == this) return true;
196            if (this.getClass().isInstance(obj)) {
197                CloneWorkspaceRequest that = (CloneWorkspaceRequest)obj;
198                if (!this.nameOfWorkspaceToBeCloned.equals(that.nameOfWorkspaceToBeCloned())) return false;
199                if (!this.desiredNameOfTargetWorkspace.equals(that.desiredNameOfTargetWorkspace())) return false;
200                return true;
201            }
202            return false;
203        }
204    
205        /**
206         * {@inheritDoc}
207         * 
208         * @see java.lang.Object#toString()
209         */
210        @Override
211        public String toString() {
212            return "clone workspace \"" + nameOfWorkspaceToBeCloned() + "\" as workspace \"" + desiredNameOfTargetWorkspace() + "\"";
213        }
214    }