private QName addModel() { this.testNamespace = "http://www.alfresco.org/test/" + modelName; this.modelQName = QName.createQName(testNamespace, modelName); // Create a model this.model = M2Model.createModel(modelName + ":" + modelName); model.createNamespace(testNamespace, modelName); model.createImport(NamespaceService.DICTIONARY_MODEL_1_0_URI, "d"); model.createImport(NamespaceService.CONTENT_MODEL_1_0_URI, NamespaceService.CONTENT_MODEL_PREFIX); this.typeQName = QName.createQName(testNamespace, "type1"); this.type = model.createType(modelName + ":" + typeQName.getLocalName()); type.setParentName("cm:folder"); this.propertyQName = QName.createQName(testNamespace, "prop1"); this.property = type.createProperty(modelName + ":" + propertyQName.getLocalName()); property.setType("d:text"); dictionaryDAO.putModel(model); return modelQName; }
this.model.removeImport(NamespaceService.DICTIONARY_MODEL_1_0_URI); CompiledModel compiledModel = model.compile(dictionaryDAO, namespaceDAO, true); modelValidator.validateModel(compiledModel); this.model.removeImport(NamespaceService.CONTENT_MODEL_1_0_URI); try model.compile(dictionaryDAO, namespaceDAO, true); fail("Should have failed as the model's type references the content model (cm:folder)."); model.createImport(NamespaceService.CONTENT_MODEL_1_0_URI, NamespaceService.CONTENT_MODEL_PREFIX); model.compile(dictionaryDAO, namespaceDAO, true); this.model.removeNamespace(testNamespace); try model.compile(dictionaryDAO, namespaceDAO, true); fail("Should have failed as the type's name references the namespace.");
private Pair<String, String> getModelNamespaceUriPrefix(M2Model model) { ParameterCheck.mandatory("model", model); List<M2Namespace> namespaces = model.getNamespaces(); if (namespaces.isEmpty()) { throw new CustomModelException.InvalidNamespaceException(MSG_NAMESPACE_NOT_EXISTS, new Object[] { model.getName() }); } if (namespaces.size() > 1) { throw new CustomModelException.InvalidNamespaceException(MSG_NAMESPACE_MANY_EXIST, new Object[] { model.getName() }); } M2Namespace ns = namespaces.iterator().next(); return new Pair<>(ns.getUri(), ns.getPrefix()); }
String modelName = model.getName(); if (loadedModels.contains(modelName) == false) for (M2Namespace importNamespace : model.getImports())
private void checkCircularDependency(ModelDefinition model, M2Model existingModel, String parentPrefixedName) throws AlfrescoRuntimeException { for (NamespaceDefinition importedNamespace : model.getImportedNamespaces()) { ModelDefinition md = null; if ((md = dictionaryService.getModelByNamespaceUri(importedNamespace.getUri())) != null) { if (existingModel.getNamespace(importedNamespace.getUri()) != null) { throw new AlfrescoRuntimeException("Failed to validate model update - found circular dependency. You can't set parent " + parentPrefixedName + " as it's model already depends on " + existingModel.getName()); } checkCircularDependency(md, existingModel, parentPrefixedName); } } }
private M2Model getM2Model(final NodeRef modelNodeRef) { ContentReader reader = contentService.getReader(modelNodeRef, ContentModel.PROP_CONTENT); if (reader == null) { return null; } InputStream in = reader.getContentInputStream(); try { return M2Model.createModel(in); } finally { if (in != null) { try { in.close(); } catch (IOException e) { logger.error("Failed to close input stream for " + modelNodeRef); } } } }
final M2Model model = M2Model.createModel(namespacePair.getSecond() + QName.NAMESPACE_PREFIX + modelName); model.createNamespace(namespacePair.getFirst(), namespacePair.getSecond()); model.setDescription(desc); model.setAuthor("John Doe");
M2Model model = M2Model.createModel(namespacePair.getSecond() + QName.NAMESPACE_PREFIX + modelName); model.createNamespace(namespacePair.getFirst(), namespacePair.getSecond()); model.setDescription(desc); model.setAuthor("John Doe"); model.createAspect(namespacePair.getSecond() + QName.NAMESPACE_PREFIX + aspectName); model.removeNamespace(namespacePair.getFirst()); try model.createNamespace(namespacePair.getFirst(), null); try model.createNamespace("http://www.alfresco.org/model/contenttest/1.0", namespacePair.getSecond()); try 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 model.removeNamespace("http://www.alfresco.org/model/content/1.0"); model.createNamespace(namespacePair.getFirst(), "cm"); try model = M2Model.createModel(newNamespacePair.getSecond() + QName.NAMESPACE_PREFIX + modelName); model.createNamespace(newNamespacePair.getFirst(), newNamespacePair.getSecond()); model.setDescription(desc);
try M2Model model = M2Model.createModel(is); modelName = model.getName();
M2Model testModel = M2Model.createModel(modelStream); dictionaryDAO.putModel(testModel); assertEquals("Unexpected number of removed models", 0, diffResults1.getRemovedModels().size()); AlfrescoModelDiff diff = diffResults1.getNewModels().get(0); assertEquals("Unexpected model name change", QName.createQName(testModel.getName(), namespaceService).toString(), diff.getModelName()); Long testModelChecksum = tracker.getChecksum(QName.createQName(testModel.getName(), namespaceService)); assertNotNull("", testModelChecksum); M2Type anotherType = testModel.createType("anothertype"); M2Property prop1 = anotherType.createProperty("prop1"); prop1.setType("d:text"); assertEquals("Unexpected changed model name", QName.createQName(testModel.getName(), namespaceService).toString(), changedModel.getModelName()); assertNotNull("", changedModel.getOldChecksum().longValue()); dictionaryDAO.removeModel(QName.createQName(testModel.getName(), namespaceService));
@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()); }
m2Model = M2Model.createModel(is); CompiledModel compiledModel= m2Model.compile(dictionaryDAO, namespaceDAO, true); ModelDefinition modelDefinition = compiledModel.getModelDefinition();
M2Model model = M2Model.createModel("tempModel"); model.createNamespace("test", "t"); model.createNamespace("testx", ""); for (int m = 0; m < 30; m++) model.createAspect("t:aspect_" + m);
@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()); }
return m2Model.compile(dictionaryDAO, namespaceDAO, true);
private CompiledModel createUpdateModel(final String modelFileName, final M2Model m2Model, final boolean activate, String errMsgId, boolean requiresNewTx) { // Validate model CompiledModel compiledModel = compileModel(m2Model); // Validate properties default values validatePropsDefaultValues(compiledModel); ByteArrayOutputStream xml = new ByteArrayOutputStream(); m2Model.toXML(xml); final InputStream modelStream = new ByteArrayInputStream(xml.toByteArray()); // Create the model node NodeRef nodeRef = doInTransaction(errMsgId, requiresNewTx, new RetryingTransactionCallback<NodeRef>() { public NodeRef execute() throws Exception { return repoAdminService.deployModel(modelStream, modelFileName, activate); } }); if (!nodeService.hasAspect(nodeRef, ASPECT_CUSTOM_MODEL)) { // Add the 'customModelManagement' marker aspect, to // indicate that this model has been created dynamically by this service nodeService.addAspect(nodeRef, ASPECT_CUSTOM_MODEL, null); } // Add hidden aspect if (!hiddenAspect.hasHiddenAspect(nodeRef)) { hiddenAspect.hideNode(nodeRef, false, false, false); } return compiledModel; }
private M2Model getM2Model(final NodeRef modelNodeRef) { ContentReader reader = contentService.getReader(modelNodeRef, ContentModel.PROP_CONTENT); if (reader == null) { return null; } InputStream in = reader.getContentInputStream(); try { return M2Model.createModel(in); } finally { if (in != null) { try { in.close(); } catch (IOException e) { logger.error("Failed to close input stream for " + modelNodeRef); } } } }
M2Model model = M2Model.createModel(namespacePair.getSecond() + QName.NAMESPACE_PREFIX + modelName); model.createNamespace(namespacePair.getFirst(), namespacePair.getSecond()); model.setDescription(desc); model.setAuthor("John Doe");
try M2Model model = M2Model.createModel(is); modelName = model.getName();