Package org.apache.syncope.core.persistence.beans

Examples of org.apache.syncope.core.persistence.beans.SyncTask


    /**
     * {@inheritDoc}
     */
    @Override
    protected boolean hasToBeRegistered(final TaskExec execution) {
        SyncTask syncTask = (SyncTask) task;

        // True if either failed and failures have to be registered, or if ALL
        // has to be registered.
        return (Status.valueOf(execution.getStatus()) == Status.FAILURE && syncTask.getResource().getSyncTraceLevel()
                .ordinal() >= TraceLevel.FAILURES.ordinal())
                || syncTask.getResource().getSyncTraceLevel() == TraceLevel.ALL;
    }
View Full Code Here


     *
     * @param delta sync delta
     * @return list of matching users
     */
    private List<Long> findExistingUsers(final SyncDelta delta) {
        final SyncTask syncTask = (SyncTask) this.task;

        final String uid = delta.getPreviousUid() == null
                ? delta.getUid().getUidValue()
                : delta.getPreviousUid().getUidValue();

        // ---------------------------------
        // Get sync policy specification
        // ---------------------------------
        final SyncPolicy policy = syncTask.getResource().getSyncPolicy();
        final SyncPolicySpec policySpec = policy != null
                ? (SyncPolicySpec) policy.getSpecification()
                : null;
        // ---------------------------------

        final List<Long> result = new ArrayList<Long>();

        if (policySpec != null && !policySpec.getAlternativeSearchAttrs().isEmpty()) {

            // search external attribute name/value
            // about each specified name
            final ConnectorObject object = delta.getObject();

            final Map<String, Attribute> extValues = new HashMap<String, Attribute>();

            for (SchemaMapping mapping : syncTask.getResource().getMappings()) {
                extValues.put(SchemaMappingUtil.getIntAttrName(mapping), object.getAttributeByName(SchemaMappingUtil
                        .getExtAttrName(mapping)));
            }

            // search user by attributes specified into the policy
            NodeCond searchCondition = null;

            for (String schema : policySpec.getAlternativeSearchAttrs()) {
                Attribute value = extValues.get(schema);

                AttributeCond.Type type;
                String expression = null;

                if (value == null || value.getValue() == null || value.getValue().isEmpty()
                        || (value.getValue().size() == 1 && value.getValue().get(0) == null)) {
                    type = AttributeCond.Type.ISNULL;
                } else {
                    type = AttributeCond.Type.EQ;
                    expression = value.getValue().size() > 1
                            ? value.getValue().toString()
                            : value.getValue().get(0).toString();
                }

                NodeCond nodeCond;

                // just Username or SyncopeUserId can be selected to be used
                if ("id".equalsIgnoreCase(schema) || "username".equalsIgnoreCase(schema)) {

                    final SyncopeUserCond cond = new SyncopeUserCond();
                    cond.setSchema(schema);
                    cond.setType(type);
                    cond.setExpression(expression);

                    nodeCond = NodeCond.getLeafCond(cond);

                } else {
                    final AttributeCond cond = new AttributeCond();
                    cond.setSchema(schema);
                    cond.setType(type);
                    cond.setExpression(expression);

                    nodeCond = NodeCond.getLeafCond(cond);
                }

                searchCondition = searchCondition != null
                        ? NodeCond.getAndCond(searchCondition, nodeCond)
                        : nodeCond;
            }

            List<SyncopeUser> users = userSearchDAO.search(EntitlementUtil.getRoleIds(entitlementDAO.findAll()),
                    searchCondition);
            for (SyncopeUser user : users) {
                result.add(user.getId());
            }
        } else {
            final SyncopeUser found;
            List<SyncopeUser> users;

            final SchemaMapping accountIdMap = SchemaMappingUtil.getAccountIdMapping(syncTask.getResource()
                    .getMappings());

            switch (accountIdMap.getIntMappingType()) {
                case Username:
                    found = userDAO.find(uid);
View Full Code Here

     * @throws JobExecutionException if anything goes wrong
     */
    protected void synchronizeMemberships(final SyncopeSyncResultHandler handler, final SyncDelta delta,
            final RoleTO roleTO) throws JobExecutionException {

        final SyncTask task = handler.getSyncTask();
        final ExternalResource resource = task.getResource();
        final Connector connector = handler.getConnector();

        for (Object membValue : getMembAttrValues(delta, connector)) {
            Long userId = handler.findMatchingAttributableId(ObjectClass.ACCOUNT, membValue.toString());
            if (userId != null) {
View Full Code Here

     *
     * @param delta sync delta
     * @return list of matching users
     */
    private List<Long> findExistingUsers(final SyncDelta delta) {
        final SyncTask syncTask = (SyncTask) this.task;

        final String uid = delta.getPreviousUid() == null
                ? delta.getUid().getUidValue()
                : delta.getPreviousUid().getUidValue();

        // ---------------------------------
        // Get sync policy specification
        // ---------------------------------
        final SyncPolicy policy = syncTask.getResource().getSyncPolicy();
        final SyncPolicySpec policySpec = policy != null
                ? (SyncPolicySpec) policy.getSpecification()
                : null;
        // ---------------------------------

        final List<Long> result = new ArrayList<Long>();

        if (policySpec != null && !policySpec.getAlternativeSearchAttrs().isEmpty()) {

            // search external attribute name/value
            // about each specified name
            final ConnectorObject object = delta.getObject();

            final Map<String, Attribute> extValues = new HashMap<String, Attribute>();

            for (SchemaMapping mapping : syncTask.getResource().getMappings()) {
                extValues.put(SchemaMappingUtil.getIntAttrName(mapping), object.getAttributeByName(SchemaMappingUtil
                        .getExtAttrName(mapping)));
            }

            // search user by attributes specified into the policy
            NodeCond searchCondition = null;

            for (String schema : policySpec.getAlternativeSearchAttrs()) {
                Attribute value = extValues.get(schema);

                AttributeCond.Type type;
                String expression = null;

                if (value == null || value.getValue() == null || value.getValue().isEmpty()) {
                    type = AttributeCond.Type.ISNULL;
                } else {
                    type = AttributeCond.Type.EQ;
                    expression = value.getValue().size() > 1
                            ? value.getValue().toString()
                            : value.getValue().get(0).toString();
                }

                NodeCond nodeCond;

                // just Username or SyncopeUserId can be selected to be used
                if ("id".equalsIgnoreCase(schema) || "username".equalsIgnoreCase(schema)) {

                    final SyncopeUserCond cond = new SyncopeUserCond();
                    cond.setSchema(schema);
                    cond.setType(type);
                    cond.setExpression(expression);

                    nodeCond = NodeCond.getLeafCond(cond);

                } else {
                    final AttributeCond cond = new AttributeCond();
                    cond.setSchema(schema);
                    cond.setType(type);
                    cond.setExpression(expression);

                    nodeCond = NodeCond.getLeafCond(cond);
                }

                searchCondition = searchCondition != null
                        ? NodeCond.getAndCond(searchCondition, nodeCond)
                        : nodeCond;
            }

            List<SyncopeUser> users = userSearchDAO.search(EntitlementUtil.getRoleIds(entitlementDAO.findAll()),
                    searchCondition);
            for (SyncopeUser user : users) {
                result.add(user.getId());
            }
        } else {
            final SyncopeUser found;
            List<SyncopeUser> users;

            final SchemaMapping accountIdMap = SchemaMappingUtil.getAccountIdMapping(syncTask.getResource()
                    .getMappings());

            switch (accountIdMap.getIntMappingType()) {
                case Username:
                    found = userDAO.find(uid);
View Full Code Here

        if (!(task instanceof SyncTask)) {
            throw new JobExecutionException("Task " + taskId + " isn't a SyncTask");
        }

        final SyncTask syncTask = (SyncTask) this.task;

        ConnectorFacadeProxy connector;
        try {
            connector = connInstanceLoader.getConnector(syncTask.getResource());
        } catch (Exception e) {
            final String msg = String.format("Connector instance bean for resource %s and connInstance %s not found",
                    syncTask.getResource(), syncTask.getResource().getConnector());

            throw new JobExecutionException(msg, e);
        }

        final SchemaMapping accountIdMap = SchemaMappingUtil.getAccountIdMapping(syncTask.getResource().getMappings());

        if (accountIdMap == null) {
            throw new JobExecutionException("Invalid account id mapping for resource " + syncTask.getResource());
        }

        LOG.debug("Execute synchronization with token {}", syncTask.getResource().getSyncToken() != null
                ? syncTask.getResource().getSyncToken().getValue()
                : null);

        final List<SyncResult> results = new ArrayList<SyncResult>();

        actions.beforeAll(syncTask);

        try {
            final SyncPolicy syncPolicy = syncTask.getResource().getSyncPolicy();

            final ConflictResolutionAction conflictResolutionAction = syncPolicy != null
                    && syncPolicy.getSpecification() != null
                    ? ((SyncPolicySpec) syncPolicy.getSpecification()).getConflictResolutionAction()
                    : ConflictResolutionAction.IGNORE;

            if (syncTask.isFullReconciliation()) {
                connector.getAllObjects(ObjectClass.ACCOUNT, new SyncResultsHandler() {

                    @Override
                    public boolean handle(final SyncDelta delta) {
                        try {

                            return results.addAll(handleDelta(syncTask, delta, conflictResolutionAction, dryRun));

                        } catch (JobExecutionException e) {
                            LOG.error("Reconciliation failed", e);
                            return false;
                        }
                    }
                }, connector.getOperationOptions(syncTask.getResource()));
            } else {
                connector.sync(syncTask.getResource().getSyncToken(), new SyncResultsHandler() {

                    @Override
                    public boolean handle(final SyncDelta delta) {
                        try {

                            return results.addAll(handleDelta(syncTask, delta, conflictResolutionAction, dryRun));

                        } catch (JobExecutionException e) {
                            LOG.error("Synchronization failed", e);
                            return false;
                        }
                    }
                }, connector.getOperationOptions(syncTask.getResource()));
            }

            if (!dryRun && !syncTask.isFullReconciliation()) {
                try {
                    ExternalResource resource = resourceDAO.find(syncTask.getResource().getName());
                    resource.setSyncToken(connector.getLatestSyncToken());
                    resourceDAO.save(resource);

                } catch (Exception e) {
                    throw new JobExecutionException("While updating SyncToken", e);
                }
            }
        } catch (Exception e) {
            throw new JobExecutionException("While syncing on connector", e);
        }

        actions.afterAll(syncTask, results);

        final String result = createReport(results, syncTask.getResource().getSyncTraceLevel(), dryRun);

        LOG.debug("Sync result: {}", result);

        return result.toString();
    }
View Full Code Here

    /**
     * {@inheritDoc}
     */
    @Override
    protected boolean hasToBeRegistered(final TaskExec execution) {
        SyncTask syncTask = (SyncTask) task;

        // True if either failed and failures have to be registered, or if ALL
        // has to be registered.
        return (Status.valueOf(execution.getStatus()) == Status.FAILURE && syncTask.getResource().getSyncTraceLevel()
                .ordinal() >= TraceLevel.FAILURES.ordinal())
                || syncTask.getResource().getSyncTraceLevel() == TraceLevel.ALL;
    }
View Full Code Here

        assertEquals(executionNumber + 1, task.getExecs().size());
    }

    @Test
    public void addSyncTaskExecution() {
        SyncTask task = taskDAO.find(4L);
        assertNotNull(task);

        int executionNumber = task.getExecs().size();

        TaskExec execution = new TaskExec();
        execution.setStatus("Text-free status");
        execution.setTask(task);
        task.addExec(execution);
        execution.setMessage("A message");

        task = taskDAO.save(task);

        taskDAO.flush();

        task = taskDAO.find(4L);
        assertNotNull(task);

        assertEquals(executionNumber + 1, task.getExecs().size());
    }
View Full Code Here

    @Test
    public void saveSyncTask() {
        ExternalResource resource = resourceDAO.find("ws-target-resource-1");
        assertNotNull(resource);

        SyncTask task = new SyncTask();
        task.setUserTemplate(new UserTO());
        task.setCronExpression("BLA BLA");

        // this save() fails because of an invalid Cron Expression
        InvalidEntityException exception = null;
        try {
            taskDAO.save(task);
        } catch (InvalidEntityException e) {
            exception = e;
        }
        assertNotNull(exception);

        task.setCronExpression(null);
        // this save() fails because a SyncTask requires a target resource
        exception = null;
        try {
            taskDAO.save(task);
        } catch (InvalidEntityException e) {
            exception = e;
        }
        assertNotNull(exception);

        task.setResource(resource);
        task.setJobActionsClassName(getClass().getName());

        // this save() fails because jobActionsClassName does not implement
        // the right interface
        exception = null;
        try {
            taskDAO.save(task);
        } catch (InvalidEntityException e) {
            exception = e;
        }
        assertNotNull(exception);

        task.setJobActionsClassName(TestSyncJobActions.class.getName());
        // this save() finally works
        task = taskDAO.save(task);
        assertNotNull(task);

        SyncTask actual = taskDAO.find(task.getId());
        assertEquals(task, actual);
    }
View Full Code Here

            pushTask.setUnmatchingRule(pushTaskTO.getUnmatchingRule() == null
                    ? UnmatchingRule.ASSIGN : pushTaskTO.getUnmatchingRule());

        } else if (task instanceof SyncTask && taskTO instanceof SyncTaskTO) {
            final SyncTask syncTask = (SyncTask) task;
            final SyncTaskTO syncTaskTO = (SyncTaskTO) taskTO;

            syncTask.setMatchingRule(syncTaskTO.getMatchingRule() == null
                    ? MatchingRule.UPDATE : syncTaskTO.getMatchingRule());

            syncTask.setUnmatchingRule(syncTaskTO.getUnmatchingRule() == null
                    ? UnmatchingRule.PROVISION : syncTaskTO.getUnmatchingRule());

            // 1. validate JEXL expressions in user and role templates
            if (syncTaskTO.getUserTemplate() != null) {
                UserTO template = syncTaskTO.getUserTemplate();

                if (StringUtils.isNotBlank(template.getUsername())
                        && !JexlUtil.isExpressionValid(template.getUsername())) {

                    sce.getElements().add("Invalid JEXL: " + template.getUsername());
                }
                if (StringUtils.isNotBlank(template.getPassword())
                        && !JexlUtil.isExpressionValid(template.getPassword())) {

                    sce.getElements().add("Invalid JEXL: " + template.getPassword());
                }

                checkJexl(template, sce);

                for (MembershipTO memb : template.getMemberships()) {
                    checkJexl(memb, sce);
                }
            }
            if (syncTaskTO.getRoleTemplate() != null) {
                RoleTO template = syncTaskTO.getRoleTemplate();

                if (StringUtils.isNotBlank(template.getName()) && !JexlUtil.isExpressionValid(template.getName())) {
                    sce.getElements().add("Invalid JEXL: " + template.getName());
                }

                checkJexl(template, sce);
            }
            if (!sce.isEmpty()) {
                throw sce;
            }

            // 2. all JEXL expressions are valid: accept user and role templates
            syncTask.setUserTemplate(syncTaskTO.getUserTemplate());
            syncTask.setRoleTemplate(syncTaskTO.getRoleTemplate());

            syncTask.setFullReconciliation(syncTaskTO.isFullReconciliation());
        }

        // 3. fill the remaining fields
        task.setPerformCreate(taskTO.isPerformCreate());
        task.setPerformUpdate(taskTO.isPerformUpdate());
View Full Code Here

        assertEquals(executionNumber + 1, task.getExecs().size());
    }

    @Test
    public void addSyncTaskExecution() {
        SyncTask task = taskDAO.find(4L);
        assertNotNull(task);

        int executionNumber = task.getExecs().size();

        TaskExec execution = new TaskExec();
        execution.setStatus("Text-free status");
        execution.setTask(task);
        task.addExec(execution);
        execution.setMessage("A message");

        taskDAO.save(task);
        taskDAO.flush();

        task = taskDAO.find(4L);
        assertNotNull(task);

        assertEquals(executionNumber + 1, task.getExecs().size());
    }
View Full Code Here

TOP

Related Classes of org.apache.syncope.core.persistence.beans.SyncTask

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.