org.alfresco.service.cmr.dictionary.CustomModelDefinition Java Examples

The following examples show how to use org.alfresco.service.cmr.dictionary.CustomModelDefinition. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CustomModelsImpl.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 6 votes vote down vote up
private CustomModelDefinition updateModel(ModelDetails modelDetails, String errorMsg)
{
    M2Model m2Model = convertToM2Model(modelDetails);
    try
    {
        CustomModelDefinition modelDef = customModelService.updateCustomModel(modelDetails.getModel().getName(), m2Model, modelDetails.isActive());
        return modelDef;
    }
    catch (CustomModelConstraintException mce)
    {
        throw new ConstraintViolatedException(mce.getMessage());
    }
    catch (InvalidCustomModelException iex)
    {
        throw new InvalidArgumentException(iex.getMessage());
    }
    catch (Exception ex)
    {
        if (ex.getMessage() != null)
        {
            errorMsg = ex.getMessage();
        }
        throw new ApiException(errorMsg, ex);
    }
}
 
Example #2
Source File: CustomModelsImpl.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public CustomModelConstraint getCustomModelConstraint(String modelName, String constraintName, Parameters parameters)
{
    if (constraintName == null)
    {
        throw new InvalidArgumentException(CONSTRAINT_NAME_NULL_ERR);
    }

    final CustomModelDefinition modelDef = getCustomModelImpl(modelName);
    QName constraintQname = QName.createQName(modelDef.getName().getNamespaceURI(), constraintName);

    ConstraintDefinition constraintDef = customModelService.getCustomConstraint(constraintQname);
    if (constraintDef == null)
    {
        throw new EntityNotFoundException(constraintName);
    }

    return new CustomModelConstraint(constraintDef, dictionaryService);
}
 
Example #3
Source File: CustomModelServiceImpl.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public CustomModelsInfo getCustomModelsInfo()
{
    List<CustomModelDefinition> page = getCustomModels(new PagingRequest(0, Integer.MAX_VALUE)).getPage();

    int activeModels = 0;
    int activeTypes = 0;
    int activeAspects = 0;
    for (CustomModelDefinition cm : page)
    {
        if (cm.isActive())
        {
            activeModels++;
            activeTypes += cm.getTypeDefinitions().size();
            activeAspects += cm.getAspectDefinitions().size();
        }
    }

    CustomModelsInfo info = new CustomModelsInfo();
    info.setNumberOfActiveModels(activeModels);
    info.setNumberOfActiveTypes(activeTypes);
    info.setNumberOfActiveAspects(activeAspects);
    return info;
}
 
Example #4
Source File: CustomModelsImpl.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 6 votes vote down vote up
private CustomModelDefinition getCustomModelImpl(String modelName)
{
    if(modelName == null)
    {
        throw new InvalidArgumentException(MODEL_NAME_NULL_ERR);
    }

    CustomModelDefinition model = null;
    try
    {
        model = customModelService.getCustomModel(modelName);
    }
    catch (CustomModelException ex)
    {
        throw new EntityNotFoundException(modelName);
    }

    if (model == null)
    {
        throw new EntityNotFoundException(modelName);
    }

    return model;
}
 
Example #5
Source File: CustomModelsImpl.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public CollectionWithPagingInfo<CustomModel> getCustomModels(Parameters parameters)
{
    Paging paging = parameters.getPaging();
    PagingRequest pagingRequest = Util.getPagingRequest(paging);
    PagingResults<CustomModelDefinition> results = customModelService.getCustomModels(pagingRequest);

    Integer totalItems = results.getTotalResultCount().getFirst();
    List<CustomModelDefinition> page = results.getPage();

    List<CustomModel> models = new ArrayList<>(page.size());
    for (CustomModelDefinition modelDefinition : page)
    {
        models.add(new CustomModel(modelDefinition));
    }

    return CollectionWithPagingInfo.asPaged(paging, models, results.hasMoreItems(), (totalItems == null ? null : totalItems.intValue()));
}
 
