]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
0239eee10b939cb084280c1fe026bd6f69f8c920
[tmp/jakarta-migration.git] /
1 package org.collectionspace.services.common.document;
2
3 import org.collectionspace.services.common.context.MultipartServiceContext;
4 import org.collectionspace.services.common.context.ServiceContext;
5 import org.collectionspace.services.common.document.DocumentHandler.Action;
6 import org.slf4j.Logger;
7 import org.slf4j.LoggerFactory;
8
9 // TODO: Auto-generated Javadoc
10 /**
11  * The Class ValidatorHandlerImpl.
12  */
13 public abstract class ValidatorHandlerImpl<IT, OT> implements ValidatorHandler<IT, OT> {
14
15     /**
16      * The logger.
17      */
18     private final Logger logger = LoggerFactory.getLogger(ValidatorHandlerImpl.class);
19     private ServiceContext<IT, OT> ctx;
20
21     protected ServiceContext<IT, OT> getServiceContext() {
22         return ctx;
23     }
24     // gets reset by calls to setServiceContext() method
25     protected boolean enforceAsserts = true;
26
27     public boolean getEnforceAsserts() {
28         return enforceAsserts;
29     }
30
31     public void setEnforceAsserts(ServiceContext<IT, OT> ctx) {
32         Boolean disableAssertsAttr = ctx.getServiceBinding().isDisableAsserts();
33         if (disableAssertsAttr == null) {
34             enforceAsserts = true;
35         } else {
36             enforceAsserts = !disableAssertsAttr.booleanValue();
37         }
38     }
39
40     protected void setServiceContext(ServiceContext<IT, OT> ctx) {
41         this.ctx = ctx;
42
43     }
44
45     protected void CS_ASSERT(boolean expression, String errorMsg) throws AssertionError {
46         if (expression != true) {
47             if (errorMsg == null) {
48                 errorMsg = "Validation exception occurred in: "
49                         + this.getClass().getName();
50             }
51             throw new AssertionError(errorMsg);
52         }
53     }
54
55     protected void CS_ASSERT(boolean expression) throws AssertionError {
56         CS_ASSERT(expression, null);
57     }
58
59     private void init(ServiceContext<IT, OT> ctx) {
60         setEnforceAsserts(ctx);
61         setServiceContext(ctx);
62     }
63
64     /*
65      * (non-Javadoc) @see
66      * org.collectionspace.services.common.document.ValidatorHandler#validate(org.collectionspace.services.common.document.DocumentHandler.Action,
67      * org.collectionspace.services.common.context.ServiceContext)
68      */
69     @Override
70     public void validate(Action action, ServiceContext<IT, OT> ctx)
71             throws InvalidDocumentException {
72         init(ctx);
73
74         switch (action) {
75             case CREATE:
76                 handleCreate();
77                 break;
78             case GET:
79                 handleGet();
80                 break;
81             case GET_ALL:
82                 handleGetAll();
83                 break;
84             case UPDATE:
85                 handleUpdate();
86                 break;
87             case DELETE:
88                 handleDelete();
89                 break;
90             default:
91                 throw new UnsupportedOperationException("ValidatorHandlerImpl: Unknown action = "
92                         + action);
93         }
94     }
95
96     protected boolean enforceAsserts() {
97         return !ctx.getServiceBinding().isDisableAsserts();
98     }
99
100     protected Object getCommonPart() {
101         Object result = null;
102
103         try {
104             MultipartServiceContext multiPartCtx = (MultipartServiceContext) getServiceContext();
105             result = multiPartCtx.getInputPart(ctx.getCommonPartLabel(),
106                     getCommonPartClass());
107         } catch (Exception e) {
108             if (logger.isDebugEnabled() == true) {
109                 logger.debug("Could not extract common part from multipart input.", e);
110             }
111         }
112
113         return result;
114     }
115
116     abstract protected Class<?> getCommonPartClass();
117
118     /**
119      * Handle create.
120      *
121      * @param ctx the ctx
122      */
123     abstract protected void handleCreate() throws InvalidDocumentException;
124
125     /**
126      * Handle get.
127      *
128      * @param ctx the ctx
129      */
130     abstract protected void handleGet() throws InvalidDocumentException;
131
132     /**
133      * Handle get all.
134      *
135      * @param ctx the ctx
136      */
137     abstract protected void handleGetAll() throws InvalidDocumentException;
138
139     /**
140      * Handle update.
141      *
142      * @param ctx the ctx
143      */
144     abstract protected void handleUpdate() throws InvalidDocumentException;
145
146     /**
147      * Handle delete.
148      *
149      * @param ctx the ctx
150      */
151     abstract protected void handleDelete() throws InvalidDocumentException;
152 }