Search in sources :

Example 1 with BatchProcessor

use of org.alfresco.repo.batch.BatchProcessor in project alfresco-repository by Alfresco.

the class RenameSiteAuthorityDisplayName method renameDispayNames.

/**
 * Rename display names of authorities of sites.
 *
 * @param siteInfos
 *            list of sites
 */
private void renameDispayNames(final List<SiteInfo> siteInfos) {
    final String tenantDomain = tenantAdminService.getCurrentUserDomain();
    final Iterator<SiteInfo> pathItr = siteInfos.listIterator();
    BatchProcessWorkProvider<SiteInfo> siteWorkProvider = new BatchProcessWorkProvider<SiteInfo>() {

        @Override
        public int getTotalEstimatedWorkSize() {
            return siteInfos.size();
        }

        @Override
        public Collection<SiteInfo> getNextWork() {
            int batchCount = 0;
            List<SiteInfo> nodes = new ArrayList<SiteInfo>(BATCH_SIZE);
            while (pathItr.hasNext() && batchCount++ != BATCH_SIZE) {
                nodes.add(pathItr.next());
            }
            return nodes;
        }
    };
    // prepare the batch processor and worker object
    BatchProcessor<SiteInfo> siteBatchProcessor = new BatchProcessor<SiteInfo>("RenameSiteAuthorityDisplayName", this.transactionHelper, siteWorkProvider, BATCH_THREADS, BATCH_SIZE, this.applicationEventPublisher, progress_logger, BATCH_SIZE * 10);
    BatchProcessWorker<SiteInfo> worker = new BatchProcessWorker<SiteInfo>() {

        @Override
        public String getIdentifier(SiteInfo entry) {
            return entry.getShortName();
        }

        @Override
        public void beforeProcess() throws Throwable {
            // Disable rules
            ruleService.disableRules();
            // Authentication
            String systemUser = AuthenticationUtil.getSystemUserName();
            systemUser = tenantAdminService.getDomainUser(systemUser, tenantDomain);
            AuthenticationUtil.setRunAsUser(systemUser);
        }

        @Override
        public void afterProcess() throws Throwable {
            // Enable rules
            ruleService.enableRules();
            // Clear authentication
            AuthenticationUtil.clearCurrentSecurityContext();
        }

        @Override
        public void process(SiteInfo siteInfo) throws Throwable {
            // Set all the permissions of site
            Set<AccessPermission> sitePermissions = permissionService.getAllSetPermissions(siteInfo.getNodeRef());
            for (AccessPermission sitePermission : sitePermissions) {
                // Use only GROUP authority
                if (sitePermission.getAuthorityType() == AuthorityType.GROUP) {
                    String authorityName = sitePermission.getAuthority();
                    String currDisplayName = authorityService.getAuthorityDisplayName(authorityName);
                    String necessaryName = ((SiteServiceImpl) siteService).getSiteRoleGroup(siteInfo.getShortName(), sitePermission.getPermission(), false);
                    String alternativeName = ((SiteServiceImpl) siteService).getSiteRoleGroup(siteInfo.getShortName(), sitePermission.getPermission(), true);
                    // check for correct displayName
                    if ((!necessaryName.equalsIgnoreCase(currDisplayName)) || (!alternativeName.equalsIgnoreCase(currDisplayName))) {
                        // fix incorrect display name
                        authorityService.setAuthorityDisplayName(authorityName, necessaryName);
                    }
                }
            }
        }
    };
    siteBatchProcessor.process(worker, true);
}
Also used : SiteInfo(org.alfresco.service.cmr.site.SiteInfo) ArrayList(java.util.ArrayList) AccessPermission(org.alfresco.service.cmr.security.AccessPermission) SiteServiceImpl(org.alfresco.repo.site.SiteServiceImpl) BatchProcessWorkProvider(org.alfresco.repo.batch.BatchProcessWorkProvider) BatchProcessWorker(org.alfresco.repo.batch.BatchProcessor.BatchProcessWorker) BatchProcessor(org.alfresco.repo.batch.BatchProcessor)

Example 2 with BatchProcessor

use of org.alfresco.repo.batch.BatchProcessor in project alfresco-repository by Alfresco.

the class ImapUnsubscribedAspectPatch method applyInternal.

