1 package org.collectionspace.services.movement.nuxeo;
5 import org.collectionspace.services.common.context.ServiceContext;
6 import org.collectionspace.services.common.context.MultipartServiceContext;
7 import org.collectionspace.services.common.datetime.DateTimeFormatUtils;
8 import org.collectionspace.services.common.document.InvalidDocumentException;
9 import org.collectionspace.services.common.document.ValidatorHandler;
10 import org.collectionspace.services.common.document.DocumentHandler.Action;
11 import org.collectionspace.services.movement.MovementsCommon;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
16 public class MovementValidatorHandler implements ValidatorHandler {
18 final Logger logger = LoggerFactory.getLogger(MovementValidatorHandler.class);
21 public void validate(Action action, ServiceContext ctx)
22 throws InvalidDocumentException {
24 if(logger.isDebugEnabled()) {
25 logger.debug("validate() action=" + action.name());
28 MultipartServiceContext mctx = (MultipartServiceContext) ctx;
29 MovementsCommon mc = (MovementsCommon) mctx.getInputPart(mctx.getCommonPartLabel(),
30 MovementsCommon.class);
31 StringBuilder msgBldr = new StringBuilder("validate()");
32 boolean invalid = false;
34 List<String> patterns = DateTimeFormatUtils.getDateFormatPatternsForTenant(ctx);
36 // FIXME: This is an early proof-of-concept.
38 // We need a better way of determining which fields
39 // in the incoming payload are date fields whose values we
40 // might wish to validate, and of extracting their values,
41 // than hard-coding them here.
44 boolean validDateFormat = false;
45 String locDate = mc.getLocationDate();
46 for (String pattern : patterns) {
47 if (DateTimeFormatUtils.isParseableByDatePattern(locDate, pattern)) {
48 validDateFormat = true;
51 if (! validDateFormat) {
53 msgBldr.append("\nlocationDate : unrecognized date format '" + locDate + "'");
58 if(action.equals(Action.CREATE)) {
59 //create specific validation here
60 } else if(action.equals(Action.UPDATE)) {
61 //update specific validation here
65 String msg = msgBldr.toString();
67 throw new InvalidDocumentException(msg);
69 } catch (InvalidDocumentException ide) {
71 } catch (Exception e) {
72 throw new InvalidDocumentException(e);