Search in sources :

Example 21 with RunAsWork

use of org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork in project records-management by Alfresco.

the class HoldServiceImpl method beforeDeleteNode.

/**
 * Behaviour unfreezes node's that will no longer he held after delete.
 *
 * @see org.alfresco.repo.node.NodeServicePolicies.BeforeDeleteNodePolicy#beforeDeleteNode(org.alfresco.service.cmr.repository.NodeRef)
 */
@Behaviour(kind = BehaviourKind.CLASS, type = "rma:hold", notificationFrequency = NotificationFrequency.EVERY_EVENT)
@Override
public void beforeDeleteNode(final NodeRef hold) {
    if (nodeService.exists(hold) && isHold(hold)) {
        RunAsWork<Void> work = new RunAsWork<Void>() {

            @Override
            public Void doWork() {
                List<NodeRef> frozenNodes = getHeld(hold);
                for (NodeRef frozenNode : frozenNodes) {
                    removeFreezeAspect(frozenNode, 1);
                }
                return null;
            }
        };
        // run as system user
        authenticationUtil.runAsSystem(work);
    }
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) RunAsWork(org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork) Behaviour(org.alfresco.repo.policy.annotation.Behaviour)

Example 22 with RunAsWork

use of org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork in project records-management by Alfresco.

the class DispositionServiceImpl method updateNextDispositionAction.

/**
 * @see org.alfresco.module.org_alfresco_module_rm.disposition.DispositionService#updateNextDispositionAction(NodeRef)
 */
@Override
public void updateNextDispositionAction(final NodeRef nodeRef) {
    ParameterCheck.mandatory("nodeRef", nodeRef);
    RunAsWork<Void> runAsWork = new RunAsWork<Void>() {

        /**
         * @see org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork#doWork()
         */
        @Override
        public Void doWork() {
            // Get this disposition instructions for the node
            DispositionSchedule di = getDispositionSchedule(nodeRef);
            if (di != null) {
                // Get the current action node
                NodeRef currentDispositionAction = null;
                if (nodeService.hasAspect(nodeRef, ASPECT_DISPOSITION_LIFECYCLE)) {
                    List<ChildAssociationRef> assocs = nodeService.getChildAssocs(nodeRef, ASSOC_NEXT_DISPOSITION_ACTION, ASSOC_NEXT_DISPOSITION_ACTION);
                    if (!assocs.isEmpty()) {
                        currentDispositionAction = assocs.get(0).getChildRef();
                    }
                }
                if (currentDispositionAction != null) {
                    // Move it to the history association
                    nodeService.moveNode(currentDispositionAction, nodeRef, ASSOC_DISPOSITION_ACTION_HISTORY, ASSOC_DISPOSITION_ACTION_HISTORY);
                }
                List<DispositionActionDefinition> dispositionActionDefinitions = di.getDispositionActionDefinitions();
                DispositionActionDefinition currentDispositionActionDefinition = null;
                DispositionActionDefinition nextDispositionActionDefinition = null;
                if (currentDispositionAction == null) {
                    if (!dispositionActionDefinitions.isEmpty()) {
                        // The next disposition action is the first action
                        nextDispositionActionDefinition = dispositionActionDefinitions.get(0);
                    }
                } else {
                    // Get the current action
                    String currentADId = (String) nodeService.getProperty(currentDispositionAction, PROP_DISPOSITION_ACTION_ID);
                    currentDispositionActionDefinition = di.getDispositionActionDefinition(currentADId);
                    // In this case it will be searched by name
                    if (currentDispositionActionDefinition == null) {
                        String currentADName = (String) nodeService.getProperty(currentDispositionAction, PROP_DISPOSITION_ACTION);
                        currentDispositionActionDefinition = di.getDispositionActionDefinitionByName(currentADName);
                    }
                    // Get the next disposition action
                    int index = currentDispositionActionDefinition.getIndex();
                    index++;
                    if (index < dispositionActionDefinitions.size()) {
                        nextDispositionActionDefinition = dispositionActionDefinitions.get(index);
                    }
                }
                if (nextDispositionActionDefinition != null) {
                    if (!nodeService.hasAspect(nodeRef, ASPECT_DISPOSITION_LIFECYCLE)) {
                        // Add the disposition life cycle aspect
                        nodeService.addAspect(nodeRef, ASPECT_DISPOSITION_LIFECYCLE, null);
                    }
                    initialiseDispositionAction(nodeRef, nextDispositionActionDefinition);
                }
            }
            return null;
        }
    };
    AuthenticationUtil.runAsSystem(runAsWork);
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) RunAsWork(org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork) ChildAssociationRef(org.alfresco.service.cmr.repository.ChildAssociationRef)

Example 23 with RunAsWork

use of org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork in project records-management by Alfresco.

the class DataLoadSystemTest method loadInPlace.

private void loadInPlace() {
    AuthenticationUtil.runAs(new RunAsWork<Void>() {

        public Void doWork() throws Exception {
            final SiteInfo site = siteService.getSite("test");
            if (site == null) {
                throw new AlfrescoRuntimeException("The collab site test is not present.");
            }
            final NodeRef filePlan = filePlanService.getFilePlanBySiteId(FilePlanService.DEFAULT_RM_SITE_ID);
            if (filePlan == null) {
                Assert.fail("The default RM site is not present.");
            }
            // create users and add to site
            repeatInTransactionBatches(new RunAsWork<Void>() {

                public Void doWork() throws Exception {
                    // create user
                    String userName = GUID.generate();
                    System.out.println("Creating user " + userName);
                    createPerson(userName, true);
                    // add to collab site
                    siteService.setMembership("test", userName, SiteRole.SiteCollaborator.toString());
                    return null;
                }
            }, USER_COUNT);
            // create content and declare as record
            repeatInTransactionBatches(new RunAsWork<Void>() {

                public Void doWork() throws Exception {
                    // create document
                    NodeRef docLib = siteService.getContainer(site.getShortName(), SiteService.DOCUMENT_LIBRARY);
                    NodeRef document = fileFolderService.create(docLib, GUID.generate(), ContentModel.TYPE_CONTENT).getNodeRef();
                    recordService.createRecord(filePlan, document);
                    return null;
                }
            }, INPLACE_RECORD_COUNT);
            return null;
        }
    }, AuthenticationUtil.getAdminUserName());
}
Also used : SiteInfo(org.alfresco.service.cmr.site.SiteInfo) NodeRef(org.alfresco.service.cmr.repository.NodeRef) RunAsWork(org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException)

