Search in sources :

Example 1 with RuleServiceException

use of org.alfresco.service.cmr.rule.RuleServiceException in project alfresco-repository by Alfresco.

the class RuleLinkTest method testLinkRule.

@Test
public void testLinkRule() {
    // Create a rule
    Rule rule = createTestRule(false, "bobs rule");
    this.ruleService.saveRule(folderOne, rule);
    assertTrue(this.ruleService.hasRules(folderOne));
    assertEquals(1, ruleService.getRules(folderOne, false).size());
    assertFalse(ruleService.isLinkedToRuleNode(folderOne));
    assertFalse(this.ruleService.hasRules(folderTwo));
    assertEquals(0, ruleService.getRules(folderTwo, false).size());
    assertFalse(ruleService.isLinkedToRuleNode(folderTwo));
    Action linkAction = actionService.createAction(LinkRules.NAME);
    linkAction.setParameterValue(LinkRules.PARAM_LINK_FROM_NODE, folderOne);
    actionService.executeAction(linkAction, folderTwo);
    assertTrue(this.ruleService.hasRules(folderOne));
    assertEquals(1, ruleService.getRules(folderOne, false).size());
    assertFalse(ruleService.isLinkedToRuleNode(folderOne));
    assertTrue(this.ruleService.hasRules(folderTwo));
    assertEquals(1, ruleService.getRules(folderTwo, false).size());
    boolean value = ruleService.isLinkedToRuleNode(folderTwo);
    assertTrue(value);
    assertEquals(folderOne, ruleService.getLinkedToRuleNode(folderTwo));
    List<NodeRef> linkedFrom = ruleService.getLinkedFromRuleNodes(folderTwo);
    assertNotNull(linkedFrom);
    assertTrue(linkedFrom.isEmpty());
    linkedFrom = ruleService.getLinkedFromRuleNodes(folderOne);
    assertNotNull(linkedFrom);
    assertEquals(1, linkedFrom.size());
    assertEquals(folderTwo, linkedFrom.get(0));
    // Check that you can't modify the rules on a linked rule node
    try {
        rule = createTestRule(false, "bobs rule 2");
        this.ruleService.saveRule(folderTwo, rule);
        fail("Shouldn't be able to add a new rule to a linked rule set");
    } catch (RuleServiceException e) {
    // Expected
    }
    // Add another rule to folder one
    rule = createTestRule(false, "bobs other rule");
    this.ruleService.saveRule(folderOne, rule);
    assertTrue(this.ruleService.hasRules(folderOne));
    assertEquals(2, ruleService.getRules(folderOne, false).size());
    assertFalse(ruleService.isLinkedToRuleNode(folderOne));
    assertTrue(this.ruleService.hasRules(folderTwo));
    assertEquals(2, ruleService.getRules(folderTwo, false).size());
    value = ruleService.isLinkedToRuleNode(folderTwo);
    assertTrue(value);
    assertEquals(folderOne, ruleService.getLinkedToRuleNode(folderTwo));
    linkedFrom = ruleService.getLinkedFromRuleNodes(folderTwo);
    assertNotNull(linkedFrom);
    assertTrue(linkedFrom.isEmpty());
    linkedFrom = ruleService.getLinkedFromRuleNodes(folderOne);
    assertNotNull(linkedFrom);
    assertEquals(1, linkedFrom.size());
    assertEquals(folderTwo, linkedFrom.get(0));
    // Unlink
    unlink(folderTwo);
    assertTrue(this.ruleService.hasRules(folderOne));
    assertEquals(2, ruleService.getRules(folderOne, false).size());
    assertFalse(ruleService.isLinkedToRuleNode(folderOne));
    assertFalse(this.ruleService.hasRules(folderTwo));
    assertEquals(0, ruleService.getRules(folderTwo, false).size());
    assertFalse(ruleService.isLinkedToRuleNode(folderTwo));
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) Action(org.alfresco.service.cmr.action.Action) RuleServiceException(org.alfresco.service.cmr.rule.RuleServiceException) Rule(org.alfresco.service.cmr.rule.Rule) BaseSpringTest(org.alfresco.util.BaseSpringTest) Test(org.junit.Test)

Example 2 with RuleServiceException

use of org.alfresco.service.cmr.rule.RuleServiceException in project alfresco-repository by Alfresco.

the class TransformActionExecuter method executeImpl.

/**
 * @see org.alfresco.repo.action.executer.ActionExecuterAbstractBase#executeImpl(Action, org.alfresco.service.cmr.repository.NodeRef)
 */
