/
NodeRef.java
311 lines (288 loc) · 8.94 KB
/
NodeRef.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
* #%L
* Alfresco Data model classes
* %%
* Copyright (C) 2005 - 2016 Alfresco Software Limited
* %%
* This file is part of the Alfresco software.
* If the software was purchased under a paid Alfresco license, the terms of
* the paid license agreement will prevail. Otherwise, the software is
* provided under the following open source license terms:
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
* #L%
*/
package org.alfresco.service.cmr.repository;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.alfresco.api.AlfrescoPublicApi;
import org.apache.commons.logging.Log;
/**
* Reference to a node
*
* @author Derek Hulley
*/
@AlfrescoPublicApi
public final class NodeRef implements EntityRef, Serializable
{
// Let's force introspection of this class straight away so that we don't get contention when multiple threads try
// to cache BeanInfo
static
{
try
{
Introspector.getBeanInfo(NodeRef.class);
}
catch (IntrospectionException e)
{
throw new ExceptionInInitializerError(e);
}
}
private static final long serialVersionUID = 3760844584074227768L;
private static final String URI_FILLER = "/";
private static final Pattern nodeRefPattern = Pattern.compile(".+://.+/.+");
private final StoreRef storeRef;
private final String id;
/**
* @see #NodeRef(StoreRef, String)
* @see StoreRef#StoreRef(String, String)
*/
public NodeRef(String protocol, String identifier, String id)
{
this(new StoreRef(protocol, identifier), id);
}
/**
* Construct a Node Reference from a Store Reference and Node Id
*
* @param storeRef store reference
* @param id the manually assigned identifier of the node
*/
public NodeRef(StoreRef storeRef, String id)
{
if (storeRef == null)
{
throw new IllegalArgumentException("Store reference may not be null");
}
if (id == null)
{
throw new IllegalArgumentException("Node id may not be null");
}
this.storeRef = storeRef;
this.id = id;
}
/**
* Construct a Node Reference from a string representation of a Node Reference.
* <p>
* The string representation of a Node Reference is as follows:
* </p>
* <pre><storeref>/<nodeId></pre>
*
* @param nodeRef the string representation of a node ref
*/
public NodeRef(String nodeRef)
{
int lastForwardSlash = nodeRef.lastIndexOf('/');
if(lastForwardSlash == -1)
{
throw new MalformedNodeRefException("Invalid node ref - does not contain forward slash: " + nodeRef);
}
this.storeRef = new StoreRef(nodeRef.substring(0, lastForwardSlash));
this.id = nodeRef.substring(lastForwardSlash+1);
}
@Override
public String toString()
{
return storeRef.toString() + URI_FILLER + id;
}
/**
* Override equals for this ref type
*
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj)
{
if (this == obj)
{
return true;
}
if (obj instanceof NodeRef)
{
NodeRef that = (NodeRef) obj;
return (this.id.equals(that.id)
&& this.storeRef.equals(that.storeRef));
}
else
{
return false;
}
}
/**
* Hashes on ID alone. As the number of copies of a particular node will be minimal, this is acceptable
*/
@Override
public int hashCode()
{
return id.hashCode();
}
/**
* @return The StoreRef part of this reference
*/
public final StoreRef getStoreRef()
{
return storeRef;
}
/**
* @return The Node Id part of this reference
*/
public final String getId()
{
return id;
}
/**
* Determine if passed string conforms to the pattern of a node reference
*
* @param nodeRef the node reference as a string
* @return true => it matches the pattern of a node reference
*/
public static boolean isNodeRef(String nodeRef)
{
Matcher matcher = nodeRefPattern.matcher(nodeRef);
return matcher.matches();
}
/**
* Converts a {@link String} containing a comma-separated list of {@link NodeRef} Ids into NodeRefs.
* @param values the {@link String} of {@link NodeRef} ids.
* @return A {@link List} of {@link NodeRef NodeRefs}.
*/
public static List<NodeRef> getNodeRefs(String values)
{
return getNodeRefs(values, null);
}
/**
* Converts a {@link String} containing a comma-separated list of {@link NodeRef} Ids into NodeRefs.
* If a <code>logger</code> is supplied then invalid ids are logged as warnings.
* @param values the {@link String} of {@link NodeRef} ids.
* @param logger Log
* @return A {@link List} of {@link NodeRef NodeRefs}.
*/
public static List<NodeRef> getNodeRefs(String values, Log logger)
{
if(values==null || values.length()==0)
return Collections.emptyList();
String[] nodeRefIds = values.split(",");
List<NodeRef> nodeRefs = new ArrayList<NodeRef>(nodeRefIds.length);
for (String nodeRefString : nodeRefIds)
{
String nodeRefId = nodeRefString.trim();
if (NodeRef.isNodeRef(nodeRefId))
{
NodeRef nodeRef = new NodeRef(nodeRefId);
nodeRefs.add(nodeRef);
}
else if (logger!=null)
{
logNodeRefError(nodeRefId, logger);
}
}
return nodeRefs;
}
private static void logNodeRefError(String nodeRefId, Log logger)
{
if (logger.isWarnEnabled())
{
StringBuilder msg = new StringBuilder();
msg.append("Target Node: ").append(nodeRefId);
msg.append(" is not a valid NodeRef and has been ignored.");
logger.warn(msg.toString());
}
}
/**
* Helper class to convey the status of a <b>node</b>.
*
* @author Derek Hulley
*/
public static class Status
{
private final Long dbId;
private final NodeRef nodeRef;
private final String changeTxnId;
private final Long dbTxnId;
private final boolean deleted;
public Status(Long dbId, NodeRef nodeRef, String changeTxnId, Long dbTxnId, boolean deleted)
{
this.dbId = dbId;
this.nodeRef = nodeRef;
this.changeTxnId = changeTxnId;
this.dbTxnId = dbTxnId;
this.deleted = deleted;
}
/**
* Return the database ID for the node
*/
public Long getDbId()
{
return dbId;
}
/**
* @return Returns the NodeRef that to which this status applies
*/
public NodeRef getNodeRef()
{
return nodeRef;
}
/**
* @return Returns the ID of the last transaction to change the node
*
* @deprecated This will be removed when we have switched to SOLR tracking only
*/
public String getChangeTxnId()
{
return changeTxnId;
}
/**
* @return Returns the db ID of the last transaction to change the node
*/
public Long getDbTxnId()
{
return dbTxnId;
}
/**
* @return Returns true if the node has been deleted, otherwise false
*/
public boolean isDeleted()
{
return deleted;
}
// debug display string
@Override
public String toString()
{
StringBuilder sb = new StringBuilder(50);
sb.append("Status[")
.append("id=").append(dbId)
.append("changeTxnId=").append(changeTxnId)
.append(", dbTxnId=").append(dbTxnId)
.append(", deleted=").append(deleted)
.append("]");
return sb.toString();
}
}
}