* @param policy bean.
* @return policy TO.
*/
@SuppressWarnings("unchecked")
public <T extends PolicyTO> T getPolicyTO(final Policy policy) {
final PolicyTO policyTO;
final boolean isGlobal = isGlobalPolicy(policy.getType());
switch (policy.getType()) {
case GLOBAL_PASSWORD:
case PASSWORD:
if (!(policy.getSpecification() instanceof PasswordPolicySpec)) {
throw new ClassCastException("Expected " + PasswordPolicySpec.class.getName()
+ ", found " + policy.getSpecification().getClass().getName());
}
policyTO = new PasswordPolicyTO(isGlobal);
((PasswordPolicyTO) policyTO).setSpecification((PasswordPolicySpec) policy.getSpecification());
break;
case GLOBAL_ACCOUNT:
case ACCOUNT:
if (!(policy.getSpecification() instanceof AccountPolicySpec)) {
throw new ClassCastException("Expected " + AccountPolicySpec.class.getName()
+ ", found " + policy.getSpecification().getClass().getName());
}
policyTO = new AccountPolicyTO(isGlobal);
((AccountPolicyTO) policyTO).setSpecification((AccountPolicySpec) policy.getSpecification());
break;
case GLOBAL_SYNC:
case SYNC:
default:
if (!(policy.getSpecification() instanceof SyncPolicySpec)) {
throw new ClassCastException("Expected " + SyncPolicySpec.class.getName()
+ ", found " + policy.getSpecification().getClass().getName());
}
policyTO = new SyncPolicyTO(isGlobal);
((SyncPolicyTO) policyTO).setSpecification((SyncPolicySpec) policy.getSpecification());
}
policyTO.setId(policy.getId());
policyTO.setDescription(policy.getDescription());
for (ExternalResource resource : resourceDAO.findByPolicy(policy)) {
policyTO.getUsedByResources().add(resource.getName());
}
if (isGlobal) {
for (ExternalResource resource : resourceDAO.findWithoutPolicy(policy.getType())) {
policyTO.getUsedByResources().add(resource.getName());
}
}
for (SyncopeRole role : roleDAO.findByPolicy(policy)) {
policyTO.getUsedByRoles().add(role.getId());
}
if (isGlobal) {
for (SyncopeRole role : roleDAO.findWithoutPolicy(policy.getType())) {
policyTO.getUsedByRoles().add(role.getId());
}
}
return (T) policyTO;
}