Search in sources :

Example 1 with ConstraintDefinition

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

the class CustomModelsImpl method getCustomModelConstraint.

@Override
public CustomModelConstraint getCustomModelConstraint(String modelName, String constraintName, Parameters parameters) {
    if (constraintName == null) {
        throw new InvalidArgumentException(CONSTRAINT_NAME_NULL_ERR);
    }
    final CustomModelDefinition modelDef = getCustomModelImpl(modelName);
    QName constraintQname = QName.createQName(modelDef.getName().getNamespaceURI(), constraintName);
    ConstraintDefinition constraintDef = customModelService.getCustomConstraint(constraintQname);
    if (constraintDef == null) {
        throw new EntityNotFoundException(constraintName);
    }
    return new CustomModelConstraint(constraintDef, dictionaryService);
}
Also used : CustomModelDefinition(org.alfresco.service.cmr.dictionary.CustomModelDefinition) InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) QName(org.alfresco.service.namespace.QName) EntityNotFoundException(org.alfresco.rest.framework.core.exceptions.EntityNotFoundException) CustomModelConstraint(org.alfresco.rest.api.model.CustomModelConstraint) ConstraintDefinition(org.alfresco.service.cmr.dictionary.ConstraintDefinition)

Example 2 with ConstraintDefinition

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

the class CustomModelsImpl method getCustomModelConstraints.

@Override
public CollectionWithPagingInfo<CustomModelConstraint> getCustomModelConstraints(String modelName, Parameters parameters) {
    CustomModelDefinition modelDef = getCustomModelImpl(modelName);
    Collection<ConstraintDefinition> constraintDefinitions = modelDef.getModelDefinedConstraints();
    // TODO Should we support paging?
    Paging paging = Paging.DEFAULT;
    List<CustomModelConstraint> customModelConstraints = convertToCustomModelConstraints(constraintDefinitions);
    return CollectionWithPagingInfo.asPaged(paging, customModelConstraints, false, constraintDefinitions.size());
}
Also used : CustomModelDefinition(org.alfresco.service.cmr.dictionary.CustomModelDefinition) Paging(org.alfresco.rest.framework.resource.parameters.Paging) CustomModelConstraint(org.alfresco.rest.api.model.CustomModelConstraint) ConstraintDefinition(org.alfresco.service.cmr.dictionary.ConstraintDefinition)

Example 3 with ConstraintDefinition

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

the class WorkflowModelBuilder method buildPropertyLabels.

private Map<String, String> buildPropertyLabels(WorkflowTask task, Map<String, Object> properties) {
    TypeDefinition taskType = task.getDefinition().getMetadata();
    final Map<QName, PropertyDefinition> propDefs = taskType.getProperties();
    return CollectionUtils.transform(properties, new Function<Entry<String, Object>, Pair<String, String>>() {

        @Override
        public Pair<String, String> apply(Entry<String, Object> entry) {
            String propName = entry.getKey();
            PropertyDefinition propDef = propDefs.get(qNameConverter.mapNameToQName(propName));
            if (propDef != null) {
                List<ConstraintDefinition> constraints = propDef.getConstraints();
                for (ConstraintDefinition constraintDef : constraints) {
                    Constraint constraint = constraintDef.getConstraint();
                    if (constraint instanceof ListOfValuesConstraint) {
                        ListOfValuesConstraint listConstraint = (ListOfValuesConstraint) constraint;
                        String label = listConstraint.getDisplayLabel(String.valueOf(entry.getValue()), dictionaryService);
                        return new Pair<String, String>(propName, label);
                    }
                }
            }
            return null;
        }
    });
}
Also used : Constraint(org.alfresco.service.cmr.dictionary.Constraint) ListOfValuesConstraint(org.alfresco.repo.dictionary.constraint.ListOfValuesConstraint) QName(org.alfresco.service.namespace.QName) PropertyDefinition(org.alfresco.service.cmr.dictionary.PropertyDefinition) TypeDefinition(org.alfresco.service.cmr.dictionary.TypeDefinition) ConstraintDefinition(org.alfresco.service.cmr.dictionary.ConstraintDefinition) Entry(java.util.Map.Entry) ListOfValuesConstraint(org.alfresco.repo.dictionary.constraint.ListOfValuesConstraint) ArrayList(java.util.ArrayList) List(java.util.List) Pair(org.alfresco.util.Pair)

Example 4 with ConstraintDefinition

use of org.alfresco.service.cmr.dictionary.ConstraintDefinition in project acs-community-packaging by Alfresco.

the class TextFieldGenerator method getListOfValuesConstraint.

