Search in sources :

Example 11 with NamespaceException

use of org.alfresco.service.namespace.NamespaceException in project alfresco-repository by Alfresco.

the class AbstractNodeEventFilter method expandTypeDef.

protected Collection<QName> expandTypeDef(String typeDef) {
    if ((typeDef == null) || typeDef.isEmpty() || typeDef.equals("none") || typeDef.contains("${")) {
        return Collections.emptyList();
    }
    if (typeDef.indexOf(' ') < 0) {
        return Collections.singleton(getQName(typeDef));
    }
    String[] typeDefParts = typeDef.split(" ");
    if (typeDefParts.length != 2) {
        LOGGER.warn("Ignoring invalid blacklist type pattern: " + typeDef);
        return Collections.emptyList();
    }
    if (typeDefParts[1].equals(MARKER_INCLUDE_SUBTYPES)) {
        if (typeDefParts[0].indexOf('*') >= 0) {
            LOGGER.warn("Ignoring invalid blacklist type pattern: " + typeDef);
            return Collections.emptyList();
        }
        QName baseType;
        try {
            baseType = getQName(typeDefParts[0]);
        } catch (NamespaceException ne) {
            return Collections.emptyList();
        }
        return dictionaryService.getSubTypes(baseType, true);
    }
    LOGGER.warn("Ignoring invalid blacklist type pattern: " + typeDef);
    return Collections.emptyList();
}
Also used : QName(org.alfresco.service.namespace.QName) NamespaceException(org.alfresco.service.namespace.NamespaceException)

Example 12 with NamespaceException

use of org.alfresco.service.namespace.NamespaceException in project alfresco-repository by Alfresco.

the class TypeVirtualizationMethod method asRegExpQNamePatternFilters.

private QNamePattern[] asRegExpQNamePatternFilters(String filtersString) {
    String[] filters = filtersString.split(",");
    List<QNamePattern> patterns = new ArrayList<>(3);
    for (int i = 0; i < filters.length; i++) {
        String trimmedFilters = filters[i].trim();
        if (!trimmedFilters.isEmpty()) {
            if ("*".equals(trimmedFilters)) {
                patterns.clear();
                patterns.add(RegexQNamePattern.MATCH_ALL);
                break;
            }
            if ("none".equals(trimmedFilters)) {
                patterns.clear();
                break;
            }
            String[] components = filters[i].split(":");
            if (components == null || components.length != 2 || components[0].trim().isEmpty() || components[1].trim().isEmpty()) {
                throw new IllegalArgumentException("Illegal filters string " + filtersString + ". Expected <prefix>:<name> | <prefix>:'*' instead of " + filters[i]);
            }
            try {
                String uri = namespacePrefixResolver.getNamespaceURI(components[0]);
                if (uri == null) {
                    // replicate expected resolver behavior
                    throw new NamespaceException("Unregistrered prefix " + components[0]);
                }
                String localName = components[1];
                if ("*".equals(localName.trim())) {
                    localName = ".*";
                }
                Pattern.compile(uri);
                Pattern.compile(localName);
                RegexQNamePattern qNamePattern = new RegexQNamePattern(uri, localName);
                if (!qNamePattern.isMatch(QName.createQName(uri, components[1]))) {
                    throw new IllegalArgumentException("Illegal filters string " + filtersString + " due to invalid regexp translatrion in  " + filters[i] + " as " + qNamePattern);
                }
                patterns.add(qNamePattern);
            } catch (NamespaceException e) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Illegal filters string " + filtersString + " due to unregistered name space in  " + filters[i], e);
                }
            } catch (PatternSyntaxException e) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Illegal filters string " + filtersString + " due to invalid regexp translation in  " + filters[i], e);
                }
            }
        }
    }
    return patterns.toArray(new QNamePattern[] {});
}
Also used : RegexQNamePattern(org.alfresco.service.namespace.RegexQNamePattern) QNamePattern(org.alfresco.service.namespace.QNamePattern) RegexQNamePattern(org.alfresco.service.namespace.RegexQNamePattern) ArrayList(java.util.ArrayList) NamespaceException(org.alfresco.service.namespace.NamespaceException) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Example 13 with NamespaceException

