Search in sources :

Example 1 with AlfrescoModelDiff

use of org.alfresco.solr.client.AlfrescoModelDiff in project SearchServices by Alfresco.

the class ModelTrackerTest method setUpTestTrackModels.

@SuppressWarnings("unchecked")
private String setUpTestTrackModels() throws AuthenticationException, IOException, JSONException {
    QName modelName = QName.createQName("qname");
    TYPE type = TYPE.CHANGED;
    Long oldChecksum = new Long(0);
    Long newChecksum = new Long(1);
    AlfrescoModelDiff diff = new AlfrescoModelDiff(modelName, type, oldChecksum, newChecksum);
    List<AlfrescoModelDiff> modelDiffs = new ArrayList<>();
    modelDiffs.add(diff);
    when(this.repositoryClient.getModelsDiff(any(String.class), any(List.class))).thenReturn(modelDiffs);
    final String name = "a model name";
    M2Model model = M2Model.createModel(name);
    M2Model spiedModel = spy(model);
    model.createNamespace("uri", "prefix");
    AlfrescoModel alfrescoModel = new AlfrescoModel(spiedModel, newChecksum);
    when(this.repositoryClient.getModel(any(String.class), eq(modelName))).thenReturn(alfrescoModel);
    NamespaceDAO namespaceDao = mock(NamespaceDAO.class);
    Collection<String> values = new ArrayList<>();
    values.add("prefix");
    when(namespaceDao.getPrefixes(anyString())).thenReturn(values);
    when(this.srv.getNamespaceDAO()).thenReturn(namespaceDao);
    when(this.srv.getM2Model(modelName)).thenReturn(spiedModel);
    when(this.srv.putModel(spiedModel)).thenReturn(true);
    return name;
}
Also used : NamespaceDAO(org.alfresco.repo.dictionary.NamespaceDAO) QName(org.alfresco.service.namespace.QName) M2Model(org.alfresco.repo.dictionary.M2Model) AlfrescoModelDiff(org.alfresco.solr.client.AlfrescoModelDiff) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Matchers.anyString(org.mockito.Matchers.anyString) AlfrescoModel(org.alfresco.solr.client.AlfrescoModel) TYPE(org.alfresco.solr.client.AlfrescoModelDiff.TYPE)

Example 2 with AlfrescoModelDiff

use of org.alfresco.solr.client.AlfrescoModelDiff 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)

Aggregations

M2Model (org.alfresco.repo.dictionary.M2Model)2 QName (org.alfresco.service.namespace.QName)2 AlfrescoModel (org.alfresco.solr.client.AlfrescoModel)2 AlfrescoModelDiff (org.alfresco.solr.client.AlfrescoModelDiff)2 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 M2Namespace (org.alfresco.repo.dictionary.M2Namespace)1 NamespaceDAO (org.alfresco.repo.dictionary.NamespaceDAO)1 StoreRef (org.alfresco.service.cmr.repository.StoreRef)1 TYPE (org.alfresco.solr.client.AlfrescoModelDiff.TYPE)1 Matchers.anyString (org.mockito.Matchers.anyString)1