Java Code Examples for org.alfresco.repo.dictionary.M2Model#createModel()

The following examples show how to use org.alfresco.repo.dictionary.M2Model#createModel() . 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: PerformanceNodeServiceTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Loads the test model required for building the node graphs
 */
public static DictionaryService loadModel(ApplicationContext applicationContext)
{
    DictionaryDAO dictionaryDao = (DictionaryDAO) applicationContext.getBean("dictionaryDAO");
    
    // load the system model
    ClassLoader cl = PerformanceNodeServiceTest.class.getClassLoader();
    InputStream modelStream = cl.getResourceAsStream("alfresco/model/contentModel.xml");
    assertNotNull(modelStream);
    M2Model model = M2Model.createModel(modelStream);
    dictionaryDao.putModel(model);
    
    // load the test model
    modelStream = cl.getResourceAsStream("org/alfresco/repo/node/BaseNodeServiceTest_model.xml");
    assertNotNull(modelStream);
    model = M2Model.createModel(modelStream);
    dictionaryDao.putModel(model);
    
    DictionaryComponent dictionary = new DictionaryComponent();
    dictionary.setDictionaryDAO(dictionaryDao);
    
    return dictionary;
}
 
Example 2
Source File: NodeRefPropertyMethodInterceptorTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Before
public void before() throws Exception
{
    mlAwareNodeService = (NodeService) applicationContext.getBean("mlAwareNodeService");
    nodeService = (NodeService) applicationContext.getBean("nodeService");
    dictionaryDAO = (DictionaryDAO) applicationContext.getBean("dictionaryDAO");

    authenticationComponent = (AuthenticationComponent) applicationContext.getBean("authenticationComponent");

    authenticationComponent.setSystemUserAsCurrentUser();

    ClassLoader cl = BaseNodeServiceTest.class.getClassLoader();
    InputStream modelStream = cl.getResourceAsStream("org/alfresco/repo/node/NodeRefTestModel.xml");
    assertNotNull(modelStream);
    M2Model model = M2Model.createModel(modelStream);
    dictionaryDAO.putModel(model);

    StoreRef storeRef = nodeService.createStore(StoreRef.PROTOCOL_WORKSPACE, "Test_" + System.currentTimeMillis());
    rootNodeRef = nodeService.getRootNode(storeRef);

}
 
Example 3
Source File: DbNodeServiceImplPropagationTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Loads the test model required for building the node graphs
 */
public static DictionaryService loadModel(ApplicationContext applicationContext)
{
    DictionaryDAO dictionaryDao = (DictionaryDAO) applicationContext.getBean("dictionaryDAO");
    // load the system model
    ClassLoader cl = BaseNodeServiceTest.class.getClassLoader();
    InputStream modelStream = cl.getResourceAsStream("alfresco/model/contentModel.xml");
    assertNotNull(modelStream);
    M2Model model = M2Model.createModel(modelStream);
    dictionaryDao.putModel(model);
    // load the test model
    modelStream = cl.getResourceAsStream("org/alfresco/repo/node/BaseNodeServiceTest_model.xml");
    assertNotNull(modelStream);
    model = M2Model.createModel(modelStream);
    dictionaryDao.putModel(model);
    
    DictionaryComponent dictionary = new DictionaryComponent();
    dictionary.setDictionaryDAO(dictionaryDao);
    // done
    return dictionary;
}
 
Example 4
Source File: BaseNodeServiceTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Loads the test model required for building the node graphs
 */
public static DictionaryService loadModel(ApplicationContext applicationContext)
{
    DictionaryDAO dictionaryDao = (DictionaryDAO) applicationContext.getBean("dictionaryDAO");
    // load the system model
    ClassLoader cl = BaseNodeServiceTest.class.getClassLoader();
    InputStream modelStream = cl.getResourceAsStream("alfresco/model/contentModel.xml");
    assertNotNull(modelStream);
    M2Model model = M2Model.createModel(modelStream);
    dictionaryDao.putModel(model);
    // load the test model
    modelStream = cl.getResourceAsStream("org/alfresco/repo/node/BaseNodeServiceTest_model.xml");
    assertNotNull(modelStream);
    model = M2Model.createModel(modelStream);
    dictionaryDao.putModel(model);
    
    DictionaryComponent dictionary = new DictionaryComponent();
    dictionary.setDictionaryDAO(dictionaryDao);
    // done
    return dictionary;
}
 
Example 5
Source File: TemporaryModels.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
public QName loadModel(InputStream modelStream) 
{
    try
    {
        final M2Model model = M2Model.createModel(modelStream);
        
        return loadModel(model);
    }
    catch(DictionaryException e)
    {
        throw new DictionaryException("Could not import model", e);
    }
	
}
 