@Override
protected String applyInternal() throws Exception {
    final List<ChildAssociationRef> users = nodeService.getChildAssocs(personService.getPeopleContainer(), ContentModel.ASSOC_CHILDREN, RegexQNamePattern.MATCH_ALL);
    final long maxNodeId = patchDAO.getMaxAdmNodeID();
    BatchProcessWorkProvider<NodeRef> workProvider = new BatchProcessWorkProvider<NodeRef>() {

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

        public int getTotalEstimatedWorkSize() {
            return result.size();
        }

        public Collection<NodeRef> getNextWork() {
            result.clear();
            while (result.isEmpty() && minSearchNodeId < maxNodeId) {
                nodeDAO.getNodesWithAspects(Collections.singleton(ASPECT_NON_SUBSCRIBED), minSearchNodeId, minSearchNodeId + count, new NodeRefQueryCallback() {

                    public boolean handle(Pair<Long, NodeRef> nodePair) {
                        result.add(nodePair.getSecond());
                        return true;
                    }
                });
                minSearchNodeId = minSearchNodeId + count + 1;
            }
            return result;
        }
    };
    BatchProcessor<NodeRef> batchProcessor = new BatchProcessor<NodeRef>("ImapUnsubscribedAspectPatch", transactionService.getRetryingTransactionHelper(), workProvider, batchThreads, batchSize, applicationEventPublisher, null, 1000);
    BatchProcessWorker<NodeRef> worker = new BatchProcessWorker<NodeRef>() {

        public void afterProcess() throws Throwable {
        }

        public void beforeProcess() throws Throwable {
        }

        public String getIdentifier(NodeRef entry) {
            return entry.toString();
        }

        public void process(NodeRef entry) throws Throwable {
            nodeService.removeAspect(entry, ImapModel.ASPECT_IMAP_FOLDER_NONSUBSCRIBED);
            for (ChildAssociationRef userRef : users) {
                nodeService.createAssociation(userRef.getChildRef(), entry, ImapModel.ASSOC_IMAP_UNSUBSCRIBED);
            }
        }
    };
    batchProcessor.process(worker, true);
    return I18NUtil.getMessage(MSG_NONSUBSCRIBED_ASPECT_REMOVED);
}
Also used : NodeRefQueryCallback(org.alfresco.repo.domain.node.NodeDAO.NodeRefQueryCallback) ChildAssociationRef(org.alfresco.service.cmr.repository.ChildAssociationRef) BatchProcessWorkProvider(org.alfresco.repo.batch.BatchProcessWorkProvider) BatchProcessWorker(org.alfresco.repo.batch.BatchProcessor.BatchProcessWorker) NodeRef(org.alfresco.service.cmr.repository.NodeRef) BatchProcessor(org.alfresco.repo.batch.BatchProcessor) ArrayList(java.util.ArrayList) List(java.util.List)

Example 3 with BatchProcessor

use of org.alfresco.repo.batch.BatchProcessor in project alfresco-repository by Alfresco.

the class AddUnmovableAspectToSitesPatch method applyInternal.

@Override
protected String applyInternal() throws Exception {
    BatchProcessWorkProvider<ChildAssociationRef> workProvider = new BatchProcessWorkProvider<ChildAssociationRef>() {

        NodeRef sitesRoot = siteService.getSiteRoot();

        List<ChildAssociationRef> sites = nodeService.getChildAssocs(sitesRoot, Collections.singleton(SiteModel.TYPE_SITE));

        final Iterator<ChildAssociationRef> iterator = sites.listIterator();

        @Override
        public int getTotalEstimatedWorkSize() {
            return sites.size();
        }

        @Override
        public Collection<ChildAssociationRef> getNextWork() {
            List<ChildAssociationRef> sites = new ArrayList<ChildAssociationRef>(BATCH_SIZE);
            while (iterator.hasNext() && sites.size() <= BATCH_SIZE) {
                sites.add(iterator.next());
            }
            return sites;
        }
    };
    RetryingTransactionHelper txnHelper = transactionService.getRetryingTransactionHelper();
    txnHelper.setForceWritable(true);
    BatchProcessor<ChildAssociationRef> batchProcessor = new BatchProcessor<ChildAssociationRef>("AddUnmovableAspectToSitesPatch", txnHelper, workProvider, NUM_THREADS, BATCH_SIZE, applicationEventPublisher, logger, 1000);
    final String authenticatedUser = AuthenticationUtil.getFullyAuthenticatedUser();
    BatchProcessor.BatchProcessWorker<ChildAssociationRef> worker = new BatchProcessor.BatchProcessWorker<ChildAssociationRef>() {

        public void afterProcess() throws Throwable {
        }

        public void beforeProcess() throws Throwable {
        }

        public String getIdentifier(ChildAssociationRef entry) {
            return entry.toString();
        }

        public void process(final ChildAssociationRef child) throws Throwable {
            /*
                 * Fix for MNT-15064.
                 * Run as authenticated user to make sure the nodes are searched in the correct space store.
                 */
            RunAsWork<Void> work = new RunAsWork<Void>() {

                @Override
                public Void doWork() throws Exception {
                    try {
                        behaviourFilter.disableBehaviour(ContentModel.ASPECT_AUDITABLE);
                        nodeService.addAspect(child.getChildRef(), ContentModel.ASPECT_UNMOVABLE, null);
                    } finally {
                        behaviourFilter.enableBehaviour(ContentModel.ASPECT_AUDITABLE);
                    }
                    return null;
                }
            };
            AuthenticationUtil.runAs(work, authenticatedUser);
        }
    };
    batchProcessor.process(worker, true);
    return I18NUtil.getMessage(MSG_SUCCESS);
}
Also used : RetryingTransactionHelper(org.alfresco.repo.transaction.RetryingTransactionHelper) RunAsWork(org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork) ArrayList(java.util.ArrayList) ChildAssociationRef(org.alfresco.service.cmr.repository.ChildAssociationRef) BatchProcessWorkProvider(org.alfresco.repo.batch.BatchProcessWorkProvider) NodeRef(org.alfresco.service.cmr.repository.NodeRef) BatchProcessor(org.alfresco.repo.batch.BatchProcessor) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List)

