syncope-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jbernha...@apache.org
Subject svn commit: r1416823 [2/2] - in /syncope/branches/cxf/common: ./ src/main/java/org/apache/syncope/ src/main/java/org/apache/syncope/annotation/ src/main/java/org/apache/syncope/controller/ src/main/java/org/apache/syncope/exceptions/ src/main/java/org/...
Date Tue, 04 Dec 2012 08:55:11 GMT
Modified: syncope/branches/cxf/common/src/main/java/org/apache/syncope/util/AttributableOperations.java
URL: http://svn.apache.org/viewvc/syncope/branches/cxf/common/src/main/java/org/apache/syncope/util/AttributableOperations.java?rev=1416823&r1=1416822&r2=1416823&view=diff
==============================================================================
--- syncope/branches/cxf/common/src/main/java/org/apache/syncope/util/AttributableOperations.java
(original)
+++ syncope/branches/cxf/common/src/main/java/org/apache/syncope/util/AttributableOperations.java
Tue Dec  4 08:55:06 2012
@@ -25,6 +25,7 @@ import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+
 import org.apache.commons.lang.SerializationUtils;
 import org.apache.syncope.mod.AbstractAttributableMod;
 import org.apache.syncope.mod.AttributeMod;
@@ -39,7 +40,8 @@ import org.apache.syncope.to.RoleTO;
 import org.apache.syncope.to.UserTO;
 
 /**
- * Utility class for manipulating classes extending AbstractAttributableTO and AbstractAttributableMod.
+ * Utility class for manipulating classes extending AbstractAttributableTO and
+ * AbstractAttributableMod.
  *
  * @see AbstractAttributableTO
  * @see AbstractAttributableMod
@@ -49,6 +51,7 @@ public final class AttributableOperation
     private AttributableOperations() {
     }
 
+    @SuppressWarnings("unchecked")
     public static <T extends AbstractAttributableTO> T clone(final T original) {
         return (T) SerializationUtils.clone(original);
     }
@@ -59,7 +62,8 @@ public final class AttributableOperation
     }
 
     private static void populate(final Map<String, AttributeTO> updatedAttrs,
-            final Map<String, AttributeTO> originalAttrs, final AbstractAttributableMod
result, final boolean virtuals) {
+            final Map<String, AttributeTO> originalAttrs, final AbstractAttributableMod
result,
+            final boolean virtuals) {
 
         for (Map.Entry<String, AttributeTO> entry : updatedAttrs.entrySet()) {
             AttributeMod mod = new AttributeMod();
@@ -99,11 +103,8 @@ public final class AttributableOperation
         }
     }
 
-    private static void diff(
-            final AbstractAttributableTO updated,
-            final AbstractAttributableTO original,
-            final AbstractAttributableMod result,
-            final boolean incremental) {
+    private static void diff(final AbstractAttributableTO updated, final AbstractAttributableTO
original,
+            final AbstractAttributableMod result, final boolean incremental) {
 
         // 1. check same id
         if (updated.getId() != original.getId()) {
@@ -181,8 +182,10 @@ public final class AttributableOperation
     /**
      * Calculate modifications needed by first in order to be equal to second.
      *
-     * @param updated updated UserTO
-     * @param original original UserTO
+     * @param updated
+     *            updated UserTO
+     * @param original
+     *            original UserTO
      * @return UserMod containing differences
      */
     public static UserMod diff(final UserTO updated, final UserTO original) {
@@ -192,9 +195,12 @@ public final class AttributableOperation
     /**
      * Calculate modifications needed by first in order to be equal to second.
      *
-     * @param updated updated UserTO
-     * @param original original UserTO
-     * @param incremental perform incremental diff (without removing existing info)
+     * @param updated
+     *            updated UserTO
+     * @param original
+     *            original UserTO
+     * @param incremental
+     *            perform incremental diff (without removing existing info)
      * @return UserMod containing differences
      */
     public static UserMod diff(final UserTO updated, final UserTO original, boolean incremental)
{
@@ -273,8 +279,10 @@ public final class AttributableOperation
     /**
      * Calculate modifications needed by first in order to be equal to second.
      *
-     * @param updated updated RoleTO
-     * @param original original RoleTO
+     * @param updated
+     *            updated RoleTO
+     * @param original
+     *            original RoleTO
      * @return RoleMod containing differences
      */
     public static RoleMod diff(final RoleTO updated, final RoleTO original) {
@@ -300,13 +308,13 @@ public final class AttributableOperation
         }
 
         // 4. entitlements
-        Set<String> updatedEnts = new HashSet<String>(updated.getEntitlements());
-        Set<String> originalEnts = new HashSet<String>(original.getEntitlements());
-        if (updatedEnts.equals(originalEnts)) {
-            result.setEntitlements(null);
-        } else {
-            result.setEntitlements(updated.getEntitlements());
-        }
+        Set<String> updatedEnts = new HashSet<String>(updated.getEntitlementList());
+        Set<String> originalEnts = new HashSet<String>(original.getEntitlementList());
+        originalEnts.removeAll(updatedEnts);
+        result.setEntitlementsToBeRemoved(originalEnts);
+        originalEnts = new HashSet<String>(original.getEntitlementList());
+        updatedEnts.removeAll(originalEnts);
+        result.setEntitlementsToBeAdded(updatedEnts);
 
         // 5. owner
         result.setUserOwner(new ReferenceMod(updated.getUserOwner()));
@@ -338,8 +346,8 @@ public final class AttributableOperation
         return new ArrayList<AttributeTO>(attrs.values());
     }
 
-    private static <T extends AbstractAttributableTO, K extends AbstractAttributableMod>
void apply(final T to,
-            final K mod, final T result) {
+    private static <T extends AbstractAttributableTO, K extends AbstractAttributableMod>
void apply(
+            final T to, final K mod, final T result) {
 
         // 1. check same id
         if (to.getId() != mod.getId()) {
@@ -347,8 +355,8 @@ public final class AttributableOperation
         }
 
         // 2. attributes
-        result.setAttributes(getUpdateValues(to.getAttributeMap(), mod.getAttributesToBeRemoved(),
mod.
-                getAttributesToBeUpdated()));
+        result.setAttributes(getUpdateValues(to.getAttributeMap(), mod.getAttributesToBeRemoved(),
+                mod.getAttributesToBeUpdated()));
 
         // 3. derived attributes
         Map<String, AttributeTO> attrs = to.getDerivedAttributeMap();
@@ -364,8 +372,8 @@ public final class AttributableOperation
         result.setDerivedAttributes(new ArrayList<AttributeTO>(attrs.values()));
 
         // 4. virtual attributes
-        result.setVirtualAttributes(getUpdateValues(to.getVirtualAttributeMap(), mod.getVirtualAttributesToBeRemoved(),
-                mod.getVirtualAttributesToBeUpdated()));
+        result.setVirtualAttributes(getUpdateValues(to.getVirtualAttributeMap(),
+                mod.getVirtualAttributesToBeRemoved(), mod.getVirtualAttributesToBeUpdated()));
 
         // 5. resources
         result.getResources().removeAll(mod.getResourcesToBeRemoved());

Added: syncope/branches/cxf/common/src/main/java/org/apache/syncope/validation/InvalidEntityException.java
URL: http://svn.apache.org/viewvc/syncope/branches/cxf/common/src/main/java/org/apache/syncope/validation/InvalidEntityException.java?rev=1416823&view=auto
==============================================================================
--- syncope/branches/cxf/common/src/main/java/org/apache/syncope/validation/InvalidEntityException.java
(added)
+++ syncope/branches/cxf/common/src/main/java/org/apache/syncope/validation/InvalidEntityException.java
Tue Dec  4 08:55:06 2012
@@ -0,0 +1,92 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+package org.apache.syncope.validation;
+
+import java.util.EnumSet;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+import javax.validation.ConstraintViolation;
+import javax.validation.ValidationException;
+import org.apache.syncope.types.EntityViolationType;
+
+public class InvalidEntityException extends ValidationException {
+
+    private static final long serialVersionUID = 3249297275444409691L;
+
+    private String entityClassSimpleName;
+
+    private final Map<Class, Set<EntityViolationType>> violations;
+
+    public InvalidEntityException(final String entityClassSimpleName, final Set<ConstraintViolation<Object>>
violations) {
+
+        super();
+
+        this.entityClassSimpleName = entityClassSimpleName;
+
+        this.violations = new HashMap<Class, Set<EntityViolationType>>();
+        EntityViolationType entityViolationType;
+        for (ConstraintViolation<Object> violation : violations) {
+            try {
+                entityViolationType = EntityViolationType.valueOf(violation.getMessageTemplate());
+            } catch (IllegalArgumentException e) {
+                entityViolationType = EntityViolationType.Standard;
+                entityViolationType.setMessageTemplate(violation.getPropertyPath() + ": "
+ violation.getMessage());
+            }
+
+            if (!this.violations.containsKey(violation.getLeafBean().getClass())) {
+
+                this.violations.put(violation.getLeafBean().getClass(), EnumSet.noneOf(EntityViolationType.class));
+            }
+
+            this.violations.get(violation.getLeafBean().getClass()).add(entityViolationType);
+        }
+    }
+
+    public final boolean hasViolation(final EntityViolationType type) {
+        boolean found = false;
+        for (Class entity : violations.keySet()) {
+            if (violations.get(entity).contains(type)) {
+                found = true;
+            }
+        }
+
+        return found;
+    }
+
+    public String getEntityClassSimpleName() {
+        return entityClassSimpleName;
+    }
+
+    public final Map<Class, Set<EntityViolationType>> getViolations() {
+        return violations;
+    }
+
+    @Override
+    public String getMessage() {
+        StringBuilder sb = new StringBuilder();
+
+        for (Class entity : violations.keySet()) {
+            sb.append(entity.getSimpleName()).append(" ").append(violations.get(entity).toString()).append(",
");
+        }
+        sb.delete(sb.lastIndexOf(", "), sb.length());
+
+        return sb.toString();
+    }
+}



Mime
View raw message