/**
 * Retrieves the list of values constraint for the item, if it has one
 *
 * @param context FacesContext
 * @param propertySheet The property sheet being generated
 * @param item The item being generated
 * @return The constraint if the item has one, null otherwise
 */
protected ListOfValuesConstraint getListOfValuesConstraint(FacesContext context, UIPropertySheet propertySheet, PropertySheetItem item) {
    ListOfValuesConstraint lovConstraint = null;
    // get the property definition for the item
    PropertyDefinition propertyDef = getPropertyDefinition(context, propertySheet.getNode(), item.getName());
    if (propertyDef != null) {
        // go through the constaints and see if it has the
        // list of values constraint
        List<ConstraintDefinition> constraints = propertyDef.getConstraints();
        for (ConstraintDefinition constraintDef : constraints) {
            Constraint constraint = constraintDef.getConstraint();
            if (constraint instanceof ListOfValuesConstraint) {
                lovConstraint = (ListOfValuesConstraint) constraint;
                break;
            }
        }
    }
    return lovConstraint;
}
Also used : Constraint(org.alfresco.service.cmr.dictionary.Constraint) ListOfValuesConstraint(org.alfresco.repo.dictionary.constraint.ListOfValuesConstraint) ListOfValuesConstraint(org.alfresco.repo.dictionary.constraint.ListOfValuesConstraint) PropertyDefinition(org.alfresco.service.cmr.dictionary.PropertyDefinition) ConstraintDefinition(org.alfresco.service.cmr.dictionary.ConstraintDefinition)

Example 5 with ConstraintDefinition

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

the class RMCaveatConfigComponentImpl method validateAndReset.

/**
 * Validate the caveat config and optionally update the cache.
 *
 * @param nodeRef The nodeRef of the config
 * @param updateCache Set to <code>true</code> to update the cache
 */
