Search in sources :

Example 1 with DictionaryException

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

the class AbstractRuleWebScript method convertValue.

private Serializable convertValue(QName typeQName, Object propertyValue) throws JSONException {
    Serializable value = null;
    DataTypeDefinition typeDef = dictionaryService.getDataType(typeQName);
    if (typeDef == null) {
        throw new AlfrescoRuntimeException("Action property type definition " + typeQName.toPrefixString() + " is unknown.");
    }
    if (propertyValue instanceof JSONArray) {
        // Convert property type to java class
        Class<?> javaClass = null;
        String javaClassName = typeDef.getJavaClassName();
        try {
            javaClass = Class.forName(javaClassName);
        } catch (ClassNotFoundException e) {
            throw new DictionaryException("Java class " + javaClassName + " of property type " + typeDef.getName() + " is invalid", e);
        }
        int length = ((JSONArray) propertyValue).length();
        List<Serializable> list = new ArrayList<Serializable>(length);
        for (int i = 0; i < length; i++) {
            list.add(convertValue(typeQName, ((JSONArray) propertyValue).get(i)));
        }
        value = (Serializable) list;
    } else {
        if (typeQName.equals(DataTypeDefinition.QNAME) == true && typeQName.toString().contains(":") == true) {
            value = QName.createQName(propertyValue.toString(), namespaceService);
        } else {
            value = (Serializable) DefaultTypeConverter.INSTANCE.convert(dictionaryService.getDataType(typeQName), propertyValue);
        }
    }
    return value;
}
Also used : Serializable(java.io.Serializable) JSONArray(org.json.JSONArray) ArrayList(java.util.ArrayList) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) DataTypeDefinition(org.alfresco.service.cmr.dictionary.DataTypeDefinition) DictionaryException(org.alfresco.service.cmr.dictionary.DictionaryException)

Example 2 with DictionaryException

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

the class UISearchCustomProperties method createComponentsFromConfig.

/**
 * Build the components from the Advanced Search config entries
 *
 * @param context FacesContext
 */
@SuppressWarnings("unchecked")
private void createComponentsFromConfig(FacesContext context) {
    DictionaryService dd = Repository.getServiceRegistry(context).getDictionaryService();
    AdvancedSearchConfigElement config = (AdvancedSearchConfigElement) Application.getConfigService(context).getConfig("Advanced Search").getConfigElement(AdvancedSearchConfigElement.CONFIG_ELEMENT_ID);
    // create an appropriate component for each custom property
    // using the DataDictionary to look-up labels and value types
    String beanBinding = (String) getAttributes().get("bean") + '.' + (String) getAttributes().get("var");
    List<CustomProperty> props = config.getCustomProperties();
    if (props != null) {
        for (CustomProperty property : props) {
            try {
                // try to find the Property definition for the specified Type or Aspect
                PropertyDefinition propDef = null;
                if (property.Type != null) {
                    QName type = Repository.resolveToQName(property.Type);
                    TypeDefinition typeDef = dd.getType(type);
                    if (typeDef == null) {
                        logger.warn("No Type Definition found for: " + property.Type + " - Was an Aspect expected?");
                        continue;
                    }
                    propDef = typeDef.getProperties().get(Repository.resolveToQName(property.Property));
                } else if (property.Aspect != null) {
                    QName aspect = Repository.resolveToQName(property.Aspect);
                    AspectDefinition aspectDef = dd.getAspect(aspect);
                    if (aspectDef == null) {
                        logger.warn("No Aspect Definition found for: " + property.Aspect + " - Was a Type expected?");
                        continue;
                    }
                    propDef = aspectDef.getProperties().get(Repository.resolveToQName(property.Property));
                }
                // if we found a def, then we can build components to represent it
                if (propDef != null) {
                    // resolve display label I18N message
                    String label;
                    if (property.LabelId != null && property.LabelId.length() != 0) {
                        label = Application.getMessage(context, property.LabelId);
                    } else {
                        // or use dictionary label or QName as last resort
                        label = propDef.getTitle(dd) != null ? propDef.getTitle(dd) : propDef.getName().getLocalName();
                    }
                    // special handling for Date and DateTime
                    DataTypeDefinition dataTypeDef = propDef.getDataType();
                    if (DataTypeDefinition.DATE.equals(dataTypeDef.getName()) || DataTypeDefinition.DATETIME.equals(dataTypeDef.getName())) {
                        getChildren().add(generateControl(context, propDef, label, beanBinding));
                    } else {
                        // add ListOfValues constraint components
                        ListOfValuesConstraint constraint = getListOfValuesConstraint(propDef);
                        if (constraint != null && propDef != null && propDef.isProtected() == false) {
                            getChildren().add(generateCheck(context, propDef, beanBinding));
                            getChildren().add(generateLabel(context, label + ": "));
                        } else {
                            getChildren().add(generateLabel(context, ""));
                            getChildren().add(generateLabel(context, label + ": "));
                        }
                        getChildren().add(generateControl(context, propDef, null, beanBinding));
                    }
                }
            } catch (DictionaryException ddErr) {
                logger.warn("Error building custom properties for Advanced Search: " + ddErr.getMessage());
            }
        }
    }
}
Also used : DictionaryService(org.alfresco.service.cmr.dictionary.DictionaryService) QName(org.alfresco.service.namespace.QName) CustomProperty(org.alfresco.web.config.AdvancedSearchConfigElement.CustomProperty) AspectDefinition(org.alfresco.service.cmr.dictionary.AspectDefinition) ListOfValuesConstraint(org.alfresco.repo.dictionary.constraint.ListOfValuesConstraint) DataTypeDefinition(org.alfresco.service.cmr.dictionary.DataTypeDefinition) AdvancedSearchConfigElement(org.alfresco.web.config.AdvancedSearchConfigElement) PropertyDefinition(org.alfresco.service.cmr.dictionary.PropertyDefinition) TypeDefinition(org.alfresco.service.cmr.dictionary.TypeDefinition) DataTypeDefinition(org.alfresco.service.cmr.dictionary.DataTypeDefinition) DictionaryException(org.alfresco.service.cmr.dictionary.DictionaryException)