Example #6
Source File: CustomModelsImpl.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public CustomAspect getCustomAspect(String modelName, String aspectName, Parameters parameters)
{
    if(aspectName == null)
    {
        throw new InvalidArgumentException(ASPECT_NAME_NULL_ERR);
    }

    final CustomModelDefinition modelDef = getCustomModelImpl(modelName);
    QName aspectQname = QName.createQName(modelDef.getName().getNamespaceURI(), aspectName);

    AspectDefinition customAspectDef = customModelService.getCustomAspect(aspectQname);
    if (customAspectDef == null)
    {
        throw new EntityNotFoundException(aspectName);
    }
    
    // Check if inherited properties have been requested
    boolean includeInheritedProps = hasSelectProperty(parameters, SELECT_ALL_PROPS);
    return convertToCustomAspect(customAspectDef, includeInheritedProps);
}
 
Example #7
Source File: CustomModelServiceImplTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void testListTypesAspects_Empty() throws Exception
{
    final String modelName = makeUniqueName("testCustomModel");
    Pair<String, String> namespacePair = getTestNamespacePrefixPair();

    final M2Model model = M2Model.createModel(namespacePair.getSecond() + QName.NAMESPACE_PREFIX + modelName);
    model.createNamespace(namespacePair.getFirst(), namespacePair.getSecond());

    createModel(model, false);

    // Retrieve the created model
    CustomModelDefinition modelDefinition = getModel(modelName);
    assertNotNull(modelDefinition);
    assertEquals(modelName, modelDefinition.getName().getLocalName());

    // List all of the model's types
    Collection<TypeDefinition> types = modelDefinition.getTypeDefinitions();
    assertEquals(0, types.size());

    // List all of the model's aspects
    Collection<AspectDefinition> aspects = modelDefinition.getAspectDefinitions();
    assertEquals(0, aspects.size());
}
 
Example #8
Source File: CustomModelsImpl.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public CustomType getCustomType(String modelName, String typeName, Parameters parameters)
{
    if(typeName == null)
    {
        throw new InvalidArgumentException(TYPE_NAME_NULL_ERR);
    }

    final CustomModelDefinition modelDef = getCustomModelImpl(modelName);
    QName typeQname = QName.createQName(modelDef.getName().getNamespaceURI(), typeName);

    TypeDefinition customTypeDef = customModelService.getCustomType(typeQname);
    if (customTypeDef == null)
    {
        throw new EntityNotFoundException(typeName);
    }
    
    // Check if inherited properties have been requested
    boolean includeInheritedProps = hasSelectProperty(parameters, SELECT_ALL_PROPS);
    return convertToCustomType(customTypeDef, includeInheritedProps);
}
 
Example #9
Source File: CustomModel.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 5 votes vote down vote up
public CustomModel(CustomModelDefinition modelDefinition, List<CustomType> types, List<CustomAspect> aspects, List<CustomModelConstraint> constraints)
{
    this.name = modelDefinition.getName().getLocalName();
    this.author = modelDefinition.getAuthor();
    this.description = modelDefinition.getDescription();
    this.status = modelDefinition.isActive() ? ModelStatus.ACTIVE : ModelStatus.DRAFT;
    // we don't need to check for NoSuchElementException, as we don't allow
    // the model to be saved without a valid namespace
    NamespaceDefinition nsd = modelDefinition.getNamespaces().iterator().next();
    this.namespaceUri = nsd.getUri();
    this.namespacePrefix = nsd.getPrefix();
    this.types = types;
    this.aspects = aspects;
    this.constraints = constraints;
}
 
Example #10
Source File: CustomModelServiceImplTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
private CustomModelDefinition getModel(final String modelName)
{
    return transactionHelper.doInTransaction(new RetryingTransactionCallback<CustomModelDefinition>()
    {
        public CustomModelDefinition execute() throws Exception
        {
            return customModelService.getCustomModel(modelName);
        }
    });
}
 