Example 6
Source File: PerformanceNodeServiceTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
protected void setUp() throws Exception
{
    applicationContext = ApplicationContextHelper.getApplicationContext();
    DictionaryDAO dictionaryDao = (DictionaryDAO) applicationContext.getBean("dictionaryDAO");
    
    // load the system model
    ClassLoader cl = PerformanceNodeServiceTest.class.getClassLoader();
    InputStream modelStream = cl.getResourceAsStream("alfresco/model/contentModel.xml");
    assertNotNull(modelStream);
    M2Model model = M2Model.createModel(modelStream);
    dictionaryDao.putModel(model);
    
    // load the test model
    modelStream = cl.getResourceAsStream("org/alfresco/repo/node/BaseNodeServiceTest_model.xml");
    assertNotNull(modelStream);
    model = M2Model.createModel(modelStream);
    dictionaryDao.putModel(model);
    
    DictionaryComponent dictionary = new DictionaryComponent();
    dictionary.setDictionaryDAO(dictionaryDao);
    dictionaryService = loadModel(applicationContext);
    
    nodeService = (NodeService) applicationContext.getBean("nodeService");
    txnService = (TransactionService) applicationContext.getBean("transactionComponent");
    contentService = (ContentService) applicationContext.getBean("contentService");
    
    // create a first store directly
    RetryingTransactionCallback<NodeRef> createStoreWork = new RetryingTransactionCallback<NodeRef>()
    {
        public NodeRef execute()
        {
            StoreRef storeRef = nodeService.createStore(
                    StoreRef.PROTOCOL_WORKSPACE,
                    "Test_" + System.nanoTime());
            return nodeService.getRootNode(storeRef);
        }
    };
    rootNodeRef = txnService.getRetryingTransactionHelper().doInTransaction(createStoreWork);
}
 
Example 7
Source File: DbNodeServiceImplPropagationTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Before
public void before() throws Exception
{
    txnService = (TransactionService) applicationContext.getBean("transactionComponent");
    nodeDAO = (NodeDAO) applicationContext.getBean("nodeDAO");
    dialect = (Dialect) applicationContext.getBean("dialect");
    nodeService = (NodeService) applicationContext.getBean("dbNodeService");
    
    authenticationComponent = (AuthenticationComponent) applicationContext.getBean("authenticationComponent");
    
    authenticationComponent.setSystemUserAsCurrentUser();
    
    DictionaryDAO dictionaryDao = (DictionaryDAO) applicationContext.getBean("dictionaryDAO");
    // load the system model
    ClassLoader cl = BaseNodeServiceTest.class.getClassLoader();
    InputStream modelStream = cl.getResourceAsStream("alfresco/model/contentModel.xml");
    assertNotNull(modelStream);
    M2Model model = M2Model.createModel(modelStream);
    dictionaryDao.putModel(model);
    // load the test model
    modelStream = cl.getResourceAsStream("org/alfresco/repo/node/BaseNodeServiceTest_model.xml");
    assertNotNull(modelStream);
    model = M2Model.createModel(modelStream);
    dictionaryDao.putModel(model);
    
    DictionaryComponent dictionary = new DictionaryComponent();
    dictionary.setDictionaryDAO(dictionaryDao);
    dictionaryService = loadModel(applicationContext);
    
    restartAuditableTxn();
}
 
Example 8
Source File: ConcurrentNodeServiceSearchTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
protected void setUp() throws Exception
{
    ctx = ApplicationContextHelper.getApplicationContext();
    DictionaryDAO dictionaryDao = (DictionaryDAO) ctx.getBean("dictionaryDAO");
    // load the system model
    ClassLoader cl = BaseNodeServiceTest.class.getClassLoader();
    InputStream modelStream = cl.getResourceAsStream("alfresco/model/systemModel.xml");
    assertNotNull(modelStream);
    M2Model model = M2Model.createModel(modelStream);
    dictionaryDao.putModel(model);
    // load the test model
    modelStream = cl.getResourceAsStream("org/alfresco/repo/node/BaseNodeServiceTest_model.xml");
    assertNotNull(modelStream);
    model = M2Model.createModel(modelStream);
    dictionaryDao.putModel(model);

    nodeService = (NodeService) ctx.getBean("dbNodeService");
    transactionService = (TransactionService) ctx.getBean("transactionComponent");
    this.authenticationComponent = (AuthenticationComponent) ctx.getBean("authenticationComponent");

    this.authenticationComponent.setSystemUserAsCurrentUser();

    // create a first store directly
    UserTransaction tx = transactionService.getUserTransaction();
    tx.begin();
    StoreRef storeRef = nodeService.createStore(StoreRef.PROTOCOL_WORKSPACE, "Test_" + System.currentTimeMillis());
    rootNodeRef = nodeService.getRootNode(storeRef);
    tx.commit();
}
 
