Search in sources :

Example 1 with NamespaceDefinition

use of org.alfresco.service.cmr.dictionary.NamespaceDefinition in project alfresco-repository by Alfresco.

the class ModelValidatorImpl method canDeleteModel.

private boolean canDeleteModel(Collection<NamespaceDefinition> namespaceDefs, Collection<TypeDefinition> typeDefs, Collection<AspectDefinition> aspectDefs, Tenant tenant) {
    boolean canDelete = true;
    String tenantDomain = "for tenant [" + (tenant == null ? TenantService.DEFAULT_DOMAIN : tenant.getTenantDomain()) + "]";
    List<WorkflowDefinition> workflowDefs = workflowService.getDefinitions();
    if (workflowDefs.size() > 0) {
        if (namespaceDefs.size() > 0) {
            // check workflow namespace usage
            for (WorkflowDefinition workflowDef : workflowDefs) {
                String workflowDefName = workflowDef.getName();
                String workflowNamespaceURI = null;
                try {
                    workflowNamespaceURI = QName.createQName(BPMEngineRegistry.getLocalId(workflowDefName), namespaceService).getNamespaceURI();
                } catch (NamespaceException ne) {
                    logger.warn("Skipped workflow when validating model delete - unknown namespace: " + ne);
                    continue;
                }
                for (NamespaceDefinition namespaceDef : namespaceDefs) {
                    if (workflowNamespaceURI.equals(namespaceDef.getUri())) {
                        logger.warn("Failed to validate model delete" + tenantDomain + " - found workflow process definition " + workflowDefName + " using model namespace '" + namespaceDef.getUri() + "'");
                        canDelete = false;
                    }
                }
            }
        }
    }
    // check for type usages
    outer: for (TypeDefinition type : typeDefs) {
        try {
            validateDeleteClass(tenant, type);
        } catch (ModelInUseException e) {
            canDelete = false;
            break outer;
        } catch (ModelNotInUseException e) {
        // ok, continue
        }
    }
    // check for aspect usages
    outer: for (AspectDefinition aspect : aspectDefs) {
        try {
            validateDeleteClass(tenant, aspect);
        } catch (ModelInUseException e) {
            canDelete = false;
            break outer;
        } catch (ModelNotInUseException e) {
        // ok, continue
        }
    }
    return canDelete;
}
Also used : WorkflowDefinition(org.alfresco.service.cmr.workflow.WorkflowDefinition) NamespaceException(org.alfresco.service.namespace.NamespaceException) NamespaceDefinition(org.alfresco.service.cmr.dictionary.NamespaceDefinition) AspectDefinition(org.alfresco.service.cmr.dictionary.AspectDefinition) TypeDefinition(org.alfresco.service.cmr.dictionary.TypeDefinition)

Example 2 with NamespaceDefinition

use of org.alfresco.service.cmr.dictionary.NamespaceDefinition in project alfresco-repository by Alfresco.

the class ModelValidatorImpl method canDeleteModel.

/**
 * {@inheritDoc}
 */