Example 24 with RunAsWork

use of org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork in project records-management by Alfresco.

the class RecordServiceImpl method recordCreationSanityCheckOnFilePlan.

/**
 * Helper method to check the given file plan before trying to determine the unfiled records container.
 *
 * @param filePlan The reference of the file plan node
 */
private NodeRef recordCreationSanityCheckOnFilePlan(NodeRef filePlan) {
    NodeRef result = null;
    if (filePlan == null) {
        // TODO .. eventually make the file plan parameter required
        result = AuthenticationUtil.runAs(new RunAsWork<NodeRef>() {

            @Override
            public NodeRef doWork() {
                return filePlanService.getFilePlanBySiteId(FilePlanService.DEFAULT_RM_SITE_ID);
            }
        }, AuthenticationUtil.getAdminUserName());
        // if the file plan is still null, raise an exception
        if (result == null) {
            String msg = "Cannot create record, because the default file plan cannot be determined. Make sure at least one file plan has been created.";
            LOGGER.debug(msg);
            throw new RecordCreationException(msg);
        }
    } else {
        // verify that the provided file plan is actually a file plan
        if (!filePlanService.isFilePlan(filePlan)) {
            String msg = "Cannot create record, because the provided file plan node reference is not a file plan.";
            LOGGER.debug(msg);
            throw new RecordCreationException(msg);
        }
        result = filePlan;
    }
    return result;
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) RunAsWork(org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork)

Example 25 with RunAsWork

use of org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork in project records-management by Alfresco.

the class RecordServiceImpl method createRecordFromContent.

/**
 * @see org.alfresco.module.org_alfresco_module_rm.record.RecordService#createNewRecord(org.alfresco.service.cmr.repository.NodeRef, java.lang.String, org.alfresco.service.namespace.QName, java.util.Map, org.alfresco.service.cmr.repository.ContentReader)
 */
@Override
public NodeRef createRecordFromContent(NodeRef parent, String name, QName type, Map<QName, Serializable> properties, ContentReader reader) {
    ParameterCheck.mandatory("nodeRef", parent);
    ParameterCheck.mandatory("name", name);
    NodeRef result = null;
    NodeRef destination = parent;
    if (isFilePlan(parent)) {
        // get the unfiled record container for the file plan
        destination = filePlanService.getUnfiledContainer(parent);
        if (destination == null) {
            throw new AlfrescoRuntimeException("Unable to create record, because unfiled container could not be found.");
        }
    }
    // if none set the default record type is cm:content
    if (type == null) {
        type = ContentModel.TYPE_CONTENT;
    } else if (!dictionaryService.isSubClass(type, ContentModel.TYPE_CONTENT)) {
        throw new AlfrescoRuntimeException("Record can only be created from a sub-type of cm:content.");
    }
    disablePropertyEditableCheck();
    try {
        // create the new record
        final NodeRef record = fileFolderService.create(destination, name, type).getNodeRef();
        // set the properties
        if (properties != null) {
            nodeService.addProperties(record, properties);
        }
        // set the content
        if (reader != null) {
            ContentWriter writer = fileFolderService.getWriter(record);
            writer.setEncoding(reader.getEncoding());
            writer.setMimetype(reader.getMimetype());
            writer.putContent(reader);
        }
        result = authenticationUtil.runAsSystem(new RunAsWork<NodeRef>() {

            public NodeRef doWork() throws Exception {
                // a destination for the report has been selected.
                if (!nodeService.hasAspect(record, ASPECT_RECORD)) {
                    // make record
                    makeRecord(record);
                    generateRecordIdentifier(nodeService, identifierService, record);
                }
                return record;
            }
        });
    } finally {
        enablePropertyEditableCheck();
    }
    return result;
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) ContentWriter(org.alfresco.service.cmr.repository.ContentWriter) RunAsWork(org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException)

Aggregations

RunAsWork (org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork)34 NodeRef (org.alfresco.service.cmr.repository.NodeRef)26 HashMap (java.util.HashMap)11 Serializable (java.io.Serializable)8 AlfrescoRuntimeException (org.alfresco.error.AlfrescoRuntimeException)7 FileNotFoundException (org.alfresco.service.cmr.model.FileNotFoundException)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 FileExistsException (org.alfresco.service.cmr.model.FileExistsException)4 ContentWriter (org.alfresco.service.cmr.repository.ContentWriter)4 QName (org.alfresco.service.namespace.QName)4 HashSet (java.util.HashSet)3 Map (java.util.Map)3 Set (java.util.Set)3 AccessDeniedException (org.alfresco.repo.security.permissions.AccessDeniedException)3 ChildAssociationRef (org.alfresco.service.cmr.repository.ChildAssociationRef)3 SiteInfo (org.alfresco.service.cmr.site.SiteInfo)3 Version (org.alfresco.service.cmr.version.Version)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2