Example 9
Source File: RhinoScriptTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
protected void setUp() throws Exception
{
    super.setUp();
    ctx = ApplicationContextHelper.getApplicationContext();
    transactionService = (TransactionService)ctx.getBean("transactionComponent");
    contentService = (ContentService)ctx.getBean("contentService");
    nodeService = (NodeService)ctx.getBean("nodeService");
    scriptService = (ScriptService)ctx.getBean("scriptService");
    serviceRegistry = (ServiceRegistry)ctx.getBean("ServiceRegistry");
    
    this.authenticationComponent = (AuthenticationComponent)ctx.getBean("authenticationComponent");
    this.authenticationComponent.setSystemUserAsCurrentUser();
    
    DictionaryDAO dictionaryDao = (DictionaryDAO)ctx.getBean("dictionaryDAO");
    
    // load the system model
    ClassLoader cl = BaseNodeServiceTest.class.getClassLoader();
    InputStream modelStream = cl.getResourceAsStream("alfresco/model/contentModel.xml");
    assertNotNull(modelStream);
    M2Model model = M2Model.createModel(modelStream);
    dictionaryDao.putModel(model);
    
    // load the test model
    modelStream = cl.getResourceAsStream("org/alfresco/repo/node/BaseNodeServiceTest_model.xml");
    assertNotNull(modelStream);
    model = M2Model.createModel(modelStream);
    dictionaryDao.putModel(model);
    
    DictionaryComponent dictionary = new DictionaryComponent();
    dictionary.setDictionaryDAO(dictionaryDao);
    BaseNodeServiceTest.loadModel(ctx);
}
 
Example 10
Source File: SOLRAPIClient.java    From SearchServices with GNU Lesser General Public License v3.0 5 votes vote down vote up
public AlfrescoModel getModel(String coreName, QName modelName) throws AuthenticationException, IOException
{
    // If the model is new to the SOLR side the prefix will be unknown so we can not generate prefixes for the request!
    // Always use the full QName with explicit URI
    StringBuilder url = new StringBuilder(GET_MODEL);

    URLCodec encoder = new URLCodec();
    // must send the long name as we may not have the prefix registered
    url.append("?modelQName=").append(encoder.encode(modelName.toString(), "UTF-8"));
    
    GetRequest req = new GetRequest(url.toString());

    Response response = null;
    try
    {
        response = repositoryHttpClient.sendRequest(req);
        if(response.getStatus() != HttpStatus.SC_OK)
        {
            throw new AlfrescoRuntimeException(coreName + " GetModel return status is " + response.getStatus());
        }

        return new AlfrescoModel(M2Model.createModel(response.getContentAsStream()),
                Long.valueOf(response.getHeader(CHECKSUM_HEADER)));
    }
    finally
    {
        if(response != null)
        {
            response.release();
        }
    }
}
 
Example 11
Source File: CustomModelImportTest.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 4 votes vote down vote up
public void testInvalidNumberOfZipEntries() throws Exception
{
    long timestamp = System.currentTimeMillis();
    String modelName = getClass().getSimpleName() + timestamp;
    String prefix = "prefix" + timestamp;
    String uri = "uriNamespace" + timestamp;

    // Model one
    M2Model modelOne = M2Model.createModel(prefix + QName.NAMESPACE_PREFIX + modelName);
    modelOne.createNamespace(uri, prefix);
    modelOne.setDescription("Model 1");
    ByteArrayOutputStream xml = new ByteArrayOutputStream();
    modelOne.toXML(xml);
    ZipEntryContext contextOne = new ZipEntryContext(modelName + ".xml", xml.toByteArray());

    // Model two
    modelName += "two";
    prefix += "two";
    uri += "two";
    M2Model modelTwo = M2Model.createModel(prefix + QName.NAMESPACE_PREFIX + modelName);
    modelTwo.createNamespace(uri, prefix);
    modelTwo.setDescription("Model 2");
    xml = new ByteArrayOutputStream();
    modelTwo.toXML(xml);
    ZipEntryContext contextTwo = new ZipEntryContext(modelName + ".xml", xml.toByteArray());

    // Model three
    modelName += "three";
    prefix += "three";
    uri += "three";
    M2Model modelThree = M2Model.createModel(prefix + QName.NAMESPACE_PREFIX + modelName);
    modelThree.createNamespace(uri, prefix);
    modelThree.setDescription("Model 3");
    xml = new ByteArrayOutputStream();
    modelThree.toXML(xml);
    ZipEntryContext contextThree = new ZipEntryContext(modelName + ".xml", xml.toByteArray());

    File zipFile = createZip(contextOne, contextTwo, contextThree);

    PostRequest postRequest = buildMultipartPostRequest(zipFile);
    sendRequest(postRequest, 400); // more than two zip entries
}
 
Example 12
Source File: CustomModelsImpl.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Converts the given {@code org.alfresco.rest.api.model.CustomModel}
 * object, a collection of {@code org.alfresco.rest.api.model.CustomType}
 * objects, a collection of
 * {@code org.alfresco.rest.api.model.CustomAspect} objects, and a collection of
 * {@code org.alfresco.rest.api.model.CustomModelConstraint} objects into a {@link M2Model} object
 * 
 * @param customModel the custom model
 * @param types the custom types
 * @param aspects the custom aspects
 * @param constraints the custom constraints
 * @return {@link M2Model} object
 */
