1 package org.collectionspace.services.common.document;
\r
4 import org.collectionspace.services.common.context.MultipartServiceContext;
\r
5 import org.collectionspace.services.common.context.ServiceContext;
\r
6 import org.collectionspace.services.common.document.DocumentHandler.Action;
\r
7 import org.slf4j.Logger;
\r
8 import org.slf4j.LoggerFactory;
\r
10 // TODO: Auto-generated Javadoc
\r
12 * The Class ValidatorHandlerImpl.
\r
14 public abstract class ValidatorHandlerImpl<IT, OT> implements ValidatorHandler<IT, OT> {
\r
17 private final Logger logger = LoggerFactory.getLogger(ValidatorHandlerImpl.class);
\r
19 private ServiceContext<IT, OT> ctx;
\r
21 protected ServiceContext<IT, OT> getServiceContext() {
\r
25 // gets reset by calls to setServiceContext() method
\r
26 protected boolean enforceAsserts = true;
\r
28 public boolean getEnforceAsserts() {
\r
29 return enforceAsserts;
\r
32 public void setEnforceAsserts(ServiceContext<IT, OT> ctx) {
\r
33 Boolean disableAssertsAttr = ctx.getServiceBinding().isDisableAsserts();
\r
34 if (disableAssertsAttr == null) {
\r
35 enforceAsserts = true;
\r
37 enforceAsserts = !disableAssertsAttr.booleanValue();
\r
41 protected void setServiceContext(ServiceContext<IT, OT> ctx) {
\r
46 protected void CS_ASSERT(boolean expression, String errorMsg) throws AssertionError {
\r
47 if (expression != true) {
\r
48 if (errorMsg == null) {
\r
49 errorMsg = "Validation exception occurred in: " +
\r
50 this.getClass().getName();
\r
52 throw new AssertionError(errorMsg);
\r
56 protected void CS_ASSERT(boolean expression) throws AssertionError {
\r
57 CS_ASSERT(expression, null);
\r
60 private void init(ServiceContext<IT, OT> ctx) {
\r
61 setEnforceAsserts(ctx);
\r
62 setServiceContext(ctx);
\r
66 * @see org.collectionspace.services.common.document.ValidatorHandler#validate(org.collectionspace.services.common.document.DocumentHandler.Action, org.collectionspace.services.common.context.ServiceContext)
\r
69 public void validate(Action action, ServiceContext<IT, OT> ctx)
\r
70 throws InvalidDocumentException {
\r
90 throw new UnsupportedOperationException("ValidatorHandlerImpl: Unknow action = " +
\r
95 protected boolean enforceAsserts() {
\r
96 return !ctx.getServiceBinding().isDisableAsserts();
\r
99 protected Object getCommonPart() {
\r
100 Object result = null;
\r
103 MultipartServiceContext multiPartCtx = (MultipartServiceContext) getServiceContext();
\r
104 result = multiPartCtx.getInputPart(ctx.getCommonPartLabel(),
\r
105 getCommonPartClass());
\r
106 } catch (Exception e) {
\r
107 if (logger.isDebugEnabled() == true) {
\r
108 logger.debug("Could not extract common part from multipart input.", e);
\r
115 abstract protected Class<?> getCommonPartClass();
\r
120 * @param ctx the ctx
\r
122 abstract protected void handleCreate() throws InvalidDocumentException;
\r
127 * @param ctx the ctx
\r
129 abstract protected void handleGet() throws InvalidDocumentException;
\r
134 * @param ctx the ctx
\r
136 abstract protected void handleGetAll() throws InvalidDocumentException;
\r
141 * @param ctx the ctx
\r
143 abstract protected void handleUpdate() throws InvalidDocumentException;
\r
148 * @param ctx the ctx
\r
150 abstract protected void handleDelete() throws InvalidDocumentException;
\r