@Override
protected void executeImpl(Action ruleAction, NodeRef actionedUponNodeRef) {
    if (this.nodeService.exists(actionedUponNodeRef) == false) {
        // node doesn't exist - can't do anything
        return;
    }
    // First check that the node is a sub-type of content
    QName typeQName = this.nodeService.getType(actionedUponNodeRef);
    if (this.dictionaryService.isSubClass(typeQName, ContentModel.TYPE_CONTENT) == false) {
        // it is not content, so can't transform
        return;
    }
    // Get the mime type
    String mimeType = (String) ruleAction.getParameterValue(PARAM_MIME_TYPE);
    // Get the content reader
    ContentReader contentReader = this.contentService.getReader(actionedUponNodeRef, ContentModel.PROP_CONTENT);
    if (null == contentReader || !contentReader.exists()) {
        throw new RuleServiceException(CONTENT_READER_NOT_FOUND_MESSAGE);
    }
    TransformationOptions transformationOptions = newTransformationOptions(ruleAction, actionedUponNodeRef);
    // getExecuteAsynchronously() is not true for async convert content rules, so using Thread name
    // options.setUse(ruleAction.getExecuteAsynchronously() ? "asyncRule" :"syncRule");
    transformationOptions.setUse(Thread.currentThread().getName().contains("Async") ? "asyncRule" : "syncRule");
    String sourceMimetype = contentReader.getMimetype();
    long sourceSizeInBytes = contentReader.getSize();
    String contentUrl = contentReader.getContentUrl();
    Map<String, String> options = converter.getOptions(transformationOptions, sourceMimetype, mimeType);
    if (!synchronousTransformClient.isSupported(sourceMimetype, sourceSizeInBytes, contentUrl, mimeType, options, null, actionedUponNodeRef)) {
        String optionsString = TransformerDebug.toString(options);
        throw new RuleServiceException(String.format(TRANSFORMER_NOT_EXISTS_MESSAGE_PATTERN, sourceMimetype, mimeType, optionsString));
    }
    // Get the details of the copy destination
    NodeRef destinationParent = (NodeRef) ruleAction.getParameterValue(PARAM_DESTINATION_FOLDER);
    QName destinationAssocTypeQName = (QName) ruleAction.getParameterValue(PARAM_ASSOC_TYPE_QNAME);
    QName destinationAssocQName = (QName) ruleAction.getParameterValue(PARAM_ASSOC_QNAME);
    // default the assoc params if they're not present
    if (destinationAssocTypeQName == null) {
        destinationAssocTypeQName = ContentModel.ASSOC_CONTAINS;
    }
    if (destinationAssocQName == null) {
        destinationAssocQName = QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "copy");
    }
    // Get the overwirte value
    boolean overwrite = true;
    Boolean overwriteValue = (Boolean) ruleAction.getParameterValue(PARAM_OVERWRITE_COPY);
    if (overwriteValue != null) {
        overwrite = overwriteValue.booleanValue();
    }
    // Calculate the destination name
    String originalName = (String) nodeService.getProperty(actionedUponNodeRef, ContentModel.PROP_NAME);
    String newName = transformName(this.mimetypeService, originalName, mimeType, true);
    // Since we are overwriting we need to figure out whether the destination node exists
    NodeRef copyNodeRef = null;
    if (overwrite == true) {
        // Try and find copies of the actioned upon node reference.
        // Include the parent folder because that's where the copy will be if this action
        // had done the first copy.
        PagingResults<CopyInfo> copies = copyService.getCopies(actionedUponNodeRef, destinationParent, new PagingRequest(1000));
        for (CopyInfo copyInfo : copies.getPage()) {
            NodeRef copy = copyInfo.getNodeRef();
            String copyName = copyInfo.getName();
            // We know that it is in the destination parent, but avoid working copies
            if (checkOutCheckInService.isWorkingCopy(copy)) {
                // It is a working copy
                continue;
            } else if (!newName.equals(copyName)) {
                // The copy's name is not what this action would have set it to
                continue;
            }
            if (copyNodeRef == null) {
                copyNodeRef = copy;
            } else {
                throw new RuleServiceException(ERR_OVERWRITE);
            }
        }
    }
    if (copyNodeRef == null) {
        // Copy the content node
        copyNodeRef = this.copyService.copy(actionedUponNodeRef, destinationParent, destinationAssocTypeQName, QName.createQName(destinationAssocQName.getNamespaceURI(), newName));
        // Adjust the name of the copy
        nodeService.setProperty(copyNodeRef, ContentModel.PROP_NAME, newName);
        String originalTitle = (String) nodeService.getProperty(actionedUponNodeRef, ContentModel.PROP_TITLE);
        if (originalTitle != null) {
            nodeService.setProperty(copyNodeRef, ContentModel.PROP_TITLE, originalTitle);
        }
    }
    // Only do the transformation if some content is available
    if (contentReader != null) {
        // get the writer and set it up
        ContentWriter contentWriter = this.contentService.getWriter(copyNodeRef, ContentModel.PROP_CONTENT, true);
        // new mimetype
        contentWriter.setMimetype(mimeType);
        // original encoding
        contentWriter.setEncoding(contentReader.getEncoding());
        // TODO: Check failure patterns for actions.
        try {
            doTransform(ruleAction, actionedUponNodeRef, contentReader, copyNodeRef, contentWriter);
            ruleAction.setParameterValue(PARAM_RESULT, copyNodeRef);
        } catch (NoTransformerException e) {
            if (logger.isDebugEnabled()) {
                logger.debug("No transformer found to execute rule: \n" + "   reader: " + contentReader + "\n" + "   writer: " + contentWriter + "\n" + "   action: " + this);
            }
            throw new RuleServiceException(TRANSFORMING_ERROR_MESSAGE + e.getMessage());
        }
    }
}
Also used : RuleServiceException(org.alfresco.service.cmr.rule.RuleServiceException) QName(org.alfresco.service.namespace.QName) ContentReader(org.alfresco.service.cmr.repository.ContentReader) CopyInfo(org.alfresco.service.cmr.repository.CopyService.CopyInfo) PagingRequest(org.alfresco.query.PagingRequest) NoTransformerException(org.alfresco.service.cmr.repository.NoTransformerException) TransformationOptions(org.alfresco.service.cmr.repository.TransformationOptions) NodeRef(org.alfresco.service.cmr.repository.NodeRef) ContentWriter(org.alfresco.service.cmr.repository.ContentWriter)