private M2Model convertToM2Model(CustomModel customModel, Collection<CustomType> types, Collection<CustomAspect> aspects, Collection<CustomModelConstraint> constraints)
{
    validateBasicModelInput(customModel);

    Set<Pair<String, String>> namespacesToImport = new LinkedHashSet<>();

    final String namespacePrefix = customModel.getNamespacePrefix();
    final String namespaceURI = customModel.getNamespaceUri();
    // Construct the model name
    final String name = constructName(customModel.getName(), namespacePrefix);

    M2Model model = M2Model.createModel(name);
    model.createNamespace(namespaceURI, namespacePrefix);
    model.setDescription(customModel.getDescription());
    String author = customModel.getAuthor();
    if (author == null)
    {
        author = getCurrentUserFullName();
    }
    model.setAuthor(author);

    // Types
    if(types != null)
    {
        for(CustomType type : types)
        {
           validateName(type.getName(), TYPE_NAME_NULL_ERR);
           M2Type m2Type = model.createType(constructName(type.getName(), namespacePrefix));
           m2Type.setDescription(type.getDescription());
           m2Type.setTitle(type.getTitle());
           setParentName(m2Type, type.getParentName(), namespacesToImport, namespacePrefix);
           setM2Properties(m2Type, type.getProperties(), namespacePrefix, namespacesToImport);
        }
    }

    // Aspects
    if(aspects != null)
    {
        for(CustomAspect aspect : aspects)
        {
            validateName(aspect.getName(), ASPECT_NAME_NULL_ERR);
            M2Aspect m2Aspect = model.createAspect(constructName(aspect.getName(), namespacePrefix));
            m2Aspect.setDescription(aspect.getDescription());
            m2Aspect.setTitle(aspect.getTitle());
            setParentName(m2Aspect, aspect.getParentName(), namespacesToImport, namespacePrefix);
            setM2Properties(m2Aspect, aspect.getProperties(), namespacePrefix, namespacesToImport);
        }
    }

    // Constraints
    if(constraints != null)
    {
        for (CustomModelConstraint constraint : constraints)
        {
            validateName(constraint.getName(), CONSTRAINT_NAME_NULL_ERR);
            final String constraintName = constructName(constraint.getName(), namespacePrefix);
            M2Constraint m2Constraint = model.createConstraint(constraintName, constraint.getType());
            // Set title, desc and parameters
            setConstraintOtherData(constraint, m2Constraint, null);
        }
    }

    // Add imports
    for (Pair<String, String> uriPrefix : namespacesToImport)
    {
        // Don't import the already defined namespace
        if (!namespaceURI.equals(uriPrefix.getFirst()))
        {
            model.createImport(uriPrefix.getFirst(), uriPrefix.getSecond());
        }
    }

    return model;
}
 
Example 13
Source File: BaseNodeServiceTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Before
public void before()
{
    metadataEncryptor = (MetadataEncryptor) applicationContext.getBean("metadataEncryptor");
    
    transactionService = (TransactionService) applicationContext.getBean("transactionComponent");
    retryingTransactionHelper = (RetryingTransactionHelper) applicationContext.getBean("retryingTransactionHelper");
    policyComponent = (PolicyComponent) applicationContext.getBean("policyComponent");
    authenticationComponent = (AuthenticationComponent) applicationContext.getBean("authenticationComponent");
    contentService = (ContentService) applicationContext.getBean("contentService");
    
    authenticationComponent.setSystemUserAsCurrentUser();
    
    DictionaryDAO dictionaryDao = (DictionaryDAO) applicationContext.getBean("dictionaryDAO");
    // load the system model
    ClassLoader cl = BaseNodeServiceTest.class.getClassLoader();
    InputStream modelStream = cl.getResourceAsStream("alfresco/model/contentModel.xml");
    assertNotNull(modelStream);
    M2Model model = M2Model.createModel(modelStream);
    dictionaryDao.putModel(model);
    // load the test model
    modelStream = cl.getResourceAsStream("org/alfresco/repo/node/BaseNodeServiceTest_model.xml");
    assertNotNull(modelStream);
    model = M2Model.createModel(modelStream);
    dictionaryDao.putModel(model);
    
    DictionaryComponent dictionary = new DictionaryComponent();
    dictionary.setDictionaryDAO(dictionaryDao);
    dictionaryService = loadModel(applicationContext);
    
    nodeService = getNodeService();
    
    // create a first store directly
    StoreRef storeRef = nodeService.createStore(
            StoreRef.PROTOCOL_WORKSPACE,
            "Test_" + System.currentTimeMillis());
    rootNodeRef = nodeService.getRootNode(storeRef);
    
    StoreRef catStoreRef = nodeService.createStore(
            StoreRef.PROTOCOL_WORKSPACE,
            "Test_cat_" + System.currentTimeMillis());
    NodeRef catRootNodeRef = nodeService.getRootNode(catStoreRef);
    
    cat = nodeService.createNode(catRootNodeRef, ContentModel.ASSOC_CHILDREN, QName.createQName("{namespace}cat"), ContentModel.TYPE_CATEGORY).getChildRef();
    
    // downgrade integrity checks
    IntegrityChecker.setWarnInTransaction();
}
 