@Override
public boolean canDeleteModel(final QName modelName) {
    boolean canDeleteModel = true;
    // TODO add model locking during delete (would need to be tenant-aware & cluster-aware) to avoid potential
    // for concurrent addition of new content/workflow as model is being deleted
    final Collection<NamespaceDefinition> namespaceDefs;
    final Collection<TypeDefinition> typeDefs;
    final Collection<AspectDefinition> aspectDefs;
    try {
        namespaceDefs = dictionaryDAO.getNamespaces(modelName);
        typeDefs = dictionaryDAO.getTypes(modelName);
        aspectDefs = dictionaryDAO.getAspects(modelName);
        // TODO - in case of MT we do not currently allow deletion of an overridden model (with usages) ... but could allow if (re-)inherited model is equivalent to an incremental update only ?
        canDeleteModel &= canDeleteModel(namespaceDefs, typeDefs, aspectDefs, null);
        if (canDeleteModel) {
            if (tenantService.isEnabled() && tenantService.isTenantUser() == false) {
                // TODO should fix this up - won't scale
                // shared model - need to check all tenants (whether enabled or disabled) unless they have overridden
                List<Tenant> tenants = tenantAdminService.getAllTenants();
                for (final Tenant tenant : tenants) {
                    // validate model delete within context of tenant domain
                    canDeleteModel &= AuthenticationUtil.runAs(new RunAsWork<Boolean>() {

                        public Boolean doWork() {
                            boolean canDelete = canDeleteModel(namespaceDefs, typeDefs, aspectDefs, tenant);
                            return canDelete;
                        }
                    }, tenantService.getDomainUser(AuthenticationUtil.getSystemUserName(), tenant.getTenantDomain()));
                    if (!canDeleteModel) {
                        break;
                    }
                }
            }
        }
    } catch (DictionaryException e) {
        if (logger.isDebugEnabled()) {
            logger.debug("Dictionary model '" + modelName + "' does not exist ... skip delete validation : " + e);
        }
        // we must return true here - there is no model
        canDeleteModel = true;
    }
    return canDeleteModel;
}
Also used : Tenant(org.alfresco.repo.tenant.Tenant) TenantRunAsWork(org.alfresco.repo.tenant.TenantUtil.TenantRunAsWork) RunAsWork(org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork) NamespaceDefinition(org.alfresco.service.cmr.dictionary.NamespaceDefinition) AspectDefinition(org.alfresco.service.cmr.dictionary.AspectDefinition) TypeDefinition(org.alfresco.service.cmr.dictionary.TypeDefinition) DictionaryException(org.alfresco.service.cmr.dictionary.DictionaryException)

Example 3 with NamespaceDefinition

use of org.alfresco.service.cmr.dictionary.NamespaceDefinition in project alfresco-repository by Alfresco.

the class CustomModelServiceImplTest method testCreateBasicInactiveModel.

