Search in sources :

Example 1 with RMListOfValuesConstraint

use of org.alfresco.module.org_alfresco_module_rm.caveat.RMListOfValuesConstraint in project records-management by Alfresco.

the class RecordsManagementAdminServiceImpl method getCustomConstraintDefinitions.

/**
 * @see org.alfresco.module.org_alfresco_module_rm.admin.RecordsManagementAdminService#getCustomConstraintDefinitions(org.alfresco.service.namespace.QName)
 */
public List<ConstraintDefinition> getCustomConstraintDefinitions(QName modelQName) {
    mandatory("modelQName", modelQName);
    Collection<ConstraintDefinition> conDefs = getDictionaryService().getConstraints(modelQName, true);
    for (ConstraintDefinition conDef : conDefs) {
        Constraint con = conDef.getConstraint();
        if (!(con instanceof RMListOfValuesConstraint)) {
            conDefs.remove(conDef);
        }
    }
    return new ArrayList<ConstraintDefinition>(conDefs);
}
Also used : RMListOfValuesConstraint(org.alfresco.module.org_alfresco_module_rm.caveat.RMListOfValuesConstraint) Constraint(org.alfresco.service.cmr.dictionary.Constraint) M2Constraint(org.alfresco.repo.dictionary.M2Constraint) RMListOfValuesConstraint(org.alfresco.module.org_alfresco_module_rm.caveat.RMListOfValuesConstraint) ArrayList(java.util.ArrayList) ConstraintDefinition(org.alfresco.service.cmr.dictionary.ConstraintDefinition)

Example 2 with RMListOfValuesConstraint

use of org.alfresco.module.org_alfresco_module_rm.caveat.RMListOfValuesConstraint in project records-management by Alfresco.

the class RecordsManagementAdminServiceImplTest method testCreateCustomConstraints.