Example 14
Source File: CopyServiceImplTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Creates the test model used by the tests
 */
private void createTestModel()
{
    M2Model model = M2Model.createModel("test:nodeoperations");
    model.createNamespace(TEST_TYPE_NAMESPACE, "test");
    model.createImport(NamespaceService.DICTIONARY_MODEL_1_0_URI, NamespaceService.DICTIONARY_MODEL_PREFIX);
    model.createImport(NamespaceService.SYSTEM_MODEL_1_0_URI, NamespaceService.SYSTEM_MODEL_PREFIX);
    model.createImport(NamespaceService.CONTENT_MODEL_1_0_URI, NamespaceService.CONTENT_MODEL_PREFIX);

    M2Type testType = model.createType("test:" + TEST_TYPE_QNAME.getLocalName());
    testType.setParentName("cm:" + ContentModel.TYPE_CONTENT.getLocalName());
    
    M2Property prop1 = testType.createProperty("test:" + PROP1_QNAME_MANDATORY.getLocalName());
    prop1.setMandatory(true);
    prop1.setType("d:" + DataTypeDefinition.TEXT.getLocalName());
    prop1.setMultiValued(false);
    
    M2Property prop2 = testType.createProperty("test:" + PROP2_QNAME_OPTIONAL.getLocalName());
    prop2.setMandatory(false);
    prop2.setType("d:" + DataTypeDefinition.TEXT.getLocalName());
    prop2.setMandatory(false);
    
    M2Property propNodeRef = testType.createProperty("test:" + PROP_QNAME_MY_NODE_REF.getLocalName());
    propNodeRef.setMandatory(false);
    propNodeRef.setType("d:" + DataTypeDefinition.NODE_REF.getLocalName());
    propNodeRef.setMandatory(false);
    
    M2Property propAnyNodeRef = testType.createProperty("test:" + PROP_QNAME_MY_ANY.getLocalName());
    propAnyNodeRef.setMandatory(false);
    propAnyNodeRef.setType("d:" + DataTypeDefinition.ANY.getLocalName());
    propAnyNodeRef.setMandatory(false);
    
    M2ChildAssociation childAssoc = testType.createChildAssociation("test:" + TEST_CHILD_ASSOC_TYPE_QNAME.getLocalName());
    childAssoc.setTargetClassName("sys:base");
    childAssoc.setTargetMandatory(false);
    
    M2Association assoc = testType.createAssociation("test:" + TEST_ASSOC_TYPE_QNAME.getLocalName());
    assoc.setTargetClassName("sys:base");
    assoc.setTargetMandatory(false);
    
    M2Aspect testAspect = model.createAspect("test:" + TEST_ASPECT_QNAME.getLocalName());
    
    M2Property prop3 = testAspect.createProperty("test:" + PROP3_QNAME_MANDATORY.getLocalName());
    prop3.setMandatory(true);
    prop3.setType("d:" + DataTypeDefinition.TEXT.getLocalName());
    prop3.setMultiValued(false);
    
    M2Property prop4 = testAspect.createProperty("test:" + PROP4_QNAME_OPTIONAL.getLocalName());
    prop4.setMandatory(false);
    prop4.setType("d:" + DataTypeDefinition.TEXT.getLocalName());
    prop4.setMultiValued(false);

    M2Aspect testMandatoryAspect = model.createAspect("test:" + TEST_MANDATORY_ASPECT_QNAME.getLocalName());
    M2Property prop5 = testMandatoryAspect.createProperty("test:" + PROP5_QNAME_MANDATORY.getLocalName());
    prop5.setType("d:" + DataTypeDefinition.TEXT.getLocalName());
    prop5.setMandatory(true);

    testType.addMandatoryAspect("test:" + TEST_MANDATORY_ASPECT_QNAME.getLocalName());
    
    dictionaryDAO.putModel(model);
}
 