Example 3 with RuleServiceException

use of org.alfresco.service.cmr.rule.RuleServiceException in project alfresco-repository by Alfresco.

the class ParameterizedItemDefinitionImpl method setParameterDefinitions.

/**
 * Set the parameter definitions for the rule item
 *
 * @param parameterDefinitions  the parameter definitions
 */
public void setParameterDefinitions(List<ParameterDefinition> parameterDefinitions) {
    Locale currentLocale = I18NUtil.getLocale();
    new HashMap<Locale, Map<String, ParameterDefinition>>();
    if (hasDuplicateNames(parameterDefinitions) == true) {
        throw new RuleServiceException(ERR_NAME_DUPLICATION);
    }
    this.parameterDefinitions.put(currentLocale, parameterDefinitions);
    createParamDefinitionsByName();
}
Also used : Locale(java.util.Locale) HashMap(java.util.HashMap) RuleServiceException(org.alfresco.service.cmr.rule.RuleServiceException) ParameterDefinition(org.alfresco.service.cmr.action.ParameterDefinition)

Example 4 with RuleServiceException

use of org.alfresco.service.cmr.rule.RuleServiceException in project alfresco-repository by Alfresco.

the class RuleServiceImpl method getRule.

/**
 * Create the rule object from the rule node reference
 *
 * @param ruleNodeRef    the rule node reference
 * @return                the rule
 */