Example 3 with DictionaryException

use of org.alfresco.service.cmr.dictionary.DictionaryException in project SearchServices by Alfresco.

the class AlfrescoSolrDataModel method validateModel.

private Set<String> validateModel(M2Model model) {
    HashSet<String> errors = new HashSet<String>();
    try {
        dictionaryDAO.getCompiledModel(QName.createQName(model.getName(), namespaceDAO));
    } catch (DictionaryException e) {
        // No model to diff
        return errors;
    } catch (NamespaceException e) {
        // namespace unknown - no model
        return errors;
    }
    List<M2ModelDiff> modelDiffs = dictionaryDAO.diffModelIgnoringConstraints(model);
    for (M2ModelDiff modelDiff : modelDiffs) {
        if (modelDiff.getDiffType().equals(M2ModelDiff.DIFF_UPDATED)) {
            errors.add("Model not updated: " + model.getName() + "   Failed to validate model update - found non-incrementally updated " + modelDiff.getElementType() + " '" + modelDiff.getElementName() + "'");
        }
    }
    return errors;
}
Also used : NamespaceException(org.alfresco.service.namespace.NamespaceException) M2ModelDiff(org.alfresco.repo.dictionary.M2ModelDiff) HashSet(java.util.HashSet) DictionaryException(org.alfresco.service.cmr.dictionary.DictionaryException)

Example 4 with DictionaryException

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

the class ActionsImpl method convertValue.

private Serializable convertValue(QName typeQName, Object propertyValue) throws JSONException {
    Serializable value;
    DataTypeDefinition typeDef = dictionaryService.getDataType(typeQName);
    if (typeDef == null) {
        throw new AlfrescoRuntimeException("Action property type definition " + typeQName.toPrefixString() + " is unknown.");
    }
    if (propertyValue instanceof JSONArray) {
        // Convert property type to java class
        String javaClassName = typeDef.getJavaClassName();
        try {
            Class.forName(javaClassName);
        } catch (ClassNotFoundException e) {
            throw new DictionaryException("Java class " + javaClassName + " of property type " + typeDef.getName() + " is invalid", e);
        }
        int length = ((JSONArray) propertyValue).length();
        List<Serializable> list = new ArrayList<>(length);
        for (int i = 0; i < length; i++) {
            list.add(convertValue(typeQName, ((JSONArray) propertyValue).get(i)));
        }
        value = (Serializable) list;
    } else {
        if (typeQName.equals(DataTypeDefinition.QNAME) && typeQName.toString().contains(":")) {
            value = QName.createQName(propertyValue.toString(), namespaceService);
        } else {
            value = (Serializable) DefaultTypeConverter.INSTANCE.convert(dictionaryService.getDataType(typeQName), propertyValue);
        }
    }
    return value;
}
Also used : Serializable(java.io.Serializable) JSONArray(org.json.JSONArray) ArrayList(java.util.ArrayList) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) DataTypeDefinition(org.alfresco.service.cmr.dictionary.DataTypeDefinition) DictionaryException(org.alfresco.service.cmr.dictionary.DictionaryException)