Example 15
Source File: TemplateServiceImplTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Before
public void setUp() throws Exception
{
    transactionService = (TransactionService)applicationContext.getBean("transactionComponent");
    nodeService = (NodeService)applicationContext.getBean("nodeService");
    templateService = (TemplateService)applicationContext.getBean("templateService");
    serviceRegistry = (ServiceRegistry)applicationContext.getBean("ServiceRegistry");

    this.authenticationComponent = (AuthenticationComponent)applicationContext.getBean("authenticationComponent");
    this.authenticationComponent.setSystemUserAsCurrentUser();

    DictionaryDAO dictionaryDao = (DictionaryDAO)applicationContext.getBean("dictionaryDAO");

    // load the system model
    ClassLoader cl = BaseNodeServiceTest.class.getClassLoader();
    InputStream modelStream = cl.getResourceAsStream("alfresco/model/contentModel.xml");
    assertNotNull(modelStream);
    M2Model model = M2Model.createModel(modelStream);
    dictionaryDao.putModel(model);

    // load the test model
    modelStream = cl.getResourceAsStream("org/alfresco/repo/node/BaseNodeServiceTest_model.xml");
    assertNotNull(modelStream);
    model = M2Model.createModel(modelStream);
    dictionaryDao.putModel(model);

    DictionaryComponent dictionary = new DictionaryComponent();
    dictionary.setDictionaryDAO(dictionaryDao);
    BaseNodeServiceTest.loadModel(applicationContext);

    transactionService.getRetryingTransactionHelper().doInTransaction(
            new RetryingTransactionCallback<Object>()
            {
                @SuppressWarnings("unchecked")
                public Object execute() throws Exception
                {
                    StoreRef store = nodeService.createStore(StoreRef.PROTOCOL_WORKSPACE, "template_" + System.currentTimeMillis());
                    root_node = nodeService.getRootNode(store);
                    Map<QName, Serializable> properties = new HashMap<QName, Serializable>(11);
                    properties.put(ContentModel.PROP_NAME, (Serializable) "subFolder");
                    NodeRef subFolderRef = nodeService.createNode(
                            root_node,
                            ContentModel.ASSOC_CHILDREN,
                            QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI,QName.createValidLocalName("subFolder")),
                            ContentModel.TYPE_FOLDER,
                            properties).getChildRef();
                    properties.put(ContentModel.PROP_NAME, (Serializable) "subSubFolder");
                    NodeRef subSubFolderRef =nodeService.createNode(
                            subFolderRef,
                            ContentModel.ASSOC_CONTAINS,
                            QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI,QName.createValidLocalName("subSubFolder")),
                            ContentModel.TYPE_FOLDER,
                            properties).getChildRef();
                    properties.put(ContentModel.PROP_NAME, (Serializable) "subSubSubFolder");
                    nodeService.createNode(
                            subSubFolderRef,
                            ContentModel.ASSOC_CONTAINS,
                            QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI,QName.createValidLocalName("subSubSubFolder")),
                            ContentModel.TYPE_FOLDER,
                            properties);
                    BaseNodeServiceTest.buildNodeGraph(nodeService, root_node);
                    return null;
                }
            });
}
 
Example 16
Source File: CustomModelImportTest.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 4 votes vote down vote up
public void testUploadModel_UnsupportedModelElements() throws Exception
{
    // Note: here we only test a couple of not-supported model elements to check for the correct status code.
    // This test should be removed when we implement the required support

    long timestamp = System.currentTimeMillis();
    final String modelName = getClass().getSimpleName() + timestamp;
    final String prefix = "prefix"+timestamp;
    final String uri = "uriNamespace"+timestamp;
    final String aspectName = prefix + QName.NAMESPACE_PREFIX + "testAspec";
    final String typeName = prefix + QName.NAMESPACE_PREFIX + "testType";
    final String associationName = prefix + QName.NAMESPACE_PREFIX + "testAssociation";

    M2Model model = M2Model.createModel(prefix + QName.NAMESPACE_PREFIX + modelName);
    model.createNamespace(uri, prefix);
    model.setAuthor("John Doe");
    model.createAspect(aspectName);
    model.createImport(NamespaceService.CONTENT_MODEL_1_0_URI, NamespaceService.CONTENT_MODEL_PREFIX);

    M2Type type = model.createType(typeName);
    // Add 'association' not supported yet.
    M2Association association = type.createAssociation(associationName);
    association.setSourceMandatory(false);
    association.setSourceMany(false);
    association.setTargetMandatory(false);
    association.setTargetClassName("cm:content");

    ByteArrayOutputStream xml = new ByteArrayOutputStream();
    model.toXML(xml);
    ZipEntryContext context = new ZipEntryContext(modelName + ".xml", xml.toByteArray());
    File zipFile = createZip(context);

    PostRequest postRequest = buildMultipartPostRequest(zipFile);
    sendRequest(postRequest, 409); // <associations> element is not supported yet

    type.removeAssociation(associationName);
    // Add 'mandatory-aspect' not supported yet.
    type.addMandatoryAspect(aspectName);
    xml = new ByteArrayOutputStream();
    model.toXML(xml);
    context = new ZipEntryContext(modelName + ".xml", xml.toByteArray());
    zipFile = createZip(context);

    postRequest = buildMultipartPostRequest(zipFile);
    sendRequest(postRequest, 409); // <mandatory-aspects> element is not supported yet
}
 