public void testCreateCustomConstraints() throws Exception {
    final int beforeCnt = retryingTransactionHelper.doInTransaction(new RetryingTransactionHelper.RetryingTransactionCallback<Integer>() {

        public Integer execute() throws Throwable {
            List<ConstraintDefinition> result = rmAdminService.getCustomConstraintDefinitions(RecordsManagementCustomModel.RM_CUSTOM_MODEL);
            assertNotNull(result);
            return result.size();
        }
    });
    final String conTitle = "test title - " + testRunID;
    final List<String> allowedValues = new ArrayList<String>(3);
    allowedValues.add("RED");
    allowedValues.add("AMBER");
    allowedValues.add("GREEN");
    final QName testCon = retryingTransactionHelper.doInTransaction(new RetryingTransactionHelper.RetryingTransactionCallback<QName>() {

        public QName execute() throws Throwable {
            String conLocalName = "test-" + testRunID;
            final QName result = QName.createQName(RecordsManagementCustomModel.RM_CUSTOM_URI, conLocalName);
            rmAdminService.addCustomConstraintDefinition(result, conTitle, true, allowedValues, MatchLogic.AND);
            return result;
        }
    });
    // Set the current security context as System - to see allowed values (unless caveat config is also updated for admin)
    AuthenticationUtil.setFullyAuthenticatedUser(AuthenticationUtil.getSystemUserName());
    retryingTransactionHelper.doInTransaction(new RetryingTransactionHelper.RetryingTransactionCallback<Void>() {

        public Void execute() throws Throwable {
            List<ConstraintDefinition> customConstraintDefs = rmAdminService.getCustomConstraintDefinitions(RecordsManagementCustomModel.RM_CUSTOM_MODEL);
            assertEquals(beforeCnt + 1, customConstraintDefs.size());
            boolean found = false;
            for (ConstraintDefinition conDef : customConstraintDefs) {
                if (conDef.getName().equals(testCon)) {
                    assertEquals(conTitle, conDef.getTitle(dictionaryService));
                    Constraint con = conDef.getConstraint();
                    assertTrue(con instanceof RMListOfValuesConstraint);
                    assertEquals("LIST", ((RMListOfValuesConstraint) con).getType());
                    assertEquals(3, ((RMListOfValuesConstraint) con).getAllowedValues().size());
                    found = true;
                    break;
                }
            }
            assertTrue(found);
            return null;
        }
    });
    // Set the current security context as admin
    AuthenticationUtil.setFullyAuthenticatedUser(AuthenticationUtil.getAdminUserName());
    retryingTransactionHelper.doInTransaction(new RetryingTransactionHelper.RetryingTransactionCallback<Void>() {

        public Void execute() throws Throwable {
            allowedValues.clear();
            allowedValues.add("RED");
            allowedValues.add("YELLOW");
            rmAdminService.changeCustomConstraintValues(testCon, allowedValues);
            return null;
        }
    });
    // Set the current security context as System - to see allowed values (unless caveat config is also updated for admin)
    AuthenticationUtil.setFullyAuthenticatedUser(AuthenticationUtil.getSystemUserName());
    retryingTransactionHelper.doInTransaction(new RetryingTransactionHelper.RetryingTransactionCallback<Void>() {

        public Void execute() throws Throwable {
            List<ConstraintDefinition> customConstraintDefs = rmAdminService.getCustomConstraintDefinitions(RecordsManagementCustomModel.RM_CUSTOM_MODEL);
            assertEquals(beforeCnt + 1, customConstraintDefs.size());
            boolean found = false;
            for (ConstraintDefinition conDef : customConstraintDefs) {
                if (conDef.getName().equals(testCon)) {
                    assertEquals(conTitle, conDef.getTitle(dictionaryService));
                    Constraint con = conDef.getConstraint();
                    assertTrue(con instanceof RMListOfValuesConstraint);
                    assertEquals("LIST", ((RMListOfValuesConstraint) con).getType());
                    assertEquals(2, ((RMListOfValuesConstraint) con).getAllowedValues().size());
                    found = true;
                    break;
                }
            }
            assertTrue(found);
            return null;
        }
    });
    // Set the current security context as admin
    AuthenticationUtil.setFullyAuthenticatedUser(AuthenticationUtil.getAdminUserName());
    // Add custom property to record with test constraint
    retryingTransactionHelper.doInTransaction(new RetryingTransactionHelper.RetryingTransactionCallback<Void>() {

        public Void execute() throws Throwable {
            String propLocalName = "myProp-" + testRunID;
            QName dataType = DataTypeDefinition.TEXT;
            String propTitle = "My property title";
            String description = "My property description";
            String defaultValue = null;
            boolean multiValued = false;
            boolean mandatory = false;
            boolean isProtected = false;
            QName propName = rmAdminService.addCustomPropertyDefinition(null, ASPECT_RECORD, propLocalName, dataType, propTitle, description, defaultValue, multiValued, mandatory, isProtected, testCon);
            createdCustomProperties.add(propName);
            return null;
        }
    });
}
Also used : RetryingTransactionHelper(org.alfresco.repo.transaction.RetryingTransactionHelper) RMListOfValuesConstraint(org.alfresco.module.org_alfresco_module_rm.caveat.RMListOfValuesConstraint) Constraint(org.alfresco.service.cmr.dictionary.Constraint) QName(org.alfresco.service.namespace.QName) ArrayList(java.util.ArrayList) RMListOfValuesConstraint(org.alfresco.module.org_alfresco_module_rm.caveat.RMListOfValuesConstraint) Constraint(org.alfresco.service.cmr.dictionary.Constraint) ConstraintDefinition(org.alfresco.service.cmr.dictionary.ConstraintDefinition) RMListOfValuesConstraint(org.alfresco.module.org_alfresco_module_rm.caveat.RMListOfValuesConstraint) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

ArrayList (java.util.ArrayList)2 RMListOfValuesConstraint (org.alfresco.module.org_alfresco_module_rm.caveat.RMListOfValuesConstraint)2 Constraint (org.alfresco.service.cmr.dictionary.Constraint)2 ConstraintDefinition (org.alfresco.service.cmr.dictionary.ConstraintDefinition)2 List (java.util.List)1 M2Constraint (org.alfresco.repo.dictionary.M2Constraint)1 RetryingTransactionHelper (org.alfresco.repo.transaction.RetryingTransactionHelper)1 QName (org.alfresco.service.namespace.QName)1