Search in sources :

Example 1 with BatchProcessWorkProvider

use of org.alfresco.repo.batch.BatchProcessWorkProvider 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 2 with BatchProcessWorkProvider

use of org.alfresco.repo.batch.BatchProcessWorkProvider 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 3 with BatchProcessWorkProvider

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

the class AlfrescoCmisServiceImpl method bulkUpdateProperties.

@Override
public List<BulkUpdateObjectIdAndChangeToken> bulkUpdateProperties(final String repositoryId, List<BulkUpdateObjectIdAndChangeToken> objectIdAndChangeTokens, final Properties properties, final List<String> addSecondaryTypeIds, final List<String> removeSecondaryTypeIds, ExtensionsData extension) {
    checkRepositoryId(repositoryId);
    if (objectIdAndChangeTokens.size() > connector.getBulkMaxItems()) {
        throw new CmisConstraintException("Bulk update not supported for more than " + connector.getBulkMaxItems() + " objects.");
    }
    // MNT-16376 We need the CMIS call context from this thread to set
    // to the working threads below, in order to correctly identify
    // the CMIS version protocol used for this CMIS call
    final CallContext cmisCallContext = AlfrescoCmisServiceCall.get();
    // WorkProvider
    class WorkProvider implements BatchProcessWorkProvider<BulkEntry> {

        private final Iterator<BulkUpdateObjectIdAndChangeToken> iterator;

        private final int size;

        private final int batchSize;

        private BulkUpdateContext context;

        public WorkProvider(List<BulkUpdateObjectIdAndChangeToken> objectIdAndChangeTokens, BulkUpdateContext context, int batchSize) {
            this.iterator = objectIdAndChangeTokens.iterator();
            this.size = objectIdAndChangeTokens.size();
            this.context = context;
            this.batchSize = batchSize;
        }

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

        @Override
        public synchronized Collection<BulkEntry> getNextWork() {
            Collection<BulkEntry> results = new ArrayList<BulkEntry>(batchSize);
            while (results.size() < batchSize && iterator.hasNext()) {
                results.add(new BulkEntry(context, iterator.next(), properties, addSecondaryTypeIds, removeSecondaryTypeIds, getContext().isObjectInfoRequired()));
            }
            return results;
        }
    }
    BulkUpdateContext context = new BulkUpdateContext(objectIdAndChangeTokens.size());
    RetryingTransactionHelper helper = connector.getRetryingTransactionHelper();
    final String runAsUser = AuthenticationUtil.getRunAsUser();
    // Worker
    BatchProcessWorker<BulkEntry> worker = new BatchProcessWorker<BulkEntry>() {

        @Override
        public void process(final BulkEntry entry) throws Throwable {
            entry.update();
        }

        public String getIdentifier(BulkEntry entry) {
            return entry.getObjectIdAndChangeToken().getId();
        }

        @Override
        public void beforeProcess() throws Throwable {
            // Authentication
            AuthenticationUtil.pushAuthentication();
            AuthenticationUtil.setFullyAuthenticatedUser(runAsUser);
            AlfrescoCmisServiceCall.set(cmisCallContext);
        }

        @Override
        public void afterProcess() throws Throwable {
            // Clear authentication
            AuthenticationUtil.popAuthentication();
        }
    };
    // Processor
    BatchProcessor<BulkEntry> processor = new BatchProcessor<BulkEntry>("CMISbulkUpdateProperties", helper, new WorkProvider(objectIdAndChangeTokens, context, connector.getBulkBatchSize()), connector.getBulkWorkerThreads(), connector.getBulkBatchSize(), null, logger, 100);
    processor.process(worker, true);
    for (CMISNodeInfo info : context.getSuccesses()) {
        NodeRef nodeRef = info.getNodeRef();
        connector.getActivityPoster().postFileFolderUpdated(info.isFolder(), nodeRef);
    }
    return context.getChanges();
}
Also used : RetryingTransactionHelper(org.alfresco.repo.transaction.RetryingTransactionHelper) CmisConstraintException(org.apache.chemistry.opencmis.commons.exceptions.CmisConstraintException) ArrayList(java.util.ArrayList) CMISNodeInfo(org.alfresco.opencmis.dictionary.CMISNodeInfo) CallContext(org.apache.chemistry.opencmis.commons.server.CallContext) 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) BatchProcessWorkProvider(org.alfresco.repo.batch.BatchProcessWorkProvider) Iterator(java.util.Iterator) ObjectInFolderList(org.apache.chemistry.opencmis.commons.data.ObjectInFolderList) ArrayList(java.util.ArrayList) TypeDefinitionList(org.apache.chemistry.opencmis.commons.definitions.TypeDefinitionList) ObjectList(org.apache.chemistry.opencmis.commons.data.ObjectList) List(java.util.List)

Example 4 with BatchProcessWorkProvider

use of org.alfresco.repo.batch.BatchProcessWorkProvider 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 5 with BatchProcessWorkProvider

use of org.alfresco.repo.batch.BatchProcessWorkProvider 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)

Aggregations

BatchProcessWorkProvider (org.alfresco.repo.batch.BatchProcessWorkProvider)8 BatchProcessor (org.alfresco.repo.batch.BatchProcessor)8 ArrayList (java.util.ArrayList)7 RetryingTransactionHelper (org.alfresco.repo.transaction.RetryingTransactionHelper)5 NodeRef (org.alfresco.service.cmr.repository.NodeRef)5 List (java.util.List)4 BatchProcessWorker (org.alfresco.repo.batch.BatchProcessor.BatchProcessWorker)4 Iterator (java.util.Iterator)2 ChildAssociationRef (org.alfresco.service.cmr.repository.ChildAssociationRef)2 Serializable (java.io.Serializable)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 SealedObject (javax.crypto.SealedObject)1 CMISNodeInfo (org.alfresco.opencmis.dictionary.CMISNodeInfo)1 PagingRequest (org.alfresco.query.PagingRequest)1 JobSettings (org.alfresco.repo.activities.feed.JobSettings)1 RepoCtx (org.alfresco.repo.activities.feed.RepoCtx)1 NodeRefQueryCallback (org.alfresco.repo.domain.node.NodeDAO.NodeRefQueryCallback)1 NodePropertyEntity (org.alfresco.repo.domain.node.NodePropertyEntity)1 NodePropertyKey (org.alfresco.repo.domain.node.NodePropertyKey)1