Example #11
Source File: CustomModelsImpl.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public CustomModel getCustomModel(String modelName, Parameters parameters)
{
    CustomModelDefinition modelDef = getCustomModelImpl(modelName);

    if (hasSelectProperty(parameters, SELECT_ALL))
    {
        return new CustomModel(modelDef, 
                    convertToCustomTypes(modelDef.getTypeDefinitions(), false),
                    convertToCustomAspects(modelDef.getAspectDefinitions(), false),
                    convertToCustomModelConstraints(modelDef.getModelDefinedConstraints()));
    }

    return new CustomModel(modelDef);
}
 
Example #12
Source File: CustomModelsImpl.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 5 votes vote down vote up
private CustomModel createCustomModelImpl(CustomModel model, boolean basicModelOnly)
{
    M2Model m2Model = null;
    if (basicModelOnly)
    {
        m2Model = convertToM2Model(model, null, null, null);
    }
    else
    {
        m2Model = convertToM2Model(model, model.getTypes(), model.getAspects(), model.getConstraints());
    }

    boolean activate = ModelStatus.ACTIVE.equals(model.getStatus());
    try
    {
        CustomModelDefinition modelDefinition = customModelService.createCustomModel(m2Model, activate);
        return new CustomModel(modelDefinition);
    }
    catch (ModelExistsException me)
    {
        throw new ConstraintViolatedException(me.getMessage());
    }
    catch (CustomModelConstraintException ncx)
    {
        throw new ConstraintViolatedException(ncx.getMessage());
    }
    catch (InvalidCustomModelException iex)
    {
        throw new InvalidArgumentException(iex.getMessage());
    }
    catch (Exception e)
    {
        throw new ApiException("cmm.rest_api.model_invalid", e);
    }
}
 
Example #13
Source File: CustomModelsImpl.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public CollectionWithPagingInfo<CustomType> getCustomTypes(String modelName, Parameters parameters)
{
    CustomModelDefinition modelDef = getCustomModelImpl(modelName);
    Collection<TypeDefinition> typeDefinitions = modelDef.getTypeDefinitions();
    // TODO Should we support paging?
    Paging paging = Paging.DEFAULT;

    List<CustomType> customTypes = convertToCustomTypes(typeDefinitions, false);

    return CollectionWithPagingInfo.asPaged(paging, customTypes, false, typeDefinitions.size());

}
 
Example #14
Source File: CustomModelsImpl.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public CollectionWithPagingInfo<CustomAspect> getCustomAspects(String modelName, Parameters parameters)
{
    CustomModelDefinition modelDef = getCustomModelImpl(modelName);
    Collection<AspectDefinition> aspectDefinitions = modelDef.getAspectDefinitions();
    // TODO Should we support paging?
    Paging paging = Paging.DEFAULT;

    List<CustomAspect> customAspects = convertToCustomAspects(aspectDefinitions, false);

    return CollectionWithPagingInfo.asPaged(paging, customAspects, false, aspectDefinitions.size());
}
 
Example #15
Source File: CustomModelsImpl.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public CollectionWithPagingInfo<CustomModelConstraint> getCustomModelConstraints(String modelName, Parameters parameters)
{
    CustomModelDefinition modelDef = getCustomModelImpl(modelName);
    Collection<ConstraintDefinition> constraintDefinitions = modelDef.getModelDefinedConstraints();
    // TODO Should we support paging?
    Paging paging = Paging.DEFAULT;

    List<CustomModelConstraint> customModelConstraints = convertToCustomModelConstraints(constraintDefinitions);

    return CollectionWithPagingInfo.asPaged(paging, customModelConstraints, false, constraintDefinitions.size());
}
 