@Test
public void testCreateBasicInactiveModel() throws Exception {
    final String modelName1 = makeUniqueName("testCustomModel1");
    final String desc = "This is test custom model desc";
    Pair<String, String> namespacePair = getTestNamespacePrefixPair();
    M2Model model = M2Model.createModel(namespacePair.getSecond() + QName.NAMESPACE_PREFIX + modelName1);
    model.createNamespace(namespacePair.getFirst(), namespacePair.getSecond());
    model.setDescription(desc);
    model.setAuthor("John Doe");
    // Create the 1st model
    CustomModelDefinition modelDefinition = createModel(model, false);
    assertNotNull(modelDefinition);
    assertEquals(modelName1, modelDefinition.getName().getLocalName());
    assertTrue("There is no imported namespace.", modelDefinition.getImportedNamespaces().isEmpty());
    NamespaceDefinition namespaceDefinition = modelDefinition.getNamespaces().iterator().next();
    assertNotNull(namespaceDefinition);
    assertEquals(namespacePair.getFirst(), namespaceDefinition.getUri());
    assertEquals(namespacePair.getSecond(), namespaceDefinition.getPrefix());
    assertEquals(desc, modelDefinition.getDescription());
    assertEquals("John Doe", modelDefinition.getAuthor());
    final String modelName2 = makeUniqueName("testCustomModel2");
    model = M2Model.createModel(namespacePair.getSecond() + QName.NAMESPACE_PREFIX + modelName2);
    model.createNamespace(namespacePair.getFirst(), "newTestPrefix");
    // Create the 2nd model - duplicate namespace URI
    try {
        createModel(model, false);
        fail("Shouldn't be able to create a model with an already in-use namespace uri.");
    } catch (NamespaceConstraintException ex) {
    // Expected
    }
    final String modelName3 = makeUniqueName("testCustomModel3");
    model = M2Model.createModel(namespacePair.getSecond() + QName.NAMESPACE_PREFIX + modelName3);
    model.createNamespace(getTestNamespacePrefixPair().getFirst(), namespacePair.getSecond());
    // Create the 2nd model - duplicate namespace Prefix
    try {
        createModel(model, false);
        fail("Shouldn't be able to create a model with an already in-use namespace prefix.");
    } catch (NamespaceConstraintException ex) {
    // Expected
    }
    namespacePair = getTestNamespacePrefixPair();
    model = M2Model.createModel(namespacePair.getSecond() + QName.NAMESPACE_PREFIX + modelName2);
    model.createNamespace(namespacePair.getFirst(), namespacePair.getSecond());
    modelDefinition = createModel(model, false);
    assertNotNull(modelDefinition);
    assertEquals(modelName2, modelDefinition.getName().getLocalName());
    namespaceDefinition = modelDefinition.getNamespaces().iterator().next();
    assertNotNull(namespaceDefinition);
    assertEquals(namespacePair.getFirst(), namespaceDefinition.getUri());
    assertEquals(namespacePair.getSecond(), namespaceDefinition.getPrefix());
    try {
        // Test duplicate model
        createModel(model, false);
        fail("Shouldn't be able to create a duplicate model.");
    } catch (Exception e) {
    // Expected
    }
    try {
        // Test creating a model with the same name as the bootstrapped model
        model.setName("contentmodel");
        createModel(model, false);
        fail("Shouldn't be able to create a model with the same name as the bootstrapped model.");
    } catch (Exception e) {
    // Expected
    }
    // Test list all models
    try {
        customModelService.getCustomModels(null);
        fail("Should have thrown IllegalArgumentException as PagingRequest was null.");
    } catch (IllegalArgumentException e) {
    // Expected
    }
    final PagingRequest pagingRequest = new PagingRequest(0, Integer.MAX_VALUE);
    PagingResults<CustomModelDefinition> result = transactionHelper.doInTransaction(new RetryingTransactionCallback<PagingResults<CustomModelDefinition>>() {

        public PagingResults<CustomModelDefinition> execute() throws Exception {
            return customModelService.getCustomModels(pagingRequest);
        }
    });
    assertTrue(result.getTotalResultCount().getFirst() >= 2);
}
Also used : CustomModelDefinition(org.alfresco.service.cmr.dictionary.CustomModelDefinition) PagingResults(org.alfresco.query.PagingResults) NamespaceDefinition(org.alfresco.service.cmr.dictionary.NamespaceDefinition) NamespaceConstraintException(org.alfresco.service.cmr.dictionary.CustomModelException.NamespaceConstraintException) NamespaceConstraintException(org.alfresco.service.cmr.dictionary.CustomModelException.NamespaceConstraintException) InvalidNamespaceException(org.alfresco.service.cmr.dictionary.CustomModelException.InvalidNamespaceException) ActiveModelConstraintException(org.alfresco.service.cmr.dictionary.CustomModelException.ActiveModelConstraintException) ModelDoesNotExistException(org.alfresco.service.cmr.dictionary.CustomModelException.ModelDoesNotExistException) PagingRequest(org.alfresco.query.PagingRequest) Test(org.junit.Test)

Example 4 with NamespaceDefinition

use of org.alfresco.service.cmr.dictionary.NamespaceDefinition in project alfresco-repository by Alfresco.

the class CustomModelServiceImplTest method testUpdateModel.

