1 package org.collectionspace.services.batch.nuxeo;
3 import org.collectionspace.services.batch.BatchCommon;
4 import org.collectionspace.services.batch.BatchInvocable;
5 import org.collectionspace.services.client.PoxPayloadIn;
6 import org.collectionspace.services.client.PoxPayloadOut;
7 import org.collectionspace.services.common.document.InvalidDocumentException;
8 import org.collectionspace.services.common.document.ValidatorHandlerImpl;
10 import org.slf4j.Logger;
11 import org.slf4j.LoggerFactory;
13 public class BatchValidatorHandler extends ValidatorHandlerImpl<PoxPayloadIn, PoxPayloadOut> {
15 final Logger logger = LoggerFactory.getLogger(BatchValidatorHandler.class);
20 private static final String VALIDATION_ERROR = "The batch record payload was invalid. See log file for more details.";
21 private static final String NAME_NULL_ERROR = "The batch record field \"name\" cannot be empty or missing.";
22 private static final String MISSING_CLASS_ERROR = "The Java class '%s' (fully qualified with package name) for the batch job named '%s' cannot be found.";
25 protected Class<?> getCommonPartClass() {
26 return BatchCommon.class;
30 protected void handleCreate() throws InvalidDocumentException {
32 BatchCommon batchCommon = (BatchCommon) getCommonPart();
33 validateBatchCommon(batchCommon);
34 } catch (AssertionError e) {
35 if (logger.isErrorEnabled() == true) {
36 logger.error(e.getMessage(), e);
38 throw new InvalidDocumentException(VALIDATION_ERROR, e);
43 protected void handleGet() throws InvalidDocumentException {
44 // TODO Auto-generated method stub
49 protected void handleGetAll() throws InvalidDocumentException {
50 // TODO Auto-generated method stub
55 protected void handleUpdate() throws InvalidDocumentException {
57 BatchCommon batchCommon = (BatchCommon) getCommonPart();
58 validateBatchCommon(batchCommon);
59 } catch (AssertionError e) {
60 if (logger.isErrorEnabled() == true) {
61 logger.error(e.getMessage(), e);
63 throw new InvalidDocumentException(VALIDATION_ERROR, e);
68 protected void handleDelete() throws InvalidDocumentException {
69 // TODO Auto-generated method stub
76 private boolean canFindClass(String className) {
77 boolean result = false;
80 ClassLoader tccl = Thread.currentThread().getContextClassLoader();
81 Class<?> c = tccl.loadClass(className);
82 tccl.setClassAssertionStatus(className, true);
83 if (!BatchInvocable.class.isAssignableFrom(c)) {
84 throw new RuntimeException("BatchResource: Class: " + className + " does not implement BatchInvocable!");
87 } catch (Exception e) {
88 String msg = String.format("Could not find load batch class named '%s'",
96 private void validateBatchCommon(BatchCommon batchCommon) {
97 CS_ASSERT(batchCommon != null);
100 // Ensure a batch name
101 String batchName = batchCommon.getName();
102 CS_ASSERT(batchName != null, NAME_NULL_ERROR);
103 CS_ASSERT(batchName.isEmpty() == false, NAME_NULL_ERROR);
106 // Ensure a batch class
107 String batchClassName = batchCommon.getClassName();
108 CS_ASSERT(batchName != null, NAME_NULL_ERROR);
109 CS_ASSERT(batchName.isEmpty() == false, NAME_NULL_ERROR);
112 // Ensure we can find and load the batch Java class
113 if (canFindClass(batchClassName) == false) {
114 String msg = String.format(MISSING_CLASS_ERROR, batchClassName, batchCommon.getName());
115 CS_ASSERT(false, batchClassName);