Search in sources :

Example 21 with PropertyDefinition

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

the class NodeParameterProcessor method processPropertyDefinitions.

/**
 * Process the supplied map of property definitions and add the ones that match the supplied fragment to the list of suggestions.
 *
 * @param definition  Definition (aspect or type) to get properties of and the call this method for associated aspects
 * @param substitutionFragment  Substitution fragment to search for
 * @param suggestions  The current list of suggestions to which we will add newly found suggestions
 */
private boolean processPropertyDefinitions(Map<QName, PropertyDefinition> properties, String substitutionFragment, Set<String> suggestions) {
    boolean gotMaximumSuggestions = false;
    if (properties != null) {
        for (Map.Entry<QName, PropertyDefinition> entry : properties.entrySet()) {
            PropertyDefinition propertyDefinition = entry.getValue();
            QName type = propertyDefinition.getDataType().getName();
            if (ArrayUtils.contains(supportedDataTypes, type)) {
                String suggestion = getName() + "." + entry.getKey().getPrefixString();
                if (suggestion.toLowerCase().contains(substitutionFragment)) {
                    if (suggestions.size() < this.maximumNumberSuggestions) {
                        suggestions.add(suggestion);
                    } else {
                        gotMaximumSuggestions = true;
                        break;
                    }
                }
            }
        }
    }
    return gotMaximumSuggestions;
}
Also used : QName(org.alfresco.service.namespace.QName) Map(java.util.Map) PropertyDefinition(org.alfresco.service.cmr.dictionary.PropertyDefinition)

Example 22 with PropertyDefinition

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

Example 23 with PropertyDefinition

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

the class ApplyCustomTypeAction method addParameterDefinitions.

/**
 * @see org.alfresco.module.org_alfresco_module_rm.action.RMActionExecuterAbstractBase#addParameterDefinitions(java.util.List)
 */
@Override
protected final void addParameterDefinitions(List<ParameterDefinition> paramList) {
    AspectDefinition aspectDef = getDictionaryService().getAspect(customTypeAspect);
    for (PropertyDefinition propDef : aspectDef.getProperties().values()) {
        QName propName = propDef.getName();
        QName propType = propDef.getDataType().getName();
        paramList.add(new ParameterDefinitionImpl(propName.toPrefixString(), propType, propDef.isMandatory(), null));
    }
}
Also used : QName(org.alfresco.service.namespace.QName) AspectDefinition(org.alfresco.service.cmr.dictionary.AspectDefinition) PropertyDefinition(org.alfresco.service.cmr.dictionary.PropertyDefinition) ParameterDefinitionImpl(org.alfresco.repo.action.ParameterDefinitionImpl)

Example 24 with PropertyDefinition

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

the class RecordsManagementTypeFormFilterUnitTest method mockPropertyDefintionMap.

/**
 * Helper method to createa a mock property definition map
 */
private Map<QName, PropertyDefinition> mockPropertyDefintionMap(int size) {
    Map<QName, PropertyDefinition> properties = new HashMap<QName, PropertyDefinition>(size);
    for (int i = 0; i < size; i++) {
        QName name = generateQName(RM_URI);
        PropertyDefinition propDef = mock(PropertyDefinition.class);
        when(propDef.getName()).thenReturn(name);
        DataTypeDefinition mockDataTypeDefinition = mock(DataTypeDefinition.class);
        when(mockDataTypeDefinition.getName()).thenReturn(DataTypeDefinition.TEXT);
        when(propDef.getDataType()).thenReturn(mockDataTypeDefinition);
        properties.put(name, propDef);
    }
    return properties;
}
Also used : HashMap(java.util.HashMap) AlfMock.generateQName(org.alfresco.module.org_alfresco_module_rm.test.util.AlfMock.generateQName) QName(org.alfresco.service.namespace.QName) DataTypeDefinition(org.alfresco.service.cmr.dictionary.DataTypeDefinition) PropertyDefinition(org.alfresco.service.cmr.dictionary.PropertyDefinition)

Example 25 with PropertyDefinition

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

the class RecordsManagementNodeFormFilter method forceSupplementalMarkingListProperty.

/**
 * Forces the "rmc:supplementalMarkingList" property to be present, if it is
 * already on the given node this method does nothing, otherwise a property
 * field definition is generated for the property.
 *
 * @param form The Form instance to add the property to
 * @param nodeRef The node the form is being generated for
 */
protected void forceSupplementalMarkingListProperty(Form form, NodeRef nodeRef) {
    if (!this.nodeService.hasAspect(nodeRef, RecordsManagementCustomModel.ASPECT_SUPPLEMENTAL_MARKING_LIST)) {
        PropertyDefinition propDef = this.dictionaryService.getProperty(RecordsManagementCustomModel.PROP_SUPPLEMENTAL_MARKING_LIST);
        if (propDef != null) {
            Field field = FieldUtils.makePropertyField(propDef, null, null, namespaceService, dictionaryService);
            form.addField(field);
        } else if (logger.isWarnEnabled()) {
            logger.warn("Could not add " + RecordsManagementCustomModel.PROP_SUPPLEMENTAL_MARKING_LIST.getLocalName() + " property as it's definition could not be found");
        }
    }
}
Also used : Field(org.alfresco.repo.forms.Field) PropertyDefinition(org.alfresco.service.cmr.dictionary.PropertyDefinition)

Aggregations

PropertyDefinition (org.alfresco.service.cmr.dictionary.PropertyDefinition)77 QName (org.alfresco.service.namespace.QName)51 HashMap (java.util.HashMap)25 ArrayList (java.util.ArrayList)24 NodeRef (org.alfresco.service.cmr.repository.NodeRef)16 Map (java.util.Map)15 Serializable (java.io.Serializable)14 AssociationDefinition (org.alfresco.service.cmr.dictionary.AssociationDefinition)11 ClassDefinition (org.alfresco.service.cmr.dictionary.ClassDefinition)11 AlfrescoRuntimeException (org.alfresco.error.AlfrescoRuntimeException)9 DataTypeDefinition (org.alfresco.service.cmr.dictionary.DataTypeDefinition)9 Collection (java.util.Collection)7 List (java.util.List)7 AspectDefinition (org.alfresco.service.cmr.dictionary.AspectDefinition)7 TypeDefinition (org.alfresco.service.cmr.dictionary.TypeDefinition)7 BooleanQuery (org.apache.lucene.search.BooleanQuery)7 Builder (org.apache.lucene.search.BooleanQuery.Builder)7 Constraint (org.alfresco.service.cmr.dictionary.Constraint)6 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)6 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)6