Search in sources :

Example 1 with M2Namespace

use of org.alfresco.repo.dictionary.M2Namespace in project SearchServices by Alfresco.

the class ModelTracker method loadPersistedModels.

/**
 */
private void loadPersistedModels() {
    HashMap<String, M2Model> modelMap = new HashMap<String, M2Model>();
    if (alfrescoModelDir.exists() && alfrescoModelDir.isDirectory()) {
        // A filter for XML files
        FileFilter filter = new FileFilter() {

            @Override
            public boolean accept(File pathname) {
                return pathname.isFile() && pathname.getName().endsWith(".xml");
            }
        };
        // List XML files
        for (File file : alfrescoModelDir.listFiles(filter)) {
            InputStream modelStream = null;
            M2Model model;
            try {
                modelStream = new FileInputStream(file);
                model = M2Model.createModel(modelStream);
            } catch (IOException e) {
                throw new AlfrescoRuntimeException("File not found: " + file, e);
            } finally {
                if (modelStream != null) {
                    try {
                        modelStream.close();
                    } catch (Exception e) {
                    }
                }
            }
            // Model successfully loaded
            for (M2Namespace namespace : model.getNamespaces()) {
                modelMap.put(namespace.getUri(), model);
            }
        }
    }
    // Load the models ensuring that they are loaded in the correct order
    HashSet<String> loadedModels = new HashSet<String>();
    for (M2Model model : modelMap.values()) {
        loadModel(modelMap, loadedModels, model);
    }
    if (modelMap.size() > 0) {
        AlfrescoSolrDataModel.getInstance().afterInitModels();
    }
}
Also used : M2Namespace(org.alfresco.repo.dictionary.M2Namespace) HashMap(java.util.HashMap) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) M2Model(org.alfresco.repo.dictionary.M2Model) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) AuthenticationException(org.alfresco.httpclient.AuthenticationException) JSONException(org.json.JSONException) IOException(java.io.IOException) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) FileFilter(java.io.FileFilter) File(java.io.File) HashSet(java.util.HashSet)

Example 2 with M2Namespace

use of org.alfresco.repo.dictionary.M2Namespace in project SearchServices by Alfresco.

the class ModelTracker method trackModelsImpl.

/**
 * Tracks models. Reflects changes and updates on disk copy
 *
 * @throws AuthenticationException
 * @throws IOException
 * @throws JSONException
 */
