You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@syncope.apache.org by fm...@apache.org on 2012/03/02 15:27:35 UTC
svn commit: r1296224 [2/2] - in /incubator/syncope/trunk:
client/src/main/java/org/syncope/types/
console/src/main/java/org/syncope/console/commons/
core/src/main/java/org/syncope/core/persistence/beans/
core/src/main/java/org/syncope/core/persistence/...
Modified: incubator/syncope/trunk/core/src/main/java/org/syncope/core/util/SchemaMappingUtil.java
URL: http://svn.apache.org/viewvc/incubator/syncope/trunk/core/src/main/java/org/syncope/core/util/SchemaMappingUtil.java?rev=1296224&r1=1296223&r2=1296224&view=diff
==============================================================================
--- incubator/syncope/trunk/core/src/main/java/org/syncope/core/util/SchemaMappingUtil.java (original)
+++ incubator/syncope/trunk/core/src/main/java/org/syncope/core/util/SchemaMappingUtil.java Fri Mar 2 14:27:34 2012
@@ -19,15 +19,42 @@
package org.syncope.core.util;
import java.util.ArrayList;
+import java.util.Collections;
import java.util.List;
+import java.util.Map;
+import org.apache.commons.collections.keyvalue.DefaultMapEntry;
import org.identityconnectors.framework.common.objects.OperationalAttributes;
import org.identityconnectors.framework.common.objects.Uid;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.syncope.core.persistence.beans.AbstractAttrValue;
import org.syncope.core.persistence.beans.AbstractAttributable;
+import org.syncope.core.persistence.beans.AbstractDerAttr;
+import org.syncope.core.persistence.beans.AbstractSchema;
+import org.syncope.core.persistence.beans.AbstractVirAttr;
import org.syncope.core.persistence.beans.SchemaMapping;
+import org.syncope.core.persistence.beans.membership.MDerSchema;
+import org.syncope.core.persistence.beans.membership.MSchema;
+import org.syncope.core.persistence.beans.membership.MVirSchema;
+import org.syncope.core.persistence.beans.role.RDerSchema;
+import org.syncope.core.persistence.beans.role.RSchema;
+import org.syncope.core.persistence.beans.role.RVirSchema;
import org.syncope.core.persistence.beans.user.SyncopeUser;
+import org.syncope.core.persistence.beans.user.UAttr;
+import org.syncope.core.persistence.beans.user.UAttrValue;
+import org.syncope.core.persistence.beans.user.UDerSchema;
+import org.syncope.core.persistence.beans.user.USchema;
+import org.syncope.core.persistence.beans.user.UVirSchema;
+import org.syncope.core.persistence.dao.SchemaDAO;
+import org.syncope.types.IntMappingType;
public class SchemaMappingUtil {
+ /**
+ * Logger.
+ */
+ protected static final Logger LOG = LoggerFactory.getLogger(SchemaMappingUtil.class);
+
public static String getExtAttrName(final SchemaMapping mapping) {
final String name;
@@ -42,8 +69,163 @@ public class SchemaMappingUtil {
return name;
}
+ public static String getIntAttrName(final SchemaMapping mapping) {
+ final String name;
+
+ switch (mapping.getIntMappingType()) {
+ case SyncopeUserId:
+ name = "id";
+ break;
+ case Username:
+ name = "username";
+ break;
+ case Password:
+ name = "password";
+ break;
+ default:
+ name = mapping.getIntAttrName();
+ }
+
+ return name;
+ }
+
+ public static String getIntAttrName(final SchemaMapping mapping, final IntMappingType type) {
+ return type == mapping.getIntMappingType() ? getIntAttrName(mapping) : null;
+ }
+
+ /**
+ * Get attribute values.
+ *
+ * @param mapping mapping.
+ * @param attributables list of attributables.
+ * @param password password.
+ * @return schema and attribute values.
+ */
+ public static Map.Entry<AbstractSchema, List<AbstractAttrValue>> getIntValues(
+ final SchemaMapping mapping,
+ final List<AbstractAttributable> attributables,
+ final String password,
+ final SchemaDAO schemaDAO) {
+
+ LOG.debug("Get attributes for '{}' and mapping type '{}'", attributables, mapping.getIntMappingType());
+
+ AbstractSchema schema = null;
+
+ List<AbstractAttrValue> values = new ArrayList<AbstractAttrValue>();
+
+ switch (mapping.getIntMappingType()) {
+ case UserSchema:
+ case RoleSchema:
+ case MembershipSchema:
+ schema = schemaDAO.find(
+ mapping.getIntAttrName(),
+ SchemaMappingUtil.getIntMappingTypeClass(mapping.getIntMappingType()));
+
+ for (AbstractAttributable attributable : attributables) {
+ final UAttr attr = attributable.getAttribute(mapping.getIntAttrName());
+
+ if (attr != null && attr.getValues() != null) {
+ values.addAll(schema.isUniqueConstraint()
+ ? Collections.singletonList(attr.getUniqueValue()) : attr.getValues());
+ }
+
+ LOG.debug("Retrieved attribute {}"
+ + "\n* IntAttrName {}"
+ + "\n* IntMappingType {}"
+ + "\n* Attribute values {}",
+ new Object[]{attr, mapping.getIntAttrName(), mapping.getIntMappingType(), values});
+ }
+
+ break;
+
+ case UserVirtualSchema:
+ case RoleVirtualSchema:
+ case MembershipVirtualSchema:
+
+ for (AbstractAttributable attributable : attributables) {
+ AbstractVirAttr virAttr = attributable.getVirtualAttribute(mapping.getIntAttrName());
+
+ if (virAttr != null && virAttr.getValues() != null) {
+ for (String value : virAttr.getValues()) {
+ AbstractAttrValue attrValue = new UAttrValue();
+ attrValue.setStringValue(value);
+ values.add(attrValue);
+ }
+ }
+
+ LOG.debug("Retrieved virtual attribute {}"
+ + "\n* IntAttrName {}"
+ + "\n* IntMappingType {}"
+ + "\n* Attribute values {}",
+ new Object[]{virAttr, mapping.getIntAttrName(), mapping.getIntMappingType(), values});
+ }
+ break;
+
+ case UserDerivedSchema:
+ case RoleDerivedSchema:
+ case MembershipDerivedSchema:
+ for (AbstractAttributable attributable : attributables) {
+ AbstractDerAttr derAttr = attributable.getDerivedAttribute(
+ mapping.getIntAttrName());
+
+ if (derAttr != null) {
+ AbstractAttrValue attrValue = new UAttrValue();
+ attrValue.setStringValue(
+ derAttr.getValue(attributable.getAttributes()));
+ values.add(attrValue);
+ }
+
+ LOG.debug("Retrieved attribute {}"
+ + "\n* IntAttrName {}"
+ + "\n* IntMappingType {}"
+ + "\n* Attribute values {}",
+ new Object[]{derAttr, mapping.getIntAttrName(),
+ mapping.getIntMappingType(), values});
+ }
+ break;
+
+ case Username:
+ for (AbstractAttributable attributable : attributables) {
+ AbstractAttrValue attrValue = new UAttrValue();
+ attrValue.setStringValue(((SyncopeUser) attributable).getUsername());
+ values.add(attrValue);
+ }
+ break;
+
+ case SyncopeUserId:
+ for (AbstractAttributable attributable : attributables) {
+ AbstractAttrValue attrValue = new UAttrValue();
+ attrValue.setStringValue(attributable.getId().toString());
+ values.add(attrValue);
+ }
+ break;
+
+ case Password:
+ AbstractAttrValue attrValue = new UAttrValue();
+
+ if (password != null) {
+ attrValue.setStringValue(password);
+ }
+
+ values.add(attrValue);
+ break;
+
+ default:
+ }
+
+ LOG.debug("Retrived values '{}'", values);
+
+ return new DefaultMapEntry(schema, values);
+ }
+
public static List<String> getIntValueAsStrings(
final AbstractAttributable attributable, final SchemaMapping mapping) {
+ return getIntValueAsStrings(attributable, mapping, null);
+ }
+
+ public static List<String> getIntValueAsStrings(
+ final AbstractAttributable attributable, final SchemaMapping mapping, String clearPassword) {
+
final List<String> value;
switch (mapping.getIntMappingType()) {
@@ -52,8 +234,12 @@ public class SchemaMappingUtil {
value.add(((SyncopeUser) attributable).getUsername());
break;
case Password:
- value = new ArrayList<String>();
- value.add(((SyncopeUser) attributable).getPassword());
+ if (clearPassword == null) {
+ value = null;
+ } else {
+ value = new ArrayList<String>();
+ value.add(clearPassword);
+ }
break;
case UserSchema:
case RoleSchema:
@@ -78,4 +264,52 @@ public class SchemaMappingUtil {
return value;
}
+
+ /**
+ * For given source mapping type, return the corresponding Class object.
+ *
+ * @param intMappingType source mapping type
+ * @return corresponding Class object, if any (can be null)
+ */
+ public static Class getIntMappingTypeClass(final IntMappingType intMappingType) {
+
+ Class result;
+
+ switch (intMappingType) {
+ case UserSchema:
+ result = USchema.class;
+ break;
+ case RoleSchema:
+ result = RSchema.class;
+ break;
+ case MembershipSchema:
+ result = MSchema.class;
+ break;
+
+ case UserDerivedSchema:
+ result = UDerSchema.class;
+ break;
+ case RoleDerivedSchema:
+ result = RDerSchema.class;
+ break;
+ case MembershipDerivedSchema:
+ result = MDerSchema.class;
+ break;
+
+ case UserVirtualSchema:
+ result = UVirSchema.class;
+ break;
+ case RoleVirtualSchema:
+ result = RVirSchema.class;
+ break;
+ case MembershipVirtualSchema:
+ result = MVirSchema.class;
+ break;
+
+ default:
+ result = null;
+ }
+
+ return result;
+ }
}
Modified: incubator/syncope/trunk/core/src/test/java/org/syncope/core/persistence/dao/ResourceTest.java
URL: http://svn.apache.org/viewvc/incubator/syncope/trunk/core/src/test/java/org/syncope/core/persistence/dao/ResourceTest.java?rev=1296224&r1=1296223&r2=1296224&view=diff
==============================================================================
--- incubator/syncope/trunk/core/src/test/java/org/syncope/core/persistence/dao/ResourceTest.java (original)
+++ incubator/syncope/trunk/core/src/test/java/org/syncope/core/persistence/dao/ResourceTest.java Fri Mar 2 14:27:34 2012
@@ -31,6 +31,7 @@ import org.syncope.core.persistence.bean
import org.syncope.core.persistence.beans.SchemaMapping;
import org.syncope.core.AbstractTest;
import org.syncope.core.persistence.validation.entity.InvalidEntityException;
+import org.syncope.core.util.SchemaMappingUtil;
import org.syncope.types.AttributableType;
import org.syncope.types.IntMappingType;
@@ -81,9 +82,8 @@ public class ResourceTest extends Abstra
@Test
public void getAccountId() {
- SchemaMapping mapping = resourceDAO.getMappingForAccountId(
- "ws-target-resource-2");
- assertEquals("fullname", mapping.getIntAttrName());
+ SchemaMapping mapping = resourceDAO.getMappingForAccountId("ws-target-resource-2");
+ assertEquals("fullname", SchemaMappingUtil.getIntAttrName(mapping));
}
@Test
@@ -213,16 +213,14 @@ public class ResourceTest extends Abstra
for (SchemaMapping schemaMapping : actual.getMappings()) {
- if ("icon".equals(schemaMapping.getIntAttrName())) {
+ if ("icon".equals(SchemaMappingUtil.getIntAttrName(schemaMapping))) {
assertTrue(IntMappingType.contains(
- AttributableType.ROLE,
- schemaMapping.getIntMappingType().toString()));
+ AttributableType.ROLE, schemaMapping.getIntMappingType().toString()));
}
- if ("mderiveddata".equals(schemaMapping.getIntAttrName())) {
+ if ("mderiveddata".equals(SchemaMappingUtil.getIntAttrName(schemaMapping))) {
assertTrue(IntMappingType.contains(
- AttributableType.MEMBERSHIP,
- schemaMapping.getIntMappingType().toString()));
+ AttributableType.MEMBERSHIP, schemaMapping.getIntMappingType().toString()));
}
}
}
Modified: incubator/syncope/trunk/core/src/test/java/org/syncope/core/persistence/relationships/ResourceTest.java
URL: http://svn.apache.org/viewvc/incubator/syncope/trunk/core/src/test/java/org/syncope/core/persistence/relationships/ResourceTest.java?rev=1296224&r1=1296223&r2=1296224&view=diff
==============================================================================
--- incubator/syncope/trunk/core/src/test/java/org/syncope/core/persistence/relationships/ResourceTest.java (original)
+++ incubator/syncope/trunk/core/src/test/java/org/syncope/core/persistence/relationships/ResourceTest.java Fri Mar 2 14:27:34 2012
@@ -44,6 +44,7 @@ import org.syncope.core.persistence.bean
import org.syncope.core.persistence.beans.PropagationTask;
import org.syncope.core.persistence.dao.PolicyDAO;
import org.syncope.core.persistence.dao.TaskDAO;
+import org.syncope.core.util.SchemaMappingUtil;
import org.syncope.types.PropagationMode;
import org.syncope.types.IntMappingType;
@@ -101,10 +102,7 @@ public class ResourceTest extends Abstra
Set<SchemaMapping> beforeUserIdMappings = new HashSet<SchemaMapping>();
for (SchemaMapping mapping : resourceDAO.findAllMappings()) {
- if (userId.getName().equals(mapping.getIntAttrName())
- && mapping.getIntMappingType()
- == IntMappingType.UserSchema) {
-
+ if (userId.getName().equals(SchemaMappingUtil.getIntAttrName(mapping, IntMappingType.UserSchema))) {
beforeUserIdMappings.add(mapping);
}
}
@@ -141,16 +139,12 @@ public class ResourceTest extends Abstra
Set<SchemaMapping> afterUserIdMappings = new HashSet<SchemaMapping>();
for (SchemaMapping mapping : resourceDAO.findAllMappings()) {
- if (userId.getName().equals(mapping.getIntAttrName())
- && mapping.getIntMappingType()
- == IntMappingType.UserSchema) {
-
+ if (userId.getName().equals(SchemaMappingUtil.getIntAttrName(mapping, IntMappingType.UserSchema))) {
afterUserIdMappings.add(mapping);
}
}
- assertEquals(beforeUserIdMappings.size(),
- afterUserIdMappings.size() - 1);
+ assertEquals(beforeUserIdMappings.size(), afterUserIdMappings.size() - 1);
}
@Test
Modified: incubator/syncope/trunk/core/src/test/java/org/syncope/core/persistence/relationships/SchemaTest.java
URL: http://svn.apache.org/viewvc/incubator/syncope/trunk/core/src/test/java/org/syncope/core/persistence/relationships/SchemaTest.java?rev=1296224&r1=1296223&r2=1296224&view=diff
==============================================================================
--- incubator/syncope/trunk/core/src/test/java/org/syncope/core/persistence/relationships/SchemaTest.java (original)
+++ incubator/syncope/trunk/core/src/test/java/org/syncope/core/persistence/relationships/SchemaTest.java Fri Mar 2 14:27:34 2012
@@ -35,6 +35,7 @@ import org.syncope.core.persistence.dao.
import org.syncope.core.persistence.dao.UserDAO;
import org.syncope.core.AbstractTest;
import org.syncope.core.util.AttributableUtil;
+import org.syncope.core.util.SchemaMappingUtil;
import org.syncope.types.AttributableType;
import org.syncope.types.IntMappingType;
@@ -66,10 +67,7 @@ public class SchemaTest extends Abstract
// check for associated mappings
Set<SchemaMapping> mappings = new HashSet<SchemaMapping>();
for (SchemaMapping mapping : resourceDAO.findAllMappings()) {
- if (schema.getName().equals(mapping.getIntAttrName())
- && mapping.getIntMappingType()
- == IntMappingType.UserSchema) {
-
+ if (schema.getName().equals(SchemaMappingUtil.getIntAttrName(mapping, IntMappingType.UserSchema))) {
mappings.add(mapping);
}
}
@@ -88,10 +86,7 @@ public class SchemaTest extends Abstract
// check for mappings deletion
mappings = new HashSet<SchemaMapping>();
for (SchemaMapping mapping : resourceDAO.findAllMappings()) {
- if ("fullname".equals(mapping.getIntAttrName())
- && mapping.getIntMappingType()
- == IntMappingType.UserSchema) {
-
+ if ("fullname".equals(SchemaMappingUtil.getIntAttrName(mapping, IntMappingType.UserSchema))) {
mappings.add(mapping);
}
}
@@ -114,10 +109,7 @@ public class SchemaTest extends Abstract
// check for associated mappings
Set<SchemaMapping> mappings = new HashSet<SchemaMapping>();
for (SchemaMapping mapping : resourceDAO.findAllMappings()) {
- if (schema.getName().equals(mapping.getIntAttrName())
- && mapping.getIntMappingType()
- == IntMappingType.UserSchema) {
-
+ if (schema.getName().equals(SchemaMappingUtil.getIntAttrName(mapping, IntMappingType.UserSchema))) {
mappings.add(mapping);
}
}