@SuppressWarnings("unchecked")
protected void validateAndReset(NodeRef nodeRef) {
    ContentReader cr = contentService.getReader(nodeRef, ContentModel.PROP_CONTENT);
    if (cr != null) {
        // TODO - check who can change caveat config !
        // TODO - locking (or checkout/checkin)
        String caveatConfigData = cr.getContentString();
        if (caveatConfigData != null) {
            NodeRef existing = getCaveatConfigNode();
            if ((existing != null && (!existing.equals(nodeRef)))) {
                throw new AlfrescoRuntimeException("Cannot create more than one caveat config (existing=" + existing + ", new=" + nodeRef + ")");
            }
            try {
                if (logger.isTraceEnabled()) {
                    logger.trace(caveatConfigData);
                }
                Set<QName> models = new HashSet<QName>(1);
                Set<QName> props = new HashSet<QName>(10);
                Set<String> expectedPrefixes = new HashSet<String>(10);
                if (caveatModelQNames.size() > 0) {
                    models.addAll(caveatModelQNames);
                } else {
                    models.addAll(dictionaryService.getAllModels());
                }
                if (logger.isTraceEnabled()) {
                    logger.trace("validateAndReset: models to check " + models);
                }
                for (QName model : models) {
                    props.addAll(dictionaryService.getProperties(model, DATATYPE_TEXT));
                    expectedPrefixes.addAll(namespaceService.getPrefixes(model.getNamespaceURI()));
                }
                if (props.size() == 0) {
                    logger.warn("validateAndReset: no caveat properties found");
                } else {
                    if (logger.isTraceEnabled()) {
                        logger.trace("validateAndReset: properties to check " + props);
                    }
                }
                Map<String, Object> caveatConfigMap = JSONtoFmModel.convertJSONObjectToMap(caveatConfigData);
                for (Map.Entry<String, Object> conEntry : caveatConfigMap.entrySet()) {
                    String conStr = conEntry.getKey();
                    QName conQName = QName.resolveToQName(namespaceService, conStr);
                    // check prefix
                    String conPrefix = QName.splitPrefixedQName(conStr)[0];
                    boolean prefixFound = false;
                    for (String expectedPrefix : expectedPrefixes) {
                        if (conPrefix.equals(expectedPrefix)) {
                            prefixFound = true;
                        }
                    }
                    if (!prefixFound) {
                        throw new AlfrescoRuntimeException("Unexpected prefix: " + conPrefix + " (" + conStr + ") expected one of " + expectedPrefixes + ")");
                    }
                    Map<String, List<String>> caveatMap = (Map<String, List<String>>) conEntry.getValue();
                    List<String> allowedValues = null;
                    @SuppressWarnings("unused") boolean found = false;
                    for (QName propertyName : props) {
                        PropertyDefinition propDef = dictionaryService.getProperty(propertyName);
                        List<ConstraintDefinition> conDefs = propDef.getConstraints();
                        for (ConstraintDefinition conDef : conDefs) {
                            final Constraint con = conDef.getConstraint();
                            if (con instanceof RMListOfValuesConstraint) {
                                String conName = ((RMListOfValuesConstraint) con).getShortName();
                                if (conName.equals(conStr)) {
                                    // note: assumes only one caveat/LOV against a given property
                                    allowedValues = AuthenticationUtil.runAs(new RunAsWork<List<String>>() {

                                        public List<String> doWork() {
                                            return ((RMListOfValuesConstraint) con).getAllowedValues();
                                        }
                                    }, AuthenticationUtil.getSystemUserName());
                                    found = true;
                                    break;
                                }
                            }
                        }
                    }
                    if (allowedValues != null) {
                        if (logger.isInfoEnabled()) {
                            logger.info("Processing constraint: " + conQName);
                        }
                        for (Map.Entry<String, List<String>> caveatEntry : caveatMap.entrySet()) {
                            String authorityName = caveatEntry.getKey();
                            List<String> caveatList = caveatEntry.getValue();
                            // validate authority (user or group) - note: groups are configured with fullname (ie. GROUP_xxx)
                            if ((!authorityService.authorityExists(authorityName) && !personService.personExists(authorityName))) {
                                // TODO - review warnings (& I18N)
                                String msg = "User/group does not exist: " + authorityName + " (constraint=" + conStr + ")";
                                logger.warn(msg);
                            }
                            // validate caveat list
                            for (String value : caveatList) {
                                if (!allowedValues.contains(value)) {
                                    // TODO - review warnings (& add I18N)
                                    String msg = "Invalid value in list: " + value + " (authority=" + authorityName + ", constraint=" + conStr + ")";
                                    logger.warn(msg);
                                }
                            }
                        }
                    }
                }
                try {
                    writeLock.lock();
                    // we can't just clear the cache, as all puts to the cache afterwards in this transaction will be ignored
                    // first delete all keys that are now not in the config
                    caveatConfig.getKeys().retainAll(caveatConfigMap.keySet());
                    for (Map.Entry<String, Object> conEntry : caveatConfigMap.entrySet()) {
                        String conStr = conEntry.getKey();
                        Map<String, List<String>> caveatMap = (Map<String, List<String>>) conEntry.getValue();
                        Map<String, List<String>> cacheValue = caveatConfig.get(conStr);
                        if (cacheValue == null || !cacheValue.equals(caveatMap)) {
                            // update the cache
                            caveatConfig.put(conStr, caveatMap);
                        }
                    }
                } finally {
                    writeLock.unlock();
                }
            } catch (JSONException e) {
                throw new AlfrescoRuntimeException("Invalid caveat config syntax: " + e);
            }
        }
    }
}
Also used : Constraint(org.alfresco.service.cmr.dictionary.Constraint) RunAsWork(org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork) NodeRef(org.alfresco.service.cmr.repository.NodeRef) List(java.util.List) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) QName(org.alfresco.service.namespace.QName) ContentReader(org.alfresco.service.cmr.repository.ContentReader) JSONException(org.json.JSONException) PropertyDefinition(org.alfresco.service.cmr.dictionary.PropertyDefinition) ConstraintDefinition(org.alfresco.service.cmr.dictionary.ConstraintDefinition) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) JSONObject(org.json.JSONObject) Map(java.util.Map) MimetypeMap(org.alfresco.repo.content.MimetypeMap) HashMap(java.util.HashMap)

Aggregations

ConstraintDefinition (org.alfresco.service.cmr.dictionary.ConstraintDefinition)15 Constraint (org.alfresco.service.cmr.dictionary.Constraint)11 ArrayList (java.util.ArrayList)10 QName (org.alfresco.service.namespace.QName)9 List (java.util.List)8 PropertyDefinition (org.alfresco.service.cmr.dictionary.PropertyDefinition)5 ListOfValuesConstraint (org.alfresco.repo.dictionary.constraint.ListOfValuesConstraint)3 CustomModelConstraint (org.alfresco.rest.api.model.CustomModelConstraint)3 CustomModelDefinition (org.alfresco.service.cmr.dictionary.CustomModelDefinition)3 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 InvalidArgumentException (org.alfresco.rest.framework.core.exceptions.InvalidArgumentException)2 JSONObject (org.json.JSONObject)2 Serializable (java.io.Serializable)1 Entry (java.util.Map.Entry)1 AccessDeniedException (net.sf.acegisecurity.AccessDeniedException)1 AlfrescoRuntimeException (org.alfresco.error.AlfrescoRuntimeException)1