Example 17
Source File: IncompleteNodeTaggerTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
public void setUp() throws Exception
{
    ApplicationContext ctx = ApplicationContextHelper.getApplicationContext();
    DictionaryDAO dictionaryDao = (DictionaryDAO) ctx.getBean("dictionaryDAO");
    ClassLoader cl = BaseNodeServiceTest.class.getClassLoader();
    // load the test model
    InputStream modelStream = cl.getResourceAsStream("org/alfresco/repo/node/integrity/IntegrityTest_model.xml");
    assertNotNull(modelStream);
    M2Model model = M2Model.createModel(modelStream);
    dictionaryDao.putModel(model);

    tagger = (IncompleteNodeTagger) ctx.getBean("incompleteNodeTagger");

    serviceRegistry = (ServiceRegistry) ctx.getBean(ServiceRegistry.SERVICE_REGISTRY);
    nodeService = serviceRegistry.getNodeService();
    authenticationService = serviceRegistry.getAuthenticationService();
    permissionService = serviceRegistry.getPermissionService();
    this.authenticationComponent = (AuthenticationComponent) ctx.getBean("authenticationComponent");
    
    this.authenticationComponent.setSystemUserAsCurrentUser();
    
    String user = getName();
    if (!authenticationService.authenticationExists(user))
    {
        authenticationService.createAuthentication(user, user.toCharArray());
    }
    
    // begin a transaction
    TransactionService transactionService = serviceRegistry.getTransactionService();
    txn = transactionService.getUserTransaction();
    txn.begin();
    StoreRef storeRef = new StoreRef(StoreRef.PROTOCOL_WORKSPACE, getName());
    if (!nodeService.exists(storeRef))
    {
        nodeService.createStore(storeRef.getProtocol(), storeRef.getIdentifier());
        rootNodeRef = nodeService.getRootNode(storeRef);
        // Make sure our user can do everything
        permissionService.setPermission(rootNodeRef, user, PermissionService.ALL_PERMISSIONS, true);
    }
    else
    {
        rootNodeRef = nodeService.getRootNode(storeRef);
    }
    
    properties = new PropertyMap();
    properties.put(IntegrityTest.TEST_PROP_TEXT_C, "abc");
    
    // Authenticate as a test-specific user
    authenticationComponent.setCurrentUser(user);
}
 
Example 18
Source File: IntegrityTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
public void setUp() throws Exception
{
    ApplicationContext ctx = ApplicationContextHelper.getApplicationContext();
    DictionaryDAO dictionaryDao = (DictionaryDAO) ctx.getBean("dictionaryDAO");
    ClassLoader cl = BaseNodeServiceTest.class.getClassLoader();
    // load the test model
    InputStream modelStream = cl.getResourceAsStream("org/alfresco/repo/node/integrity/IntegrityTest_model.xml");
    assertNotNull(modelStream);
    M2Model model = M2Model.createModel(modelStream);
    dictionaryDao.putModel(model);

    integrityChecker = (IntegrityChecker) ctx.getBean("integrityChecker");
    integrityChecker.setEnabled(true);
    integrityChecker.setFailOnViolation(true);
    integrityChecker.setTraceOn(true);
    integrityChecker.setMaxErrorsPerTransaction(100);   // we want to count the correct number of errors
    
    MetadataEncryptor encryptor = (MetadataEncryptor) ctx.getBean("metadataEncryptor");

    serviceRegistry = (ServiceRegistry) ctx.getBean(ServiceRegistry.SERVICE_REGISTRY);
    nodeService = serviceRegistry.getNodeService();
    this.authenticationComponent = (AuthenticationComponent)ctx.getBean("authenticationComponent");
    
    this.authenticationComponent.setSystemUserAsCurrentUser();
    
    // begin a transaction
    TransactionService transactionService = serviceRegistry.getTransactionService();
    txn = transactionService.getUserTransaction();
    txn.begin();
    StoreRef storeRef = new StoreRef(StoreRef.PROTOCOL_WORKSPACE, getName());
    if (!nodeService.exists(storeRef))
    {
        nodeService.createStore(storeRef.getProtocol(), storeRef.getIdentifier());
    }
    rootNodeRef = nodeService.getRootNode(storeRef);
    
    allProperties = new PropertyMap();
    allProperties.put(TEST_PROP_TEXT_A, "ABC");
    allProperties.put(TEST_PROP_TEXT_B, "DEF");
    allProperties.put(TEST_PROP_INT_A, "123");
    allProperties.put(TEST_PROP_INT_B, "456");
    allProperties.put(TEST_PROP_ENCRYPTED_A, "ABC");
    allProperties.put(TEST_PROP_ENCRYPTED_B, "DEF");
    allProperties = encryptor.encrypt(allProperties);
}
 
