Search in sources :

Example 11 with Constraint

use of org.alfresco.service.cmr.dictionary.Constraint in project records-management by Alfresco.

the class RMCaveatConfigServiceImpl method getAllRMConstraints.

/**
 * Get all Constraint Lists
 */
public Set<RMConstraintInfo> getAllRMConstraints() {
    Set<RMConstraintInfo> info = new HashSet<RMConstraintInfo>();
    List<ConstraintDefinition> defs = new ArrayList<ConstraintDefinition>(10);
    for (QName caveatModelQName : rmCaveatConfigComponent.getRMCaveatModels()) {
        defs.addAll(recordsManagementAdminService.getCustomConstraintDefinitions(caveatModelQName));
    }
    for (ConstraintDefinition dictionaryDef : defs) {
        Constraint con = dictionaryDef.getConstraint();
        if (con instanceof RMListOfValuesConstraint) {
            final RMListOfValuesConstraint def = (RMListOfValuesConstraint) con;
            RMConstraintInfo i = new RMConstraintInfo();
            i.setName(def.getShortName());
            i.setTitle(def.getTitle());
            // note: assumes only one caveat/LOV against a given property
            List<String> allowedValues = AuthenticationUtil.runAs(new RunAsWork<List<String>>() {

                public List<String> doWork() {
                    return def.getAllowedValues();
                }
            }, AuthenticationUtil.getSystemUserName());
            i.setAllowedValues(allowedValues.toArray(new String[allowedValues.size()]));
            i.setCaseSensitive(def.isCaseSensitive());
            info.add(i);
        }
    }
    return info;
}
Also used : Constraint(org.alfresco.service.cmr.dictionary.Constraint) QName(org.alfresco.service.namespace.QName) ArrayList(java.util.ArrayList) ConstraintDefinition(org.alfresco.service.cmr.dictionary.ConstraintDefinition) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet)

Example 12 with Constraint

use of org.alfresco.service.cmr.dictionary.Constraint 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)

Example 13 with Constraint

use of org.alfresco.service.cmr.dictionary.Constraint in project alfresco-remote-api by Alfresco.

the class WorkflowRestImpl method getFormModelElements.

/**
 * @param type the type to get the elements for
 * @param paging Paging
 * @return collection with all valid form-model elements for the given type.
 */
public CollectionWithPagingInfo<FormModelElement> getFormModelElements(TypeDefinition type, Paging paging) {
    Map<QName, PropertyDefinition> taskProperties = type.getProperties();
    Set<QName> typesToExclude = getTypesToExclude(type);
    List<FormModelElement> page = new ArrayList<FormModelElement>();
    for (Entry<QName, PropertyDefinition> entry : taskProperties.entrySet()) {
        String name = entry.getKey().toPrefixString(namespaceService).replace(':', '_');
        // Only add properties which are not part of an excluded type
        if (!typesToExclude.contains(entry.getValue().getContainerClass().getName()) && excludeModelTypes.contains(name) == false) {
            FormModelElement element = new FormModelElement();
            element.setName(name);
            element.setQualifiedName(entry.getKey().toString());
            element.setTitle(entry.getValue().getTitle(dictionaryService));
            element.setRequired(entry.getValue().isMandatory());
            element.setDataType(entry.getValue().getDataType().getName().toPrefixString(namespaceService));
            element.setDefaultValue(entry.getValue().getDefaultValue());
            if (entry.getValue().getConstraints() != null) {
                for (ConstraintDefinition constraintDef : entry.getValue().getConstraints()) {
                    Constraint constraint = constraintDef.getConstraint();
                    if (constraint != null && constraint instanceof ListOfValuesConstraint) {
                        ListOfValuesConstraint valuesConstraint = (ListOfValuesConstraint) constraint;
                        if (valuesConstraint.getAllowedValues() != null && valuesConstraint.getAllowedValues().size() > 0) {
                            element.setAllowedValues(valuesConstraint.getAllowedValues());
                        }
                    }
                }
            }
            page.add(element);
        }
    }
    Map<QName, AssociationDefinition> taskAssociations = type.getAssociations();
    for (Entry<QName, AssociationDefinition> entry : taskAssociations.entrySet()) {
        // Only add associations which are not part of an excluded type
        if (!typesToExclude.contains(entry.getValue().getSourceClass().getName())) {
            FormModelElement element = new FormModelElement();
            element.setName(entry.getKey().toPrefixString(namespaceService).replace(':', '_'));
            element.setQualifiedName(entry.getKey().toString());
            element.setTitle(entry.getValue().getTitle(dictionaryService));
            element.setRequired(entry.getValue().isTargetMandatory());
            element.setDataType(entry.getValue().getTargetClass().getName().toPrefixString(namespaceService));
            page.add(element);
        }
    }
    return CollectionWithPagingInfo.asPaged(paging, page, false, page.size());
}
Also used : FormModelElement(org.alfresco.rest.workflow.api.model.FormModelElement) Constraint(org.alfresco.service.cmr.dictionary.Constraint) ListOfValuesConstraint(org.alfresco.repo.dictionary.constraint.ListOfValuesConstraint) QName(org.alfresco.service.namespace.QName) ArrayList(java.util.ArrayList) PropertyDefinition(org.alfresco.service.cmr.dictionary.PropertyDefinition) ConstraintDefinition(org.alfresco.service.cmr.dictionary.ConstraintDefinition) AssociationDefinition(org.alfresco.service.cmr.dictionary.AssociationDefinition) ListOfValuesConstraint(org.alfresco.repo.dictionary.constraint.ListOfValuesConstraint)

Aggregations

Constraint (org.alfresco.service.cmr.dictionary.Constraint)13 ArrayList (java.util.ArrayList)12 ConstraintDefinition (org.alfresco.service.cmr.dictionary.ConstraintDefinition)11 List (java.util.List)8 QName (org.alfresco.service.namespace.QName)7 ListOfValuesConstraint (org.alfresco.repo.dictionary.constraint.ListOfValuesConstraint)5 PropertyDefinition (org.alfresco.service.cmr.dictionary.PropertyDefinition)5 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 RMListOfValuesConstraint (org.alfresco.module.org_alfresco_module_rm.caveat.RMListOfValuesConstraint)2 MimetypeMap (org.alfresco.repo.content.MimetypeMap)2 JSONObject (org.json.JSONObject)2 Serializable (java.io.Serializable)1 Entry (java.util.Map.Entry)1 SelectItem (javax.faces.model.SelectItem)1 AccessDeniedException (net.sf.acegisecurity.AccessDeniedException)1 AlfrescoRuntimeException (org.alfresco.error.AlfrescoRuntimeException)1 MatchLogic (org.alfresco.module.org_alfresco_module_rm.caveat.RMListOfValuesConstraint.MatchLogic)1 M2Constraint (org.alfresco.repo.dictionary.M2Constraint)1