private void trackModelsImpl() throws AuthenticationException, IOException, JSONException {
    long start = System.nanoTime();
    List<AlfrescoModelDiff> modelDiffs = client.getModelsDiff(coreName, this.infoSrv.getAlfrescoModels());
    HashMap<String, M2Model> modelMap = new HashMap<String, M2Model>();
    for (AlfrescoModelDiff modelDiff : modelDiffs) {
        switch(modelDiff.getType()) {
            case CHANGED:
                AlfrescoModel changedModel = client.getModel(coreName, modelDiff.getModelName());
                for (M2Namespace namespace : changedModel.getModel().getNamespaces()) {
                    modelMap.put(namespace.getUri(), changedModel.getModel());
                }
                break;
            case NEW:
                AlfrescoModel newModel = client.getModel(coreName, modelDiff.getModelName());
                for (M2Namespace namespace : newModel.getModel().getNamespaces()) {
                    modelMap.put(namespace.getUri(), newModel.getModel());
                }
                break;
            case REMOVED:
                // We need to know the prefix for the uri to delete them
                break;
        }
    }
    HashSet<String> loadedModels = new HashSet<String>();
    for (M2Model model : modelMap.values()) {
        loadModel(modelMap, loadedModels, model);
    }
    if (loadedModels.size() > 0) {
        this.infoSrv.afterInitModels();
    }
    for (AlfrescoModelDiff modelDiff : modelDiffs) {
        switch(modelDiff.getType()) {
            case CHANGED:
                removeMatchingModels(alfrescoModelDir, modelDiff.getModelName());
                M2Model changedModel = this.infoSrv.getM2Model(modelDiff.getModelName());
                File changedFile = new File(alfrescoModelDir, getModelFileName(changedModel));
                FileOutputStream cos = new FileOutputStream(changedFile);
                changedModel.toXML(cos);
                cos.flush();
                cos.close();
                break;
            case NEW:
                M2Model newModel = this.infoSrv.getM2Model(modelDiff.getModelName());
                // add on file
                File newFile = new File(alfrescoModelDir, getModelFileName(newModel));
                FileOutputStream nos = new FileOutputStream(newFile);
                newModel.toXML(nos);
                nos.flush();
                nos.close();
                break;
            case REMOVED:
                // This will remove the model from the dictionary on completion
                try {
                    removeMatchingModels(alfrescoModelDir, modelDiff.getModelName());
                } finally {
                    AlfrescoSolrDataModel.getInstance().removeModel(modelDiff.getModelName());
                }
                break;
        }
    }
    long end = System.nanoTime();
    trackerStats.addModelTime(end - start);
    if (true == runPostModelLoadInit) {
        for (Object key : props.keySet()) {
            String stringKey = (String) key;
            if (stringKey.startsWith("alfresco.index.store")) {
                StoreRef store = new StoreRef(props.getProperty(stringKey));
                indexedStores.add(store);
            }
            if (stringKey.startsWith("alfresco.ignore.store")) {
                StoreRef store = new StoreRef(props.getProperty(stringKey));
                ignoredStores.add(store);
            }
            if (stringKey.startsWith("alfresco.index.tenant")) {
                indexedTenants.add(props.getProperty(stringKey));
            }
            if (stringKey.startsWith("alfresco.ignore.tenant")) {
                ignoredTenants.add(props.getProperty(stringKey));
            }
            if (stringKey.startsWith("alfresco.index.datatype")) {
                QName qname = expandQName(props.getProperty(stringKey));
                indexedDataTypes.add(qname);
            }
            if (stringKey.startsWith("alfresco.ignore.datatype")) {
                QName qname = expandQName(props.getProperty(stringKey));
                ignoredDataTypes.add(qname);
            }
            if (stringKey.startsWith("alfresco.index.type")) {
                QName qname = expandQName(props.getProperty(stringKey));
                indexedTypes.add(qname);
            }
            if (stringKey.startsWith("alfresco.ignore.type")) {
                QName qname = expandQName(props.getProperty(stringKey));
                ignoredTypes.add(qname);
            }
            if (stringKey.startsWith("alfresco.index.aspect")) {
                QName qname = expandQName(props.getProperty(stringKey));
                indexedAspects.add(qname);
            }
            if (stringKey.startsWith("alfresco.ignore.aspect")) {
                QName qname = expandQName(props.getProperty(stringKey));
                ignoredAspects.add(qname);
            }
            if (stringKey.startsWith("alfresco.index.field")) {
                String name = expandName(props.getProperty(stringKey));
                indexedFields.add(name);
            }
            if (stringKey.startsWith("alfresco.ignore.field")) {
                String name = expandName(props.getProperty(stringKey));
                ignoredFields.add(name);
            }
        }
        runPostModelLoadInit = false;
    }
}
Also used : M2Namespace(org.alfresco.repo.dictionary.M2Namespace) StoreRef(org.alfresco.service.cmr.repository.StoreRef) HashMap(java.util.HashMap) QName(org.alfresco.service.namespace.QName) M2Model(org.alfresco.repo.dictionary.M2Model) AlfrescoModelDiff(org.alfresco.solr.client.AlfrescoModelDiff) AlfrescoModel(org.alfresco.solr.client.AlfrescoModel) FileOutputStream(java.io.FileOutputStream) File(java.io.File) HashSet(java.util.HashSet)

Example 3 with M2Namespace

use of org.alfresco.repo.dictionary.M2Namespace in project alfresco-remote-api by Alfresco.

the class CustomModelsImpl method validateImportedM2Model.

private void validateImportedM2Model(M2Model m2Model) {
    List<M2Namespace> namespaces = m2Model.getNamespaces();
    if (namespaces.size() > 1) {
        throw new ConstraintViolatedException(I18NUtil.getMessage("cmm.rest_api.model.import_namespace_multiple_found", namespaces.size()));
    } else if (namespaces.isEmpty()) {
        throw new ConstraintViolatedException("cmm.rest_api.model.import_namespace_undefined");
    }
    checkUnsupportedModelElements(m2Model.getTypes());
    checkUnsupportedModelElements(m2Model.getAspects());
}
Also used : M2Namespace(org.alfresco.repo.dictionary.M2Namespace) ConstraintViolatedException(org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException)

Aggregations

M2Namespace (org.alfresco.repo.dictionary.M2Namespace)3 File (java.io.File)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 M2Model (org.alfresco.repo.dictionary.M2Model)2 FileFilter (java.io.FileFilter)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 AlfrescoRuntimeException (org.alfresco.error.AlfrescoRuntimeException)1 AuthenticationException (org.alfresco.httpclient.AuthenticationException)1 ConstraintViolatedException (org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException)1 StoreRef (org.alfresco.service.cmr.repository.StoreRef)1 QName (org.alfresco.service.namespace.QName)1 AlfrescoModel (org.alfresco.solr.client.AlfrescoModel)1 AlfrescoModelDiff (org.alfresco.solr.client.AlfrescoModelDiff)1 JSONException (org.json.JSONException)1