Example 4 with BatchProcessor

use of org.alfresco.repo.batch.BatchProcessor in project alfresco-repository by Alfresco.

the class AliasableAspectPatch method applyInternal.

@Override
protected String applyInternal() throws Exception {
    BatchProcessWorkProvider<NodeRef> workProvider = new BatchProcessWorkProvider<NodeRef>() {

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

        Long aspectQNameId = 0L;

        long maxNodeId = getPatchDAO().getMaxAdmNodeID();

        long minSearchNodeId = 1;

        long maxSearchNodeId = count;

        Pair<Long, QName> val = getQnameDAO().getQName(EmailServerModel.ASPECT_ALIASABLE);

        public int getTotalEstimatedWorkSize() {
            return result.size();
        }

        public Collection<NodeRef> getNextWork() {
            if (val != null) {
                Long aspectQNameId = val.getFirst();
                result.clear();
                while (result.isEmpty() && minSearchNodeId < maxNodeId) {
                    List<Long> nodeids = getPatchDAO().getNodesByAspectQNameId(aspectQNameId, minSearchNodeId, maxSearchNodeId);
                    for (Long nodeid : nodeids) {
                        NodeRef.Status status = getNodeDAO().getNodeIdStatus(nodeid);
                        if (!status.isDeleted()) {
                            result.add(status.getNodeRef());
                        }
                    }
                    minSearchNodeId = minSearchNodeId + count;
                    maxSearchNodeId = maxSearchNodeId + count;
                }
            }
            return result;
        }
    };
    RetryingTransactionHelper txnHelper = transactionService.getRetryingTransactionHelper();
    // Configure the helper to run in read-only mode
    // MNT-10764
    txnHelper.setForceWritable(true);
    BatchProcessor<NodeRef> batchProcessor = new BatchProcessor<NodeRef>("AliasableAspectPatch", txnHelper, workProvider, batchThreads, batchSize, applicationEventPublisher, logger, 1000);
    BatchProcessWorker<NodeRef> worker = new BatchProcessWorker<NodeRef>() {

        public void afterProcess() throws Throwable {
        }

        public void beforeProcess() throws Throwable {
        }

        public String getIdentifier(NodeRef entry) {
            return entry.toString();
        }

        public void process(NodeRef entry) throws Throwable {
            String alias = (String) nodeService.getProperty(entry, EmailServerModel.PROP_ALIAS);
            if (alias != null) {
                NodeRef existing = (NodeRef) getAttributeService().getAttribute(AliasableAspect.ALIASABLE_ATTRIBUTE_KEY_1, AliasableAspect.ALIASABLE_ATTRIBUTE_KEY_2, AliasableAspect.normaliseAlias(alias));
                if (existing != null) {
                    if (!existing.equals(entry)) {
                        // alias is used by more than one node - warning of some sort?
                        if (logger.isWarnEnabled()) {
                            logger.warn("Email alias is not unique, alias:" + alias + " nodeRef:" + entry);
                        }
                        try {
                            behaviourFilter.disableBehaviour(EmailServerModel.ASPECT_ALIASABLE);
                            nodeService.removeAspect(entry, EmailServerModel.ASPECT_ALIASABLE);
                        } finally {
                            behaviourFilter.enableBehaviour(EmailServerModel.ASPECT_ALIASABLE);
                        }
                    }
                // else do nothing - attribute already exists.
                } else {
                    if (logger.isDebugEnabled()) {
                        logger.debug("creating email alias attribute for " + alias);
                    }
                    getAttributeService().createAttribute(entry, AliasableAspect.ALIASABLE_ATTRIBUTE_KEY_1, AliasableAspect.ALIASABLE_ATTRIBUTE_KEY_2, AliasableAspect.normaliseAlias(alias));
                }
            }
        }
    };
    // Now set the batch processor to work
    batchProcessor.process(worker, true);
    return I18NUtil.getMessage(MSG_SUCCESS);
}
Also used : RetryingTransactionHelper(org.alfresco.repo.transaction.RetryingTransactionHelper) BatchProcessWorkProvider(org.alfresco.repo.batch.BatchProcessWorkProvider) BatchProcessWorker(org.alfresco.repo.batch.BatchProcessor.BatchProcessWorker) NodeRef(org.alfresco.service.cmr.repository.NodeRef) BatchProcessor(org.alfresco.repo.batch.BatchProcessor) ArrayList(java.util.ArrayList) List(java.util.List) Pair(org.alfresco.util.Pair)