Example #16
Source File: CustomModelsImpl.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 5 votes vote down vote up
public ModelDetails(CustomModelDefinition modelDefinition)
{
    this.model = new CustomModel(modelDefinition);
    this.active = modelDefinition.isActive();
    this.types = convertToCustomTypes(modelDefinition.getTypeDefinitions(), false);
    this.aspects = convertToCustomAspects(modelDefinition.getAspectDefinitions(), false);
    this.modelDefinedConstraints = convertToCustomModelConstraints(modelDefinition.getModelDefinedConstraints());
}
 
Example #17
Source File: BaseCustomModelApiTest.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 5 votes vote down vote up
protected CustomModelDefinition getModelDefinition(final String modelName)
{
    return transactionHelper.doInTransaction(new RetryingTransactionCallback<CustomModelDefinition>()
    {
        @Override
        public CustomModelDefinition execute() throws Throwable
        {
            return customModelService.getCustomModel(modelName);
        }
    });
}
 
Example #18
Source File: CustomModelServiceImpl.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public CustomModelDefinition getCustomModel(String modelName)
{
    ParameterCheck.mandatoryString("modelName", modelName);

    Pair<CompiledModel, Boolean> compiledModelPair = getCustomCompiledModel(modelName);
    CustomModelDefinition result = (compiledModelPair == null) ? null : new CustomModelDefinitionImpl(
                compiledModelPair.getFirst(), compiledModelPair.getSecond(), dictionaryService);

    return result;
}
 
Example #19
Source File: CustomModelServiceImplTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
private CustomModelDefinition updateModel(final String modelName, final M2Model m2Model, final boolean activate)
{
    return transactionHelper.doInTransaction(new RetryingTransactionCallback<CustomModelDefinition>()
    {
        public CustomModelDefinition execute() throws Exception
        {
            return customModelService.updateCustomModel(modelName, m2Model, activate);
        }
    });
}
 
Example #20
Source File: CustomModelServiceImplTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
private CustomModelDefinition createModel(final M2Model m2Model, final boolean activate)
{
    return transactionHelper.doInTransaction(new RetryingTransactionCallback<CustomModelDefinition>()
    {
        public CustomModelDefinition execute() throws Exception
        {
            return customModelService.createCustomModel(m2Model, activate);
        }
    });
}
 
Example #21
Source File: CustomModelServiceImplTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void testisNamespaceUriExists()
{
    final String modelName = makeUniqueName("testCustomModel");

    final Pair<String, String> namespacePair = getTestNamespacePrefixPair();
    M2Model model = M2Model.createModel(namespacePair.getSecond() + QName.NAMESPACE_PREFIX + modelName);
    model.createNamespace(namespacePair.getFirst(), namespacePair.getSecond());
    model.setAuthor("John Doe");

    assertNull(customModelService.getCustomModelByUri(namespacePair.getFirst()));
    // Create the model
    CustomModelDefinition modelDefinition = createModel(model, false);
    assertNotNull(modelDefinition);
    assertEquals(modelName, modelDefinition.getName().getLocalName());

    ModelDefinition modelDefinitionByUri = transactionHelper.doInTransaction(new RetryingTransactionCallback<ModelDefinition>()
    {
        @Override
        public ModelDefinition execute() throws Throwable
        {
            assertTrue(customModelService.isNamespaceUriExists(namespacePair.getFirst()));
            return customModelService.getCustomModelByUri(namespacePair.getFirst());
        }
    });
    assertNotNull(modelDefinitionByUri);
    assertEquals(modelName, modelDefinitionByUri.getName().getLocalName());
}
 
Example #22
Source File: CustomModelRepoRestartTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
private CustomModelDefinition getModel(final String modelName)
{
    return transactionHelper.doInTransaction(new RetryingTransactionCallback<CustomModelDefinition>()
    {
        public CustomModelDefinition execute() throws Exception
        {
            return customModelService.getCustomModel(modelName);
        }
    });
}
 
