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    
029    /**
030     * Request that a new workspace be created with the supplied name. The request also specifies the desired
031     * {@link #conflictBehavior() behavior} for the recipient if a workspace already exists with the name.
032     */
033    public final class CreateWorkspaceRequest extends Request {
034    
035        private static final long serialVersionUID = 1L;
036    
037        /**
038         * The options for the behavior when a request specifies a workspace name that already is used by an existing workspace.
039         */
040        public static enum CreateConflictBehavior {
041            /** Do not create the workspace, and record as an {@link Request#setError(Throwable) error} on the request. */
042            DO_NOT_CREATE,
043    
044            /** Create the workspace by adjusting the name so that it is unique. */
045            CREATE_WITH_ADJUSTED_NAME
046        }
047    
048        /**
049         * The default {@link CreateConflictBehavior} that will be used if it is unspecified.
050         */
051        public static final CreateConflictBehavior DEFAULT_CREATE_CONFLICT_BEHAVIOR = CreateConflictBehavior.DO_NOT_CREATE;
052    
053        private final String desiredNameOfNewWorkspace;
054        private final CreateConflictBehavior createConflictBehavior;
055        private String actualWorkspaceName;
056        private Location actualLocationOfRoot;
057    
058        /**
059         * Create a request to create a new workspace, and specify the behavior should a workspace already exists with a name that
060         * matches the desired name for the new workspace.
061         * 
062         * @param desiredNameOfNewWorkspace the desired name of the new workspace
063         * @param createConflictBehavior the behavior if a workspace already exists with the same name
064         */
065        public CreateWorkspaceRequest( String desiredNameOfNewWorkspace,
066                                       CreateConflictBehavior createConflictBehavior ) {
067            CheckArg.isNotNull(desiredNameOfNewWorkspace, "desiredNameOfNewWorkspace");
068            this.desiredNameOfNewWorkspace = desiredNameOfNewWorkspace;
069            this.createConflictBehavior = createConflictBehavior != null ? createConflictBehavior : DEFAULT_CREATE_CONFLICT_BEHAVIOR;
070        }
071    
072        /**
073         * Get the desired name for the new workspace.
074         * 
075         * @return the desired name for the new workspace; never null
076         */
077        public String desiredNameOfNewWorkspace() {
078            return desiredNameOfNewWorkspace;
079        }
080    
081        /**
082         * Get the desired behavior if a workspace already exists with the {@link #desiredNameOfNewWorkspace() desired workspace name}
083         * .
084         * 
085         * @return the desired behavior; never null
086         */
087        public CreateConflictBehavior conflictBehavior() {
088            return createConflictBehavior;
089        }
090    
091        /**
092         * Get the actual name of the workspace that was created. This will be the same as the {@link #desiredNameOfNewWorkspace()
093         * desired name} unless there was a conflict and the {@link #conflictBehavior() desired behavior} was to
094         * {@link CreateConflictBehavior#CREATE_WITH_ADJUSTED_NAME alter the name}.
095         * 
096         * @return the actual name of the workspace that was created, or null if a workspace was not created (yet)
097         */
098        public String getActualWorkspaceName() {
099            return actualWorkspaceName;
100        }
101    
102        /**
103         * Set the actual name of the workspace that was created. This should be the same as the {@link #desiredNameOfNewWorkspace()
104         * desired name} unless there was a conflict and the {@link #conflictBehavior() desired behavior} was to
105         * {@link CreateConflictBehavior#CREATE_WITH_ADJUSTED_NAME alter the name}.
106         * 
107         * @param actualWorkspaceName the actual name of the workspace that was created, or null if a workspace was not created
108         */
109        public void setActualWorkspaceName( String actualWorkspaceName ) {
110            this.actualWorkspaceName = actualWorkspaceName;
111        }
112    
113        /**
114         * Get the actual location of the root node in the new workspace, or null if the workspace was not (yet) created.
115         * 
116         * @return the actual location of the root node in the new workspace, or null if the workspace was not (yet) created
117         */
118        public Location getActualLocationOfRoot() {
119            return actualLocationOfRoot;
120        }
121    
122        /**
123         * Set the actual location of the root node in the new workspace.
124         * 
125         * @param actualLocationOfRoot the actual location of the workspace's root node.
126         */
127        public void setActualRootLocation( Location actualLocationOfRoot ) {
128            this.actualLocationOfRoot = actualLocationOfRoot;
129        }
130    
131        /**
132         * {@inheritDoc}
133         * 
134         * @see org.jboss.dna.graph.request.Request#isReadOnly()
135         */
136        @Override
137        public boolean isReadOnly() {
138            return false;
139        }
140    
141        /**
142         * {@inheritDoc}
143         * 
144         * @see java.lang.Object#hashCode()
145         */
146        @Override
147        public int hashCode() {
148            return desiredNameOfNewWorkspace.hashCode();
149        }
150    
151        /**
152         * {@inheritDoc}
153         * 
154         * @see java.lang.Object#equals(java.lang.Object)
155         */
156        @Override
157        public boolean equals( Object obj ) {
158            if (obj == this) return true;
159            if (this.getClass().isInstance(obj)) {
160                CreateWorkspaceRequest that = (CreateWorkspaceRequest)obj;
161                if (!this.desiredNameOfNewWorkspace.equals(that.desiredNameOfNewWorkspace())) return false;
162                return true;
163            }
164            return false;
165        }
166    
167        /**
168         * {@inheritDoc}
169         * 
170         * @see java.lang.Object#toString()
171         */
172        @Override
173        public String toString() {
174            return "create new workspace \"" + desiredNameOfNewWorkspace() + "\"";
175        }
176    }