Example 5 with BatchProcessor

use of org.alfresco.repo.batch.BatchProcessor in project alfresco-repository by Alfresco.

the class UpgradePasswordHashWorker method doWork.

/**
 * @param progress          the thread-safe progress
 */
private synchronized void doWork(UpgradePasswordHashWorkResult progress) throws Exception {
    // Build batch processor
    BatchProcessWorkProvider<Long> workProvider = new UpgradePasswordHashWorkProvider(progress);
    BatchProcessWorker<Long> worker = new UpgradePasswordHashBatch(progress);
    RetryingTransactionHelper retryingTransactionHelper = transactionService.getRetryingTransactionHelper();
    retryingTransactionHelper.setForceWritable(true);
    // Create the QNames if they don't exist
    retryingTransactionHelper.doInTransaction(new RetryingTransactionHelper.RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            qnameDAO.getOrCreateQName(ContentModel.PROP_PASSWORD_HASH);
            qnameDAO.getOrCreateQName(ContentModel.PROP_HASH_INDICATOR);
            return null;
        }
    }, false, true);
    BatchProcessor<Long> batchProcessor = new BatchProcessor<Long>("UpgradePasswordHashWorker", retryingTransactionHelper, workProvider, threadCount, batchSize, ctx, logger, 1000);
    batchProcessor.process(worker, true);
}
Also used : BatchProcessor(org.alfresco.repo.batch.BatchProcessor) RetryingTransactionHelper(org.alfresco.repo.transaction.RetryingTransactionHelper) AtomicLong(java.util.concurrent.atomic.AtomicLong)

Aggregations

BatchProcessor (org.alfresco.repo.batch.BatchProcessor)15 BatchProcessWorkProvider (org.alfresco.repo.batch.BatchProcessWorkProvider)10 ArrayList (java.util.ArrayList)7 RetryingTransactionHelper (org.alfresco.repo.transaction.RetryingTransactionHelper)7 NodeRef (org.alfresco.service.cmr.repository.NodeRef)7 List (java.util.List)4 BatchProcessWorker (org.alfresco.repo.batch.BatchProcessor.BatchProcessWorker)4 Serializable (java.io.Serializable)2 Iterator (java.util.Iterator)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 AlfrescoRuntimeException (org.alfresco.error.AlfrescoRuntimeException)2 NodePropertyEntity (org.alfresco.repo.domain.node.NodePropertyEntity)2 LockAcquisitionException (org.alfresco.repo.lock.LockAcquisitionException)2 RunAsWork (org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork)2 ChildAssociationRef (org.alfresco.service.cmr.repository.ChildAssociationRef)2 InvalidNodeRefException (org.alfresco.service.cmr.repository.InvalidNodeRefException)2 QName (org.alfresco.service.namespace.QName)2 File (java.io.File)1 HashMap (java.util.HashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1