Example 5 with DictionaryException

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

the class CustomModelUploadPost method processUpload.

protected ImportResult processUpload(ZipFile zipFile, String filename) throws IOException {
    if (zipFile.size() > 2) {
        throw new WebScriptException(Status.STATUS_BAD_REQUEST, "cmm.rest_api.model.import_invalid_zip_package");
    }
    CustomModel customModel = null;
    String shareExtModule = null;
    Enumeration<? extends ZipEntry> entries = zipFile.entries();
    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();
        if (!entry.isDirectory()) {
            final String entryName = entry.getName();
            try (InputStream input = new BufferedInputStream(zipFile.getInputStream(entry), BUFFER_SIZE)) {
                if (!(entryName.endsWith(CustomModelServiceImpl.SHARE_EXT_MODULE_SUFFIX)) && customModel == null) {
                    try {
                        M2Model m2Model = M2Model.createModel(input);
                        customModel = importModel(m2Model);
                    } catch (DictionaryException ex) {
                        if (shareExtModule == null) {
                            // Get the input stream again, as the zip file doesn't support reset.
                            try (InputStream moduleInputStream = new BufferedInputStream(zipFile.getInputStream(entry), BUFFER_SIZE)) {
                                shareExtModule = getExtensionModule(moduleInputStream, entryName);
                            }
                            if (shareExtModule == null) {
                                throw new WebScriptException(Status.STATUS_BAD_REQUEST, "cmm.rest_api.model.import_invalid_zip_entry_format", new Object[] { entryName });
                            }
                        } else {
                            throw new WebScriptException(Status.STATUS_BAD_REQUEST, "cmm.rest_api.model.import_invalid_model_entry", new Object[] { entryName });
                        }
                    }
                } else {
                    shareExtModule = getExtensionModule(input, entryName);
                    if (shareExtModule == null) {
                        throw new WebScriptException(Status.STATUS_BAD_REQUEST, "cmm.rest_api.model.import_invalid_ext_module_entry", new Object[] { entryName });
                    }
                }
            }
        }
    }
    return new ImportResult(customModel, shareExtModule);
}
Also used : WebScriptException(org.springframework.extensions.webscripts.WebScriptException) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) M2Model(org.alfresco.repo.dictionary.M2Model) CustomModel(org.alfresco.rest.api.model.CustomModel) DictionaryException(org.alfresco.service.cmr.dictionary.DictionaryException)

Aggregations

DictionaryException (org.alfresco.service.cmr.dictionary.DictionaryException)5 DataTypeDefinition (org.alfresco.service.cmr.dictionary.DataTypeDefinition)3 Serializable (java.io.Serializable)2 ArrayList (java.util.ArrayList)2 AlfrescoRuntimeException (org.alfresco.error.AlfrescoRuntimeException)2 JSONArray (org.json.JSONArray)2 BufferedInputStream (java.io.BufferedInputStream)1 InputStream (java.io.InputStream)1 HashSet (java.util.HashSet)1 ZipEntry (java.util.zip.ZipEntry)1 M2Model (org.alfresco.repo.dictionary.M2Model)1 M2ModelDiff (org.alfresco.repo.dictionary.M2ModelDiff)1 ListOfValuesConstraint (org.alfresco.repo.dictionary.constraint.ListOfValuesConstraint)1 CustomModel (org.alfresco.rest.api.model.CustomModel)1 AspectDefinition (org.alfresco.service.cmr.dictionary.AspectDefinition)1 DictionaryService (org.alfresco.service.cmr.dictionary.DictionaryService)1 PropertyDefinition (org.alfresco.service.cmr.dictionary.PropertyDefinition)1 TypeDefinition (org.alfresco.service.cmr.dictionary.TypeDefinition)1 NamespaceException (org.alfresco.service.namespace.NamespaceException)1 QName (org.alfresco.service.namespace.QName)1