@Test
public void testUpdateModel() throws Exception {
    final String modelName = makeUniqueName("testUpdateCustomModel");
    final String desc = "This is test custom model desc";
    Pair<String, String> namespacePair = getTestNamespacePrefixPair();
    M2Model model = M2Model.createModel(namespacePair.getSecond() + QName.NAMESPACE_PREFIX + modelName);
    model.createNamespace(namespacePair.getFirst(), namespacePair.getSecond());
    model.setDescription(desc);
    model.setAuthor("John Doe");
    // Add aspect
    String aspectName = "testMarkerAspect";
    model.createAspect(namespacePair.getSecond() + QName.NAMESPACE_PREFIX + aspectName);
    // Create the model
    CustomModelDefinition modelDefinition = createModel(model, false);
    assertNotNull(modelDefinition);
    assertEquals(modelName, modelDefinition.getName().getLocalName());
    assertFalse(modelDefinition.isActive());
    NamespaceDefinition namespaceDefinition = modelDefinition.getNamespaces().iterator().next();
    assertEquals(namespacePair.getFirst(), namespaceDefinition.getUri());
    assertEquals(namespacePair.getSecond(), namespaceDefinition.getPrefix());
    assertEquals(desc, modelDefinition.getDescription());
    assertEquals("John Doe", modelDefinition.getAuthor());
    assertEquals(1, modelDefinition.getAspectDefinitions().size());
    // Update the model by removing the namespace
    model.removeNamespace(namespacePair.getFirst());
    try {
        updateModel(modelName, model, false);
        fail("Shouldn't be able to update a custom model with an empty namespace.");
    } catch (InvalidNamespaceException ex) {
    // Expected
    }
    // Update the model by removing the namespace prefix
    model.createNamespace(namespacePair.getFirst(), null);
    try {
        updateModel(modelName, model, false);
        fail("Model validation should have failed, as the namespace prefix is null.");
    } catch (IllegalArgumentException ex) {
    // Expected
    }
    // Update the model by adding more namespace URIs
    model.createNamespace("http://www.alfresco.org/model/contenttest/1.0", namespacePair.getSecond());
    try {
        updateModel(modelName, model, false);
        fail("Shouldn't be able to add more than one namespace URI into a custom model.");
    } catch (InvalidNamespaceException ex) {
    // Expected
    }
    // Update the namespace with a URI that has already been used
    model.removeNamespace(namespacePair.getFirst());
    model.removeNamespace("http://www.alfresco.org/model/contenttest/1.0");
    model.createNamespace("http://www.alfresco.org/model/content/1.0", namespacePair.getSecond());
    try {
        updateModel(modelName, model, false);
        fail("Shouldn't be able to update a model with an already in-use namespace URI.");
    } catch (NamespaceConstraintException ex) {
    // Expected
    }
    // Update the namespace with a Prefix that has already been used
    model.removeNamespace("http://www.alfresco.org/model/content/1.0");
    model.createNamespace(namespacePair.getFirst(), "cm");
    try {
        updateModel(modelName, model, false);
        fail("Shouldn't be able to update a model with an already in-use namespace Prefix.");
    } catch (NamespaceConstraintException ex) {
    // Expected
    }
    // New namespace
    Pair<String, String> newNamespacePair = getTestNamespacePrefixPair();
    model = M2Model.createModel(newNamespacePair.getSecond() + QName.NAMESPACE_PREFIX + modelName);
    model.createNamespace(newNamespacePair.getFirst(), newNamespacePair.getSecond());
    model.setDescription(desc);
    // Update non-existing model
    try {
        updateModel(modelName + "non-existing model", model, false);
        fail("Should have thrown ModelDoesNotExistException.");
    } catch (ModelDoesNotExistException ex) {
    // Expected
    }
    modelDefinition = updateModel(modelName, model, false);
    namespaceDefinition = modelDefinition.getNamespaces().iterator().next();
    assertEquals(newNamespacePair.getFirst(), namespaceDefinition.getUri());
    assertEquals(newNamespacePair.getSecond(), namespaceDefinition.getPrefix());
    assertEquals(desc, modelDefinition.getDescription());
    assertNull(modelDefinition.getAuthor());
    assertEquals(0, modelDefinition.getAspectDefinitions().size());
    // Test that the cache is updated correctly. This means the cache should have removed the old namespace URI.
    QName aspectQName = QName.createQName("{" + namespacePair.getFirst() + "}" + aspectName);
    AspectDefinition aspectDefinition = getAspect(aspectQName);
    assertNull(aspectDefinition);
    transactionHelper.doInTransaction(new RetryingTransactionCallback<Void>() {

        public Void execute() throws Exception {
            // Activate the model
            customModelService.activateCustomModel(modelName);
            return null;
        }
    });
    // Retrieve the model
    modelDefinition = getModel(modelName);
    assertNotNull(modelDefinition);
    assertTrue(modelDefinition.isActive());
    // Try to update only the namespace URI of an active model
    Pair<String, String> activeModelNamespacePair = getTestNamespacePrefixPair();
    model = M2Model.createModel(newNamespacePair.getSecond() + QName.NAMESPACE_PREFIX + modelName);
    model.createNamespace(activeModelNamespacePair.getFirst(), newNamespacePair.getSecond());
    try {
        // true => as we activated the model
        updateModel(modelName, model, true);
        fail("Shouldn't be able to update the namespace URI of an active model.");
    } catch (ActiveModelConstraintException ax) {
    // Expected
    }
    // Try to update only the namespace prefix of an active model
    activeModelNamespacePair = getTestNamespacePrefixPair();
    model = M2Model.createModel(activeModelNamespacePair.getSecond() + QName.NAMESPACE_PREFIX + modelName);
    model.createNamespace(newNamespacePair.getFirst(), activeModelNamespacePair.getSecond());
    try {
        // true => as we activated the model
        updateModel(modelName, model, true);
        fail("Shouldn't be able to update the namespace prefix of an active model.");
    } catch (ActiveModelConstraintException ax) {
    // Expected
    }
    // Try to update both the namespace URI and prefix of an active model
    activeModelNamespacePair = getTestNamespacePrefixPair();
    model = M2Model.createModel(activeModelNamespacePair.getSecond() + QName.NAMESPACE_PREFIX + modelName);
    model.createNamespace(activeModelNamespacePair.getFirst(), activeModelNamespacePair.getSecond());
    try {
        // true => as we activated the model
        updateModel(modelName, model, true);
        fail("Shouldn't be able to update the namespace URI and namespace prefix of an active model.");
    } catch (ActiveModelConstraintException ax) {
    // Expected
    }
    // Update active model's desc and author
    modelDefinition = getModel(modelName);
    namespaceDefinition = modelDefinition.getNamespaces().iterator().next();
    model = M2Model.createModel(namespaceDefinition.getPrefix() + QName.NAMESPACE_PREFIX + modelName);
    model.createNamespace(namespaceDefinition.getUri(), namespaceDefinition.getPrefix());
    model.setDescription(desc);
    model.setAuthor("Admin Admin");
    modelDefinition = updateModel(modelName, model, true);
    assertEquals(modelName, modelDefinition.getName().getLocalName());
    assertTrue(modelDefinition.isActive());
    assertEquals(desc, modelDefinition.getDescription());
    assertEquals("Admin Admin", modelDefinition.getAuthor());
}
Also used : CustomModelDefinition(org.alfresco.service.cmr.dictionary.CustomModelDefinition) QName(org.alfresco.service.namespace.QName) AspectDefinition(org.alfresco.service.cmr.dictionary.AspectDefinition) NamespaceConstraintException(org.alfresco.service.cmr.dictionary.CustomModelException.NamespaceConstraintException) InvalidNamespaceException(org.alfresco.service.cmr.dictionary.CustomModelException.InvalidNamespaceException) ActiveModelConstraintException(org.alfresco.service.cmr.dictionary.CustomModelException.ActiveModelConstraintException) ModelDoesNotExistException(org.alfresco.service.cmr.dictionary.CustomModelException.ModelDoesNotExistException) ActiveModelConstraintException(org.alfresco.service.cmr.dictionary.CustomModelException.ActiveModelConstraintException) InvalidNamespaceException(org.alfresco.service.cmr.dictionary.CustomModelException.InvalidNamespaceException) NamespaceDefinition(org.alfresco.service.cmr.dictionary.NamespaceDefinition) NamespaceConstraintException(org.alfresco.service.cmr.dictionary.CustomModelException.NamespaceConstraintException) ModelDoesNotExistException(org.alfresco.service.cmr.dictionary.CustomModelException.ModelDoesNotExistException) Test(org.junit.Test)

