Project

General

Profile

« Previous | Next » 

Revision 6883

add method: setObsoletedBy (https://redmine.dataone.org/issues/2185)
augement new method: deleteReplicationMetadata

View differences:

src/edu/ucsb/nceas/metacat/dataone/CNodeService.java
25 25

  
26 26
import java.io.InputStream;
27 27
import java.math.BigInteger;
28
import java.util.ArrayList;
28 29
import java.util.Calendar;
29 30
import java.util.Date;
30 31
import java.util.List;
......
216 217
   * @throws NotAuthorized
217 218
   * @throws NotFound
218 219
   * @throws NotImplemented
220
   * @throws VersionMismatch
219 221
   */
220 222
  public boolean deleteReplicationMetadata(Session session, Identifier pid, NodeReference nodeId, long serialVersion) 
221
  	throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, NotImplemented {
223
  	throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, NotImplemented, VersionMismatch {
222 224
	  
223
	  List<Node> nodeList = D1Client.getCN().listNodes().getNodeList();
225
	  	// The lock to be used for this identifier
226
		Lock lock = null;
227

  
228
		// get the subject
229
		Subject subject = session.getSubject();
230

  
231
		// are we allowed to do this?
232
		if (!isAdminAuthorized(session, pid, Permission.WRITE)) {
233
			if (!isAuthorized(session, pid, Permission.WRITE)) {
234
				throw new NotAuthorized("4881", Permission.WRITE
235
						+ " not allowed by " + subject.getValue() + " on "
236
						+ pid.getValue());
237

  
238
			}
239
		}
240

  
241
		SystemMetadata systemMetadata = null;
242
		try {
243
			lock = HazelcastService.getInstance().getLock(pid.getValue());
244
			lock.lock();
245
			logMetacat.debug("Locked identifier " + pid.getValue());
246

  
247
			try {
248
				if (HazelcastService.getInstance().getSystemMetadataMap().containsKey(pid)) {
249
					systemMetadata = HazelcastService.getInstance().getSystemMetadataMap().get(pid);
250
				}
251

  
252
				// did we get it correctly?
253
				if (systemMetadata == null) {
254
					throw new NotFound("4884", "Couldn't find an object identified by " + pid.getValue());
255
				}
256

  
257
				// does the request have the most current system metadata?
258
				if (systemMetadata.getSerialVersion().longValue() != serialVersion) {
259
					String msg = "The requested system metadata version number "
260
							+ serialVersion
261
							+ " differs from the current version at "
262
							+ systemMetadata.getSerialVersion().longValue()
263
							+ ". Please get the latest copy in order to modify it.";
264
					throw new VersionMismatch("4886", msg);
265

  
266
				}
267

  
268
			} catch (RuntimeException e) { // Catch is generic since HZ throws RuntimeException
269
				throw new NotFound("4884", "No record found for: " + pid.getValue());
270

  
271
			}
272
			  
273
			// check permissions
274
			// TODO: is this necessary?
275
			List<Node> nodeList = D1Client.getCN().listNodes().getNodeList();
276
			boolean isAllowed = ServiceMethodRestrictionUtil.isMethodAllowed(session.getSubject(), nodeList, "CNReplication", "deleteReplicationMetadata");
277
			if (isAllowed) {
278
				throw new NotAuthorized("4881", "Caller is not authorized to deleteReplicationMetadata");
279
			}
280
			  
281
			// delete the replica from the given node
282
			D1Client.getMN(nodeId).delete(session, pid);
283
			  
284
			// reflect that change in the system metadata
285
			List<Replica> updatedReplicas = new ArrayList<Replica>(systemMetadata.getReplicaList());
286
			for (Replica r: systemMetadata.getReplicaList()) {
287
				  if (r.getReplicaMemberNode().equals(nodeId)) {
288
					  updatedReplicas.remove(r);
289
					  break;
290
				  }
291
			}
292
			systemMetadata.setReplicaList(updatedReplicas);
293

  
294
			// update the metadata
295
			try {
296
				systemMetadata.setSerialVersion(systemMetadata.getSerialVersion().add(BigInteger.ONE));
297
				systemMetadata.setDateSysMetadataModified(Calendar.getInstance().getTime());
298
				HazelcastService.getInstance().getSystemMetadataMap().put(systemMetadata.getIdentifier(), systemMetadata);
299
			} catch (RuntimeException e) {
300
				throw new ServiceFailure("4882", e.getMessage());
301
			}
302

  
303
		} catch (RuntimeException e) {
304
			throw new ServiceFailure("4882", e.getMessage());
305
		} finally {
306
			lock.unlock();
307
			logMetacat.debug("Unlocked identifier " + pid.getValue());
308
		}
309

  
310
		return true;	  
224 311
	  
225
	  // check permissions
226
	  boolean isAllowed = ServiceMethodRestrictionUtil.isMethodAllowed(session.getSubject(), nodeList, "CNReplication", "deleteReplicationMetadata");
227
	  
228
	  if (isAllowed) {
229
		  throw new NotAuthorized("0000", "Caller is not authorized to deleteReplicationMetadata");
230
	  }
231
	  
232
	  // delete the replica from the given node
233
	  D1Client.getMN(nodeId).delete(session, pid);
234
	  
235
	  return true;
236 312
  }
237 313
  
314
  /**
315
   * Set the obsoletedBy attribute in System Metadata
316
   * @param session
317
   * @param pid
318
   * @param obsoletedByPid
319
   * @param serialVersion
320
   * @return
321
   * @throws NotImplemented
322
   * @throws NotFound
323
   * @throws NotAuthorized
324
   * @throws ServiceFailure
325
   * @throws InvalidRequest
326
   * @throws InvalidToken
327
   * @throws VersionMismatch
328
   */
329
  public boolean setObsoletedBy(Session session, Identifier pid,
330
			Identifier obsoletedByPid, long serialVersion)
331
			throws NotImplemented, NotFound, NotAuthorized, ServiceFailure,
332
			InvalidRequest, InvalidToken, VersionMismatch {
333

  
334
		// The lock to be used for this identifier
335
		Lock lock = null;
336

  
337
		// get the subject
338
		Subject subject = session.getSubject();
339

  
340
		// are we allowed to do this?
341
		if (!isAdminAuthorized(session, pid, Permission.WRITE)) {
342
			if (!isAuthorized(session, pid, Permission.WRITE)) {
343
				throw new NotAuthorized("4881", Permission.WRITE
344
						+ " not allowed by " + subject.getValue() + " on "
345
						+ pid.getValue());
346

  
347
			}
348
		}
349

  
350
		SystemMetadata systemMetadata = null;
351
		try {
352
			lock = HazelcastService.getInstance().getLock(pid.getValue());
353
			lock.lock();
354
			logMetacat.debug("Locked identifier " + pid.getValue());
355

  
356
			try {
357
				if (HazelcastService.getInstance().getSystemMetadataMap().containsKey(pid)) {
358
					systemMetadata = HazelcastService.getInstance().getSystemMetadataMap().get(pid);
359
				}
360

  
361
				// did we get it correctly?
362
				if (systemMetadata == null) {
363
					throw new NotFound("4884", "Couldn't find an object identified by " + pid.getValue());
364
				}
365

  
366
				// does the request have the most current system metadata?
367
				if (systemMetadata.getSerialVersion().longValue() != serialVersion) {
368
					String msg = "The requested system metadata version number "
369
							+ serialVersion
370
							+ " differs from the current version at "
371
							+ systemMetadata.getSerialVersion().longValue()
372
							+ ". Please get the latest copy in order to modify it.";
373
					throw new VersionMismatch("4886", msg);
374

  
375
				}
376

  
377
			} catch (RuntimeException e) { // Catch is generic since HZ throws RuntimeException
378
				throw new NotFound("4884", "No record found for: " + pid.getValue());
379

  
380
			}
381

  
382
			// set the new policy
383
			systemMetadata.setObsoletedBy(obsoletedByPid);
384

  
385
			// update the metadata
386
			try {
387
				systemMetadata.setSerialVersion(systemMetadata.getSerialVersion().add(BigInteger.ONE));
388
				systemMetadata.setDateSysMetadataModified(Calendar.getInstance().getTime());
389
				HazelcastService.getInstance().getSystemMetadataMap().put(systemMetadata.getIdentifier(), systemMetadata);
390
			} catch (RuntimeException e) {
391
				throw new ServiceFailure("4882", e.getMessage());
392
			}
393

  
394
		} catch (RuntimeException e) {
395
			throw new ServiceFailure("4882", e.getMessage());
396
		} finally {
397
			lock.unlock();
398
			logMetacat.debug("Unlocked identifier " + pid.getValue());
399
		}
400

  
401
		return true;
402
	}
238 403
  
404
  
239 405
  /**
240 406
   * Set the replication status for an object given the object identifier
241 407
   * 

Also available in: Unified diff