syncope-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ilgro...@apache.org
Subject [2/3] syncope git commit: Internal JPA refactoring: removing Policy#getType
Date Mon, 28 Mar 2016 09:58:37 GMT
Internal JPA refactoring: removing Policy#getType


Project: http://git-wip-us.apache.org/repos/asf/syncope/repo
Commit: http://git-wip-us.apache.org/repos/asf/syncope/commit/31fea77b
Tree: http://git-wip-us.apache.org/repos/asf/syncope/tree/31fea77b
Diff: http://git-wip-us.apache.org/repos/asf/syncope/diff/31fea77b

Branch: refs/heads/master
Commit: 31fea77bf445480af866ead354ceb4b72576dd9b
Parents: b2e07eb
Author: Francesco Chicchiriccò <ilgrosso@apache.org>
Authored: Mon Mar 28 10:08:24 2016 +0200
Committer: Francesco Chicchiriccò <ilgrosso@apache.org>
Committed: Mon Mar 28 10:08:24 2016 +0200

----------------------------------------------------------------------
 .../commands/policy/PolicyResultManager.java    |  33 +--
 .../common/lib/policy/AbstractPolicyTO.java     |  17 --
 .../common/lib/policy/AccountPolicyTO.java      |   5 -
 .../common/lib/policy/PasswordPolicyTO.java     |   5 -
 .../syncope/common/lib/policy/PolicyType.java   |  43 ----
 .../syncope/common/lib/policy/PullPolicyTO.java |   5 -
 .../syncope/common/lib/types/PolicyType.java    |  43 ++++
 .../apache/syncope/core/logic/PolicyLogic.java  |  51 ++---
 .../apache/syncope/core/logic/SyncopeLogic.java |   7 +-
 .../api/dao/ExternalResourceDAO.java            |   3 +-
 .../core/persistence/api/dao/PolicyDAO.java     |   3 +-
 .../core/persistence/api/entity/Policy.java     |   4 -
 .../jpa/dao/JPAExternalResourceDAO.java         |  32 ++-
 .../persistence/jpa/dao/JPAPlainAttrDAO.java    |   4 +-
 .../jpa/dao/JPAPlainAttrValueDAO.java           |   6 +-
 .../persistence/jpa/dao/JPAPlainSchemaDAO.java  |   2 +-
 .../core/persistence/jpa/dao/JPAPolicyDAO.java  |  40 ++--
 .../core/persistence/jpa/dao/JPARealmDAO.java   |   9 +-
 .../jpa/entity/policy/AbstractPolicy.java       |  12 -
 .../jpa/entity/policy/JPAAccountPolicy.java     |   6 -
 .../jpa/entity/policy/JPAPasswordPolicy.java    |   6 -
 .../jpa/entity/policy/JPAPullPolicy.java        |   6 -
 .../jpa/entity/policy/JPAPushPolicy.java        |   6 -
 .../core/persistence/jpa/inner/PolicyTest.java  |   5 +-
 .../test/resources/domains/MasterContent.xml    |  24 +-
 .../provisioning/api/data/PolicyDataBinder.java |   4 +-
 .../java/data/PolicyDataBinderImpl.java         | 225 +++++++++----------
 .../rest/cxf/service/PolicyServiceImpl.java     |  20 +-
 .../apache/syncope/fit/core/PolicyITCase.java   |   2 -
 29 files changed, 250 insertions(+), 378 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/syncope/blob/31fea77b/client/cli/src/main/java/org/apache/syncope/client/cli/commands/policy/PolicyResultManager.java