Example 5 with NamespaceDefinition

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

the class CustomModelsImpl method createCustomModel.

@Override
public CustomModel createCustomModel(M2Model m2Model) {
    // Check the current user is authorised to import the custom model
    validateCurrentUser();
    validateImportedM2Model(m2Model);
    CompiledModel compiledModel = null;
    try {
        compiledModel = customModelService.compileModel(m2Model);
    } catch (CustomModelConstraintException mce) {
        throw new ConstraintViolatedException(mce.getMessage());
    } catch (InvalidCustomModelException iex) {
        throw new InvalidArgumentException(iex.getMessage());
    }
    ModelDefinition modelDefinition = compiledModel.getModelDefinition();
    CustomModel customModel = new CustomModel();
    customModel.setName(modelDefinition.getName().getLocalName());
    customModel.setAuthor(modelDefinition.getAuthor());
    customModel.setDescription(modelDefinition.getDescription(dictionaryService));
    customModel.setStatus(ModelStatus.DRAFT);
    NamespaceDefinition nsd = modelDefinition.getNamespaces().iterator().next();
    customModel.setNamespaceUri(nsd.getUri());
    customModel.setNamespacePrefix(nsd.getPrefix());
    List<CustomType> customTypes = convertToCustomTypes(compiledModel.getTypes(), false);
    List<CustomAspect> customAspects = convertToCustomAspects(compiledModel.getAspects(), false);
    List<ConstraintDefinition> constraintDefinitions = CustomModelDefinitionImpl.removeInlineConstraints(compiledModel);
    List<CustomModelConstraint> customModelConstraints = convertToCustomModelConstraints(constraintDefinitions);
    customModel.setTypes(customTypes);
    customModel.setAspects(customAspects);
    customModel.setConstraints(customModelConstraints);
    return createCustomModelImpl(customModel, false);
}
Also used : CustomType(org.alfresco.rest.api.model.CustomType) InvalidCustomModelException(org.alfresco.service.cmr.dictionary.CustomModelException.InvalidCustomModelException) CustomAspect(org.alfresco.rest.api.model.CustomAspect) ConstraintViolatedException(org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException) CustomModelConstraintException(org.alfresco.service.cmr.dictionary.CustomModelException.CustomModelConstraintException) CustomModel(org.alfresco.rest.api.model.CustomModel) ConstraintDefinition(org.alfresco.service.cmr.dictionary.ConstraintDefinition) InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) CompiledModel(org.alfresco.repo.dictionary.CompiledModel) ModelDefinition(org.alfresco.service.cmr.dictionary.ModelDefinition) CustomModelDefinition(org.alfresco.service.cmr.dictionary.CustomModelDefinition) NamespaceDefinition(org.alfresco.service.cmr.dictionary.NamespaceDefinition) CustomModelConstraint(org.alfresco.rest.api.model.CustomModelConstraint)

