]> git.aero2k.de Git - tmp/jakarta-migration.git/commitdiff
CSPACE-5137: Modified Location validator handler to reflect repeatable term info...
authorAron Roberts <aron@socrates.berkeley.edu>
Mon, 7 May 2012 23:50:39 +0000 (16:50 -0700)
committerAron Roberts <aron@socrates.berkeley.edu>
Mon, 7 May 2012 23:50:39 +0000 (16:50 -0700)
services/location/service/src/main/java/org/collectionspace/services/location/nuxeo/LocationValidatorHandler.java
services/organization/service/src/main/java/org/collectionspace/services/organization/nuxeo/OrganizationValidatorHandler.java
services/person/service/src/main/java/org/collectionspace/services/person/nuxeo/PersonValidatorHandler.java
services/place/service/src/main/java/org/collectionspace/services/place/nuxeo/PlaceValidatorHandler.java

index 0c6345969f569e4b7f92c3f469b7d7a94ab31b38..ac1e52f0d1a108dc84aa8500f4a3e4f0e0452ae5 100644 (file)
@@ -6,7 +6,7 @@
  *  http://www.collectionspace.org
  *  http://wiki.collectionspace.org
 
- *  Copyright 2009 University of California at Berkeley
+ *  Copyright 2009 University of California, Berkeley
 
  *  Licensed under the Educational Community License (ECL), Version 2.0.
  *  You may not use this file except in compliance with this License.