use of org.alfresco.service.namespace.NamespaceException in project alfresco-repository by Alfresco.

the class FormServiceImplTest method disabledTestFDKModel.

@SuppressWarnings({ "deprecation" })
public void disabledTestFDKModel() throws Exception {
    // NOTE: The FDK is not loaded by default, for this test to work you must
    // import and make the "Forms Development Kit" project a dependency
    // of the "Repository" project.
    DictionaryService dictionary = (DictionaryService) this.applicationContext.getBean("DictionaryService");
    try {
        dictionary.getType(QName.createQName("fdk", "everything", this.namespaceService));
    } catch (NamespaceException ne) {
        fail("FDK namespace is missing, ensure you've made the 'Forms Development Kit' project a dependency of the 'Repository' project when enabling this test!");
    }
    // from the check above we know the 'fdk' namespace is present so we can safely
    // use the FDK model, firstly create an instance of an everything node
    String fdkUri = "http://www.alfresco.org/model/fdk/1.0";
    QName everythingType = QName.createQName(fdkUri, "everything");
    QName textProperty = QName.createQName(fdkUri, "text");
    QName underscoreProperty = QName.createQName(fdkUri, "with_underscore");
    QName dashProperty = QName.createQName(fdkUri, "with-dash");
    QName duplicateProperty = QName.createQName(fdkUri, "duplicate");
    QName periodProperty = QName.createQName(fdkUri, "period");
    String guid = GUID.generate();
    String name = "everything" + guid + ".txt";
    String textValue = "This is some text.";
    String underscoreValue = "Property with an underscore in the name.";
    String dashValue = "Property with a dash in the name.";
    String duplicateValue = "Property with the same name as an association.";
    String periodValue = "day|1";
    Map<QName, Serializable> docProps = new HashMap<QName, Serializable>(4);
    docProps.put(ContentModel.PROP_NAME, name);
    docProps.put(textProperty, textValue);
    docProps.put(underscoreProperty, underscoreValue);
    docProps.put(dashProperty, dashValue);
    docProps.put(duplicateProperty, duplicateValue);
    docProps.put(periodProperty, periodValue);
    NodeRef everythingNode = this.nodeService.createNode(this.folder, ContentModel.ASSOC_CONTAINS, QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, name), everythingType, docProps).getChildRef();
    // define a list of fields to retrieve from the node
    List<String> fields = new ArrayList<String>(4);
    fields.add("cm:name");
    fields.add("fdk:text");
    fields.add("fdk:with_underscore");
    fields.add("fdk:with-dash");
    fields.add("prop:fdk:duplicate");
    fields.add("assoc:fdk:duplicate");
    fields.add("fdk:period");
    Form form = this.formService.getForm(new Item(NODE_FORM_ITEM_KIND, everythingNode.toString()), fields);
    // check a form got returned
    assertNotNull("Expecting form to be present", form);
    // check the type is correct
    assertEquals("fdk:everything", form.getItem().getType());
    // check the field definitions
    Collection<FieldDefinition> fieldDefs = form.getFieldDefinitions();
    assertNotNull("Expecting to find fields", fieldDefs);
    assertEquals("Expecting to find " + fields.size() + " fields", fields.size(), fieldDefs.size());
    // find the fields, as we have a duplicate we can't use a Map
    PropertyFieldDefinition nameField = null;
    PropertyFieldDefinition textField = null;
    PropertyFieldDefinition underscoreField = null;
    PropertyFieldDefinition dashField = null;
    PropertyFieldDefinition periodField = null;
    PropertyFieldDefinition duplicatePropField = null;
    AssociationFieldDefinition duplicateAssocField = null;
    for (FieldDefinition field : fieldDefs) {
        if (field.getName().equals("cm:name")) {
            nameField = (PropertyFieldDefinition) field;
        } else if (field.getName().equals("fdk:text")) {
            textField = (PropertyFieldDefinition) field;
        } else if (field.getName().equals("fdk:with_underscore")) {
            underscoreField = (PropertyFieldDefinition) field;
        } else if (field.getName().equals("fdk:with-dash")) {
            dashField = (PropertyFieldDefinition) field;
        } else if (field.getName().equals("fdk:duplicate")) {
            if (field instanceof PropertyFieldDefinition) {
                duplicatePropField = (PropertyFieldDefinition) field;
            } else if (field instanceof AssociationFieldDefinition) {
                duplicateAssocField = (AssociationFieldDefinition) field;
            }
        } else if (field.getName().equals("fdk:period")) {
            periodField = (PropertyFieldDefinition) field;
        }
    }
    assertNotNull("Expected to find nameField", nameField);
    assertNotNull("Expected to find textField", textField);
    assertNotNull("Expected to find underscoreField", underscoreField);
    assertNotNull("Expected to find dashField", dashField);
    assertNotNull("Expected to find periodField", periodField);
    assertNotNull("Expected to find duplicatePropField", duplicatePropField);
    assertNotNull("Expected to find duplicateAssocField", duplicateAssocField);
    // check the field values
    FormData values = form.getFormData();
    assertEquals(name, values.getFieldData(nameField.getDataKeyName()).getValue());
    assertEquals(textValue, values.getFieldData(textField.getDataKeyName()).getValue());
    assertEquals(underscoreValue, values.getFieldData(underscoreField.getDataKeyName()).getValue());
    assertEquals(dashValue, values.getFieldData(dashField.getDataKeyName()).getValue());
    assertEquals(periodValue, values.getFieldData(periodField.getDataKeyName()).getValue().toString());
    assertEquals(duplicateValue, values.getFieldData(duplicatePropField.getDataKeyName()).getValue());
    FieldData fieldData = values.getFieldData(duplicateAssocField.getDataKeyName());
    assertNotNull(fieldData);
    List<?> assocs = (List<?>) fieldData.getValue();
    assertNotNull(assocs);
    assertEquals(0, assocs.size());
    // check the period property data type parameters were returned
    DataTypeParameters dtp = periodField.getDataTypeParameters();
    assertNotNull("Expected to find data type parameters for the fdk:period field", dtp);
    // update the properties via FormService
    FormData data = new FormData();
    // setup the new property values
    String newText = "This is the new text property";
    data.addFieldData(textField.getDataKeyName(), newText);
    String newUnderscore = "This is the new value for the underscore property.";
    data.addFieldData(underscoreField.getDataKeyName(), newUnderscore);
    String newDash = "This is the new value for the dash property.";
    data.addFieldData(dashField.getDataKeyName(), newDash);
    String newDuplicateProp = "This is the new value for the duplicate property.";
    data.addFieldData(duplicatePropField.getDataKeyName(), newDuplicateProp);
    // add new association value
    data.addFieldData(duplicateAssocField.getDataKeyName() + "_added", this.document.toString());
    // persist the data
    this.formService.saveForm(new Item(NODE_FORM_ITEM_KIND, everythingNode.toString()), data);
    // retrieve the data directly from the node service to ensure its been changed
    Map<QName, Serializable> updatedProps = this.nodeService.getProperties(everythingNode);
    String updatedText = (String) updatedProps.get(textProperty);
    String updatedUnderscore = (String) updatedProps.get(underscoreProperty);
    String updatedDash = (String) updatedProps.get(dashProperty);
    String updatedDuplicate = (String) updatedProps.get(duplicateProperty);
    // check values were updated
    assertEquals(newText, updatedText);
    assertEquals(newUnderscore, updatedUnderscore);
    assertEquals(newDash, updatedDash);
    assertEquals(newDuplicateProp, updatedDuplicate);
    // retrieve the association that should now be present
    assocs = this.nodeService.getTargetAssocs(everythingNode, duplicateProperty);
    assertEquals(1, assocs.size());
    // request a form for a type with an underscore in it's name
    fields = new ArrayList<String>(1);
    fields.add("cm:name");
    form = this.formService.getForm(new Item(TYPE_FORM_ITEM_KIND, "fdk:with_underscore"), fields);
    assertNotNull(form);
    // make sure there is 1 fields
    fieldDefs = form.getFieldDefinitions();
    assertNotNull(fieldDefs);
    assertEquals(1, fieldDefs.size());
    // save the form to ensure persistence works too
    String nodeName = GUID.generate() + ".txt";
    data = new FormData();
    data.addFieldData("prop_cm_name", nodeName);
    data.addFieldData(AbstractFormProcessor.DESTINATION, this.folder.toString());
    NodeRef newNode = (NodeRef) this.formService.saveForm(new Item(TYPE_FORM_ITEM_KIND, "fdk:with_underscore"), data);
    assertNotNull(newNode);
    // get and save a form for a type and property that has a dash in the namespace prefix
    fields = new ArrayList<String>(2);
    fields.add("cm:name");
    fields.add("my-fdk:more_text");
    form = this.formService.getForm(new Item(TYPE_FORM_ITEM_KIND, "my-fdk:namespace-with-dash"), fields);
    assertNotNull(form);
    // make sure there are 2 fields
    fieldDefs = form.getFieldDefinitions();
    assertNotNull(fieldDefs);
    assertEquals(2, fieldDefs.size());
    // save the form to ensure persistence works too
    nodeName = GUID.generate() + ".txt";
    data = new FormData();
    data.addFieldData("prop_cm_name", nodeName);
    data.addFieldData("prop_my-fdk_more_text", "This is some text");
    data.addFieldData(TypeFormProcessor.DESTINATION, this.folder.toString());
    newNode = (NodeRef) this.formService.saveForm(new Item(TYPE_FORM_ITEM_KIND, "my-fdk_namespace-with-dash"), data);
    assertNotNull(newNode);
    // retrieve the properties and check the values
    Map<QName, Serializable> props = nodeService.getProperties(newNode);
    assertEquals(nodeName, (String) props.get(ContentModel.PROP_NAME));
    assertEquals("This is some text", (String) props.get(QName.createQName("http://www.alfresco.org/model/my-fdk/1.0", "more_text")));
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) QName(org.alfresco.service.namespace.QName) ArrayList(java.util.ArrayList) FieldData(org.alfresco.repo.forms.FormData.FieldData) NodeRef(org.alfresco.service.cmr.repository.NodeRef) DictionaryService(org.alfresco.service.cmr.dictionary.DictionaryService) NamespaceException(org.alfresco.service.namespace.NamespaceException) List(java.util.List) ArrayList(java.util.ArrayList)

Aggregations

NamespaceException (org.alfresco.service.namespace.NamespaceException)13 QName (org.alfresco.service.namespace.QName)9 Serializable (java.io.Serializable)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 NodeRef (org.alfresco.service.cmr.repository.NodeRef)2 JSONException (org.json.JSONException)2 Collection (java.util.Collection)1 Date (java.util.Date)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1 Locale (java.util.Locale)1 PatternSyntaxException (java.util.regex.PatternSyntaxException)1 M2ModelDiff (org.alfresco.repo.dictionary.M2ModelDiff)1 ActivityFeedEntity (org.alfresco.repo.domain.activities.ActivityFeedEntity)1 Field (org.alfresco.repo.forms.Field)1 FieldData (org.alfresco.repo.forms.FormData.FieldData)1 SyntheticPropertyDefinition (org.alfresco.repo.search.impl.solr.facet.SolrFacetService.SyntheticPropertyDefinition)1