----------------------------------------------------------------------
diff --git a/client/cli/src/main/java/org/apache/syncope/client/cli/commands/policy/PolicyResultManager.java b/client/cli/src/main/java/org/apache/syncope/client/cli/commands/policy/PolicyResultManager.java
index d0432f3..e933128 100644
--- a/client/cli/src/main/java/org/apache/syncope/client/cli/commands/policy/PolicyResultManager.java
+++ b/client/cli/src/main/java/org/apache/syncope/client/cli/commands/policy/PolicyResultManager.java
@@ -32,27 +32,14 @@ public class PolicyResultManager extends CommonsResultManager {
     public void printPolicies(final LinkedList<AbstractPolicyTO> policyTOs) {
         System.out.println("");
         for (AbstractPolicyTO policyTO : policyTOs) {
-            if (!policyTOs.isEmpty()) {
-                final PolicyType policyType = policyTO.getType();
-                switch (policyType) {
-                    case ACCOUNT:
-                        printAccountPolicy((AccountPolicyTO) policyTO);
-                        break;
-                    case PASSWORD:
-                        printPasswordPolicy((PasswordPolicyTO) policyTO);
-                        break;
-                    case PUSH:
-                        System.out.println(policyTO);
-                        break;
-                    case PULL:
-                        printPullPolicy((PullPolicyTO) policyTO);
-                        break;
-                    default:
-                        break;
-                }
+            if (policyTO instanceof AccountPolicyTO) {
+                printAccountPolicy((AccountPolicyTO) policyTO);
+            } else if (policyTO instanceof PasswordPolicyTO) {
+                printPasswordPolicy((PasswordPolicyTO) policyTO);
+            } else if (policyTO instanceof PullPolicyTO) {
+                printPullPolicy((PullPolicyTO) policyTO);
             }
         }
-
     }
 
     public void printPoliciesByType(final String policyTypeString, final LinkedList<AbstractPolicyTO> policyTOs) {
@@ -86,7 +73,7 @@ public class PolicyResultManager extends CommonsResultManager {
 
     public void printAccountPolicy(final AccountPolicyTO policyTO) {
         System.out.println(" > KEY: " + policyTO.getKey());
-        System.out.println("    type: " + policyTO.getType().name());
+        System.out.println("    type: " + policyTO.getClass().getSimpleName());
         System.out.println("    description: " + policyTO.getDescription());
         System.out.println("    resources : " + policyTO.getUsedByResources().toString());
         System.out.println("    realms : " + policyTO.getUsedByRealms().toString());
@@ -99,20 +86,20 @@ public class PolicyResultManager extends CommonsResultManager {
 
     public void printPasswordPolicy(final PasswordPolicyTO policyTO) {
         System.out.println(" > KEY: " + policyTO.getKey());
-        System.out.println("    type: " + policyTO.getType().name());
+        System.out.println("    type: " + policyTO.getClass().getSimpleName());
         System.out.println("    description: " + policyTO.getDescription());
         System.out.println("    resources : " + policyTO.getUsedByResources().toString());
         System.out.println("    realms : " + policyTO.getUsedByRealms().toString());
         System.out.println("    history lenght : " + policyTO.getHistoryLength());
         System.out.println("    allow null password : " + policyTO.isAllowNullPassword());
         System.out.println("    RULES : ");
-        System.out.println("       > class : " + ((PasswordPolicyTO) policyTO).getRuleConfs());
+        System.out.println("       > class : " + policyTO.getRuleConfs());
         System.out.println("");
     }
 
     public void printPullPolicy(final PullPolicyTO policyTO) {
         System.out.println(" > KEY: " + policyTO.getKey());
-        System.out.println("    type: " + policyTO.getType().name());
+        System.out.println("    type: " + policyTO.getClass().getSimpleName());
         System.out.println("    description: " + policyTO.getDescription());
         System.out.println("    resources : " + policyTO.getUsedByResources().toString());
         System.out.println("    realms : " + policyTO.getUsedByRealms().toString());

http://git-wip-us.apache.org/repos/asf/syncope/blob/31fea77b/common/lib/src/main/java/org/apache/syncope/common/lib/policy/AbstractPolicyTO.java
----------------------------------------------------------------------
diff --git a/common/lib/src/main/java/org/apache/syncope/common/lib/policy/AbstractPolicyTO.java b/common/lib/src/main/java/org/apache/syncope/common/lib/policy/AbstractPolicyTO.java
index 34f824f..69564ac 100644
--- a/common/lib/src/main/java/org/apache/syncope/common/lib/policy/AbstractPolicyTO.java
+++ b/common/lib/src/main/java/org/apache/syncope/common/lib/policy/AbstractPolicyTO.java
@@ -29,7 +29,6 @@ import javax.xml.bind.annotation.XmlRootElement;
 import javax.xml.bind.annotation.XmlSeeAlso;
 import javax.xml.bind.annotation.XmlType;
 import org.apache.syncope.common.lib.AbstractBaseBean;
-import org.apache.syncope.common.lib.types.PolicyType;
 
 @XmlRootElement(name = "abstractPolicy")
 @XmlType
@@ -43,22 +42,10 @@ public abstract class AbstractPolicyTO extends AbstractBaseBean {
 
     private String description;
 
-    private final PolicyType type;
-
     private final List<String> usedByResources = new ArrayList<>();
 
     private final List<String> usedByRealms = new ArrayList<>();
 
-    private AbstractPolicyTO() {
-        super();
-        throw new UnsupportedOperationException("No-arg constructor is just to keep JAXB from complaining");
-    }
-
-    protected AbstractPolicyTO(final PolicyType type) {
-        super();
-        this.type = type;
-    }
-
     public long getKey() {
         return key;
     }
@@ -76,10 +63,6 @@ public abstract class AbstractPolicyTO extends AbstractBaseBean {
         this.description = description;
     }
 
-    public PolicyType getType() {
-        return type;
-    }
-
     @XmlElementWrapper(name = "usedByResources")
     @XmlElement(name = "resource")
     @JsonProperty("usedByResources")

http://git-wip-us.apache.org/repos/asf/syncope/blob/31fea77b/common/lib/src/main/java/org/apache/syncope/common/lib/policy/AccountPolicyTO.java
----------------------------------------------------------------------
diff --git a/common/lib/src/main/java/org/apache/syncope/common/lib/policy/AccountPolicyTO.java b/common/lib/src/main/java/org/apache/syncope/common/lib/policy/AccountPolicyTO.java
index b1e1049..63b78a9 100644
--- a/common/lib/src/main/java/org/apache/syncope/common/lib/policy/AccountPolicyTO.java
+++ b/common/lib/src/main/java/org/apache/syncope/common/lib/policy/AccountPolicyTO.java
@@ -25,7 +25,6 @@ import javax.xml.bind.annotation.XmlElement;
 import javax.xml.bind.annotation.XmlElementWrapper;
 import javax.xml.bind.annotation.XmlRootElement;
 import javax.xml.bind.annotation.XmlType;
-import org.apache.syncope.common.lib.types.PolicyType;
 
 @XmlRootElement(name = "accountPolicy")
 @XmlType
@@ -41,10 +40,6 @@ public class AccountPolicyTO extends AbstractPolicyTO {
 
     private final List<String> resources = new ArrayList<>();
 
-    public AccountPolicyTO() {
-        super(PolicyType.ACCOUNT);
-    }
-
     public boolean isPropagateSuspension() {
         return propagateSuspension;
     }

http://git-wip-us.apache.org/repos/asf/syncope/blob/31fea77b/common/lib/src/main/java/org/apache/syncope/common/lib/policy/PasswordPolicyTO.java
----------------------------------------------------------------------
diff --git a/common/lib/src/main/java/org/apache/syncope/common/lib/policy/PasswordPolicyTO.java b/common/lib/src/main/java/org/apache/syncope/common/lib/policy/PasswordPolicyTO.java
index 49c73a3..20f238a 100644
--- a/common/lib/src/main/java/org/apache/syncope/common/lib/policy/PasswordPolicyTO.java
+++ b/common/lib/src/main/java/org/apache/syncope/common/lib/policy/PasswordPolicyTO.java
@@ -25,7 +25,6 @@ import javax.xml.bind.annotation.XmlElement;
 import javax.xml.bind.annotation.XmlElementWrapper;
 import javax.xml.bind.annotation.XmlRootElement;
 import javax.xml.bind.annotation.XmlType;
-import org.apache.syncope.common.lib.types.PolicyType;
 
 @XmlRootElement(name = "passwordPolicy")
 @XmlType
@@ -39,10 +38,6 @@ public class PasswordPolicyTO extends AbstractPolicyTO {
 
     private final List<AbstractPasswordRuleConf> ruleConfs = new ArrayList<>();
 
-    public PasswordPolicyTO() {
-        super(PolicyType.PASSWORD);
-    }
-
     public boolean isAllowNullPassword() {
         return allowNullPassword;
     }

http://git-wip-us.apache.org/repos/asf/syncope/blob/31fea77b/common/lib/src/main/java/org/apache/syncope/common/lib/policy/PolicyType.java
----------------------------------------------------------------------
diff --git a/common/lib/src/main/java/org/apache/syncope/common/lib/policy/PolicyType.java b/common/lib/src/main/java/org/apache/syncope/common/lib/policy/PolicyType.java
deleted file mode 100644
index ea429c2..0000000
--- a/common/lib/src/main/java/org/apache/syncope/common/lib/policy/PolicyType.java
+++ /dev/null
@@ -1,43 +0,0 @@
-/*
- * 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.common.lib.types;
-
-import javax.xml.bind.annotation.XmlEnum;
-
-@XmlEnum
-public enum PolicyType {
-
-    /**
-     * How username values should look like.
-     */
-    ACCOUNT,
-    /**
-     * How password values should look like.
-     */
-    PASSWORD,
-    /**
-     * For handling conflicts resolution during pull.
-     */
-    PULL,
-    /**
-     * For handling conflicts resolution during push.
-     */
-    PUSH;
-
-}

http://git-wip-us.apache.org/repos/asf/syncope/blob/31fea77b/common/lib/src/main/java/org/apache/syncope/common/lib/policy/PullPolicyTO.java
----------------------------------------------------------------------
diff --git a/common/lib/src/main/java/org/apache/syncope/common/lib/policy/PullPolicyTO.java b/common/lib/src/main/java/org/apache/syncope/common/lib/policy/PullPolicyTO.java
index 91c8e8b..2618e43 100644
--- a/common/lib/src/main/java/org/apache/syncope/common/lib/policy/PullPolicyTO.java
+++ b/common/lib/src/main/java/org/apache/syncope/common/lib/policy/PullPolicyTO.java
@@ -20,7 +20,6 @@ package org.apache.syncope.common.lib.policy;
 
 import javax.xml.bind.annotation.XmlRootElement;
 import javax.xml.bind.annotation.XmlType;
-import org.apache.syncope.common.lib.types.PolicyType;
 
 @XmlRootElement(name = "pullPolicy")
 @XmlType
@@ -30,10 +29,6 @@ public class PullPolicyTO extends AbstractPolicyTO {
 
     private PullPolicySpec specification;
 
-    public PullPolicyTO() {
-        super(PolicyType.PULL);
-    }
-
     public PullPolicySpec getSpecification() {
         return specification;
     }

http://git-wip-us.apache.org/repos/asf/syncope/blob/31fea77b/common/lib/src/main/java/org/apache/syncope/common/lib/types/PolicyType.java
----------------------------------------------------------------------
diff --git a/common/lib/src/main/java/org/apache/syncope/common/lib/types/PolicyType.java b/common/lib/src/main/java/org/apache/syncope/common/lib/types/PolicyType.java
new file mode 100644
index 0000000..ea429c2
--- /dev/null
+++ b/common/lib/src/main/java/org/apache/syncope/common/lib/types/PolicyType.java
@@ -0,0 +1,43 @@
+/*
+ * 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.common.lib.types;
+
+import javax.xml.bind.annotation.XmlEnum;
+
+@XmlEnum
+public enum PolicyType {
+
+    /**
+     * How username values should look like.
+     */
+    ACCOUNT,
+    /**
+     * How password values should look like.
+     */
+    PASSWORD,
+    /**
+     * For handling conflicts resolution during pull.
+     */
+    PULL,
+    /**
+     * For handling conflicts resolution during push.
+     */
+    PUSH;
+
+}

http://git-wip-us.apache.org/repos/asf/syncope/blob/31fea77b/core/logic/src/main/java/org/apache/syncope/core/logic/PolicyLogic.java
----------------------------------------------------------------------
diff --git a/core/logic/src/main/java/org/apache/syncope/core/logic/PolicyLogic.java b/core/logic/src/main/java/org/apache/syncope/core/logic/PolicyLogic.java
index 76c0d66..7452138 100644
--- a/core/logic/src/main/java/org/apache/syncope/core/logic/PolicyLogic.java
+++ b/core/logic/src/main/java/org/apache/syncope/core/logic/PolicyLogic.java
@@ -25,9 +25,6 @@ import org.apache.commons.collections4.CollectionUtils;
 import org.apache.commons.collections4.Transformer;
 import org.apache.commons.lang3.ArrayUtils;
 import org.apache.syncope.common.lib.policy.AbstractPolicyTO;
-import org.apache.syncope.common.lib.policy.AccountPolicyTO;
-import org.apache.syncope.common.lib.policy.PasswordPolicyTO;
-import org.apache.syncope.common.lib.policy.PullPolicyTO;
 import org.apache.syncope.common.lib.types.PolicyType;
 import org.apache.syncope.common.lib.types.StandardEntitlement;
 import org.apache.syncope.core.persistence.api.dao.NotFoundException;
@@ -35,11 +32,12 @@ import org.apache.syncope.core.persistence.api.dao.PolicyDAO;
 import org.apache.syncope.core.persistence.api.entity.policy.AccountPolicy;
 import org.apache.syncope.core.persistence.api.entity.policy.PasswordPolicy;
 import org.apache.syncope.core.persistence.api.entity.Policy;
+import org.apache.syncope.core.persistence.api.entity.policy.PullPolicy;
+import org.apache.syncope.core.persistence.api.entity.policy.PushPolicy;
 import org.apache.syncope.core.provisioning.api.data.PolicyDataBinder;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.security.access.prepost.PreAuthorize;
 import org.springframework.stereotype.Component;
-import org.apache.syncope.core.persistence.api.entity.policy.PullPolicy;
 
 @Component
 public class PolicyLogic extends AbstractTransactionalLogic<AbstractPolicyTO> {
@@ -52,48 +50,35 @@ public class PolicyLogic extends AbstractTransactionalLogic<AbstractPolicyTO> {
 
     @PreAuthorize("hasRole('" + StandardEntitlement.POLICY_CREATE + "')")
     public <T extends AbstractPolicyTO> T create(final T policyTO) {
-        return binder.getPolicyTO(policyDAO.save(binder.getPolicy(null, policyTO)));
-    }
-
-    private <T extends AbstractPolicyTO, K extends Policy> T update(final T policyTO, final K policy) {
-        binder.getPolicy(policy, policyTO);
-        K savedPolicy = policyDAO.save(policy);
-        return binder.getPolicyTO(savedPolicy);
+        return binder.getPolicyTO(policyDAO.save(binder.create(policyTO)));
     }
 
     @PreAuthorize("hasRole('" + StandardEntitlement.POLICY_UPDATE + "')")
-    public PasswordPolicyTO update(final PasswordPolicyTO policyTO) {
+    public AbstractPolicyTO update(final AbstractPolicyTO policyTO) {
         Policy policy = policyDAO.find(policyTO.getKey());
-        if (!(policy instanceof PasswordPolicy)) {
-            throw new NotFoundException("PasswordPolicy with key " + policyTO.getKey());
-        }
-
-        return update(policyTO, policy);
+        return binder.getPolicyTO(policyDAO.save(binder.update(policy, policyTO)));
     }
 
-    @PreAuthorize("hasRole('" + StandardEntitlement.POLICY_UPDATE + "')")
-    public AccountPolicyTO update(final AccountPolicyTO policyTO) {
-        Policy policy = policyDAO.find(policyTO.getKey());
-        if (!(policy instanceof AccountPolicy)) {
-            throw new NotFoundException("AccountPolicy with key " + policyTO.getKey());
-        }
+    private Class<? extends Policy> getPolicyClass(final PolicyType policyType) {
+        switch (policyType) {
+            case ACCOUNT:
+                return AccountPolicy.class;
 
-        return update(policyTO, policy);
-    }
+            case PASSWORD:
+                return PasswordPolicy.class;
 
-    @PreAuthorize("hasRole('" + StandardEntitlement.POLICY_UPDATE + "')")
-    public PullPolicyTO update(final PullPolicyTO policyTO) {
-        Policy policy = policyDAO.find(policyTO.getKey());
-        if (!(policy instanceof PullPolicy)) {
-            throw new NotFoundException(PullPolicy.class.getSimpleName() + " with key " + policyTO.getKey());
-        }
+            case PULL:
+                return PullPolicy.class;
 
-        return update(policyTO, policy);
+            case PUSH:
+            default:
+                return PushPolicy.class;
+        }
     }
 
     @PreAuthorize("hasRole('" + StandardEntitlement.POLICY_LIST + "')")
     public <T extends AbstractPolicyTO> List<T> list(final PolicyType type) {
-        return CollectionUtils.collect(policyDAO.find(type), new Transformer<Policy, T>() {
+        return CollectionUtils.collect(policyDAO.find(getPolicyClass(type)), new Transformer<Policy, T>() {
 
             @Override
             public T transform(final Policy input) {

http://git-wip-us.apache.org/repos/asf/syncope/blob/31fea77b/core/logic/src/main/java/org/apache/syncope/core/logic/SyncopeLogic.java
----------------------------------------------------------------------
diff --git a/core/logic/src/main/java/org/apache/syncope/core/logic/SyncopeLogic.java b/core/logic/src/main/java/org/apache/syncope/core/logic/SyncopeLogic.java
index af8d1e1..dbec17e 100644
--- a/core/logic/src/main/java/org/apache/syncope/core/logic/SyncopeLogic.java
+++ b/core/logic/src/main/java/org/apache/syncope/core/logic/SyncopeLogic.java
@@ -33,7 +33,6 @@ import org.apache.syncope.common.lib.AbstractBaseBean;
 import org.apache.syncope.common.lib.info.NumbersInfo;
 import org.apache.syncope.common.lib.info.SystemInfo;
 import org.apache.syncope.common.lib.info.PlatformInfo;
-import org.apache.syncope.common.lib.types.PolicyType;
 import org.apache.syncope.common.lib.types.TaskType;
 import org.apache.syncope.core.spring.security.PasswordGenerator;
 import org.apache.syncope.core.persistence.api.ImplementationLookup;
@@ -50,6 +49,8 @@ import org.apache.syncope.core.persistence.api.dao.TaskDAO;
 import org.apache.syncope.core.persistence.api.dao.UserDAO;
 import org.apache.syncope.core.persistence.api.dao.VirSchemaDAO;
 import org.apache.syncope.core.persistence.api.entity.AnyType;
+import org.apache.syncope.core.persistence.api.entity.policy.AccountPolicy;
+import org.apache.syncope.core.persistence.api.entity.policy.PasswordPolicy;
 import org.apache.syncope.core.provisioning.api.AnyObjectProvisioningManager;
 import org.apache.syncope.core.provisioning.api.ConnIdBundleManager;
 import org.apache.syncope.core.provisioning.api.GroupProvisioningManager;
@@ -279,9 +280,9 @@ public class SyncopeLogic extends AbstractLogic<AbstractBaseBean> {
         numbersInfo.getConfCompleteness().put(
                 NumbersInfo.ConfItem.RESOURCE, numbersInfo.getTotalResources() > 0);
         numbersInfo.getConfCompleteness().put(
-                NumbersInfo.ConfItem.ACCOUNT_POLICY, !policyDAO.find(PolicyType.ACCOUNT).isEmpty());
+                NumbersInfo.ConfItem.ACCOUNT_POLICY, !policyDAO.find(AccountPolicy.class).isEmpty());
         numbersInfo.getConfCompleteness().put(
-                NumbersInfo.ConfItem.PASSWORD_POLICY, !policyDAO.find(PolicyType.PASSWORD).isEmpty());
+                NumbersInfo.ConfItem.PASSWORD_POLICY, !policyDAO.find(PasswordPolicy.class).isEmpty());
         numbersInfo.getConfCompleteness().put(
                 NumbersInfo.ConfItem.NOTIFICATION, !notificationDAO.findAll().isEmpty());
         numbersInfo.getConfCompleteness().put(

http://git-wip-us.apache.org/repos/asf/syncope/blob/31fea77b/core/persistence-api/src/main/java/org/apache/syncope/core/persistence/api/dao/ExternalResourceDAO.java
----------------------------------------------------------------------
diff --git a/core/persistence-api/src/main/java/org/apache/syncope/core/persistence/api/dao/ExternalResourceDAO.java b/core/persistence-api/src/main/java/org/apache/syncope/core/persistence/api/dao/ExternalResourceDAO.java
index 35e9689..6c443e8 100644
--- a/core/persistence-api/src/main/java/org/apache/syncope/core/persistence/api/dao/ExternalResourceDAO.java
+++ b/core/persistence-api/src/main/java/org/apache/syncope/core/persistence/api/dao/ExternalResourceDAO.java
@@ -20,7 +20,6 @@ package org.apache.syncope.core.persistence.api.dao;
 
 import java.util.List;
 import org.apache.syncope.common.lib.types.IntMappingType;
-import org.apache.syncope.common.lib.types.PolicyType;
 import org.apache.syncope.core.persistence.api.entity.Policy;
 import org.apache.syncope.core.persistence.api.entity.resource.ExternalResource;
 import org.apache.syncope.core.persistence.api.entity.resource.Provision;
@@ -35,7 +34,7 @@ public interface ExternalResourceDAO extends DAO<ExternalResource, String> {
 
     List<ExternalResource> findByPolicy(Policy policy);
 
-    List<ExternalResource> findWithoutPolicy(PolicyType type);
+    List<ExternalResource> findWithoutPolicy(Class<? extends Policy> policyClass);
 
     List<ExternalResource> findAll();
 

http://git-wip-us.apache.org/repos/asf/syncope/blob/31fea77b/core/persistence-api/src/main/java/org/apache/syncope/core/persistence/api/dao/PolicyDAO.java
----------------------------------------------------------------------
diff --git a/core/persistence-api/src/main/java/org/apache/syncope/core/persistence/api/dao/PolicyDAO.java b/core/persistence-api/src/main/java/org/apache/syncope/core/persistence/api/dao/PolicyDAO.java
index a27f1c0..aa41223 100644
--- a/core/persistence-api/src/main/java/org/apache/syncope/core/persistence/api/dao/PolicyDAO.java
+++ b/core/persistence-api/src/main/java/org/apache/syncope/core/persistence/api/dao/PolicyDAO.java
@@ -19,7 +19,6 @@
 package org.apache.syncope.core.persistence.api.dao;
 
 import java.util.List;
-import org.apache.syncope.common.lib.types.PolicyType;
 import org.apache.syncope.core.persistence.api.entity.policy.AccountPolicy;
 import org.apache.syncope.core.persistence.api.entity.resource.ExternalResource;
 import org.apache.syncope.core.persistence.api.entity.Policy;
@@ -28,7 +27,7 @@ public interface PolicyDAO extends DAO<Policy, Long> {
 
     <T extends Policy> T find(Long key);
 
-    <T extends Policy> List<T> find(PolicyType type);
+    <T extends Policy> List<T> find(Class<T> reference);
 
     List<AccountPolicy> findByResource(ExternalResource resource);
 

http://git-wip-us.apache.org/repos/asf/syncope/blob/31fea77b/core/persistence-api/src/main/java/org/apache/syncope/core/persistence/api/entity/Policy.java
----------------------------------------------------------------------
diff --git a/core/persistence-api/src/main/java/org/apache/syncope/core/persistence/api/entity/Policy.java b/core/persistence-api/src/main/java/org/apache/syncope/core/persistence/api/entity/Policy.java
index d02de0a..39d91ce 100644
--- a/core/persistence-api/src/main/java/org/apache/syncope/core/persistence/api/entity/Policy.java
+++ b/core/persistence-api/src/main/java/org/apache/syncope/core/persistence/api/entity/Policy.java
@@ -18,13 +18,9 @@
  */
 package org.apache.syncope.core.persistence.api.entity;
 
-import org.apache.syncope.common.lib.types.PolicyType;
-
 public interface Policy extends Entity<Long> {
 
     String getDescription();
 
-    PolicyType getType();
-
     void setDescription(String description);
 }

http://git-wip-us.apache.org/repos/asf/syncope/blob/31fea77b/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/dao/JPAExternalResourceDAO.java
----------------------------------------------------------------------
diff --git a/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/dao/JPAExternalResourceDAO.java b/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/dao/JPAExternalResourceDAO.java
index 44c6e08..be02327 100644
--- a/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/dao/JPAExternalResourceDAO.java
+++ b/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/dao/JPAExternalResourceDAO.java
@@ -24,7 +24,6 @@ import java.util.Set;
 import javax.persistence.Query;
 import javax.persistence.TypedQuery;
 import org.apache.syncope.common.lib.types.IntMappingType;
-import org.apache.syncope.common.lib.types.PolicyType;
 import org.apache.syncope.common.lib.types.TaskType;
 import org.apache.syncope.core.persistence.api.dao.AnyObjectDAO;
 import org.apache.syncope.core.persistence.api.dao.ExternalResourceDAO;
@@ -41,6 +40,8 @@ import org.apache.syncope.core.persistence.api.entity.Policy;
 import org.apache.syncope.core.persistence.api.entity.VirSchema;
 import org.apache.syncope.core.persistence.api.entity.anyobject.AnyObject;
 import org.apache.syncope.core.persistence.api.entity.group.Group;
+import org.apache.syncope.core.persistence.api.entity.policy.PasswordPolicy;
+import org.apache.syncope.core.persistence.api.entity.policy.PullPolicy;
 import org.apache.syncope.core.persistence.api.entity.resource.Provision;
 import org.apache.syncope.core.persistence.api.entity.user.User;
 import org.apache.syncope.core.persistence.jpa.entity.resource.JPAMappingItem;
@@ -93,41 +94,34 @@ public class JPAExternalResourceDAO extends AbstractDAO<ExternalResource, String
         return entityManager().find(JPAProvision.class, key);
     }
 
-    private StringBuilder getByPolicyQuery(final PolicyType type) {
+    private StringBuilder getByPolicyQuery(final Class<? extends Policy> policyClass) {
         StringBuilder query = new StringBuilder("SELECT e FROM ").
                 append(JPAExternalResource.class.getSimpleName()).
                 append(" e WHERE e.");
-        switch (type) {
-            case ACCOUNT:
-                query.append("accountPolicy");
-                break;
 
-            case PASSWORD:
-                query.append("passwordPolicy");
-                break;
-
-            case PULL:
-                query.append("pullPolicy");
-                break;
-
-            default:
-                break;
+        if (AccountPolicy.class.isAssignableFrom(policyClass)) {
+            query.append("accountPolicy");
+        } else if (PasswordPolicy.class.isAssignableFrom(policyClass)) {
+            query.append("passwordPolicy");
+        } else if (PullPolicy.class.isAssignableFrom(policyClass)) {
+            query.append("pullPolicy");
         }
+
         return query;
     }
 
     @Override
     public List<ExternalResource> findByPolicy(final Policy policy) {
         TypedQuery<ExternalResource> query = entityManager().createQuery(
-                getByPolicyQuery(policy.getType()).append(" = :policy").toString(), ExternalResource.class);
+                getByPolicyQuery(policy.getClass()).append(" = :policy").toString(), ExternalResource.class);
         query.setParameter("policy", policy);
         return query.getResultList();
     }
 
     @Override
-    public List<ExternalResource> findWithoutPolicy(final PolicyType type) {
+    public List<ExternalResource> findWithoutPolicy(final Class<? extends Policy> policyClass) {
         TypedQuery<ExternalResource> query = entityManager().createQuery(
-                getByPolicyQuery(type).append(" IS NULL").toString(), ExternalResource.class);
+                getByPolicyQuery(policyClass).append(" IS NULL").toString(), ExternalResource.class);
         return query.getResultList();
     }
 

http://git-wip-us.apache.org/repos/asf/syncope/blob/31fea77b/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/dao/JPAPlainAttrDAO.java
----------------------------------------------------------------------
diff --git a/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/dao/JPAPlainAttrDAO.java b/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/dao/JPAPlainAttrDAO.java
index 2afbe15..0d17176 100644
--- a/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/dao/JPAPlainAttrDAO.java
+++ b/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/dao/JPAPlainAttrDAO.java
@@ -35,7 +35,7 @@ import org.springframework.stereotype.Repository;
 @Repository
 public class JPAPlainAttrDAO extends AbstractDAO<PlainAttr<?>, Long> implements PlainAttrDAO {
 
-    public <T extends PlainAttr<?>> Class<? extends AbstractPlainAttr<?>> getJPAEntityReference(
+    public <T extends PlainAttr<?>> Class<? extends AbstractPlainAttr<?>> getEntityReference(
             final Class<T> reference) {
 
         return CPlainAttr.class.isAssignableFrom(reference)
@@ -51,7 +51,7 @@ public class JPAPlainAttrDAO extends AbstractDAO<PlainAttr<?>, Long> implements
 
     @Override
     public <T extends PlainAttr<?>> T find(final Long key, final Class<T> reference) {
-        return reference.cast(entityManager().find(getJPAEntityReference(reference), key));
+        return reference.cast(entityManager().find(getEntityReference(reference), key));
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/syncope/blob/31fea77b/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/dao/JPAPlainAttrValueDAO.java
----------------------------------------------------------------------
diff --git a/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/dao/JPAPlainAttrValueDAO.java b/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/dao/JPAPlainAttrValueDAO.java
index 4b414be..62a25de 100644
--- a/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/dao/JPAPlainAttrValueDAO.java
+++ b/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/dao/JPAPlainAttrValueDAO.java
@@ -46,7 +46,7 @@ import org.springframework.stereotype.Repository;
 public class JPAPlainAttrValueDAO extends AbstractDAO<PlainAttrValue, Long> implements PlainAttrValueDAO {
 
     @SuppressWarnings("unchecked")
-    private <T extends PlainAttrValue> Class<? extends AbstractPlainAttrValue> getJPAEntityReference(
+    private <T extends PlainAttrValue> Class<? extends AbstractPlainAttrValue> getEntityReference(
             final Class<T> reference) {
 
         return AbstractPlainAttrValue.class.isAssignableFrom(reference)
@@ -72,13 +72,13 @@ public class JPAPlainAttrValueDAO extends AbstractDAO<PlainAttrValue, Long> impl
 
     @Override
     public <T extends PlainAttrValue> T find(final Long key, final Class<T> reference) {
-        return reference.cast(entityManager().find(getJPAEntityReference(reference), key));
+        return reference.cast(entityManager().find(getEntityReference(reference), key));
     }
 
     @Override
     public <T extends PlainAttrValue> List<T> findAll(final Class<T> reference) {
         TypedQuery<T> query = entityManager().createQuery(
-                "SELECT e FROM " + getJPAEntityReference(reference).getSimpleName() + " e", reference);
+                "SELECT e FROM " + getEntityReference(reference).getSimpleName() + " e", reference);
         return query.getResultList();
     }
 

http://git-wip-us.apache.org/repos/asf/syncope/blob/31fea77b/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/dao/JPAPlainSchemaDAO.java
----------------------------------------------------------------------
diff --git a/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/dao/JPAPlainSchemaDAO.java b/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/dao/JPAPlainSchemaDAO.java
index 61b3dbd..8e9edcc 100644
--- a/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/dao/JPAPlainSchemaDAO.java
+++ b/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/dao/JPAPlainSchemaDAO.java
@@ -74,7 +74,7 @@ public class JPAPlainSchemaDAO extends AbstractDAO<PlainSchema, String> implemen
     @Override
     public <T extends PlainAttr<?>> List<T> findAttrs(final PlainSchema schema, final Class<T> reference) {
         TypedQuery<T> query = entityManager().createQuery(
-                "SELECT e FROM " + ((JPAPlainAttrDAO) plainAttrDAO).getJPAEntityReference(reference).getSimpleName()
+                "SELECT e FROM " + ((JPAPlainAttrDAO) plainAttrDAO).getEntityReference(reference).getSimpleName()
                 + " e WHERE e.schema=:schema", reference);
         query.setParameter("schema", schema);
 

http://git-wip-us.apache.org/repos/asf/syncope/blob/31fea77b/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/dao/JPAPolicyDAO.java
----------------------------------------------------------------------
diff --git a/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/dao/JPAPolicyDAO.java b/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/dao/JPAPolicyDAO.java
index 4b89f8c..db216be 100644
--- a/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/dao/JPAPolicyDAO.java
+++ b/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/dao/JPAPolicyDAO.java
@@ -19,9 +19,7 @@
 package org.apache.syncope.core.persistence.jpa.dao;
 
 import java.util.List;
-import javax.persistence.Query;
 import javax.persistence.TypedQuery;
-import org.apache.syncope.common.lib.types.PolicyType;
 import org.apache.syncope.core.persistence.api.dao.PolicyDAO;
 import org.apache.syncope.core.persistence.api.dao.RealmDAO;
 import org.apache.syncope.core.persistence.api.entity.policy.AccountPolicy;
@@ -29,8 +27,13 @@ import org.apache.syncope.core.persistence.api.entity.resource.ExternalResource;
 import org.apache.syncope.core.persistence.api.entity.policy.PasswordPolicy;
 import org.apache.syncope.core.persistence.api.entity.Policy;
 import org.apache.syncope.core.persistence.api.entity.Realm;
+import org.apache.syncope.core.persistence.api.entity.policy.PullPolicy;
+import org.apache.syncope.core.persistence.api.entity.policy.PushPolicy;
 import org.apache.syncope.core.persistence.jpa.entity.policy.AbstractPolicy;
 import org.apache.syncope.core.persistence.jpa.entity.policy.JPAAccountPolicy;
+import org.apache.syncope.core.persistence.jpa.entity.policy.JPAPasswordPolicy;
+import org.apache.syncope.core.persistence.jpa.entity.policy.JPAPullPolicy;
+import org.apache.syncope.core.persistence.jpa.entity.policy.JPAPushPolicy;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Repository;
 
@@ -40,27 +43,30 @@ public class JPAPolicyDAO extends AbstractDAO<Policy, Long> implements PolicyDAO
     @Autowired
     private RealmDAO realmDAO;
 
-    @Override
+    private <T extends Policy> Class<? extends AbstractPolicy> getEntityReference(final Class<T> reference) {
+        return AccountPolicy.class.isAssignableFrom(reference)
+                ? JPAAccountPolicy.class
+                : PasswordPolicy.class.isAssignableFrom(reference)
+                ? JPAPasswordPolicy.class
+                : PullPolicy.class.isAssignableFrom(reference)
+                ? JPAPullPolicy.class
+                : PushPolicy.class.isAssignableFrom(reference)
+                ? JPAPushPolicy.class
+                : null;
+    }
+
     @SuppressWarnings("unchecked")
+    @Override
     public <T extends Policy> T find(final Long key) {
-        final Query query = entityManager().createQuery(
-                "SELECT e FROM " + AbstractPolicy.class.getSimpleName() + " e WHERE e.id=:id");
-        query.setParameter("id", key);
-
-        List<T> result = query.getResultList();
-        return result.isEmpty()
-                ? null
-                : result.iterator().next();
+        return (T) entityManager().find(AbstractPolicy.class, key);
     }
 
     @Override
-    @SuppressWarnings("unchecked")
-    public <T extends Policy> List<T> find(final PolicyType type) {
-        final Query query = entityManager().createQuery(
-                "SELECT e FROM " + AbstractPolicy.class.getSimpleName() + " e WHERE e.type=:type");
-        query.setParameter("type", type);
+    public <T extends Policy> List<T> find(final Class<T> reference) {
+        TypedQuery<T> query = entityManager().createQuery(
+                "SELECT e FROM " + getEntityReference(reference).getSimpleName() + " e", reference);
 
-        return (List<T>) query.getResultList();
+        return query.getResultList();
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/syncope/blob/31fea77b/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/dao/JPARealmDAO.java
----------------------------------------------------------------------
diff --git a/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/dao/JPARealmDAO.java b/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/dao/JPARealmDAO.java
index 5100de8..d0a4782 100644
--- a/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/dao/JPARealmDAO.java
+++ b/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/dao/JPARealmDAO.java
@@ -27,7 +27,6 @@ import org.apache.commons.collections4.IterableUtils;
 import org.apache.commons.collections4.Predicate;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.syncope.common.lib.SyncopeConstants;
-import org.apache.syncope.common.lib.types.PolicyType;
 import org.apache.syncope.core.persistence.api.dao.MalformedPathException;
 import org.apache.syncope.core.persistence.api.dao.RealmDAO;
 import org.apache.syncope.core.persistence.api.dao.RoleDAO;
@@ -35,6 +34,8 @@ import org.apache.syncope.core.persistence.api.entity.policy.AccountPolicy;
 import org.apache.syncope.core.persistence.api.entity.Policy;
 import org.apache.syncope.core.persistence.api.entity.Realm;
 import org.apache.syncope.core.persistence.api.entity.Role;
+import org.apache.syncope.core.persistence.api.entity.policy.PasswordPolicy;
+import org.apache.syncope.core.persistence.api.entity.policy.PullPolicy;
 import org.apache.syncope.core.persistence.jpa.entity.JPARealm;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Repository;
@@ -102,9 +103,9 @@ public class JPARealmDAO extends AbstractDAO<Realm, Long> implements RealmDAO {
         List<Realm> result = new ArrayList<>();
 
         for (Realm child : findChildren(realm)) {
-            if ((policy.getType() == PolicyType.ACCOUNT
+            if ((policy instanceof AccountPolicy
                     && child.getAccountPolicy() == null || policy.equals(child.getAccountPolicy()))
-                    || (policy.getType() == PolicyType.PASSWORD
+                    || (policy instanceof PasswordPolicy
                     && child.getPasswordPolicy() == null || policy.equals(child.getPasswordPolicy()))) {
 
                 result.add(child);
@@ -117,7 +118,7 @@ public class JPARealmDAO extends AbstractDAO<Realm, Long> implements RealmDAO {
 
     @Override
     public <T extends Policy> List<Realm> findByPolicy(final T policy) {
-        if (policy.getType() == PolicyType.PULL) {
+        if (PullPolicy.class.isAssignableFrom(policy.getClass())) {
             return Collections.<Realm>emptyList();
         }
 

http://git-wip-us.apache.org/repos/asf/syncope/blob/31fea77b/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/entity/policy/AbstractPolicy.java
----------------------------------------------------------------------
diff --git a/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/entity/policy/AbstractPolicy.java b/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/entity/policy/AbstractPolicy.java
index 843bbcd..5b72758 100644
--- a/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/entity/policy/AbstractPolicy.java
+++ b/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/entity/policy/AbstractPolicy.java
@@ -19,13 +19,10 @@
 package org.apache.syncope.core.persistence.jpa.entity.policy;
 
 import javax.persistence.Entity;
-import javax.persistence.EnumType;
-import javax.persistence.Enumerated;
 import javax.persistence.Id;
 import javax.persistence.Inheritance;
 import javax.persistence.InheritanceType;
 import javax.validation.constraints.NotNull;
-import org.apache.syncope.common.lib.types.PolicyType;
 import org.apache.syncope.core.persistence.api.entity.Policy;
 import org.apache.syncope.core.persistence.jpa.entity.AbstractEntity;
 
@@ -41,10 +38,6 @@ public abstract class AbstractPolicy extends AbstractEntity<Long> implements Pol
     @NotNull
     private String description;
 
-    @NotNull
-    @Enumerated(EnumType.STRING)
-    protected PolicyType type;
-
     @Override
     public Long getKey() {
         return id;
@@ -59,9 +52,4 @@ public abstract class AbstractPolicy extends AbstractEntity<Long> implements Pol
     public void setDescription(final String description) {
         this.description = description;
     }
-
-    @Override
-    public PolicyType getType() {
-        return type;
-    }
 }

http://git-wip-us.apache.org/repos/asf/syncope/blob/31fea77b/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/entity/policy/JPAAccountPolicy.java
----------------------------------------------------------------------
diff --git a/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/entity/policy/JPAAccountPolicy.java b/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/entity/policy/JPAAccountPolicy.java
index 146b127..f65adb4 100644
--- a/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/entity/policy/JPAAccountPolicy.java
+++ b/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/entity/policy/JPAAccountPolicy.java
@@ -36,7 +36,6 @@ import javax.validation.constraints.Min;
 import org.apache.commons.collections4.CollectionUtils;
 import org.apache.commons.collections4.Transformer;
 import org.apache.syncope.common.lib.policy.AccountRuleConf;
-import org.apache.syncope.common.lib.types.PolicyType;
 import org.apache.syncope.core.provisioning.api.utils.EntityUtils;
 import org.apache.syncope.core.persistence.api.entity.policy.AccountPolicy;
 import org.apache.syncope.core.persistence.api.entity.resource.ExternalResource;
@@ -70,11 +69,6 @@ public class JPAAccountPolicy extends AbstractPolicy implements AccountPolicy {
             @JoinColumn(name = "resource_name"))
     private Set<JPAExternalResource> resources = new HashSet<>();
 
-    public JPAAccountPolicy() {
-        super();
-        this.type = PolicyType.ACCOUNT;
-    }
-
     @Override
     public boolean isPropagateSuspension() {
         return isBooleanAsInteger(propagateSuspension);

http://git-wip-us.apache.org/repos/asf/syncope/blob/31fea77b/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/entity/policy/JPAPasswordPolicy.java
----------------------------------------------------------------------
diff --git a/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/entity/policy/JPAPasswordPolicy.java b/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/entity/policy/JPAPasswordPolicy.java
index b6013fd..ba70562 100644
--- a/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/entity/policy/JPAPasswordPolicy.java
+++ b/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/entity/policy/JPAPasswordPolicy.java
@@ -31,7 +31,6 @@ import javax.validation.constraints.Min;
 import org.apache.commons.collections4.CollectionUtils;
 import org.apache.commons.collections4.Transformer;
 import org.apache.syncope.common.lib.policy.PasswordRuleConf;
-import org.apache.syncope.common.lib.types.PolicyType;
 import org.apache.syncope.core.persistence.api.entity.policy.PasswordPolicy;
 
 @Entity
@@ -52,11 +51,6 @@ public class JPAPasswordPolicy extends AbstractPolicy implements PasswordPolicy
     @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER, mappedBy = "passwordPolicy")
     private List<JPAPasswordRuleConfInstance> ruleConfs = new ArrayList<>();
 
-    public JPAPasswordPolicy() {
-        super();
-        this.type = PolicyType.PASSWORD;
-    }
-
     @Override
     public boolean isAllowNullPassword() {
         return isBooleanAsInteger(allowNullPassword);

http://git-wip-us.apache.org/repos/asf/syncope/blob/31fea77b/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/entity/policy/JPAPullPolicy.java
----------------------------------------------------------------------
diff --git a/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/entity/policy/JPAPullPolicy.java b/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/entity/policy/JPAPullPolicy.java
index c408b43..e5fb20f 100644
--- a/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/entity/policy/JPAPullPolicy.java
+++ b/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/entity/policy/JPAPullPolicy.java
@@ -22,7 +22,6 @@ import javax.persistence.Entity;
 import javax.persistence.Lob;
 import javax.persistence.Table;
 import org.apache.syncope.common.lib.policy.PullPolicySpec;
-import org.apache.syncope.common.lib.types.PolicyType;
 import org.apache.syncope.core.provisioning.api.serialization.POJOHelper;
 import org.apache.syncope.core.persistence.api.entity.policy.PullPolicy;
 
@@ -37,11 +36,6 @@ public class JPAPullPolicy extends AbstractPolicy implements PullPolicy {
     @Lob
     private String specification;
 
-    public JPAPullPolicy() {
-        super();
-        this.type = PolicyType.PULL;
-    }
-
     @Override
     public PullPolicySpec getSpecification() {
         return POJOHelper.deserialize(specification, PullPolicySpec.class);

http://git-wip-us.apache.org/repos/asf/syncope/blob/31fea77b/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/entity/policy/JPAPushPolicy.java
----------------------------------------------------------------------
diff --git a/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/entity/policy/JPAPushPolicy.java b/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/entity/policy/JPAPushPolicy.java
index 7b0c1ef..dcdfc1d 100644
--- a/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/entity/policy/JPAPushPolicy.java
+++ b/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/entity/policy/JPAPushPolicy.java
@@ -21,7 +21,6 @@ package org.apache.syncope.core.persistence.jpa.entity.policy;
 import javax.persistence.Entity;
 import javax.persistence.Lob;
 import javax.persistence.Table;
-import org.apache.syncope.common.lib.types.PolicyType;
 import org.apache.syncope.common.lib.policy.PushPolicySpec;
 import org.apache.syncope.core.provisioning.api.serialization.POJOHelper;
 import org.apache.syncope.core.persistence.api.entity.policy.PushPolicy;
@@ -37,11 +36,6 @@ public class JPAPushPolicy extends AbstractPolicy implements PushPolicy {
     @Lob
     private String specification;
 
-    public JPAPushPolicy() {
-        super();
-        this.type = PolicyType.PUSH;
-    }
-
     @Override
     public PushPolicySpec getSpecification() {
         return POJOHelper.deserialize(specification, PushPolicySpec.class);

http://git-wip-us.apache.org/repos/asf/syncope/blob/31fea77b/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/inner/PolicyTest.java
----------------------------------------------------------------------
diff --git a/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/inner/PolicyTest.java b/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/inner/PolicyTest.java
index 426a3de..a3be9bb 100644
--- a/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/inner/PolicyTest.java
+++ b/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/inner/PolicyTest.java
@@ -28,7 +28,6 @@ import java.util.List;
 import org.apache.commons.lang3.ArrayUtils;
 import org.apache.syncope.common.lib.types.AnyTypeKind;
 import org.apache.syncope.common.lib.policy.DefaultPasswordRuleConf;
-import org.apache.syncope.common.lib.types.PolicyType;
 import org.apache.syncope.common.lib.policy.PullPolicySpec;
 import org.apache.syncope.core.provisioning.api.serialization.POJOHelper;
 import org.apache.syncope.core.persistence.api.dao.AnyTypeDAO;
@@ -76,7 +75,7 @@ public class PolicyTest extends AbstractTest {
 
     @Test
     public void findByType() {
-        List<? extends Policy> policies = policyDAO.find(PolicyType.PULL);
+        List<PullPolicy> policies = policyDAO.find(PullPolicy.class);
         assertNotNull("findById did not work", policies);
         assertFalse(policies.isEmpty());
     }
@@ -99,7 +98,6 @@ public class PolicyTest extends AbstractTest {
         policy = policyDAO.save(policy);
 
         assertNotNull(policy);
-        assertEquals(PolicyType.PULL, policy.getType());
         assertEquals(pullURuleName,
                 policy.getSpecification().getCorrelationRules().get(anyTypeDAO.findUser().getKey()));
         assertEquals(pullGRuleName,
@@ -120,7 +118,6 @@ public class PolicyTest extends AbstractTest {
         policy = policyDAO.save(policy);
 
         assertNotNull(policy);
-        assertEquals(PolicyType.PASSWORD, policy.getType());
         assertEquals(((DefaultPasswordRuleConf) policy.getRuleConfs().get(1)).getMaxLength(), 8);
         assertEquals(((DefaultPasswordRuleConf) policy.getRuleConfs().get(1)).getMinLength(), 6);
     }

http://git-wip-us.apache.org/repos/asf/syncope/blob/31fea77b/core/persistence-jpa/src/test/resources/domains/MasterContent.xml
----------------------------------------------------------------------
diff --git a/core/persistence-jpa/src/test/resources/domains/MasterContent.xml b/core/persistence-jpa/src/test/resources/domains/MasterContent.xml
index 44e82b8..24e3894 100644
--- a/core/persistence-jpa/src/test/resources/domains/MasterContent.xml
+++ b/core/persistence-jpa/src/test/resources/domains/MasterContent.xml
@@ -105,34 +105,28 @@ under the License.
                mandatoryCondition="false" multivalue="1" uniqueConstraint="0" readonly="0"/>
   
   <!-- sample policies -->
-  <PullPolicy id="1" description="a pull policy" type="PULL" 
-              specification='{"conflictResolutionAction":"IGNORE"}'/>
-  <PasswordPolicy id="2" description="a password policy" type="PASSWORD" 
-                  historyLength="1" allowNullPassword="1"/> 
+  <PullPolicy id="1" description="a pull policy" specification='{"conflictResolutionAction":"IGNORE"}'/>
+  <PasswordPolicy id="2" description="a password policy" historyLength="1" allowNullPassword="1"/> 
   <PasswordRuleConfInstance id="2" passwordPolicy_id="2"
                             serializedInstance='{"@class":"org.apache.syncope.common.lib.policy.DefaultPasswordRuleConf","maxLength":0,"minLength":8,"nonAlphanumericRequired":false,"alphanumericRequired":false,"digitRequired":false,"lowercaseRequired":false,"uppercaseRequired":false,"mustStartWithDigit":false,"mustntStartWithDigit":false,"mustEndWithDigit":false,"mustntEndWithDigit":false,"mustStartWithNonAlpha":false,"mustStartWithAlpha":false,"mustntStartWithNonAlpha":false,"mustntStartWithAlpha":false,"mustEndWithNonAlpha":false,"mustEndWithAlpha":false,"mustntEndWithNonAlpha":false,"mustntEndWithAlpha":false,"wordsNotPermitted":[],"schemasNotPermitted":[],"prefixesNotPermitted":["notpermitted1","notpermitted2"],"suffixesNotPermitted":[]}'/>
-  <PullPolicy id="3" description="pull policy 2" type="PULL" 
+  <PullPolicy id="3" description="pull policy 2"
               specification='{"conflictResolutionAction":"ALL","correlationRules":{"USER":"[\"username\",\"firstname\"]"}}'/>
-  <PasswordPolicy id="4" description="sample password policy" type="PASSWORD"
-                  historyLength="0"  allowNullPassword="1"/> 
+  <PasswordPolicy id="4" description="sample password policy" historyLength="0"  allowNullPassword="1"/> 
   <PasswordRuleConfInstance id="4" passwordPolicy_id="4"
                             serializedInstance='{"@class":"org.apache.syncope.common.lib.policy.DefaultPasswordRuleConf","maxLength":0,"minLength":10,"nonAlphanumericRequired":false,"alphanumericRequired":false,"digitRequired":true,"lowercaseRequired":false,"uppercaseRequired":false,"mustStartWithDigit":false,"mustntStartWithDigit":false,"mustEndWithDigit":false,"mustntEndWithDigit":false,"mustStartWithNonAlpha":false,"mustStartWithAlpha":false,"mustntStartWithNonAlpha":false,"mustntStartWithAlpha":false,"mustEndWithNonAlpha":false,"mustEndWithAlpha":false,"mustntEndWithNonAlpha":false,"mustntEndWithAlpha":false,"wordsNotPermitted":[],"schemasNotPermitted":[],"prefixesNotPermitted":["notpermitted1","notpermitted2"],"suffixesNotPermitted":[]}'/>
-  <AccountPolicy id="5" description="an account policy" type="ACCOUNT"
+  <AccountPolicy id="5" description="an account policy"
                  propagateSuspension="0" maxAuthenticationAttempts="0"/>
   <AccountRuleConfInstance id="5" accountPolicy_id="5"
                            serializedInstance='{"@class":"org.apache.syncope.common.lib.policy.DefaultAccountRuleConf","maxLength":0,"minLength":0,"pattern":null,"allUpperCase":false,"allLowerCase":false,"wordsNotPermitted":[],"schemasNotPermitted":[],"prefixesNotPermitted":[],"suffixesNotPermitted":[]}'/>
-  <AccountPolicy id="6" description="sample account policy" type="ACCOUNT"
+  <AccountPolicy id="6" description="sample account policy"
                  propagateSuspension="0" maxAuthenticationAttempts="3"/>
   <AccountRuleConfInstance id="6" accountPolicy_id="6"
                            serializedInstance='{"@class":"org.apache.syncope.common.lib.policy.DefaultAccountRuleConf","maxLength":0,"minLength":4,"pattern":null,"allUpperCase":false,"allLowerCase":false,"wordsNotPermitted":[],"schemasNotPermitted":[],"prefixesNotPermitted":["notpermitted1","notpermitted2"],"suffixesNotPermitted":[]}'/>
-  <PullPolicy id="7" description="pull policy 1" type="PULL" 
-              specification='{"conflictResolutionAction":"IGNORE"}'/>
-  <PasswordPolicy id="8" description="sample password policy" type="PASSWORD"
-                  historyLength="0" allowNullPassword="0"/> 
+  <PullPolicy id="7" description="pull policy 1" specification='{"conflictResolutionAction":"IGNORE"}'/>
+  <PasswordPolicy id="8" description="sample password policy" historyLength="0" allowNullPassword="0"/> 
   <PasswordRuleConfInstance id="8" passwordPolicy_id="8"
                             serializedInstance='{"@class":"org.apache.syncope.common.lib.policy.DefaultPasswordRuleConf","maxLength":0,"minLength":10,"nonAlphanumericRequired":true,"alphanumericRequired":false,"digitRequired":true,"lowercaseRequired":true,"uppercaseRequired":true,"mustStartWithDigit":true,"mustntStartWithDigit":false,"mustEndWithDigit":true,"mustntEndWithDigit":false,"mustStartWithNonAlpha":false,"mustStartWithAlpha":false,"mustntStartWithNonAlpha":false,"mustntStartWithAlpha":false,"mustEndWithNonAlpha":false,"mustEndWithAlpha":false,"mustntEndWithNonAlpha":false,"mustntEndWithAlpha":false,"wordsNotPermitted":[],"schemasNotPermitted":[],"prefixesNotPermitted":["notpermitted1","notpermitted2"],"suffixesNotPermitted":[]}'/>
-  <PullPolicy id="9" description="pull policy for java rule" type="PULL" 
-              specification='{"conflictResolutionAction":"IGNORE"}'/>
+  <PullPolicy id="9" description="pull policy for java rule" specification='{"conflictResolutionAction":"IGNORE"}'/>
 
   <RelationshipType name="inclusion" description="Models the act that an object is included in another"/>
   <RelationshipType name="neighborhood"/>

http://git-wip-us.apache.org/repos/asf/syncope/blob/31fea77b/core/provisioning-api/src/main/java/org/apache/syncope/core/provisioning/api/data/PolicyDataBinder.java
----------------------------------------------------------------------
diff --git a/core/provisioning-api/src/main/java/org/apache/syncope/core/provisioning/api/data/PolicyDataBinder.java b/core/provisioning-api/src/main/java/org/apache/syncope/core/provisioning/api/data/PolicyDataBinder.java
index 10ac492..cea03df 100644
--- a/core/provisioning-api/src/main/java/org/apache/syncope/core/provisioning/api/data/PolicyDataBinder.java
+++ b/core/provisioning-api/src/main/java/org/apache/syncope/core/provisioning/api/data/PolicyDataBinder.java
@@ -23,7 +23,9 @@ import org.apache.syncope.core.persistence.api.entity.Policy;
 
 public interface PolicyDataBinder {
 
-    <T extends Policy> T getPolicy(T policy, AbstractPolicyTO policyTO);
+    <T extends Policy> T create(AbstractPolicyTO policyTO);
+
+    <T extends Policy> T update(T policy, AbstractPolicyTO policyTO);
 
     <T extends AbstractPolicyTO> T getPolicyTO(Policy policy);
 

http://git-wip-us.apache.org/repos/asf/syncope/blob/31fea77b/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/data/PolicyDataBinderImpl.java
----------------------------------------------------------------------
diff --git a/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/data/PolicyDataBinderImpl.java b/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/data/PolicyDataBinderImpl.java
index 92149c3..625f88d 100644
--- a/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/data/PolicyDataBinderImpl.java
+++ b/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/data/PolicyDataBinderImpl.java
@@ -57,135 +57,134 @@ public class PolicyDataBinderImpl implements PolicyDataBinder {
     @Autowired
     private EntityFactory entityFactory;
 
-    @SuppressWarnings("unchecked")
-    @Override
-    public <T extends AbstractPolicyTO> T getPolicyTO(final Policy policy) {
-        T policyTO;
-        switch (policy.getType()) {
-            case PASSWORD:
-                PasswordPolicy passwordPolicy = PasswordPolicy.class.cast(policy);
-                PasswordPolicyTO passwordPolicyTO = new PasswordPolicyTO();
-                policyTO = (T) passwordPolicyTO;
-
-                passwordPolicyTO.setAllowNullPassword(passwordPolicy.isAllowNullPassword());
-                passwordPolicyTO.setHistoryLength(passwordPolicy.getHistoryLength());
-
-                passwordPolicyTO.getRuleConfs().clear();
-                for (PasswordRuleConf ruleConf : passwordPolicy.getRuleConfs()) {
-                    passwordPolicyTO.getRuleConfs().add((AbstractPasswordRuleConf) ruleConf);
-                }
-                break;
-
-            case ACCOUNT:
-                AccountPolicy accountPolicy = AccountPolicy.class.cast(policy);
-                AccountPolicyTO accountPolicyTO = new AccountPolicyTO();
-                policyTO = (T) accountPolicyTO;
+    private void throwInvalidPolicy(final Policy policy, final AbstractPolicyTO policyTO) {
+        SyncopeClientException sce = SyncopeClientException.build(ClientExceptionType.InvalidPolicy);
+        sce.getElements().add(String.format("Cannot update %s from %s",
+                policy.getClass().getSimpleName(), policyTO.getClass().getSimpleName()));
+        throw sce;
+    }
 
-                accountPolicyTO.setMaxAuthenticationAttempts(accountPolicy.getMaxAuthenticationAttempts());
-                accountPolicyTO.setPropagateSuspension(accountPolicy.isPropagateSuspension());
+    @SuppressWarnings("unchecked")
+    private <T extends Policy> T getPolicy(final T policy, final AbstractPolicyTO policyTO) {
+        T result = policy;
 
-                accountPolicyTO.getRuleConfs().clear();
-                for (AccountRuleConf ruleConf : accountPolicy.getRuleConfs()) {
-                    accountPolicyTO.getRuleConfs().add((AbstractAccountRuleConf) ruleConf);
+        if (policyTO instanceof PasswordPolicyTO) {
+            if (result == null) {
+                result = (T) entityFactory.newEntity(PasswordPolicy.class);
+            } else if (!(policyTO instanceof PasswordPolicyTO)) {
+                throwInvalidPolicy(policy, policyTO);
+            }
+
+            PasswordPolicy passwordPolicy = PasswordPolicy.class.cast(result);
+            PasswordPolicyTO passwordPolicyTO = PasswordPolicyTO.class.cast(policyTO);
+
+            passwordPolicy.setAllowNullPassword(passwordPolicyTO.isAllowNullPassword());
+            passwordPolicy.setHistoryLength(passwordPolicyTO.getHistoryLength());
+
+            passwordPolicy.removeAllRuleConfs();
+            for (PasswordRuleConf conf : passwordPolicyTO.getRuleConfs()) {
+                passwordPolicy.add(conf);
+            }
+        } else if (policyTO instanceof AccountPolicyTO) {
+            if (result == null) {
+                result = (T) entityFactory.newEntity(AccountPolicy.class);
+            } else if (!(policyTO instanceof AccountPolicyTO)) {
+                throwInvalidPolicy(policy, policyTO);
+            }
+
+            AccountPolicy accountPolicy = AccountPolicy.class.cast(result);
+            AccountPolicyTO accountPolicyTO = AccountPolicyTO.class.cast(policyTO);
+
+            accountPolicy.setMaxAuthenticationAttempts(accountPolicyTO.getMaxAuthenticationAttempts());
+            accountPolicy.setPropagateSuspension(accountPolicyTO.isPropagateSuspension());
+
+            accountPolicy.removeAllRuleConfs();
+            for (AccountRuleConf conf : accountPolicyTO.getRuleConfs()) {
+                accountPolicy.add(conf);
+            }
+
+            accountPolicy.getResources().clear();
+            for (String resourceName : accountPolicyTO.getResources()) {
+                ExternalResource resource = resourceDAO.find(resourceName);
+                if (resource == null) {
+                    LOG.debug("Ignoring invalid resource {} ", resourceName);
+                } else {
+                    accountPolicy.add(resource);
                 }
+            }
+        } else if (policyTO instanceof PullPolicyTO) {
+            if (result == null) {
+                result = (T) entityFactory.newEntity(PullPolicy.class);
+            } else if (!(policyTO instanceof PullPolicyTO)) {
+                throwInvalidPolicy(policy, policyTO);
+            }
+
+            ((PullPolicy) result).setSpecification(((PullPolicyTO) policyTO).getSpecification());
+        }
 
-                accountPolicyTO.getResources().addAll(accountPolicy.getResourceNames());
-                break;
-
-            case PULL:
-            default:
-                policyTO = (T) new PullPolicyTO();
-                ((PullPolicyTO) policyTO).setSpecification(((PullPolicy) policy).getSpecification());
+        if (result != null) {
+            result.setDescription(policyTO.getDescription());
         }
 
-        policyTO.setKey(policy.getKey());
-        policyTO.setDescription(policy.getDescription());
+        return result;
+    }
 
-        for (ExternalResource resource : resourceDAO.findByPolicy(policy)) {
-            policyTO.getUsedByResources().add(resource.getKey());
-        }
-        for (Realm realm : realmDAO.findByPolicy(policy)) {
-            policyTO.getUsedByRealms().add(realm.getFullPath());
-        }
+    @Override
+    public <T extends Policy> T create(final AbstractPolicyTO policyTO) {
+        return getPolicy(null, policyTO);
+    }
 
-        return policyTO;
+    @Override
+    public <T extends Policy> T update(final T policy, final AbstractPolicyTO policyTO) {
+        return getPolicy(policy, policyTO);
     }
 
     @SuppressWarnings("unchecked")
     @Override
-    public <T extends Policy> T getPolicy(final T policy, final AbstractPolicyTO policyTO) {
-        if (policy != null && policy.getType() != policyTO.getType()) {
-            SyncopeClientException sce = SyncopeClientException.build(ClientExceptionType.InvalidPolicy);
-            sce.getElements().add(String.format("Cannot update %s from %s", policy.getType(), policyTO.getType()));
-            throw sce;
+    public <T extends AbstractPolicyTO> T getPolicyTO(final Policy policy) {
+        T policyTO = null;
+
+        if (policy instanceof PasswordPolicy) {
+            PasswordPolicy passwordPolicy = PasswordPolicy.class.cast(policy);
+            PasswordPolicyTO passwordPolicyTO = new PasswordPolicyTO();
+            policyTO = (T) passwordPolicyTO;
+
+            passwordPolicyTO.setAllowNullPassword(passwordPolicy.isAllowNullPassword());
+            passwordPolicyTO.setHistoryLength(passwordPolicy.getHistoryLength());
+
+            for (PasswordRuleConf ruleConf : passwordPolicy.getRuleConfs()) {
+                passwordPolicyTO.getRuleConfs().add((AbstractPasswordRuleConf) ruleConf);
+            }
+        } else if (policy instanceof AccountPolicy) {
+            AccountPolicy accountPolicy = AccountPolicy.class.cast(policy);
+            AccountPolicyTO accountPolicyTO = new AccountPolicyTO();
+            policyTO = (T) accountPolicyTO;
+
+            accountPolicyTO.setMaxAuthenticationAttempts(accountPolicy.getMaxAuthenticationAttempts());
+            accountPolicyTO.setPropagateSuspension(accountPolicy.isPropagateSuspension());
+
+            for (AccountRuleConf ruleConf : accountPolicy.getRuleConfs()) {
+                accountPolicyTO.getRuleConfs().add((AbstractAccountRuleConf) ruleConf);
+            }
+
+            accountPolicyTO.getResources().addAll(accountPolicy.getResourceNames());
+        } else if (policy instanceof PullPolicy) {
+            policyTO = (T) new PullPolicyTO();
+            ((PullPolicyTO) policyTO).setSpecification(((PullPolicy) policy).getSpecification());
         }
 
-        T result = policy;
-        switch (policyTO.getType()) {
-            case PASSWORD:
-                if (!(policyTO instanceof PasswordPolicyTO)) {
-                    throw new ClassCastException("Expected " + PasswordPolicyTO.class.getName()
-                            + ", found " + policyTO.getClass().getName());
-                }
-                if (result == null) {
-                    result = (T) entityFactory.newEntity(PasswordPolicy.class);
-                }
-                PasswordPolicy passwordPolicy = PasswordPolicy.class.cast(result);
-                PasswordPolicyTO passwordPolicyTO = PasswordPolicyTO.class.cast(policyTO);
-
-                passwordPolicy.setAllowNullPassword(passwordPolicyTO.isAllowNullPassword());
-                passwordPolicy.setHistoryLength(passwordPolicyTO.getHistoryLength());
-
-                passwordPolicy.removeAllRuleConfs();
-                for (PasswordRuleConf conf : passwordPolicyTO.getRuleConfs()) {
-                    passwordPolicy.add(conf);
-                }
-                break;
-
-            case ACCOUNT:
-                if (!(policyTO instanceof AccountPolicyTO)) {
-                    throw new ClassCastException("Expected " + AccountPolicyTO.class.getName()
-                            + ", found " + policyTO.getClass().getName());
-                }
-                if (result == null) {
-                    result = (T) entityFactory.newEntity(AccountPolicy.class);
-                }
-                AccountPolicy accountPolicy = AccountPolicy.class.cast(result);
-                AccountPolicyTO accountPolicyTO = AccountPolicyTO.class.cast(policyTO);
-
-                accountPolicy.setMaxAuthenticationAttempts(accountPolicyTO.getMaxAuthenticationAttempts());
-                accountPolicy.setPropagateSuspension(accountPolicyTO.isPropagateSuspension());
+        if (policyTO != null) {
+            policyTO.setKey(policy.getKey());
+            policyTO.setDescription(policy.getDescription());
 
-                accountPolicy.removeAllRuleConfs();
-                for (AccountRuleConf conf : accountPolicyTO.getRuleConfs()) {
-                    accountPolicy.add(conf);
-                }
-
-                accountPolicy.getResources().clear();
-                for (String resourceName : accountPolicyTO.getResources()) {
-                    ExternalResource resource = resourceDAO.find(resourceName);
-                    if (resource == null) {
-                        LOG.debug("Ignoring invalid resource {} ", resourceName);
-                    } else {
-                        accountPolicy.add(resource);
-                    }
-                }
-                break;
-
-            case PULL:
-            default:
-                if (!(policyTO instanceof PullPolicyTO)) {
-                    throw new ClassCastException("Expected " + PullPolicyTO.class.getName()
-                            + ", found " + policyTO.getClass().getName());
-                }
-                if (result == null) {
-                    result = (T) entityFactory.newEntity(PullPolicy.class);
-                }
-                ((PullPolicy) result).setSpecification(((PullPolicyTO) policyTO).getSpecification());
+            for (ExternalResource resource : resourceDAO.findByPolicy(policy)) {
+                policyTO.getUsedByResources().add(resource.getKey());
+            }
+            for (Realm realm : realmDAO.findByPolicy(policy)) {
+                policyTO.getUsedByRealms().add(realm.getFullPath());
+            }
         }
 
-        result.setDescription(policyTO.getDescription());
-
-        return result;
+        return policyTO;
     }
 }

http://git-wip-us.apache.org/repos/asf/syncope/blob/31fea77b/core/rest-cxf/src/main/java/org/apache/syncope/core/rest/cxf/service/PolicyServiceImpl.java
----------------------------------------------------------------------
diff --git a/core/rest-cxf/src/main/java/org/apache/syncope/core/rest/cxf/service/PolicyServiceImpl.java b/core/rest-cxf/src/main/java/org/apache/syncope/core/rest/cxf/service/PolicyServiceImpl.java
index 2e7be27..95fe255 100644
--- a/core/rest-cxf/src/main/java/org/apache/syncope/core/rest/cxf/service/PolicyServiceImpl.java
+++ b/core/rest-cxf/src/main/java/org/apache/syncope/core/rest/cxf/service/PolicyServiceImpl.java
@@ -22,9 +22,6 @@ import java.net.URI;
 import java.util.List;
 import javax.ws.rs.core.Response;
 import org.apache.syncope.common.lib.policy.AbstractPolicyTO;
-import org.apache.syncope.common.lib.policy.AccountPolicyTO;
-import org.apache.syncope.common.lib.policy.PasswordPolicyTO;
-import org.apache.syncope.common.lib.policy.PullPolicyTO;
 import org.apache.syncope.common.lib.types.PolicyType;
 import org.apache.syncope.common.rest.api.RESTHeaders;
 import org.apache.syncope.common.rest.api.service.PolicyService;
@@ -64,21 +61,6 @@ public class PolicyServiceImpl extends AbstractServiceImpl implements PolicyServ
 
     @Override
     public void update(final AbstractPolicyTO policyTO) {
-        switch (policyTO.getType()) {
-            case ACCOUNT:
-                logic.update((AccountPolicyTO) policyTO);
-                break;
-
-            case PASSWORD:
-                logic.update((PasswordPolicyTO) policyTO);
-                break;
-
-            case PULL:
-                logic.update((PullPolicyTO) policyTO);
-                break;
-
-            default:
-                break;
-        }
+        logic.update(policyTO);
     }
 }

http://git-wip-us.apache.org/repos/asf/syncope/blob/31fea77b/fit/core-reference/src/test/java/org/apache/syncope/fit/core/PolicyITCase.java
----------------------------------------------------------------------
diff --git a/fit/core-reference/src/test/java/org/apache/syncope/fit/core/PolicyITCase.java b/fit/core-reference/src/test/java/org/apache/syncope/fit/core/PolicyITCase.java
index 10f0f56..6df4935 100644
--- a/fit/core-reference/src/test/java/org/apache/syncope/fit/core/PolicyITCase.java
+++ b/fit/core-reference/src/test/java/org/apache/syncope/fit/core/PolicyITCase.java
@@ -113,7 +113,6 @@ public class PolicyITCase extends AbstractITCase {
         PullPolicyTO policyTO = createPolicy(policy);
 
         assertNotNull(policyTO);
-        assertEquals(PolicyType.PULL, policyTO.getType());
         assertEquals(TestPullRule.class.getName(),
                 policyTO.getSpecification().getCorrelationRules().get(AnyTypeKind.USER.name()));
     }
@@ -137,7 +136,6 @@ public class PolicyITCase extends AbstractITCase {
         policy = policyService.read(policy.getKey());
 
         assertNotNull(policy);
-        assertEquals(PolicyType.PASSWORD, policy.getType());
         assertEquals(22, ((DefaultPasswordRuleConf) policy.getRuleConfs().get(0)).getMaxLength());
         assertEquals(8, ((DefaultPasswordRuleConf) policy.getRuleConfs().get(0)).getMinLength());
     }


Mime
View raw message