Example #23
Source File: CustomModelServiceImpl.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void deactivateCustomModel(final String modelName)
{
    CustomModelDefinition customModelDefinition = getCustomModel(modelName);
    if (customModelDefinition == null)
    {
        throw new CustomModelException.ModelDoesNotExistException(MSG_MODEL_NOT_EXISTS, new Object[] { modelName });
    }

    Collection<TypeDefinition> modelTypes = customModelDefinition.getTypeDefinitions();
    Collection<AspectDefinition> modelAspects = customModelDefinition.getAspectDefinitions();

    for (CompiledModel cm : getAllCustomM2Models(false))
    {
        // Ignore type/aspect dependency check within the model itself
        if (!customModelDefinition.getName().equals(cm.getModelDefinition().getName()))
        {
            // Check if the type of the model being deactivated is the parent of another model's type
            validateTypeAspectDependency(modelTypes, cm.getTypes());

            // Check if the aspect of the model being deactivated is the parent of another model's aspect
            validateTypeAspectDependency(modelAspects, cm.getAspects());
        }
    }

    // requiresNewTx = true, in order to catch any exception thrown within
    // "DictionaryModelType$DictionaryModelTypeTransactionListener" model validation.
    doInTransaction(MSG_UNABLE_MODEL_DEACTIVATE, true, new RetryingTransactionCallback<Void>()
    {
        public Void execute() throws Exception
        {
            repoAdminService.deactivateModel(modelName);
            return null;
        }
    });
}
 
Example #24
Source File: CustomModelServiceImpl.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public CustomModelDefinition createCustomModel(M2Model m2Model, boolean activate)
{
    ParameterCheck.mandatory("m2Model", m2Model);

    String modelName = m2Model.getName();
    int colonIndex = modelName.indexOf(QName.NAMESPACE_PREFIX);
    final String modelFileName = (colonIndex == -1) ? modelName : modelName.substring(colonIndex + 1);

    if (isModelExists(modelFileName))
    {
        throw new CustomModelException.ModelExistsException(MSG_NAME_ALREADY_IN_USE, new Object[] { modelFileName });
    }

    // Validate the model namespace URI
    validateModelNamespaceUri(getModelNamespaceUriPrefix(m2Model).getFirst());
    // Validate the model namespace prefix
    validateModelNamespacePrefix(getModelNamespaceUriPrefix(m2Model).getSecond());

    // Return the created model definition
    CompiledModel compiledModel = createUpdateModel(modelFileName, m2Model, activate, MSG_CREATE_MODEL_ERR, false);
    CustomModelDefinition modelDef = new CustomModelDefinitionImpl(compiledModel, activate, dictionaryService);

    if (logger.isDebugEnabled())
    {
        logger.debug(modelFileName + " model has been created.");
    }
    return modelDef;
}
 
Example #25
Source File: CustomModelServiceImpl.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public PagingResults<CustomModelDefinition> getCustomModels(PagingRequest pagingRequest)
{
    ParameterCheck.mandatory("pagingRequest", pagingRequest);

    List<CustomModelDefinition> result = getAllCustomModels();
    return result.isEmpty() ? new EmptyPagingResults<CustomModelDefinition>() : wrapResult(pagingRequest, result);
}
 
Example #26
Source File: CustomModel.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 4 votes vote down vote up
public CustomModel(CustomModelDefinition modelDefinition)
{
    this(modelDefinition, null, null, null);
}
 
