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