From f22f4e979dc6306d1332537b9060e04167c5aa50 Mon Sep 17 00:00:00 2001 From: Richard Millet Date: Fri, 7 Jan 2011 23:13:35 +0000 Subject: [PATCH] CSPACE-1133, CSPACE-1140: Updated Intake and CollectionObject validation handlers to use the new validation parent class. --- .../CollectionObjectValidatorHandler.java | 241 ++++++++++-------- .../intake/nuxeo/IntakeValidatorHandler.java | 60 ++++- .../nuxeo/RelationValidatorHandler.java | 10 +- 3 files changed, 193 insertions(+), 118 deletions(-) diff --git a/services/collectionobject/service/src/main/java/org/collectionspace/services/collectionobject/nuxeo/CollectionObjectValidatorHandler.java b/services/collectionobject/service/src/main/java/org/collectionspace/services/collectionobject/nuxeo/CollectionObjectValidatorHandler.java index 492de0df4..8d6db72cd 100644 --- a/services/collectionobject/service/src/main/java/org/collectionspace/services/collectionobject/nuxeo/CollectionObjectValidatorHandler.java +++ b/services/collectionobject/service/src/main/java/org/collectionspace/services/collectionobject/nuxeo/CollectionObjectValidatorHandler.java @@ -1,102 +1,139 @@ -/** - * This document is a part of the source code and related artifacts - * for CollectionSpace, an open source collections management system - * for museums and related institutions: - - * http://www.collectionspace.org - * http://wiki.collectionspace.org - - * Copyright 2009 University of California at Berkeley - - * Licensed under the Educational Community License (ECL), Version 2.0. - * You may not use this file except in compliance with this License. - - * You may obtain a copy of the ECL 2.0 License at - - * https://source.collectionspace.org/collection-space/LICENSE.txt - - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - *//** - * This document is a part of the source code and related artifacts - * for CollectionSpace, an open source collections management system - * for museums and related institutions: - - * http://www.collectionspace.org - * http://wiki.collectionspace.org - - * Copyright 2009 University of California at Berkeley - - * Licensed under the Educational Community License (ECL), Version 2.0. - * You may not use this file except in compliance with this License. - - * You may obtain a copy of the ECL 2.0 License at - - * https://source.collectionspace.org/collection-space/LICENSE.txt - - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -/* - * To change this template, choose Tools | Templates - * and open the template in the editor. - */ -package org.collectionspace.services.collectionobject.nuxeo; - -import org.collectionspace.services.collectionobject.CollectionobjectsCommon; -import org.collectionspace.services.common.context.MultipartServiceContext; -import org.collectionspace.services.common.context.ServiceContext; -import org.collectionspace.services.common.document.DocumentHandler.Action; -import org.collectionspace.services.common.document.InvalidDocumentException; -import org.collectionspace.services.common.document.ValidatorHandler; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * - * @author - */ -public class CollectionObjectValidatorHandler implements ValidatorHandler { - - final Logger logger = LoggerFactory.getLogger(CollectionObjectValidatorHandler.class); - - @Override - public void validate(Action action, ServiceContext ctx) - throws InvalidDocumentException { - if(logger.isDebugEnabled()) { - logger.debug("validate() action=" + action.name()); - } - try { - MultipartServiceContext mctx = (MultipartServiceContext) ctx; - CollectionobjectsCommon co = (CollectionobjectsCommon) mctx.getInputPart(mctx.getCommonPartLabel(), - CollectionobjectsCommon.class); - StringBuilder msgBldr = new StringBuilder("validate()"); - boolean invalid = false; - if (co.getObjectNumber() == null || co.getObjectNumber().isEmpty()) { - invalid = true; - msgBldr.append("\nobjectNumber : missing"); - } - if(action.equals(Action.CREATE)) { - //create specific validation here - } else if(action.equals(Action.UPDATE)) { - //update specific validation here - } - - if (invalid) { - String msg = msgBldr.toString(); - logger.error(msg); - throw new InvalidDocumentException(msg); - } - } catch (InvalidDocumentException ide) { - throw ide; - } catch (Exception e) { - throw new InvalidDocumentException(e); - } - } -} +/** + * This document is a part of the source code and related artifacts + * for CollectionSpace, an open source collections management system + * for museums and related institutions: + + * http://www.collectionspace.org + * http://wiki.collectionspace.org + + * Copyright 2009 University of California at Berkeley + + * Licensed under the Educational Community License (ECL), Version 2.0. + * You may not use this file except in compliance with this License. + + * You may obtain a copy of the ECL 2.0 License at + + * https://source.collectionspace.org/collection-space/LICENSE.txt + + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *//** + * This document is a part of the source code and related artifacts + * for CollectionSpace, an open source collections management system + * for museums and related institutions: + + * http://www.collectionspace.org + * http://wiki.collectionspace.org + + * Copyright 2009 University of California at Berkeley + + * Licensed under the Educational Community License (ECL), Version 2.0. + * You may not use this file except in compliance with this License. + + * You may obtain a copy of the ECL 2.0 License at + + * https://source.collectionspace.org/collection-space/LICENSE.txt + + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package org.collectionspace.services.collectionobject.nuxeo; + +import org.collectionspace.services.collectionobject.CollectionobjectsCommon; +//import org.collectionspace.services.common.context.MultipartServiceContext; +//import org.collectionspace.services.common.context.ServiceContext; +import org.collectionspace.services.common.document.InvalidDocumentException; +import org.collectionspace.services.common.document.ValidatorHandlerImpl; + +import org.jboss.resteasy.plugins.providers.multipart.MultipartInput; +import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * + * @author + */ +public class CollectionObjectValidatorHandler extends ValidatorHandlerImpl { + + final Logger logger = LoggerFactory.getLogger(CollectionObjectValidatorHandler.class); + + // + // Error Strings + // + private static final String VALIDATION_ERROR = "The collection object record payload was invalid. See log file for more details."; + private static final String OBJECTNUMBER_NULL_ERROR = "The collection object field \"objectNumber\" cannot be empty or missing."; + + @Override + protected Class getCommonPartClass() { + return CollectionobjectsCommon.class; + } + + @Override + protected void handleGet(){ + // TODO Auto-generated method stub + + } + + @Override + protected void handleGetAll() { + // TODO Auto-generated method stub + + } + + @Override + protected void handleUpdate() + throws InvalidDocumentException { + try { + CollectionobjectsCommon co = (CollectionobjectsCommon) getCommonPart(); + validateCollectionobjectsCommon(co); + } catch (AssertionError e) { + if (logger.isErrorEnabled() == true) { + logger.error(e.getMessage(), e); + } + throw new InvalidDocumentException(VALIDATION_ERROR, e); + } + } + + @Override + protected void handleDelete() { + // TODO Auto-generated method stub + + } + + @Override + protected void handleCreate() + throws InvalidDocumentException { + try { + CollectionobjectsCommon co = (CollectionobjectsCommon) getCommonPart(); + validateCollectionobjectsCommon(co); + } catch (AssertionError e) { + if (logger.isErrorEnabled() == true) { + logger.error(e.getMessage(), e); + } + throw new InvalidDocumentException(VALIDATION_ERROR, e); + } + } + + // + // Private Methods + // + private void validateCollectionobjectsCommon(CollectionobjectsCommon co) throws AssertionError { + assert(co != null); + String objectNumber = co.getObjectNumber(); + assert(objectNumber != null); + assert(objectNumber.isEmpty() == false) : OBJECTNUMBER_NULL_ERROR; + + } +} diff --git a/services/intake/service/src/main/java/org/collectionspace/services/intake/nuxeo/IntakeValidatorHandler.java b/services/intake/service/src/main/java/org/collectionspace/services/intake/nuxeo/IntakeValidatorHandler.java index 7656ef1e3..d03640444 100644 --- a/services/intake/service/src/main/java/org/collectionspace/services/intake/nuxeo/IntakeValidatorHandler.java +++ b/services/intake/service/src/main/java/org/collectionspace/services/intake/nuxeo/IntakeValidatorHandler.java @@ -1,18 +1,64 @@ package org.collectionspace.services.intake.nuxeo; -import org.collectionspace.services.common.context.ServiceContext; import org.collectionspace.services.common.document.InvalidDocumentException; -import org.collectionspace.services.common.document.ValidatorHandler; -import org.collectionspace.services.common.document.DocumentHandler.Action; +import org.collectionspace.services.common.document.ValidatorHandlerImpl; +import org.collectionspace.services.intake.IntakesCommon; -public class IntakeValidatorHandler implements ValidatorHandler { +import org.jboss.resteasy.plugins.providers.multipart.MultipartInput; +import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class IntakeValidatorHandler extends ValidatorHandlerImpl { + + /** The logger. */ + private final Logger logger = LoggerFactory.getLogger(IntakeValidatorHandler.class); + + /** Error Messages **/ + private static final String VALIDATION_ERROR = "The intake record payload was invalid. See log file for more details."; + + + @Override + protected Class getCommonPartClass() { + return IntakesCommon.class; + } + + @Override + protected void handleCreate() throws InvalidDocumentException { + try { + IntakesCommon intakesCommon = (IntakesCommon)getCommonPart(); + assert(intakesCommon != null); + } catch (AssertionError e) { + if (logger.isErrorEnabled() == true) { + logger.error(e.getMessage(), e); + } + throw new InvalidDocumentException(VALIDATION_ERROR, e); + } + } + + @Override + protected void handleGet() throws InvalidDocumentException { + // TODO Auto-generated method stub + + } + + @Override + protected void handleGetAll() throws InvalidDocumentException { + // TODO Auto-generated method stub + + } @Override - public void validate(Action action, ServiceContext ctx) - throws InvalidDocumentException { + protected void handleUpdate() throws InvalidDocumentException { // TODO Auto-generated method stub - System.out.println("IntakeValidatorHandler executed."); + + } + @Override + protected void handleDelete() throws InvalidDocumentException { + // TODO Auto-generated method stub + } } diff --git a/services/relation/service/src/main/java/org/collectionspace/services/relation/nuxeo/RelationValidatorHandler.java b/services/relation/service/src/main/java/org/collectionspace/services/relation/nuxeo/RelationValidatorHandler.java index 1299076a2..01fc0228f 100644 --- a/services/relation/service/src/main/java/org/collectionspace/services/relation/nuxeo/RelationValidatorHandler.java +++ b/services/relation/service/src/main/java/org/collectionspace/services/relation/nuxeo/RelationValidatorHandler.java @@ -2,15 +2,8 @@ package org.collectionspace.services.relation.nuxeo; //import junit.framework.Assert; -import org.collectionspace.services.common.context.MultipartServiceContext; -import org.collectionspace.services.common.context.ServiceContext; - import org.collectionspace.services.common.document.InvalidDocumentException; -import org.collectionspace.services.common.document.ValidatorHandler; import org.collectionspace.services.common.document.ValidatorHandlerImpl; -import org.collectionspace.services.common.document.DocumentHandler.Action; - -import org.collectionspace.services.nuxeo.client.java.RemoteDocumentModelHandlerImpl; import org.collectionspace.services.relation.RelationsCommon; import org.jboss.resteasy.plugins.providers.multipart.MultipartInput; @@ -27,7 +20,7 @@ public class RelationValidatorHandler extends ValidatorHandlerImpl ctx = getServiceContext(); try { RelationsCommon relationsCommon = (RelationsCommon)getCommonPart(); assert(relationsCommon != null); -- 2.47.3