Aggregations

NamespaceDefinition (org.alfresco.service.cmr.dictionary.NamespaceDefinition)5 AspectDefinition (org.alfresco.service.cmr.dictionary.AspectDefinition)3 CustomModelDefinition (org.alfresco.service.cmr.dictionary.CustomModelDefinition)3 ActiveModelConstraintException (org.alfresco.service.cmr.dictionary.CustomModelException.ActiveModelConstraintException)2 InvalidNamespaceException (org.alfresco.service.cmr.dictionary.CustomModelException.InvalidNamespaceException)2 ModelDoesNotExistException (org.alfresco.service.cmr.dictionary.CustomModelException.ModelDoesNotExistException)2 NamespaceConstraintException (org.alfresco.service.cmr.dictionary.CustomModelException.NamespaceConstraintException)2 TypeDefinition (org.alfresco.service.cmr.dictionary.TypeDefinition)2 Test (org.junit.Test)2 PagingRequest (org.alfresco.query.PagingRequest)1 PagingResults (org.alfresco.query.PagingResults)1 CompiledModel (org.alfresco.repo.dictionary.CompiledModel)1 RunAsWork (org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork)1 Tenant (org.alfresco.repo.tenant.Tenant)1 TenantRunAsWork (org.alfresco.repo.tenant.TenantUtil.TenantRunAsWork)1 CustomAspect (org.alfresco.rest.api.model.CustomAspect)1 CustomModel (org.alfresco.rest.api.model.CustomModel)1 CustomModelConstraint (org.alfresco.rest.api.model.CustomModelConstraint)1 CustomType (org.alfresco.rest.api.model.CustomType)1 ConstraintViolatedException (org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException)1