Example #27
Source File: CustomModelServiceImplTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Test
public void testCreateDownload() throws Exception
{
    final String modelName = makeUniqueName("testDownloadCustomModel");
    final String modelExportFileName = modelName + ".xml";
    final String shareExtExportFileName = "CMM_" + modelName + "_module.xml";

    Pair<String, String> namespacePair = getTestNamespacePrefixPair();
    M2Model model = M2Model.createModel(namespacePair.getSecond() + QName.NAMESPACE_PREFIX + modelName);
    model.createNamespace(namespacePair.getFirst(), namespacePair.getSecond());
    model.setAuthor("Admin");
    model.createImport("http://www.alfresco.org/model/content/1.0", "cm");

    // Add Type
    String typeName = "testType";
    M2Type m2Type = model.createType(namespacePair.getSecond() + QName.NAMESPACE_PREFIX + typeName);
    m2Type.setTitle("Test type title");
    m2Type.setParentName("cm:content");

    // Create the model
    CustomModelDefinition modelDefinition = createModel(model, false);

    assertNotNull(modelDefinition);
    assertEquals(modelName, modelDefinition.getName().getLocalName());

    NodeRef downloadNode = createDownload(modelName, false);
    assertNotNull(downloadNode);

    DownloadStatus status = cmmDownloadTestUtil.getDownloadStatus(downloadNode);
    while (status.getStatus() == DownloadStatus.Status.PENDING)
    {
        Thread.sleep(PAUSE_TIME);
        status = cmmDownloadTestUtil.getDownloadStatus(downloadNode);
    }

    Set<String> entries = cmmDownloadTestUtil.getDownloadEntries(downloadNode);
    assertEquals(1, entries.size());
    String modelEntry = cmmDownloadTestUtil.getDownloadEntry(entries, modelExportFileName);
    assertNotNull(modelEntry);
    assertEquals(modelEntry, modelExportFileName);

    // Create Share extension module
    cmmDownloadTestUtil.createShareExtModule(modelName);

    downloadNode = createDownload(modelName, true);
    assertNotNull(downloadNode);

    status = cmmDownloadTestUtil.getDownloadStatus(downloadNode);
    while (status.getStatus() == DownloadStatus.Status.PENDING)
    {
        Thread.sleep(PAUSE_TIME);
        status = cmmDownloadTestUtil.getDownloadStatus(downloadNode);
    }

    entries = cmmDownloadTestUtil.getDownloadEntries(downloadNode);
    assertEquals(2, entries.size());

    modelEntry = cmmDownloadTestUtil.getDownloadEntry(entries, modelExportFileName);
    assertNotNull(modelEntry);
    assertEquals(modelEntry, modelExportFileName);

    String shareExtEntry = cmmDownloadTestUtil.getDownloadEntry(entries, shareExtExportFileName);
    assertNotNull(shareExtEntry);
    assertEquals(shareExtEntry, shareExtExportFileName);

    // Create Share extension module - this will override the existing module
    cmmDownloadTestUtil.createShareExtModule(modelName + System.currentTimeMillis());

    // The module id dose not exist, so the CMM service logs the error
    // (warning) and creates a zip containing only the model.
    downloadNode = createDownload(modelName, true);
    assertNotNull(downloadNode);

    status = cmmDownloadTestUtil.getDownloadStatus(downloadNode);
    while (status.getStatus() == DownloadStatus.Status.PENDING)
    {
        Thread.sleep(PAUSE_TIME);
        status = cmmDownloadTestUtil.getDownloadStatus(downloadNode);
    }

    entries = cmmDownloadTestUtil.getDownloadEntries(downloadNode);
    assertEquals(1, entries.size());
    modelEntry = cmmDownloadTestUtil.getDownloadEntry(entries, modelExportFileName);
    assertNotNull(modelEntry);
    assertEquals(modelEntry, modelExportFileName);
}
 
