1 package org.collectionspace.services.common.document;
\r
3 import org.collectionspace.services.common.context.MultipartServiceContext;
\r
4 import org.collectionspace.services.common.context.ServiceContext;
\r
5 import org.collectionspace.services.common.document.DocumentHandler.Action;
\r
6 import org.slf4j.Logger;
\r
7 import org.slf4j.LoggerFactory;
\r
9 // TODO: Auto-generated Javadoc
\r
11 * The Class ValidatorHandlerImpl.
\r
13 public abstract class ValidatorHandlerImpl<IT, OT> implements ValidatorHandler<IT, OT> {
\r
18 private final Logger logger = LoggerFactory.getLogger(ValidatorHandlerImpl.class);
\r
19 private ServiceContext<IT, OT> ctx;
\r
21 protected ServiceContext<IT, OT> getServiceContext() {
\r
24 // gets reset by calls to setServiceContext() method
\r
25 protected boolean enforceAsserts = true;
\r
27 public boolean getEnforceAsserts() {
\r
28 return enforceAsserts;
\r
31 public void setEnforceAsserts(ServiceContext<IT, OT> ctx) {
\r
32 Boolean disableAssertsAttr = ctx.getServiceBinding().isDisableAsserts();
\r
33 if (disableAssertsAttr == null) {
\r
34 enforceAsserts = true;
\r
36 enforceAsserts = !disableAssertsAttr.booleanValue();
\r
40 protected void setServiceContext(ServiceContext<IT, OT> ctx) {
\r
45 protected void CS_ASSERT(boolean expression, String errorMsg) throws AssertionError {
\r
46 if (expression != true) {
\r
47 if (errorMsg == null) {
\r
48 errorMsg = "Validation exception occurred in: "
\r
49 + this.getClass().getName();
\r
51 throw new AssertionError(errorMsg);
\r
55 protected void CS_ASSERT(boolean expression) throws AssertionError {
\r
56 CS_ASSERT(expression, null);
\r
59 private void init(ServiceContext<IT, OT> ctx) {
\r
60 setEnforceAsserts(ctx);
\r
61 setServiceContext(ctx);
\r
65 * (non-Javadoc) @see
\r
66 * org.collectionspace.services.common.document.ValidatorHandler#validate(org.collectionspace.services.common.document.DocumentHandler.Action,
\r
67 * org.collectionspace.services.common.context.ServiceContext)
\r
70 public void validate(Action action, ServiceContext<IT, OT> ctx)
\r
71 throws InvalidDocumentException {
\r
91 throw new UnsupportedOperationException("ValidatorHandlerImpl: Unknown action = "
\r
96 protected boolean enforceAsserts() {
\r
97 return !ctx.getServiceBinding().isDisableAsserts();
\r
100 protected Object getCommonPart() {
\r
101 Object result = null;
\r
104 MultipartServiceContext multiPartCtx = (MultipartServiceContext) getServiceContext();
\r
105 result = multiPartCtx.getInputPart(ctx.getCommonPartLabel(),
\r
106 getCommonPartClass());
\r
107 } catch (Exception e) {
\r
108 if (logger.isDebugEnabled() == true) {
\r
109 logger.debug("Could not extract common part from multipart input.", e);
\r
116 abstract protected Class<?> getCommonPartClass();
\r
121 * @param ctx the ctx
\r
123 abstract protected void handleCreate() throws InvalidDocumentException;
\r
128 * @param ctx the ctx
\r
130 abstract protected void handleGet() throws InvalidDocumentException;
\r
135 * @param ctx the ctx
\r
137 abstract protected void handleGetAll() throws InvalidDocumentException;
\r
142 * @param ctx the ctx
\r
144 abstract protected void handleUpdate() throws InvalidDocumentException;
\r
149 * @param ctx the ctx
\r
151 abstract protected void handleDelete() throws InvalidDocumentException;
\r