Example 19
Source File: CustomModelUploadPost.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 4 votes vote down vote up
protected ImportResult processUpload(ZipFile zipFile, String filename) throws IOException
{
    if (zipFile.size() > 2)
    {
        throw new WebScriptException(Status.STATUS_BAD_REQUEST, "cmm.rest_api.model.import_invalid_zip_package");
    }

    CustomModel customModel = null;
    String shareExtModule = null;
    Enumeration<? extends ZipEntry> entries = zipFile.entries();
    while (entries.hasMoreElements())
    {
        ZipEntry entry = entries.nextElement();

        if (!entry.isDirectory())
        {
            final String entryName = entry.getName();
            try (InputStream input = new BufferedInputStream(zipFile.getInputStream(entry), BUFFER_SIZE))
            {
                if (!(entryName.endsWith(CustomModelServiceImpl.SHARE_EXT_MODULE_SUFFIX)) && customModel == null)
                {
                    try
                    {
                        M2Model m2Model = M2Model.createModel(input);
                        customModel = importModel(m2Model);
                    }
                    catch (DictionaryException ex)
                    {
                        if (shareExtModule == null)
                        {
                            // Get the input stream again, as the zip file doesn't support reset.
                            try (InputStream moduleInputStream = new BufferedInputStream(zipFile.getInputStream(entry), BUFFER_SIZE))
                            {
                                shareExtModule = getExtensionModule(moduleInputStream, entryName);
                            }

                            if (shareExtModule == null)
                            {
                                throw new WebScriptException(Status.STATUS_BAD_REQUEST, "cmm.rest_api.model.import_invalid_zip_entry_format", new Object[] { entryName });
                            }
                        }
                        else
                        {
                            throw new WebScriptException(Status.STATUS_BAD_REQUEST, "cmm.rest_api.model.import_invalid_model_entry", new Object[] { entryName });
                        }
                    }
                }
                else
                {
                    shareExtModule = getExtensionModule(input, entryName);
                    if (shareExtModule == null)
                    {
                        throw new WebScriptException(Status.STATUS_BAD_REQUEST, "cmm.rest_api.model.import_invalid_ext_module_entry", new Object[] { entryName });
                    }
                }
            }
        }
    }

    return new ImportResult(customModel, shareExtModule);
}
 
Example 20
Source File: UpdateRepoEventIT.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Test
public void testUpdateNodeTypeWithCustomType()
{
    String modelName = "testModel" + System.currentTimeMillis();
    String modelDescription = "testModel description";
    Pair<String, String> namespacePair = getNamespacePair();

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

    String typeName = "testType";
    M2Type m2Type = model.createType(namespacePair.getSecond() + QName.NAMESPACE_PREFIX + typeName);
    m2Type.setTitle("Test type title");

    // Create active model
    CustomModelDefinition modelDefinition =
            retryingTransactionHelper.doInTransaction(() -> customModelService.createCustomModel(model, true));

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

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

    // node.Created event should be generated for the model
    RepoEvent<EventData<NodeResource>> resultRepoEvent = getRepoEvent(1);
    assertEquals("Wrong repo event type.", EventType.NODE_CREATED.getType(), resultRepoEvent.getType());
    NodeResource nodeResource = getNodeResource(resultRepoEvent);
    assertEquals("Incorrect node type was found", "cm:dictionaryModel", nodeResource.getNodeType());

    final NodeRef nodeRef = createNode(ContentModel.TYPE_CONTENT);
    // old node's type
    assertEquals(ContentModel.TYPE_CONTENT, nodeService.getType(nodeRef));

    // node.Created event should be generated
    resultRepoEvent = getRepoEvent(2);
    assertEquals("Wrong repo event type.", EventType.NODE_CREATED.getType(), resultRepoEvent.getType());
    nodeResource = getNodeResource(resultRepoEvent);
    assertEquals("cm:content node type was not found", "cm:content", nodeResource.getNodeType());

    QName typeQName = QName.createQName("{" + namespacePair.getFirst()+ "}" + typeName);
    retryingTransactionHelper.doInTransaction(() -> {
        nodeService.setType(nodeRef, typeQName);

        // new node's type
        assertEquals(typeQName, nodeService.getType(nodeRef));
        return null;
    });

    // we should have 3 events, node.Created for the model, node.Created for the node and node.Updated
    checkNumOfEvents(3);

    resultRepoEvent = getRepoEvent(3);
    assertEquals("Wrong repo event type.", EventType.NODE_UPDATED.getType(), resultRepoEvent.getType());
    nodeResource = getNodeResource(resultRepoEvent);
    assertEquals("Incorrect node type was found", namespacePair.getSecond() + QName.NAMESPACE_PREFIX + typeName, nodeResource.getNodeType());

    NodeResource resourceBefore = getNodeResourceBefore(3);
    assertEquals("Incorrect node type was found", "cm:content", resourceBefore.getNodeType());
    assertNull(resourceBefore.getId());
    assertNull(resourceBefore.getContent());
    assertNull(resourceBefore.isFile());
    assertNull(resourceBefore.isFolder());
    assertNull(resourceBefore.getModifiedByUser());
    assertNull(resourceBefore.getCreatedAt());
    assertNull(resourceBefore.getCreatedByUser());
    assertNull(resourceBefore.getProperties());
    assertNull(resourceBefore.getAspectNames());
    assertNull(resourceBefore.getPrimaryHierarchy());

}