Example #28
Source File: CustomModelServiceImplTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Test
public void testDeactivateModel() throws Exception
{
    final String modelName = makeUniqueName("testDeactivateCustomModel");
    final String desc = "This is test custom model desc";

    Pair<String, String> namespacePair = getTestNamespacePrefixPair();
    final M2Model model = M2Model.createModel(namespacePair.getSecond() + QName.NAMESPACE_PREFIX + modelName);
    model.createNamespace(namespacePair.getFirst(), namespacePair.getSecond());
    model.setDescription(desc);
    model.setAuthor("John Doe");

    // Create the model
    CustomModelDefinition modelDefinition = createModel(model, true);
    assertNotNull(modelDefinition);
    assertEquals(modelName, modelDefinition.getName().getLocalName());
    assertTrue(modelDefinition.isActive());

    // Deactivate the model
    customModelService.deactivateCustomModel(modelName);

    // Retrieve the model
    modelDefinition = transactionHelper.doInTransaction(new RetryingTransactionCallback<CustomModelDefinition>()
    {
        public CustomModelDefinition execute() throws Exception
        {
            return customModelService.getCustomModel(modelName);
        }
    });

    assertNotNull(modelDefinition);
    assertEquals(modelName, modelDefinition.getName().getLocalName());
    assertFalse(modelDefinition.isActive());

    // Try to deactivate the model again
    try
    {
        customModelService.deactivateCustomModel(modelName);
        fail("Shouldn't be able to deactivate an already deactivated model.");
    }
    catch (Exception ex)
    {
        // Expected
    }
}
 
Example #29
Source File: CustomModelsImpl.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public CustomModel updateCustomModel(String modelName, CustomModel model, Parameters parameters)
{
    // Check the current user is authorised to update the custom model
    validateCurrentUser();

    // Check to see if the model exists
    ModelDetails existingModelDetails = new ModelDetails(getCustomModelImpl(modelName));
    CustomModel existingModel = existingModelDetails.getModel();

    // The model just needs to be activated/deactivated (in other words,
    // the other properties should be untouched)
    if (hasSelectProperty(parameters, SELECT_STATUS))
    {
        ModelStatus status = model.getStatus();
        if (status == null)
        {
            throw new InvalidArgumentException("cmm.rest_api.model_status_null");
        }
        try
        {
            if (ModelStatus.ACTIVE.equals(status))
            {
                customModelService.activateCustomModel(modelName);
            }
            else
            {
                customModelService.deactivateCustomModel(modelName);
            }
            // update the model's status
            existingModel.setStatus(status);
            return existingModel;
        }
        catch (CustomModelConstraintException mce)
        {
            throw new ConstraintViolatedException(mce.getMessage());
        }
        catch (Exception ex)
        {
            throw new ApiException(ex.getMessage(), ex);
        }
    }
    else
    {
        if (model.getName() != null && !(existingModel.getName().equals(model.getName())))
        {
            throw new InvalidArgumentException("cmm.rest_api.model_name_cannot_update");
        }

        existingModel.setNamespaceUri(model.getNamespaceUri());
        final boolean isNamespacePrefixChanged = !(existingModel.getNamespacePrefix().equals(model.getNamespacePrefix()));
        if(isNamespacePrefixChanged)
        {
            // Change types' and aspects' parents as well as the property constraint's Ref namespace prefix
            replacePrefix(existingModelDetails.getTypes(), existingModel.getNamespacePrefix(), model.getNamespacePrefix());
            replacePrefix(existingModelDetails.getAspects(), existingModel.getNamespacePrefix(), model.getNamespacePrefix());
        }
        existingModel.setNamespacePrefix(model.getNamespacePrefix());
        existingModel.setAuthor(model.getAuthor());
        existingModel.setDescription(model.getDescription());

        CustomModelDefinition modelDef = updateModel(existingModelDetails, "cmm.rest_api.model_update_failure");
        return new CustomModel(modelDef);
    }
}
 
Example #30
Source File: CustomModelServiceImplTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Test
public void testActivateModel() throws Exception
{
    final String modelName = makeUniqueName("testCustomModel");
    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");

    // Create the model
    CustomModelDefinition modelDefinition = createModel(model, false);

    assertNotNull(modelDefinition);
    assertEquals(modelName, modelDefinition.getName().getLocalName());
    assertFalse(modelDefinition.isActive());

    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);
    assertEquals(modelName, modelDefinition.getName().getLocalName());
    assertTrue(modelDefinition.isActive());

    // Try to activate the model again
    try
    {
        customModelService.activateCustomModel(modelName);
        fail("Shouldn't be able to activate an already activated model.");
    }
    catch (Exception ex)
    {
        // Expected
    }
}