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.request;
25
26 import org.modeshape.common.util.CheckArg;
27 import org.modeshape.common.util.HashCode;
28 import org.modeshape.graph.GraphI18n;
29 import org.modeshape.graph.Location;
30 import org.modeshape.graph.property.Path;
31
32 /**
33 * Instruction that a branch be deleted.
34 */
35 public class DeleteBranchRequest extends ChangeRequest {
36
37 private static final long serialVersionUID = 1L;
38
39 private final Location at;
40 private final String workspaceName;
41 private Location actualLocation;
42
43 /**
44 * Create a request to delete a branch.
45 *
46 * @param at the location of the top node in the existing branch that is to be deleted
47 * @param workspaceName the name of the workspace containing the parent
48 * @throws IllegalArgumentException if the location or workspace name is null
49 */
50 public DeleteBranchRequest( Location at,
51 String workspaceName ) {
52 CheckArg.isNotNull(at, "at");
53 CheckArg.isNotNull(workspaceName, "workspaceName");
54 this.workspaceName = workspaceName;
55 this.at = at;
56 }
57
58 /**
59 * Get the location defining the top of the branch to be deleted
60 *
61 * @return the location of the branch; never null
62 */
63 public Location at() {
64 return at;
65 }
66
67 /**
68 * Get the name of the workspace in which the branch exists.
69 *
70 * @return the name of the workspace; never null
71 */
72 public String inWorkspace() {
73 return workspaceName;
74 }
75
76 /**
77 * {@inheritDoc}
78 *
79 * @see org.modeshape.graph.request.Request#isReadOnly()
80 */
81 @Override
82 public boolean isReadOnly() {
83 return false;
84 }
85
86 /**
87 * Sets the actual and complete location of the node being deleted. This method must be called when processing the request,
88 * and the actual location must have a {@link Location#getPath() path}.
89 *
90 * @param actual the actual location of the node being deleted, or null if the {@link #at() current location} should be used
91 * @throws IllegalArgumentException if the actual location is null or does not have a path.
92 * @throws IllegalStateException if the request is frozen
93 */
94 public void setActualLocationOfNode( Location actual ) {
95 checkNotFrozen();
96 CheckArg.isNotNull(actual, "actual");
97 if (!actual.hasPath()) {
98 throw new IllegalArgumentException(GraphI18n.actualLocationMustHavePath.text(actual));
99 }
100 this.actualLocation = actual;
101 }
102
103 /**
104 * Get the actual location of the node that was deleted.
105 *
106 * @return the actual location, or null if the actual location was not set
107 */
108 public Location getActualLocationOfNode() {
109 return actualLocation;
110 }
111
112 /**
113 * {@inheritDoc}
114 *
115 * @see org.modeshape.graph.request.ChangeRequest#changes(java.lang.String, org.modeshape.graph.property.Path)
116 */
117 @Override
118 public boolean changes( String workspace,
119 Path path ) {
120 return this.workspaceName.equals(workspace) && at.hasPath() && at.getPath().isAtOrBelow(path);
121 }
122
123 /**
124 * {@inheritDoc}
125 *
126 * @see org.modeshape.graph.request.ChangeRequest#changedLocation()
127 */
128 @Override
129 public Location changedLocation() {
130 return actualLocation != null ? actualLocation : at;
131 }
132
133 /**
134 * {@inheritDoc}
135 *
136 * @see org.modeshape.graph.request.ChangeRequest#changedWorkspace()
137 */
138 @Override
139 public String changedWorkspace() {
140 return workspaceName;
141 }
142
143 /**
144 * {@inheritDoc}
145 *
146 * @see org.modeshape.graph.request.Request#cancel()
147 */
148 @Override
149 public void cancel() {
150 super.cancel();
151 this.actualLocation = null;
152 }
153
154 /**
155 * {@inheritDoc}
156 *
157 * @see java.lang.Object#hashCode()
158 */
159 @Override
160 public int hashCode() {
161 return HashCode.compute(at, workspaceName);
162 }
163
164 /**
165 * {@inheritDoc}
166 *
167 * @see java.lang.Object#equals(java.lang.Object)
168 */
169 @Override
170 public boolean equals( Object obj ) {
171 if (obj == this) return true;
172 if (this.getClass().isInstance(obj)) {
173 DeleteBranchRequest that = (DeleteBranchRequest)obj;
174 if (!this.at().isSame(that.at())) return false;
175 if (!this.inWorkspace().equals(that.inWorkspace())) return false;
176 return true;
177 }
178 return false;
179 }
180
181 /**
182 * {@inheritDoc}
183 *
184 * @see java.lang.Object#toString()
185 */
186 @Override
187 public String toString() {
188 return "delete branch " + at() + " in the \"" + workspaceName + "\" workspace";
189 }
190
191 /**
192 * {@inheritDoc}
193 * <p>
194 * This method does not clone the results.
195 * </p>
196 *
197 * @see org.modeshape.graph.request.ChangeRequest#clone()
198 */
199 @Override
200 public DeleteBranchRequest clone() {
201 DeleteBranchRequest request = new DeleteBranchRequest(actualLocation != null ? actualLocation : at, workspaceName);
202 request.setActualLocationOfNode(actualLocation);
203 return request;
204 }
205
206 @Override
207 public RequestType getType() {
208 return RequestType.DELETE_BRANCH;
209 }
210 }