@SuppressWarnings("unchecked")
public Rule getRule(NodeRef ruleNodeRef) {
    // Get the rule properties
    Map<QName, Serializable> props = this.runtimeNodeService.getProperties(ruleNodeRef);
    // Create the rule
    Rule rule = new Rule(ruleNodeRef);
    // Set the title and description
    String title = DefaultTypeConverter.INSTANCE.convert(String.class, props.get(ContentModel.PROP_TITLE));
    String description = DefaultTypeConverter.INSTANCE.convert(String.class, props.get(ContentModel.PROP_DESCRIPTION));
    rule.setTitle(title);
    rule.setDescription(description);
    // Set the rule types
    rule.setRuleTypes((List<String>) props.get(RuleModel.PROP_RULE_TYPE));
    // Set the applied to children value
    boolean isAppliedToChildren = false;
    Boolean value = (Boolean) props.get(RuleModel.PROP_APPLY_TO_CHILDREN);
    if (value != null) {
        isAppliedToChildren = value.booleanValue();
    }
    rule.applyToChildren(isAppliedToChildren);
    // Set the execute asynchronously value
    boolean executeAsync = false;
    Boolean value2 = (Boolean) props.get(RuleModel.PROP_EXECUTE_ASYNC);
    if (value2 != null) {
        executeAsync = value2.booleanValue();
    }
    rule.setExecuteAsynchronously(executeAsync);
    // Set the disabled value
    boolean ruleDisabled = false;
    Boolean value3 = (Boolean) props.get(RuleModel.PROP_DISABLED);
    if (value3 != null) {
        ruleDisabled = value3.booleanValue();
    }
    rule.setRuleDisabled(ruleDisabled);
    // Get the action node reference
    List<ChildAssociationRef> actions = this.nodeService.getChildAssocs(ruleNodeRef, RuleModel.ASSOC_ACTION, RuleModel.ASSOC_ACTION);
    if (actions.size() == 0) {
        throw new RuleServiceException("Rule exists without a specified action");
    } else if (actions.size() > 1) {
        throw new RuleServiceException("Rule exists with more than one specified action");
    }
    NodeRef actionNodeRef = actions.get(0).getChildRef();
    // Here we need to create the action from the action node reference
    Action action = runtimeActionService.createAction(actionNodeRef);
    rule.setAction(action);
    return rule;
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) Serializable(java.io.Serializable) Action(org.alfresco.service.cmr.action.Action) CompositeAction(org.alfresco.service.cmr.action.CompositeAction) RuleServiceException(org.alfresco.service.cmr.rule.RuleServiceException) QName(org.alfresco.service.namespace.QName) Rule(org.alfresco.service.cmr.rule.Rule) ChildAssociationRef(org.alfresco.service.cmr.repository.ChildAssociationRef)

Example 5 with RuleServiceException

use of org.alfresco.service.cmr.rule.RuleServiceException in project alfresco-repository by Alfresco.

the class RuleServiceImpl method removeAllRules.

@Override
public void removeAllRules(NodeRef nodeRef) {
    checkForLinkedRules(nodeRef);
    if (this.permissionService.hasPermission(nodeRef, PermissionService.CHANGE_PERMISSIONS) == AccessStatus.ALLOWED) {
        if (this.nodeService.exists(nodeRef) == true && this.nodeService.hasAspect(nodeRef, RuleModel.ASPECT_RULES) == true) {
            NodeRef folder = getSavedRuleFolderRef(nodeRef);
            if (folder != null) {
                List<ChildAssociationRef> ruleChildAssocs = this.nodeService.getChildAssocs(folder, RegexQNamePattern.MATCH_ALL, ASSOC_NAME_RULES_REGEX);
                for (ChildAssociationRef ruleChildAssoc : ruleChildAssocs) {
                    this.nodeService.removeChild(folder, ruleChildAssoc.getChildRef());
                }
            }
            // As this was the last rule on the node, remove the aspect
            this.nodeService.removeAspect(nodeRef, RuleModel.ASPECT_RULES);
        }
        // Drop the rules from the cache
        nodeRulesCache.remove(nodeRef);
    } else {
        throw new RuleServiceException("Insufficient permissions to remove a rule.");
    }
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) RuleServiceException(org.alfresco.service.cmr.rule.RuleServiceException) ChildAssociationRef(org.alfresco.service.cmr.repository.ChildAssociationRef)

Aggregations

RuleServiceException (org.alfresco.service.cmr.rule.RuleServiceException)11 NodeRef (org.alfresco.service.cmr.repository.NodeRef)9 Action (org.alfresco.service.cmr.action.Action)4 ChildAssociationRef (org.alfresco.service.cmr.repository.ChildAssociationRef)4 QName (org.alfresco.service.namespace.QName)4 CompositeAction (org.alfresco.service.cmr.action.CompositeAction)3 Rule (org.alfresco.service.cmr.rule.Rule)3 Serializable (java.io.Serializable)2 HashMap (java.util.HashMap)2 PagingRequest (org.alfresco.query.PagingRequest)2 CopyInfo (org.alfresco.service.cmr.repository.CopyService.CopyInfo)2 Locale (java.util.Locale)1 ActionCondition (org.alfresco.service.cmr.action.ActionCondition)1 ParameterDefinition (org.alfresco.service.cmr.action.ParameterDefinition)1 ContentReader (org.alfresco.service.cmr.repository.ContentReader)1 ContentWriter (org.alfresco.service.cmr.repository.ContentWriter)1 NoTransformerException (org.alfresco.service.cmr.repository.NoTransformerException)1 TransformationOptions (org.alfresco.service.cmr.repository.TransformationOptions)1 BaseSpringTest (org.alfresco.util.BaseSpringTest)1 Test (org.junit.Test)1