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 not {@link Location#equals(Object) equal to} the {@link #at()
92 * current location}, or if the actual location does not have a path.
93 * @throws IllegalStateException if the request is frozen
94 */
95 public void setActualLocationOfNode( Location actual ) {
96 checkNotFrozen();
97 if (!at.equals(actual)) { // not same if actual is null
98 throw new IllegalArgumentException(GraphI18n.actualLocationNotEqualToInputLocation.text(actual, at));
99 }
100 assert actual != null;
101 if (!actual.hasPath()) {
102 throw new IllegalArgumentException(GraphI18n.actualLocationMustHavePath.text(actual));
103 }
104 this.actualLocation = actual;
105 }
106
107 /**
108 * Get the actual location of the node that was deleted.
109 *
110 * @return the actual location, or null if the actual location was not set
111 */
112 public Location getActualLocationOfNode() {
113 return actualLocation;
114 }
115
116 /**
117 * {@inheritDoc}
118 *
119 * @see org.modeshape.graph.request.ChangeRequest#changes(java.lang.String, org.modeshape.graph.property.Path)
120 */
121 @Override
122 public boolean changes( String workspace,
123 Path path ) {
124 return this.workspaceName.equals(workspace) && at.hasPath() && at.getPath().isAtOrBelow(path);
125 }
126
127 /**
128 * {@inheritDoc}
129 *
130 * @see org.modeshape.graph.request.ChangeRequest#changedLocation()
131 */
132 @Override
133 public Location changedLocation() {
134 return actualLocation != null ? actualLocation : at;
135 }
136
137 /**
138 * {@inheritDoc}
139 *
140 * @see org.modeshape.graph.request.ChangeRequest#changedWorkspace()
141 */
142 @Override
143 public String changedWorkspace() {
144 return workspaceName;
145 }
146
147 /**
148 * {@inheritDoc}
149 *
150 * @see org.modeshape.graph.request.Request#cancel()
151 */
152 @Override
153 public void cancel() {
154 super.cancel();
155 this.actualLocation = null;
156 }
157
158 /**
159 * {@inheritDoc}
160 *
161 * @see java.lang.Object#hashCode()
162 */
163 @Override
164 public int hashCode() {
165 return HashCode.compute(at, workspaceName);
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 DeleteBranchRequest that = (DeleteBranchRequest)obj;
178 if (!this.at().isSame(that.at())) return false;
179 if (!this.inWorkspace().equals(that.inWorkspace())) return false;
180 return true;
181 }
182 return false;
183 }
184
185 /**
186 * {@inheritDoc}
187 *
188 * @see java.lang.Object#toString()
189 */
190 @Override
191 public String toString() {
192 return "delete branch " + at() + " in the \"" + workspaceName + "\" workspace";
193 }
194
195 /**
196 * {@inheritDoc}
197 * <p>
198 * This method does not clone the results.
199 * </p>
200 *
201 * @see org.modeshape.graph.request.ChangeRequest#clone()
202 */
203 @Override
204 public DeleteBranchRequest clone() {
205 DeleteBranchRequest request = new DeleteBranchRequest(actualLocation != null ? actualLocation : at, workspaceName);
206 request.setActualLocationOfNode(actualLocation);
207 return request;
208 }
209 }