@@ -37,7 +37,9 @@ import org.slf4j.LoggerFactory;
 /**
  * LocationValidatorHandler
  * 
- * Validates data supplied when attempting to create and/or update Location records.
+ * Performs validation when making requests related to Location records.
+ * As an example, you can modify this class to customize validation of
+ * payloads supplied in requests to create and/or update records.
  */
 public class LocationValidatorHandler extends ValidatorHandlerImpl {
 
index 524e2ee713b86add1c2bcef02121f18a9ba2f154..36b4acadd747b2784cd62111fc9bd1bdf5a353cb 100644 (file)
@@ -37,7 +37,9 @@ import org.slf4j.LoggerFactory;
 /**
  * OrganizationValidatorHandler
  * 
- * Validates data supplied when attempting to create and/or update Organization records.
+ * Performs validation when making requests related to Organization records.
+ * As an example, you can modify this class to customize validation of
+ * payloads supplied in requests to create and/or update records.
  */
 public class OrganizationValidatorHandler extends ValidatorHandlerImpl {
 
index 19ba74b1a56ac0bf38206c20c15b35050b0bd6fb..2066c5c1134ad1ee34453323b116ae5b2e94704c 100644 (file)
@@ -36,10 +36,9 @@ import org.slf4j.LoggerFactory;
 /**
  * PersonValidatorHandler
  *
- * Validates data supplied when attempting to create and/or update Person
- * records.
- *
- * $LastChangedRevision: $ $LastChangedDate: $
+ * Performs validation when making requests related to Person records.
+ * As an example, you can modify this class to customize validation of
+ * payloads supplied in requests to create and/or update records.
  */
 public class PersonValidatorHandler extends ValidatorHandlerImpl {
 
index bbe1f568c9187e19a2f6a8a0d9985d79b93cb296..d5c2c5d87eb207d47d5dceb24f30eecf9afcdf09 100644 (file)
@@ -6,7 +6,7 @@
  *  http://www.collectionspace.org
  *  http://wiki.collectionspace.org
 
- *  Copyright 2009 University of California at Berkeley
+ *  Copyright 2009 University of California, Berkeley
 
  *  Licensed under the Educational Community License (ECL), Version 2.0.
  *  You may not use this file except in compliance with this License.
  *  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.place.nuxeo;
 
+import java.util.List;
 import java.util.regex.Pattern;
-
-import org.collectionspace.services.place.PlacesCommon;
-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.api.Tools;
 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.place.PlaceTermGroup;
+import org.collectionspace.services.place.PlaceTermGroupList;
+import org.collectionspace.services.place.PlacesCommon;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 /**
- *
- * @author 
+ * PlaceValidatorHandler
+ * 
+ * Performs validation when making requests related to Place records.
+ * As an example, you can modify this class to customize validation of
+ * payloads supplied in requests to create and/or update records.
  */
-public class PlaceValidatorHandler implements ValidatorHandler {
+public class PlaceValidatorHandler extends ValidatorHandlerImpl {
 
     final Logger logger = LoggerFactory.getLogger(PlaceValidatorHandler.class);
-    private static final Pattern shortIdBadPattern = Pattern.compile("[\\W]"); //.matcher(input).matches()
+    // 'Bad pattern' for shortIdentifiers matches any non-word characters
+    private static final Pattern SHORT_ID_BAD_PATTERN = Pattern.compile("[\\W]"); //.matcher(input).matches()
+    private static final String VALIDATION_ERROR = "The record payload was invalid. See log file for more details.";
+    private static final String SHORT_ID_BAD_CHARS_ERROR =
+            "shortIdentifier must only contain standard word characters";
+    private static final String HAS_NO_TERMS_ERROR =
+            "Authority items must contain at least one term.";
+    private static final String HAS_AN_EMPTY_TERM_ERROR =
+            "Each term group in an authority item must contain "
+            + "a non-empty term name or "
+            + "a non-empty term display name.";
 
     @Override
-    public void validate(Action action, ServiceContext ctx)
-            throws InvalidDocumentException {
-        if(logger.isDebugEnabled()) {
-            logger.debug("validate() action=" + action.name());
-        }
+    protected Class getCommonPartClass() {
+        return PlacesCommon.class;
+    }
 
-        // Bail out if the validation action is for delete.
-        if (action.equals(Action.DELETE)) {
-               return;
-        }
-        
-        try {
-            MultipartServiceContext mctx = (MultipartServiceContext) ctx;
-            PlacesCommon place = (PlacesCommon) mctx.getInputPart(mctx.getCommonPartLabel(),
-                    PlacesCommon.class);
-            String msg = "";
-            boolean invalid = false;
-
-            // Validation occurring on both creates and updates
-            String displayName = place.getDisplayName();
-            if (!place.isDisplayNameComputed() && ((displayName == null) || displayName.trim().isEmpty())) {
-                invalid = true;
-                msg += "displayName must be non-null and non-blank if displayNameComputed is false";
+    @Override
+    protected void handleCreate() throws InvalidDocumentException {
+        PlacesCommon place = (PlacesCommon) getCommonPart();
+        // No guarantee that there is a common part in every post/update.
+        if (place != null) {
+            try {
+                String shortId = place.getShortIdentifier();
+                if (shortId != null) {
+                    CS_ASSERT(shortIdentifierContainsOnlyValidChars(shortId), SHORT_ID_BAD_CHARS_ERROR);
+                }
+                CS_ASSERT(containsAtLeastOneTerm(place), HAS_NO_TERMS_ERROR);
+                CS_ASSERT(allTermsContainNameOrDisplayName(place), HAS_AN_EMPTY_TERM_ERROR);
+            } catch (AssertionError e) {
+                if (logger.isErrorEnabled()) {
+                    logger.error(e.getMessage(), e);
+                }
+                throw new InvalidDocumentException(VALIDATION_ERROR, e);
             }
+        }
+    }
 
-            // Validation specific to creates or updates
-            if (action.equals(Action.CREATE)) {
-                String shortId = place.getShortIdentifier();
-                // Per CSPACE-2215, shortIdentifier values that are null (missing)
-                // oe the empty string are now legally accepted in create payloads.
-                // In either of those cases, a short identifier will be synthesized from
-                // a display name or supplied in another manner.
-                if ((shortId != null) && (shortIdBadPattern.matcher(shortId).find())) {
-                    invalid = true;
-                    msg += "shortIdentifier must only contain standard word characters";
+    @Override
+    protected void handleGet() throws InvalidDocumentException {
+    }
+
+    @Override
+    protected void handleGetAll() throws InvalidDocumentException {
+    }
+
+    @Override
+    protected void handleUpdate() throws InvalidDocumentException {
+        PlacesCommon place = (PlacesCommon) getCommonPart();
+        // No guarantee that there is a common part in every post/update.
+        if (place != null) {
+            try {
+                // shortIdentifier is among a set of fields that are
+                // prevented from being changed on an update, and thus
+                // we don't need to check its value here.
+                CS_ASSERT(containsAtLeastOneTerm(place), HAS_NO_TERMS_ERROR);
+                CS_ASSERT(allTermsContainNameOrDisplayName(place), HAS_AN_EMPTY_TERM_ERROR);
+            } catch (AssertionError e) {
+                if (logger.isErrorEnabled()) {
+                    logger.error(e.getMessage(), e);
                 }
-            } else if (action.equals(Action.UPDATE)) {
+                throw new InvalidDocumentException(VALIDATION_ERROR, e);
             }
+        }
+    }
 
-            if (invalid) {
-                logger.error(msg);
-                throw new InvalidDocumentException(msg);
+    @Override
+    protected void handleDelete() throws InvalidDocumentException {
+    }
+
+    private boolean shortIdentifierContainsOnlyValidChars(String shortId) {
+        // Check whether any characters match the 'bad' pattern
+        if (SHORT_ID_BAD_PATTERN.matcher(shortId).find()) {
+            return false;
+        }
+        return true;
+    }
+
+    private boolean containsAtLeastOneTerm(PlacesCommon place) {
+        PlaceTermGroupList termGroupList = place.getPlaceTermGroupList();
+        if (termGroupList == null) {
+            return false;
+        }
+        List<PlaceTermGroup> termGroups = termGroupList.getPlaceTermGroup();
+        if ((termGroups == null) || (termGroups.isEmpty())){ 
+            return false;
+        }
+        return true;
+    }
+
+    private boolean allTermsContainNameOrDisplayName(PlacesCommon place) {
+        PlaceTermGroupList termGroupList = place.getPlaceTermGroupList();
+        List<PlaceTermGroup> termGroups = termGroupList.getPlaceTermGroup();
+        for (PlaceTermGroup termGroup : termGroups) {
+            if (Tools.isBlank(termGroup.getTermName()) || Tools.isBlank(termGroup.getTermDisplayName())) {
+                return false;
             }
-        } catch (InvalidDocumentException ide) {
-            throw ide;
-        } catch (Exception e) {
-            throw new InvalidDocumentException(e);
         }
+        return true;
     }
 }
+